(* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. *) open Utils let simpleInstruction name = Printf.printf "%s" name let regInstruction name r = Printf.printf "%s %s" name (Register.show r) let rnInstruction name rn = Printf.printf "%s %s" name (Register.show_r_n rn) let labelInstruction name label = Printf.printf "%s %s" name label let srcDestInstruction name src dest = let left = Register.show_r_n src in let right = Register.show dest in Printf.printf "%s %s %s" name left right let mathInstruction name src i dest = let left = Register.show_r_n src in let right = Register.show_r_n i in Printf.printf "%s %s %s %s" name left right (Register.show dest) let testInstruction name left func right = let left = Register.show_r_n left in let right = Register.show_r_n right in Printf.printf "%s %s %s %s" name left func right let disassembleInstruction (exa : Exa.t) (op : OpCode.t) (offset : int) : unit = let loc = if exa.ip == offset then ">" else " " in Printf.printf "%s%04d %s" loc offset (OpCode.show op); print_newline () let disassembleFile ?(pos = -1) (file : File.t) : unit = Printf.printf "__ %s __\n" file.name; print_string "[ "; let len = File.length file in for i = 0 to len - 1 do let s = Value.quote (Dynarray.get file.contents i) in if i = pos then Printf.printf "[%s]; " s else Printf.printf "%s; " s done; if pos = len then print_string "[]"; print_string "]"; print_newline () let disassembleExa (exa : Exa.t) : unit = if not exa.dead then ( Printf.printf "== %s (%s) ==\n" exa.name exa.host.name; Printf.printf "X: %s\n" (Value.show exa.x); Printf.printf "T: %s\n" (Value.show exa.t); Printf.printf "F: %s\n" (match exa.f with | None -> "None" | Some f -> f.name); Dynarray.iteri (fun idx op -> disassembleInstruction exa op idx) exa.code; if exa.ip == Dynarray.length exa.code then print_string ">\n"; match exa.f with | None -> () | Some f -> disassembleFile ~pos:exa.f_pos f); () let disassembleHost (host : Host.t) : unit = Printf.printf "Host: %s\n" host.name; print_string "Objects: "; host.grid |> StringMap.to_seq |> Seq.map (fun (name, _obj) -> name) |> List.of_seq |> String.concat ", " |> print_string; print_newline (); ()