shifts assigns, const

This commit is contained in:
Nopana_Eliyaan 2025-09-09 15:58:17 +02:00
parent 2c392f848b
commit 39bb76b12c
5 changed files with 144 additions and 13 deletions

View file

@ -317,6 +317,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) {
PreprocVar {
c.cmp_var_reg(var_object as PreprocVar, reg, config)
}
ConstVar {
c.cmp_var_reg(var_object as ConstVar, reg, config)
}
}
}
LocalVar {
@ -342,6 +345,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -366,6 +372,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) {
PreprocVar {
c.cmp_var(var_object as PreprocVar, val, config)
}
ConstVar {
c.cmp_var(var_object as ConstVar, val, config)
}
}
}
LocalVar {
@ -391,6 +400,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -416,6 +428,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) {
PreprocVar {
c.dec_var(var_object as PreprocVar, config)
}
ConstVar {
c.dec_var(var_object as ConstVar, config)
}
}
}
LocalVar {
@ -441,6 +456,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -467,6 +485,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) {
PreprocVar {
c.inc_var(var_object as PreprocVar, config)
}
ConstVar {
c.inc_var(var_object as ConstVar, config)
}
}
}
LocalVar {
@ -515,6 +536,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -715,6 +739,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) {
PreprocVar {
c.mov_reg_to_var(var_object as PreprocVar, reg, config)
}
ConstVar {
c.mov_reg_to_var(var_object as ConstVar, reg, config)
}
}
}
LocalVar {
@ -818,6 +845,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -841,6 +871,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) {
PreprocVar {
c.mov_int_to_var(var_object as PreprocVar, integer, config)
}
ConstVar {
c.mov_int_to_var(var_object as ConstVar, integer, config)
}
}
}
LocalVar {
@ -909,6 +942,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -961,6 +997,9 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) {
PreprocVar {
c.mov_var_to_reg(reg, var_object as PreprocVar, config)
}
ConstVar {
c.mov_var_to_reg(reg, var_object as ConstVar, config)
}
}
}
LocalVar {
@ -1045,6 +1084,11 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.expr(var.expr)
c.mov_reg(reg, c.main_reg())
c.g.println('; mov ${reg} const:`${var.name}`')
}
}
}
@ -2220,13 +2264,17 @@ fn (mut c Amd64) assign_var(var IdentVar, raw_type ast.Type) {
PreprocVar {
c.mov_reg_to_var(var as PreprocVar, Amd64Register.rax)
}
ConstVar {
c.mov_reg_to_var(var as ConstVar, Amd64Register.rax)
}
}
} else {
c.g.n_error('${@LOCATION} error assigning type ${typ} with size ${size}: ${info}')
c.g.n_error('${@LOCATION} error assigning var ${var} type ${typ} with size ${size}: ${info}')
}
}
// Could be nice to have left as an expr to be able to take all int assigns
// TODO: Will have a problem if the literal is bigger than max_i64: needs u64
fn (mut c Amd64) assign_ident_int_lit(node ast.AssignStmt, i i32, int_lit ast.IntegerLiteral, left ast.Ident) {
match node.op {
.plus_assign {
@ -2261,7 +2309,25 @@ fn (mut c Amd64) assign_ident_int_lit(node ast.AssignStmt, i i32, int_lit ast.In
c.allocate_var(left.name, 8, i64(int_lit.val.int()))
}
.assign {
c.mov(Amd64Register.rax, i32(int_lit.val.int()))
c.mov64(Amd64Register.rax, i64(int_lit.val.int()))
c.mov_reg_to_var(left, Amd64Register.rax)
}
.left_shift_assign {
c.mov_var_to_reg(Amd64Register.rax, left)
c.mov64(Amd64Register.rcx, i64(int_lit.val.int()))
c.shl_reg(.rax, .rcx)
c.mov_reg_to_var(left, Amd64Register.rax)
}
.right_shift_assign {
c.mov_var_to_reg(Amd64Register.rax, left)
c.mov64(Amd64Register.rcx, i64(int_lit.val.int()))
c.sar_reg(.rax, .rcx)
c.mov_reg_to_var(left, Amd64Register.rax)
}
.unsigned_right_shift_assign {
c.mov_var_to_reg(Amd64Register.rax, left)
c.mov64(Amd64Register.rcx, i64(int_lit.val.int()))
c.shr_reg(.rax, .rcx)
c.mov_reg_to_var(left, Amd64Register.rax)
}
else {
@ -3847,6 +3913,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) {
PreprocVar {
c.init_struct(var_object as PreprocVar, init)
}
ConstVar {
c.init_struct(var_object as ConstVar, init)
}
}
}
LocalVar {
@ -3895,6 +3964,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -3949,6 +4021,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) {
PreprocVar {
c.init_array(var_object as PreprocVar, node)
}
ConstVar {
c.init_array(var_object as ConstVar, node)
}
}
}
LocalVar {
@ -3968,6 +4043,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) {
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -4270,6 +4348,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf
PreprocVar {
c.mov_ssereg_to_var(var_object as PreprocVar, reg, config)
}
ConstVar {
c.mov_ssereg_to_var(var_object as ConstVar, reg, config)
}
}
}
LocalVar {
@ -4301,6 +4382,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}
@ -4326,6 +4410,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf
PreprocVar {
c.mov_var_to_ssereg(reg, var_object as PreprocVar, config)
}
ConstVar {
c.mov_var_to_ssereg(reg, var_object as ConstVar, config)
}
}
}
LocalVar {
@ -4357,6 +4444,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf
PreprocVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
ConstVar {
c.g.n_error('${@LOCATION} unsupported var type ${var}')
}
}
}

View file

@ -55,13 +55,19 @@ const blacklist = {
'string.last_index': true
'string.last_index_u8': false
'string.contains_u8': false
'malloc_noscan': true
'malloc_noscan': false
'malloc': false
'is_nil': false
'memdup': false
'vcalloc': false
'vmemcpy': false
'eprint': false
'eprintln': false
'_write_buf_to_fd': false
'_writeln_to_fd': false
'_memory_panic': true
'_memory_panic': false
'panic': false
'vcurrent_hash': false
}
const windows_blacklist = {

View file

@ -32,7 +32,7 @@ fn (mut g Gen) comptime_is_truthy(cond ast.Expr) bool {
return !g.comptime_is_truthy(cond.right)
}
else {
g.n_error('Compile time infix expr `${cond}` is not handled by the native backed.')
g.n_error('${@LOCATION} Compile time infix expr `${cond}` is not handled by the native backed.')
}
}
}
@ -58,7 +58,7 @@ fn (mut g Gen) comptime_is_truthy(cond ast.Expr) bool {
return true
}
else {
g.n_error('Compile time infix expr `${cond}` is not handled by the native backend.')
g.n_error('${@LOCATION} Compile time infix expr `${cond}` is not handled by the native backend.')
}
}
}
@ -66,11 +66,11 @@ fn (mut g Gen) comptime_is_truthy(cond ast.Expr) bool {
return g.comptime_ident(cond.name, false)
}
ast.ComptimeCall {
g.n_error('Comptime calls are not implemented')
g.n_error('${@LOCATION} Comptime calls are not implemented')
}
else {
// should be unreachable
g.n_error('Compile time conditional `${cond}` is not handled by the native backend.')
g.n_error('${@LOCATION} Compile time conditional `${cond}` is not handled by the native backend.')
}
}
return false
@ -221,7 +221,7 @@ fn (mut g Gen) comptime_ident(name string, is_comptime_option bool) bool {
|| (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {
true
} else {
g.n_error('Unhandled os ifdef name "${name}".')
g.n_error('${@LOCATION} Unhandled os ifdef name "${name}".')
false
}
}

View file

@ -54,8 +54,14 @@ fn (mut g Gen) expr(node ast.Expr) {
PreprocVar {
g.preproc_var_ident(var)
}
else {
g.n_error('${@LOCATION} Unsupported variable kind')
GlobalVar {
g.global_var_ident(node, var)
}
Register {
g.n_error('${@LOCATION} Unsupported variable kind ${var}')
}
ConstVar {
g.const_var_ident(node, var)
}
}
}
@ -193,6 +199,22 @@ fn (mut g Gen) local_var_ident(ident ast.Ident, var LocalVar) {
}
}
fn (mut g Gen) global_var_ident(ident ast.Ident, var GlobalVar) {
if g.is_register_type(var.typ) {
g.code_gen.mov_var_to_reg(g.code_gen.main_reg(), ident)
} else {
g.n_error('${@LOCATION} Unsupported variable type ${ident} ${var}')
}
}
fn (mut g Gen) const_var_ident(ident ast.Ident, var ConstVar) {
if g.is_register_type(var.typ) {
g.code_gen.mov_var_to_reg(g.code_gen.main_reg(), ident)
} else {
g.n_error('${@LOCATION} Unsupported variable type ${ident} ${var}')
}
}
fn (mut g Gen) extern_var_ident(var ExternVar) {
if g.pref.os == .linux {
main_reg := g.code_gen.main_reg()

View file

@ -261,6 +261,12 @@ struct GlobalVar {
typ ast.Type
}
struct ConstVar {
name string
expr ast.Expr
typ ast.Type
}
@[params]
struct VarConfig {
pub:
@ -268,9 +274,9 @@ pub:
typ ast.Type // type of the value you want to process e.g. struct fields.
}
type Var = GlobalVar | ExternVar | LocalVar | PreprocVar | ast.Ident
type Var = GlobalVar | ExternVar | LocalVar | PreprocVar | ConstVar | ast.Ident
type IdentVar = GlobalVar | ExternVar | LocalVar | Register | PreprocVar
type IdentVar = GlobalVar | ExternVar | LocalVar | Register | PreprocVar | ConstVar
enum JumpOp {
je
@ -322,6 +328,13 @@ fn (mut g Gen) get_var_from_ident(ident ast.Ident) IdentVar {
name: obj.name
}
}
ast.ConstField {
return ConstVar{
name: obj.name
expr: (obj as ast.ConstField).expr
typ: obj.typ
}
}
else {
g.n_error('${@LOCATION} unsupported variable type type:${obj} name:${ident.name}')
}