Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/sparc/sparcAsm.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/sparc/sparcAsm.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (view) (download)

1 : monnier 129 functor SparcAsmEmitter
2 :     (structure Instr : SPARCINSTR
3 : monnier 167 structure PseudoOps : PSEUDO_OPS
4 :     structure Shuffle : SPARCSHUFFLE where I = Instr
5 : monnier 129 ) : EMITTER_NEW =
6 :     struct
7 :     structure I = Instr
8 :     structure C = I.C
9 : monnier 167 structure P = PseudoOps
10 : monnier 129 structure R = I.Region
11 :     structure Constant = I.Constant
12 :    
13 :     fun ms n = if n < 0 then ("-" ^ Int.toString (~n)) else Int.toString n
14 :    
15 :     fun error msg = MLRiscErrorMsg.impossible ("SparcAsmEmitter." ^ msg)
16 :    
17 :     fun emit s = TextIO.output(!AsmStream.asmOutStream,s)
18 :    
19 : monnier 167 fun pseudoOp pOp = emit(P.toString pOp)
20 : monnier 129
21 :     fun defineLabel lab = emit(Label.nameOf lab ^ ":\n")
22 :    
23 :     fun comment msg = emit ("\t/* " ^ msg ^ " */")
24 :    
25 :     fun init size = (comment ("Code size = " ^ ms size))
26 :    
27 :     fun emitInstr(instr,regmap) =
28 :     let
29 :     fun rmap r = Intmap.map regmap r handle _ => r
30 :     fun eReg i = emit(I.C.cellToString(rmap i,I.C.GP))
31 :     fun eReg' i = emit(I.C.cellToString(rmap i,I.C.GP))
32 :     fun fReg f = emit(I.C.cellToString(rmap f,I.C.FP))
33 :     fun fReg' f = emit(I.C.cellToString(rmap f,I.C.FP))
34 :     fun fReg2 f = emit(I.C.cellToString(rmap f+1,I.C.FP))
35 :     fun fReg2' f = emit(I.C.cellToString(rmap f+1,I.C.FP))
36 :     fun eLabel lab = emit (Label.nameOf lab)
37 :     fun nl() = emit "\n"
38 :     fun tab() = emit "\t"
39 :     fun comma() = emit ", "
40 :     fun eInt i = emit (ms i)
41 :     fun emita a = emit (if a then ",a\t" else "\t")
42 :    
43 :     fun emitLExp lexp = emit(LabelExp.toString lexp)
44 :     fun eOp' f (I.REG r) = f r
45 :     | eOp' f (I.IMMED n) = eInt n
46 :     | eOp' f (I.LO l) = (emit "%lo("; emitLExp l; emit ")")
47 :     | eOp' f (I.HI l) = (emit "%hi("; emitLExp l; emit ")")
48 :     | eOp' f (I.LAB l) = emitLExp l
49 :     | eOp' f (I.CONST c) = emit(Constant.toString c)
50 :    
51 :     val eOp = eOp' eReg
52 :     val eFop = eOp' fReg
53 :    
54 : monnier 167 fun eAddr'(r,i) =
55 :     (eReg r;
56 : monnier 129 case i of
57 :     (I.REG 0 | I.IMMED 0) => ()
58 :     | I.IMMED n => (if n > 0 then emit "+" else (); eInt n)
59 : monnier 167 | i => (emit "+"; eOp i)
60 : monnier 129 )
61 :    
62 : monnier 167 fun eAddr(r,i) = (emit "["; eAddr'(r,i); emit "]")
63 :    
64 : monnier 129 fun arith3(x,cc,r,i,d) =
65 :     (tab(); emit x; if cc then emit "cc" else ();
66 :     tab(); eReg r; comma(); eOp i; comma(); eReg' d)
67 :     fun eLoad(x,r,i,d) =
68 :     (tab(); emit x; tab(); eAddr(r,i); comma(); eReg' d)
69 :     fun eStore(x,d,r,i) =
70 :     (tab(); emit x; tab(); eReg d; comma(); eAddr(r,i))
71 :     fun eFload(x,r,i,d) =
72 :     (tab(); emit x; tab(); eAddr(r,i); comma(); fReg' d)
73 :     fun eFstore(x,d,r,i) =
74 :     (tab(); emit x; tab(); fReg d; comma(); eAddr(r,i))
75 : monnier 167 fun eJump(r,i,0) =
76 :     (emit "\tjmp\t"; eAddr'(r,i))
77 :     | eJump(r,i,d) =
78 :     (emit "\tjmpl\t"; eAddr'(r,i); comma(); eReg' d)
79 : monnier 129 fun eMem mem = comment(R.toString mem)
80 :     fun eDelay false = ()
81 :     | eDelay true = emit "\n\tnop"
82 :    
83 :     fun arith I.ADD = "add"
84 :     | arith I.SUB = "sub"
85 :     | arith I.UMUL = "umul"
86 :     | arith I.SMUL = "smul"
87 :     | arith I.UDIV = "udiv"
88 :     | arith I.SDIV = "sdiv"
89 :     | arith I.AND = "and"
90 :     | arith I.ANDN = "andn"
91 :     | arith I.OR = "or"
92 :     | arith I.ORN = "orn"
93 :     | arith I.XOR = "xor"
94 :     | arith I.XNOR = "xnor"
95 :     | arith I.TADD = "tadd"
96 :     | arith I.TADDTV = "taddtv"
97 :     fun shift I.SLL = "sll"
98 :     | shift I.SRL = "srl"
99 :     | shift I.SRA = "srl"
100 :     fun load I.LD = "ld"
101 :     | load I.LDD = "ldd"
102 :     | load I.LDUB = "ldub"
103 :     | load I.LDSB = "ldsb"
104 :     | load I.LDSH = "ldsh"
105 :     | load I.LDUH = "lduh"
106 :     fun store I.ST = "st"
107 :     | store I.STB = "stb"
108 :     | store I.STH = "sth"
109 :     | store I.STD = "std"
110 : monnier 167 fun fload I.LDF = "ld"
111 :     | fload I.LDDF = "ldd"
112 :     | fload I.LDFSR = "ld"
113 :     fun fstore I.STF = "st"
114 :     | fstore I.STDF = "std"
115 :     | fstore I.STFSR = "st"
116 : monnier 129 fun cond I.BA = "a"
117 :     | cond I.BN = "n"
118 :     | cond I.BNE = "ne"
119 :     | cond I.BE = "e"
120 :     | cond I.BG = "g"
121 :     | cond I.BLE = "le"
122 :     | cond I.BGE = "ge"
123 :     | cond I.BL = "l"
124 :     | cond I.BGU = "gu"
125 :     | cond I.BLEU = "leu"
126 :     | cond I.BCC = "cc"
127 :     | cond I.BCS = "cs"
128 :     | cond I.BPOS = "pos"
129 :     | cond I.BNEG = "neg"
130 :     | cond I.BVC = "vc"
131 :     | cond I.BVS = "vs"
132 :     fun branch cc = "b"^cond cc
133 :     fun trap t = "t"^cond t
134 :     fun fbranch I.FBA = "fba"
135 :     | fbranch I.FBN = "fbn"
136 :     | fbranch I.FBU = "fbu"
137 :     | fbranch I.FBG = "fbg"
138 :     | fbranch I.FBUG = "fbug"
139 :     | fbranch I.FBL = "fbl"
140 :     | fbranch I.FBUL = "fbul"
141 :     | fbranch I.FBLG = "fblg"
142 :     | fbranch I.FBNE = "fbne"
143 :     | fbranch I.FBE = "fbe"
144 :     | fbranch I.FBUE = "fbue"
145 :     | fbranch I.FBGE = "fbge"
146 :     | fbranch I.FBUGE = "fbuge"
147 :     | fbranch I.FBLE = "fble"
148 :     | fbranch I.FBULE = "fbule"
149 :     | fbranch I.FBO = "fbo"
150 :     fun farith1 I.FiTOd = "fitod"
151 :     | farith1 I.FdTOi = "fdtoi"
152 :     | farith1 I.FsTOi = "fstoi"
153 :     | farith1 I.FiTOs = "fitos"
154 :     | farith1 I.FdTOs = "fdtos"
155 :     | farith1 I.FsTOd = "fstod"
156 :     | farith1 I.FMOVs = "fmovs"
157 :     | farith1 I.FABSs = "fabss"
158 :     | farith1 I.FNEGs = "fnegs"
159 :     | farith1 I.FSQRTs = "fsqrts"
160 :     | farith1 I.FSQRTd = "fsqrtd"
161 :     | farith1 _ = error "farith1"
162 :     fun farith2 I.FADDd = "faddd"
163 :     | farith2 I.FSUBd = "fsubd"
164 :     | farith2 I.FMULd = "fmuld"
165 :     | farith2 I.FDIVd = "fdivd"
166 :     | farith2 I.FADDs = "fadds"
167 :     | farith2 I.FSUBs = "fsubs"
168 :     | farith2 I.FMULs = "fmuls"
169 :     | farith2 I.FDIVs = "fdivs"
170 :     | farith2 I.FsMULd = "fsmuld"
171 :     fun fcmp I.FCMPd = "fcmpd"
172 :     | fcmp I.FCMPEd = "fcmped"
173 :     | fcmp I.FCMPs = "fcmps"
174 :     | fcmp I.FCMPEs = "fcmpes"
175 :     in
176 :     case instr of
177 :     I.LOAD{l,r,i,d,mem} => (eLoad(load l,r,i,d); eMem mem)
178 :     | I.STORE{s,d,r,i,mem} => (eStore(store s,d,r,i); eMem mem)
179 :     | I.FLOAD{l,r,i,d,mem} => (eFload(fload l,r,i,d); eMem mem)
180 :     | I.FSTORE{s,d,r,i,mem} => (eFstore(fstore s,d,r,i); eMem mem)
181 :     | I.SETHI {d=0,i=0} => emit "\tnop"
182 :     | I.SETHI {d,i} => (emit "\tsethi\t"; eInt i; comma(); eReg' d)
183 :     | I.ARITH {a=I.OR,cc=false,r=0,i=I.REG r,d} =>
184 :     (emit "\tmov\t"; eReg r; comma(); eReg' d)
185 :     | I.ARITH {a=I.OR,cc=false,r=0,i,d} =>
186 :     (emit "\tset\t"; eOp i; comma(); eReg' d)
187 :     | I.ARITH {a=I.SUB,cc=true,r,i,d=0} =>
188 :     (emit "\tcmp\t"; eReg r; comma(); eOp i)
189 :     | I.ARITH {a=I.TADDTV,cc=true,r,i,d} => arith3("taddcctv",false,r,i,d)
190 :     | I.ARITH {a,cc,r,i,d} => arith3(arith a,cc,r,i,d)
191 :     | I.SHIFT {s,r,i,d} => arith3(shift s,false,r,i,d)
192 :     | I.Bicc { b, a, label, nop} => (tab(); emit(branch b);
193 :     emita a; eLabel label; eDelay nop)
194 :     | I.FBfcc { b, a, label, nop} => (tab(); emit(fbranch b);
195 :     emita a; eLabel label; eDelay nop)
196 : monnier 167 | I.JMP {r,i,nop,...} => (eJump(r,i,0); eDelay nop)
197 :     | I.JMPL {d,r,i,nop,...} => (eJump(r,i,d); eDelay nop)
198 : monnier 129 | I.CALL {label,nop,...} => (emit "\tcall\t"; eLabel label; eDelay nop)
199 :     | I.RET{leaf=false,nop} => (emit "\tret"; eDelay nop)
200 :     | I.RET{leaf=true,nop} => (emit "\tretl"; eDelay nop)
201 :     | I.Ticc {t,r,i} => (tab();emit(trap t);emit "\t"; eAddr(r,i))
202 :     | I.FPop1 {a=I.FMOVd,r,d} =>
203 :     (emit "\tfmovs\t"; fReg r; comma(); fReg' d; nl();
204 :     emit "\tfmovs\t"; fReg2 r; comma(); fReg2' d)
205 :     | I.FPop1 {a=I.FNEGd,r,d} =>
206 :     (emit "\tfnegs\t"; fReg r; comma(); fReg' d; nl();
207 :     emit "\tfmovs\t"; fReg2 r; comma(); fReg2' d)
208 :     | I.FPop1 {a=I.FABSd,r,d} =>
209 :     (emit "\tfabds\t"; fReg r; comma(); fReg' d; nl();
210 :     emit "\tfmovs\t"; fReg2 r; comma(); fReg2' d)
211 :     | I.FPop1 {a,r,d} =>
212 :     (tab(); emit(farith1 a); tab(); fReg r; comma(); fReg' d)
213 :     | I.FPop2 {a,r1,r2,d} =>
214 :     (tab(); emit(farith2 a); tab();
215 :     fReg r1; comma(); fReg r2; comma(); fReg' d)
216 : monnier 167 | I.FCMP {cmp,r1,r2,nop} =>
217 : monnier 129 (tab(); emit(fcmp cmp); tab();
218 : monnier 167 fReg r1; comma(); fReg r2; eDelay nop)
219 : monnier 129 | I.COPY{src,dst,tmp,...} =>
220 :     (app (fn i => emitInstr(i,regmap))
221 :     (Shuffle.shuffle{src=src,dst=dst,temp=tmp,regMap=rmap}))
222 :     | I.FCOPY{src,dst,tmp,...} =>
223 :     (app (fn i => emitInstr(i,regmap))
224 :     (Shuffle.shufflefp{src=src,dst=dst,temp=tmp,regMap=rmap}))
225 :     | I.SAVE{r,i,d} => arith3("save",false,r,i,d)
226 :     | I.RESTORE{r,i,d} => arith3("restore",false,r,i,d)
227 : monnier 167 | I.RDY{d} => (emit "\trd\t%y, "; eReg' d)
228 :     | I.WRY{r,i} => (emit "\twr\t"; eReg r; comma(); eOp i; emit ", %y")
229 : monnier 129 (*
230 :     | I.ANNOTATION(i,a) => (emitInstr(i,regmap); comment(I.A.toString a))
231 :     *)
232 :     ;
233 :     nl()
234 :     end
235 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0