diff --git a/hw3/bin/backend.ml b/hw3/bin/backend.ml index 3de3992..bc634a8 100644 --- a/hw3/bin/backend.ml +++ b/hw3/bin/backend.ml @@ -87,6 +87,10 @@ let lookup m x = List.assoc x m TODO: The section below still reads like giberish, mabye reading the rest of the code/tests will help. + Update: This makes sense now, whenever you're adding two numbers together + or performing any operations on them, you need them to be in registers. + All locals in this compilation strategy are in the stack, so when + operating on them we need to move them to registers... One strategy for compiling instruction operands is to use a designated register (or registers) for holding the values being @@ -309,7 +313,25 @@ let arg_loc (n : int) : operand = - see the discussion about locals *) let stack_layout (args : uid list) ((block, lbled_blocks) : cfg) : layout = - failwith "stack_layout not implemented" + (* offset is in quad unitsf. + We start at 0, but rbp already contains a values (rbp of the calling + function) so we don't count it as a valid slot, consumers should decrement + it before using it. + *) + let open Int64 in + let next_offset = + let offset = ref 0 in + let next_offset' _ = + offset := !offset - 1; + !offset + in + next_offset' + in + let op_from_offset o = Ind3 (Lit (mul 8L (of_int o)), Rbp) in + let layout_args = + List.fold_left (fun acc arg -> (arg, op_from_offset (next_offset ())) :: acc) [] args + in + layout_args ;; (* The code for the entry-point of a function must do several things: