Skip to content

Commit b504b3f

Browse files
committed
Begin actually folding constant items into operands at site of use.
1 parent 6cfeac1 commit b504b3f

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/boot/me/semant.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type data =
5050
| DATA_frame_glue_fns of node_id
5151
| DATA_obj_vtbl of node_id
5252
| DATA_forwarding_vtbl of (Ast.ty_obj * Ast.ty_obj)
53+
| DATA_const of node_id
5354
| DATA_crate
5455
;;
5556

src/boot/me/trans.ml

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ let call_output_slot call =
3737
(fst (need_ty_fn call.call_callee_ty)).Ast.sig_output_slot
3838
;;
3939

40+
41+
type const =
42+
CONST_val of int64
43+
| CONST_frag of Asm.frag
44+
;;
45+
4046
let trans_visitor
4147
(cx:ctxt)
4248
(path:Ast.name_component Stack.t)
@@ -1006,21 +1012,84 @@ let trans_visitor
10061012
trans_cond_fail "bounds check" jmp;
10071013
based elt_reg
10081014

1009-
and trans_lval_item
1015+
and trans_const_atom
1016+
(atom:Ast.atom)
1017+
: (Ast.ty * const) =
1018+
match atom with
1019+
Ast.ATOM_literal lit ->
1020+
begin
1021+
match lit.node with
1022+
Ast.LIT_nil -> (Ast.TY_nil, CONST_val 0L)
1023+
| Ast.LIT_bool false -> (Ast.TY_bool, CONST_val 0L)
1024+
| Ast.LIT_bool true -> (Ast.TY_bool, CONST_val 1L)
1025+
| Ast.LIT_char c -> (Ast.TY_char, CONST_val (Int64.of_int c))
1026+
| Ast.LIT_int i -> (Ast.TY_int, CONST_val i)
1027+
| Ast.LIT_uint i -> (Ast.TY_uint, CONST_val i)
1028+
| Ast.LIT_mach_int (m, i) -> (Ast.TY_mach m, CONST_val i)
1029+
end
1030+
1031+
| Ast.ATOM_lval lv ->
1032+
trans_const_lval lv
1033+
1034+
and trans_const_expr
1035+
(expr:Ast.expr)
1036+
: (Ast.ty * const) =
1037+
match expr with
1038+
Ast.EXPR_atom at -> trans_const_atom at
1039+
1040+
| Ast.EXPR_binary (_, a, b) ->
1041+
let _ = trans_const_atom a in
1042+
let _ = trans_const_atom b in
1043+
unimpl None "constant-folding binary expr"
1044+
1045+
| Ast.EXPR_unary (_, x) ->
1046+
let _ = trans_const_atom x in
1047+
unimpl None "constant-folding unary expr"
1048+
1049+
and trans_const_lval
10101050
(lv:Ast.lval)
1011-
: (Il.cell * Ast.ty) =
1051+
: (Ast.ty * const) =
10121052
assert (lval_base_is_item cx lv);
1013-
let ty = lval_ty cx lv in
10141053
let item = lval_item cx lv in
10151054
check_concrete item.node.Ast.decl_params ();
10161055
match item.node.Ast.decl_item with
1017-
Ast.MOD_ITEM_const (_, Some e) ->
1018-
(Il.Reg (force_to_reg (trans_expr e)), ty)
1019-
| _ ->
1020-
bug ()
1021-
"trans_lval_full called on unsupported item lval '%a'"
1056+
Ast.MOD_ITEM_const (_, Some e) -> trans_const_expr e
1057+
1058+
| _ -> bug ()
1059+
"trans_const_lval called on unsupported item lval '%a'"
10221060
Ast.sprintf_lval lv
10231061

1062+
and trans_lval_item
1063+
(lv:Ast.lval)
1064+
: (Il.cell * Ast.ty) =
1065+
assert (lval_base_is_item cx lv);
1066+
match trans_const_lval lv with
1067+
1068+
(ty, CONST_val v) ->
1069+
let f tm =
1070+
(Il.Reg (force_to_reg (imm_of_ty v tm)), ty)
1071+
in
1072+
begin
1073+
match ty with
1074+
Ast.TY_mach tm -> f tm
1075+
| Ast.TY_uint -> f word_ty_mach
1076+
| Ast.TY_int -> f word_ty_signed_mach
1077+
| Ast.TY_bool -> f TY_u8
1078+
| Ast.TY_char -> f TY_u32
1079+
| Ast.TY_nil -> (nil_ptr, ty)
1080+
| _ -> bug ()
1081+
"trans_lval_item on %a: unexpected type %a"
1082+
Ast.sprintf_lval lv Ast.sprintf_ty ty
1083+
end
1084+
1085+
| (ty, CONST_frag f) ->
1086+
let item = lval_item cx lv in
1087+
(crate_rel_to_ptr
1088+
(trans_crate_rel_data_operand
1089+
(DATA_const item.id)
1090+
(fun _ -> f))
1091+
(referent_type cx ty), ty)
1092+
10241093
and trans_lval_full
10251094
(initializing:bool)
10261095
(lv:Ast.lval)

0 commit comments

Comments
 (0)