1 # This file is part of libFirm.
2 # Copyright (C) 2012 Karlsruhe Institute of Technology.
4 # Firm node specifications
5 # The comments are in (standard python) restructured text format and are used
6 # to generate documentation.
7 from spec_util import abstract, op
14 """Unary nodes have exactly 1 input"""
25 """Binary nodes have exactly 2 inputs"""
28 ( "left", "first operand" ),
29 ( "right", "second operand" ),
36 """returns the sum of its operands"""
37 flags = [ "commutative" ]
41 """allocates a block of memory.
42 It can be specified whether the memory should be allocated to the stack
44 Allocates memory for one or more objects (depending on value on count input).
47 ("mem", "memory dependency" ),
48 ("count", "number of objects to allocate" ),
51 ("M", "memory result"),
52 ("res", "pointer to newly allocated memory"),
53 ("X_regular", "control flow when no exception occurs"),
54 ("X_except", "control flow when exception occured"),
60 comment = "type of the objects to allocate",
64 type = "ir_where_alloc",
65 comment = "whether to allocate the variable on the stack or heap",
68 flags = [ "fragile", "uses_memory" ]
71 pinned_init = "op_pin_state_pinned"
72 attr_struct = "alloc_attr"
76 """utiliy node used to "hold" nodes in a graph that might possibly not be
77 reachable by other means or which should be reachable immediately without
78 searching through the graph.
79 Each firm-graph contains exactly one anchor node whose address is always
80 known. All other well-known graph-nodes like Start, End, NoMem, Bad, ...
81 are found by looking at the respective Anchor operand."""
84 flags = [ "dump_noblock" ]
86 attr_struct = "irg_attr"
90 customSerializer = True
94 """returns the result of a bitwise and operation of its operands"""
95 flags = [ "commutative" ]
99 """executes assembler fragments of the target machine.
101 The node contains a template for an assembler snippet. The compiler will
102 replace occurences of %0 to %9 with input/output registers,
103 %% with a single % char. Some backends allow additional specifiers (for
104 example %w3, %l3, %h3 on x86 to get a 16bit, 8hit low, 8bit high part
106 After the replacements the text is emitted into the final assembly.
108 The clobber list contains names of registers which have an undefined value
109 after the assembler instruction is executed; it may also contain 'memory'
110 or 'cc' if global state/memory changes or the condition code registers
111 (some backends implicitely set cc, memory clobbers on all ASM statements).
113 Example (an i386 instruction)::
115 ASM(text="btsl %1, %0",
116 input_constraints = ["=m", "r"],
119 As there are no output, the %0 references the first input which is just an
120 address which the asm operation writes to. %1 references to an input which
121 is passed as a register. The condition code register has an unknown value
122 after the instruction.
124 (This format is inspired by the gcc extended asm syntax)
129 flags = [ "keep", "uses_memory" ]
131 pinned_init = "op_pin_state_pinned"
132 attr_struct = "asm_attr"
134 customSerializer = True
136 ("mem", "memory dependency"),
140 name = "input_constraints",
141 type = "ir_asm_constraint*",
142 comment = "input constraints",
145 name = "n_output_constraints",
148 comment = "number of output constraints",
151 name = "output_constraints",
152 type = "ir_asm_constraint*",
153 comment = "output constraints",
159 comment = "number of clobbered registers/memory",
164 comment = "list of clobbered registers/memory",
169 comment = "assembler text",
172 # constructor is written manually at the moment, because of the clobbers+
173 # constraints arrays needing special handling (2 arguments for 1 attribute)
178 """Bad nodes indicate invalid input, which is values which should never be
181 The typical use case for the Bad node is removing unreachable code.
182 Frontends should set the current_block to Bad when it is clear that
183 following code must be unreachable (ie. after a goto or return statement).
184 Optimisations also set block predecessors to Bad when it becomes clear,
185 that a control flow edge can never be executed.
187 The gigo optimisations ensures that nodes with Bad as their block, get
188 replaced by Bad themselves. Nodes with at least 1 Bad input get exchanged
189 with Bad too. Exception to this rule are Block, Phi, Tuple and End node;
190 This is because removing inputs from a Block is hairy operation (requiring,
191 Phis to be shortened too for example). So instead of removing block inputs
192 they are set to Bad, and the actual removal is left to the control flow
193 optimisation phase. Block, Phi, Tuple with only Bad inputs however are
194 replaced by Bad right away."""
195 flags = [ "start_block", "dump_noblock" ]
198 block = "get_irg_start_block(irg)"
199 attr_struct = "bad_attr"
201 res->attr.bad.irg.irg = irg;
206 """Internal node which is temporary set to nodes which are already removed
212 customSerializer = True # this has no serializer
222 input_name = "cfgpred"
224 attr_struct = "block_attr"
229 comment = "entity representing this block",
233 customSerializer = True
236 res->attr.block.irg.irg = irg;
237 res->attr.block.backedge = new_backedge_arr(get_irg_obstack(irg), arity);
238 set_Block_matured(res, 1);
240 /* Create and initialize array for Phi-node construction. */
241 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION)) {
242 res->attr.block.graph_arr = NEW_ARR_DZ(ir_node*, get_irg_obstack(irg), irg->n_loc);
248 """performs a backend-specific builtin."""
250 ("mem", "memory dependency"),
255 ("M", "memory result"),
256 # results follow here
258 flags = [ "uses_memory" ]
261 type = "ir_builtin_kind",
263 comment = "kind of builtin",
268 comment = "method type for the builtin call",
272 pinned_init = "op_pin_state_pinned"
273 attr_struct = "builtin_attr"
275 assert((get_unknown_type() == type) || is_Method_type(type));
280 """Calls other code. Control flow is transfered to ptr, additional
281 operands are passed to the called code. Called code usually performs a
282 return operation. The operands of this return operation are the result
285 ("mem", "memory dependency"),
286 ("ptr", "pointer to called code"),
291 ("M", "memory result"),
292 ("T_result", "tuple containing all results"),
293 ("X_regular", "control flow when no exception occurs"),
294 ("X_except", "control flow when exception occured"),
296 flags = [ "fragile", "uses_memory" ]
301 comment = "type of the call (usually type of the called procedure)",
304 attr_struct = "call_attr"
306 pinned_init = "op_pin_state_pinned"
307 throws_init = "false"
309 assert((get_unknown_type() == type) || is_Method_type(type));
314 """Compares its two operands and checks whether a specified
315 relation (like less or equal) is fulfilled."""
320 type = "ir_relation",
322 comment = "Comparison relation"
325 attr_struct = "cmp_attr"
329 """Conditionally change control flow."""
331 ("selector", "condition parameter"),
334 ("false", "control flow if operand is \"false\""),
335 ("true", "control flow if operand is \"true\""),
337 flags = [ "cfopcode", "forking" ]
342 type = "cond_jmp_predicate",
343 init = "COND_JMP_PRED_NONE",
344 comment = "can indicate the most likely jump",
347 attr_struct = "cond_attr"
351 """Change control flow. The destination is choosen based on an integer input value which is looked up in a table.
353 Backends can implement this efficiently using a jump table."""
355 ("selector", "input selector"),
358 ("default", "control flow if no other case matches"),
360 flags = [ "cfopcode", "forking" ]
366 comment = "number of outputs (including pn_Switch_default)",
370 type = "ir_switch_table*",
371 comment = "table describing mapping from input values to Proj numbers",
374 attr_struct = "switch_attr"
375 attrs_name = "switcha"
379 """Specifies constraints for a value. This allows explicit representation
380 of path-sensitive properties. (Example: This value is always >= 0 on 1
381 if-branch then all users within that branch are rerouted to a confirm-node
382 specifying this property).
384 A constraint is specified for the relation between value and bound.
385 value is always returned.
386 Note that this node does NOT check or assert the constraint, it merely
389 ("value", "value to express a constraint for"),
390 ("bound", "value to compare against"),
392 mode = "get_irn_mode(irn_value)"
393 flags = [ "highlevel" ]
398 type = "ir_relation",
399 comment = "relation of value to bound",
402 attr_struct = "confirm_attr"
406 """Returns a constant value."""
407 flags = [ "constlike", "start_block" ]
408 block = "get_irg_start_block(irg)"
409 mode = "get_tarval_mode(tarval)"
416 comment = "constant value (a tarval object)",
419 attr_struct = "const_attr"
424 """Converts values between modes"""
429 """Copies a block of memory with statically known size/type."""
431 ("mem", "memory dependency"),
432 ("dst", "destination address"),
433 ("src", "source address"),
436 ("M", "memory result"),
437 ("X_regular", "control flow when no exception occurs"),
438 ("X_except", "control flow when exception occured"),
440 flags = [ "fragile", "uses_memory" ]
445 comment = "type of copied data",
448 attr_struct = "copyb_attr"
450 pinned_init = "op_pin_state_pinned"
451 throws_init = "false"
455 """returns the quotient of its 2 operands"""
457 ("mem", "memory dependency"),
458 ("left", "first operand"),
459 ("right", "second operand"),
462 ("M", "memory result"),
463 ("res", "result of computation"),
464 ("X_regular", "control flow when no exception occurs"),
465 ("X_except", "control flow when exception occured"),
467 flags = [ "fragile", "uses_memory" ]
472 comment = "mode of the result value",
475 name = "no_remainder",
480 attr_struct = "div_attr"
482 throws_init = "false"
484 arity_override = "oparity_binary"
488 """A placeholder value. This is used when constructing cyclic graphs where
489 you have cases where not all predecessors of a phi-node are known. Dummy
490 nodes are used for the unknown predecessors and replaced later."""
492 flags = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
495 block = "get_irg_start_block(irg)"
499 """Last node of a graph. It references nodes in endless loops (so called
504 input_name = "keepalive"
505 flags = [ "cfopcode" ]
507 block = "get_irg_end_block(irg)"
512 """returns the result of a bitwise exclusive or operation of its operands.
514 This is also known as the Xor operation."""
515 flags = [ "commutative" ]
519 """Frees a block of memory previously allocated by an Alloc node"""
521 ("mem", "memory dependency" ),
522 ("ptr", "pointer to the object to free"),
523 ("count", "number of objects to allocate" ),
526 flags = [ "uses_memory" ]
532 comment = "type of the allocated variable",
536 type = "ir_where_alloc",
537 comment = "whether allocation was on the stack or heap",
540 attr_struct = "free_attr"
544 """Returns its operand unchanged.
546 This is mainly used when exchanging nodes. Usually you shouldn't see Id
547 nodes since the getters/setters for node inputs skip them automatically."""
549 ("pred", "the value which is returned unchanged")
556 """Jumps to the code in its argument. The code has to be in the same
557 function and the the destination must be one of the blocks reachable
558 by the tuple results"""
562 ("target", "target address of the jump"),
564 flags = [ "cfopcode", "forking", "keep", "unknown_jump" ]
568 """Tests whether an object is an instance of a class-type"""
570 ("store", "memory dependency"),
571 ("obj", "pointer to object being queried")
574 ("M", "memory result"),
575 ("res", "checked object pointer"),
576 ("X_regular", "control flow when no exception occurs"),
577 ("X_except", "control flow when exception occured"),
579 flags = [ "highlevel" ]
584 comment = "type to check ptr for",
587 attr_struct = "io_attr"
589 pinned_init = "op_pin_state_floats"
593 """Jumps to the block connected through the out-value"""
597 flags = [ "cfopcode" ]
601 """Loads a value from memory (heap or stack)."""
603 ("mem", "memory dependency"),
604 ("ptr", "address to load from"),
607 ("M", "memory result"),
608 ("res", "result of load operation"),
609 ("X_regular", "control flow when no exception occurs"),
610 ("X_except", "control flow when exception occured"),
612 flags = [ "fragile", "uses_memory" ]
618 comment = "mode of the value to be loaded",
621 type = "ir_volatility",
623 comment = "volatile loads are a visible side-effect and may not be optimized",
624 init = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
625 to_flags = "%s == volatility_is_volatile ? cons_volatile : cons_none"
630 comment = "pointers to unaligned loads don't need to respect the load-mode/type alignments",
631 init = "flags & cons_unaligned ? align_non_aligned : align_is_aligned",
632 to_flags = "%s == align_non_aligned ? cons_unaligned : cons_none"
635 attr_struct = "load_attr"
638 type = "ir_cons_flags",
640 comment = "specifies alignment, volatility and pin state",
643 pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
644 throws_init = "(flags & cons_throws_exception) != 0"
648 """returns the additive inverse of its operand"""
653 """returns the remainder of its operands from an implied division.
657 * mod(5,3) produces 2
658 * mod(5,-3) produces 2
659 * mod(-5,3) produces -2
660 * mod(-5,-3) produces -2
663 ("mem", "memory dependency"),
664 ("left", "first operand"),
665 ("right", "second operand"),
668 ("M", "memory result"),
669 ("res", "result of computation"),
670 ("X_regular", "control flow when no exception occurs"),
671 ("X_except", "control flow when exception occured"),
673 flags = [ "fragile", "uses_memory" ]
678 comment = "mode of the result",
681 attr_struct = "mod_attr"
683 throws_init = "false"
685 arity_override = "oparity_binary"
688 """returns the product of its operands"""
689 flags = [ "commutative" ]
692 """returns the upper word of the product of its operands (the part which
693 would not fit into the result mode of a normal Mul anymore)"""
694 flags = [ "commutative" ]
698 """returns the false or true operand depending on the value of the sel
701 ("sel", "value making the output selection"),
702 ("false", "selected if sel input is false"),
703 ("true", "selected if sel input is true"),
710 """Placeholder node for cases where you don't need any memory input"""
712 flags = [ "dump_noblock" ]
715 block = "get_irg_start_block(irg)"
720 """returns the bitwise complement of a value. Works for boolean values, too."""
725 """returns the result of a bitwise or operation of its operands"""
726 flags = [ "commutative" ]
730 """Choose a value based on control flow. A phi node has 1 input for each
731 predecessor of its block. If a block is entered from its nth predecessor
732 all phi nodes produce their nth input as result."""
737 attr_struct = "phi_attr"
739 res->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), arity);'''
740 customSerializer = True
744 """Pin the value of the node node in the current block. No users of the Pin
745 node can float above the Block of the Pin. The node cannot float behind
746 this block. Often used to Pin the NoMem node."""
748 ("op", "value which is pinned"),
750 mode = "get_irn_mode(irn_op)"
751 flags = [ "highlevel" ]
756 """returns an entry of a tuple value"""
758 ("pred", "the tuple value from which a part is extracted"),
764 block = "get_nodes_block(irn_pred)"
765 graph = "get_irn_irg(irn_pred)"
770 comment = "number of tuple component to be extracted",
773 attr_struct = "proj_attr"
777 """Raises an exception. Unconditional change of control flow. Writes an
778 explicit Except variable to memory to pass it to the exception handler.
779 Must be lowered to a Call to a runtime check function."""
781 ("mem", "memory dependency"),
782 ("exo_ptr", "pointer to exception object to be thrown"),
785 ("M", "memory result"),
786 ("X", "control flow to exception handler"),
788 flags = [ "highlevel", "cfopcode" ]
793 """Returns from the current function. Takes memory and return values as
796 ("mem", "memory dependency"),
801 flags = [ "cfopcode" ]
805 """Returns its first operand bits rotated left by the amount in the 2nd
811 """Computes the address of a entity of a compound type given the base
812 address of an instance of the compound type.
814 Optimisations assume that a Sel node can only produce a NULL pointer if the
815 ptr input was NULL."""
817 ("mem", "memory dependency"),
818 ("ptr", "pointer to object to select from"),
823 mode = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
829 comment = "entity which is selected",
832 attr_struct = "sel_attr"
836 """Returns its first operands bits shifted left by the amount of the 2nd
838 The right input (shift amount) must be an unsigned integer value.
839 If the result mode has modulo_shift!=0, then the effective shift amount is
840 the right input modulo this modulo_shift amount."""
845 """Returns its first operands bits shifted right by the amount of the 2nd
846 operand. No special handling for the sign bit is performed (zero extension).
847 The right input (shift amount) must be an unsigned integer value.
848 If the result mode has modulo_shift!=0, then the effective shift amount is
849 the right input modulo this modulo_shift amount."""
854 """Returns its first operands bits shifted right by the amount of the 2nd
855 operand. The leftmost bit (usually the sign bit) stays the same
857 The right input (shift amount) must be an unsigned integer value.
858 If the result mode has modulo_shift!=0, then the effective shift amount is
859 the right input modulo this modulo_shift amount."""
864 """The first node of a graph. Execution starts with this node."""
866 ("X_initial_exec", "control flow"),
867 ("M", "initial memory"),
868 ("P_frame_base", "frame base pointer"),
869 ("T_args", "function arguments")
873 flags = [ "cfopcode" ]
876 block = "get_irg_start_block(irg)"
880 """Stores a value into memory (heap or stack)."""
882 ("mem", "memory dependency"),
883 ("ptr", "address to store to"),
884 ("value", "value to store"),
887 ("M", "memory result"),
888 ("X_regular", "control flow when no exception occurs"),
889 ("X_except", "control flow when exception occured"),
891 flags = [ "fragile", "uses_memory" ]
893 attr_struct = "store_attr"
894 pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
895 throws_init = "(flags & cons_throws_exception) != 0"
898 type = "ir_volatility",
900 comment = "volatile stores are a visible side-effect and may not be optimized",
901 init = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
902 to_flags = "%s == volatility_is_volatile ? cons_volatile : cons_none"
907 comment = "pointers to unaligned stores don't need to respect the load-mode/type alignments",
908 init = "flags & cons_unaligned ? align_non_aligned : align_is_aligned",
909 to_flags = "%s == align_non_aligned ? cons_unaligned : cons_none"
914 type = "ir_cons_flags",
916 comment = "specifies alignment, volatility and pin state",
922 """returns the difference of its operands"""
927 """A symbolic constant.
929 - *symconst_type_size* The symbolic constant represents the size of a type.
930 The type of which the constant represents the size
932 - *symconst_type_align* The symbolic constant represents the alignment of a
933 type. The type of which the constant represents the
934 size is given explicitly.
935 - *symconst_addr_ent* The symbolic constant represents the address of an
936 entity (variable or method). The variable is given
937 explicitly by a firm entity.
938 - *symconst_ofs_ent* The symbolic constant represents the offset of an
939 entity in its owner type.
940 - *symconst_enum_const* The symbolic constant is a enumeration constant of
941 an enumeration type."""
943 flags = [ "constlike", "start_block" ]
951 comment = "entity whose address is returned",
954 attr_struct = "symconst_attr"
955 customSerializer = True
956 # constructor is written manually at the moment, because of the strange
962 """The Sync operation unifies several partial memory blocks. These blocks
963 have to be pairwise disjunct or the values in common locations have to
964 be identical. This operation allows to specify all operations that
965 eventually need several partial memory blocks as input with a single
966 entrance by unifying the memories with a preceding Sync operation."""
975 """Builds a Tuple from single values.
977 This is needed to implement optimizations that remove a node that produced
978 a tuple. The node can be replaced by the Tuple operation so that the
979 following Proj nodes have not to be changed. (They are hard to find due to
980 the implementation with pointers in only one direction.) The Tuple node is
981 smaller than any other node, so that a node can be changed into a Tuple by
982 just changing its opcode and giving it a new in array."""
991 """Returns an unknown (at compile- and runtime) value. It is a valid
992 optimisation to replace an Unknown by any other constant value."""
995 block = "get_irg_start_block(irg)"
996 flags = [ "start_block", "constlike", "dump_noblock" ]