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 583 - (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 : leunga 583 (*
180 :     * Instruction encoding on the x86
181 :     * Because of variable width instructions.
182 :     * We decompose each byte field into a seperate format first, then combine
183 :     * then to form the real instructions
184 :     *)
185 :     instruction formats 8 bits
186 :     modrm{mod:2, reg:3, rm:3}
187 :     | sib{ss:2, index:3, base:3}
188 :     | immed8{imm:8}
189 :    
190 :     instruction formats 32 bits
191 :     immed32{imm:32}
192 :    
193 : monnier 409 (* A bunch of routines for emitting assembly *)
194 :     functor Assembly
195 :     (structure MemRegs : MEMORY_REGISTERS where I = Instr) =
196 :     struct
197 :     val memReg = MemRegs.memReg regmap
198 : monnier 429 fun emitInt32 i =
199 :     let val s = Int32.toString i
200 :     val s = if i >= 0 then s else "-"^String.substring(s,1,size s-1)
201 :     in emit s end
202 :    
203 : monnier 409 fun emitScale 0 = emit "1"
204 :     | emitScale 1 = emit "2"
205 :     | emitScale 2 = emit "4"
206 :     | emitScale 3 = emit "8"
207 :     | emitScale _ = error "emitScale"
208 :    
209 :     and eImmed(I.Immed (i)) = emitInt32 i
210 :     | eImmed(I.ImmedLabel lexp) = emit_labexp lexp
211 :     | eImmed _ = error "eImmed"
212 :    
213 :     and emit_operand opn =
214 :     case opn of
215 :     I.Immed i => (emit "$"; emitInt32 i)
216 :     | I.ImmedLabel lexp => (emit "$"; emit_labexp lexp)
217 :     | I.LabelEA le => emit_labexp le
218 :     | I.Relative _ => error "emit_operand"
219 :     | I.Direct r => emit_GP r
220 : monnier 498 | I.MemReg r => emit_operand(memReg opn)
221 : george 555 | I.ST f => emit_FP f
222 :     | I.FDirect f => emit_operand(memReg opn)
223 : monnier 409 | I.Displace{base,disp,mem,...} =>
224 : george 545 (emit_disp disp; emit "("; emit_GP base; emit ")";
225 : monnier 409 emit_region mem)
226 : leunga 576 | I.Indexed{base,index,scale,disp,mem,...} =>
227 :     (emit_disp disp; emit "(";
228 :     case base of
229 :     NONE => ()
230 :     | SOME base => emit_GP base;
231 :     comma();
232 :     emit_GP index; comma();
233 : george 545 emitScale scale; emit ")"; emit_region mem)
234 : monnier 409
235 : george 545 and emit_disp(I.Immed 0) = ()
236 :     | emit_disp(I.Immed i) = emitInt32 i
237 :     | emit_disp(I.ImmedLabel lexp) = emit_labexp lexp
238 :     | emit_disp _ = error "emit_disp"
239 :    
240 : monnier 409 (* The gas assembler does not like the "$" prefix for immediate
241 :     * labels in certain instructions.
242 :     *)
243 :     fun stupidGas(I.ImmedLabel lexp) = emit_labexp lexp
244 :     | stupidGas(I.LabelEA _) = error "stupidGas"
245 :     | stupidGas opnd = emit_operand opnd
246 :    
247 : george 555 (* Display the floating point binary opcode *)
248 :     fun isMemOpnd(I.MemReg _) = true
249 :     | isMemOpnd(I.FDirect f) = true
250 :     | isMemOpnd(I.LabelEA _) = true
251 :     | isMemOpnd(I.Displace _) = true
252 :     | isMemOpnd(I.Indexed _) = true
253 :     | isMemOpnd _ = false
254 : leunga 565 fun chop fbinOp =
255 : george 555 let val n = size fbinOp
256 : leunga 565 in case Char.toLower(String.sub(fbinOp,n-1)) of
257 :     (#"s" | #"l") => String.substring(fbinOp,0,n-1)
258 : george 555 | _ => fbinOp
259 : leunga 565 end
260 : george 555
261 : monnier 409 val emit_dst = emit_operand
262 :     val emit_src = emit_operand
263 :     val emit_opnd = emit_operand
264 :     val emit_rsrc = emit_operand
265 :     val emit_lsrc = emit_operand
266 :     val emit_addr = emit_operand
267 : george 545 val emit_src1 = emit_operand
268 :     end (* Instruction *)
269 : monnier 409
270 :     (* many of these instructions imply certain register usages *)
271 :     instruction
272 :     NOP
273 :     ``nop''
274 :    
275 :     | JMP of operand * Label.label list
276 :     ``jmp\t<stupidGas operand>''
277 :    
278 :     | JCC of {cond:cond, opnd:operand}
279 : george 545 ``j<cond>\t<stupidGas opnd>''
280 : monnier 409
281 :     | CALL of operand * C.cellset * C.cellset * Region.region
282 : monnier 498 ``call\t<stupidGas operand><region><
283 :     emit_defs(cellset1)><
284 :     emit_uses(cellset2)>''
285 : monnier 409
286 : george 545 | LEAVE
287 :     ``leave''
288 :    
289 : monnier 429 | RET of operand option
290 :     ``ret<case operand of NONE => ()
291 :     | SOME e => (emit "\t"; emit_operand e)>''
292 : monnier 409
293 :     (* integer *)
294 :     | MOVE of {mvOp:move, src:operand, dst:operand}
295 :     ``<mvOp>\t<src>, <dst>''
296 :    
297 :     | LEA of {r32: $GP, addr: operand}
298 :     ``leal\t<addr>, <r32>''
299 :    
300 : george 545 | CMPL of {lsrc: operand, rsrc: operand}
301 : monnier 409 ``cmpl\t<rsrc>, <lsrc>''
302 :    
303 : george 545 | CMPW of {lsrc: operand, rsrc: operand}
304 :     ``cmpb\t<rsrc>, <lsrc>''
305 :    
306 :     | CMPB of {lsrc: operand, rsrc: operand}
307 :     ``cmpb\t<rsrc>, <lsrc>''
308 :    
309 :     | TESTL of {lsrc: operand, rsrc: operand}
310 :     ``testl\t<rsrc>, <lsrc>''
311 :    
312 :     | TESTW of {lsrc: operand, rsrc: operand}
313 :     ``testw\t<rsrc>, <lsrc>''
314 :    
315 :     | TESTB of {lsrc: operand, rsrc: operand}
316 :     ``testb\t<rsrc>, <lsrc>''
317 :    
318 : monnier 409 | BINARY of {binOp:binaryOp, src:operand, dst:operand}
319 : george 545 asm: (case (src,binOp) of
320 :     (I.Direct _,
321 :     (I.SARL | I.SHRL | I.SHLL |
322 :     I.SARW | I.SHRW | I.SHLW |
323 :     I.SARB | I.SHRB | I.SHLB)) => ``<binOp>\t%cl, <dst>''
324 :     | _ => ``<binOp>\t<src>, <dst>''
325 :     )
326 : monnier 409
327 :     | MULTDIV of {multDivOp:multDivOp, src:operand}
328 :     ``<multDivOp>l\t<src>''
329 :    
330 : george 545 | MUL3 of {dst: $GP, src2: Int32.int option, src1:operand}
331 :     (* Fermin: constant operand must go first *)
332 :     asm: (case src2 of
333 :     NONE => ``imul\t<src1>, <dst>''
334 :     | SOME i => ``imul\t$<emitInt32 i>, <src1>, <dst>''
335 :     )
336 : monnier 409
337 :     | UNARY of {unOp:unaryOp, opnd:operand}
338 : george 545 ``<unOp>\t<opnd>''
339 : monnier 409
340 : george 545 (* set byte on condition code; note that
341 :     * this only sets the low order byte, so it also
342 :     * uses its operand.
343 :     *)
344 :     | SET of {cond:cond, opnd:operand}
345 :     ``set<cond>\t<opnd>''
346 :    
347 :     (* conditional move; Pentium Pro or higher only
348 :     * Destination must be a register.
349 :     *)
350 :     | CMOV of {cond:cond, src:operand, dst: $GP}
351 :     ``cmov<cond>\t<src>, <dst>''
352 :    
353 :     | PUSHL of operand
354 : monnier 409 ``pushl\t<operand>''
355 :    
356 : george 545 | PUSHW of operand
357 :     ``pushw\t<operand>''
358 :    
359 :     | PUSHB of operand
360 :     ``pushb\t<operand>''
361 :    
362 : monnier 409 | POP of operand
363 :     ``popl\t<operand>''
364 :    
365 :     | CDQ
366 :     ``cdq''
367 :    
368 :     | INTO
369 :     ``into''
370 :    
371 :     (* parallel copies *)
372 :     | COPY of {dst: $GP list, src: $GP list, tmp:operand option}
373 : george 545 asm: emitInstrs (Shuffle.shuffle{regmap,tmp,dst,src})
374 :    
375 : monnier 409 | FCOPY of {dst: $FP list, src: $FP list, tmp:operand option}
376 : george 545 asm: emitInstrs (Shuffle.shufflefp{regmap,tmp,dst,src})
377 : monnier 409
378 :     (* floating *)
379 :     | FBINARY of {binOp:fbinOp, src:operand, dst:operand}
380 : leunga 565 asm: (if isMemOpnd src then ``<binOp>\t<src>''
381 :     else ``<emit(chop(asm_fbinOp binOp))>\t<src>, <dst>''
382 :     )
383 : monnier 409
384 : leunga 565 | FIBINARY of {binOp:fibinOp, src:operand}
385 :     asm: ``<binOp>\t<src>'' (* the implied destination is %ST(0) *)
386 :    
387 : monnier 409 | FUNARY of funOp
388 :     ``<funOp>''
389 :    
390 :     | FUCOMPP
391 :     ``fucompp''
392 :    
393 : george 545 | FCOMPP
394 :     ``fcompp''
395 : monnier 409
396 : george 545 | FXCH of {opnd: $FP}
397 :     asm: (``fxch\t''; if opnd = C.ST(1) then () else ``<opnd>'')
398 : monnier 409
399 : george 545 | FSTPL of operand
400 :     ``fstpl\t<operand>''
401 :    
402 :     | FSTPS of operand
403 :     ``fstps\t<operand>''
404 :    
405 : george 555 | FSTPT of operand
406 :     ``fstps\t<operand>''
407 :    
408 : leunga 579 | FSTL of operand
409 :     ``fstl\t<operand>''
410 :    
411 :     | FSTS of operand
412 :     ``fsts\t<operand>''
413 :    
414 : leunga 565 | FLD1
415 :     ``fld1''
416 :    
417 :     | FLDL2E
418 :     ``fldl2e''
419 :    
420 :     | FLDL2T
421 :     ``fldl2t''
422 :    
423 :     | FLDLG2
424 :     ``fldlg2''
425 :    
426 :     | FLDLN2
427 :     ``fldln2''
428 :    
429 :     | FLDPI
430 :     ``fldpi''
431 :    
432 :     | FLDZ
433 :     ``fldz''
434 :    
435 : george 545 | FLDL of operand
436 :     ``fldl\t<operand>''
437 :    
438 :     | FLDS of operand
439 :     ``flds\t<operand>''
440 :    
441 : george 555 | FLDT of operand
442 :     ``fldt\t<operand>''
443 :    
444 : monnier 409 | FILD of operand
445 :     ``fild\t<operand>''
446 :    
447 : leunga 565 | FILDL of operand
448 :     ``fildl\t<operand>''
449 :    
450 :     | FILDLL of operand
451 :     ``fildll\t<operand>''
452 :    
453 : monnier 409 | FNSTSW
454 :     ``fnstsw''
455 :    
456 : george 545 | FENV of {fenvOp:fenvOp, opnd:operand} (* load/store environment *)
457 :     ``<fenvOp>\t<opnd>''
458 :    
459 : monnier 409 (* misc *)
460 :     | SAHF
461 :     ``sahf''
462 :    
463 :     (* annotations *)
464 :     | ANNOTATION of {i:instruction, a:Annotations.annotation}
465 : george 545 asm: (emitInstr i; comment(Annotations.toString a))
466 : monnier 409 end
467 :    

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