From 55089bde911334111d9f4b1d0c1ab8980cb284e0 Mon Sep 17 00:00:00 2001 From: jmug Date: Thu, 30 Jan 2025 21:20:30 -0800 Subject: [PATCH] Implement map_addr functions Signed-off-by: jmug --- hw2/bin/simulator.ml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hw2/bin/simulator.ml b/hw2/bin/simulator.ml index a6a71eb..673a10b 100644 --- a/hw2/bin/simulator.ml +++ b/hw2/bin/simulator.ml @@ -179,14 +179,23 @@ let interp_cnd { fo; fs; fz } : cnd -> bool = function (* Maps an X86lite address into Some OCaml array index, or None if the address is not within the legal address space. *) -let map_addr (addr : quad) : int option = failwith "map_addr not implemented" +let map_addr (addr : quad) : int option = + if addr >=. mem_bot && addr <. mem_top then + Some (Int64.to_int (addr -. mem_bot)) + else + None + (* Your simulator should raise this exception if it tries to read from or store to an address not within the valid address space. *) exception X86lite_segfault (* Raise X86lite_segfault when addr is invalid. *) -let map_addr_segfault (addr : quad) : int = failwith "map_addr_segfault not implemented" +let map_addr_segfault (addr : quad) : int = + let idx = map_addr addr in + match idx with + | Some i -> i + | None -> raise X86lite_segfault (* Simulates one step of the machine: - fetch the instruction at %rip