b5e5c702a34694ead2d7a08bd5f88c777502308c
[libfirm] / ir / ir / irop_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irop_t.h
4  * Purpose:     Representation of opcode of intermediate operation -- private header.
5  * Author:      Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 # ifndef _IROP_T_H_
15 # define _IROP_T_H_
16
17 # include "irop.h"
18 # include "tv.h"
19
20 /** The allowed parities */
21 typedef enum {
22   oparity_invalid = 0,
23   oparity_unary,              /**< an unary operator -- considering 'numeric' arguments. */
24   oparity_binary,             /**< an binary operator  -- considering 'numeric' arguments.*/
25   oparity_trinary,            /**< an trinary operator  -- considering 'numeric' arguments.*/
26   oparity_zero,               /**< no operators, as e.g. Const. */
27   oparity_variable,           /**< arity not fixed by opcode, but statically
28                                  known.  E.g., number of arguments to call. */
29   oparity_dynamic,            /**< arity depends on state of firm representation.
30                                  Can change by optimizations...
31                                  We must allocate a dynamic in array for the node! */
32   oparity_any,                /**< other arity */
33 } op_arity;
34
35
36 /** The irop flags */
37 typedef enum {
38   irop_flag_labeled     = 0x00000001,   /**< if set, Output edge labels on in-edges in vcg graph */
39   irop_flag_commutative = 0x00000002,   /**< operation is commutative */
40   irop_flag_cfopcode    = 0x00000004,   /**< is a control flow operation */
41   irop_flag_ip_cfopcode = 0x00000008,   /**< operation manipulates interprocedural control flow */
42   irop_flag_fragile     = 0x00000010,   /**< set if the operation can change the control flow because
43                                              of an exception */
44   irop_flag_forking     = 0x00000020,   /**< the operation is a forking control flow */
45 } irop_flags;
46
47
48 /**
49  * The compute value operation.
50  * This operation evaluates an IR node into a tarval if possible,
51  * returning tarval_bad otherwise.
52  */
53 typedef tarval *(*computed_value_func)(ir_node *n);
54
55 /**
56  * The equivalent node operation.
57  * This operation returns an equivalent node for the input node.
58  * It does not create new nodes.  It is therefore safe to free n
59  * if the node returned is not n.
60  * If a node returns a Tuple we can not just skip it.  If the size of the
61  * in array fits, we transform n into a tuple (e.g., possible for Div).
62  */
63 typedef ir_node *(*equivalent_node_func)(ir_node *n);
64
65 /**
66  * The transform node operation.
67  * This operation tries several [inplace] [optimizing] transformations
68  * and returns an equivalent node.
69  * The difference to equivalent_node() is that these
70  * transformations _do_ generate new nodes, and thus the old node must
71  * not be freed even if the equivalent node isn't the old one.
72  */
73 typedef ir_node *(*transform_node_func)(ir_node *n);
74
75 /**
76  * The node attribute compare operation.
77  * Compares the nodes attributes of two nodes of identical opcode
78  * and returns 0 if the attributes are identical, 1 if they differ.
79  */
80 typedef int (*node_cmp_attr_func)(ir_node *a, ir_node *b);
81
82 /** The type of an ir_op. */
83 struct ir_op {
84   opcode code;            /**< the unique opcode of the op */
85   ident *name;            /**< the name of the op */
86   size_t attr_size;       /**< Space needed in memory for private attributes */
87   op_pin_state op_pin_state_pinned; /**< How to deal with the node in cse, pre. */
88   op_arity opar;          /**< arity of operator. */
89   int op_index;           /**< the index of the first data operand, 0 for most cases, 1 for Div etc. */
90   unsigned flags;         /**< flags describing the behavior of the ir_op, a bitmaks of irop_flags */
91
92   /* CallBacks */
93   computed_value_func   computed_value;         /**< evaluates a node into a tarval if possible. */
94   equivalent_node_func  equivalent_node;        /**< optimizes the node by returning an equivalent one. */
95   transform_node_func   transform_node;         /**< optimizes the node by transforming it. */
96   node_cmp_attr_func    node_cmp_attr;          /**< compares two node attributes. */
97 };
98
99 /**
100  * Creates a new ir operation.
101  *
102  * @param code      the opcode, one of type \c opcode
103  * @param name      the printable name of this opcode
104  * @param p         wheater operations of this opcode are op_pin_state_pinned or floating
105  * @param flags     a bitmask of irop_flags describing the behavior of the ir operation
106  * @param opar      the parity of this ir operation
107  * @param op_index  if the parity is oparity_unary, oparity_binary or oparity_trinary the index
108  *                  of the left operand
109  * @param attr_size attribute size for this ir operation
110  *
111  * @return The genenerated ir operation.
112  */
113 ir_op * new_ir_op(opcode code, const char *name, op_pin_state p,
114                    unsigned flags, op_arity opar, int op_index, size_t attr_size);
115
116 /**
117  * Frees a newly created ir operation.
118  */
119 void free_ir_op(ir_op *code);
120
121 /** Initialize the irop module. */
122 void init_op(void);
123
124 /** Free memory used by irop module. */
125 void finish_op(void);
126
127 /** Returns the attribute size of nodes of this opcode.
128    @note Use not encouraged, internal feature. */
129 static INLINE int get_op_attr_size (const ir_op *op) {
130   return op->attr_size;
131 }
132
133 /** Returns non-zero if op is one of Start, End, Jmp, Cond, Return, Raise or Bad. */
134 static INLINE int is_cfopcode(const ir_op *op) {
135   return op->flags & irop_flag_cfopcode;
136 }
137
138 /** Returns true if the operation manipulates interprocedural control flow:
139    CallBegin, EndReg, EndExcept */
140 static INLINE int is_ip_cfopcode(const ir_op *op) {
141   return op->flags & irop_flag_ip_cfopcode;
142 }
143
144 /* Returns non-zero if operation is commutative */
145 static INLINE int is_op_commutative(const ir_op *op) {
146   return op->flags & irop_flag_commutative;
147 }
148
149 /* Returns non-zero if operation is fragile */
150 static INLINE int is_op_fragile(const ir_op *op) {
151   return op->flags & irop_flag_fragile;
152 }
153
154 /* Returns non-zero if operation is forking control flow */
155 static INLINE int is_op_forking(const ir_op *op) {
156   return op->flags & irop_flag_forking;
157 }
158
159 static INLINE opcode __get_op_code(const ir_op *op) {
160   return op->code;
161 }
162
163 static INLINE ident *__get_op_ident(ir_op *op){
164   return op->name;
165 }
166
167 static INLINE op_pin_state __get_op_pinned(const ir_op *op) {
168   return op->op_pin_state_pinned;
169 }
170
171
172 #define get_op_code(op)         __get_op_code(op)
173 #define get_op_ident(op)        __get_op_ident(op)
174 #define get_op_pinned(op)       __get_op_pinned(op)
175
176
177 #endif /* _IROP_T_H_ */