| 1 |
structure AMD64Demo = |
| 2 |
struct |
| 3 |
structure C = AMD64Instr.C |
| 4 |
structure T = AMD64MLTree |
| 5 |
structure CFG = AMD64CFG |
| 6 |
|
| 7 |
fun codegen (functionName, mltreeStms, ls) = let |
| 8 |
val _ = Label.reset() |
| 9 |
val insnStrm = AMD64FlowGraph.build() |
| 10 |
val stream as AMD64Stream.STREAM |
| 11 |
{ beginCluster, (* start a cluster *) |
| 12 |
endCluster, (* end a cluster *) |
| 13 |
emit, (* emit MLTREE stm *) |
| 14 |
defineLabel, (* define a local label *) |
| 15 |
entryLabel, (* define an external entry *) |
| 16 |
exitBlock, (* mark the end of a procedure *) |
| 17 |
pseudoOp, (* emit a pseudo op *) |
| 18 |
annotation, (* add an annotation *) |
| 19 |
... } = |
| 20 |
AMD64.selectInstructions insnStrm |
| 21 |
fun doit () = ( |
| 22 |
beginCluster 0; (* start a new cluster *) |
| 23 |
pseudoOp PseudoOpsBasisTyp.TEXT; |
| 24 |
entryLabel functionName; (* define the entry label *) |
| 25 |
app emit mltreeStms; (* emit all the statements *) |
| 26 |
exitBlock ls; |
| 27 |
endCluster []) |
| 28 |
val cfg = doit () |
| 29 |
val cfg = AMD64RA.run cfg |
| 30 |
val cfg = AMD64Expand.run cfg |
| 31 |
in |
| 32 |
(cfg, stream) (* end the cluster *) |
| 33 |
end (* codegen *) |
| 34 |
|
| 35 |
fun li i = T.LI (T.I.fromInt (32, i)) |
| 36 |
|
| 37 |
fun dumpOutput (cfg, stream) = let |
| 38 |
val (cfg as Graph.GRAPH graph, blocks) = |
| 39 |
AMD64BlockPlacement.blockPlacement cfg |
| 40 |
val CFG.INFO{annotations=an, data, decls, ...} = #graph_info graph |
| 41 |
in |
| 42 |
AMD64Emit.asmEmit (cfg, blocks) |
| 43 |
end (* dumpOutput *) |
| 44 |
|
| 45 |
structure CB = CellsBasis |
| 46 |
val [rax, rbx, rdi, rsi, rdx, rcx, r8, r9, r10, r11, r12, r13, r14, r15] = |
| 47 |
([C.rax, C.rbx, C.rdi, C.rsi, C.rdx, C.rcx] @ |
| 48 |
C.Regs CB.GP {from=8, to=15, step=1}) |
| 49 |
|
| 50 |
val [xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, |
| 51 |
xmm11, xmm12, xmm13, xmm14, xmm15] = |
| 52 |
C.Regs CB.FP {from=0, to=15, step=1} |
| 53 |
|
| 54 |
val callerSaves = [rax, rcx, rdx, rsi, rdi, r8, r9, r10, r11] |
| 55 |
|
| 56 |
structure CTy = CTypes |
| 57 |
val intTy = CTy.C_signed CTy.I_int |
| 58 |
fun reg r = T.REG (64, r) |
| 59 |
fun gpr r = T.GPR (reg r) |
| 60 |
fun fpr r = T.FPR (T.FREG (32, r)) |
| 61 |
|
| 62 |
fun eg () = let |
| 63 |
val l = Label.label "eg" () |
| 64 |
val raxR = T.GPR (T.REG (32, C.rax)) |
| 65 |
val f1 = C.newFreg () |
| 66 |
val f2 = C.newFreg () |
| 67 |
val f3 = C.newFreg () |
| 68 |
val r1 = C.newReg () |
| 69 |
val r2 = C.newReg () |
| 70 |
val deds as [rax, rdi, rsi, rbx, rsp, r10] = |
| 71 |
[C.rax, C.rdi, C.rsi, C.rbx, C.rsp, C.GPReg 10] |
| 72 |
val tmps = map (fn _ => C.newReg ()) deds |
| 73 |
(* c-calls stuff *) |
| 74 |
val ccL = Label.label "cFun" () |
| 75 |
val numArgs = 0 |
| 76 |
fun genArgs 0 = [] |
| 77 |
| genArgs i = CCalls.ARG (li (numArgs-i+1)) :: genArgs (i-1) |
| 78 |
val args = genArgs numArgs |
| 79 |
val fpArgs = [CCalls.FARG (T.FREG (32, f1)), CCalls.FARG (T.FREG (32, f2))] |
| 80 |
val fpTys = [CTy.C_float, CTy.C_float] |
| 81 |
val paramTys = map (fn _ => intTy) args @ fpTys |
| 82 |
val proto = {conv="", retTy=CTy.C_void, paramTys=paramTys} |
| 83 |
(* val {result, callseq} = CCalls.genCall { |
| 84 |
name=T.LABEL ccL, |
| 85 |
paramAlloc=fn _ => false, |
| 86 |
structRet=fn _ => T.REG (64, C.rax), |
| 87 |
saveRestoreDedicated=fn _ => {save=[], restore=[]}, |
| 88 |
callComment=NONE, |
| 89 |
proto=proto, |
| 90 |
args=args @ fpArgs |
| 91 |
} |
| 92 |
*) |
| 93 |
val stms = [ |
| 94 |
T.FMV (64, f3, T.FADD (64, T.FLOAD (64, T.ADD (64, li 128, T.REG (64, r2)), ()), T.FLOAD (64, T.ADD (64, li 128, T.REG (64, r1)), ()))), |
| 95 |
T.FSTORE (64, T.REG (64, r1), T.FREG (64, f3), ()) |
| 96 |
] |
| 97 |
(* val stms' = [ |
| 98 |
T.FMV (32, f1, T.FLOAD (32, T.ADD (64, li 128, T.REG (64, r1)), ())), |
| 99 |
T.FMV (32, f2, T.FADD (32, T.FREG (32, f1), T.FREG (32, f1))), |
| 100 |
T.FCOPY (32, [xmm0, xmm1], [f1, f2]), |
| 101 |
T.CALL {funct=T.LABEL ccL, targets=[], defs=[], uses=[fpr xmm0, fpr xmm1], |
| 102 |
region=UserRegion.memory, pops=0} |
| 103 |
] |
| 104 |
val stms = |
| 105 |
[ T.FMV (32, f1, T.FLOAD (32, T.ADD (64, li 128, T.REG (64, r1)), ())), |
| 106 |
T.COPY (64, tmps, deds) |
| 107 |
] |
| 108 |
val stms1 = [ |
| 109 |
T.MV (64, r1, li 999), |
| 110 |
T.MV (64, rax, T.COND (64, T.CMP (64, T.EQ, reg r1, li 123), li 11, li 22)) |
| 111 |
(* T.MV (64, r1, T.LABEL ccL) |
| 112 |
T.STORE (64, T.REG (64, r1), T.LABEL ccL, ())*) |
| 113 |
] *) |
| 114 |
(*@ [ |
| 115 |
T.COPY (64, deds, tmps), |
| 116 |
T.MV (32, r2, T.MULS (32, li 5, T.ADD (32, T.REG (32, C.rax), T.REG (32, r1)))), |
| 117 |
T.BCC (T.FCMP (32, T.<, T.FREG (32, f2), T.FLOAD (32, T.REG (64, r1), ())), ccL), |
| 118 |
T.FSTORE (32, T.REG (64, r1), T.FREG (32, f1), ()), |
| 119 |
T.FMV (32, f1, T.CVTF2F (64, 32, T.FLOAD (64, T.REG (64, C.rbx), ()))), |
| 120 |
T.FMV (64, f2, T.CVTI2F (64, 64, T.LOAD (64, T.REG (64, C.rbx), ()))), |
| 121 |
T.FMV (64, f3, T.FADD (64, T.FREG (64, f1), T.FREG (64, f2))), |
| 122 |
T.FMV (32, f1, T.CVTI2F (32, 32, T.REG (32, C.rax))), |
| 123 |
T.JMP (T.LABEL ccL, []) |
| 124 |
] *) |
| 125 |
in |
| 126 |
dumpOutput (codegen (l, stms, [raxR])) |
| 127 |
end |
| 128 |
|
| 129 |
end (* AMD64Demo *) |
| 130 |
|
| 131 |
structure D = AMD64Demo |