cgen: emit a const variable instead of a define
parent
89d64b21ea
commit
a7cfcab95d
|
@ -3,7 +3,7 @@ module constants
|
|||
// Commonly used constants across RNGs - some taken from "Numerical Recipes".
|
||||
pub const (
|
||||
lower_mask = u64(0x00000000FFFFFFFF)
|
||||
max_u32 = 0xFFFFFFFF
|
||||
max_u32 = u32(0xFFFFFFFF)
|
||||
max_u64 = u64(0xFFFFFFFFFFFFFFFF)
|
||||
max_u32_as_f32 = f32(max_u32) + 1
|
||||
max_u64_as_f64 = f64(max_u64) + 1
|
||||
|
|
|
@ -4211,7 +4211,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||
if field.is_simple_define_const() {
|
||||
// "Simple" expressions are not going to need multiple statements,
|
||||
// only the ones which are inited later, so it's safe to use expr_string
|
||||
g.const_decl_simple_define(name, g.expr_string(field_expr))
|
||||
mut styp := g.typ(field.typ)
|
||||
g.const_decl_simple_define(styp, name, g.expr_string(field_expr))
|
||||
} else {
|
||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
||||
}
|
||||
|
@ -4246,7 +4247,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp
|
|||
// with -cstrict. Add checker errors for overflows instead,
|
||||
// so V can catch them earlier, instead of relying on the
|
||||
// C compiler for that.
|
||||
g.const_decl_simple_define(name, ct_value.str())
|
||||
g.const_decl_simple_define(styp, name, ct_value.str())
|
||||
return true
|
||||
}
|
||||
if typ == ast.u64_type {
|
||||
|
@ -4305,16 +4306,18 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp
|
|||
}
|
||||
|
||||
fn (mut g Gen) const_decl_write_precomputed(styp string, cname string, ct_value string) {
|
||||
g.definitions.writeln('$styp $cname = $ct_value; // precomputed')
|
||||
g.definitions.writeln('const $styp $cname = $ct_value; // precomputed')
|
||||
}
|
||||
|
||||
fn (mut g Gen) const_decl_simple_define(name string, val string) {
|
||||
// Simple expressions should use a #define
|
||||
// so that we don't pollute the binary with unnecessary global vars
|
||||
// Do not do this when building a module, otherwise the consts
|
||||
// will not be accessible.
|
||||
g.definitions.write_string('#define _const_$name ')
|
||||
g.definitions.writeln(val)
|
||||
fn (mut g Gen) const_decl_simple_define(typ string, name string, val string) {
|
||||
$if true {
|
||||
g.definitions.write_string('#define _const_$name ')
|
||||
g.definitions.writeln(val)
|
||||
} $else {
|
||||
g.definitions.write_string('const $typ _const_$name = ')
|
||||
g.definitions.write_string(val)
|
||||
g.definitions.writeln(';')
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ ast.Type, unwrap_option bool) {
|
||||
|
|
Loading…
Reference in New Issue