refactor codegen emitting slot indices

This commit is contained in:
tali 2023-12-02 15:36:53 -05:00
parent 0460e6b646
commit 62cf27b0c7
2 changed files with 16 additions and 9 deletions

View File

@ -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

View File

@ -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;