fixed broken version
[libfirm] / ir / be / ia32 / ia32_spec.pl
1 # Creation: 2005/10/19
2 # $Id$
3 # This is the specification for the ia32 assembler Firm-operations
4
5 # the cpu architecture (ia32, ia64, mips, sparc, ppc, ...)
6
7 $arch = "ia32";
8
9 # The node description is done as a perl hash initializer with the
10 # following structure:
11 #
12 # %nodes = (
13 #
14 # <op-name> => {
15 #   "op_flags" => "N|L|C|X|I|F|Y|H|c",
16 #   "arity"    => "0|1|2|3|variable|dynamic|all",
17 #   "state"    => "floats|pinned",
18 #   "args"     => [
19 #                   { "type" => "type 1", "name" => "name 1" },
20 #                   { "type" => "type 2", "name" => "name 2" },
21 #                   ...
22 #                 ],
23 #   "comment"  => "any comment for constructor",
24 #   "rd_constructor" => "c source code which constructs an ir_node"
25 # },
26 #
27 # ... # (all nodes you need to describe)
28 #
29 # ); # close the %nodes initializer
30
31 # the op_flags correspond to the firm irop_flags:
32 #   N   irop_flag_none
33 #   L   irop_flag_labeled
34 #   C   irop_flag_commutative
35 #   X   irop_flag_cfopcode
36 #   I   irop_flag_ip_cfopcode
37 #   F   irop_flag_fragile
38 #   Y   irop_flag_forking
39 #   H   irop_flag_highlevel
40 #   c   irop_flag_constlike
41 #
42 # op_flags: flags for the operation, OPTIONAL (default is "N")
43 #
44 # state: state of the operation, OPTIONAL (default is "pinned")
45 #
46 # arity: arity of the operation, MUST NOT BE OMITTED
47 #
48 # args:  the OPTIONAL arguments of the node constructor (debug, irg and block
49 #        are always the first 3 arguments and are always autmatically
50 #        created)
51 #        If this key is missing the following arguments will be created:
52 #        for i = 1 .. arity: ir_node *op_i
53 #        ir_mode *mode
54 #
55 # comment: OPTIONAL comment for the node constructor
56 #
57 # rd_constructor: for every operation there will be a
58 #      new_rd_<arch>_<op-name> function with the arguments from above
59 #      which creates the ir_node corresponding to the defined operation
60 #      you can either put the complete source code of this function here
61 #
62 #      This key is OPTIONAL. If omitted, the following constructor will
63 #      be created:
64 #      if (!op_<arch>_<op-name>) assert(0);
65 #      for i = 1 to arity
66 #         set in[i] = op_i
67 #      done
68 #      res = new_ir_node(db, irg, block, op_<arch>_<op-name>, mode, arity, in)
69 #      res = optimize_node(res)
70 #      IRN_VRFY_IRG(res, irg)
71 #      return res
72 #
73 # NOTE: rd_constructor and args are only optional if and only if arity is 0,1,2 or 3
74
75 #--------------------------------------------------#
76 #                        _                         #
77 #                       (_)                        #
78 #  _ __   _____      __  _ _ __    ___  _ __  ___  #
79 # | '_ \ / _ \ \ /\ / / | | '__|  / _ \| '_ \/ __| #
80 # | | | |  __/\ V  V /  | | |    | (_) | |_) \__ \ #
81 # |_| |_|\___| \_/\_/   |_|_|     \___/| .__/|___/ #
82 #                                      | |         #
83 #                                      |_|         #
84 #--------------------------------------------------#
85
86 %nodes = (
87
88 # arithmetic operations
89
90 # commutative operations
91
92 "Add" => {
93   "op_flags" => "C",
94   "arity"    => 2,
95   "comment"  => "construct Add: Add(a, b) = Add(b, a) = a + b",
96   "emit"     => '. addl %s2, %d1\t\t\t/* Add(%s1, %s2) -> %d1 */'
97 },
98
99 "Add_i" => {
100   "arity"    => 1,
101   "comment"  => "construct Add: Add(a, const) = Add(const, a) = a + const",
102   "emit"     => '. addl %c, %d1\t\t\t/* Add(%c, %s1) -> %d1 */'
103 },
104
105 "Mul" => {
106   "op_flags" => "C",
107   "arity"    => 2,
108   "comment"  => "construct Mul: Mul(a, b) = Mul(b, a) = a * b",
109   "emit"     =>
110 '  if (mode_is_signed(get_irn_mode(n))) {
111 4. imull %s2\t\t\t/* signed Mul(%s1, %s2) -> %d1 */
112   }
113   else {
114 4. mull %s2\t\t\t/* unsigned Mul(%s1, %s2) -> %d1 */
115   }
116 '
117 },
118
119 "Mul_i" => {
120   "state"    => "pinned",
121   "arity"    => 1,
122   "comment"  => "construct Mul: Mul(a, const) = Mul(const, a) = a * const",
123   "emit"     =>
124 '  if (mode_is_signed(get_irn_mode(n))) {
125 4. imull %c\t\t\t/* signed Mul(%c, %s1) -> %d1 */
126   }
127   else {
128 4. mull %c\t\t\t/* unsigned Mul(%c, %s1) -> %d1 */
129   }
130 '
131 },
132
133 "Mulh" => {
134   "op_flags" => "C",
135   "arity"    => 2,
136   "comment"  => "construct Mulh: Mulh(a, b) = Mulh(b, a) = get_32_highest_bits(a * b)",
137   "emit"     =>
138 '  if (mode_is_signed(get_irn_mode(n))) {
139 4. imull %s2\t\t\t/* signed Mulh(%s1, %s2) -> %d1 */
140   }
141   else {
142 4. mull %s2\t\t\t/* unsigned Mulh(%s1, %s2) -> %d1 */
143   }
144 '
145 },
146
147 "Mulh_i" => {
148   "state"    => "pinned",
149   "arity"    => 1,
150   "comment"  => "construct Mulh: Mulh(a, const) = Mulh(const, a) = get_32_highest_bits(a * const)",
151   "emit"     =>
152 '  if (mode_is_signed(get_irn_mode(n))) {
153 4. imull %c\t\t\t/* signed Mulh(%c, %s1) -> %d1 */
154   }
155   else {
156 4. mull %c\t\t\t/* unsigned Mulh(%c, %s1) -> %d1 */
157   }
158 '
159 },
160
161 "And" => {
162   "op_flags" => "C",
163   "arity"    => 2,
164   "comment"  => "construct And: And(a, b) = And(b, a) = a AND b",
165   "emit"     => '. andl %s2, %d1\t\t\t/* And(%s1, %s2) -> %d1 */'
166 },
167
168 "And_i" => {
169   "arity"    => 1,
170   "comment"  => "construct And: And(a, const) = And(const, a) = a AND const",
171   "emit"     => '. andl %c, %d1\t\t\t/* And(%c, %s1) -> %d1 */'
172 },
173
174 "Or" => {
175   "op_flags" => "C",
176   "arity"    => 2,
177   "comment"  => "construct Or: Or(a, b) = Or(b, a) = a OR b",
178   "emit"     => '. orl %s2, %d1\t\t\t/* Or(%s1, %s2) -> %d1 */'
179 },
180
181 "Or_i" => {
182   "arity"    => 1,
183   "comment"  => "construct Or: Or(a, const) = Or(const, a) = a OR const",
184   "emit"     => '. orl %c, %d1\t\t\t/* Or(%c, %s1) -> %d1 */'
185 },
186
187 "Eor" => {
188   "op_flags" => "C",
189   "arity"    => 2,
190   "comment"  => "construct Eor: Eor(a, b) = Eor(b, a) = a EOR b",
191   "emit"     => '. xorl %s2, %d1\t\t\t/* Xor(%s1, %s2) -> %d1 */'
192 },
193
194 "Eor_i" => {
195   "arity"    => 1,
196   "comment"  => "construct Eor: Eor(a, const) = Eor(const, a) = a EOR const",
197   "emit"     => '. xorl %c, %d1\t\t\t/* Xor(%c, %s1) -> %d1 */'
198 },
199
200 "Max" => {
201   "op_flags" => "C",
202   "arity"    => 2,
203   "comment"  => "construct Max: Max(a, b) = Max(b, a) = a > b ? a : b",
204   "emit"     =>
205 '2. cmpl %s2, %s1\t\t\t/* prepare Max */
206   if (mode_is_signed(get_irn_mode(n))) {
207 2.  cmovg %s1, %d1\t\t\t/* Max(%s1, %s2) -> %d1 */
208   }
209   else {
210 2.  cmova %s1, %d1\t\t\t/* Max(%s1, %s2) -> %d1 */
211   }
212 '
213 },
214
215 "Min" => {
216   "op_flags" => "C",
217   "arity"    => 2,
218   "comment"  => "construct Min: Min(a, b) = Min(b, a) = a < b ? a : b",
219   "emit"     =>
220 '2. cmpl %s2, %s1\t\t\t/* prepare Min (%s1 - %s2) */
221   if (mode_is_signed(get_irn_mode(n))) {
222 2.  cmovl %s1, %d1\t\t\t/* Min(%s1, %s2) -> %d1 */
223   }
224   else {
225 2.  cmovb %s1, %d1\t\t\t/* Min(%s1, %s2) -> %d1 */
226   }
227 '
228 },
229
230 # not commutative operations
231
232 "Sub" => {
233   "arity"    => 2,
234   "comment"  => "construct Sub: Sub(a, b) = a - b",
235   "emit"     => '. subl %s2, %s1\t\t\t/* Sub(%s1, %s2) -> %d1 */'
236 },
237
238 "Sub_i" => {
239   "arity"    => 1,
240   "comment"  => "construct Sub: Sub(a, const) = a - const",
241   "emit"     => '. subl %c, %s1\t\t\t/* Sub(%s1, %c) -> %d1 */'
242 },
243
244 "Mod" => {
245   "arity"    => 2,
246   "comment"  => "construct Mod: Mod(a, b) = a % b",
247   "emit"     =>
248 '  if (mode_is_signed(get_irn_mode(n))) {
249 4.  cltd\t\t\t/* sign extend EAX -> EDX:EAX */\n
250 4.  idivl %s2\t\t\t/* signed Mod(%s1, %s2) -> %d1 */
251   }
252   else {
253 4.  xorl edx, edx\t\t\t/* EDX = 0 */
254 4.  divl %s2\t\t\t/* unsigned Mod(%s1, %s2) -> %d1 */
255   }
256 '
257 },
258
259 "DivMod" => {
260   "arity"    => 2,
261   "comment"  => "construct DivMod: DivMod(a,b) = (a / b, a % b)",
262   "emit"     =>
263 '  if (mode_is_signed(get_irn_mode(n))) {
264 4.  cltd\t\t\t/* sign extend EAX -> EDX:EAX */\n
265 4.  idivl %s2\t\t\t/* signed DivMod(%s1, %s2) -> (%d1:%d2) (Div, Mod) */
266   }
267   else {
268 4.  xorl edx, edx\t\t\t/* EDX = 0 */
269 4.  divl %s2\t\t\t/* unsigned DivMod(%s1, %s2) -> (%d1:%d2) (Div, Mod) */
270   }
271 '
272 },
273
274 "Div" => {
275   "arity"    => 2,
276   "comment"  => "construct Div: Div(a, b) = a / b",
277   "emit"     =>
278 '  if (mode_is_signed(get_irn_mode(n))) {
279 4.  cltd\t\t\t/* sign extend EAX -> EDX:EAX */\n
280 4.  idivl %s2\t\t\t/* signed Div(%s1, %s2) -> %d1 */
281   }
282   else {
283 4.  xorl edx, edx\t\t\t/* EDX = 0 */
284 4.  divl %s2\t\t\t/* unsigned Div(%s1, %s2) -> %d1 */
285   }
286 '
287 },
288
289 "Shl" => {
290   "arity"    => 2,
291   "comment"  => "construct Shl: Shl(a, b) = a << b",
292   "emit"     => '. shll %s2, %d1\t\t\t/* Shl(%s1, %s2) -> %d1 */'
293 },
294
295 "Shl_i" => {
296   "arity"    => 1,
297   "comment"  => "construct Shl: Shl(a, const) = a << const",
298   "emit"     => '. shll %c, %d1\t\t\t/* Shl(%s1, %c) -> %d1 */'
299 },
300
301 "Shr" => {
302   "arity"    => 2,
303   "comment"  => "construct Shr: Shr(a, b) = a >> b",
304   "emit"     => '. shrl %s2, %d1\t\t\t/* Shr(%s1, %s2) -> %d1 */'
305 },
306
307 "Shr_i" => {
308   "arity"    => 1,
309   "comment"  => "construct Shr: Shr(a, const) = a >> const",
310   "emit"     => '. shrl %c, %d1\t\t\t/* Shr(%s1, %c) -> %d1 */'
311 },
312
313 "Shrs" => {
314   "arity"    => 2,
315   "comment"  => "construct Shrs: Shrs(a, b) = a >> b",
316   "emit"     => '. sarl %s2, %d1\t\t\t/* Shrs(%s1, %s2) -> %d1 */'
317 },
318
319 "Shrs_i" => {
320   "arity"    => 1,
321   "comment"  => "construct Shrs: Shrs(a, const) = a >> const",
322   "emit"     => '. sarl %c, %d1\t\t\t/* Shrs(%s1, %c) -> %d1 */'
323 },
324
325 "RotR" => {
326   "arity"    => 2,
327   "comment"  => "construct RotR: RotR(a, b) = a ROTR b",
328   "emit"     => '. rorl %s2, %d1\t\t\t/* RotR(%s1, %s2) -> %d1 */'
329 },
330
331 "RotR_i" => {
332   "arity"    => 1,
333   "comment"  => "construct RotR: RotR(a, const) = a ROTR const",
334   "emit"     => '. rorl %c, %d1\t\t\t/* RotR(%s1, %c) -> %d1 */'
335 },
336
337 "RotL" => {
338   "arity"    => 2,
339   "comment"  => "construct RotL: RotL(a, b) = a ROTL b",
340   "emit"     => '. roll %s2, %d1\t\t\t/* RotL(%s1, %s2) -> %d1 */'
341 },
342
343 "RotL_i" => {
344   "arity"    => 1,
345   "comment"  => "construct RotL: RotL(a, const) = a ROTL const",
346   "emit"     => '. roll %c, %d1\t\t\t/* RotL(%s1, %c) -> %d1 */'
347 },
348
349 "Minus" => {
350   "arity"    => 1,
351   "comment"  => "construct Minus: Minus(a) = -a",
352   "emit"     => '. negl %d1\t\t\t/* Neg(%s1) -> %d1 */'
353 },
354
355 "Inc" => {
356   "arity"    => 1,
357   "comment"  => "construct Increment: Inc(a) = a++",
358   "emit"     => '. incl %d1\t\t\t/* Inc(%s1) -> %d1 */'
359 },
360
361 "Dec" => {
362   "arity"    => 1,
363   "comment"  => "construct Decrement: Dec(a) = a--",
364   "emit"     => '. decl %d1\t\t\t/* Dec(%s1) -> %d1 */'
365 },
366
367 "Abs" => {
368   "arity"    => 1,
369   "comment"  => "construct Abs: Abs(a) = |a|",
370   "emit"     =>
371 '2. cdq\t\t\t/* Abs: EAX->EDX:EAX */
372 2.  xorl %%edx, %%eax\t\t\t/* Abs: one-completent */
373 2.  subl %%edx, %%eax\t\t\t/* Abs: two-complement */
374 '
375 },
376
377 "Not" => {
378   "arity"    => 1,
379   "comment"  => "construct Not: Not(a) = !a",
380   "emit"     => '. notl %d1\t\t\t/* Not(%s1) -> %d1 */'
381 },
382
383 # other operations
384
385 "Conv" => {
386   "arity"    => 1,
387   "comment"  => "construct Conv: Conv(a) = (conv)a"
388 },
389
390 "Cmp" => {
391   "op_flags" => "C",
392   "arity"    => 2,
393   "comment"  => "construct Cmp: Cmp(a, b) = a CMP b",
394   "emit"     => '. cmpl %s2, %s1\t\t\t/* Cmp(%s1, %s2) -> flags */'
395 },
396
397 "Cmp_i" => {
398   "arity"    => 1,
399   "comment"  => "construct Cmp: Cmp(a, const) = Cmp(const, a) = a CMP const",
400   "emit"     => '. cmpl %c, %s1\t\t\t/* Cmp(%s1, %c) -> flags */'
401 },
402
403 # Load / Store
404
405 "Load" => {
406   "arity"    => 2,
407   "comment"  => "construct Load: Load(mem-edge, ptr) = LD ptr",
408   "emit"     => '. movl (%s1), %d1\t\t\t/* Load((%s1)) -> %d1 */'
409 },
410
411 "Store" => {
412   "arity"    => 3,
413   "comment"  => "construct Store: Store(mem-edge, ptr, val) = ST ptr,val",
414   "emit"     => '. movl %s1, (%d1)\t\t\t/* Store(%s1) -> (%d1) */'
415 },
416
417 "Lea" => {
418   "arity"    => 2,
419   "comment"  => "construct Lea: Lea(a,b) = lea offs(a,b,const) | res = a + b * const + offs with const = 0,1,2,4,8",
420   "emit"     => '. leal %o(%s1, %s2, %c), %d1\t\t\t/* %d1 = %s1 + %s2 << %c + %o */'
421 },
422
423 "Lea_i" => {
424   "arity"    => 1,
425   "comment"  => "construct Lea: Lea(a) = lea offs(a) | res = a + offs",
426   "emit"     => '. leal %c(%s1), %d1\t\t\t/* %d1 = %s1 + %c */'
427 },
428
429 # Call
430
431 "Call" => {
432   "arity"    => 1,
433   "comment"  => "construct Call: Call(...)",
434   "args"     => [ { "type" => "ir_node *", "name" => "old_call" } ],
435   "rd_constructor" =>
436 "  ir_node *res;
437   ir_node *in[1];
438   asmop_attr *attr;
439
440   if (!op_ia32_Call) assert(0);
441
442   in[0] = get_Call_mem(old_call);
443
444   res = new_ir_node(db, irg, block, op_ia32_Call, mode_T, 1, in);
445   res = optimize_node(res);
446   irn_vrfy_irg(res, irg);
447
448   attr = (asmop_attr *)get_irn_generic_attr(res);
449   attr->data.old_ir = old_call;
450
451   return res;
452 "
453 }
454
455 ); # end of %nodes