diff --git a/vlib/v/gen/native/amd64.v b/vlib/v/gen/native/amd64.v index fb341725fa..24d90aab73 100644 --- a/vlib/v/gen/native/amd64.v +++ b/vlib/v/gen/native/amd64.v @@ -314,6 +314,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) { ExternVar { c.cmp_var_reg(var_object as ExternVar, reg, config) } + PreprocVar { + c.cmp_var_reg(var_object as PreprocVar, reg, config) + } } } LocalVar { @@ -336,6 +339,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -357,6 +363,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) { ExternVar { c.cmp_var(var_object as ExternVar, val, config) } + PreprocVar { + c.cmp_var(var_object as PreprocVar, val, config) + } } } LocalVar { @@ -379,6 +388,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -401,6 +413,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) { ExternVar { c.dec_var(var_object as ExternVar, config) } + PreprocVar { + c.dec_var(var_object as PreprocVar, config) + } } } LocalVar { @@ -423,6 +438,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -446,6 +464,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) { ExternVar { c.inc_var(var_object as ExternVar, config) } + PreprocVar { + c.inc_var(var_object as PreprocVar, config) + } } } LocalVar { @@ -492,6 +513,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -689,6 +713,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) { ExternVar { c.mov_reg_to_var(var_object as ExternVar, reg, config) } + PreprocVar { + c.mov_reg_to_var(var_object as PreprocVar, reg, config) + } } } LocalVar { @@ -773,6 +800,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -793,6 +823,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) { ExternVar { c.mov_int_to_var(var_object as ExternVar, integer, config) } + PreprocVar { + c.mov_int_to_var(var_object as PreprocVar, integer, config) + } } } LocalVar { @@ -858,6 +891,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -907,6 +943,9 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) { ExternVar { c.mov_var_to_reg(reg, var_object as ExternVar, config) } + PreprocVar { + c.mov_var_to_reg(reg, var_object as PreprocVar, config) + } } } LocalVar { @@ -975,6 +1014,9 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -2147,6 +2189,9 @@ fn (mut c Amd64) assign_var(var IdentVar, raw_type ast.Type) { ExternVar { c.mov_reg_to_var(var as ExternVar, Amd64Register.rax) } + PreprocVar { + c.mov_reg_to_var(var as PreprocVar, Amd64Register.rax) + } } } else { c.g.n_error('${@LOCATION} error assigning type ${typ} with size ${size}: ${info}') @@ -3771,6 +3816,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) { ExternVar { c.init_struct(var_object as ExternVar, init) } + PreprocVar { + c.init_struct(var_object as PreprocVar, init) + } } } LocalVar { @@ -3816,6 +3864,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -3867,6 +3918,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) { ExternVar { c.init_array(var_object as ExternVar, node) } + PreprocVar { + c.init_array(var_object as PreprocVar, node) + } } } LocalVar { @@ -3883,6 +3937,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) { ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -4182,6 +4239,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf ExternVar { c.mov_ssereg_to_var(var_object as ExternVar, reg, config) } + PreprocVar { + c.mov_ssereg_to_var(var_object as PreprocVar, reg, config) + } } } LocalVar { @@ -4210,6 +4270,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } @@ -4232,6 +4295,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf ExternVar { c.mov_var_to_ssereg(reg, var_object as ExternVar, config) } + PreprocVar { + c.mov_var_to_ssereg(reg, var_object as PreprocVar, config) + } } } LocalVar { @@ -4260,6 +4326,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf ExternVar { c.g.n_error('${@LOCATION} unsupported var type ${var}') } + PreprocVar { + c.g.n_error('${@LOCATION} unsupported var type ${var}') + } } } diff --git a/vlib/v/gen/native/blacklist.v b/vlib/v/gen/native/blacklist.v index 48e2f4e87f..e9fecae189 100644 --- a/vlib/v/gen/native/blacklist.v +++ b/vlib/v/gen/native/blacklist.v @@ -37,7 +37,7 @@ const blacklist = { 'int_min': false 'flush_stdout': false 'flush_stderr': false - 'print_character': true + 'print_character': false 'u8.is_alnum': false 'u8.is_bin_digit': false 'u8.is_capital': false diff --git a/vlib/v/gen/native/expr.v b/vlib/v/gen/native/expr.v index a06fc14ecd..1b27506e48 100644 --- a/vlib/v/gen/native/expr.v +++ b/vlib/v/gen/native/expr.v @@ -51,6 +51,9 @@ fn (mut g Gen) expr(node ast.Expr) { ExternVar { g.extern_var_ident(var) } + PreprocVar { + g.preproc_var_ident(var) + } else { g.n_error('${@LOCATION} Unsupported variable kind') } @@ -203,6 +206,11 @@ fn (mut g Gen) extern_var_ident(var ExternVar) { } } +fn (mut g Gen) preproc_var_ident(var PreprocVar) { + main_reg := g.code_gen.main_reg() + g.code_gen.mov64(main_reg, var.val) +} + fn (mut g Gen) condition(expr ast.Expr, neg bool) i32 { g.println('; condition cjmp if ${neg}:') g.expr(expr) diff --git a/vlib/v/gen/native/gen.v b/vlib/v/gen/native/gen.v index 026622d5b1..884e2f3105 100644 --- a/vlib/v/gen/native/gen.v +++ b/vlib/v/gen/native/gen.v @@ -15,6 +15,10 @@ import v.eval import term import strconv +const c_preprocessed = { + 'C.EOF': -1 +} + @[heap; minify] pub struct Gen { out_name string @@ -243,6 +247,12 @@ struct ExternVar { name string } +struct PreprocVar { + typ ast.Type + name string + val i64 +} + struct GlobalVar {} @[params] @@ -252,9 +262,9 @@ pub: typ ast.Type // type of the value you want to process e.g. struct fields. } -type Var = GlobalVar | ExternVar | LocalVar | ast.Ident +type Var = GlobalVar | ExternVar | LocalVar | PreprocVar | ast.Ident -type IdentVar = GlobalVar | ExternVar | LocalVar | Register +type IdentVar = GlobalVar | ExternVar | LocalVar | Register | PreprocVar enum JumpOp { je @@ -281,6 +291,14 @@ fn (mut g Gen) get_var_from_ident(ident ast.Ident) IdentVar { if ident.name in g.extern_symbols { return ExternVar{ident.info.typ, ident.name} } + mut is_preprocessed := true + mut preprocessed_val := c_preprocessed[ident.name] or { + is_preprocessed = false + 0 + } + if is_preprocessed { + return PreprocVar{ident.info.typ, ident.name, preprocessed_val} + } mut obj := ident.obj if obj !in [ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister] { obj = ident.scope.find(ident.name) or { diff --git a/vlib/v/gen/native/tests/builtin.vv b/vlib/v/gen/native/tests/builtin.vv index 72d8f66748..68cae3d5a2 100644 --- a/vlib/v/gen/native/tests/builtin.vv +++ b/vlib/v/gen/native/tests/builtin.vv @@ -135,8 +135,6 @@ println(int_max(-32, 32)) println(int_min(32, -32)) println(int_min(-32, 32)) -//print_character(`a`) enable when C vars will work - // assert u8(`a`).ascii_str() == 'a' when ptr index will work // assert u8(`b`).ascii_str() != 'a' // assert u8(0x4F).hex() == '4F' when fixed array index will work diff --git a/vlib/v/gen/native/tests/linux.vv b/vlib/v/gen/native/tests/linux.vv index cb946c1483..be30e35b94 100644 --- a/vlib/v/gen/native/tests/linux.vv +++ b/vlib/v/gen/native/tests/linux.vv @@ -25,4 +25,5 @@ fn main() { test_c_extern_vars() flush_stderr() flush_stdout() + print_character(`a`) } diff --git a/vlib/v/gen/native/tests/linux.vv.out b/vlib/v/gen/native/tests/linux.vv.out index 3b4da7b210..791eea7e78 100644 --- a/vlib/v/gen/native/tests/linux.vv.out +++ b/vlib/v/gen/native/tests/linux.vv.out @@ -5,3 +5,4 @@ 2 99 abc +a