| 1 |
(*--------------------------------------------------------------------------- |
| 2 |
* Backend specific stuff. You'll need one instance of these things |
| 3 |
* for each architecture. |
| 4 |
*---------------------------------------------------------------------------*) |
| 5 |
|
| 6 |
(* |
| 7 |
* The Sparc instruction set, specialized with respect to the |
| 8 |
* user constant and region types. |
| 9 |
*) |
| 10 |
structure SparcInstr = SparcInstr |
| 11 |
(structure LabelExp = LabelExp |
| 12 |
structure Region = UserRegion |
| 13 |
) |
| 14 |
|
| 15 |
(* |
| 16 |
* How to serialize parallel copies |
| 17 |
*) |
| 18 |
structure SparcShuffle = SparcShuffle(SparcInstr) |
| 19 |
|
| 20 |
(* |
| 21 |
* The assembler |
| 22 |
*) |
| 23 |
structure SparcAsm = SparcAsmEmitter |
| 24 |
(structure Instr = SparcInstr |
| 25 |
structure Stream = Stream |
| 26 |
structure Shuffle = SparcShuffle |
| 27 |
val V9 = false (* we'll generate V8 instructions for now *) |
| 28 |
) |
| 29 |
|
| 30 |
(* |
| 31 |
* The flowgraph (cluster) representation specialized to the sparc instruction |
| 32 |
* set. |
| 33 |
*) |
| 34 |
structure SparcFlowGraph = |
| 35 |
FlowGraph(structure I = SparcInstr |
| 36 |
structure P = UserPseudoOps |
| 37 |
) |
| 38 |
(* |
| 39 |
* Because of various Sparc related ugliness. Pseudo instructions |
| 40 |
* related to integer multiplication/division are handled via callbacks. |
| 41 |
* Here we can decide what actual code to generate. Here we only |
| 42 |
* handle a subset of of the pseudo instructions. |
| 43 |
*) |
| 44 |
structure SparcPseudoInstrs = |
| 45 |
struct |
| 46 |
structure I = SparcInstr |
| 47 |
structure C = SparcInstr.C |
| 48 |
|
| 49 |
type format1 = |
| 50 |
{r:C.cell, i:I.operand, d:C.cell} * |
| 51 |
(I.operand -> I.C.cell) -> I.instruction list |
| 52 |
|
| 53 |
type format2 = |
| 54 |
{i:I.operand, d:C.cell} * |
| 55 |
(I.operand -> I.C.cell) -> I.instruction list |
| 56 |
|
| 57 |
fun error msg = MLRiscErrorMsg.impossible ("SparcPseudoInstrs."^msg) |
| 58 |
|
| 59 |
fun umul32({r, i, d}, reduceOpnd) = [I.ARITH{a=I.UMUL,r=r,i=i,d=d}] |
| 60 |
fun smul32({r, i, d}, reduceOpnd) = [I.ARITH{a=I.SMUL,r=r,i=i,d=d}] |
| 61 |
fun udiv32({r,i,d},reduceOpnd) = |
| 62 |
[I.WRY{r=C.r0,i=I.REG(C.r0)},I.ARITH{a=I.UDIV,r=r,i=i,d=d}] |
| 63 |
|
| 64 |
fun sdiv32({r,i,d},reduceOpnd) = |
| 65 |
let val t1 = C.newReg() |
| 66 |
in [I.SHIFT{s=I.SRA,r=r,i=I.IMMED 31,d=t1}, |
| 67 |
I.WRY{r=t1,i=I.REG(C.r0)}, |
| 68 |
I.ARITH{a=I.SDIV,r=r,i=i,d=d} |
| 69 |
] |
| 70 |
end |
| 71 |
|
| 72 |
fun cvti2d({i,d},reduceOpnd) = error "cvti2d" |
| 73 |
(* There is no data path between integer and floating point registers. |
| 74 |
So we actually have to use some memory location for temporary |
| 75 |
This is commented out for now. |
| 76 |
*) |
| 77 |
(* |
| 78 |
[I.STORE{s=I.ST,r=C.stackptrR,i=floatTmpOffset,d=reduceOpnd i,mem=stack}, |
| 79 |
I.FLOAD{l=I.LDF,r=C.stackptrR,i=floatTmpOffset,d=d,mem=stack}, |
| 80 |
I.FPop1{a=I.FiTOd,r=d,d=d} |
| 81 |
] |
| 82 |
*) |
| 83 |
fun cvti2s _ = error "cvti2s" |
| 84 |
fun cvti2q _ = error "cvti2q" |
| 85 |
|
| 86 |
fun smul32trap _ = error "smul32trap" |
| 87 |
fun sdiv32trap _ = error "sdiv32trap" |
| 88 |
|
| 89 |
val overflowtrap32 = [] (* not needed *) |
| 90 |
val overflowtrap64 = [] (* not needed *) |
| 91 |
end |
| 92 |
|
| 93 |
|
| 94 |
(* |
| 95 |
* Instruction selection module for Sparc. |
| 96 |
*) |
| 97 |
structure SparcMLTreeComp = |
| 98 |
Sparc(structure SparcInstr = SparcInstr |
| 99 |
structure SparcMLTree = MLTree |
| 100 |
structure PseudoInstrs = SparcPseudoInstrs |
| 101 |
structure ExtensionComp = UserMLTreeExtComp |
| 102 |
(structure I = SparcInstr |
| 103 |
structure T = SparcMLTree |
| 104 |
) |
| 105 |
(* Some sparc specific parameters *) |
| 106 |
val V9 = false |
| 107 |
val muluCost = ref 5 |
| 108 |
val multCost = ref 3 |
| 109 |
val divuCost = ref 5 |
| 110 |
val divtCost = ref 5 |
| 111 |
val registerwindow = ref false |
| 112 |
val useBR = ref false |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
(*--------------------------------------------------------------------------- |
| 117 |
* Okay. Finally, we can tie the front-end and back-end together. |
| 118 |
*---------------------------------------------------------------------------*) |
| 119 |
structure SparcBackEnd = |
| 120 |
BackEnd |
| 121 |
(structure Flowgraph = SparcFlowGraph |
| 122 |
structure MLTreeComp = SparcMLTreeComp |
| 123 |
structure InsnProps = SparcProps(SparcInstr) |
| 124 |
structure Asm = SparcAsm |
| 125 |
|
| 126 |
structure RA = |
| 127 |
RISC_RA |
| 128 |
(structure I = SparcInstr |
| 129 |
structure Flowgraph = Flowgraph |
| 130 |
structure Asm = Asm |
| 131 |
structure InsnProps = InsnProps |
| 132 |
structure Spill = RASpill(structure Asm = Asm |
| 133 |
structure InsnProps = InsnProps) |
| 134 |
structure Rewrite = SparcRewrite(SparcInstr) |
| 135 |
structure SpillHeur = ChaitinSpillHeur |
| 136 |
structure C = I.C |
| 137 |
|
| 138 |
val sp = C.stackptrR |
| 139 |
val spill = UserRegion.spill |
| 140 |
|
| 141 |
structure SpillTable = SpillTable |
| 142 |
(val initialSpillOffset = 0 (* This is probably wrong!!!!! *) |
| 143 |
val spillAreaSz = 4000 |
| 144 |
val architecture = "Sparc" |
| 145 |
) |
| 146 |
open SpillTable |
| 147 |
|
| 148 |
fun pure(I.ANNOTATION{i,...}) = pure i |
| 149 |
| pure(I.LOAD _) = true |
| 150 |
| pure(I.FLOAD _) = true |
| 151 |
| pure(I.SETHI _) = true |
| 152 |
| pure(I.SHIFT _) = true |
| 153 |
| pure(I.FPop1 _) = true |
| 154 |
| pure(I.FPop2 _) = true |
| 155 |
| pure _ = false |
| 156 |
|
| 157 |
(* I'm assuming only r0 and the stack pointer is dedicated *) |
| 158 |
structure Int = |
| 159 |
struct |
| 160 |
val dedicated = [I.C.stackptrR, I.C.GPReg 0] |
| 161 |
val avail = |
| 162 |
C.SortedCells.return |
| 163 |
(C.SortedCells.difference( |
| 164 |
C.SortedCells.uniq( |
| 165 |
C.Regs C.GP {from=0, to=31, step=1}), |
| 166 |
C.SortedCells.uniq dedicated) |
| 167 |
) |
| 168 |
|
| 169 |
fun copy((rds as [_], rss as [_]), _) = |
| 170 |
I.COPY{dst=rds, src=rss, impl=ref NONE, tmp=NONE} |
| 171 |
| copy((rds, rss), I.COPY{tmp, ...}) = |
| 172 |
I.COPY{dst=rds, src=rss, impl=ref NONE, tmp=tmp} |
| 173 |
|
| 174 |
(* spill copy temp *) |
| 175 |
fun spillCopyTmp(_,I.COPY{dst,src,tmp,impl},loc) = |
| 176 |
I.COPY{dst=dst, src=src, impl=impl, |
| 177 |
tmp=SOME(I.Displace{base=sp, disp=get loc})} |
| 178 |
|
| 179 |
(* spill register *) |
| 180 |
fun spillInstr{an,src,spilledCell,spillLoc} = |
| 181 |
[I.STORE{s=I.ST, r=sp, i=I.IMMED(get spillLoc), d=src, |
| 182 |
mem=spill}] |
| 183 |
|
| 184 |
(* reload register *) |
| 185 |
fun reloadInstr{an,dst,spilledCell,spillLoc} = |
| 186 |
[I.LOAD{l=I.LD, r=sp, i=I.IMMED(get spillLoc), d=dst, |
| 187 |
mem=spill}] |
| 188 |
end |
| 189 |
|
| 190 |
structure Float = |
| 191 |
struct |
| 192 |
val dedicated = [] |
| 193 |
val avail = C.Regs C.FP {from=0, to=31, step=2} |
| 194 |
|
| 195 |
fun copy((fds as [_], fss as [_]), _) = |
| 196 |
I.FCOPY{dst=fds, src=fss, impl=ref NONE, tmp=NONE} |
| 197 |
| copy((fds, fss), I.FCOPY{tmp, ...}) = |
| 198 |
I.FCOPY{dst=fds, src=fss, impl=ref NONE, tmp=tmp} |
| 199 |
|
| 200 |
fun spillCopyTmp(_,I.FCOPY{dst,src,tmp,impl},loc) = |
| 201 |
I.FCOPY{dst=dst, src=src, impl=impl, |
| 202 |
tmp=SOME(I.Displace{base=sp, disp=getF loc})} |
| 203 |
|
| 204 |
fun spillInstr(_, d,loc) = |
| 205 |
[I.FSTORE{s=I.STDF, r=sp, i=I.IMMED(getF loc), d=d, mem=spill}] |
| 206 |
|
| 207 |
fun reloadInstr(_,d,loc) = |
| 208 |
[I.FLOAD{l=I.LDDF, r=sp, i=I.IMMED(getF loc), d=d, mem=spill}] |
| 209 |
end |
| 210 |
) |
| 211 |
) |