2ad963eb7c733457b3d4f861829b21ec03e36c4e
[libfirm] / ir / be / sparc / sparc_spec.pl
1 # Creation: 2006/02/13
2 # $Id$
3
4 $arch = "sparc";
5
6 $mode_gp      = "mode_Iu";
7 $mode_flags   = "mode_Bu";
8 $mode_fp      = "mode_D";
9
10 $normal      =  0; # no special type
11 $caller_save =  1; # caller save (register must be saved by the caller of a function)
12 $callee_save =  2; # callee save (register must be saved by the called function)
13 $ignore      =  4; # ignore (do not assign this register)
14 $arbitrary   =  8; # emitter can choose an arbitrary register of this class
15 $virtual     = 16; # the register is a virtual one
16 $state       = 32; # register represents a state
17
18 # available SPARC registers: 8 globals, 24 window regs (8 ins, 8 outs, 8 locals)
19 %reg_classes = (
20         gp => [
21                 { name => "g0", realname => "g0", type => $ignore }, # hardwired 0, behaves like /dev/null
22                 { name => "g1", realname => "g1", type => $caller_save }, # temp. value
23                 { name => "g2", realname => "g2", type => $caller_save },
24                 { name => "g3", realname => "g3", type => $caller_save },
25                 { name => "g4", realname => "g4", type => $caller_save },
26                 { name => "g5", realname => "g5", type => $ignore }, # reserved by SPARC ABI
27                 { name => "g6", realname => "g6", type => $ignore }, # reserved by SPARC ABI
28                 { name => "g7", realname => "g7", type => $ignore }, # reserved by SPARC ABI
29
30                 # window's out registers
31                 { name => "o0", realname => "o0", type => $caller_save }, # param 1 / return value from callee
32                 { name => "o1", realname => "o1", type => $caller_save }, # param 2
33                 { name => "o2", realname => "o2", type => $caller_save }, # param 3
34                 { name => "o3", realname => "o3", type => $caller_save }, # param 4
35                 { name => "o4", realname => "o4", type => $caller_save }, # param 5
36                 { name => "o5", realname => "o5", type => $caller_save }, # param 6
37                 { name => "sp", realname => "sp", type => $ignore }, # our stackpointer
38                 { name => "o7", realname => "o6", type => $ignore }, # temp. value / address of CALL instr.
39
40                 # window's local registers
41                 { name => "l0", realname => "l0", type => 0 },
42                 { name => "l1", realname => "l1", type => 0 },
43                 { name => "l2", realname => "l2", type => 0 },
44                 { name => "l3", realname => "l3", type => 0 },
45                 { name => "l4", realname => "l4", type => 0 },
46                 { name => "l5", realname => "l5", type => 0 },
47                 { name => "l6", realname => "l6", type => 0 },
48                 { name => "l7", realname => "l7", type => 0 },
49
50                 # window's in registers
51                 { name => "i0", realname => "i0", type => 0 }, # incoming param1 / return value to caller
52                 { name => "i1", realname => "i1", type => 0 }, # param 2
53                 { name => "i2", realname => "i2", type => 0 }, # param 3
54                 { name => "i3", realname => "i3", type => 0 }, # param 4
55                 { name => "i4", realname => "i4", type => 0 }, # param 5
56                 { name => "i5", realname => "i5", type => 0 }, # param 6
57                 { name => "fp", realname => "fp", type => $ignore }, # our framepointer
58                 { name => "i7", realname => "i7", type => $ignore }, # return address - 8
59                 { mode => $mode_gp }
60         ],
61         flags => [
62                 { name => "y", realname => "y", type => $ignore },  # the multiply/divide state register
63                 { mode => $mode_flags, flags => "manual_ra" }
64         ],
65         # fp registers can be accessed any time
66         fp  => [
67                 { name => "f0",  type => $caller_save },
68                 { name => "f1",  type => $caller_save },
69                 { name => "f2",  type => $caller_save },
70                 { name => "f3",  type => $caller_save },
71                 { name => "f4",  type => $caller_save },
72                 { name => "f5",  type => $caller_save },
73                 { name => "f6",  type => $caller_save },
74                 { name => "f7",  type => $caller_save },
75                 { name => "f8",  type => $caller_save },
76                 { name => "f9",  type => $caller_save },
77                 { name => "f10", type => $caller_save },
78                 { name => "f11", type => $caller_save },
79                 { name => "f12", type => $caller_save },
80                 { name => "f13", type => $caller_save },
81                 { name => "f14", type => $caller_save },
82                 { name => "f15", type => $caller_save },
83                 { name => "f16", type => $caller_save },
84                 { name => "f17", type => $caller_save },
85                 { name => "f18", type => $caller_save },
86                 { name => "f19", type => $caller_save },
87                 { name => "f20", type => $caller_save },
88                 { name => "f21", type => $caller_save },
89                 { name => "f22", type => $caller_save },
90                 { name => "f23", type => $caller_save },
91                 { name => "f24", type => $caller_save },
92                 { name => "f25", type => $caller_save },
93                 { name => "f26", type => $caller_save },
94                 { name => "f27", type => $caller_save },
95                 { name => "f28", type => $caller_save },
96                 { name => "f29", type => $caller_save },
97                 { name => "f30", type => $caller_save },
98                 { name => "f31", type => $caller_save },
99                 { mode => $mode_fp }
100         ]
101 ); # %reg_classes
102
103 %emit_templates = (
104 # emit source reg or imm dep. on node's arity
105         RI => "${arch}_emit_reg_or_imm(node, -1);",
106         R1I => "${arch}_emit_reg_or_imm(node, 0);",
107         R2I => "${arch}_emit_reg_or_imm(node, 1);",
108         R3I => "${arch}_emit_reg_or_imm(node, 2);",
109 # simple reg emitters
110         S1 => "${arch}_emit_source_register(node, 0);",
111         S2 => "${arch}_emit_source_register(node, 1);",
112         S3 => "${arch}_emit_source_register(node, 2);",
113         S4 => "${arch}_emit_source_register(node, 3);",
114         S5 => "${arch}_emit_source_register(node, 4);",
115         S6 => "${arch}_emit_source_register(node, 5);",
116         D1 => "${arch}_emit_dest_register(node, 0);",
117         D2 => "${arch}_emit_dest_register(node, 1);",
118         D3 => "${arch}_emit_dest_register(node, 2);",
119         D4 => "${arch}_emit_dest_register(node, 3);",
120         D5 => "${arch}_emit_dest_register(node, 4);",
121         D6 => "${arch}_emit_dest_register(node, 5);",
122 # more custom emitters
123         C  => "${arch}_emit_immediate(node);",
124         LM  => "${arch}_emit_load_mode(node);",
125         SM  => "${arch}_emit_store_mode(node);",
126         EXTPREF  => "${arch}_emit_mode_sign_prefix(node);",
127         FPM  => "${arch}_emit_fp_mode_suffix(node);",
128         FPLM  => "${arch}_emit_fp_load_mode(node);",
129         FPSM  => "${arch}_emit_fp_store_mode(node);",
130         O  => "${arch}_emit_offset(node);",
131 );
132
133 $default_attr_type = "sparc_attr_t";
134 $default_copy_attr = "sparc_copy_attr";
135
136
137 %init_attr = (
138         sparc_attr_t             => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);",
139         sparc_load_store_attr_t  => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);\n".
140                                     "\tinit_sparc_load_store_attributes(res, ls_mode, entity, entity_sign, offset, is_frame_entity);",
141         sparc_symconst_attr_t    => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);\n".
142                                     "\tinit_sparc_symconst_attributes(res, entity);",
143         sparc_cmp_attr_t         => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);\n",
144         sparc_jmp_cond_attr_t    => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);",
145         sparc_jmp_switch_attr_t  => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);",
146         sparc_save_attr_t        => "\tinit_sparc_attributes(res, flags, in_reqs, exec_units, n_res);",
147
148 );
149
150 %compare_attr = (
151         sparc_attr_t            => "cmp_attr_sparc",
152         sparc_load_store_attr_t => "cmp_attr_sparc_load_store",
153         sparc_symconst_attr_t   => "cmp_attr_sparc_symconst",
154         sparc_jmp_cond_attr_t   => "cmp_attr_sparc_jmp_cond",
155         sparc_jmp_switch_attr_t => "cmp_attr_sparc_jmp_switch",
156         sparc_cmp_attr_t        => "cmp_attr_sparc_cmp",
157         sparc_save_attr_t       => "cmp_attr_sparc_save",
158 );
159
160 # addressing modes: imm, reg, reg +/- imm, reg + reg
161 # max. imm = 13 bits signed (-4096 ... 4096)
162
163 my %cmp_operand_constructors = (
164         imm => {
165                 attr       => "int immediate_value, bool ins_permuted, bool is_unsigned",
166                 custominit => "sparc_set_attr_imm(res, immediate_value);" .
167                                                 "\tinit_sparc_cmp_attr(res, ins_permuted, is_unsigned);",
168                 reg_req    => { in => [ "gp" ], out => [ "flags" ] },
169         ins        => [ "left" ],
170         },
171         reg => {
172         attr       => "bool ins_permuted, bool is_unsigned",
173                 custominit => "init_sparc_cmp_attr(res, ins_permuted, is_unsigned);",
174                 reg_req    => { in => [ "gp", "gp" ], out => [ "flags" ] },
175                 ins        => [ "left", "right" ],
176         },
177 );
178
179 my %unop_operand_constructors = (
180         imm => {
181                 attr       => "int immediate_value",
182                 custominit => "sparc_set_attr_imm(res, immediate_value);",
183                 reg_req    => { in => [], out => [ "gp" ] },
184         },
185         reg => {
186                 reg_req    => { in => [ "gp" ], out => [ "gp" ] },
187         },
188 );
189
190 my %binop_operand_constructors = (
191         imm => {
192                 attr       => "int immediate_value",
193                 custominit => "sparc_set_attr_imm(res, immediate_value);",
194                 reg_req    => { in => [ "gp" ], out => [ "gp" ] },
195                 ins        => [ "left" ],
196         },
197         reg => {
198                 reg_req    => { in => [ "gp", "gp" ], out => [ "gp" ] },
199                 ins        => [ "left", "right" ],
200         },
201 );
202
203 %nodes = (
204
205 Add => {
206         irn_flags => [ "rematerializable" ],
207         mode            => $mode_gp,
208         emit      => '. add %S1, %R2I, %D1',
209         constructors => \%binop_operand_constructors,
210 },
211
212 Sub => {
213         irn_flags => [ "rematerializable" ],
214         mode            => $mode_gp,
215         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
216         emit      => '. sub %S1, %R2I, %D1',
217         constructors => \%binop_operand_constructors,
218 },
219
220
221 # Load / Store
222 Ld => {
223         op_flags  => [ "labeled", "fragile" ],
224         state     => "exc_pinned",
225         ins       => [ "ptr", "mem" ],
226         outs      => [ "res", "M" ],
227         reg_req   => { in => [ "gp", "none" ], out => [ "gp", "none" ] },
228         attr_type => "sparc_load_store_attr_t",
229         attr      => "ir_mode *ls_mode, ir_entity *entity, int entity_sign, long offset, bool is_frame_entity",
230         emit      => '. ld%LM [%S1%O], %D1'
231 },
232
233 HiImm => {
234         irn_flags => [ "rematerializable" ],
235         state     => "exc_pinned",
236         outs      => [ "res" ],
237         mode      => $mode_gp,
238         reg_req   => { in => [], out => [ "gp" ] },
239         attr       => "int immediate_value",
240         custominit => "sparc_set_attr_imm(res, immediate_value);",
241 },
242
243 LoImm => {
244         irn_flags => [ "rematerializable" ],
245         state     => "exc_pinned",
246         ins       => [ "hireg" ],
247         outs      => [ "res" ],
248         mode      => $mode_gp,
249         reg_req   => { in => [ "gp" ], out => [ "gp" ] },
250         attr       => "int immediate_value",
251         custominit => "sparc_set_attr_imm(res, immediate_value);",
252 },
253
254 St => {
255         op_flags  => [ "labeled", "fragile" ],
256         mode            => "mode_M",
257         state     => "exc_pinned",
258         ins       => [ "ptr", "val", "mem" ],
259         outs      => [ "mem" ],
260         reg_req   => { in => [ "gp", "gp", "none" ], out => [ "none" ] },
261         attr_type => "sparc_load_store_attr_t",
262         attr      => "ir_mode *ls_mode, ir_entity *entity, int entity_sign, long offset, bool is_frame_entity",
263         emit      => '. st%SM %S2, [%S1%O]'
264 },
265
266 Mov => {
267         irn_flags => [ "rematerializable" ],
268         arity     => "variable",
269         emit      => '. mov %R1I, %D1',
270         mode      => $mode_gp,
271         constructors => \%unop_operand_constructors,
272 },
273
274 Save => {
275         reg_req   => {
276                 in => [ "sp", "none"],
277                 out => [ "sp:I|S","none" ]
278         },
279         ins       => [ "stack", "mem" ],
280         outs      => [ "stack", "mem" ],
281         attr      => "int initial_stacksize",
282         attr_type => "sparc_save_attr_t",
283         init_attr => "\tinit_sparc_save_attr(res, initial_stacksize);",
284 },
285
286 SubSP => {
287         reg_req   => { in => [ "sp", "gp", "none" ], out => [ "sp:I|S", "gp", "none" ] },
288         ins       => [ "stack", "size", "mem" ],
289         outs      => [ "stack", "addr", "M" ],
290         emit      => ". sub %S1, %S2, %D1\n",
291 },
292
293 AddSP => {
294         reg_req   => { in => [ "sp", "gp", "none" ], out => [ "sp:I|S", "none" ] },
295         ins       => [ "stack", "size", "mem" ],
296         outs      => [ "stack", "M" ],
297         emit      => ". add %S1, %S2, %D1\n",
298 },
299
300 SymConst => {
301         op_flags  => [ "constlike" ],
302         irn_flags => [ "rematerializable" ],
303         attr      => "ir_entity *entity",
304         reg_req   => { out => [ "gp" ] },
305         attr_type => "sparc_symconst_attr_t",
306         mode      => $mode_gp,
307 },
308
309 FrameAddr => {
310         op_flags  => [ "constlike" ],
311         irn_flags => [ "rematerializable" ],
312         attr      => "ir_entity *entity",
313         reg_req   => { in => [ "gp" ], out => [ "gp" ] },
314         ins       => [ "base" ],
315         attr_type => "sparc_symconst_attr_t",
316         mode      => $mode_gp,
317 },
318
319 BXX => {
320         op_flags  => [ "labeled", "cfopcode", "forking" ],
321         state     => "pinned",
322         mode      => "mode_T",
323         reg_req   => { in => [ "flags" ], out => [ "none", "none" ] },
324         attr      => "int proj_num",
325         attr_type => "sparc_jmp_cond_attr_t",
326         init_attr => "\tset_sparc_jmp_cond_proj_num(res, proj_num);",
327 },
328
329 Ba => {
330         state     => "pinned",
331         op_flags  => [ "cfopcode" ],
332         irn_flags => [ "simple_jump" ],
333         reg_req   => { out => [ "none" ] },
334         mode      => "mode_X",
335 },
336
337 Cmp => {
338         irn_flags    => [ "rematerializable", "modify_flags" ],
339         emit         => '. cmp %S1, %R2I',
340         mode         => $mode_flags,
341         attr_type    => "sparc_cmp_attr_t",
342         constructors => \%cmp_operand_constructors,
343 },
344
345 Tst => {
346         irn_flags    => [ "rematerializable", "modify_flags" ],
347         emit         => '. tst %S1',
348         mode         => $mode_flags,
349         attr_type    => "sparc_cmp_attr_t",
350         attr         => "bool ins_permuted, bool is_unsigned",
351         custominit   => "init_sparc_cmp_attr(res, ins_permuted, is_unsigned);",
352         reg_req      => { in => [ "gp" ], out => [ "flags" ] },
353         ins          => [ "left" ],
354 },
355
356 SwitchJmp => {
357         op_flags  => [ "labeled", "cfopcode", "forking" ],
358         state     => "pinned",
359         mode      => "mode_T",
360         attr      => "int n_projs, long def_proj_num",
361         init_attr => "\tset_sparc_jmp_switch_n_projs(res, n_projs);\n".
362                                         "\tset_sparc_jmp_switch_default_proj_num(res, def_proj_num);",
363         reg_req   => { in => [ "gp" ], out => [ "none" ] },
364         attr_type => "sparc_jmp_switch_attr_t",
365 },
366
367 Sll => {
368         irn_flags => [ "rematerializable" ],
369         mode            => $mode_gp,
370         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
371         emit      => '. sll %S1, %R2I, %D1',
372         constructors => \%binop_operand_constructors,
373 },
374
375 Slr => {
376         irn_flags => [ "rematerializable" ],
377         mode            => $mode_gp,
378         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
379         emit      => '. srl %S1, %R2I, %D1',
380         constructors => \%binop_operand_constructors,
381 },
382
383 Sra => {
384         irn_flags => [ "rematerializable" ],
385         mode            => $mode_gp,
386         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
387         emit      => '. sra %S1, %R2I, %D1',
388         constructors => \%binop_operand_constructors,
389 },
390
391 And => {
392         irn_flags => [ "rematerializable" ],
393         mode            => $mode_gp,
394         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
395         emit      => '. and %S1, %R2I, %D1',
396         constructors => \%binop_operand_constructors,
397 },
398
399 Or => {
400         irn_flags => [ "rematerializable" ],
401         mode            => $mode_gp,
402         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
403         emit      => '. or %S1, %R2I, %D1',
404         constructors => \%binop_operand_constructors,
405 },
406
407 Xor => {
408         irn_flags => [ "rematerializable" ],
409         mode            => $mode_gp,
410         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
411         emit      => '. xor %S1, %R2I, %D1',
412         constructors => \%binop_operand_constructors,
413 },
414
415 Mul => {
416         state     => "exc_pinned",
417         reg_req   => { in => [ "gp", "gp" ], out => [ "gp", "flags" ] },
418         outs      => [ "low", "high" ],
419         constructors => \%binop_operand_constructors,
420         #emit      =>'. mul %S1, %R2I, %D1'
421 },
422
423 Mulh => {
424         state     => "exc_pinned",
425         reg_req   => { in => [ "gp", "gp" ], out => [ "gp", "gp" ] },
426         outs      => [ "low", "high" ],
427         constructors => \%binop_operand_constructors,
428 },
429
430 Div => {
431         irn_flags => [ "rematerializable" ],
432         state     => "exc_pinned",
433         reg_req   => { in => [ "gp", "gp" ], out => [ "gp" ] },
434         outs      => [ "res" ],
435         constructors => \%binop_operand_constructors,
436         #mode      => $mode_gp,
437         #emit      =>'. div %S1, %R2I, %D1'
438 },
439
440 Minus => {
441         irn_flags => [ "rematerializable" ],
442         mode        => $mode_gp,
443         reg_req   => { in => [ "gp" ], out => [ "gp" ] },
444         emit      => ". sub %%g0, %S1, %D1"
445 },
446
447 Not => {
448         irn_flags   => [ "rematerializable" ],
449         mode          => $mode_gp,
450         reg_req     => { in => [ "gp" ], out => [ "gp" ] },
451         emit        => '. xnor %S1, %%g0, %D1'
452 },
453
454 Nop => {
455         op_flags => [ "keep" ],
456         reg_req  => { in => [], out => [ "none" ] },
457         emit     => '. nop',
458 },
459
460 fAdd => {
461         op_flags  => [ "commutative" ],
462         irn_flags => [ "rematerializable" ],
463         reg_req   => { in => [ "fp", "fp" ], out => [ "fp" ] },
464         emit      => '. fadd%FPM %S1, %S2, %D1'
465 },
466
467 fMul => {
468         op_flags  => [ "commutative" ],
469         reg_req   => { in => [ "fp", "fp" ], out => [ "fp" ] },
470         emit      =>'. fmul%FPM %S1, %S2, %D1'
471 },
472
473 fsMuld => {
474         op_flags  => [ "commutative" ],
475         reg_req   => { in => [ "fp", "fp" ], out => [ "fp" ] },
476         emit      =>'. fsmuld %S1, %S2, %D1'
477 },
478
479 FsTOd => {
480         irn_flags => [ "rematerializable" ],
481         reg_req   => { in => [ "fp" ], out => [ "fp" ] },
482         emit      =>'. FsTOd %S1, %D1'
483 },
484
485 FdTOs => {
486         irn_flags => [ "rematerializable" ],
487         reg_req   => { in => [ "fp" ], out => [ "fp" ] },
488         emit      =>'. FdTOs %S1, %D1'
489 },
490
491 FiTOs => {
492         irn_flags => [ "rematerializable" ],
493         reg_req   => { in => [ "gp" ], out => [ "fp" ] },
494         emit      =>'. FiTOs %S1, %D1'
495 },
496
497 FiTOd => {
498         irn_flags => [ "rematerializable" ],
499         reg_req   => { in => [ "gp" ], out => [ "fp" ] },
500         emit      =>'. FiTOd %S1, %D1'
501 },
502
503 FsTOi => {
504         irn_flags => [ "rematerializable" ],
505         reg_req   => { in => [ "fp" ], out => [ "gp" ] },
506         emit      =>'. FsTOi %S1, %D1'
507 },
508
509 FdTOi => {
510         irn_flags => [ "rematerializable" ],
511         reg_req   => { in => [ "fp" ], out => [ "gp" ] },
512         emit      =>'. FdTOi %S1, %D1'
513 },
514
515 ); # end of %nodes