@@ -914,6 +914,8 @@ fn (mut c Amd64) mov_reg_to_var(var Var, reg Amd64Register, config VarConfig) {
914914 }
915915}
916916
917+ // does not support 64bits
918+ // TODO: remove and replace by a more general function (like mov_reg_to_var)
917919fn (mut c Amd64) cg_mov_int_to_var (var Var, integer i32 , config VarConfig) {
918920 match var {
919921 ast.Ident {
@@ -2410,7 +2412,7 @@ fn (mut c Amd64) assign_var(var IdentVar, raw_type ast.Type) {
24102412fn (mut c Amd64) assign_ident_int_lit (node ast.AssignStmt, i i32 , int_lit ast.IntegerLiteral, left ast.Ident) {
24112413 match node.op {
24122414 .decl_assign {
2413- c.cg_allocate_var (left.name, 8 , i64 (int_lit.val.int ()))
2415+ c.cg_allocate_stack_var (left.name, 8 , i64 (int_lit.val.int ()))
24142416 }
24152417 .assign {
24162418 c.mov64 (.rax, i64 (int_lit.val.int ()))
@@ -2518,7 +2520,7 @@ fn (mut c Amd64) mov_float_xmm0_var(reg Amd64Register, var_type ast.Type) {
25182520}
25192521
25202522fn (mut c Amd64) cg_create_string_struct (typ ast.Type, name string , str string ) i32 {
2521- dest := c.cg_allocate_var (name, c.g.get_type_size (typ), i64 (0 ))
2523+ dest := c.cg_allocate_stack_var (name, c.g.get_type_size (typ), i64 (0 ))
25222524 c.learel (.rax, c.g.allocate_string (str, 3 , .rel32 ))
25232525 c.cg_mov_reg_to_var (LocalVar{dest, ast.u64_ type_idx, name}, .reg0 )
25242526 offset := c.g.get_field_offset (typ, 'len' )
@@ -2547,7 +2549,7 @@ fn (mut c Amd64) assign_ident_right_expr(node ast.AssignStmt, i i32, right ast.E
25472549 match val {
25482550 Number {
25492551 if node.op == .decl_assign {
2550- c.cg_allocate_var (ident.name, enum_info.size, val)
2552+ c.cg_allocate_stack_var (ident.name, enum_info.size, val)
25512553 } else {
25522554 c.mov64 (.rax, val)
25532555 c.cg_mov_reg_to_var (ident, .reg0 )
@@ -2695,7 +2697,7 @@ fn (mut c Amd64) assign_ident_right_expr(node ast.AssignStmt, i i32, right ast.E
26952697 return
26962698 }
26972699 ast.AtExpr {
2698- dest := c.cg_allocate_var (name, 8 , i64 (0 ))
2700+ dest := c.cg_allocate_stack_var (name, 8 , i64 (0 ))
26992701 c.learel (.rsi, c.g.allocate_string (c.g.comptime_at (right), 3 , .rel32 ))
27002702 c.mov_reg_to_var (LocalVar{dest, ast.u64_ type_idx, name}, .rsi)
27012703 }
@@ -2814,7 +2816,7 @@ fn (mut c Amd64) assign_ident_right_expr(node ast.AssignStmt, i i32, right ast.E
28142816 /*
28152817 ast.IndexExpr {
28162818 // a := arr[0]
2817- offset := c.cg_allocate_var (name, c.g.get_sizeof_ident(ident), 0)
2819+ offset := c.cg_allocate_stack_var (name, c.g.get_sizeof_ident(ident), 0)
28182820 if c.g.pref.is_verbose {
28192821 println('infix assignment ${name} offset=${offset.hex2()}')
28202822 }
@@ -2843,43 +2845,6 @@ fn (mut c Amd64) assign_ident_right_expr(node ast.AssignStmt, i i32, right ast.E
28432845 }*/
28442846}
28452847
2846- fn (mut c Amd64) cg_gen_index_expr (node ast.IndexExpr) {
2847- if node.left_type.is_string () {
2848- c.g.expr (node.index)
2849- c.push (.rax)
2850-
2851- c.g.expr (node.left) // load address of string struct
2852- c.mov_deref (.rax, .rax, ast.u64_ type_idx) // load value of the str pointer
2853-
2854- c.pop (.rdx) // index
2855- c.add_reg (.rax, .rdx) // add the offset to the address
2856- } else if node.left_type.is_any_kind_of_pointer () {
2857- // load the pointer
2858- c.g.expr (node.left)
2859- c.mov_reg (.rcx, .rax)
2860- // add the index times the size (bytes) of the type
2861- c.g.expr (node.index)
2862- c.mov (.rbx, i32 (c.g.get_type_size (node.typ)))
2863- c.mul_reg_rax (.rbx)
2864- c.add_reg (.rax, .rcx)
2865- } else if node.is_array {
2866- // TODO: use functions from builtin instead (array.set, array.get...)
2867- c.g.expr (node.left)
2868- offset := c.g.get_field_offset (ast.array_type, 'data' )
2869- if offset != 0 {
2870- c.add (.rax, offset)
2871- }
2872- c.mov_deref (.rcx, .rax, ast.u64_ type)
2873- // add the index times the size (bytes) of the type
2874- c.g.expr (node.index)
2875- c.mov (.rbx, i32 (c.g.get_type_size (node.typ)))
2876- c.mul_reg_rax (.rbx)
2877- c.add_reg (.rax, .rcx)
2878- } else {
2879- c.g.n_error ('${@LOCATION } index expr: unhandled node type {node}' )
2880- }
2881- }
2882-
28832848// /!\ for div, mul, mod the left value should always be .rax
28842849fn (mut c Amd64) apply_op_int (left_value Amd64 Register, right_value Amd64 Register, op token.Kind) {
28852850 match op {
@@ -4044,7 +4009,7 @@ fn (mut c Amd64) cg_fn_decl(node ast.FnDecl) {
40444009 // define defer vars
40454010 for i in 0 .. node.defer_stmts.len {
40464011 name := '_defer${i }'
4047- c.cg_allocate_var (name, 8 , i64 (0 ))
4012+ c.cg_allocate_stack_var (name, 8 , i64 (0 ))
40484013 }
40494014 // body
40504015 c.g.stmts (node.stmts)
@@ -4084,14 +4049,14 @@ pub fn (mut c Amd64) cg_builtin_decl(builtin BuiltinFn) {
40844049 c.cg_leave ()
40854050}
40864051
4087- pub fn (mut c Amd64) allocate_var_two_step (name string , size i32 , initial_val Number) i32 {
4088- c.cg_allocate_var (name, size - 8 , i64 (0 ))
4089- return c.cg_allocate_var (name, 8 , initial_val)
4052+ pub fn (mut c Amd64) allocate_stack_var_two_step (name string , size i32 , initial_val Number) i32 {
4053+ c.cg_allocate_stack_var (name, size - 8 , i64 (0 ))
4054+ return c.cg_allocate_stack_var (name, 8 , initial_val)
40904055}
40914056
4092- pub fn (mut c Amd64) cg_allocate_var (name string , size i32 , initial_val Number) i32 {
4057+ pub fn (mut c Amd64) cg_allocate_stack_var (name string , size i32 , initial_val Number) i32 {
40934058 if size > 8 {
4094- return c.allocate_var_two_step (name, size, initial_val)
4059+ return c.allocate_stack_var_two_step (name, size, initial_val)
40954060 }
40964061
40974062 padding := (size - c.g.stack_var_pos % size) % size
@@ -4605,6 +4570,7 @@ fn (mut c Amd64) cg_gen_match_expr(expr ast.MatchExpr) {
46054570 for cond in branch.exprs {
46064571 match cond {
46074572 ast.RangeExpr {
4573+ // TODO: clean up
46084574 c.pop (.rdx)
46094575 c.g.expr (cond.low)
46104576 c.cmp_reg (.rax, .rdx)
@@ -5084,16 +5050,6 @@ fn (mut c Amd64) cg_gen_cast_expr(expr ast.CastExpr) {
50845050 }
50855051}
50865052
5087- fn (mut c Amd64) cg_cmp_to_stack_top (reg Register) {
5088- second_reg := if reg.amd64 () == Amd64 Register.rbx {
5089- Amd64 Register.rax
5090- } else {
5091- Amd64 Register.rbx
5092- }
5093- c.pop (second_reg)
5094- c.cmp_reg (second_reg, reg.amd64 ())
5095- }
5096-
50975053// Temporary!
50985054fn (mut c Amd64) adr (r Arm64 Register, delta i32 ) {
50995055 c.g.n_error ('`adr` instruction not supported with amd64' )
0 commit comments