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/x86.md
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/x86/x86.md

Parent Directory Parent Directory | Revision Log Revision Log


Revision 576 - (view) (download)

1 : monnier 409 (*
2 :     * 32bit, x86 instruction set.
3 :     *)
4 :     architecture X86 =
5 :     struct
6 :    
7 :     name "X86"
8 :    
9 :     superscalar
10 :    
11 :     little endian (* is this right??? *)
12 :    
13 :     lowercase assembly
14 :    
15 :     (*
16 :     * Assembly note:
17 :     * Note: we are using the AT&T syntax (for Linux) and not the intel syntax
18 :     * memory operands have the form:
19 :     * section:disp(base, index, scale)
20 :     * Most of the complication is actually in emiting the correct
21 :     * operand syntax.
22 :     *)
23 :    
24 :     (* Note: While the x86 has only 8 integer and 8 floating point registers,
25 :     * the SMLNJ compiler fakes it by assuming that it has 32 integer
26 :     * and 32 floating point registers. That's why we
27 :     *)
28 :    
29 :     storage
30 : george 545 GP "r" = 32 cells of 32 bits in cellset called "register"
31 :     assembly as
32 :     (fn (0,8) => "%al"
33 :     | (0,16) => "%ax"
34 :     | (0,32) => "%eax"
35 :     | (1,8) => "%cl"
36 :     | (1,16) => "%cx"
37 :     | (1,32) => "%ecx"
38 :     | (2,8) => "%dl"
39 :     | (2,16) => "%dx"
40 :     | (2,32) => "%edx"
41 :     | (3,8) => "%bl"
42 :     | (3,16) => "%bx"
43 :     | (3,32) => "%ebx"
44 :     | (4,16) => "%sp"
45 :     | (4,32) => "%esp"
46 :     | (5,16) => "%bp"
47 :     | (5,32) => "%ebp"
48 :     | (6,16) => "%si"
49 :     | (6,32) => "%esi"
50 :     | (7,16) => "%di"
51 :     | (7,32) => "%edi"
52 :     | (r,_) => "%"^Int.toString r
53 : monnier 409 )
54 : george 545 | FP "f" = 32 cells of 80 bits in cellset called "floating point register"
55 : leunga 565 assembly as (fn (0,_) => "%st"
56 :     | (f,_) =>
57 :     if f < 8 then "%st("^Int.toString f^")"
58 :     else "%f"^Int.toString f
59 :     (* pseudo register *)
60 : george 545 )
61 :     | CC "cc" = cells of 32 bits in cellset called "condition code register"
62 :     assembly as "cc"
63 :     | MEM "m" = cells of 8 bits called "memory"
64 :     assembly as (fn (r,_) => "m"^Int.toString r)
65 :     | CTRL "ctrl" = cells of 8 bits called "control"
66 :     assembly as (fn (r,_) => "ctrl"^Int.toString r)
67 :    
68 : monnier 409 locations
69 :     eax = $GP[0]
70 :     and ecx = $GP[1]
71 :     and edx = $GP[2]
72 :     and ebx = $GP[3]
73 :     and esp = $GP[4]
74 :     and ebp = $GP[5]
75 :     and esi = $GP[6]
76 :     and edi = $GP[7]
77 : monnier 429 and stackptrR = $GP[4]
78 : george 545 and ST(x) = $FP[x]
79 : monnier 409 and asmTmpR = ~1 (* not used *)
80 :     and fasmTmp = ~1 (* not used *)
81 :    
82 : george 545 structure RTL =
83 : monnier 409 struct
84 : george 545 end
85 : monnier 409
86 :     structure Instruction = struct
87 :     (* An effective address can be any combination of
88 :     * base + index*scale + disp
89 :     * or
90 :     * B + I*SCALE + DISP
91 :     *
92 :     * where any component is optional. The operand datatype captures
93 :     * all these combinations.
94 :     *
95 :     * DISP == Immed | ImmedLabel | Const
96 :     * B == Displace{base=B, disp=0}
97 :     * B+DISP == Displace{base=B, disp=DISP}
98 :     * I*SCALE+DISP == Indexed{base=NONE, index=I, scale=SCALE, disp=D}
99 :     * B+I*SCALE+DISP == Indexed{base=SOME B, index=I, scale=SCALE, disp=DISP}
100 :     *
101 :     * Note1: The index register cannot be EBP.
102 :     * The disp field must be one of Immed, ImmedLabel, or Const.
103 :     *)
104 :    
105 :     (* Note: Relative is only generated after sdi resolution *)
106 :     datatype operand =
107 :     Immed of Int32.int
108 :     | ImmedLabel of LabelExp.labexp
109 :     | Relative of int
110 :     | LabelEA of LabelExp.labexp
111 :     | Direct of $GP
112 :     | FDirect of $FP
113 : george 555 | ST of $FP
114 : monnier 498 | MemReg of int (* pseudo memory register *)
115 : monnier 409 | Displace of {base: $GP, disp:operand, mem:Region.region}
116 :     | Indexed of {base: $GP option, index: $GP, scale:int, disp:operand,
117 :     mem:Region.region}
118 : george 545
119 :     type addressing_mode = operand
120 : monnier 409
121 :     type ea = operand
122 :    
123 : george 545 datatype cond! =
124 :     EQ "e" | NE | LT "l" | LE | GT "g" | GE
125 :     | B | BE (* below *) | A | AE (* above *)
126 :     | C | NC (* if carry *)| P | NP (* if parity *)
127 :     | O | NO (* overflow *)
128 : monnier 409
129 : george 545 datatype binaryOp! =
130 :     ADDL | SUBL | ANDL | ORL | XORL | SHLL | SARL | SHRL | ADCL | SBBL
131 :     | ADDW | SUBW | ANDW | ORW | XORW | SHLW | SARW | SHRW
132 :     | ADDB | SUBB | ANDB | ORB | XORB | SHLB | SARB | SHRB
133 :    
134 : monnier 409 datatype multDivOp! = UMUL | IDIV | UDIV
135 :    
136 : george 545 datatype unaryOp! = DECL | INCL | NEGL | NOTL | NOTW | NOTB
137 : monnier 409
138 : george 545 datatype move! = MOVL
139 :     | MOVB
140 :     | MOVW
141 :     | MOVSWL | MOVZWL (* word -> long *)
142 :     | MOVSBL | MOVZBL (* byte -> long *)
143 : monnier 409
144 :     (* The Intel manual is incorrect on the description of FDIV and FDIVR *)
145 :     datatype fbinOp! =
146 : leunga 565 FADDP | FADDS
147 :     | FMULP | FMULS
148 :     | FCOMS
149 :     | FCOMPS
150 :     | FSUBP | FSUBS (* ST(1) := ST-ST(1); [pop] *)
151 :     | FSUBRP | FSUBRS (* ST(1) := ST(1)-ST; [pop] *)
152 :     | FDIVP | FDIVS (* ST(1) := ST/ST(1); [pop] *)
153 :     | FDIVRP | FDIVRS (* ST(1) := ST(1)/ST; [pop] *)
154 :     | FADDL
155 :     | FMULL
156 :     | FCOML
157 :     | FCOMPL
158 :     | FSUBL (* ST(1) := ST-ST(1); [pop] *)
159 :     | FSUBRL (* ST(1) := ST(1)-ST; [pop] *)
160 :     | FDIVL (* ST(1) := ST/ST(1); [pop] *)
161 :     | FDIVRL (* ST(1) := ST(1)/ST; [pop] *)
162 : monnier 409
163 : leunga 565 datatype fibinOp! =
164 :     FIADDS | FIMULS | FICOMS | FICOMPS
165 :     | FISUBS | FISUBRS | FIDIVS | FIDIVRS
166 :     | FIADDL | FIMULL | FICOML | FICOMPL
167 :     | FISUBL | FISUBRL | FIDIVL | FIDIVRL
168 :    
169 : monnier 409 datatype funOp! = FABS | FCHS
170 : george 545 | FSIN | FCOS | FTAN
171 :     | FSCALE | FRNDINT | FSQRT
172 :     | FTST | FXAM
173 :     | FINCSTP | FDECSTP
174 : monnier 409
175 : george 545 datatype fenvOp! = FLDENV | FNLDENV | FSTENV | FNSTENV
176 :    
177 : monnier 409 end (* struct Instruction *)
178 :    
179 :     (* A bunch of routines for emitting assembly *)
180 :     functor Assembly
181 :     (structure MemRegs : MEMORY_REGISTERS where I = Instr) =
182 :     struct
183 :     val memReg = MemRegs.memReg regmap
184 : monnier 429 fun emitInt32 i =
185 :     let val s = Int32.toString i
186 :     val s = if i >= 0 then s else "-"^String.substring(s,1,size s-1)
187 :     in emit s end
188 :    
189 : monnier 409 fun emitScale 0 = emit "1"
190 :     | emitScale 1 = emit "2"
191 :     | emitScale 2 = emit "4"
192 :     | emitScale 3 = emit "8"
193 :     | emitScale _ = error "emitScale"
194 :    
195 :     and eImmed(I.Immed (i)) = emitInt32 i
196 :     | eImmed(I.ImmedLabel lexp) = emit_labexp lexp
197 :     | eImmed _ = error "eImmed"
198 :    
199 :     and emit_operand opn =
200 :     case opn of
201 :     I.Immed i => (emit "$"; emitInt32 i)
202 :     | I.ImmedLabel lexp => (emit "$"; emit_labexp lexp)
203 :     | I.LabelEA le => emit_labexp le
204 :     | I.Relative _ => error "emit_operand"
205 :     | I.Direct r => emit_GP r
206 : monnier 498 | I.MemReg r => emit_operand(memReg opn)
207 : george 555 | I.ST f => emit_FP f
208 :     | I.FDirect f => emit_operand(memReg opn)
209 : monnier 409 | I.Displace{base,disp,mem,...} =>
210 : george 545 (emit_disp disp; emit "("; emit_GP base; emit ")";
211 : monnier 409 emit_region mem)
212 : leunga 576 | I.Indexed{base,index,scale,disp,mem,...} =>
213 :     (emit_disp disp; emit "(";
214 :     case base of
215 :     NONE => ()
216 :     | SOME base => emit_GP base;
217 :     comma();
218 :     emit_GP index; comma();
219 : george 545 emitScale scale; emit ")"; emit_region mem)
220 : monnier 409
221 : george 545 and emit_disp(I.Immed 0) = ()
222 :     | emit_disp(I.Immed i) = emitInt32 i
223 :     | emit_disp(I.ImmedLabel lexp) = emit_labexp lexp
224 :     | emit_disp _ = error "emit_disp"
225 :    
226 : monnier 409 (* The gas assembler does not like the "$" prefix for immediate
227 :     * labels in certain instructions.
228 :     *)
229 :     fun stupidGas(I.ImmedLabel lexp) = emit_labexp lexp
230 :     | stupidGas(I.LabelEA _) = error "stupidGas"
231 :     | stupidGas opnd = emit_operand opnd
232 :    
233 : george 555 (* Display the floating point binary opcode *)
234 :     fun isMemOpnd(I.MemReg _) = true
235 :     | isMemOpnd(I.FDirect f) = true
236 :     | isMemOpnd(I.LabelEA _) = true
237 :     | isMemOpnd(I.Displace _) = true
238 :     | isMemOpnd(I.Indexed _) = true
239 :     | isMemOpnd _ = false
240 : leunga 565 fun chop fbinOp =
241 : george 555 let val n = size fbinOp
242 : leunga 565 in case Char.toLower(String.sub(fbinOp,n-1)) of
243 :     (#"s" | #"l") => String.substring(fbinOp,0,n-1)
244 : george 555 | _ => fbinOp
245 : leunga 565 end
246 : george 555
247 : monnier 409 val emit_dst = emit_operand
248 :     val emit_src = emit_operand
249 :     val emit_opnd = emit_operand
250 :     val emit_rsrc = emit_operand
251 :     val emit_lsrc = emit_operand
252 :     val emit_addr = emit_operand
253 : george 545 val emit_src1 = emit_operand
254 :     end (* Instruction *)
255 : monnier 409
256 :     (* many of these instructions imply certain register usages *)
257 :     instruction
258 :     NOP
259 :     ``nop''
260 :    
261 :     | JMP of operand * Label.label list
262 :     ``jmp\t<stupidGas operand>''
263 :    
264 :     | JCC of {cond:cond, opnd:operand}
265 : george 545 ``j<cond>\t<stupidGas opnd>''
266 : monnier 409
267 :     | CALL of operand * C.cellset * C.cellset * Region.region
268 : monnier 498 ``call\t<stupidGas operand><region><
269 :     emit_defs(cellset1)><
270 :     emit_uses(cellset2)>''
271 : monnier 409
272 : george 545 | LEAVE
273 :     ``leave''
274 :    
275 : monnier 429 | RET of operand option
276 :     ``ret<case operand of NONE => ()
277 :     | SOME e => (emit "\t"; emit_operand e)>''
278 : monnier 409
279 :     (* integer *)
280 :     | MOVE of {mvOp:move, src:operand, dst:operand}
281 :     ``<mvOp>\t<src>, <dst>''
282 :    
283 :     | LEA of {r32: $GP, addr: operand}
284 :     ``leal\t<addr>, <r32>''
285 :    
286 : george 545 | CMPL of {lsrc: operand, rsrc: operand}
287 : monnier 409 ``cmpl\t<rsrc>, <lsrc>''
288 :    
289 : george 545 | CMPW of {lsrc: operand, rsrc: operand}
290 :     ``cmpb\t<rsrc>, <lsrc>''
291 :    
292 :     | CMPB of {lsrc: operand, rsrc: operand}
293 :     ``cmpb\t<rsrc>, <lsrc>''
294 :    
295 :     | TESTL of {lsrc: operand, rsrc: operand}
296 :     ``testl\t<rsrc>, <lsrc>''
297 :    
298 :     | TESTW of {lsrc: operand, rsrc: operand}
299 :     ``testw\t<rsrc>, <lsrc>''
300 :    
301 :     | TESTB of {lsrc: operand, rsrc: operand}
302 :     ``testb\t<rsrc>, <lsrc>''
303 :    
304 : monnier 409 | BINARY of {binOp:binaryOp, src:operand, dst:operand}
305 : george 545 asm: (case (src,binOp) of
306 :     (I.Direct _,
307 :     (I.SARL | I.SHRL | I.SHLL |
308 :     I.SARW | I.SHRW | I.SHLW |
309 :     I.SARB | I.SHRB | I.SHLB)) => ``<binOp>\t%cl, <dst>''
310 :     | _ => ``<binOp>\t<src>, <dst>''
311 :     )
312 : monnier 409
313 :     | MULTDIV of {multDivOp:multDivOp, src:operand}
314 :     ``<multDivOp>l\t<src>''
315 :    
316 : george 545 | MUL3 of {dst: $GP, src2: Int32.int option, src1:operand}
317 :     (* Fermin: constant operand must go first *)
318 :     asm: (case src2 of
319 :     NONE => ``imul\t<src1>, <dst>''
320 :     | SOME i => ``imul\t$<emitInt32 i>, <src1>, <dst>''
321 :     )
322 : monnier 409
323 :     | UNARY of {unOp:unaryOp, opnd:operand}
324 : george 545 ``<unOp>\t<opnd>''
325 : monnier 409
326 : george 545 (* set byte on condition code; note that
327 :     * this only sets the low order byte, so it also
328 :     * uses its operand.
329 :     *)
330 :     | SET of {cond:cond, opnd:operand}
331 :     ``set<cond>\t<opnd>''
332 :    
333 :     (* conditional move; Pentium Pro or higher only
334 :     * Destination must be a register.
335 :     *)
336 :     | CMOV of {cond:cond, src:operand, dst: $GP}
337 :     ``cmov<cond>\t<src>, <dst>''
338 :    
339 :     | PUSHL of operand
340 : monnier 409 ``pushl\t<operand>''
341 :    
342 : george 545 | PUSHW of operand
343 :     ``pushw\t<operand>''
344 :    
345 :     | PUSHB of operand
346 :     ``pushb\t<operand>''
347 :    
348 : monnier 409 | POP of operand
349 :     ``popl\t<operand>''
350 :    
351 :     | CDQ
352 :     ``cdq''
353 :    
354 :     | INTO
355 :     ``into''
356 :    
357 :     (* parallel copies *)
358 :     | COPY of {dst: $GP list, src: $GP list, tmp:operand option}
359 : george 545 asm: emitInstrs (Shuffle.shuffle{regmap,tmp,dst,src})
360 :    
361 : monnier 409 | FCOPY of {dst: $FP list, src: $FP list, tmp:operand option}
362 : george 545 asm: emitInstrs (Shuffle.shufflefp{regmap,tmp,dst,src})
363 : monnier 409
364 :     (* floating *)
365 :     | FBINARY of {binOp:fbinOp, src:operand, dst:operand}
366 : leunga 565 asm: (if isMemOpnd src then ``<binOp>\t<src>''
367 :     else ``<emit(chop(asm_fbinOp binOp))>\t<src>, <dst>''
368 :     )
369 : monnier 409
370 : leunga 565 | FIBINARY of {binOp:fibinOp, src:operand}
371 :     asm: ``<binOp>\t<src>'' (* the implied destination is %ST(0) *)
372 :    
373 : monnier 409 | FUNARY of funOp
374 :     ``<funOp>''
375 :    
376 :     | FUCOMPP
377 :     ``fucompp''
378 :    
379 : george 545 | FCOMPP
380 :     ``fcompp''
381 : monnier 409
382 : george 545 | FXCH of {opnd: $FP}
383 :     asm: (``fxch\t''; if opnd = C.ST(1) then () else ``<opnd>'')
384 : monnier 409
385 : george 545 | FSTPL of operand
386 :     ``fstpl\t<operand>''
387 :    
388 :     | FSTPS of operand
389 :     ``fstps\t<operand>''
390 :    
391 : george 555 | FSTPT of operand
392 :     ``fstps\t<operand>''
393 :    
394 : leunga 565 | FLD1
395 :     ``fld1''
396 :    
397 :     | FLDL2E
398 :     ``fldl2e''
399 :    
400 :     | FLDL2T
401 :     ``fldl2t''
402 :    
403 :     | FLDLG2
404 :     ``fldlg2''
405 :    
406 :     | FLDLN2
407 :     ``fldln2''
408 :    
409 :     | FLDPI
410 :     ``fldpi''
411 :    
412 :     | FLDZ
413 :     ``fldz''
414 :    
415 : george 545 | FLDL of operand
416 :     ``fldl\t<operand>''
417 :    
418 :     | FLDS of operand
419 :     ``flds\t<operand>''
420 :    
421 : george 555 | FLDT of operand
422 :     ``fldt\t<operand>''
423 :    
424 : monnier 409 | FILD of operand
425 :     ``fild\t<operand>''
426 :    
427 : leunga 565 | FILDL of operand
428 :     ``fildl\t<operand>''
429 :    
430 :     | FILDLL of operand
431 :     ``fildll\t<operand>''
432 :    
433 : monnier 409 | FNSTSW
434 :     ``fnstsw''
435 :    
436 : george 545 | FENV of {fenvOp:fenvOp, opnd:operand} (* load/store environment *)
437 :     ``<fenvOp>\t<opnd>''
438 :    
439 : monnier 409 (* misc *)
440 :     | SAHF
441 :     ``sahf''
442 :    
443 :     (* annotations *)
444 :     | ANNOTATION of {i:instruction, a:Annotations.annotation}
445 : george 545 asm: (emitInstr i; comment(Annotations.toString a))
446 : monnier 409 end
447 :    

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