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

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