86abe1a0fb0f0382a935f49eb88a69804d83c56b
[libfirm] / ir / be / TEMPLATE / TEMPLATE_spec.pl
1 # Creation: 2006/02/13
2 # $Id$
3 # This is a template specification for the Firm-Backend
4
5 $new_emit_syntax = 1;
6
7 # the cpu architecture (ia32, ia64, mips, sparc, ppc, ...)
8
9 $arch = "TEMPLATE";
10
11 # The node description is done as a perl hash initializer with the
12 # following structure:
13 #
14 # %nodes = (
15 #
16 # <op-name> => {
17 #   op_flags  => "N|L|C|X|I|F|Y|H|c|K",                 # optional
18 #   irn_flags => "R|N|I"                                # optional
19 #   arity     => "0|1|2|3 ... |variable|dynamic|any",   # optional
20 #   state     => "floats|pinned|mem_pinned|exc_pinned", # optional
21 #   args      => [
22 #                    { type => "type 1", name => "name 1" },
23 #                    { type => "type 2", name => "name 2" },
24 #                    ...
25 #                  ],
26 #   comment   => "any comment for constructor",  # optional
27 #   reg_req   => { in => [ "reg_class|register" ], out => [ "reg_class|register|in_rX" ] },
28 #   cmp_attr  => "c source code for comparing node attributes", # optional
29 #   outs      => { "out1", "out2" } # optional, creates pn_op_out1, ... consts
30 #   ins       => { "in1", "in2" }   # optional, creates n_op_in1, ... consts
31 #   mode      => "mode_Iu"          # optional, predefines the mode
32 #   emit      => "emit code with templates",   # optional for virtual nodes
33 #   rd_constructor => "c source code which constructs an ir_node"  # optional
34 # },
35 #
36 # ... # (all nodes you need to describe)
37 #
38 # ); # close the %nodes initializer
39
40 # op_flags: flags for the operation, OPTIONAL (default is "N")
41 # the op_flags correspond to the firm irop_flags:
42 #   N   irop_flag_none
43 #   L   irop_flag_labeled
44 #   C   irop_flag_commutative
45 #   X   irop_flag_cfopcode
46 #   I   irop_flag_ip_cfopcode
47 #   F   irop_flag_fragile
48 #   Y   irop_flag_forking
49 #   H   irop_flag_highlevel
50 #   c   irop_flag_constlike
51 #   K   irop_flag_keep
52 #
53 # irn_flags: special node flags, OPTIONAL (default is 0)
54 # following irn_flags are supported:
55 #   R   rematerializeable
56 #   N   not spillable
57 #   I   ignore for register allocation
58 #
59 # state: state of the operation, OPTIONAL (default is "floats")
60 #
61 # arity: arity of the operation, MUST NOT BE OMITTED
62 #
63 # args:  the OPTIONAL arguments of the node constructor (debug, irg and block
64 #        are always the first 3 arguments and are always autmatically
65 #        created)
66 #        If this key is missing the following arguments will be created:
67 #        for i = 1 .. arity: ir_node *op_i
68 #        ir_mode *mode
69 #
70 # outs:  if a node defines more than one output, the names of the projections
71 #        nodes having outs having automatically the mode mode_T
72 #
73 # comment: OPTIONAL comment for the node constructor
74 #
75 # rd_constructor: for every operation there will be a
76 #      new_rd_<arch>_<op-name> function with the arguments from above
77 #      which creates the ir_node corresponding to the defined operation
78 #      you can either put the complete source code of this function here
79 #
80 #      This key is OPTIONAL. If omitted, the following constructor will
81 #      be created:
82 #      if (!op_<arch>_<op-name>) assert(0);
83 #      for i = 1 to arity
84 #         set in[i] = op_i
85 #      done
86 #      res = new_ir_node(db, irg, block, op_<arch>_<op-name>, mode, arity, in)
87 #      return res
88 #
89 # NOTE: rd_constructor and args are only optional if and only if arity is 0,1,2 or 3
90
91 # register types:
92 #   0 - no special type
93 #   1 - caller save (register must be saved by the caller of a function)
94 #   2 - callee save (register must be saved by the called function)
95 #   4 - ignore (do not assign this register)
96 # NOTE: Last entry of each class is the largest Firm-Mode a register can hold
97 %reg_classes = (
98         general_purpose => [
99                 { name => "r0", type => 1 },
100                 { name => "r1", type => 1 },
101                 { name => "r2", type => 1 },
102                 { name => "r3", type => 1 },
103                 { name => "r4", type => 1 },
104                 { name => "r5", type => 1 },
105                 { name => "r6", type => 1 },
106                 { name => "r7", type => 2 },
107                 { name => "r8", type => 2 },
108                 { name => "r9", type => 2 },
109                 { name => "r10", type => 2 },
110                 { name => "r11", type => 2 },
111                 { name => "r12", type => 2 },
112                 { name => "r13", type => 2 },
113                 { name => "sp", realname => "r14", type => 4 },  # this is our stackpointer
114                 { name => "bp", realname => "r15", type => 4 },  # this is out basepointer
115                 { mode => "mode_Iu" }
116         ],
117         floating_point  => [
118                 { name => "f0", type => 1 },
119                 { name => "f1", type => 1 },
120                 { name => "f2", type => 1 },
121                 { name => "f3", type => 1 },
122                 { name => "f4", type => 1 },
123                 { name => "f5", type => 1 },
124                 { name => "f6", type => 1 },
125                 { name => "f7", type => 1 },
126                 { name => "f8", type => 1 },
127                 { name => "f9", type => 1 },
128                 { name => "f10", type => 1 },
129                 { name => "f11", type => 1 },
130                 { name => "f12", type => 1 },
131                 { name => "f13", type => 1 },
132                 { name => "f14", type => 1 },
133                 { name => "f15", type => 1 },
134                 { mode => "mode_D" }
135         ]
136 ); # %reg_classes
137
138 %emit_templates = (
139     S1 => "${arch}_emit_source_register(node, 0);",
140     S2 => "${arch}_emit_source_register(node, 1);",
141     S3 => "${arch}_emit_source_register(node, 2);",
142     S4 => "${arch}_emit_source_register(node, 3);",
143     S5 => "${arch}_emit_source_register(node, 4);",
144     S6 => "${arch}_emit_source_register(node, 5);",
145     D1 => "${arch}_emit_dest_register(node, 0);",
146     D2 => "${arch}_emit_dest_register(node, 1);",
147     D3 => "${arch}_emit_dest_register(node, 2);",
148     D4 => "${arch}_emit_dest_register(node, 3);",
149     D5 => "${arch}_emit_dest_register(node, 4);",
150     D6 => "${arch}_emit_dest_register(node, 5);",
151         C  => "${arch}_emit_immediate(node);"
152 );
153
154 #--------------------------------------------------#
155 #                        _                         #
156 #                       (_)                        #
157 #  _ __   _____      __  _ _ __    ___  _ __  ___  #
158 # | '_ \ / _ \ \ /\ / / | | '__|  / _ \| '_ \/ __| #
159 # | | | |  __/\ V  V /  | | |    | (_) | |_) \__ \ #
160 # |_| |_|\___| \_/\_/   |_|_|     \___/| .__/|___/ #
161 #                                      | |         #
162 #                                      |_|         #
163 #--------------------------------------------------#
164
165 %nodes = (
166
167 #-----------------------------------------------------------------#
168 #  _       _                                         _            #
169 # (_)     | |                                       | |           #
170 #  _ _ __ | |_ ___  __ _  ___ _ __   _ __   ___   __| | ___  ___  #
171 # | | '_ \| __/ _ \/ _` |/ _ \ '__| | '_ \ / _ \ / _` |/ _ \/ __| #
172 # | | | | | ||  __/ (_| |  __/ |    | | | | (_) | (_| |  __/\__ \ #
173 # |_|_| |_|\__\___|\__, |\___|_|    |_| |_|\___/ \__,_|\___||___/ #
174 #                   __/ |                                         #
175 #                  |___/                                          #
176 #-----------------------------------------------------------------#
177
178 # commutative operations
179
180 Add => {
181   op_flags  => "C",
182   irn_flags => "R",
183   comment   => "construct Add: Add(a, b) = Add(b, a) = a + b",
184   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
185   emit      => '. add %S1, %S2, %D1'
186 },
187
188 Add_i => {
189   irn_flags => "R",
190   comment   => "construct Add: Add(a, const) = Add(const, a) = a + const",
191   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
192   emit      => '. add %S1, %C, %D1'
193 },
194
195 Mul => {
196   op_flags  => "C",
197   irn_flags => "R",
198   comment   => "construct Mul: Mul(a, b) = Mul(b, a) = a * b",
199   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
200   emit      =>'. mul %S1, %S2, %D1'
201 },
202
203 Mul_i => {
204   irn_flags => "R",
205   comment   => "construct Mul: Mul(a, const) = Mul(const, a) = a * const",
206   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
207   emit      => '. mul %S1, %C, %D1'
208 },
209
210 And => {
211   op_flags  => "C",
212   irn_flags => "R",
213   comment   => "construct And: And(a, b) = And(b, a) = a AND b",
214   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
215   emit      => '. and %S1, %S2, %D1'
216 },
217
218 And_i => {
219   irn_flags => "R",
220   comment   => "construct And: And(a, const) = And(const, a) = a AND const",
221   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
222   emit      => '. and %S1, %C, %D1'
223 },
224
225 Or => {
226   op_flags  => "C",
227   irn_flags => "R",
228   comment   => "construct Or: Or(a, b) = Or(b, a) = a OR b",
229   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
230   emit      => '. or %S1, %S2, %D1'
231 },
232
233 Or_i => {
234   op_flags  => "C",
235   irn_flags => "R",
236   comment   => "construct Or: Or(a, const) = Or(const, a) = a OR const",
237   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
238   emit      => '. or %S1, %C, %D1'
239 },
240
241 Eor => {
242   op_flags  => "C",
243   irn_flags => "R",
244   comment   => "construct Eor: Eor(a, b) = Eor(b, a) = a EOR b",
245   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
246   emit      => '. xor %S1, %S2, %D1'
247 },
248
249 Eor_i => {
250   irn_flags => "R",
251   comment   => "construct Eor: Eor(a, const) = Eor(const, a) = a EOR const",
252   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
253   emit      => '. xor %S1, %C, %D1'
254 },
255
256 # not commutative operations
257
258 Sub => {
259   irn_flags => "R",
260   comment   => "construct Sub: Sub(a, b) = a - b",
261   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
262   emit      => '. sub %S1, %S2, %D1'
263 },
264
265 Sub_i => {
266   irn_flags => "R",
267   comment   => "construct Sub: Sub(a, const) = a - const",
268   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
269   emit      => '. subl %S1, %C, %D1'
270 },
271
272 Shl => {
273   irn_flags => "R",
274   comment   => "construct Shl: Shl(a, b) = a << b",
275   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
276   emit      => '. shl %S1, %S2, %D1'
277 },
278
279 Shl_i => {
280   irn_flags => "R",
281   comment   => "construct Shl: Shl(a, const) = a << const",
282   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
283   emit      => '. shl %S1, %C, %D1'
284 },
285
286 Shr => {
287   irn_flags => "R",
288   comment   => "construct Shr: Shr(a, b) = a >> b",
289   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "in_r1" ] },
290   emit      => '. shr %S2, %D1'
291 },
292
293 Shr_i => {
294   irn_flags => "R",
295   comment   => "construct Shr: Shr(a, const) = a >> const",
296   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
297   emit      => '. shr %S1, %C, %D1'
298 },
299
300 RotR => {
301   irn_flags => "R",
302   comment   => "construct RotR: RotR(a, b) = a ROTR b",
303   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
304   emit      => '. ror %S1, %S2, %D1'
305 },
306
307 RotL => {
308   irn_flags => "R",
309   comment   => "construct RotL: RotL(a, b) = a ROTL b",
310   reg_req   => { in => [ "general_purpose", "general_purpose" ], out => [ "general_purpose" ] },
311   emit      => '. rol %S1, %S2, %D1'
312 },
313
314 RotL_i => {
315   irn_flags => "R",
316   comment   => "construct RotL: RotL(a, const) = a ROTL const",
317   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
318   emit      => '. rol %S1, %C, %D1'
319 },
320
321 Minus => {
322   irn_flags => "R",
323   comment   => "construct Minus: Minus(a) = -a",
324   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
325   emit      => '. neg %S1, %D1'
326 },
327
328 Inc => {
329   irn_flags => "R",
330   comment   => "construct Increment: Inc(a) = a++",
331   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
332   emit      => '. inc %S1, %D1'
333 },
334
335 Dec => {
336   irn_flags => "R",
337   comment   => "construct Decrement: Dec(a) = a--",
338   reg_req   => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
339   emit      => '. dec %S1, %D1'
340 },
341
342 Not => {
343   arity       => 1,
344   remat       => 1,
345   comment     => "construct Not: Not(a) = !a",
346   reg_req     => { in => [ "general_purpose" ], out => [ "general_purpose" ] },
347   emit        => '. not %S1, %D1'
348 },
349
350 # other operations
351
352 Const => {
353   op_flags  => "c",
354   irn_flags => "R",
355   comment   => "represents an integer constant",
356   reg_req   => { out => [ "general_purpose" ] },
357   emit      => '. mov %C, %D1',
358   cmp_attr  =>
359 '
360         /* TODO: compare Const attributes */
361     return 1;
362 '
363 },
364
365 # Load / Store
366
367 Load => {
368   op_flags  => "L|F",
369   irn_flags => "R",
370   state     => "exc_pinned",
371   comment   => "construct Load: Load(ptr, mem) = LD ptr -> reg",
372   reg_req   => { in => [ "general_purpose", "none" ], out => [ "general_purpose" ] },
373   emit      => '. mov (%S1), %D1'
374 },
375
376 Store => {
377   op_flags  => "L|F",
378   irn_flags => "R",
379   state     => "exc_pinned",
380   comment   => "construct Store: Store(ptr, val, mem) = ST ptr,val",
381   reg_req   => { in => [ "general_purpose", "general_purpose", "none" ] },
382   emit      => '. movl %S2, (%S1)'
383 },
384
385 #--------------------------------------------------------#
386 #    __ _             _                     _            #
387 #   / _| |           | |                   | |           #
388 #  | |_| | ___   __ _| |_   _ __   ___   __| | ___  ___  #
389 #  |  _| |/ _ \ / _` | __| | '_ \ / _ \ / _` |/ _ \/ __| #
390 #  | | | | (_) | (_| | |_  | | | | (_) | (_| |  __/\__ \ #
391 #  |_| |_|\___/ \__,_|\__| |_| |_|\___/ \__,_|\___||___/ #
392 #--------------------------------------------------------#
393
394 # commutative operations
395
396 fAdd => {
397   op_flags  => "C",
398   irn_flags => "R",
399   comment   => "construct FP Add: Add(a, b) = Add(b, a) = a + b",
400   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
401   emit      => '. fadd %S1, %S2, %D1'
402 },
403
404 fMul => {
405   op_flags  => "C",
406   comment   => "construct FP Mul: Mul(a, b) = Mul(b, a) = a * b",
407   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
408   emit      =>'. fmul %S1, %S2, %D1'
409 },
410
411 fMax => {
412   op_flags  => "C",
413   irn_flags => "R",
414   comment   => "construct FP Max: Max(a, b) = Max(b, a) = a > b ? a : b",
415   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
416   emit      =>'. fmax %S1, %S2, %D1'
417 },
418
419 fMin => {
420   op_flags  => "C",
421   irn_flags => "R",
422   comment   => "construct FP Min: Min(a, b) = Min(b, a) = a < b ? a : b",
423   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
424   emit      =>'. fmin %S1, %S2, %D1'
425 },
426
427 # not commutative operations
428
429 fSub => {
430   irn_flags => "R",
431   comment   => "construct FP Sub: Sub(a, b) = a - b",
432   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
433   emit      => '. fsub %S1, %S2, %D1'
434 },
435
436 fDiv => {
437   comment   => "construct FP Div: Div(a, b) = a / b",
438   reg_req   => { in => [ "floating_point", "floating_point" ], out => [ "floating_point" ] },
439   emit      => '. fdiv %S1, %S2, %D1'
440 },
441
442 fMinus => {
443   irn_flags => "R",
444   comment   => "construct FP Minus: Minus(a) = -a",
445   reg_req   => { in => [ "floating_point" ], out => [ "floating_point" ] },
446   emit      => '. fneg %S1, %D1'
447 },
448
449 # other operations
450
451 fConst => {
452   op_flags  => "c",
453   irn_flags => "R",
454   comment   => "represents a FP constant",
455   reg_req   => { out => [ "floating_point" ] },
456   emit      => '. fmov %C, %D1',
457   cmp_attr  =>
458 '
459         /* TODO: compare fConst attributes */
460         return 1;
461 '
462 },
463
464 # Load / Store
465
466 fLoad => {
467   op_flags  => "L|F",
468   irn_flags => "R",
469   state     => "exc_pinned",
470   comment   => "construct FP Load: Load(ptr, mem) = LD ptr",
471   reg_req   => { in => [ "general_purpose", "none" ], out => [ "floating_point" ] },
472   emit      => '. fmov (%S1), %D1'
473 },
474
475 fStore => {
476   op_flags  => "L|F",
477   irn_flags => "R",
478   state     => "exc_pinned",
479   comment   => "construct Store: Store(ptr, val, mem) = ST ptr,val",
480   reg_req   => { in => [ "general_purpose", "floating_point", "none" ] },
481   emit      => '. fmov %S2, (%S1)'
482 },
483
484 ); # end of %nodes