From 62cf27b0c7271c4f073c1b787823590d676f25d1 Mon Sep 17 00:00:00 2001 From: tali Date: Sat, 2 Dec 2023 15:36:53 -0500 Subject: [PATCH] refactor codegen emitting slot indices --- lib/compile/bcc.ml | 18 ++++++++++-------- lib/runtime/code.ml | 7 ++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/compile/bcc.ml b/lib/compile/bcc.ml index e4ac101..90ab252 100644 --- a/lib/compile/bcc.ml +++ b/lib/compile/bcc.ml @@ -5,7 +5,7 @@ module Env = Map.Make (String) type binding = { self : Code.regidx; - slot : Value.slotidx; + elem : Value.elem; } let compile modl = @@ -23,8 +23,9 @@ let compile modl = | Ast.Path (Var name) -> ( match Env.find name env with | exception Not_found -> Fmt.failwith "unbound: %S" name - | { self; slot } -> - emit_mov sp (Code.cst_of_int slot); + | { self; elem } -> + let idx = Code.cst (Value.of_elem elem) in + emit_mov sp idx; emit (GET (self, sp)); Reg sp) | Ast.Path (Ele (obj, ele)) -> @@ -81,9 +82,9 @@ let compile modl = (fun (env, n) -> function | Ast.Item_fun (_, _, _) | Ast.Item_exp _ -> env, n | Ast.Item_obj (name, _) | Ast.Item_val (name, _) -> - let slot = n in - let env = Env.add name { self; slot } env in - Hashtbl.add elems name (Value.Field slot); + let elem = Value.Field n in + let env = Env.add name { self; elem } env in + Hashtbl.add elems name elem; env, n + 1) (env, 0) items @@ -97,8 +98,9 @@ let compile modl = (* emit constructor, compile val fields, and get result of final expression *) emit (CON (self, vtable)); let emit_set name rhs = - let slot = (Env.find name env).slot in - emit_mov sp (Code.cst_of_int slot); + let { elem; _ } = Env.find name env in + let idx = Code.cst (Value.of_elem elem) in + emit_mov sp idx; emit_mov (sp + 1) rhs; emit (SET (self, sp)) in diff --git a/lib/runtime/code.ml b/lib/runtime/code.ml index b9acf43..81218a8 100644 --- a/lib/runtime/code.ml +++ b/lib/runtime/code.ml @@ -7,7 +7,12 @@ type operand = | Cst_int of int64 | Reg of regidx -let cst_of_int i = Cst_int (Int64.of_int i) +let cst = function + | Value.Nil -> Cst_nil + | Value.True -> Cst_true + | Value.False -> Cst_false + | Value.Int i -> Cst_int i + | _ -> invalid_arg "value cannot be converted to constant operand" type basic_block = { mutable ins_builder : ins list;