(* 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 Common open Utils type t = Common.exa let exa_counter = ref 0 let next_name (name : string) : string = let ret = !exa_counter in exa_counter := !exa_counter + 1; name ^ string_of_int ret let show_code (exa : t) : string = exa.code |> Dynarray.to_list |> List.map OpCode.show |> String.concat "\n" let length (exa : t) = Dynarray.length exa.code [@@inline always] let inc_instruction (exa : t) : unit = let new_ip = exa.ip + 1 in if new_ip <= length exa then exa.ip <- new_ip; () let get_instruction (exa : t) : OpCode.t option = if exa.ip >= length exa then None else Some (Dynarray.get exa.code exa.ip) let is_m_instruction (exa : t) : bool = match get_instruction exa with | None -> false | Some op -> OpCode.is_m_op op let get_label (exa : t) (label : string) : int = StringMap.find exa.labels label let create (name : string) (host : host) (code : code) : t = { name; dead = false; x = Int 0; t = Int 0; f = None; f_pos = 0; mode = GLOBAL; code = Dynarray.of_list code.ops; labels = code.labels; ip = 0; host; waiting = false; } let copy (exa : t) : t = let name = next_name exa.name in { name; dead = false; x = Int 0; t = Int 0; f = None; f_pos = 0; mode = exa.mode; code = Dynarray.copy exa.code; labels = StringMap.copy exa.labels; ip = 0; host = exa.host; waiting = false; }