[m-dev.] [reuse] diff: eager codegen structure reuse
Peter Ross
petdr at miscrit.be
Tue Oct 10 03:59:41 AEDT 2000
Hi,
===================================================================
Estimated hours taken: 8
First cut of structure reuse with the eager code generator.
code_info.m:
unify_gen.m:
Pass down the how_to_construct type.
var_locn.m:
Generate the code which does structure reuse.
Index: code_info.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/code_info.m,v
retrieving revision 1.252.2.2
diff -u -r1.252.2.2 code_info.m
--- code_info.m 2000/10/09 09:09:50 1.252.2.2
+++ code_info.m 2000/10/09 16:47:27
@@ -3036,7 +3036,8 @@
code_info::in, code_info::out) is det.
:- pred code_info__assign_cell_to_var(prog_var::in, tag::in,
- list(maybe(rval))::in, string::in, code_tree::out,
+ list(maybe(rval))::in, string::in,
+ how_to_construct::in, code_tree::out,
code_info::in, code_info::out) is det.
:- pred code_info__place_var(prog_var::in, lval::in, code_tree::out,
@@ -3217,7 +3218,8 @@
},
code_info__set_var_locns_info(VarInfo).
-code_info__assign_cell_to_var(Var, Ptag, Vector, TypeMsg, Code) -->
+code_info__assign_cell_to_var(Var, Ptag, Vector, TypeMsg,
+ HowToConstruct, Code) -->
code_info__get_var_locns_info(VarInfo0),
code_info__get_next_cell_number(CellNum),
{
@@ -3233,7 +3235,7 @@
;
VarInfo0 = var_locn_info(VarLocInfo0),
var_locn__assign_cell_to_var(Var, Ptag, Vector, CellNum,
- TypeMsg, Code, VarLocInfo0, VarLocInfo),
+ TypeMsg, HowToConstruct, Code, VarLocInfo0, VarLocInfo),
VarInfo = var_locn_info(VarLocInfo)
},
code_info__set_var_locns_info(VarInfo).
Index: unify_gen.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/unify_gen.m,v
retrieving revision 1.108.2.2
diff -u -r1.108.2.2 unify_gen.m
--- unify_gen.m 2000/09/27 15:00:48 1.108.2.2
+++ unify_gen.m 2000/10/09 16:47:59
@@ -62,10 +62,11 @@
{ Code = empty }
)
;
- { Uni = construct(Var, ConsId, Args, Modes, _, _, AditiInfo) },
+ { Uni = construct(Var, ConsId, Args, Modes,
+ HowToConstruct, _, AditiInfo) },
( code_info__variable_is_forward_live(Var) ->
unify_gen__generate_construction(Var, ConsId,
- Args, Modes, AditiInfo, Code)
+ Args, Modes, HowToConstruct, AditiInfo, Code)
;
{ Code = empty }
)
@@ -268,28 +269,32 @@
% instantiate the arguments of that term.
:- pred unify_gen__generate_construction(prog_var::in, cons_id::in,
- list(prog_var)::in, list(uni_mode)::in, maybe(rl_exprn_id)::in,
- code_tree::out, code_info::in, code_info::out) is det.
+ list(prog_var)::in, list(uni_mode)::in, how_to_construct::in,
+ maybe(rl_exprn_id)::in, code_tree::out,
+ code_info::in, code_info::out) is det.
-unify_gen__generate_construction(Var, Cons, Args, Modes, AditiInfo, Code) -->
+unify_gen__generate_construction(Var, Cons, Args, Modes,
+ HowToConstruct, AditiInfo, Code) -->
code_info__cons_id_to_tag(Var, Cons, Tag),
unify_gen__generate_construction_2(Tag, Var, Args,
- Modes, AditiInfo, Code).
+ Modes, HowToConstruct, AditiInfo, Code).
:- pred unify_gen__generate_construction_2(cons_tag::in, prog_var::in,
- list(prog_var)::in, list(uni_mode)::in, maybe(rl_exprn_id)::in,
+ list(prog_var)::in, list(uni_mode)::in,
+ how_to_construct::in, maybe(rl_exprn_id)::in,
code_tree::out, code_info::in, code_info::out) is det.
unify_gen__generate_construction_2(string_constant(String),
- Var, _Args, _Modes, _, empty) -->
+ Var, _Args, _Modes, _HowToConstruct, _, empty) -->
code_info__assign_const_to_var(Var, const(string_const(String))).
unify_gen__generate_construction_2(int_constant(Int),
- Var, _Args, _Modes, _, empty) -->
+ Var, _Args, _Modes, _HowToConstruct, _, empty) -->
code_info__assign_const_to_var(Var, const(int_const(Int))).
unify_gen__generate_construction_2(float_constant(Float),
- Var, _Args, _Modes, _, empty) -->
+ Var, _Args, _Modes, _HowToConstruct, _, empty) -->
code_info__assign_const_to_var(Var, const(float_const(Float))).
-unify_gen__generate_construction_2(no_tag, Var, Args, Modes, _, Code) -->
+unify_gen__generate_construction_2(no_tag, Var, Args, Modes, _HowToConstruct,
+ _, Code) -->
( { Args = [Arg], Modes = [Mode] } ->
code_info__variable_type(Arg, Type),
unify_gen__generate_sub_unify(ref(Var), ref(Arg),
@@ -298,17 +303,22 @@
{ error(
"unify_gen__generate_construction_2: no_tag: arity != 1") }
).
+
+ % RRR need to modify this code!
unify_gen__generate_construction_2(unshared_tag(Ptag),
- Var, Args, Modes, _, Code) -->
+ Var, Args, Modes, HowToConstruct, _, Code) -->
code_info__get_module_info(ModuleInfo),
unify_gen__var_types(Args, ArgTypes),
{ unify_gen__generate_cons_args(Args, ArgTypes, Modes, ModuleInfo,
Rvals) },
code_info__variable_type(Var, VarType),
{ unify_gen__var_type_msg(VarType, VarTypeMsg) },
- code_info__assign_cell_to_var(Var, Ptag, Rvals, VarTypeMsg, Code).
+ code_info__assign_cell_to_var(Var, Ptag, Rvals, VarTypeMsg,
+ HowToConstruct, Code).
+
+ % RRR need to modify this code!
unify_gen__generate_construction_2(shared_remote_tag(Ptag, Sectag),
- Var, Args, Modes, _, Code) -->
+ Var, Args, Modes, HowToConstruct, _, Code) -->
code_info__get_module_info(ModuleInfo),
unify_gen__var_types(Args, ArgTypes),
{ unify_gen__generate_cons_args(Args, ArgTypes, Modes, ModuleInfo,
@@ -317,13 +327,16 @@
{ Rvals = [yes(const(int_const(Sectag))) | Rvals0] },
code_info__variable_type(Var, VarType),
{ unify_gen__var_type_msg(VarType, VarTypeMsg) },
- code_info__assign_cell_to_var(Var, Ptag, Rvals, VarTypeMsg, Code).
+ code_info__assign_cell_to_var(Var, Ptag, Rvals, VarTypeMsg,
+ HowToConstruct, Code).
+
unify_gen__generate_construction_2(shared_local_tag(Bits1, Num1),
- Var, _Args, _Modes, _, empty) -->
+ Var, _Args, _Modes, _HowToConstruct, _, empty) -->
code_info__assign_const_to_var(Var,
mkword(Bits1, unop(mkbody, const(int_const(Num1))))).
unify_gen__generate_construction_2(type_ctor_info_constant(ModuleName,
- TypeName, TypeArity), Var, Args, _Modes, _, empty) -->
+ TypeName, TypeArity), Var, Args, _Modes,
+ _HowToConstruct, _, empty) -->
( { Args = [] } ->
[]
;
@@ -333,7 +346,8 @@
{ DataAddr = rtti_addr(RttiTypeId, type_ctor_info) },
code_info__assign_const_to_var(Var, const(data_addr_const(DataAddr))).
unify_gen__generate_construction_2(base_typeclass_info_constant(ModuleName,
- ClassId, Instance), Var, Args, _Modes, _, empty) -->
+ ClassId, Instance), Var, Args, _HowToConstruct,
+ _Modes, _, empty) -->
( { Args = [] } ->
[]
;
@@ -342,7 +356,7 @@
code_info__assign_const_to_var(Var, const(data_addr_const(data_addr(
ModuleName, base_typeclass_info(ClassId, Instance))))).
unify_gen__generate_construction_2(tabling_pointer_constant(PredId, ProcId),
- Var, Args, _Modes, _, empty) -->
+ Var, Args, _Modes, _HowToConstruct, _, empty) -->
( { Args = [] } ->
[]
;
@@ -354,7 +368,7 @@
{ DataAddr = data_addr(ModuleName, tabling_pointer(ProcLabel)) },
code_info__assign_const_to_var(Var, const(data_addr_const(DataAddr))).
unify_gen__generate_construction_2(code_addr_constant(PredId, ProcId),
- Var, Args, _Modes, _, empty) -->
+ Var, Args, _Modes, _HowToConstruct, _, empty) -->
( { Args = [] } ->
[]
;
@@ -365,7 +379,7 @@
code_info__assign_const_to_var(Var, const(code_addr_const(CodeAddr))).
unify_gen__generate_construction_2(
pred_closure_tag(PredId, ProcId, EvalMethod),
- Var, Args, _Modes, _AditiInfo, Code) -->
+ Var, Args, _Modes, HowToConstruct, _AditiInfo, Code) -->
% This code constructs or extends a closure.
% The structure of closures is defined in runtime/mercury_ho_call.h.
@@ -551,7 +565,8 @@
yes(const(int_const(NumArgs)))
| PredArgs
] },
- code_info__assign_cell_to_var(Var, 0, Vector, "closure", Code)
+ code_info__assign_cell_to_var(Var, 0, Vector, "closure",
+ HowToConstruct, Code)
).
:- pred unify_gen__generate_extra_closure_args(list(prog_var)::in, lval::in,
Index: var_locn.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/var_locn.m,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 var_locn.m
--- var_locn.m 2000/09/25 17:06:00 1.1.2.1
+++ var_locn.m 2000/10/09 16:48:01
@@ -149,7 +149,8 @@
% and updates the state of VarLocnInfo0 accordingly.
:- pred var_locn__assign_cell_to_var(prog_var::in, tag::in,
- list(maybe(rval))::in, int::in, string::in, code_tree::out,
+ list(maybe(rval))::in, int::in, string::in,
+ how_to_construct::in, code_tree::out,
var_locn_info::in, var_locn_info::out) is det.
% var_locn__place_var(Var, Lval, Code, VarLocnInfo0, VarLocnInfo)
@@ -773,7 +774,8 @@
%----------------------------------------------------------------------------%
-var_locn__assign_cell_to_var(Var, Ptag, Vector, CellNum, TypeMsg, Code) -->
+var_locn__assign_cell_to_var(Var, Ptag, Vector, CellNum, TypeMsg,
+ HowToConstruct, Code) -->
{ Reuse = no },
{ CellRval0 = create(Ptag, Vector, uniform(no), can_be_either,
CellNum, TypeMsg, Reuse) },
@@ -786,8 +788,19 @@
var_locn__assign_const_to_var(Var, CellRval),
{ Code = empty }
;
- var_locn__assign_dynamic_cell_to_var(Var, Ptag, Vector,
- TypeMsg, Code)
+ (
+ { HowToConstruct = construct_statically(_) },
+ { error("var_locn__assign_cell_to_var") }
+ ;
+ { HowToConstruct = construct_dynamically },
+ var_locn__assign_dynamic_cell_to_var(Var, Ptag, Vector,
+ TypeMsg, Code)
+ ;
+ { HowToConstruct = reuse_cell(CellToReuse) },
+ { CellToReuse = cell_to_reuse(ReuseVar,
+ _ReuseConsId, _) },
+ var_locn__reuse_cell(Var, ReuseVar, Ptag, Vector, Code)
+ )
).
:- pred var_locn__assign_dynamic_cell_to_var(prog_var::in, tag::in,
@@ -807,6 +820,61 @@
var_locn__set_magic_var_location(Var, Lval),
var_locn__assign_cell_args(Vector, yes(Ptag), lval(Lval), 0, ArgsCode),
{ Code = tree(CellCode, ArgsCode) }.
+
+:- pred var_locn__reuse_cell(prog_var::in, prog_var::in, tag::in,
+ list(maybe(rval))::in, code_tree::out,
+ var_locn_info::in, var_locn_info::out) is det.
+
+var_locn__reuse_cell(Var, ReuseVar, Ptag, Vector, Code) -->
+ var_locn__check_var_is_unknown(Var),
+
+ var_locn__produce_var(ReuseVar, ReuseRval, ReuseCode),
+
+ { require(tree__is_empty(ReuseCode),
+ "var_locn__reuse_cell: ReuseCode non-empty!") },
+
+ { ReuseRval = lval(ReuseLval0) ->
+ _ReuseLval = ReuseLval0
+ ;
+ error("var_locn__reuse_cell: not an lval")
+ },
+
+ var_locn__select_preferred_reg_or_stack(Var, Lval),
+ var_locn__get_var_name(Var, VarName),
+
+ % XXX we need to worry about the tag!
+ % XXX also look at assign_var_to_var
+ { CellCode = node([
+ assign(Lval, ReuseRval)
+ - string__append("Reusing cell on heap for ", VarName)
+ ]) },
+ var_locn__set_magic_var_location(Var, Lval),
+
+ var_locn__assign_cell_args(Vector, yes(Ptag), lval(Lval), 0, ArgsCode),
+ { Code = tree(CellCode, ArgsCode) }.
+
+/*
+:- pred var_locn__reuse_cell(prog_var::in, tag::in,
+ list(maybe(rval))::in, string::in, code_tree::out,
+ var_locn_info::in, var_locn_info::out) is det.
+
+var_locn__reuse_cell(Var, Ptag, Vector, TypeMsg, Code) -->
+ var_locn__check_var_is_unknown(Var),
+
+ % XXX Here we want to look up the address of the reuse
+ % var.
+
+ var_locn__select_preferred_reg_or_stack(Var, Lval),
+ var_locn__get_var_name(Var, VarName),
+ { list__length(Vector, Size) },
+ { CellCode = node([
+ incr_hp(Lval, yes(Ptag), const(int_const(Size)), TypeMsg)
+ - string__append("Allocating heap for ", VarName)
+ ]) },
+ var_locn__set_magic_var_location(Var, Lval),
+ var_locn__assign_cell_args(Vector, yes(Ptag), lval(Lval), 0, ArgsCode),
+ { Code = tree(CellCode, ArgsCode) }.
+*/
:- pred var_locn__assign_cell_args(list(maybe(rval))::in,
maybe(tag)::in, rval::in, int::in, code_tree::out,
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list