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".
|
// Commonly used constants across RNGs - some taken from "Numerical Recipes".
|
||||||
pub const (
|
pub const (
|
||||||
lower_mask = u64(0x00000000FFFFFFFF)
|
lower_mask = u64(0x00000000FFFFFFFF)
|
||||||
max_u32 = 0xFFFFFFFF
|
max_u32 = u32(0xFFFFFFFF)
|
||||||
max_u64 = u64(0xFFFFFFFFFFFFFFFF)
|
max_u64 = u64(0xFFFFFFFFFFFFFFFF)
|
||||||
max_u32_as_f32 = f32(max_u32) + 1
|
max_u32_as_f32 = f32(max_u32) + 1
|
||||||
max_u64_as_f64 = f64(max_u64) + 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() {
|
if field.is_simple_define_const() {
|
||||||
// "Simple" expressions are not going to need multiple statements,
|
// "Simple" expressions are not going to need multiple statements,
|
||||||
// only the ones which are inited later, so it's safe to use expr_string
|
// 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 {
|
} else {
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
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,
|
// with -cstrict. Add checker errors for overflows instead,
|
||||||
// so V can catch them earlier, instead of relying on the
|
// so V can catch them earlier, instead of relying on the
|
||||||
// C compiler for that.
|
// 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
|
return true
|
||||||
}
|
}
|
||||||
if typ == ast.u64_type {
|
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) {
|
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) {
|
fn (mut g Gen) const_decl_simple_define(typ string, name string, val string) {
|
||||||
// Simple expressions should use a #define
|
$if true {
|
||||||
// so that we don't pollute the binary with unnecessary global vars
|
g.definitions.write_string('#define _const_$name ')
|
||||||
// Do not do this when building a module, otherwise the consts
|
g.definitions.writeln(val)
|
||||||
// will not be accessible.
|
} $else {
|
||||||
g.definitions.write_string('#define _const_$name ')
|
g.definitions.write_string('const $typ _const_$name = ')
|
||||||
g.definitions.writeln(val)
|
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) {
|
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