X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=scripts%2Fir_spec.py;h=07a995a1b815af9002ad8886a2c65a484120a1e3;hb=9af128bc5ad9480ecc5c78fdeebe3a2776126d2a;hp=ea4a1f3492b37d80cd5fca7ab48071ad06afe404;hpb=5025bfbdddadddabb850763898ad508935eebe43;p=libfirm diff --git a/scripts/ir_spec.py b/scripts/ir_spec.py index ea4a1f349..07a995a1b 100755 --- a/scripts/ir_spec.py +++ b/scripts/ir_spec.py @@ -1,10 +1,13 @@ -from spec_util import abstract, setnodedefaults +# Firm node specifications +# The comments are in (standard python) restructured text format and are used +# to generate documentation. +from spec_util import abstract, op -class Op(object): - """Base class for firm nodes""" -abstract(Op) +name = "ir" -class Unop(Op): +@abstract +@op +class Unop(object): """Unary nodes have exactly 1 input""" name = "unop" ins = [ @@ -12,9 +15,10 @@ class Unop(Op): ] op_index = 0 pinned = "no" -abstract(Unop) -class Binop(Op): +@abstract +@op +class Binop(object): """Binary nodes have exactly 2 inputs""" name = "binop" ins = [ @@ -23,13 +27,14 @@ class Binop(Op): ] op_index = 0 pinned = "no" -abstract(Binop) +@op class Add(Binop): """returns the sum of its operands""" flags = [ "commutative" ] -class Alloc(Op): +@op +class Alloc: """allocates a block of memory. It can be specified whether the memory should be allocated to the stack or to the heap. @@ -63,7 +68,8 @@ class Alloc(Op): pinned_init = "op_pin_state_pinned" attr_struct = "alloc_attr" -class Anchor(Op): +@op +class Anchor: """utiliy node used to "hold" nodes in a graph that might possibly not be reachable by other means or which should be reachable immediately without searching through the graph. @@ -80,12 +86,40 @@ class Anchor(Op): noconstructor = True customSerializer = True +@op class And(Binop): """returns the result of a bitwise and operation of its operands""" flags = [ "commutative" ] -class ASM(Op): - """executes assembler fragments of the target machine""" +@op +class ASM: + """executes assembler fragments of the target machine. + + The node contains a template for an assembler snippet. The compiler will + replace occurences of %0 to %9 with input/output registers, + %% with a single % char. Some backends allow additional specifiers (for + example %w3, %l3, %h3 on x86 to get a 16bit, 8hit low, 8bit high part + of a register). + After the replacements the text is emitted into the final assembly. + + The clobber list contains names of registers which have an undefined value + after the assembler instruction is executed; it may also contain 'memory' + or 'cc' if global state/memory changes or the condition code registers + (some backends implicitely set cc, memory clobbers on all ASM statements). + + Example (an i386 instruction):: + + ASM(text="btsl %1, %0", + input_constraints = ["=m", "r"], + clobbers = ["cc"]) + + As there are no output, the %0 references the first input which is just an + address which the asm operation writes to. %1 references to an input which + is passed as a register. The condition code register has an unknown value + after the instruction. + + (This format is inspired by the gcc extended asm syntax) + """ mode = "mode_T" arity = "variable" flags = [ "keep", "uses_memory" ] @@ -135,7 +169,8 @@ class ASM(Op): # constraints arrays needing special handling (2 arguments for 1 attribute) noconstructor = True -class Bad(Op): +@op +class Bad: """Bad nodes indicate invalid input, which is values which should never be computed. @@ -146,7 +181,7 @@ class Bad(Op): that a control flow edge can never be executed. The gigo optimisations ensures that nodes with Bad as their block, get - replaced by Bad themselfes. Nodes with at least 1 Bad input get exchanged + replaced by Bad themselves. Nodes with at least 1 Bad input get exchanged with Bad too. Exception to this rule are Block, Phi, Tuple and End node; This is because removing inputs from a Block is hairy operation (requiring, Phis to be shortened too for example). So instead of removing block inputs @@ -162,7 +197,8 @@ class Bad(Op): res->attr.bad.irg.irg = irg; ''' -class Deleted(Op): +@op +class Deleted: """Internal node which is temporary set to nodes which are already removed from the graph.""" mode = "mode_Bad" @@ -171,7 +207,8 @@ class Deleted(Op): noconstructor = True customSerializer = True # this has no serializer -class Block(Op): +@op +class Block: """A basic block""" mode = "mode_BB" knownBlock = True @@ -192,21 +229,22 @@ class Block(Op): init = ''' res->attr.block.irg.irg = irg; - res->attr.block.backedge = new_backedge_arr(irg->obst, arity); + res->attr.block.backedge = new_backedge_arr(get_irg_obstack(irg), arity); set_Block_matured(res, 1); /* Create and initialize array for Phi-node construction. */ - if (get_irg_phase_state(irg) == phase_building) { - res->attr.block.graph_arr = NEW_ARR_D(ir_node *, irg->obst, irg->n_loc); - memset(res->attr.block.graph_arr, 0, irg->n_loc * sizeof(ir_node*)); + if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION)) { + res->attr.block.graph_arr = NEW_ARR_DZ(ir_node*, get_irg_obstack(irg), irg->n_loc); } ''' +@op class Borrow(Binop): """Returns the borrow bit from and implied subtractions of its 2 operands""" flags = [] -class Bound(Op): +@op +class Bound: """Performs a bounds-check: if lower <= index < upper then return index, otherwise throw an exception.""" ins = [ @@ -227,7 +265,8 @@ class Bound(Op): throws_init = "false" attr_struct = "bound_attr" -class Builtin(Op): +@op +class Builtin: """performs a backend-specific builtin.""" ins = [ ("mem", "memory dependency"), @@ -257,7 +296,8 @@ class Builtin(Op): assert((get_unknown_type() == type) || is_Method_type(type)); ''' -class Call(Op): +@op +class Call: """Calls other code. Control flow is transfered to ptr, additional operands are passed to the called code. Called code usually performs a return operation. The operands of this return operation are the result @@ -289,11 +329,13 @@ class Call(Op): assert((get_unknown_type() == type) || is_Method_type(type)); ''' +@op class Carry(Binop): """Computes the value of the carry-bit that would result when adding the 2 operands""" flags = [ "commutative" ] +@op class Cast(Unop): """perform a high-level type cast""" mode = "get_irn_mode(irn_op)" @@ -308,6 +350,7 @@ class Cast(Unop): attr_struct = "cast_attr" init = "assert(is_atomic_type(type));" +@op class Cmp(Binop): """Compares its two operands and checks whether a specified relation (like less or equal) is fulfilled.""" @@ -322,7 +365,8 @@ class Cmp(Binop): ] attr_struct = "cmp_attr" -class Cond(Op): +@op +class Cond: """Conditionally change control flow.""" ins = [ ("selector", "condition parameter"), @@ -343,7 +387,8 @@ class Cond(Op): ] attr_struct = "cond_attr" -class Switch(Op): +@op +class Switch: """Change control flow. The destination is choosen based on an integer input value which is looked up in a table. Backends can implement this efficiently using a jump table.""" @@ -370,7 +415,8 @@ class Switch(Op): attr_struct = "switch_attr" attrs_name = "switcha" -class Confirm(Op): +@op +class Confirm: """Specifies constraints for a value. This allows explicit representation of path-sensitive properties. (Example: This value is always >= 0 on 1 if-branch then all users within that branch are rerouted to a confirm-node @@ -396,7 +442,8 @@ class Confirm(Op): ] attr_struct = "confirm_attr" -class Const(Op): +@op +class Const: """Returns a constant value.""" flags = [ "constlike", "start_block" ] block = "get_irg_start_block(irg)" @@ -413,20 +460,13 @@ class Const(Op): attr_struct = "const_attr" attrs_name = "con" +@op class Conv(Unop): """Converts values between modes""" flags = [] - attrs = [ - dict( - name = "strict", - type = "int", - init = "0", - comment = "force floating point to restrict precision even if backend computes in higher precision (deprecated)", - ) - ] - attr_struct = "conv_attr" -class CopyB(Op): +@op +class CopyB: """Copies a block of memory with statically known size/type.""" ins = [ ("mem", "memory dependency"), @@ -451,7 +491,8 @@ class CopyB(Op): pinned_init = "op_pin_state_pinned" throws_init = "false" -class Div(Op): +@op +class Div: """returns the quotient of its 2 operands""" ins = [ ("mem", "memory dependency"), @@ -483,7 +524,8 @@ class Div(Op): op_index = 1 arity_override = "oparity_binary" -class Dummy(Op): +@op +class Dummy: """A placeholder value. This is used when constructing cyclic graphs where you have cases where not all predecessors of a phi-node are known. Dummy nodes are used for the unknown predecessors and replaced later.""" @@ -493,7 +535,8 @@ class Dummy(Op): pinned = "yes" block = "get_irg_start_block(irg)" -class End(Op): +@op +class End: """Last node of a graph. It references nodes in endless loops (so called keepalive edges)""" mode = "mode_X" @@ -504,13 +547,15 @@ class End(Op): block = "get_irg_end_block(irg)" singleton = True +@op class Eor(Binop): """returns the result of a bitwise exclusive or operation of its operands. This is also known as the Xor operation.""" flags = [ "commutative" ] -class Free(Op): +@op +class Free: """Frees a block of memory previously allocated by an Alloc node""" ins = [ ("mem", "memory dependency" ), @@ -534,7 +579,8 @@ class Free(Op): ] attr_struct = "free_attr" -class Id(Op): +@op +class Id: """Returns its operand unchanged. This is mainly used when exchanging nodes. Usually you shouldn't see Id @@ -545,7 +591,8 @@ class Id(Op): pinned = "no" flags = [] -class IJmp(Op): +@op +class IJmp: """Jumps to the code in its argument. The code has to be in the same function and the the destination must be one of the blocks reachable by the tuple results""" @@ -556,7 +603,8 @@ class IJmp(Op): ] flags = [ "cfopcode", "forking", "keep", "unknown_jump" ] -class InstOf(Op): +@op +class InstOf: """Tests whether an object is an instance of a class-type""" ins = [ ("store", "memory dependency"), @@ -580,14 +628,16 @@ class InstOf(Op): pinned = "memory" pinned_init = "op_pin_state_floats" -class Jmp(Op): +@op +class Jmp: """Jumps to the block connected through the out-value""" mode = "mode_X" pinned = "yes" ins = [] flags = [ "cfopcode" ] -class Load(Op): +@op +class Load: """Loads a value from memory (heap or stack).""" ins = [ ("mem", "memory dependency"), @@ -612,14 +662,14 @@ class Load(Op): name = "volatility", comment = "volatile loads are a visible side-effect and may not be optimized", init = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile", - to_flags = "%s == volatility_is_volatile ? cons_volatile : 0" + to_flags = "%s == volatility_is_volatile ? cons_volatile : cons_none" ), dict( type = "ir_align", name = "unaligned", comment = "pointers to unaligned loads don't need to respect the load-mode/type alignments", init = "flags & cons_unaligned ? align_non_aligned : align_is_aligned", - to_flags = "%s == align_non_aligned ? cons_unaligned : 0" + to_flags = "%s == align_non_aligned ? cons_unaligned : cons_none" ), ] attr_struct = "load_attr" @@ -633,11 +683,13 @@ class Load(Op): pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned" throws_init = "(flags & cons_throws_exception) != 0" +@op class Minus(Unop): - """returns the difference between its operands""" + """returns the additive inverse of its operand""" flags = [] -class Mod(Op): +@op +class Mod: """returns the remainder of its operands from an implied division. Examples: @@ -681,7 +733,8 @@ class Mulh(Binop): would not fit into the result mode of a normal Mul anymore)""" flags = [ "commutative" ] -class Mux(Op): +@op +class Mux: """returns the false or true operand depending on the value of the sel operand""" ins = [ @@ -692,7 +745,8 @@ class Mux(Op): flags = [] pinned = "no" -class NoMem(Op): +@op +class NoMem: """Placeholder node for cases where you don't need any memory input""" mode = "mode_M" flags = [ "dump_noblock" ] @@ -701,15 +755,18 @@ class NoMem(Op): block = "get_irg_start_block(irg)" singleton = True +@op class Not(Unop): """returns the bitwise complement of a value. Works for boolean values, too.""" flags = [] +@op class Or(Binop): """returns the result of a bitwise or operation of its operands""" flags = [ "commutative" ] -class Phi(Op): +@op +class Phi: """Choose a value based on control flow. A phi node has 1 input for each predecessor of its block. If a block is entered from its nth predecessor all phi nodes produce their nth input as result.""" @@ -718,15 +775,11 @@ class Phi(Op): flags = [] attr_struct = "phi_attr" init = ''' - res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);''' - init_after_opt = ''' - /* Memory Phis in endless loops must be kept alive. - As we can't distinguish these easily we keep all of them alive. */ - if (is_Phi(res) && mode == mode_M) - add_End_keepalive(get_irg_end(irg), res);''' + res->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), arity);''' customSerializer = True -class Pin(Op): +@op +class Pin: """Pin the value of the node node in the current block. No users of the Pin node can float above the Block of the Pin. The node cannot float behind this block. Often used to Pin the NoMem node.""" @@ -737,7 +790,8 @@ class Pin(Op): flags = [ "highlevel" ] pinned = "yes" -class Proj(Op): +@op +class Proj: """returns an entry of a tuple value""" ins = [ ("pred", "the tuple value from which a part is extracted"), @@ -757,7 +811,8 @@ class Proj(Op): ] attr_struct = "proj_attr" -class Raise(Op): +@op +class Raise: """Raises an exception. Unconditional change of control flow. Writes an explicit Except variable to memory to pass it to the exception handler. Must be lowered to a Call to a runtime check function.""" @@ -772,7 +827,8 @@ class Raise(Op): flags = [ "highlevel", "cfopcode" ] pinned = "yes" -class Return(Op): +@op +class Return: """Returns from the current function. Takes memory and return values as operands.""" ins = [ @@ -788,7 +844,8 @@ class Rotl(Binop): operand""" flags = [] -class Sel(Op): +@op +class Sel: """Computes the address of a entity of a compound type given the base address of an instance of the compound type. @@ -811,6 +868,7 @@ class Sel(Op): ] attr_struct = "sel_attr" +@op class Shl(Binop): """Returns its first operands bits shifted left by the amount of the 2nd operand. @@ -819,6 +877,7 @@ class Shl(Binop): the right input modulo this modulo_shift amount.""" flags = [] +@op class Shr(Binop): """Returns its first operands bits shifted right by the amount of the 2nd operand. No special handling for the sign bit is performed (zero extension). @@ -827,6 +886,7 @@ class Shr(Binop): the right input modulo this modulo_shift amount.""" flags = [] +@op class Shrs(Binop): """Returns its first operands bits shifted right by the amount of the 2nd operand. The leftmost bit (usually the sign bit) stays the same @@ -836,7 +896,8 @@ class Shrs(Binop): the right input modulo this modulo_shift amount.""" flags = [] -class Start(Op): +@op +class Start: """The first node of a graph. Execution starts with this node.""" outs = [ ("X_initial_exec", "control flow"), @@ -851,7 +912,8 @@ class Start(Op): knownBlock = True block = "get_irg_start_block(irg)" -class Store(Op): +@op +class Store: """Stores a value into memory (heap or stack).""" ins = [ ("mem", "memory dependency"), @@ -874,14 +936,14 @@ class Store(Op): name = "volatility", comment = "volatile stores are a visible side-effect and may not be optimized", init = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile", - to_flags = "%s == volatility_is_volatile ? cons_volatile : 0" + to_flags = "%s == volatility_is_volatile ? cons_volatile : cons_none" ), dict( type = "ir_align", name = "unaligned", comment = "pointers to unaligned stores don't need to respect the load-mode/type alignments", init = "flags & cons_unaligned ? align_non_aligned : align_is_aligned", - to_flags = "%s == align_non_aligned ? cons_unaligned : 0" + to_flags = "%s == align_non_aligned ? cons_unaligned : cons_none" ), ] constructor_args = [ @@ -892,11 +954,13 @@ class Store(Op): ), ] +@op class Sub(Binop): """returns the difference of its operands""" flags = [] -class SymConst(Op): +@op +class SymConst: """A symbolic constant. - *symconst_type_size* The symbolic constant represents the size of a type. @@ -930,7 +994,8 @@ class SymConst(Op): # union argument noconstructor = True -class Sync(Op): +@op +class Sync: """The Sync operation unifies several partial memory blocks. These blocks have to be pairwise disjunct or the values in common locations have to be identical. This operation allows to specify all operations that @@ -941,7 +1006,8 @@ class Sync(Op): pinned = "no" arity = "dynamic" -class Tuple(Op): +@op +class Tuple: """Builds a Tuple from single values. This is needed to implement optimizations that remove a node that produced @@ -955,26 +1021,11 @@ class Tuple(Op): pinned = "no" flags = [] -class Unknown(Op): +@op +class Unknown: """Returns an unknown (at compile- and runtime) value. It is a valid optimisation to replace an Unknown by any other constant value.""" knownBlock = True pinned = "yes" block = "get_irg_start_block(irg)" flags = [ "start_block", "constlike", "dump_noblock" ] - -# Prepare node list - -def getOpList(namespace): - nodes = [] - for t in namespace.values(): - if type(t) != type: - continue - - if issubclass(t, Op): - setnodedefaults(t) - nodes.append(t) - return nodes - -nodes = getOpList(globals()) -nodes = sorted(nodes, lambda x,y: cmp(x.name, y.name))