| 1 |
local |
| 2 |
(* |
| 3 |
* User defined constant type. Dummy for now. |
| 4 |
* In practice, you'll want to use this type to implement constants with |
| 5 |
* values that cannot be determined until final code generation, e.g. |
| 6 |
* stack frame offset. |
| 7 |
*) |
| 8 |
structure UserConst = |
| 9 |
struct |
| 10 |
type const = unit |
| 11 |
fun toString() = "" |
| 12 |
fun hash() = 0w0 |
| 13 |
fun valueOf _ = 0 |
| 14 |
fun == _ = true |
| 15 |
end |
| 16 |
|
| 17 |
(* |
| 18 |
* User defined datatype for representing aliasing. Dummy for now. |
| 19 |
* You'll need this to represent aliasing information. |
| 20 |
*) |
| 21 |
structure UserRegion = |
| 22 |
struct |
| 23 |
type region = unit |
| 24 |
fun toString () = "" |
| 25 |
val memory = () |
| 26 |
val stack = () |
| 27 |
val readonly = () |
| 28 |
val spill = () |
| 29 |
end |
| 30 |
|
| 31 |
(* |
| 32 |
* User defined datatype for representing pseudo assembly operators. |
| 33 |
* Dummy for now. |
| 34 |
* |
| 35 |
* You'll need this to represent assembler directives. |
| 36 |
*) |
| 37 |
structure UserPseudoOps = |
| 38 |
struct |
| 39 |
type pseudo_op = unit |
| 40 |
fun toString () = "" |
| 41 |
fun emitValue _ = () |
| 42 |
fun sizeOf _ = 0 |
| 43 |
fun adjustLabels _ = true |
| 44 |
end |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
(* |
| 49 |
* Client defined extensions. None for now. |
| 50 |
* You'll need this only if you need to extend the set of MLTREE operators |
| 51 |
*) |
| 52 |
structure UserExtension = |
| 53 |
struct |
| 54 |
|
| 55 |
type ('s,'r,'f,'c) sx = ('s,'r,'f,'c) AMD64InstrExt.sext |
| 56 |
type ('s,'r,'f,'c) rx = unit |
| 57 |
type ('s,'r,'f,'c) fx = unit |
| 58 |
type ('s,'r,'f,'c) ccx = unit |
| 59 |
|
| 60 |
end |
| 61 |
|
| 62 |
(* |
| 63 |
* This module controls how we handle user extensions. Since we don't |
| 64 |
* have any yet. This is just a bunch of dummy routines. |
| 65 |
*) |
| 66 |
functor UserMLTreeExtComp |
| 67 |
( structure I : AMD64INSTR where T.Extension = UserExtension |
| 68 |
structure TS : MLTREE_STREAM where T = I.T |
| 69 |
structure CFG : CONTROL_FLOW_GRAPH where I = I and P = TS.S.P |
| 70 |
) : MLTREE_EXTENSION_COMP = |
| 71 |
struct |
| 72 |
structure T = TS.T |
| 73 |
structure TS = TS |
| 74 |
structure I = I |
| 75 |
structure CFG = CFG |
| 76 |
structure C = I.C |
| 77 |
|
| 78 |
structure CompInstrExt = AMD64CompInstrExt ( |
| 79 |
structure I = I |
| 80 |
structure TS = TS |
| 81 |
structure CFG = CFG) |
| 82 |
|
| 83 |
type reducer = |
| 84 |
(I.instruction,C.cellset,I.operand,I.addressing_mode,CFG.cfg) TS.reducer |
| 85 |
|
| 86 |
val compileSext = CompInstrExt.compileSext |
| 87 |
|
| 88 |
fun compileRext _ = raise Fail "AMD64CompExtFn.compileRext" |
| 89 |
fun compileFext _ = raise Fail "AMD64CompExtFn.compileFext" |
| 90 |
fun compileCCext _ = raise Fail "AMD64CompExtFn.compileCCext" |
| 91 |
|
| 92 |
end |
| 93 |
|
| 94 |
val floats16ByteAligned = true |
| 95 |
|
| 96 |
structure AMD64MLTree = |
| 97 |
MLTreeF (structure Constant = UserConst |
| 98 |
structure Region = UserRegion |
| 99 |
structure Extension = UserExtension) |
| 100 |
|
| 101 |
structure AMD64MLTreeEval = |
| 102 |
MLTreeEval (structure T = AMD64MLTree |
| 103 |
fun eq _ _ = false |
| 104 |
val eqRext = eq val eqFext = eq |
| 105 |
val eqCCext = eq val eqSext = eq) |
| 106 |
|
| 107 |
|
| 108 |
functor AMD64PseudoOpsFn ( |
| 109 |
structure T : MLTREE |
| 110 |
structure MLTreeEval : MLTREE_EVAL where T = T |
| 111 |
) : PSEUDO_OPS_BASIS = AMD64GasPseudoOps ( |
| 112 |
structure T = T |
| 113 |
structure MLTreeEval = MLTreeEval) |
| 114 |
|
| 115 |
(* |
| 116 |
functor AMD64PseudoOpsFn ( |
| 117 |
structure T : MLTREE |
| 118 |
structure MLTreeEval : MLTREE_EVAL where T = T |
| 119 |
) : PSEUDO_OPS_BASIS = AMD64DarwinPseudoOps ( |
| 120 |
structure T = T |
| 121 |
structure MLTreeEval = MLTreeEval) |
| 122 |
*) |
| 123 |
|
| 124 |
structure AMD64PseudoOps = AMD64PseudoOpsFn( |
| 125 |
structure T = AMD64MLTree |
| 126 |
structure MLTreeEval = AMD64MLTreeEval) |
| 127 |
|
| 128 |
structure PseudoOps = |
| 129 |
struct |
| 130 |
|
| 131 |
structure Client = |
| 132 |
struct |
| 133 |
structure AsmPseudoOps = AMD64PseudoOps |
| 134 |
type pseudo_op = unit |
| 135 |
|
| 136 |
fun toString () = "" |
| 137 |
|
| 138 |
fun emitValue _ = raise Fail "todo" |
| 139 |
fun sizeOf _ = raise Fail "todo" |
| 140 |
fun adjustLabels _ = raise Fail "todo" |
| 141 |
end (* Client *) |
| 142 |
|
| 143 |
structure PseudoOps = PseudoOps (structure Client = Client) |
| 144 |
end |
| 145 |
|
| 146 |
structure AMD64Stream = InstructionStream(PseudoOps.PseudoOps) |
| 147 |
structure AMD64Instr = AMD64Instr (AMD64MLTree) |
| 148 |
structure AMD64Shuffle = AMD64Shuffle(AMD64Instr) |
| 149 |
|
| 150 |
structure AMD64MLTreeHash = |
| 151 |
MLTreeHash (structure T = AMD64MLTree |
| 152 |
fun h _ _ = 0w0 |
| 153 |
val hashRext = h val hashFext = h |
| 154 |
val hashCCext = h val hashSext = h) |
| 155 |
|
| 156 |
structure AMD64Asm = AMD64AsmEmitter |
| 157 |
(structure Instr = AMD64Instr |
| 158 |
structure S = AMD64Stream |
| 159 |
structure MLTreeEval = AMD64MLTreeEval |
| 160 |
structure Shuffle = AMD64Shuffle |
| 161 |
) |
| 162 |
|
| 163 |
structure AMD64InsnProps = AMD64Props |
| 164 |
(structure Instr = AMD64Instr |
| 165 |
structure MLTreeHash = AMD64MLTreeHash |
| 166 |
structure MLTreeEval = AMD64MLTreeEval) |
| 167 |
|
| 168 |
structure AMD64CFG = ControlFlowGraph ( |
| 169 |
structure I = AMD64Asm.I |
| 170 |
structure GraphImpl = DirectedGraph |
| 171 |
structure InsnProps = AMD64InsnProps |
| 172 |
structure Asm = AMD64Asm) |
| 173 |
|
| 174 |
structure AMD64MLTStream = MLTreeStream ( |
| 175 |
structure T = AMD64MLTree |
| 176 |
structure S = AMD64Stream) |
| 177 |
|
| 178 |
structure CompInstrExt = AMD64CompInstrExt ( |
| 179 |
structure I = AMD64Instr |
| 180 |
structure TS = AMD64MLTStream |
| 181 |
structure CFG = AMD64CFG) |
| 182 |
|
| 183 |
structure AMD64MTC = struct |
| 184 |
structure T = AMD64MLTree |
| 185 |
structure TS = AMD64MLTStream |
| 186 |
structure I = AMD64Instr |
| 187 |
structure CFG = AMD64CFG |
| 188 |
structure C = I.C |
| 189 |
type reducer = |
| 190 |
(I.instruction,C.cellset,I.operand,I.addressing_mode,AMD64CFG.cfg) TS.reducer |
| 191 |
fun unimplemented _ = MLRiscErrorMsg.impossible "UserMLTreeExtComp" |
| 192 |
val compileSext = CompInstrExt.compileSext |
| 193 |
val compileRext = unimplemented |
| 194 |
val compileFext = unimplemented |
| 195 |
val compileCCext = unimplemented |
| 196 |
|
| 197 |
structure AMD64MLTreeUtils : MLTREE_UTILS = |
| 198 |
struct |
| 199 |
structure T = AMD64MLTree |
| 200 |
structure IX = AMD64InstrExt |
| 201 |
structure U = MLTreeUtils ( |
| 202 |
structure T = T |
| 203 |
fun hashSext _ _ = 0w0 |
| 204 |
fun hashRext _ _ = 0w0 |
| 205 |
fun hashFext _ _ = 0w0 |
| 206 |
fun hashCCext _ _ = 0w0 |
| 207 |
fun eqSext _ _ = raise Fail "eqSext" |
| 208 |
fun eqRext _ _ = raise Fail "eqRext" |
| 209 |
fun eqFext _ _ = raise Fail "eqFext" |
| 210 |
fun eqCCext _ _ = raise Fail "eqCCext" |
| 211 |
fun showSext (prt : T.printer) ext = raise Fail "todo" |
| 212 |
fun showRext _ _ = raise Fail "showRext" |
| 213 |
fun showFext _ _ = raise Fail "showFext" |
| 214 |
fun showCCext _ _ = raise Fail "showCCext") |
| 215 |
open U |
| 216 |
end |
| 217 |
end |
| 218 |
|
| 219 |
structure AMD64 = AMD64Gen ( |
| 220 |
structure I = AMD64Instr |
| 221 |
structure MLTreeUtils = AMD64MTC.AMD64MLTreeUtils |
| 222 |
structure ExtensionComp = AMD64MTC |
| 223 |
val floats16ByteAligned = floats16ByteAligned |
| 224 |
fun signBit _ = raise Fail "todo" |
| 225 |
fun negateSignBit _ = raise Fail "todo" |
| 226 |
) |
| 227 |
|
| 228 |
structure AMD64Emit = CFGEmit ( |
| 229 |
structure CFG = AMD64CFG |
| 230 |
structure E = AMD64Asm) |
| 231 |
|
| 232 |
|
| 233 |
structure AMD64FlowGraph = BuildFlowgraph |
| 234 |
(structure Props = AMD64InsnProps |
| 235 |
structure Stream = AMD64Stream |
| 236 |
structure CFG = AMD64CFG) |
| 237 |
|
| 238 |
structure AMD64Expand = CFGExpandCopies (structure CFG=AMD64CFG |
| 239 |
structure Shuffle = AMD64Shuffle) |
| 240 |
structure AMD64BlockPlacement = DefaultBlockPlacement(AMD64CFG) |
| 241 |
|
| 242 |
structure RASpill = RASpillWithRenaming ( |
| 243 |
structure Asm = AMD64Asm |
| 244 |
structure InsnProps = AMD64InsnProps |
| 245 |
val max_dist = ref 4 |
| 246 |
val keep_multiple_values = ref false) |
| 247 |
|
| 248 |
structure C = AMD64Cells |
| 249 |
|
| 250 |
datatype spill_operand_kind = SPILL_LOC |
| 251 |
| CONST_VAL |
| 252 |
|
| 253 |
datatype ra_phase = SPILL_PROPAGATION |
| 254 |
| SPILL_COLORING |
| 255 |
|
| 256 |
structure IntRA = |
| 257 |
struct |
| 258 |
val dedicated = [C.rsp, C.rbp] |
| 259 |
val allRegs = C.Regs CellsBasis.GP {from=0, to=15, step=1} |
| 260 |
val allRegsSet = foldl C.addReg C.empty allRegs |
| 261 |
val avail = let |
| 262 |
val availSet = foldl C.rmvReg allRegsSet dedicated |
| 263 |
in |
| 264 |
C.getReg availSet |
| 265 |
end |
| 266 |
fun spillInit _ = () |
| 267 |
fun spillLoc {info=frame, an, cell, id=loc} = |
| 268 |
raise Fail "" |
| 269 |
(* {opnd = AMD64Instr.Immed 0, kind = SPILL_LOC}*) |
| 270 |
val phases = [SPILL_PROPAGATION, SPILL_COLORING] |
| 271 |
end (* IntRA *) |
| 272 |
|
| 273 |
structure FloatRA = |
| 274 |
struct |
| 275 |
val avail = C.Regs CellsBasis.FP {from=0, to=15, step=1} |
| 276 |
val dedicated = [] |
| 277 |
fun spillInit _ = () |
| 278 |
fun spillLoc (info, ans, id) = raise Fail "" |
| 279 |
val phases = [SPILL_PROPAGATION, SPILL_COLORING] |
| 280 |
end (* FloatRA *) |
| 281 |
|
| 282 |
(* register allocation *) |
| 283 |
structure AMD64RA = AMD64RegAlloc ( |
| 284 |
structure I = AMD64Instr |
| 285 |
structure CFG = AMD64CFG |
| 286 |
structure Asm = AMD64Asm |
| 287 |
structure SpillHeur = ChowHennessySpillHeur |
| 288 |
structure Spill = RASpill |
| 289 |
structure Props = AMD64InsnProps |
| 290 |
val floats16ByteAligned = floats16ByteAligned |
| 291 |
type spill_info = unit |
| 292 |
fun beforeRA (Graph.GRAPH graph) = () |
| 293 |
datatype spill_operand_kind = datatype spill_operand_kind |
| 294 |
datatype ra_phase = datatype ra_phase |
| 295 |
structure Int = IntRA |
| 296 |
structure Float = FloatRA) |
| 297 |
|
| 298 |
structure AMD64Expand = CFGExpandCopies ( |
| 299 |
structure CFG=AMD64CFG |
| 300 |
structure Shuffle = AMD64Shuffle) |
| 301 |
|
| 302 |
structure AMD64CCall = X86_64SVIDFn ( |
| 303 |
structure T = AMD64MLTree) |
| 304 |
|
| 305 |
structure RA2 = |
| 306 |
RISC_RA |
| 307 |
(structure I = AMD64Instr |
| 308 |
structure Asm = AMD64Asm |
| 309 |
structure CFG = AMD64CFG |
| 310 |
structure InsnProps = AMD64InsnProps |
| 311 |
structure Rewrite = |
| 312 |
struct |
| 313 |
structure I = AMD64Instr |
| 314 |
structure C=I.C |
| 315 |
structure CB = CellsBasis |
| 316 |
fun error msg = MLRiscErrorMsg.error("X86Rewrite", msg) |
| 317 |
|
| 318 |
fun operand (rs,rt) opnd = |
| 319 |
(case opnd |
| 320 |
of I.Direct (sz, r) => if CB.sameColor(r,rs) then I.Direct (sz, rt) else opnd |
| 321 |
| I.Displace{base, disp, mem} => |
| 322 |
if CB.sameColor(base,rs) then I.Displace{base=rt, disp=disp, mem=mem} |
| 323 |
else opnd |
| 324 |
| I.Indexed{base as SOME b, index, scale, disp, mem} => let |
| 325 |
val base'= if CB.sameColor(b,rs) then SOME rt else base |
| 326 |
val index'=if CB.sameColor(index,rs) then rt else index |
| 327 |
in I.Indexed{base=base', index=index', scale=scale, disp=disp, mem=mem} |
| 328 |
end |
| 329 |
| I.Indexed{base, index, scale, disp, mem=mem} => |
| 330 |
if CB.sameColor(index,rs) then |
| 331 |
I.Indexed{base=base, index=rt, scale=scale, disp=disp, mem=mem} |
| 332 |
else opnd |
| 333 |
| _ => opnd |
| 334 |
(*end case*)) |
| 335 |
|
| 336 |
|
| 337 |
fun rewriteDef (instr, rs, rt) = let |
| 338 |
fun operand(opnd as I.Direct (sz, r)) = |
| 339 |
if CB.sameColor(r,rs) then I.Direct (sz, rt) else opnd |
| 340 |
| operand _ = error "operand: not I.Direct" |
| 341 |
fun replace r = if CB.sameColor(r,rs) then rt else r |
| 342 |
fun rewriteX86Def(instr) = |
| 343 |
(case instr |
| 344 |
of I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => |
| 345 |
I.CALL{opnd=opnd, cutsTo=cutsTo, |
| 346 |
return=CB.CellSet.map {from=rs,to=rt} return, pops=pops, |
| 347 |
defs=CB.CellSet.map {from=rs,to=rt} defs, uses=uses, mem=mem} |
| 348 |
| I.MOVE{mvOp, src, dst} => I.MOVE{mvOp=mvOp, src=src, dst=operand dst} |
| 349 |
| I.LEAL{r32, addr} => I.LEAL{r32=replace r32, addr=addr} |
| 350 |
| I.LEAQ{r64, addr} => I.LEAQ{r64=replace r64, addr=addr} |
| 351 |
| I.BINARY{binOp, src, dst} => |
| 352 |
I.BINARY{binOp=binOp, src=src, dst=operand dst} |
| 353 |
| I.SHIFT{shiftOp, src, dst, count} => |
| 354 |
I.SHIFT{shiftOp=shiftOp, src=src, count=count, dst=operand dst} |
| 355 |
| I.UNARY{unOp, opnd} => I.UNARY{unOp=unOp, opnd=operand opnd} |
| 356 |
| I.SET{cond, opnd} => I.SET{cond=cond, opnd=operand opnd} |
| 357 |
| _ => instr |
| 358 |
(* end case *)) |
| 359 |
|
| 360 |
fun f (I.ANNOTATION{a,i}) = |
| 361 |
I.ANNOTATION{i=rewriteDef(i,rs,rt), |
| 362 |
a=(case a of |
| 363 |
CB.DEF_USE{cellkind=CB.GP,defs,uses} => |
| 364 |
CB.DEF_USE{cellkind=CB.GP,uses=uses, |
| 365 |
defs=map replace defs} |
| 366 |
| _ => a)} |
| 367 |
| f (I.INSTR i) = I.INSTR(rewriteX86Def(i)) |
| 368 |
| f (I.COPY{k as CB.GP, sz, dst, src, tmp}) = |
| 369 |
I.COPY{k=k, sz=sz, dst=map replace dst, src=src, tmp=tmp} |
| 370 |
in |
| 371 |
f(instr) |
| 372 |
end |
| 373 |
|
| 374 |
|
| 375 |
fun rewriteUse (instr, rs, rt) = let |
| 376 |
val operand = operand (rs, rt) |
| 377 |
fun replace r = if CB.sameColor(r,rs) then rt else r |
| 378 |
fun rewrite instr = (case instr |
| 379 |
of I.JMP(opnd, labs) => I.JMP(operand opnd, labs) |
| 380 |
| I.JCC{cond, opnd} => I.JCC{cond=cond, opnd = operand opnd} |
| 381 |
| I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => |
| 382 |
I.CALL{opnd=operand opnd, defs=defs, return=return, |
| 383 |
uses=CB.CellSet.map {from=rs,to=rt} uses, cutsTo=cutsTo, |
| 384 |
mem=mem, pops=pops} |
| 385 |
| I.MOVE{mvOp, src, dst as I.Direct _} => |
| 386 |
I.MOVE{mvOp=mvOp, src=operand src, dst=dst} |
| 387 |
| I.MOVE{mvOp, src, dst} => |
| 388 |
I.MOVE{mvOp=mvOp, src=operand src, dst=operand dst} |
| 389 |
| I.LEAL{r32, addr} => I.LEAL{r32=r32, addr=operand addr} |
| 390 |
| I.LEAQ{r64, addr} => I.LEAQ{r64=r64, addr=operand addr} |
| 391 |
| I.CMPL{lsrc, rsrc} => I.CMPL{lsrc=operand lsrc, rsrc=operand rsrc} |
| 392 |
| I.CMPW{lsrc, rsrc} => I.CMPW{lsrc=operand lsrc, rsrc=operand rsrc} |
| 393 |
| I.CMPB{lsrc, rsrc} => I.CMPB{lsrc=operand lsrc, rsrc=operand rsrc} |
| 394 |
| I.TESTL{lsrc, rsrc} => I.TESTL{lsrc=operand lsrc, rsrc=operand rsrc} |
| 395 |
| I.TESTW{lsrc, rsrc} => I.TESTW{lsrc=operand lsrc, rsrc=operand rsrc} |
| 396 |
| I.TESTB{lsrc, rsrc} => I.TESTB{lsrc=operand lsrc, rsrc=operand rsrc} |
| 397 |
| I.BITOP{bitOp, lsrc, rsrc} => |
| 398 |
I.BITOP{bitOp=bitOp, lsrc=operand lsrc, rsrc=operand rsrc} |
| 399 |
| I.BINARY{binOp, src, dst} => |
| 400 |
I.BINARY{binOp=binOp, src=operand src, dst=operand dst} |
| 401 |
| I.SHIFT{shiftOp, src, dst, count} => |
| 402 |
I.SHIFT{shiftOp=shiftOp, src=operand src, dst=operand dst, |
| 403 |
count=operand src} |
| 404 |
(* end case *)) |
| 405 |
|
| 406 |
fun f(I.ANNOTATION{a,i}) = |
| 407 |
I.ANNOTATION{i=rewriteUse(i, rs, rt), |
| 408 |
a = case a of |
| 409 |
CB.DEF_USE{cellkind=CB.GP,defs,uses} => |
| 410 |
CB.DEF_USE{cellkind=CB.GP,uses=map replace uses, |
| 411 |
defs=defs} |
| 412 |
| _ => a} |
| 413 |
| f(I.INSTR i) = I.INSTR(rewrite(i)) |
| 414 |
| f(I.COPY{k as CB.GP, sz, dst, src, tmp}) = |
| 415 |
I.COPY{k=k, sz=sz, dst=dst, src=List.map replace src, tmp=tmp} |
| 416 |
in |
| 417 |
f (instr:I.instruction) |
| 418 |
end |
| 419 |
|
| 420 |
|
| 421 |
fun frewriteDef _ = raise Fail "" |
| 422 |
fun frewriteUse _ = raise Fail "" |
| 423 |
end |
| 424 |
structure SpillInstr = AMD64SpillInstr ( |
| 425 |
structure I = I |
| 426 |
structure Props = AMD64InsnProps |
| 427 |
val floats16ByteAligned = true) |
| 428 |
structure SpillHeur = ChaitinSpillHeur |
| 429 |
structure Spill = RASpill (structure InsnProps = AMD64InsnProps |
| 430 |
structure Asm = AMD64Asm) |
| 431 |
|
| 432 |
datatype spillOperandKind = SPILL_LOC | CONST_VAL |
| 433 |
type spill_info = unit |
| 434 |
fun beforeRA _ = () |
| 435 |
|
| 436 |
val architecture = "amd64" |
| 437 |
fun pure _ = true |
| 438 |
|
| 439 |
structure Int = |
| 440 |
struct |
| 441 |
val allRegs = C.Regs CellsBasis.GP {from=0, to=15, step=1} |
| 442 |
val allRegsSet = List.foldl C.addReg C.empty allRegs |
| 443 |
val dedicated = [C.rsp, C.rbp] |
| 444 |
val avail = C.getReg (List.foldl C.rmvReg allRegsSet dedicated) |
| 445 |
fun spillLoc _ = raise Fail "" |
| 446 |
val mode = RACore.NO_OPTIMIZATION |
| 447 |
end |
| 448 |
structure Float = |
| 449 |
struct |
| 450 |
val avail = C.Regs CellsBasis.FP {from=0, to=15, step=1} |
| 451 |
val dedicated = [] |
| 452 |
fun spillLoc _ = raise Fail "" |
| 453 |
val mode = Word.orb (RACore.HAS_PARALLEL_COPIES, RACore.DEAD_COPY_ELIM) |
| 454 |
end |
| 455 |
|
| 456 |
) |
| 457 |
|
| 458 |
structure RA = RA2 |
| 459 |
structure Cells = AMD64Instr.C |
| 460 |
structure T = AMD64MLTree |
| 461 |
structure CFG = AMD64CFG |
| 462 |
structure FlowGraph = AMD64FlowGraph |
| 463 |
structure ChkTy = MLTreeCheckTy(structure T = T val intTy = 64) |
| 464 |
val wordTy = 64 |
| 465 |
|
| 466 |
structure MC = AMD64MCFn( |
| 467 |
structure Instr = AMD64Instr |
| 468 |
structure Shuffle = AMD64Shuffle |
| 469 |
structure MLTreeEval = AMD64MLTreeEval |
| 470 |
) |
| 471 |
|
| 472 |
fun gen (functionName, stms, result) = let |
| 473 |
val insnStrm = FlowGraph.build() |
| 474 |
val stream as AMD64Stream.STREAM |
| 475 |
{ beginCluster, (* start a cluster *) |
| 476 |
endCluster, (* end a cluster *) |
| 477 |
emit, (* emit MLTREE stm *) |
| 478 |
defineLabel, (* define a local label *) |
| 479 |
entryLabel, (* define an external entry *) |
| 480 |
exitBlock, (* mark the end of a procedure *) |
| 481 |
pseudoOp, (* emit a pseudo op *) |
| 482 |
annotation, (* add an annotation *) |
| 483 |
... } = |
| 484 |
AMD64.selectInstructions insnStrm |
| 485 |
fun doit () = ( |
| 486 |
beginCluster 0; (* start a new cluster *) |
| 487 |
pseudoOp PseudoOpsBasisTyp.TEXT; |
| 488 |
pseudoOp (PseudoOpsBasisTyp.EXPORT [functionName]); |
| 489 |
entryLabel functionName; (* define the entry label *) |
| 490 |
List.app emit stms; (* emit all the statements *) |
| 491 |
exitBlock result; |
| 492 |
endCluster []) |
| 493 |
val cfg = doit () |
| 494 |
val cfg = RA.run cfg |
| 495 |
val cfg = AMD64Expand.run cfg |
| 496 |
in |
| 497 |
(cfg, stream) (* end the cluster *) |
| 498 |
end |
| 499 |
|
| 500 |
fun dumpOutput (cfg, stream) = let |
| 501 |
val (cfg as Graph.GRAPH graph, blocks) = |
| 502 |
AMD64BlockPlacement.blockPlacement cfg |
| 503 |
val CFG.INFO{annotations=an, data, decls, ...} = #graph_info graph |
| 504 |
in |
| 505 |
AMD64Emit.asmEmit (cfg, blocks) |
| 506 |
end (* dumpOutput *) |
| 507 |
|
| 508 |
|
| 509 |
fun codegen (functionName, target, proto, initStms, args) = let |
| 510 |
val _ = Label.reset() |
| 511 |
|
| 512 |
val [functionName, target] = List.map Label.global [functionName, target] |
| 513 |
|
| 514 |
(* construct the C call *) |
| 515 |
val {result, callseq} = AMD64CCall.genCall { |
| 516 |
name=T.LABEL target, |
| 517 |
paramAlloc=fn _ => false, |
| 518 |
structRet=fn _ => T.REG (64, Cells.rax), |
| 519 |
saveRestoreDedicated=fn _ => {save=[], restore=[]}, |
| 520 |
callComment=NONE, |
| 521 |
proto=proto, |
| 522 |
args=args} |
| 523 |
|
| 524 |
fun wordLit i = T.LI (T.I.fromInt (wordTy, i)) |
| 525 |
|
| 526 |
val stms = List.concat [ |
| 527 |
[T.EXT(AMD64InstrExt.PUSHQ(T.REG(64, Cells.rbp))), |
| 528 |
T.COPY (wordTy, [Cells.rbp], [Cells.rsp])], |
| 529 |
initStms, |
| 530 |
callseq, |
| 531 |
[T.EXT(AMD64InstrExt.LEAVE)], |
| 532 |
[T.RET []]] |
| 533 |
|
| 534 |
(* val _ = List.all (fn stm => ChkTy.check stm |
| 535 |
orelse raise Fail ("typechecking error: "^AMD64MTC.AMD64MLTreeUtils.stmToString stm)) |
| 536 |
stms |
| 537 |
*) |
| 538 |
|
| 539 |
in |
| 540 |
dumpOutput(gen (functionName, stms, result)) |
| 541 |
end |
| 542 |
|
| 543 |
|
| 544 |
fun lit i = T.LI (T.I.fromInt (wordTy, i)) |
| 545 |
(* machine-specific data *) |
| 546 |
val wordTy = 64 |
| 547 |
val wordSzB = wordTy div 8 |
| 548 |
val param0 = T.REG(wordTy, Cells.rdi) |
| 549 |
|
| 550 |
(* maximum argument size in machine words *) |
| 551 |
val maxArgSz = 16 |
| 552 |
val maxArgSzB = maxArgSz * wordSzB |
| 553 |
|
| 554 |
in |
| 555 |
structure X86_64Test = GenTestFn ( |
| 556 |
structure T = AMD64MLTree |
| 557 |
structure CCall = AMD64CCall |
| 558 |
structure Cells = AMD64Cells |
| 559 |
val codegen = codegen |
| 560 |
val param0 = param0 |
| 561 |
val wordTy = 64) |
| 562 |
end (* local *) |
| 563 |
|
| 564 |
(* |
| 565 |
(* unit testing code *) |
| 566 |
structure Test = |
| 567 |
struct |
| 568 |
|
| 569 |
open CCall |
| 570 |
|
| 571 |
fun li2k (_, k, _) = k |
| 572 |
|
| 573 |
val ty1 = CTy.C_STRUCT [CTy.C_STRUCT [CTy.C_unsigned CTy.I_char, CTy.C_unsigned CTy.I_int]] |
| 574 |
val ty2 = CTy.C_STRUCT [CTy.C_signed CTy.I_short] |
| 575 |
val ty3 = CTy.C_STRUCT [CTy.C_signed CTy.I_short, CTy.C_PTR] |
| 576 |
val ty4 = CTy.C_STRUCT [CTy.C_PTR, CTy.C_PTR] |
| 577 |
val ty4 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_unsigned CTy.I_int], CTy.C_PTR] |
| 578 |
val ty5 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_float]] |
| 579 |
val ty6 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float]] |
| 580 |
val ty7 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_STRUCT[CTy.C_float,CTy.C_float],CTy.C_float,CTy.C_float]] |
| 581 |
val ty8 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_STRUCT[CTy.C_float,CTy.C_unsigned CTy.I_int],CTy.C_float,CTy.C_float]] |
| 582 |
val ty9 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float]] |
| 583 |
val ty10 = CTy.C_STRUCT [CTy.C_STRUCT[CTy.C_float,CTy.C_float, CTy.C_STRUCT[CTy.C_float,CTy.C_unsigned CTy.I_int]]] |
| 584 |
val ty11 = CTy.C_STRUCT [CTy.C_PTR, CTy.C_float, CTy.C_float, CTy.C_float] |
| 585 |
|
| 586 |
fun kindOfEB () = let |
| 587 |
fun test (eb, k) = (kindOfEightByte eb = k) orelse raise Fail "failed test" |
| 588 |
fun eb1 ty = hd (eightBytesOfCTy ty) |
| 589 |
fun eb2 ty = hd(tl (eightBytesOfCTy ty)) |
| 590 |
in |
| 591 |
List.all test [(eb1 ty1, K_GPR), (eb1 ty2, K_GPR), (eb2 ty3, K_GPR), |
| 592 |
(eb1 ty5, K_FPR), (eb1 ty6, K_FPR), (eb2 ty6, K_FPR), |
| 593 |
(eb1 ty7, K_FPR), (eb2 ty7, K_FPR), |
| 594 |
(eb1 ty8, K_GPR), (eb2 ty8, K_FPR)] |
| 595 |
end |
| 596 |
|
| 597 |
fun slots () = let |
| 598 |
fun test (lis : SA.slot list, ks2 : location_kind list) = let |
| 599 |
val ks1 = List.map li2k lis |
| 600 |
in |
| 601 |
(List.length ks1 = List.length ks2) andalso (ListPair.all (op =) (ks1, ks2)) |
| 602 |
end |
| 603 |
val tests = [ |
| 604 |
(ty2, [K_GPR]), |
| 605 |
(ty1, [K_GPR]), |
| 606 |
(ty3, [K_GPR, K_GPR]), |
| 607 |
(ty4, [K_GPR, K_GPR]), |
| 608 |
(ty5, [K_FPR]), |
| 609 |
(ty6, [K_FPR, K_FPR]), |
| 610 |
(ty7, [K_FPR, K_FPR]), |
| 611 |
(ty8, [K_GPR, K_FPR]), |
| 612 |
(ty11, [K_MEM, K_MEM, K_MEM]) |
| 613 |
] |
| 614 |
val (ts, anss) = ListPair.unzip tests |
| 615 |
in |
| 616 |
ListPair.all test (List.map slotsOfCTy ts, anss) orelse raise Fail "failed test" |
| 617 |
end |
| 618 |
end |
| 619 |
*) |
| 620 |
|