| 1 |
(* |
| 2 |
* Generate the <arch>RTLProps functor. |
| 3 |
* This structure extracts semantics and dependence |
| 4 |
* information about the instruction set needed for SSA optimizations. |
| 5 |
*) |
| 6 |
|
| 7 |
functor MDLGenRTLProps(RTLComp : MDL_RTL_COMP) : MDL_GEN_MODULE2 = |
| 8 |
struct |
| 9 |
|
| 10 |
structure RTLComp = RTLComp |
| 11 |
structure Comp = RTLComp.Comp |
| 12 |
structure M = RTLComp.MLRiscTypes |
| 13 |
structure Consts = Comp.Consts |
| 14 |
structure Ast = Comp.Ast |
| 15 |
structure Env = Comp.Env |
| 16 |
structure Tr = Comp.Trans |
| 17 |
structure RTL = RTLComp.RTL |
| 18 |
structure T = RTL.T |
| 19 |
structure C = CellsBasis |
| 20 |
|
| 21 |
open Ast Comp.Util Comp.Error |
| 22 |
|
| 23 |
exception Undefined |
| 24 |
exception NotFound |
| 25 |
|
| 26 |
(* Function to make a new RTL *) |
| 27 |
val makeNewRTL = IDexp(IDENT(["RTL"],"new")) |
| 28 |
|
| 29 |
(*------------------------------------------------------------------------ |
| 30 |
* |
| 31 |
* Generate a table of compiled RTLs templates |
| 32 |
* |
| 33 |
*------------------------------------------------------------------------*) |
| 34 |
fun genRTLTable compiled_rtls = |
| 35 |
let val md = RTLComp.md compiled_rtls |
| 36 |
|
| 37 |
val rtls = RTLComp.rtls compiled_rtls |
| 38 |
|
| 39 |
val rtlStrName = Comp.strname md "RTL" |
| 40 |
|
| 41 |
val constTbl = Consts.newConstTable() |
| 42 |
|
| 43 |
fun makeEntry(RTLComp.RTLDEF{id, args, rtl, ...}) = |
| 44 |
let val lookup = RTL.argOf rtl |
| 45 |
|
| 46 |
fun param i = APPexp(IDexp(IDENT(["T"],"PARAM")),INTexp i) |
| 47 |
|
| 48 |
fun makeArg name = |
| 49 |
let val (exp,pos) = lookup name |
| 50 |
val e = |
| 51 |
case pos of |
| 52 |
RTL.IN i => param i |
| 53 |
| RTL.OUT i => param i |
| 54 |
| RTL.IO(i,_) => param i |
| 55 |
in (name, e) |
| 56 |
end handle RTL.NotAnArgument => |
| 57 |
(warning("'"^name^"' is unused in rtl "^id); |
| 58 |
(name,param 0) |
| 59 |
) |
| 60 |
|
| 61 |
val arg = Consts.const constTbl (RECORDexp(map makeArg args)) |
| 62 |
in VALdecl[VALbind(IDpat id, |
| 63 |
APPexp(makeNewRTL, |
| 64 |
APPexp(IDexp(IDENT([rtlStrName],id)), |
| 65 |
arg))) |
| 66 |
] |
| 67 |
end |
| 68 |
|
| 69 |
val body = map makeEntry rtls |
| 70 |
|
| 71 |
in STRUCTUREdecl("Arch",[], |
| 72 |
NONE,DECLsexp |
| 73 |
[LOCALdecl(Comp.Consts.genConsts constTbl,body) |
| 74 |
]) |
| 75 |
end |
| 76 |
|
| 77 |
(*------------------------------------------------------------------------ |
| 78 |
* |
| 79 |
* Create the function rtl : instruction -> rtl |
| 80 |
* |
| 81 |
*------------------------------------------------------------------------*) |
| 82 |
fun mkRtlQueryFun compiled_rtls = |
| 83 |
let fun body{instr, rtl=RTLComp.RTLDEF{id,...}, const} = |
| 84 |
{exp=IDexp(IDENT(["Arch"],id)), casePats=[]} |
| 85 |
in RTLComp.mkQuery compiled_rtls |
| 86 |
{name = "rtl", |
| 87 |
namedArguments= true, |
| 88 |
args = [["instr"]], |
| 89 |
decls = [RTLComp.complexErrorHandler "rtl"], |
| 90 |
caseArgs = [], |
| 91 |
body = body |
| 92 |
} |
| 93 |
end |
| 94 |
|
| 95 |
(*------------------------------------------------------------------------ |
| 96 |
* |
| 97 |
* Create the function defUse : instruction -> cell list * cell list |
| 98 |
* |
| 99 |
*------------------------------------------------------------------------*) |
| 100 |
fun mkDefUseQueryFun compiled_rtls name = |
| 101 |
let val {get, decl} = M.getOpnd |
| 102 |
[("int", M.CONV("CELL(int x)")), |
| 103 |
("int32", M.CONV("CELL(int32 x)")), |
| 104 |
("intinf", M.CONV("CELL(intinf x)")), |
| 105 |
("word", M.CONV("CELL(word x)")), |
| 106 |
("word32", M.CONV("CELL(word32 x)")), |
| 107 |
("cell", M.CONV("CELL x")), |
| 108 |
("label", M.IGNORE), |
| 109 |
("cellset", M.MULTI("map CELL (CellsBasis.CellSet.toCellList x)")), |
| 110 |
("operand", M.CONV("OPERAND x")) |
| 111 |
] |
| 112 |
val decl0 = |
| 113 |
$["(* methods for computing value numbers *)", |
| 114 |
"val OT.VALUE_NUMBERING", |
| 115 |
" {int, int32, intinf, word, word32, operand, ...} =", |
| 116 |
" valueNumberingMethods", |
| 117 |
"(* methods for type conversion *)" |
| 118 |
] |
| 119 |
fun gen x = SOME(get x) |
| 120 |
in RTLComp.mkDefUseQuery |
| 121 |
compiled_rtls |
| 122 |
{name = name, |
| 123 |
args = [["valueNumberingMethods"], ["instr"]], |
| 124 |
namedArguments = false, |
| 125 |
decls = [RTLComp.complexErrorHandler name, decl0, decl], |
| 126 |
def = gen, |
| 127 |
use = gen |
| 128 |
} |
| 129 |
end |
| 130 |
|
| 131 |
(*------------------------------------------------------------------------ |
| 132 |
* |
| 133 |
* Main routine |
| 134 |
* |
| 135 |
*------------------------------------------------------------------------*) |
| 136 |
fun gen compiled_rtls = |
| 137 |
let (* The machine description *) |
| 138 |
val md = RTLComp.md compiled_rtls |
| 139 |
|
| 140 |
(* name of the structure/signature *) |
| 141 |
val strName = Comp.strname md "RTLProps" |
| 142 |
val sigName = "RTL_PROPERTIES" |
| 143 |
|
| 144 |
(* Arguments to the instruction functor *) |
| 145 |
val args = |
| 146 |
["structure Instr : "^Comp.signame md "INSTR", |
| 147 |
"structure RegionProps : REGION_PROPERTIES", |
| 148 |
"structure RTL : MLTREE_RTL", |
| 149 |
"structure OperandTable : OPERAND_TABLE where I = Instr", |
| 150 |
"structure Asm : INSTRUCTION_EMITTER where I = Instr", |
| 151 |
" sharing Instr.T = RTL.T" |
| 152 |
] |
| 153 |
|
| 154 |
(* The functor *) |
| 155 |
val strBody = |
| 156 |
[$ ["structure I = Instr", |
| 157 |
"structure C = I.C", |
| 158 |
"structure RTL = RTL", |
| 159 |
"structure T = RTL.T", |
| 160 |
"structure OT = OperandTable", |
| 161 |
"", |
| 162 |
"datatype value = CELL of C.cell", |
| 163 |
" | OPERAND of I.operand", |
| 164 |
"" |
| 165 |
], |
| 166 |
Comp.errorHandler md "RTLProps", |
| 167 |
RTLComp.complexErrorHandlerDef (), |
| 168 |
STRUCTUREdecl(Comp.strname md "RTL",[],NONE, |
| 169 |
APPsexp(IDsexp(IDENT([],Comp.strname md "RTL")), |
| 170 |
DECLsexp[ |
| 171 |
$[ "structure RTL = RTL", |
| 172 |
"structure C = C" |
| 173 |
]] |
| 174 |
) |
| 175 |
), |
| 176 |
genRTLTable compiled_rtls, |
| 177 |
mkRtlQueryFun compiled_rtls, |
| 178 |
mkDefUseQueryFun compiled_rtls "defUse" |
| 179 |
] |
| 180 |
|
| 181 |
in Comp.codegen md "mltree/RTLProps" |
| 182 |
[Comp.mkFct md "RTLProps" args sigName |
| 183 |
(map Tr.simplifyDecl strBody) |
| 184 |
] |
| 185 |
end |
| 186 |
end |