load-after-load and load-after-store with different mode was implemented in a little...
[libfirm] / scripts / ir_spec.py
1 from spec_util import abstract, setnodedefaults
2
3 class Op(object):
4         """Base class for firm nodes"""
5 abstract(Op)
6
7 class Unop(Op):
8         """Unary nodes have exactly 1 input"""
9         name     = "unop"
10         ins      = [
11                 ("op",  "operand"),
12         ]
13         op_index = 0
14         pinned   = "no"
15 abstract(Unop)
16
17 class Binop(Op):
18         """Binary nodes have exactly 2 inputs"""
19         name     = "binop"
20         ins      = [
21                 ( "left",   "first operand" ),
22                 ( "right", "second operand" ),
23         ]
24         op_index = 0
25         pinned   = "no"
26 abstract(Binop)
27
28 class Add(Binop):
29         """returns the sum of its operands"""
30         flags = [ "commutative" ]
31
32 class Alloc(Op):
33         """allocates a block of memory.
34         It can be specified whether the variable should be allocated to the stack
35         or to the heap."""
36         ins   = [
37                 ("mem",   "memory dependency" ),
38                 ("count", "number of objects to allocate" ),
39         ]
40         outs  = [
41                 ("M",         "memory result",                         "pn_Generic_M"),
42                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
43                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
44                 ("res",       "pointer to newly allocated memory",     "pn_Generic_other"),
45         ]
46         attrs = [
47                 dict(
48                         name    = "type",
49                         type    = "ir_type*",
50                         comment = "type of the allocated variable",
51                 ),
52                 dict(
53                         name    = "where",
54                         type    = "ir_where_alloc",
55                         comment = "whether to allocate the variable on the stack or heap",
56                 )
57         ]
58         flags       = [ "fragile", "uses_memory" ]
59         pinned      = "yes"
60         attr_struct = "alloc_attr"
61
62 class Anchor(Op):
63         """utiliy node used to "hold" nodes in a graph that might possibly not be
64         reachable by other means or which should be reachable immediately without
65         searching through the graph.
66         Each firm-graph contains exactly one anchor node whose address is always
67         known. All other well-known graph-nodes like Start, End, NoMem, Bad, ...
68         are found by looking at the respective Anchor operand."""
69         mode             = "mode_ANY"
70         arity            = "variable"
71         flags            = [ "dump_noblock" ]
72         pinned           = "yes"
73         attr_struct      = "irg_attr"
74         knownBlock       = True
75         singleton        = True
76         noconstructor    = True
77         customSerializer = True
78
79 class And(Binop):
80         """returns the result of a bitwise and operation of its operands"""
81         flags    = [ "commutative" ]
82
83 class ASM(Op):
84         """executes assembler fragments of the target machine"""
85         mode             = "mode_T"
86         arity            = "variable"
87         flags            = [ "keep", "uses_memory" ]
88         pinned           = "memory"
89         pinned_init      = "op_pin_state_pinned"
90         attr_struct      = "asm_attr"
91         attrs_name       = "assem"
92         customSerializer = True
93         attrs = [
94                 dict(
95                         name    = "input_constraints",
96                         type    = "ir_asm_constraint*",
97                         comment = "input constraints",
98                 ),
99                 dict(
100                         name    = "n_output_constraints",
101                         type    = "int",
102                         noprop  = True,
103                         comment = "number of output constraints",
104                 ),
105                 dict(
106                         name    = "output_constraints",
107                         type    = "ir_asm_constraint*",
108                         comment = "output constraints",
109                 ),
110                 dict(
111                         name    = "n_clobbers",
112                         type    = "int",
113                         noprop  = True,
114                         comment = "number of clobbered registers/memory",
115                 ),
116                 dict(
117                         name    = "clobbers",
118                         type    = "ident**",
119                         comment = "list of clobbered registers/memory",
120                 ),
121                 dict(
122                         name    = "text",
123                         type    = "ident*",
124                         comment = "assembler text",
125                 ),
126         ]
127         # constructor is written manually at the moment, because of the clobbers+
128         # constraints arrays needing special handling (2 arguments for 1 attribute)
129         noconstructor = True
130
131 class Bad(Op):
132         """Bad nodes indicate invalid input, which is values which should never be
133         computed.
134
135         The typical use case for the Bad node is removing unreachable code.
136         Frontends should set the current_block to Bad when it is clear that
137         following code must be unreachable (ie. after a goto or return statement).
138         Optimisations also set block predecessors to Bad when it becomes clear,
139         that a control flow edge can never be executed.
140
141         The gigo optimisations ensures that nodes with Bad as their block, get
142         replaced by Bad themselfes. Nodes with at least 1 Bad input get exchanged
143         with Bad too. Exception to this rule are Block, Phi, Tuple and End node;
144         This is because removing inputs from a Block is hairy operation (requiring,
145         Phis to be shortened too for example). So instead of removing block inputs
146         they are set to Bad, and the actual removal is left to the control flow
147         optimisation phase. Block, Phi, Tuple with only Bad inputs however are
148         replaced by Bad right away."""
149         mode          = "mode_T"
150         flags         = [ "cfopcode", "start_block", "dump_noblock" ]
151         pinned        = "yes"
152         knownBlock    = True
153         block         = "get_irg_start_block(irg)"
154         singleton     = True
155         attr_struct   = "bad_attr"
156         init = '''
157         res->attr.bad.irg.irg = irg;
158         '''
159
160 class Deleted(Op):
161         """Internal node which is temporary set to nodes which are already removed
162         from the graph."""
163         mode             = "mode_Bad"
164         flags            = [ ]
165         pinned           = "yes"
166         noconstructor    = True
167         customSerializer = True
168
169 class Block(Op):
170         """A basic block"""
171         mode             = "mode_BB"
172         knownBlock       = True
173         block            = "NULL"
174         pinned           = "yes"
175         arity            = "variable"
176         flags            = [ "labeled" ]
177         attr_struct      = "block_attr"
178         customSerializer = True
179
180         init = '''
181         res->attr.block.irg.irg     = irg;
182         res->attr.block.backedge    = new_backedge_arr(irg->obst, arity);
183         set_Block_matured(res, 1);
184
185         /* Create and initialize array for Phi-node construction. */
186         if (get_irg_phase_state(irg) == phase_building) {
187                 res->attr.block.graph_arr = NEW_ARR_D(ir_node *, irg->obst, irg->n_loc);
188                 memset(res->attr.block.graph_arr, 0, irg->n_loc * sizeof(ir_node*));
189         }
190         '''
191
192 class Borrow(Binop):
193         """Returns the borrow bit from and implied subtractions of its 2 operands"""
194         flags = []
195
196 class Bound(Op):
197         """Performs a bounds-check: if lower <= index < upper then return index,
198         otherwise throw an exception."""
199         ins    = [
200                 ("mem",    "memory dependency"),
201                 ("index",  "value to test"),
202                 ("lower",  "lower bound (inclusive)"),
203                 ("upper",  "upper bound (exclusive)"),
204         ]
205         outs  = [
206                 ("M",         "memory result",                         "pn_Generic_M"),
207                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
208                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
209                 ("res",       "the checked index",                     "pn_Generic_other"),
210         ]
211         flags  = [ "fragile", "highlevel" ]
212         pinned = "exception"
213         pinned_init = "op_pin_state_pinned"
214         attr_struct = "bound_attr"
215         attrs_name  = "bound"
216
217 class Builtin(Op):
218         """performs a backend-specific builtin."""
219         ins      = [
220                 ("mem", "memory dependency"),
221         ]
222         arity    = "variable"
223         outs     = [
224                 ("M",        "memory result", "pn_Generic_M"),
225                 ("1_result", "first result",  "pn_Generic_other"),
226         ]
227         flags    = [ "uses_memory" ]
228         attrs    = [
229                 dict(
230                         type    = "ir_builtin_kind",
231                         name    = "kind",
232                         comment = "kind of builtin",
233                 ),
234                 dict(
235                         type    = "ir_type*",
236                         name    = "type",
237                         comment = "method type for the builtin call",
238                 )
239         ]
240         pinned      = "memory"
241         pinned_init = "op_pin_state_pinned"
242         attr_struct = "builtin_attr"
243         init   = '''
244         assert((get_unknown_type() == type) || is_Method_type(type));
245         '''
246
247 class Call(Op):
248         """Calls other code. Control flow is transfered to ptr, additional
249         operands are passed to the called code. Called code usually performs a
250         return operation. The operands of this return operation are the result
251         of the Call node."""
252         ins      = [
253                 ("mem",   "memory dependency"),
254                 ("ptr",   "pointer to called code"),
255         ]
256         arity    = "variable"
257         outs     = [
258                 ("M",                "memory result",                         "pn_Generic_M"),
259                 ("X_regular",        "control flow when no exception occurs", "pn_Generic_X_regular"),
260                 ("X_except",         "control flow when exception occured",   "pn_Generic_X_except"),
261                 ("T_result",         "tuple containing all results",          "pn_Generic_other"),
262                 ("P_value_res_base", "pointer to memory register containing copied results passed by value"),
263         ]
264         flags    = [ "fragile", "uses_memory" ]
265         attrs    = [
266                 dict(
267                         type    = "ir_type*",
268                         name    = "type",
269                         comment = "type of the call (usually type of the called procedure)",
270                 ),
271                 dict(
272                         type = "unsigned",
273                         name = "tail_call",
274                         # the tail call attribute can only be set by analysis
275                         init = "0",
276                 )
277         ]
278         attr_struct = "call_attr"
279         pinned      = "memory"
280         pinned_init = "op_pin_state_pinned"
281         init = '''
282         assert((get_unknown_type() == type) || is_Method_type(type));
283         '''
284
285 class Carry(Binop):
286         """Computes the value of the carry-bit that would result when adding the 2
287         operands"""
288         flags = [ "commutative" ]
289
290 class Cast(Unop):
291         """perform a high-level type cast"""
292         mode     = "get_irn_mode(irn_op)"
293         flags    = [ "highlevel" ]
294         attrs    = [
295                 dict(
296                         type    = "ir_type*",
297                         name    = "type",
298                         comment = "target type of the case",
299                 )
300         ]
301         attr_struct = "cast_attr"
302         init     = "assert(is_atomic_type(type));"
303
304 class Cmp(Binop):
305         """Compares its two operands and checks wether a specified
306            relation (like less or equal) is fulfilled."""
307         flags = []
308         mode  = "mode_b"
309         attrs = [
310                 dict(
311                         type    = "ir_relation",
312                         name    = "relation",
313                         comment = "Comparison relation"
314                 )
315         ]
316         attr_struct = "cmp_attr"
317
318 class Cond(Op):
319         """Conditionally change control flow. There are two versions of this node:
320
321         Boolean Cond:
322         Input:  A value of mode_b
323         Output: A tuple of two control flows. The first is taken if the input is
324                 false, the second if it is true.
325
326         Switch Cond:
327         Input:  A value of mode_Iu
328         Output: A tuple of n control flows. If the Cond's input is i, control flow
329         will proceed along output i. If the input is >= n control flow proceeds
330         along output def_proj.
331         """
332         ins      = [
333                 ("selector",  "condition parameter"),
334         ]
335         outs     = [
336                 ("false", "control flow if operand is \"false\""),
337                 ("true",  "control flow if operand is \"true\""),
338         ]
339         flags    = [ "cfopcode", "forking" ]
340         pinned   = "yes"
341         attrs    = [
342                 dict(
343                         name    = "default_proj",
344                         type    = "long",
345                         init    = "0",
346                         comment = "Proj-number of default case for switch-Cond",
347                 ),
348                 dict(
349                         name    = "jmp_pred",
350                         type    = "cond_jmp_predicate",
351                         init    = "COND_JMP_PRED_NONE",
352                         comment = "can indicate the most likely jump",
353                 )
354         ]
355         attr_struct = "cond_attr"
356
357 class Confirm(Op):
358         """Specifies constraints for a value. This allows explicit representation
359         of path-sensitive properties. (Example: This value is always >= 0 on 1
360         if-branch then all users within that branch are rerouted to a confirm-node
361         specifying this property).
362
363         A constraint is specified for the relation between value and bound.
364         value is always returned.
365         Note that this node does NOT check or assert the constraint, it merely
366         specifies it."""
367         ins      = [
368                 ("value",  "value to express a constraint for"),
369                 ("bound",  "value to compare against"),
370         ]
371         mode     = "get_irn_mode(irn_value)"
372         flags    = [ "highlevel" ]
373         pinned   = "yes"
374         attrs    = [
375                 dict(
376                         name    = "relation",
377                         type    = "ir_relation",
378                         comment = "relation of value to bound",
379                 ),
380         ]
381         attr_struct = "confirm_attr"
382         attrs_name  = "confirm"
383
384 class Const(Op):
385         """Returns a constant value."""
386         flags      = [ "constlike", "start_block" ]
387         block      = "get_irg_start_block(irg)"
388         mode       = "get_tarval_mode(tarval)"
389         knownBlock = True
390         pinned     = "no"
391         attrs      = [
392                 dict(
393                         type    = "ir_tarval*",
394                         name    = "tarval",
395                         comment = "constant value (a tarval object)",
396                 )
397         ]
398         attr_struct = "const_attr"
399         attrs_name  = "con"
400
401 class Conv(Unop):
402         """Converts values between modes"""
403         flags = []
404         attrs = [
405                 dict(
406                         name    = "strict",
407                         type    = "int",
408                         init    = "0",
409                         comment = "force floating point to restrict precision even if backend computes in higher precision (deprecated)",
410                 )
411         ]
412         attr_struct = "conv_attr"
413         attrs_name  = "conv"
414
415 class CopyB(Op):
416         """Copies a block of memory"""
417         ins   = [
418                 ("mem",  "memory dependency"),
419                 ("dst",  "destination address"),
420                 ("src",  "source address"),
421         ]
422         outs  = [
423                 ("M",         "memory result",                         "pn_Generic_M"),
424                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
425                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
426         ]
427         flags = [ "fragile", "uses_memory" ]
428         attrs = [
429                 dict(
430                         name    = "type",
431                         type    = "ir_type*",
432                         comment = "type of copied data",
433                 )
434         ]
435         attr_struct = "copyb_attr"
436         attrs_name  = "copyb"
437         pinned      = "memory"
438         pinned_init = "op_pin_state_pinned"
439
440 class Div(Op):
441         """returns the quotient of its 2 operands"""
442         ins   = [
443                 ("mem",   "memory dependency"),
444                 ("left",  "first operand"),
445                 ("right", "second operand"),
446         ]
447         outs  = [
448                 ("M",         "memory result",                         "pn_Generic_M"),
449                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
450                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
451                 ("res",       "result of computation",                 "pn_Generic_other"),
452         ]
453         flags = [ "fragile", "uses_memory" ]
454         attrs_name = "div"
455         attrs = [
456                 dict(
457                         type    = "ir_mode*",
458                         name    = "resmode",
459                         comment = "mode of the result value",
460                 ),
461                 dict(
462                         name = "no_remainder",
463                         type = "int",
464                         init = "0",
465                 )
466         ]
467         attr_struct = "div_attr"
468         pinned      = "exception"
469         op_index    = 1
470         arity_override = "oparity_binary"
471
472 class Dummy(Op):
473         """A placeholder value. This is used when constructing cyclic graphs where
474         you have cases where not all predecessors of a phi-node are known. Dummy
475         nodes are used for the unknown predecessors and replaced later."""
476         ins        = []
477         flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
478         knownBlock = True
479         pinned     = "yes"
480         block      = "get_irg_start_block(irg)"
481
482 class End(Op):
483         """Last node of a graph. It references nodes in endless loops (so called
484         keepalive edges)"""
485         mode             = "mode_X"
486         pinned           = "yes"
487         arity            = "dynamic"
488         flags            = [ "cfopcode" ]
489         knownBlock       = True
490         block            = "get_irg_end_block(irg)"
491         singleton        = True
492         customSerializer = True
493
494 class Eor(Binop):
495         """returns the result of a bitwise exclusive or operation of its operands"""
496         flags    = [ "commutative" ]
497
498 class Free(Op):
499         """Frees a block of memory previously allocated by an Alloc node"""
500         ins    = [
501                 ("mem",   "memory dependency" ),
502                 ("ptr",   "pointer to the object to free"),
503                 ("size",  "number of objects to allocate" ),
504         ]
505         mode   = "mode_M"
506         flags  = [ "uses_memory" ]
507         pinned = "yes"
508         attrs  = [
509                 dict(
510                         name    = "type",
511                         type    = "ir_type*",
512                         comment = "type of the allocated variable",
513                 ),
514                 dict(
515                         name    = "where",
516                         type    = "ir_where_alloc",
517                         comment = "whether allocation was on the stack or heap",
518                 )
519         ]
520         attr_struct = "free_attr"
521
522 class Id(Op):
523         """Returns its operand unchanged."""
524         ins    = [
525            ("pred", "the value which is returned unchanged")
526         ]
527         pinned = "no"
528         flags  = []
529
530 class IJmp(Op):
531         """Jumps to the code in its argument. The code has to be in the same
532         function and the the destination must be one of the blocks reachable
533         by the tuple results"""
534         mode     = "mode_X"
535         pinned   = "yes"
536         ins      = [
537            ("target", "target address of the jump"),
538         ]
539         flags    = [ "cfopcode", "forking", "keep" ]
540
541 class InstOf(Op):
542         """Tests wether an object is an instance of a class-type"""
543         ins   = [
544            ("store", "memory dependency"),
545            ("obj",   "pointer to object being queried")
546         ]
547         outs  = [
548                 ("M",         "memory result",                         "pn_Generic_M"),
549                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
550                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
551                 ("res",       "checked object pointer",                "pn_Generic_other"),
552         ]
553         flags = [ "highlevel" ]
554         attrs = [
555                 dict(
556                         name    = "type",
557                         type    = "ir_type*",
558                         comment = "type to check ptr for",
559                 )
560         ]
561         attr_struct = "io_attr"
562         pinned      = "memory"
563         pinned_init = "op_pin_state_floats"
564
565 class Jmp(Op):
566         """Jumps to the block connected through the out-value"""
567         mode     = "mode_X"
568         pinned   = "yes"
569         ins      = []
570         flags    = [ "cfopcode" ]
571
572 class Load(Op):
573         """Loads a value from memory (heap or stack)."""
574         ins   = [
575                 ("mem", "memory dependency"),
576                 ("ptr",  "address to load from"),
577         ]
578         outs  = [
579                 ("M",         "memory result",                         "pn_Generic_M"),
580                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
581                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
582                 ("res",       "result of load operation",              "pn_Generic_other"),
583         ]
584         flags    = [ "fragile", "uses_memory" ]
585         pinned   = "exception"
586         attrs    = [
587                 dict(
588                         type      = "ir_mode*",
589                         name      = "mode",
590                         java_name = "load_mode",
591                         comment   = "mode of the value to be loaded",
592                 ),
593         ]
594         attr_struct = "load_attr"
595         constructor_args = [
596                 dict(
597                         type    = "ir_cons_flags",
598                         name    = "flags",
599                         comment = "specifies alignment, volatility and pin state",
600                 ),
601         ]
602         pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
603         init = '''
604         res->attr.load.volatility = flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile;
605         res->attr.load.aligned = flags & cons_unaligned ? align_non_aligned : align_is_aligned;
606         '''
607
608 class Minus(Unop):
609         """returns the difference between its operands"""
610         flags = []
611
612 class Mod(Op):
613         """returns the remainder of its operands from an implied division.
614
615         Examples:
616         * mod(5,3)   produces 2
617         * mod(5,-3)  produces 2
618         * mod(-5,3)  produces -2
619         * mod(-5,-3) produces -2
620         """
621         ins   = [
622                 ("mem",   "memory dependency"),
623                 ("left",  "first operand"),
624                 ("right", "second operand"),
625         ]
626         outs  = [
627                 ("M",         "memory result",                         "pn_Generic_M"),
628                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
629                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
630                 ("res",       "result of computation",                 "pn_Generic_other"),
631         ]
632         flags = [ "fragile", "uses_memory" ]
633         attrs_name = "mod"
634         attrs = [
635                 dict(
636                         type    = "ir_mode*",
637                         name    = "resmode",
638                         comment = "mode of the result",
639                 ),
640         ]
641         attr_struct = "mod_attr"
642         pinned      = "exception"
643         op_index    = 1
644         arity_override = "oparity_binary"
645
646 class Mul(Binop):
647         """returns the product of its operands"""
648         flags = [ "commutative" ]
649
650 class Mulh(Binop):
651         """returns the upper word of the product of its operands (the part which
652         would not fit into the result mode of a normal Mul anymore)"""
653         flags = [ "commutative" ]
654
655 class Mux(Op):
656         """returns the false or true operand depending on the value of the sel
657         operand"""
658         ins    = [
659            ("sel",   "value making the output selection"),
660            ("false", "selected if sel input is false"),
661            ("true",  "selected if sel input is true"),
662         ]
663         flags  = []
664         pinned = "no"
665
666 class NoMem(Op):
667         """Placeholder node for cases where you don't need any memory input"""
668         mode          = "mode_M"
669         flags         = [ "dump_noblock", "dump_noinput" ]
670         pinned        = "yes"
671         knownBlock    = True
672         block         = "get_irg_start_block(irg)"
673         singleton     = True
674
675 class Not(Unop):
676         """returns the logical complement of a value. Works for integer values too.
677         If the input is false/zero then true/one is returned, otherwise false/zero
678         is returned."""
679         flags = []
680
681 class Or(Binop):
682         """returns the result of a bitwise or operation of its operands"""
683         flags = [ "commutative" ]
684
685 class Phi(Op):
686         """Choose a value based on control flow. A phi node has 1 input for each
687         predecessor of its block. If a block is entered from its nth predecessor
688         all phi nodes produce their nth input as result."""
689         pinned        = "yes"
690         arity         = "variable"
691         flags         = []
692         attr_struct   = "phi_attr"
693         init          = '''
694         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);'''
695         init_after_opt = '''
696         /* Memory Phis in endless loops must be kept alive.
697            As we can't distinguish these easily we keep all of them alive. */
698         if (is_Phi(res) && mode == mode_M)
699                 add_End_keepalive(get_irg_end(irg), res);'''
700
701 class Pin(Op):
702         """Pin the value of the node node in the current block. No users of the Pin
703         node can float above the Block of the Pin. The node cannot float behind
704         this block. Often used to Pin the NoMem node."""
705         ins      = [
706                 ("op", "value which is pinned"),
707         ]
708         mode     = "get_irn_mode(irn_op)"
709         flags    = [ "highlevel" ]
710         pinned   = "yes"
711
712 class Proj(Op):
713         """returns an entry of a tuple value"""
714         ins              = [
715                 ("pred", "the tuple value from which a part is extracted"),
716         ]
717         flags            = []
718         pinned           = "no"
719         knownBlock       = True
720         knownGraph       = True
721         block            = "get_nodes_block(irn_pred)"
722         graph            = "get_irn_irg(irn_pred)"
723         customSerializer = True
724         attrs      = [
725                 dict(
726                         type    = "long",
727                         name    = "proj",
728                         comment = "number of tuple component to be extracted",
729                 ),
730         ]
731         attr_struct = "proj_attr"
732
733 class Raise(Op):
734         """Raises an exception. Unconditional change of control flow. Writes an
735         explicit Except variable to memory to pass it to the exception handler.
736         Must be lowered to a Call to a runtime check function."""
737         ins    = [
738                 ("mem",     "memory dependency"),
739                 ("exo_ptr", "pointer to exception object to be thrown"),
740         ]
741         outs  = [
742                 ("M", "memory result",                     "pn_Generic_M"),
743                 ("X", "control flow to exception handler", "pn_Generic_X_regular"),
744         ]
745         flags  = [ "highlevel", "cfopcode" ]
746         pinned = "yes"
747
748 class Return(Op):
749         """Returns from the current function. Takes memory and return values as
750         operands."""
751         ins      = [
752                 ("mem", "memory dependency"),
753         ]
754         arity    = "variable"
755         mode     = "mode_X"
756         flags    = [ "cfopcode" ]
757         pinned   = "yes"
758
759 class Rotl(Binop):
760         """Returns its first operand bits rotated left by the amount in the 2nd
761         operand"""
762         flags    = []
763
764 class Sel(Op):
765         """Computes the address of a entity of a compound type given the base
766         address of an instance of the compound type."""
767         ins    = [
768                 ("mem", "memory dependency"),
769                 ("ptr", "pointer to object to select from"),
770         ]
771         arity  = "variable"
772         flags  = []
773         mode   = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
774         pinned = "no"
775         attrs  = [
776                 dict(
777                         type    = "ir_entity*",
778                         name    = "entity",
779                         comment = "entity which is selected",
780                 )
781         ]
782         attr_struct = "sel_attr"
783
784 class Shl(Binop):
785         """Returns its first operands bits shifted left by the amount of the 2nd
786         operand"""
787         flags = []
788
789 class Shr(Binop):
790         """Returns its first operands bits shifted right by the amount of the 2nd
791         operand. No special handling for the sign bit (zero extension)"""
792         flags = []
793
794 class Shrs(Binop):
795         """Returns its first operands bits shifted right by the amount of the 2nd
796         operand. The leftmost bit (usually the sign bit) stays the same
797         (sign extension)"""
798         flags = []
799
800 class Start(Op):
801         """The first node of a graph. Execution starts with this node."""
802         outs       = [
803                 ("X_initial_exec", "control flow"),
804                 ("M",              "initial memory"),
805                 ("P_frame_base",   "frame base pointer"),
806                 ("P_tls",          "pointer to thread local storage segment"),
807                 ("T_args",         "function arguments")
808         ]
809         mode             = "mode_T"
810         pinned           = "yes"
811         flags            = [ "cfopcode" ]
812         singleton        = True
813         knownBlock       = True
814         customSerializer = True
815         block            = "get_irg_start_block(irg)"
816
817 class Store(Op):
818         """Stores a value into memory (heap or stack)."""
819         ins   = [
820            ("mem",   "memory dependency"),
821            ("ptr",   "address to store to"),
822            ("value", "value to store"),
823         ]
824         outs  = [
825                 ("M",         "memory result",                         "pn_Generic_M"),
826                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
827                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
828         ]
829         flags    = [ "fragile", "uses_memory" ]
830         pinned   = "exception"
831         attr_struct = "store_attr"
832         pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
833         constructor_args = [
834                 dict(
835                         type    = "ir_cons_flags",
836                         name    = "flags",
837                         comment = "specifies alignment, volatility and pin state",
838                 ),
839         ]
840         init = '''
841         res->attr.store.volatility = flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile;
842         res->attr.store.aligned = flags & cons_unaligned ? align_non_aligned : align_is_aligned;
843         '''
844
845 class Sub(Binop):
846         """returns the difference of its operands"""
847         flags = []
848
849 class SymConst(Op):
850         """A symbolic constant.
851
852          - symconst_type_tag   The symbolic constant represents a type tag.  The
853                                type the tag stands for is given explicitly.
854          - symconst_type_size  The symbolic constant represents the size of a type.
855                                The type of which the constant represents the size
856                                is given explicitly.
857          - symconst_type_align The symbolic constant represents the alignment of a
858                                type.  The type of which the constant represents the
859                                size is given explicitly.
860          - symconst_addr_ent   The symbolic constant represents the address of an
861                                entity (variable or method).  The variable is given
862                                explicitly by a firm entity.
863          - symconst_ofs_ent    The symbolic constant represents the offset of an
864                                entity in its owner type.
865          - symconst_enum_const The symbolic constant is a enumeration constant of
866                                an enumeration type."""
867         mode       = "mode_P"
868         flags      = [ "constlike", "start_block" ]
869         knownBlock = True
870         pinned     = "no"
871         attrs      = [
872                 dict(
873                         type    = "ir_entity*",
874                         name    = "entity",
875                         noprop  = True,
876                         comment = "entity whose address is returned",
877                 )
878         ]
879         attr_struct = "symconst_attr"
880         customSerializer = True
881         # constructor is written manually at the moment, because of the strange
882         # union argument
883         noconstructor = True
884
885 class Sync(Op):
886         """The Sync operation unifies several partial memory blocks. These blocks
887         have to be pairwise disjunct or the values in common locations have to
888         be identical.  This operation allows to specify all operations that
889         eventually need several partial memory blocks as input with a single
890         entrance by unifying the memories with a preceding Sync operation."""
891         mode     = "mode_M"
892         flags    = []
893         pinned   = "no"
894         arity    = "dynamic"
895
896 class Tuple(Op):
897         """Builds a Tuple from single values.
898
899         This is needed to implement optimizations that remove a node that produced
900         a tuple.  The node can be replaced by the Tuple operation so that the
901         following Proj nodes have not to be changed. (They are hard to find due to
902         the implementation with pointers in only one direction.) The Tuple node is
903         smaller than any other node, so that a node can be changed into a Tuple by
904         just changing its opcode and giving it a new in array."""
905         arity  = "variable"
906         mode   = "mode_T"
907         pinned = "no"
908         flags  = [ "labeled" ]
909
910 class Unknown(Op):
911         """Returns an unknown (at compile- and runtime) value. It is a valid
912         optimisation to replace an Unknown by any other constant value."""
913         knownBlock = True
914         pinned     = "yes"
915         block      = "get_irg_start_block(irg)"
916         flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
917
918 # Prepare node list
919
920 def getOpList(namespace):
921         nodes = []
922         for t in namespace.values():
923                 if type(t) != type:
924                         continue
925
926                 if issubclass(t, Op):
927                         setnodedefaults(t)
928                         nodes.append(t)
929         return nodes
930
931 nodes = getOpList(globals())
932 nodes = sorted(nodes, lambda x,y: cmp(x.name, y.name))