From a7cfcab95d6aa9359a824620f77fa3b812aa8250 Mon Sep 17 00:00:00 2001 From: pancake Date: Sat, 9 Apr 2022 20:29:24 +0200 Subject: [PATCH] cgen: emit a const variable instead of a define --- vlib/rand/constants/constants.v | 2 +- vlib/v/gen/c/cgen.v | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/vlib/rand/constants/constants.v b/vlib/rand/constants/constants.v index c70d3e4895..371d54d739 100644 --- a/vlib/rand/constants/constants.v +++ b/vlib/rand/constants/constants.v @@ -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 diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index edcee66730..f0d55df891 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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) {