| 1 |
local |
| 2 |
|
| 3 |
val fast_floating_point = ref true |
| 4 |
|
| 5 |
(* |
| 6 |
* User defined constant type. Dummy for now. |
| 7 |
* In practice, you'll want to use this type to implement constants with |
| 8 |
* values that cannot be determined until final code generation, e.g. |
| 9 |
* stack frame offset. |
| 10 |
*) |
| 11 |
structure UserConst = |
| 12 |
struct |
| 13 |
type const = unit |
| 14 |
fun toString() = "" |
| 15 |
fun hash() = 0w0 |
| 16 |
fun valueOf _ = 0 |
| 17 |
fun == _ = true |
| 18 |
end |
| 19 |
|
| 20 |
(* |
| 21 |
* User defined datatype for representing aliasing. Dummy for now. |
| 22 |
* You'll need this to represent aliasing information. |
| 23 |
*) |
| 24 |
structure UserRegion = |
| 25 |
struct |
| 26 |
type region = unit |
| 27 |
fun toString () = "" |
| 28 |
val memory = () |
| 29 |
val stack = () |
| 30 |
val readonly = () |
| 31 |
val spill = () |
| 32 |
end |
| 33 |
|
| 34 |
(* |
| 35 |
* User defined datatype for representing pseudo assembly operators. |
| 36 |
* Dummy for now. |
| 37 |
* |
| 38 |
* You'll need this to represent assembler directives. |
| 39 |
*) |
| 40 |
structure UserPseudoOps = |
| 41 |
struct |
| 42 |
type pseudo_op = unit |
| 43 |
fun toString () = "" |
| 44 |
fun emitValue _ = () |
| 45 |
fun sizeOf _ = 0 |
| 46 |
fun adjustLabels _ = true |
| 47 |
end |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
(* |
| 52 |
* Client defined extensions. None for now. |
| 53 |
* You'll need this only if you need to extend the set of MLTREE operators |
| 54 |
*) |
| 55 |
structure UserExtension = |
| 56 |
struct |
| 57 |
|
| 58 |
type ('s,'r,'f,'c) sx = ('s,'r,'f,'c) X86InstrExt.sext |
| 59 |
type ('s,'r,'f,'c) rx = unit |
| 60 |
type ('s,'r,'f,'c) fx = unit |
| 61 |
type ('s,'r,'f,'c) ccx = unit |
| 62 |
|
| 63 |
end |
| 64 |
|
| 65 |
(* |
| 66 |
* This module controls how we handle user extensions. Since we don't |
| 67 |
* have any yet. This is just a bunch of dummy routines. |
| 68 |
*) |
| 69 |
functor UserMLTreeExtComp |
| 70 |
( structure I : X86INSTR where T.Extension = UserExtension |
| 71 |
structure TS : MLTREE_STREAM where T = I.T |
| 72 |
structure CFG : CONTROL_FLOW_GRAPH where I = I and P = TS.S.P |
| 73 |
) : MLTREE_EXTENSION_COMP = |
| 74 |
struct |
| 75 |
structure T = TS.T |
| 76 |
structure TS = TS |
| 77 |
structure I = I |
| 78 |
structure CFG = CFG |
| 79 |
structure C = I.C |
| 80 |
|
| 81 |
structure CompInstrExt = X86CompInstrExt ( |
| 82 |
structure I = I |
| 83 |
structure TS = TS |
| 84 |
structure CFG = CFG) |
| 85 |
|
| 86 |
type reducer = |
| 87 |
(I.instruction,C.cellset,I.operand,I.addressing_mode,CFG.cfg) TS.reducer |
| 88 |
|
| 89 |
val compileSext = CompInstrExt.compileSext |
| 90 |
|
| 91 |
fun compileRext _ = raise Fail "CompExtFn.compileRext" |
| 92 |
fun compileFext _ = raise Fail "CompExtFn.compileFext" |
| 93 |
fun compileCCext _ = raise Fail "CompExtFn.compileCCext" |
| 94 |
|
| 95 |
end |
| 96 |
|
| 97 |
structure X86MLTree = |
| 98 |
MLTreeF (structure Constant = UserConst |
| 99 |
structure Region = UserRegion |
| 100 |
structure Extension = UserExtension) |
| 101 |
|
| 102 |
structure X86MLTreeEval = |
| 103 |
MLTreeEval (structure T = X86MLTree |
| 104 |
fun eq _ _ = false |
| 105 |
val eqRext = eq val eqFext = eq |
| 106 |
val eqCCext = eq val eqSext = eq) |
| 107 |
|
| 108 |
structure X86GasPseudoOps = |
| 109 |
X86GasPseudoOps(structure T=X86MLTree |
| 110 |
structure MLTreeEval=X86MLTreeEval) |
| 111 |
functor X86PseudoOpsFn ( |
| 112 |
structure T : MLTREE |
| 113 |
structure MLTreeEval : MLTREE_EVAL where T = T |
| 114 |
) : PSEUDO_OPS_BASIS = X86GasPseudoOps ( |
| 115 |
structure T = T |
| 116 |
structure MLTreeEval = MLTreeEval) |
| 117 |
(* |
| 118 |
functor X86PseudoOpsFn ( |
| 119 |
structure T : MLTREE |
| 120 |
structure MLTreeEval : MLTREE_EVAL where T = T |
| 121 |
) : PSEUDO_OPS_BASIS = X86DarwinPseudoOps ( |
| 122 |
structure T = T |
| 123 |
structure MLTreeEval = MLTreeEval) |
| 124 |
*) |
| 125 |
structure X86PseudoOps = X86PseudoOpsFn( |
| 126 |
structure T = X86MLTree |
| 127 |
structure MLTreeEval = X86MLTreeEval) |
| 128 |
|
| 129 |
structure PseudoOps = |
| 130 |
struct |
| 131 |
|
| 132 |
structure Client = |
| 133 |
struct |
| 134 |
structure AsmPseudoOps = X86PseudoOps |
| 135 |
type pseudo_op = unit |
| 136 |
|
| 137 |
fun toString () = "" |
| 138 |
|
| 139 |
fun emitValue _ = raise Fail "todo" |
| 140 |
fun sizeOf _ = raise Fail "todo" |
| 141 |
fun adjustLabels _ = raise Fail "todo" |
| 142 |
end (* Client *) |
| 143 |
|
| 144 |
structure PseudoOps = PseudoOps (structure Client = Client) |
| 145 |
end |
| 146 |
|
| 147 |
structure X86Stream = InstructionStream(PseudoOps.PseudoOps) |
| 148 |
structure X86Instr = X86Instr (X86MLTree) |
| 149 |
structure X86Shuffle = X86Shuffle(X86Instr) |
| 150 |
|
| 151 |
structure X86MLTreeHash = |
| 152 |
MLTreeHash (structure T = X86MLTree |
| 153 |
fun h _ _ = 0w0 |
| 154 |
val hashRext = h val hashFext = h |
| 155 |
val hashCCext = h val hashSext = h) |
| 156 |
|
| 157 |
|
| 158 |
functor X86MemRegs(X86Instr:X86INSTR) = struct |
| 159 |
structure I = X86Instr |
| 160 |
structure CB = CellsBasis |
| 161 |
|
| 162 |
fun memReg{reg, base} = raise Fail "" |
| 163 |
end |
| 164 |
|
| 165 |
structure X86MemRegs = X86MemRegs(X86Instr) |
| 166 |
|
| 167 |
structure X86Asm = X86AsmEmitter |
| 168 |
(structure Instr = X86Instr |
| 169 |
structure S = X86Stream |
| 170 |
val memRegBase = NONE |
| 171 |
structure MemRegs = X86MemRegs |
| 172 |
structure MLTreeEval = X86MLTreeEval |
| 173 |
structure Shuffle = X86Shuffle |
| 174 |
) |
| 175 |
|
| 176 |
structure X86InsnProps = X86Props |
| 177 |
(structure Instr = X86Instr |
| 178 |
structure MLTreeHash = X86MLTreeHash |
| 179 |
structure MLTreeEval = X86MLTreeEval) |
| 180 |
|
| 181 |
structure X86CFG = ControlFlowGraph ( |
| 182 |
structure I = X86Asm.I |
| 183 |
structure GraphImpl = DirectedGraph |
| 184 |
structure InsnProps = X86InsnProps |
| 185 |
structure Asm = X86Asm) |
| 186 |
|
| 187 |
structure X86MLTStream = MLTreeStream ( |
| 188 |
structure T = X86MLTree |
| 189 |
structure S = X86Stream) |
| 190 |
|
| 191 |
structure CompInstrExt = X86CompInstrExt ( |
| 192 |
structure I = X86Instr |
| 193 |
structure TS = X86MLTStream |
| 194 |
structure CFG = X86CFG) |
| 195 |
|
| 196 |
structure X86MTC = struct |
| 197 |
structure T = X86MLTree |
| 198 |
structure TS = X86MLTStream |
| 199 |
structure I = X86Instr |
| 200 |
structure CFG = X86CFG |
| 201 |
structure C = I.C |
| 202 |
type reducer = |
| 203 |
(I.instruction,C.cellset,I.operand,I.addressing_mode,X86CFG.cfg) TS.reducer |
| 204 |
fun unimplemented _ = MLRiscErrorMsg.impossible "UserMLTreeExtComp" |
| 205 |
val compileSext = CompInstrExt.compileSext |
| 206 |
val compileRext = unimplemented |
| 207 |
val compileFext = unimplemented |
| 208 |
val compileCCext = unimplemented |
| 209 |
|
| 210 |
structure X86MLTreeUtils : MLTREE_UTILS = |
| 211 |
struct |
| 212 |
structure T = X86MLTree |
| 213 |
structure IX = X86InstrExt |
| 214 |
structure U = MLTreeUtils ( |
| 215 |
structure T = T |
| 216 |
fun hashSext _ _ = 0w0 |
| 217 |
fun hashRext _ _ = 0w0 |
| 218 |
fun hashFext _ _ = 0w0 |
| 219 |
fun hashCCext _ _ = 0w0 |
| 220 |
fun eqSext _ _ = raise Fail "eqSext" |
| 221 |
fun eqRext _ _ = raise Fail "eqRext" |
| 222 |
fun eqFext _ _ = raise Fail "eqFext" |
| 223 |
fun eqCCext _ _ = raise Fail "eqCCext" |
| 224 |
fun showSext (prt : T.printer) ext = raise Fail "todo" |
| 225 |
fun showRext _ _ = raise Fail "showRext" |
| 226 |
fun showFext _ _ = raise Fail "showFext" |
| 227 |
fun showCCext _ _ = raise Fail "showCCext") |
| 228 |
open U |
| 229 |
end |
| 230 |
end |
| 231 |
|
| 232 |
structure X86 = X86 ( |
| 233 |
structure X86Instr = X86Instr |
| 234 |
structure MLTreeUtils = X86MTC.X86MLTreeUtils |
| 235 |
structure ExtensionComp = X86MTC |
| 236 |
structure MLTreeStream = X86MLTStream |
| 237 |
datatype arch = Pentium | PentiumPro | PentiumII | PentiumIII |
| 238 |
val arch = ref Pentium (* Lowest common denominator *) |
| 239 |
|
| 240 |
fun cvti2f _ = raise Fail "" |
| 241 |
val fast_floating_point = fast_floating_point |
| 242 |
) |
| 243 |
|
| 244 |
structure X86Emit = CFGEmit ( |
| 245 |
structure CFG = X86CFG |
| 246 |
structure E = X86Asm) |
| 247 |
|
| 248 |
|
| 249 |
structure X86FlowGraph = BuildFlowgraph |
| 250 |
(structure Props = X86InsnProps |
| 251 |
structure Stream = X86Stream |
| 252 |
structure CFG = X86CFG) |
| 253 |
|
| 254 |
structure X86Expand = CFGExpandCopies (structure CFG=X86CFG |
| 255 |
structure Shuffle = X86Shuffle) |
| 256 |
structure X86BlockPlacement = DefaultBlockPlacement(X86CFG) |
| 257 |
|
| 258 |
structure RASpill = RASpillWithRenaming ( |
| 259 |
structure Asm = X86Asm |
| 260 |
structure InsnProps = X86InsnProps |
| 261 |
val max_dist = ref 4 |
| 262 |
val keep_multiple_values = ref false) |
| 263 |
|
| 264 |
datatype spill_operand_kind = SPILL_LOC |
| 265 |
| CONST_VAL |
| 266 |
|
| 267 |
datatype ra_phase = SPILL_PROPAGATION |
| 268 |
| SPILL_COLORING |
| 269 |
|
| 270 |
fun upto(from, to) = if from>to then [] else from::(upto (from+1,to)) |
| 271 |
infix upto |
| 272 |
|
| 273 |
structure CB = CellsBasis |
| 274 |
structure I = X86Instr |
| 275 |
structure C = X86Cells |
| 276 |
|
| 277 |
structure IntRA = |
| 278 |
struct |
| 279 |
val dedicated = [C.esp, C.ebp] |
| 280 |
val allRegs = C.Regs CellsBasis.GP {from=0, to=7, step=1} |
| 281 |
val allRegsSet = foldl C.addReg C.empty allRegs |
| 282 |
val avail = let |
| 283 |
val availSet = List.foldl C.rmvReg allRegsSet dedicated |
| 284 |
in |
| 285 |
C.getReg availSet |
| 286 |
end |
| 287 |
fun spillInit _ = () |
| 288 |
val memRegs = C.Regs CB.GP {from=8,to=31,step=1} |
| 289 |
fun spillLoc {info=frame, an, cell, id=loc} = let |
| 290 |
val spillLoc = ~(loc*4) |
| 291 |
val opnd = I.Displace { |
| 292 |
base = C.ebp, |
| 293 |
disp = I.Immed (Int32.fromInt spillLoc), |
| 294 |
mem = () |
| 295 |
} |
| 296 |
in |
| 297 |
{opnd = opnd, kind = SPILL_LOC} |
| 298 |
end |
| 299 |
val phases = [SPILL_PROPAGATION, SPILL_COLORING] |
| 300 |
end (* IntRA *) |
| 301 |
|
| 302 |
structure FloatRA = |
| 303 |
struct |
| 304 |
val avail = List.map C.FPReg (0 upto 31) |
| 305 |
val dedicated = [] |
| 306 |
fun spillInit _ = () |
| 307 |
val fastMemRegs = C.Regs CB.FP {from=8, to=31, step=1} |
| 308 |
val fastPhases = [SPILL_PROPAGATION,SPILL_COLORING] |
| 309 |
val memRegs = [] |
| 310 |
fun spillLoc (info, ans, id) = raise Fail "" |
| 311 |
val phases = [SPILL_PROPAGATION] |
| 312 |
end (* FloatRA *) |
| 313 |
|
| 314 |
(* register allocation *) |
| 315 |
structure X86RA = X86RA ( |
| 316 |
structure I = X86Instr |
| 317 |
structure InsnProps = X86InsnProps |
| 318 |
structure CFG = X86CFG |
| 319 |
structure Asm = X86Asm |
| 320 |
structure SpillHeur = ChowHennessySpillHeur |
| 321 |
structure Spill = RASpill |
| 322 |
structure Props = X86InsnProps |
| 323 |
type spill_info = unit |
| 324 |
fun beforeRA (Graph.GRAPH graph) = () |
| 325 |
datatype spillOperandKind = datatype spill_operand_kind |
| 326 |
datatype raPhase = datatype ra_phase |
| 327 |
val fast_floating_point = fast_floating_point |
| 328 |
structure Int = IntRA |
| 329 |
structure Float = FloatRA) |
| 330 |
|
| 331 |
structure X86Expand = CFGExpandCopies ( |
| 332 |
structure CFG=X86CFG |
| 333 |
structure Shuffle = X86Shuffle) |
| 334 |
|
| 335 |
|
| 336 |
structure CCall = X86SVIDFn ( |
| 337 |
structure T = X86MLTree |
| 338 |
fun ix x = x |
| 339 |
val fast_floating_point = fast_floating_point |
| 340 |
val abi = "Darwin") |
| 341 |
|
| 342 |
|
| 343 |
structure C = X86Instr.C |
| 344 |
structure T = X86MLTree |
| 345 |
structure CFG = X86CFG |
| 346 |
structure FlowGraph = X86FlowGraph |
| 347 |
val wordTy = 32 |
| 348 |
|
| 349 |
fun lit i = T.LI (T.I.fromInt (32, i)) |
| 350 |
val param0 = T.LOAD(wordTy, T.ADD(32, lit 8, T.REG(32,C.ebp)), ()) |
| 351 |
|
| 352 |
in |
| 353 |
|
| 354 |
structure X86MLTree = X86MLTree |
| 355 |
|
| 356 |
structure X86MLRISCGen = |
| 357 |
struct |
| 358 |
|
| 359 |
fun codegen' (functionName, stms, result) = let |
| 360 |
val insnStrm = FlowGraph.build() |
| 361 |
val stream as X86Stream.STREAM |
| 362 |
{ beginCluster, (* start a cluster *) |
| 363 |
endCluster, (* end a cluster *) |
| 364 |
emit, (* emit MLTREE stm *) |
| 365 |
defineLabel, (* define a local label *) |
| 366 |
entryLabel, (* define an external entry *) |
| 367 |
exitBlock, (* mark the end of a procedure *) |
| 368 |
pseudoOp, (* emit a pseudo op *) |
| 369 |
annotation, (* add an annotation *) |
| 370 |
... } = |
| 371 |
X86.selectInstructions insnStrm |
| 372 |
fun doit () = ( |
| 373 |
beginCluster 0; (* start a new cluster *) |
| 374 |
pseudoOp PseudoOpsBasisTyp.TEXT; |
| 375 |
pseudoOp (PseudoOpsBasisTyp.EXPORT [functionName]); |
| 376 |
entryLabel functionName; (* define the entry label *) |
| 377 |
List.app emit stms; (* emit all the statements *) |
| 378 |
exitBlock result; |
| 379 |
endCluster []) |
| 380 |
val cfg = doit () |
| 381 |
val cfg = X86RA.run cfg |
| 382 |
val cfg = X86Expand.run cfg |
| 383 |
in |
| 384 |
(cfg, stream) |
| 385 |
end (* codegen' *) |
| 386 |
|
| 387 |
fun dumpOutput (cfg, stream) = let |
| 388 |
val (cfg as Graph.GRAPH graph, blocks) = |
| 389 |
X86BlockPlacement.blockPlacement cfg |
| 390 |
val CFG.INFO{annotations=an, data, decls, ...} = #graph_info graph |
| 391 |
in |
| 392 |
X86Emit.asmEmit (cfg, blocks) |
| 393 |
end (* dumpOutput *) |
| 394 |
|
| 395 |
fun codegen (functionName, target, proto, initStms, args) = let |
| 396 |
val [functionName, target] = List.map Label.global [functionName, target] |
| 397 |
(* construct the C call *) |
| 398 |
val {result, callseq} = CCall.genCall { |
| 399 |
name=T.LABEL target, |
| 400 |
paramAlloc=fn _ => false, |
| 401 |
structRet=fn _ => T.REG (32, C.eax), |
| 402 |
saveRestoreDedicated=fn _ => {save=[], restore=[]}, |
| 403 |
callComment=NONE, |
| 404 |
proto=proto, |
| 405 |
args=args} |
| 406 |
fun wordLit i = T.LI (T.I.fromInt (wordTy, i)) |
| 407 |
val stms = List.concat [ |
| 408 |
[T.EXT(X86InstrExt.PUSHL(T.REG(32, C.ebp))), |
| 409 |
T.COPY (wordTy, [C.ebp], [C.esp])], |
| 410 |
initStms, |
| 411 |
callseq, |
| 412 |
[T.EXT(X86InstrExt.LEAVE)], |
| 413 |
[T.RET []]] |
| 414 |
in |
| 415 |
dumpOutput(codegen'(functionName, stms, result)) |
| 416 |
end (* codegen *) |
| 417 |
|
| 418 |
end |
| 419 |
|
| 420 |
structure X86CCall = CCall |
| 421 |
structure X86Test = GenTestFn ( |
| 422 |
structure T = X86MLTree |
| 423 |
structure CCall = CCall |
| 424 |
structure Cells = X86Cells |
| 425 |
val codegen = X86MLRISCGen.codegen |
| 426 |
val param0 = param0 |
| 427 |
val wordTy = 32) |
| 428 |
|
| 429 |
end (* local *) |