Skip to content

Commit

Permalink
Start work on IR
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Apr 10, 2024
1 parent 922332f commit 26fd7f1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/basic_block.ml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/basic_block.mli
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions lib/ir.ml
Original file line number Diff line number Diff line change
@@ -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
Empty file added lib/x86.ml
Empty file.
Empty file added lib/x86.mli
Empty file.

0 comments on commit 26fd7f1

Please sign in to comment.