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