refactor codegen emitting slot indices
This commit is contained in:
parent
0460e6b646
commit
62cf27b0c7
|
@ -5,7 +5,7 @@ module Env = Map.Make (String)
|
||||||
|
|
||||||
type binding = {
|
type binding = {
|
||||||
self : Code.regidx;
|
self : Code.regidx;
|
||||||
slot : Value.slotidx;
|
elem : Value.elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
let compile modl =
|
let compile modl =
|
||||||
|
@ -23,8 +23,9 @@ let compile modl =
|
||||||
| Ast.Path (Var name) -> (
|
| Ast.Path (Var name) -> (
|
||||||
match Env.find name env with
|
match Env.find name env with
|
||||||
| exception Not_found -> Fmt.failwith "unbound: %S" name
|
| exception Not_found -> Fmt.failwith "unbound: %S" name
|
||||||
| { self; slot } ->
|
| { self; elem } ->
|
||||||
emit_mov sp (Code.cst_of_int slot);
|
let idx = Code.cst (Value.of_elem elem) in
|
||||||
|
emit_mov sp idx;
|
||||||
emit (GET (self, sp));
|
emit (GET (self, sp));
|
||||||
Reg sp)
|
Reg sp)
|
||||||
| Ast.Path (Ele (obj, ele)) ->
|
| Ast.Path (Ele (obj, ele)) ->
|
||||||
|
@ -81,9 +82,9 @@ let compile modl =
|
||||||
(fun (env, n) -> function
|
(fun (env, n) -> function
|
||||||
| Ast.Item_fun (_, _, _) | Ast.Item_exp _ -> env, n
|
| Ast.Item_fun (_, _, _) | Ast.Item_exp _ -> env, n
|
||||||
| Ast.Item_obj (name, _) | Ast.Item_val (name, _) ->
|
| Ast.Item_obj (name, _) | Ast.Item_val (name, _) ->
|
||||||
let slot = n in
|
let elem = Value.Field n in
|
||||||
let env = Env.add name { self; slot } env in
|
let env = Env.add name { self; elem } env in
|
||||||
Hashtbl.add elems name (Value.Field slot);
|
Hashtbl.add elems name elem;
|
||||||
env, n + 1)
|
env, n + 1)
|
||||||
(env, 0)
|
(env, 0)
|
||||||
items
|
items
|
||||||
|
@ -97,8 +98,9 @@ let compile modl =
|
||||||
(* emit constructor, compile val fields, and get result of final expression *)
|
(* emit constructor, compile val fields, and get result of final expression *)
|
||||||
emit (CON (self, vtable));
|
emit (CON (self, vtable));
|
||||||
let emit_set name rhs =
|
let emit_set name rhs =
|
||||||
let slot = (Env.find name env).slot in
|
let { elem; _ } = Env.find name env in
|
||||||
emit_mov sp (Code.cst_of_int slot);
|
let idx = Code.cst (Value.of_elem elem) in
|
||||||
|
emit_mov sp idx;
|
||||||
emit_mov (sp + 1) rhs;
|
emit_mov (sp + 1) rhs;
|
||||||
emit (SET (self, sp))
|
emit (SET (self, sp))
|
||||||
in
|
in
|
||||||
|
|
|
@ -7,7 +7,12 @@ type operand =
|
||||||
| Cst_int of int64
|
| Cst_int of int64
|
||||||
| Reg of regidx
|
| 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 = {
|
type basic_block = {
|
||||||
mutable ins_builder : ins list;
|
mutable ins_builder : ins list;
|
||||||
|
|
Loading…
Reference in New Issue