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