diff --git a/lib/basic_block.ml b/lib/basic_block.ml new file mode 100644 index 0000000..3617d8c --- /dev/null +++ b/lib/basic_block.ml @@ -0,0 +1,20 @@ +type id = int +type ir_array = Ir.t BatDynArray.t + +type t = { + id : id; + contents : ir_array; +} + +let id_gen = + let id = ref 0 in + fun () -> + let result = !id in + id := !id + 1; + result + +let make () = { id = id_gen (); contents = BatDynArray.create () } +let id_of basic_block = basic_block.id +let label_of basic_block = ".L" ^ string_of_int basic_block.id +let add basic_block ir = BatDynArray.add basic_block.contents ir +let to_list basic_block = BatDynArray.to_list basic_block.contents diff --git a/lib/basic_block.mli b/lib/basic_block.mli new file mode 100644 index 0000000..28329ac --- /dev/null +++ b/lib/basic_block.mli @@ -0,0 +1,22 @@ +(** [id] is unique identifier of the basic block. *) +type id + +(** [t] is a basic block.. *) +type t + +(** [make ()] is a new basic block. *) +val make : unit -> t + +(** [id_of basic_block] is the identifier of [basic_block]. *) +val id_of : t -> id + +(** [label_of basic_block] is the label of [basic_block] suitable for emission + in an object file as a symbol. *) +val label_of : t -> string + +(** [add basic_block ir] adds [ir] to the end of [basic_block]. *) +val add : t -> Ir.t -> unit + +(** [to_list basic_block] are the IR operations in [basic_block] in order as a + list. *) +val to_list : t -> Ir.t list diff --git a/lib/ir.ml b/lib/ir.ml new file mode 100644 index 0000000..c3e0aac --- /dev/null +++ b/lib/ir.ml @@ -0,0 +1,28 @@ +module Variable : sig + (** The type of an IR variable. *) + type t + + (** [to_string var] is [var] as a string. *) + val to_string : t -> string +end = struct + open Util + + type t = int + + let to_string = string_of_int >> ( ^ ) "i" +end + +type constant = int + +(* todo *) +type t = + | Add of Variable.t * Variable.t + | IAdd of Variable.t * constant + | Store of Variable.t * Variable.t + | IStore of Variable.t * constant + | Load of Variable.t * Variable.t + | ILoad of Variable.t * constant + | Param of Variable.t + | IParam of constant + | Jump of Basic_block.id + | Call of Basic_block.id diff --git a/lib/x86.ml b/lib/x86.ml new file mode 100644 index 0000000..e69de29 diff --git a/lib/x86.mli b/lib/x86.mli new file mode 100644 index 0000000..e69de29