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/x86/instructions/x86Props.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/x86/instructions/x86Props.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 815 - (view) (download)

1 : monnier 247 (* x86Props.sml -- 32bit, x86 instruction semantic properties
2 :     *
3 :     * COPYRIGHT (c) 1997 Bell Laboratories.
4 :     *)
5 :    
6 : monnier 411 functor X86Props(X86Instr : X86INSTR) : INSN_PROPERTIES =
7 : monnier 247 struct
8 :     structure I = X86Instr
9 :     structure C = I.C
10 : george 545 structure LE = I.LabelExp
11 : leunga 775 structure T = I.T
12 : monnier 247
13 :     exception NegateConditional
14 :    
15 : monnier 411 fun error msg = MLRiscErrorMsg.error("X86Props",msg)
16 : monnier 247
17 : george 545 datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL
18 : leunga 796 | IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK
19 : monnier 247 datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES
20 :     (*========================================================================
21 :     * Instruction Kinds
22 :     *========================================================================*)
23 :     fun instrKind (I.JMP _) = IK_JUMP
24 :     | instrKind (I.JCC _) = IK_JUMP
25 : monnier 411 | instrKind (I.COPY _) = IK_COPY
26 :     | instrKind (I.FCOPY _) = IK_COPY
27 : leunga 796 | instrKind (I.CALL{cutsTo=_::_,...}) = IK_CALL_WITH_CUTS
28 : monnier 411 | instrKind (I.CALL _) = IK_CALL
29 : leunga 624 | instrKind (I.PHI _) = IK_PHI
30 :     | instrKind (I.SOURCE _) = IK_SOURCE
31 :     | instrKind (I.SINK _) = IK_SINK
32 : leunga 683 | instrKind (I.RET _) = IK_JUMP
33 : monnier 411 | instrKind (I.ANNOTATION{i,...}) = instrKind i
34 : monnier 247 | instrKind _ = IK_INSTR
35 :    
36 :    
37 :     fun moveInstr(I.COPY _) = true
38 :     | moveInstr(I.FCOPY _) = true
39 : monnier 498 | moveInstr(I.MOVE{mvOp=I.MOVL, src=I.Direct _, dst=I.MemReg _, ...}) = true
40 :     | moveInstr(I.MOVE{mvOp=I.MOVL, src=I.MemReg _, dst=I.Direct _, ...}) = true
41 : leunga 731 | moveInstr(I.FMOVE{fsize=I.FP64,src=I.FPR _,dst=I.FPR _, ...}) = true
42 :     | moveInstr(I.FMOVE{fsize=I.FP64,src=I.FPR _,dst=I.FDirect _, ...}) = true
43 :     | moveInstr(I.FMOVE{fsize=I.FP64,src=I.FDirect _,dst=I.FPR _, ...}) = true
44 :     | moveInstr(I.FMOVE{fsize=I.FP64,src=I.FDirect _,dst=I.FDirect _, ...}) = true
45 : monnier 411 | moveInstr(I.ANNOTATION{i,...}) = moveInstr i
46 : monnier 247 | moveInstr _ = false
47 :    
48 :     val nop = fn () => I.NOP
49 :    
50 :    
51 :     (*========================================================================
52 :     * Parallel Move
53 :     *========================================================================*)
54 :     fun moveTmpR(I.COPY{tmp=SOME(I.Direct r), ...}) = SOME r
55 :     | moveTmpR(I.FCOPY{tmp=SOME(I.FDirect f), ...}) = SOME f
56 : leunga 731 | moveTmpR(I.FCOPY{tmp=SOME(I.FPR f), ...}) = SOME f
57 : monnier 411 | moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i
58 : monnier 247 | moveTmpR _ = NONE
59 :    
60 :     fun moveDstSrc(I.COPY{src, dst, ...}) = (dst, src)
61 :     | moveDstSrc(I.FCOPY{src, dst, ...}) = (dst, src)
62 : monnier 498 | moveDstSrc(I.MOVE{src=I.Direct rs, dst=I.MemReg rd, ...}) = ([rd], [rs])
63 :     | moveDstSrc(I.MOVE{src=I.MemReg rs, dst=I.Direct rd, ...}) = ([rd], [rs])
64 : leunga 731 | moveDstSrc(I.FMOVE{src=I.FPR rs, dst=I.FPR rd, ...}) = ([rd], [rs])
65 :     | moveDstSrc(I.FMOVE{src=I.FDirect rs, dst=I.FPR rd, ...}) = ([rd], [rs])
66 :     | moveDstSrc(I.FMOVE{src=I.FPR rs, dst=I.FDirect rd, ...}) = ([rd], [rs])
67 :     | moveDstSrc(I.FMOVE{src=I.FDirect rs, dst=I.FDirect rd, ...}) = ([rd], [rs])
68 : monnier 411 | moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i
69 : monnier 247 | moveDstSrc _ = error "moveDstSrc"
70 :    
71 :     (*=====================================================================
72 :     * Branches and Calls/Returns
73 :     *=====================================================================*)
74 :     fun branchTargets(I.JMP(_, [])) = [ESCAPES]
75 :     | branchTargets(I.JMP(_, labs)) = map LABELLED labs
76 : monnier 429 | branchTargets(I.RET _) = [ESCAPES]
77 : leunga 775 | branchTargets(I.JCC{opnd=I.ImmedLabel(T.LABEL(lab)), ...}) =
78 : monnier 247 [FALLTHROUGH, LABELLED lab]
79 : leunga 796 | branchTargets(I.CALL{cutsTo, ...}) = FALLTHROUGH :: map LABELLED cutsTo
80 : monnier 411 | branchTargets(I.ANNOTATION{i,...}) = branchTargets i
81 : monnier 247 | branchTargets _ = error "branchTargets"
82 :    
83 : leunga 775 fun jump label = I.JMP (I.ImmedLabel(T.LABEL label), [label])
84 : monnier 247
85 :     exception NotImplemented
86 : monnier 429 fun setTargets(I.JMP(I.ImmedLabel _,_),[l]) = jump l
87 :     | setTargets(I.JMP(opnd,_),_) = error "setTargets"
88 :     | setTargets(I.JCC{cond,opnd=I.ImmedLabel _},[f,t]) =
89 : leunga 775 I.JCC{cond=cond,opnd=I.ImmedLabel(T.LABEL t)}
90 : monnier 429 | setTargets(I.JCC _,_) = error "setTargets"
91 :     | setTargets(I.ANNOTATION{i,a},l) = I.ANNOTATION{i=setTargets(i,l),a=a}
92 :     | setTargets(i,_) = i
93 : monnier 247 fun negateConditional _ = raise NotImplemented
94 :    
95 : monnier 411 val immedRange={lo= ~1073741824, hi=1073741823}
96 :     val toInt32 = Int32.fromLarge o Int.toLarge
97 :     fun loadImmed{immed,t} =
98 :     I.MOVE{mvOp=I.MOVL,src=I.Immed(toInt32 immed),dst=I.Direct t}
99 : leunga 657 fun loadOperand{opn,t} = I.MOVE{mvOp=I.MOVL,src=opn,dst=I.Direct t}
100 : monnier 411
101 :     (*=====================================================================
102 :     * Hashing and Equality on operands
103 :     *=====================================================================*)
104 :     fun hashOpn(I.Immed i) = Word.fromInt(Int32.toInt i)
105 : george 545 | hashOpn(I.ImmedLabel le) = LE.hash le + 0w123
106 : monnier 411 | hashOpn(I.Relative i) = Word.fromInt i + 0w1232
107 : george 545 | hashOpn(I.LabelEA le) = LE.hash le + 0w44444
108 : leunga 744 | hashOpn(I.Direct r) = C.hashCell r
109 :     | hashOpn(I.MemReg r) = C.hashCell r + 0w2123
110 :     | hashOpn(I.ST f) = C.hashCell f + 0w88
111 :     | hashOpn(I.FPR f) = C.hashCell f + 0w881
112 :     | hashOpn(I.FDirect f) = C.hashCell f + 0w31245
113 : george 545 | hashOpn(I.Displace {base, disp, ...}) =
114 : leunga 744 hashOpn disp + C.hashCell base
115 : monnier 411 | hashOpn(I.Indexed {base, index, scale, disp, ...}) =
116 : leunga 744 C.hashCell index + Word.fromInt scale + hashOpn disp
117 : monnier 411 fun eqOpn(I.Immed a,I.Immed b) = a = b
118 : george 545 | eqOpn(I.ImmedLabel a,I.ImmedLabel b) = LE.==(a,b)
119 : monnier 411 | eqOpn(I.Relative a,I.Relative b) = a = b
120 : george 545 | eqOpn(I.LabelEA a,I.LabelEA b) = LE.==(a,b)
121 : leunga 744 | eqOpn(I.Direct a,I.Direct b) = C.sameColor(a,b)
122 :     | eqOpn(I.MemReg a,I.MemReg b) = C.sameColor(a,b)
123 :     | eqOpn(I.FDirect a,I.FDirect b) = C.sameColor(a,b)
124 :     | eqOpn(I.ST a,I.ST b) = C.sameColor(a,b)
125 :     | eqOpn(I.FPR a,I.FPR b) = C.sameColor(a,b)
126 : monnier 411 | eqOpn(I.Displace{base=a,disp=b,...},I.Displace{base=c,disp=d,...}) =
127 : leunga 744 C.sameColor(a,c) andalso eqOpn(b,d)
128 : monnier 411 | eqOpn(I.Indexed{base=a,index=b,scale=c,disp=d,...},
129 :     I.Indexed{base=e,index=f,scale=g,disp=h,...}) =
130 : leunga 744 C.sameColor(b,f) andalso c = g
131 :     andalso sameCellOption(a,e) andalso eqOpn(d,h)
132 : monnier 411 | eqOpn _ = false
133 : leunga 744 and sameCellOption(NONE, NONE) = true
134 :     | sameCellOption(SOME x, SOME y) = C.sameColor(x,y)
135 :     | sameCellOption _ = false
136 : monnier 411
137 : monnier 247 (*========================================================================
138 :     * Definition and use (for register allocation mainly)
139 :     *========================================================================*)
140 :     val eaxPair = [C.edx, C.eax]
141 :    
142 :     fun defUseR instr = let
143 :     fun operandAcc(I.Direct r, acc) = r::acc
144 : monnier 498 | operandAcc(I.MemReg r, acc) = r::acc
145 : monnier 247 | operandAcc(I.Displace{base, ...}, acc) = base::acc
146 :     | operandAcc(I.Indexed{base=SOME b, index, ...}, acc) = b::index::acc
147 :     | operandAcc(I.Indexed{base=NONE, index, ...}, acc) = index::acc
148 :     | operandAcc(_, acc) = acc
149 :    
150 :     fun operandUse opnd = operandAcc(opnd, [])
151 :    
152 : leunga 731 fun operandUse2(src1, src2) = ([], operandAcc(src1, operandUse src2))
153 :     fun operandUse3(x, y, z) = ([], operandAcc(x, operandAcc(y, operandUse y)))
154 :    
155 : monnier 247 fun operandDef(I.Direct r) = [r]
156 : monnier 498 | operandDef(I.MemReg r) = [r]
157 : monnier 247 | operandDef _ = []
158 :    
159 :     fun multdiv{src, multDivOp} = let
160 :     val uses = operandUse src
161 :     in
162 :     case multDivOp
163 : leunga 815 of (I.IDIVL1 | I.DIVL1) => (eaxPair, C.edx::C.eax::uses)
164 :     | I.MULL1 => (eaxPair, C.eax::uses)
165 : monnier 247 end
166 :    
167 : george 545 fun unary opnd = (operandDef opnd, operandUse opnd)
168 :     fun cmptest{lsrc, rsrc} = ([], operandAcc(lsrc, operandUse rsrc))
169 : leunga 815 fun espOnly() = let val sp = [C.stackptrR] in (sp, sp) end
170 : leunga 593 fun push arg = ([C.stackptrR], operandAcc(arg, [C.stackptrR]))
171 : george 545 fun float opnd = ([], operandUse opnd)
172 : monnier 247 in
173 :     case instr
174 :     of I.JMP(opnd, _) => ([], operandUse opnd)
175 :     | I.JCC{opnd, ...} => ([], operandUse opnd)
176 : leunga 796 | I.CALL{opnd,defs,uses,...} =>
177 : leunga 744 (C.getReg defs, operandAcc(opnd, C.getReg uses))
178 : monnier 498 | I.MOVE{src, dst=I.Direct r, ...} => ([r], operandUse src)
179 :     | I.MOVE{src, dst=I.MemReg r, ...} => ([r], operandUse src)
180 : monnier 247 | I.MOVE{src, dst, ...} => ([], operandAcc(dst, operandUse src))
181 :     | I.LEA{r32, addr} => ([r32], operandUse addr)
182 : george 545 | ( I.CMPL arg | I.CMPW arg | I.CMPB arg
183 : leunga 646 | I.TESTL arg | I.TESTW arg | I.TESTB arg ) => cmptest arg
184 :     | I.BITOP{lsrc, rsrc, ...} => cmptest{lsrc=lsrc,rsrc=rsrc}
185 : george 545 | I.BINARY{binOp=I.XORL,src=I.Direct rs,dst=I.Direct rd,...} =>
186 : leunga 744 if C.sameColor(rs,rd) then ([rd],[]) else ([rd],[rs,rd])
187 : george 545 | I.BINARY{src,dst,...} =>
188 :     (operandDef dst, operandAcc(src, operandUse dst))
189 : leunga 797 | I.CMPXCHG{src, dst, ...} =>
190 :     (C.eax::operandDef dst, C.eax::operandAcc(src, operandUse dst))
191 : leunga 601 | I.ENTER _ => ([C.esp, C.ebp], [C.esp, C.ebp])
192 :     | I.LEAVE => ([C.esp, C.ebp], [C.esp, C.ebp])
193 : monnier 247 | I.MULTDIV arg => multdiv arg
194 : leunga 815 | I.MUL3{src1, dst, ...}=> ([dst], operandUse src1)
195 : monnier 247
196 : george 545 | I.UNARY{opnd, ...} => unary opnd
197 :     | I.SET{opnd, ...} => unary opnd
198 :     | ( I.PUSHL arg | I.PUSHW arg | I.PUSHB arg ) => push arg
199 : leunga 641 | I.POP arg => (C.stackptrR::operandDef arg, [C.stackptrR])
200 : leunga 815 | I.PUSHFD => espOnly()
201 :     | I.POPFD => espOnly()
202 : monnier 247 | I.CDQ => ([C.edx], [C.eax])
203 :    
204 :     | I.COPY{dst, src, tmp=SOME(I.Direct r), ...} => (r::dst, src)
205 :     | I.COPY{dst, src, ...} => (dst, src)
206 : george 555 | I.FSTPT opnd => float opnd
207 : george 545 | I.FSTPL opnd => float opnd
208 :     | I.FSTPS opnd => float opnd
209 : leunga 579 | I.FSTL opnd => float opnd
210 :     | I.FSTS opnd => float opnd
211 : george 545 | I.FLDL opnd => float opnd
212 :     | I.FLDS opnd => float opnd
213 :     | I.FILD opnd => float opnd
214 : leunga 565 | I.FILDL opnd => float opnd
215 :     | I.FILDLL opnd => float opnd
216 : monnier 247 | I.FBINARY{src, ...} => ([], operandUse src)
217 : leunga 565 | I.FIBINARY{src, ...} => ([], operandUse src)
218 : george 545 | I.FENV{opnd, ...} => ([], operandUse opnd)
219 : monnier 247 | I.FNSTSW => ([C.eax], [])
220 : leunga 731 | I.FUCOM opnd => float opnd
221 :     | I.FUCOMP opnd => float opnd
222 :    
223 :     | I.FMOVE{src, dst, ...} => operandUse2(src, dst)
224 :     | I.FILOAD{ea, dst, ...} => operandUse2(ea, dst)
225 :     | I.FCMP{lsrc, rsrc, ...} => operandUse2(lsrc, rsrc)
226 :     | I.FBINOP{lsrc, rsrc, dst, ...} => operandUse3(lsrc, rsrc, dst)
227 :     | I.FIBINOP{lsrc, rsrc, dst, ...} => operandUse3(lsrc, rsrc, dst)
228 :     | I.FUNOP{src, dst, ...} => operandUse2(src, dst)
229 :    
230 : monnier 247 | I.SAHF => ([], [C.eax])
231 : leunga 815 | I.LAHF => ([C.eax], [])
232 : george 545 (* This sets the low order byte,
233 :     * do potentially it may define *and* use
234 :     *)
235 :     | I.CMOV{src,dst,...} => ([dst], operandAcc(src, [dst]))
236 : leunga 657 | I.ANNOTATION{a=C.DEF_USE{cellkind=C.GP,defs,uses}, i, ...} =>
237 :     let val (d,u) = defUseR i in (defs@d, u@uses) end
238 : monnier 498 | I.ANNOTATION{a, i, ...} => defUseR i
239 : monnier 247 | _ => ([], [])
240 :     end (* defUseR *)
241 :    
242 :     fun defUseF instr = let
243 :     fun operand(I.FDirect f) = [f]
244 : leunga 731 | operand(I.FPR f) = [f]
245 : monnier 247 | operand _ = []
246 : leunga 731
247 :     fun operandAcc(I.FDirect f, acc) = f::acc
248 :     | operandAcc(I.FPR f, acc) = f::acc
249 :     | operandAcc(_ , acc) = acc
250 :    
251 :     fun fbinop(lsrc, rsrc, dst) =
252 :     let val def = operand dst
253 :     val use = operandAcc(lsrc, operand rsrc)
254 :     in (def, use)
255 :     end
256 :    
257 :     val fcmpTmp = [C.ST 0]
258 :    
259 : monnier 247 in
260 :     case instr
261 : george 555 of I.FSTPT opnd => (operand opnd, [])
262 :     | I.FSTPL opnd => (operand opnd, [])
263 : george 545 | I.FSTPS opnd => (operand opnd, [])
264 : leunga 579 | I.FSTL opnd => (operand opnd, [])
265 :     | I.FSTS opnd => (operand opnd, [])
266 : george 555 | I.FLDT opnd => ([], operand opnd)
267 : george 545 | I.FLDL opnd => ([], operand opnd)
268 :     | I.FLDS opnd => ([], operand opnd)
269 : leunga 731 | I.FUCOM opnd => ([], operand opnd)
270 :     | I.FUCOMP opnd => ([], operand opnd)
271 : leunga 796 | I.CALL{defs, uses, ...} => (C.getFreg defs, C.getFreg uses)
272 : monnier 247 | I.FBINARY{dst, src, ...}=> (operand dst, operand dst @ operand src)
273 :     | I.FCOPY{dst, src, tmp=SOME(I.FDirect f), ...} => (f::dst, src)
274 : leunga 731 | I.FCOPY{dst, src, tmp=SOME(I.FPR f), ...} => (f::dst, src)
275 : monnier 247 | I.FCOPY{dst, src, ...} => (dst, src)
276 : leunga 731
277 :     | I.FMOVE{src, dst, ...} => (operand dst, operand src)
278 :     | I.FILOAD{ea, dst, ...} => (operand dst, [])
279 :     | I.FCMP{lsrc, rsrc, ...} => (fcmpTmp, operandAcc(lsrc, operand rsrc))
280 :     | I.FBINOP{lsrc, rsrc, dst, ...} => fbinop(lsrc, rsrc, dst)
281 :     | I.FIBINOP{lsrc, rsrc, dst, ...} => fbinop(lsrc, rsrc, dst)
282 :     | I.FUNOP{src, dst, ...} => (operand dst, operand src)
283 :    
284 : leunga 657 | I.ANNOTATION{a=C.DEF_USE{cellkind=C.FP,defs,uses}, i, ...} =>
285 :     let val (d,u) = defUseF i in (defs@d, u@uses) end
286 : monnier 498 | I.ANNOTATION{a, i, ...} => defUseF i
287 : monnier 247 | _ => ([], [])
288 :     end
289 :    
290 :     fun defUse C.GP = defUseR
291 :     | defUse C.FP = defUseF
292 :     | defUse _ = error "defUse"
293 : monnier 411
294 :     (*========================================================================
295 :     * Annotations
296 :     *========================================================================*)
297 : monnier 469 fun getAnnotations(I.ANNOTATION{i,a}) =
298 :     let val (i,an) = getAnnotations i in (i,a::an) end
299 :     | getAnnotations i = (i,[])
300 : monnier 411 fun annotate(i,a) = I.ANNOTATION{i=i,a=a}
301 : leunga 657
302 :     (*========================================================================
303 :     * Replicate an instruction
304 :     *========================================================================*)
305 :     fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
306 :     | replicate(I.COPY{tmp=SOME _, dst, src}) =
307 :     I.COPY{tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src}
308 :     | replicate(I.FCOPY{tmp=SOME _, dst, src}) =
309 :     I.FCOPY{tmp=SOME(I.FDirect(C.newFreg())), dst=dst, src=src}
310 :     | replicate i = i
311 : monnier 247 end
312 :    

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