native: add a temporary special case for C.EOF (#24724)

This commit is contained in:
Eliyaan (Nopana) 2025-06-15 19:29:36 +02:00 committed by GitHub
parent 099b57f1d0
commit 959c11b7f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 100 additions and 5 deletions

View file

@ -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}')
}
}
}

View file

@ -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

View file

@ -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)

View file

@ -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 {

View file

@ -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

View file

@ -25,4 +25,5 @@ fn main() {
test_c_extern_vars()
flush_stderr()
flush_stdout()
print_character(`a`)
}

View file

@ -5,3 +5,4 @@
2
99
abc
a