4bf7842380e87bfab0221436c38780b407d6bcbd
[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         """Returns the relation of 2 operands"""
306         outs  = [
307                 ("False", "always false",                            "0"),
308                 ("Eq",    "equal",                                   "1"),
309                 ("Lt",    "less",                                    "2"),
310                 ("Le",    "less or equal",                           "pn_Cmp_Eq|pn_Cmp_Lt"),
311                 ("Gt",    "greater",                                 "4"),
312                 ("Ge",    "greater or equal",                        "pn_Cmp_Eq|pn_Cmp_Gt"),
313                 ("Lg",    "less or greater ('not equal' for integer numbers)", "pn_Cmp_Lt|pn_Cmp_Gt"),
314                 ("Leg",   "less, equal or greater ('not unordered')", "pn_Cmp_Lt|pn_Cmp_Eq|pn_Cmp_Gt"),
315                 ("Uo",    "unordered",                               "8"),
316                 ("Ue",    "unordered or equal",                      "pn_Cmp_Uo|pn_Cmp_Eq"),
317                 ("Ul",    "unordered or less",                       "pn_Cmp_Uo|pn_Cmp_Lt"),
318                 ("Ule",   "unordered, less or equal",                "pn_Cmp_Uo|pn_Cmp_Lt|pn_Cmp_Eq"),
319                 ("Ug",    "unordered or greater",                    "pn_Cmp_Uo|pn_Cmp_Gt"),
320                 ("Uge",   "onordered, greater or equal",             "pn_Cmp_Uo|pn_Cmp_Gt|pn_Cmp_Eq"),
321                 ("Ne",    "unordered, less or greater ('not equal' for floatingpoint numbers)", "pn_Cmp_Uo|pn_Cmp_Lt|pn_Cmp_Gt"),
322                 ("True",  "always true",                             "15"),
323         ]
324         flags = []
325
326 class Cond(Op):
327         """Conditionally change control flow. There are two versions of this node:
328
329         Boolean Cond:
330         Input:  A value of mode_b
331         Output: A tuple of two control flows. The first is taken if the input is
332                 false, the second if it is true.
333
334         Switch Cond:
335         Input:  A value of mode_Iu
336         Output: A tuple of n control flows. If the Cond's input is i, control flow
337         will proceed along output i. If the input is >= n control flow proceeds
338         along output def_proj.
339         """
340         ins      = [
341                 ("selector",  "condition parameter"),
342         ]
343         outs     = [
344                 ("false", "control flow if operand is \"false\""),
345                 ("true",  "control flow if operand is \"true\""),
346         ]
347         flags    = [ "cfopcode", "forking" ]
348         pinned   = "yes"
349         attrs    = [
350                 dict(
351                         name    = "default_proj",
352                         type    = "long",
353                         init    = "0",
354                         comment = "Proj-number of default case for switch-Cond",
355                 ),
356                 dict(
357                         name    = "jmp_pred",
358                         type    = "cond_jmp_predicate",
359                         init    = "COND_JMP_PRED_NONE",
360                         comment = "can indicate the most likely jump",
361                 )
362         ]
363         attr_struct = "cond_attr"
364
365 class Confirm(Op):
366         """Specifies constraints for a value. This allows explicit representation
367         of path-sensitive properties. (Example: This value is always >= 0 on 1
368         if-branch then all users within that branch are rerouted to a confirm-node
369         specifying this property).
370
371         A constraint is specified for the relation between value and bound.
372         value is always returned.
373         Note that this node does NOT check or assert the constraint, it merely
374         specifies it."""
375         ins      = [
376                 ("value",  "value to express a constraint for"),
377                 ("bound",  "value to compare against"),
378         ]
379         mode     = "get_irn_mode(irn_value)"
380         flags    = [ "highlevel" ]
381         pinned   = "yes"
382         attrs    = [
383                 dict(
384                         name    = "cmp",
385                         type    = "pn_Cmp",
386                         comment = "compare operation",
387                 ),
388         ]
389         attr_struct = "confirm_attr"
390         attrs_name  = "confirm"
391
392 class Const(Op):
393         """Returns a constant value."""
394         flags      = [ "constlike", "start_block" ]
395         block      = "get_irg_start_block(irg)"
396         mode       = "get_tarval_mode(tarval)"
397         knownBlock = True
398         pinned     = "no"
399         attrs      = [
400                 dict(
401                         type    = "ir_tarval*",
402                         name    = "tarval",
403                         comment = "constant value (a tarval object)",
404                 )
405         ]
406         attr_struct = "const_attr"
407         attrs_name  = "con"
408
409 class Conv(Unop):
410         """Converts values between modes"""
411         flags = []
412         attrs = [
413                 dict(
414                         name    = "strict",
415                         type    = "int",
416                         init    = "0",
417                         comment = "force floating point to restrict precision even if backend computes in higher precision (deprecated)",
418                 )
419         ]
420         attr_struct = "conv_attr"
421         attrs_name  = "conv"
422
423 class CopyB(Op):
424         """Copies a block of memory"""
425         ins   = [
426                 ("mem",  "memory dependency"),
427                 ("dst",  "destination address"),
428                 ("src",  "source address"),
429         ]
430         outs  = [
431                 ("M",         "memory result",                         "pn_Generic_M"),
432                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
433                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
434         ]
435         flags = [ "fragile", "uses_memory" ]
436         attrs = [
437                 dict(
438                         name    = "type",
439                         type    = "ir_type*",
440                         comment = "type of copied data",
441                 )
442         ]
443         attr_struct = "copyb_attr"
444         attrs_name  = "copyb"
445         pinned      = "memory"
446         pinned_init = "op_pin_state_pinned"
447
448 class Div(Op):
449         """returns the quotient of its 2 operands, integer version"""
450         ins   = [
451                 ("mem",   "memory dependency"),
452                 ("left",  "first operand"),
453                 ("right", "second operand"),
454         ]
455         outs  = [
456                 ("M",         "memory result",                         "pn_Generic_M"),
457                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
458                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
459                 ("res",       "result of computation",                 "pn_Generic_other"),
460         ]
461         flags = [ "fragile", "uses_memory" ]
462         attrs_name = "divmod"
463         attrs = [
464                 dict(
465                         type    = "ir_mode*",
466                         name    = "resmode",
467                         comment = "mode of the result value",
468                 ),
469                 dict(
470                         name = "no_remainder",
471                         type = "int",
472                         init = "0",
473                 )
474         ]
475         attr_struct = "divmod_attr"
476         pinned      = "exception"
477         op_index    = 1
478         arity_override = "oparity_binary"
479
480 class DivMod(Op):
481         """divides its 2 operands and computes the remainder of the division"""
482         ins   = [
483                 ("mem",   "memory dependency"),
484                 ("left",  "first operand"),
485                 ("right", "second operand"),
486         ]
487         outs  = [
488                 ("M",         "memory result",                         "pn_Generic_M"),
489                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
490                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
491                 ("res_div",   "result of computation a/b",             "pn_Generic_other"),
492                 ("res_mod",   "result of computation a%b"),
493         ]
494         flags = [ "fragile", "uses_memory" ]
495         attrs_name = "divmod"
496         attrs = [
497                 dict(
498                         type    = "ir_mode*",
499                         name    = "resmode",
500                         comment = "mode of the result value",
501                 ),
502         ]
503         attr_struct = "divmod_attr"
504         pinned      = "exception"
505         op_index    = 1
506         arity_override = "oparity_binary"
507
508 class Dummy(Op):
509         """A placeholder value. This is used when constructing cyclic graphs where
510         you have cases where not all predecessors of a phi-node are known. Dummy
511         nodes are used for the unknown predecessors and replaced later."""
512         ins        = []
513         flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
514         knownBlock = True
515         pinned     = "yes"
516         block      = "get_irg_start_block(irg)"
517
518 class End(Op):
519         """Last node of a graph. It references nodes in endless loops (so called
520         keepalive edges)"""
521         mode             = "mode_X"
522         pinned           = "yes"
523         arity            = "dynamic"
524         flags            = [ "cfopcode" ]
525         knownBlock       = True
526         block            = "get_irg_end_block(irg)"
527         singleton        = True
528         customSerializer = True
529
530 class Eor(Binop):
531         """returns the result of a bitwise exclusive or operation of its operands"""
532         flags    = [ "commutative" ]
533
534 class Free(Op):
535         """Frees a block of memory previously allocated by an Alloc node"""
536         ins    = [
537                 ("mem",   "memory dependency" ),
538                 ("ptr",   "pointer to the object to free"),
539                 ("size",  "number of objects to allocate" ),
540         ]
541         mode   = "mode_M"
542         flags  = [ "uses_memory" ]
543         pinned = "yes"
544         attrs  = [
545                 dict(
546                         name    = "type",
547                         type    = "ir_type*",
548                         comment = "type of the allocated variable",
549                 ),
550                 dict(
551                         name    = "where",
552                         type    = "ir_where_alloc",
553                         comment = "whether allocation was on the stack or heap",
554                 )
555         ]
556         attr_struct = "free_attr"
557
558 class Id(Op):
559         """Returns its operand unchanged."""
560         ins    = [
561            ("pred", "the value which is returned unchanged")
562         ]
563         pinned = "no"
564         flags  = []
565
566 class IJmp(Op):
567         """Jumps to the code in its argument. The code has to be in the same
568         function and the the destination must be one of the blocks reachable
569         by the tuple results"""
570         mode     = "mode_X"
571         pinned   = "yes"
572         ins      = [
573            ("target", "target address of the jump"),
574         ]
575         flags    = [ "cfopcode", "forking", "keep" ]
576
577 class InstOf(Op):
578         """Tests wether an object is an instance of a class-type"""
579         ins   = [
580            ("store", "memory dependency"),
581            ("obj",   "pointer to object being queried")
582         ]
583         outs  = [
584                 ("M",         "memory result",                         "pn_Generic_M"),
585                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
586                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
587                 ("res",       "checked object pointer",                "pn_Generic_other"),
588         ]
589         flags = [ "highlevel" ]
590         attrs = [
591                 dict(
592                         name    = "type",
593                         type    = "ir_type*",
594                         comment = "type to check ptr for",
595                 )
596         ]
597         attr_struct = "io_attr"
598         pinned      = "memory"
599         pinned_init = "op_pin_state_floats"
600
601 class Jmp(Op):
602         """Jumps to the block connected through the out-value"""
603         mode     = "mode_X"
604         pinned   = "yes"
605         ins      = []
606         flags    = [ "cfopcode" ]
607
608 class Load(Op):
609         """Loads a value from memory (heap or stack)."""
610         ins   = [
611                 ("mem", "memory dependency"),
612                 ("ptr",  "address to load from"),
613         ]
614         outs  = [
615                 ("M",         "memory result",                         "pn_Generic_M"),
616                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
617                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
618                 ("res",       "result of load operation",              "pn_Generic_other"),
619         ]
620         flags    = [ "fragile", "uses_memory" ]
621         pinned   = "exception"
622         attrs    = [
623                 dict(
624                         type      = "ir_mode*",
625                         name      = "mode",
626                         java_name = "load_mode",
627                         comment   = "mode of the value to be loaded",
628                 ),
629         ]
630         attr_struct = "load_attr"
631         constructor_args = [
632                 dict(
633                         type    = "ir_cons_flags",
634                         name    = "flags",
635                         comment = "specifies alignment, volatility and pin state",
636                 ),
637         ]
638         pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
639         init = '''
640         res->attr.load.volatility = flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile;
641         res->attr.load.aligned = flags & cons_unaligned ? align_non_aligned : align_is_aligned;
642         '''
643
644 class Minus(Unop):
645         """returns the difference between its operands"""
646         flags = []
647
648 class Mod(Op):
649         """returns the remainder of its operands from an implied division.
650
651         Examples:
652         * mod(5,3)   produces 2
653         * mod(5,-3)  produces 2
654         * mod(-5,3)  produces -2
655         * mod(-5,-3) produces -2
656         """
657         ins   = [
658                 ("mem",   "memory dependency"),
659                 ("left",  "first operand"),
660                 ("right", "second operand"),
661         ]
662         outs  = [
663                 ("M",         "memory result",                         "pn_Generic_M"),
664                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
665                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
666                 ("res",       "result of computation",                 "pn_Generic_other"),
667         ]
668         flags = [ "fragile", "uses_memory" ]
669         attrs_name = "divmod"
670         attrs = [
671                 dict(
672                         type    = "ir_mode*",
673                         name    = "resmode",
674                         comment = "mode of the result",
675                 ),
676         ]
677         attr_struct = "divmod_attr"
678         pinned      = "exception"
679         op_index    = 1
680         arity_override = "oparity_binary"
681
682 class Mul(Binop):
683         """returns the product of its operands"""
684         flags = [ "commutative" ]
685
686 class Mulh(Binop):
687         """returns the upper word of the product of its operands (the part which
688         would not fit into the result mode of a normal Mul anymore)"""
689         flags = [ "commutative" ]
690
691 class Mux(Op):
692         """returns the false or true operand depending on the value of the sel
693         operand"""
694         ins    = [
695            ("sel",   "value making the output selection"),
696            ("false", "selected if sel input is false"),
697            ("true",  "selected if sel input is true"),
698         ]
699         flags  = []
700         pinned = "no"
701
702 class NoMem(Op):
703         """Placeholder node for cases where you don't need any memory input"""
704         mode          = "mode_M"
705         flags         = [ "dump_noblock", "dump_noinput" ]
706         pinned        = "yes"
707         knownBlock    = True
708         block         = "get_irg_start_block(irg)"
709         singleton     = True
710
711 class Not(Unop):
712         """returns the logical complement of a value. Works for integer values too.
713         If the input is false/zero then true/one is returned, otherwise false/zero
714         is returned."""
715         flags = []
716
717 class Or(Binop):
718         """returns the result of a bitwise or operation of its operands"""
719         flags = [ "commutative" ]
720
721 class Phi(Op):
722         """Choose a value based on control flow. A phi node has 1 input for each
723         predecessor of its block. If a block is entered from its nth predecessor
724         all phi nodes produce their nth input as result."""
725         pinned        = "yes"
726         arity         = "variable"
727         flags         = []
728         attr_struct   = "phi_attr"
729         init          = '''
730         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);'''
731         init_after_opt = '''
732         /* Memory Phis in endless loops must be kept alive.
733            As we can't distinguish these easily we keep all of them alive. */
734         if (is_Phi(res) && mode == mode_M)
735                 add_End_keepalive(get_irg_end(irg), res);'''
736
737 class Pin(Op):
738         """Pin the value of the node node in the current block. No users of the Pin
739         node can float above the Block of the Pin. The node cannot float behind
740         this block. Often used to Pin the NoMem node."""
741         ins      = [
742                 ("op", "value which is pinned"),
743         ]
744         mode     = "get_irn_mode(irn_op)"
745         flags    = [ "highlevel" ]
746         pinned   = "yes"
747
748 class Proj(Op):
749         """returns an entry of a tuple value"""
750         ins              = [
751                 ("pred", "the tuple value from which a part is extracted"),
752         ]
753         flags            = []
754         pinned           = "no"
755         knownBlock       = True
756         knownGraph       = True
757         block            = "get_nodes_block(irn_pred)"
758         graph            = "get_irn_irg(irn_pred)"
759         customSerializer = True
760         attrs      = [
761                 dict(
762                         type    = "long",
763                         name    = "proj",
764                         comment = "number of tuple component to be extracted",
765                 ),
766         ]
767         attr_struct = "proj_attr"
768
769 class Quot(Op):
770         """returns the quotient of its 2 operands, floatingpoint version"""
771         ins   = [
772            ("mem",   "memory dependency"),
773            ("left",  "first operand"),
774            ("right", "second operand"),
775         ]
776         outs  = [
777                 ("M",         "memory result",                         "pn_Generic_M"),
778                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
779                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
780                 ("res",       "result of computation",                 "pn_Generic_other"),
781         ]
782         flags = [ "fragile", "uses_memory" ]
783         attrs_name = "divmod"
784         attrs = [
785                 dict(
786                         type    = "ir_mode*",
787                         name    = "resmode",
788                         comment = "mode of the result value",
789                 ),
790         ]
791         attr_struct = "divmod_attr"
792         pinned      = "exception"
793         op_index    = 1
794         arity_override = "oparity_binary"
795
796 class Raise(Op):
797         """Raises an exception. Unconditional change of control flow. Writes an
798         explicit Except variable to memory to pass it to the exception handler.
799         Must be lowered to a Call to a runtime check function."""
800         ins    = [
801                 ("mem",     "memory dependency"),
802                 ("exo_ptr", "pointer to exception object to be thrown"),
803         ]
804         outs  = [
805                 ("M", "memory result",                     "pn_Generic_M"),
806                 ("X", "control flow to exception handler", "pn_Generic_X_regular"),
807         ]
808         flags  = [ "highlevel", "cfopcode" ]
809         pinned = "yes"
810
811 class Return(Op):
812         """Returns from the current function. Takes memory and return values as
813         operands."""
814         ins      = [
815                 ("mem", "memory dependency"),
816         ]
817         arity    = "variable"
818         mode     = "mode_X"
819         flags    = [ "cfopcode" ]
820         pinned   = "yes"
821
822 class Rotl(Binop):
823         """Returns its first operand bits rotated left by the amount in the 2nd
824         operand"""
825         flags    = []
826
827 class Sel(Op):
828         """Computes the address of a entity of a compound type given the base
829         address of an instance of the compound type."""
830         ins    = [
831                 ("mem", "memory dependency"),
832                 ("ptr", "pointer to object to select from"),
833         ]
834         arity  = "variable"
835         flags  = []
836         mode   = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
837         pinned = "no"
838         attrs  = [
839                 dict(
840                         type    = "ir_entity*",
841                         name    = "entity",
842                         comment = "entity which is selected",
843                 )
844         ]
845         attr_struct = "sel_attr"
846
847 class Shl(Binop):
848         """Returns its first operands bits shifted left by the amount of the 2nd
849         operand"""
850         flags = []
851
852 class Shr(Binop):
853         """Returns its first operands bits shifted right by the amount of the 2nd
854         operand. No special handling for the sign bit (zero extension)"""
855         flags = []
856
857 class Shrs(Binop):
858         """Returns its first operands bits shifted right by the amount of the 2nd
859         operand. The leftmost bit (usually the sign bit) stays the same
860         (sign extension)"""
861         flags = []
862
863 class Start(Op):
864         """The first node of a graph. Execution starts with this node."""
865         outs       = [
866                 ("X_initial_exec", "control flow"),
867                 ("M",              "initial memory"),
868                 ("P_frame_base",   "frame base pointer"),
869                 ("P_tls",          "pointer to thread local storage segment"),
870                 ("T_args",         "function arguments")
871         ]
872         mode             = "mode_T"
873         pinned           = "yes"
874         flags            = [ "cfopcode" ]
875         singleton        = True
876         knownBlock       = True
877         customSerializer = True
878         block            = "get_irg_start_block(irg)"
879
880 class Store(Op):
881         """Stores a value into memory (heap or stack)."""
882         ins   = [
883            ("mem",   "memory dependency"),
884            ("ptr",   "address to store to"),
885            ("value", "value to store"),
886         ]
887         outs  = [
888                 ("M",         "memory result",                         "pn_Generic_M"),
889                 ("X_regular", "control flow when no exception occurs", "pn_Generic_X_regular"),
890                 ("X_except",  "control flow when exception occured",   "pn_Generic_X_except"),
891         ]
892         flags    = [ "fragile", "uses_memory" ]
893         pinned   = "exception"
894         attr_struct = "store_attr"
895         pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
896         constructor_args = [
897                 dict(
898                         type    = "ir_cons_flags",
899                         name    = "flags",
900                         comment = "specifies alignment, volatility and pin state",
901                 ),
902         ]
903         init = '''
904         res->attr.store.volatility = flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile;
905         res->attr.store.aligned = flags & cons_unaligned ? align_non_aligned : align_is_aligned;
906         '''
907
908 class Sub(Binop):
909         """returns the difference of its operands"""
910         flags = []
911
912 class SymConst(Op):
913         """A symbolic constant.
914
915          - symconst_type_tag   The symbolic constant represents a type tag.  The
916                                type the tag stands for is given explicitly.
917          - symconst_type_size  The symbolic constant represents the size of a type.
918                                The type of which the constant represents the size
919                                is given explicitly.
920          - symconst_type_align The symbolic constant represents the alignment of a
921                                type.  The type of which the constant represents the
922                                size is given explicitly.
923          - symconst_addr_ent   The symbolic constant represents the address of an
924                                entity (variable or method).  The variable is given
925                                explicitly by a firm entity.
926          - symconst_ofs_ent    The symbolic constant represents the offset of an
927                                entity in its owner type.
928          - symconst_enum_const The symbolic constant is a enumeration constant of
929                                an enumeration type."""
930         mode       = "mode_P"
931         flags      = [ "constlike", "start_block" ]
932         knownBlock = True
933         pinned     = "no"
934         attrs      = [
935                 dict(
936                         type    = "ir_entity*",
937                         name    = "entity",
938                         noprop  = True,
939                         comment = "entity whose address is returned",
940                 )
941         ]
942         attr_struct = "symconst_attr"
943         customSerializer = True
944         # constructor is written manually at the moment, because of the strange
945         # union argument
946         noconstructor = True
947
948 class Sync(Op):
949         """The Sync operation unifies several partial memory blocks. These blocks
950         have to be pairwise disjunct or the values in common locations have to
951         be identical.  This operation allows to specify all operations that
952         eventually need several partial memory blocks as input with a single
953         entrance by unifying the memories with a preceding Sync operation."""
954         mode     = "mode_M"
955         flags    = []
956         pinned   = "no"
957         arity    = "dynamic"
958
959 class Tuple(Op):
960         """Builds a Tuple from single values.
961
962         This is needed to implement optimizations that remove a node that produced
963         a tuple.  The node can be replaced by the Tuple operation so that the
964         following Proj nodes have not to be changed. (They are hard to find due to
965         the implementation with pointers in only one direction.) The Tuple node is
966         smaller than any other node, so that a node can be changed into a Tuple by
967         just changing it's opcode and giving it a new in array."""
968         arity  = "variable"
969         mode   = "mode_T"
970         pinned = "no"
971         flags  = [ "labeled" ]
972
973 class Unknown(Op):
974         """Returns an unknown (at compile- and runtime) value. It is a valid
975         optimisation to replace an Unknown by any other constant value."""
976         knownBlock = True
977         pinned     = "yes"
978         block      = "get_irg_start_block(irg)"
979         flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
980
981 # Prepare node list
982
983 def getOpList(namespace):
984         nodes = []
985         for t in namespace.values():
986                 if type(t) != type:
987                         continue
988
989                 if issubclass(t, Op):
990                         setnodedefaults(t)
991                         nodes.append(t)
992         return nodes
993
994 nodes = getOpList(globals())
995 nodes = sorted(nodes, lambda x,y: cmp(x.name, y.name))