Implement and test arg_loc

Signed-off-by: Mariano Uvalle <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-02-14 11:48:44 -08:00
parent ea32e468a3
commit bbd068f936
2 changed files with 30 additions and 5 deletions

View file

@ -287,7 +287,18 @@ let compile_lbl_block fn lbl ctxt blk : elem =
[ NOTE: the first six arguments are numbered 0 .. 5 ] [ NOTE: the first six arguments are numbered 0 .. 5 ]
*) *)
let arg_loc (n : int) : operand = failwith "arg_loc not implemented" let arg_loc (n : int) : operand =
let n64 = Int64.of_int n in
let rbp_offset = Int64.mul 8L (Int64.sub n64 4L) in
match n with
| 0 -> Reg Rdi
| 1 -> Reg Rsi
| 2 -> Reg Rdx
| 3 -> Reg Rcx
| 4 -> Reg R08
| 5 -> Reg R09
| _ -> Ind3 (Lit rbp_offset, Rbp)
;;
(* We suggest that you create a helper function that computes the (* We suggest that you create a helper function that computes the
stack layout for a given function declaration. stack layout for a given function declaration.

View file

@ -1,12 +1,26 @@
open Util.Assert open Util.Assert
open X86
open Ll
module Backend = Llbackend.Backend
module Driver = Llbackend.Driver
open Llbackend.Backend
open Gradedtests open Gradedtests
(* These tests are provided by you -- they will be graded manually *) (* These tests are provided by you -- they will be graded manually *)
(* You should also add additional test cases here to help you *) (* You should also add additional test cases here to help you *)
(* debug your program. *) (* debug your program. *)
let provided_tests : suite = [ let arg_loc_tests =
[ "arg_loc_0", assert_eqf (fun () -> arg_loc 0) (Reg Rdi)
] ; "arg_loc_1", assert_eqf (fun () -> arg_loc 1) (Reg Rsi)
; "arg_loc_2", assert_eqf (fun () -> arg_loc 2) (Reg Rdx)
; "arg_loc_3", assert_eqf (fun () -> arg_loc 3) (Reg Rcx)
; "arg_loc_4", assert_eqf (fun () -> arg_loc 4) (Reg R08)
; "arg_loc_5", assert_eqf (fun () -> arg_loc 5) (Reg R09)
; "arg_loc_6", assert_eqf (fun () -> arg_loc 6) (Ind3 (Lit 16L, Rbp))
; "arg_loc_100", assert_eqf (fun () -> arg_loc 100) (Ind3 (Lit 768L, Rbp))
]
;;
let provided_tests : suite = [ Test ("arg_loc_tests", arg_loc_tests) ]