Reset link field to NULL on construction
[libfirm] / ir / ir / irmode.h
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2  * All rights reserved.
3  */
4 /* $Id$ */
5
6 /**
7  * @file irmode.h
8  *    irmode -- Modes for ir operators
9  *
10  * @author Christian Schaefer, Matthias Heil
11  *
12  * This module specifies the modes that type the firm nodes.  It defines
13  * a datasturcture that describes a mode and implements constructors and
14  * access routines to this datastructure. Further it defines a set of
15  * predefined modes.
16  *
17  * SEE ALSO:
18  *    UKA tech report 1999-44 for more information about modes.
19  *
20  */
21 #ifndef _IRMODE_H_
22 #define _IRMODE_H_
23
24 #include "ident.h"
25
26 #ifndef _TARVAL_TYPEDEF_
27 #define _TARVAL_TYPEDEF_
28   typedef struct tarval tarval;
29 #endif
30
31 /**
32  * Contains relevant information about a mode.
33  *
34  * Neccessary information about a mode is stored in this struct
35  * which is used by the tarval module to perform calculations
36  * and comparisons of values of a such described mode.
37  *
38  * ATTRIBUTES:
39  *  -  modecode code:           An unambigous int (enum) for the mode
40  *  -  ident *name:             Name of this mode. Two modes are different if the name is different.
41  *  -  mode_sort sort:          sort of mode specifying possible usage kategories
42  *  -  int    size:             size of the mode in Bits.
43  *  -  int    align:            byte alignment
44  *  -  unsigned sign:1:         signedness of this mode
45  *  -  ... more to come
46  *
47  * SEE ALSO:
48  *    The tech report 1999-44 describing FIRM and predefined modes
49  *    tarval.h
50  */
51 typedef struct ir_mode ir_mode;
52
53 /* ********** Predefined modes ********** */
54
55 /**
56  * Predefined mode according to tech report 1999-14.
57  */
58 typedef enum { /* irm is short for `ir mode' */
59   irm_BB,                       /**< basic block */
60   irm_X,                        /**< execution */
61   irm_F,                        /**< float(32) */
62   irm_D,                        /**< double(64) */
63   irm_E,                        /**< extended(80) */
64   irm_Bs,                       /**< signed byte(8) */
65   irm_Bu,                       /**< unsigned byte(8) */
66   irm_Hs,                       /**< signed short(16) */
67   irm_Hu,                       /**< unsigned short(16) */
68   irm_Is,                       /**< signed int(32) */
69   irm_Iu,                       /**< unsigned int(32) */
70   irm_Ls,                       /**< signed long(64) */
71   irm_Lu,                       /**< unsigned long(64) */
72   irm_C,                        /**< character */
73   irm_P,                        /**< pointer */
74   irm_b,                        /**< internal boolean */
75   irm_M,                        /**< memory */
76   irm_T,                        /**< tuple */
77   irm_U,                        /**< unicode character */
78   irm_ANY,                      /**< undefined mode */
79   irm_BAD,                      /**< bad mode */
80   irm_max                       /**< maximum value for modecode */
81 } modecode;
82
83 /** These values represent the different mode classes of value representations.
84  */
85 typedef enum {
86   /* Predefined sorts of modes */
87   irms_auxiliary,         /**< Only for Firm use. Not extensible. (irm_T) */
88   irms_control_flow,       /**< Marks all control flow modes. Not extensible. (irm_BB, irm_X) */
89   irms_memory,             /**< Marks the memory mode.  Not extensible. (irm_M) */
90   irms_internal_boolean,  /**< Internal boolean representation.
91                                Storing to memory impossible, convert first. (irm_b) */
92                             /** user-extensible sorts of modes **/
93   irms_int_number,        /**< A mode to represent int numbers.
94                                Integer computations can be performed. */
95   irms_float_number,      /**< A mode to represent float numbers.
96                                Floating point computations can be performed. */
97   irms_reference,         /**< A mode to represent entities.
98                                Restricted int computations can be performed */
99   irms_character          /**< A mode to represent characters/symbols
100                                ?? Are computations allowed? as int?? */
101 } mode_sort;
102
103 /** These values represent the different arithmetic operations possible with a mode.
104     Further arithmetics can be defined, e.g., for @@@ modes.
105  */
106 typedef enum {
107   irma_uninitialized = 0,
108   irma_none = 1,              /**< For modes for which no representation is specified.
109                                    These are modes of sort auxiliary, internal_boolean and
110                                    character. */
111   irma_twos_complement = 2,   /**< Values of the mode are represented as two's complement.
112                                    Only legal for modes of sort int_number and reference. */
113   irma_ones_complement,       /**< Values of the mode are represented  as one's complement.
114                                    Only legal for modes of sort int_number and reference. */
115   irma_int_BCD,               /**< Values of the mode are represented as binary coded decimals.
116                                    Only legal for modes of sort int_number and reference. */
117   irma_ieee754 = 256,         /**< Values of the mode are represented according to ieee754
118                                    floatingpoint standard.  Only legal for modes of sort float_number. */
119   irma_float_BCD,             /**< Values of the mode are represented  as binary coded decimals
120                                    according to @@@ which standars??? Only legal for modes of
121                                    sort float_number. */
122   irma_max
123 } mode_arithmetic;
124
125
126 /* ********** Constructor for user defined modes **************** */
127 /**
128  * Creates a new mode.
129  *
130  * @param name          the name of the mode to be created
131  * @param sort          the mode_sort of the mode to be created
132  * @param bit_size      number of bits this mode allocate
133  * @param align         the byte alignment for an entity of this mode (in bits)
134  * @param sign          non-zero if this is a signed mode
135  * @param arithmetic    arithmetic operations possible with a mode
136  *
137  * This function constructs a new mode given by the parameters.
138  * If the parameters match an already defined mode, this mode is returned
139  * (including the default modes).
140  * If the mode is newly allocated, a new unique mode_code is choosen.
141  * Also, special value tarvals will be calculated such as null,
142  * min, max and can be retrieved using the get_mode_* fuctions
143  *
144  * @return
145  *      The new mode or NULL on error.
146  *
147  * @note
148  *      It is allowed to construct the default modes. So, a call
149  *      new_ir_mode("Is", irms_int_number, 32, 4, 1) will return mode_Is.
150  */
151 ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int align, int sign, mode_arithmetic arithmetic);
152
153 /**
154  *   Checks whether a pointer points to a mode.
155  *
156  *   @param thing     an arbitrary pointer
157  *
158  *   @return
159  *       true if the thing is a mode, else false
160  */
161 int is_mode (void *thing);
162
163 /* ********** Access methods to read mode information *********** */
164
165 /** Returns the classification of the mode */
166 modecode get_mode_modecode(const ir_mode *mode);
167
168 /** Returns the ident* of the mode */
169 ident *get_mode_ident(const ir_mode *mode);
170
171 /** Returns the null-terminated name of this mode. */
172 const char *get_mode_name(const ir_mode *mode);
173
174 /** Returns a coarse classification of the mode. */
175 mode_sort get_mode_sort(const ir_mode *mode);
176
177 /** Returns the size of values of the mode in bits. */
178 int get_mode_size_bits(const ir_mode *mode);
179
180 /** Returns the size of values of the mode in bytes.  If the size is not
181     dividable by 8 returns -1. */
182 int get_mode_size_bytes(const ir_mode *mode);
183
184 /** Returns the alignment of values of the mode in bytes. */
185 int get_mode_align(const ir_mode *mode);
186
187 /** Returns the signess of a mode */
188 int get_mode_sign (const ir_mode *mode);
189
190 /** Returns the arithmetic of a mode */
191 int get_mode_arithmetic (const ir_mode *mode);
192
193 /** Returns the stored intermediate information. */
194 void* get_mode_link(const ir_mode *mode);
195
196 /** Stores new intermediate information. */
197 void set_mode_link(ir_mode *mode, void *l);
198
199 /**
200  * Returns the smallest representable value of a given mode.
201  *
202  * For modes of the sort float_number this is the most negative value
203  * bigger than -infinit.
204  */
205 tarval *get_mode_min(ir_mode *mode);
206
207 /**
208  * Returns the biggest representable value o f a given mode.
209  *
210  * For modes of the sort float_number this is the largest value lower
211  * than infinit.
212  */
213 tarval *get_mode_max(ir_mode *mode);
214
215 /**
216  * Returns the value Zero represented in this mode.
217  *
218  * Zero is the additive neutral element and as such
219  * is defined only for modes allowing addition, i.e.
220  * floats and ints, and references (NULL-Pointer)
221  * else returns tarval_bad.
222  */
223 tarval *get_mode_null(ir_mode *mode);
224
225 /**
226  * Returns the value One, represented in this mode.
227  *
228  * One, being the multiplicative neutral element,
229  * is defined only for modes allowing multiplication,
230  * i.e. ints and floats.
231  */
232 tarval *get_mode_one(ir_mode *mode);
233
234 /**
235  * Returns the positive infinite value of a mode.
236  *
237  * This is only valid for float_numbers, other modes
238  * will result in tarval_bad.
239  */
240 tarval *get_mode_infinite(ir_mode *mode);
241
242 /**
243  * Returns the NAN value of a given mode.
244  *
245  * This is only valid for float_numbers, other modes
246  * will result in tarval_bad.
247  */
248 tarval *get_mode_NAN(ir_mode *mode);
249
250 extern ir_mode *mode_M;  /**< memory */
251
252 /* -- A set of predifined, numerical modes according to Techreport 1999-44 -- */
253 extern ir_mode *mode_F;  /**< signed float(32) */
254 extern ir_mode *mode_D;  /**< signed double(64) */
255 extern ir_mode *mode_E;  /**< signed extended(80) */
256 extern ir_mode *mode_Bs; /**< signed byte (former char) */
257 extern ir_mode *mode_Bu; /**< unsigned byte (former char) */
258 extern ir_mode *mode_Hs; /**< signed short integer */
259 extern ir_mode *mode_Hu; /**< unsigened short integer */
260 extern ir_mode *mode_Is; /**< signed integer */
261 extern ir_mode *mode_Iu; /**< unsigned integer */
262 extern ir_mode *mode_Ls; /**< signed long integer */
263 extern ir_mode *mode_Lu; /**< unsigned long integer */
264
265 extern ir_mode *mode_C;  /**< 8 bit char */
266 extern ir_mode *mode_U;  /**< 16 bit unicode char */
267
268 extern ir_mode *mode_P;  /**< pointer */
269 extern ir_mode *mode_P_mach; /**< A pointer mode that is set by the client of libfirm.  This mode
270                                represents the pointer size of the target machine. Is initialized
271                                to mode_P. */
272
273 /* -- Auxiliary modes necessary for the Firm representation -- */
274 extern ir_mode *mode_b;  /**< internal boolean */
275
276 extern ir_mode *mode_X;  /**< execution */
277 extern ir_mode *mode_BB; /**< block */
278
279 extern ir_mode *mode_T;  /**< tuple (none) */
280 extern ir_mode *mode_ANY;/**< undefined mode */
281 extern ir_mode *mode_BAD;/**< bad mode */
282
283 /*@{*/
284 /** Access routines for JNI Interface */
285 ir_mode *get_modeF(void);
286 ir_mode *get_modeD(void);
287 ir_mode *get_modeE(void);
288 ir_mode *get_modeBs(void);
289 ir_mode *get_modeBu(void);
290 ir_mode *get_modeHs(void);
291 ir_mode *get_modeHu(void);
292 ir_mode *get_modeIs(void);
293 ir_mode *get_modeIu(void);
294 ir_mode *get_modeLs(void);
295 ir_mode *get_modeLu(void);
296 ir_mode *get_modeC(void);
297 ir_mode *get_modeU(void);
298 ir_mode *get_modeP(void);
299 ir_mode *get_modeb(void);
300 ir_mode *get_modeX(void);
301 ir_mode *get_modeBB(void);
302 ir_mode *get_modeM(void);
303 ir_mode *get_modeT(void);
304 ir_mode *get_modeANY(void);
305 ir_mode *get_modeBAD(void);
306
307 /** Returns the machine specific pointer mode. */
308 ir_mode *get_modeP_mach(void);
309
310 /**
311  * Sets the machine specific pointer mode.
312  * If not set, the predefined mode mode_P will be used.
313  */
314 void set_modeP_mach(ir_mode *p);
315
316 /**
317    Functions to check, whether a modecode is signed, float, int, character,
318    reference, num, numP, data, datab or dataM.
319
320    For more exact definitions read the corresponding pages
321    in the firm documentation or the following enumeration
322
323    The set of "float" is defined as:
324    float = {irm_F, irm_D, irm_E}
325
326    The set of "int" is defined as:
327    int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
328
329    The set of "character" is defined as:
330    character  = {irm_C, irm_U}
331
332    The set of "reference" is defined as:
333    reference  = {irm_P}
334
335    The set of "num" is defined as:
336    num   = {float || int}
337
338    The set of "numP" is defined as:
339    numP  =  {float || int || reference}
340
341    The set of "data" is defined as:
342    data  =  {num || character || reference}
343
344    The set of "datab" is defined as:
345    datab =  {data || irm_b }
346
347    The set of "dataM" is defined as:
348    dataM =  {data || irm_M}
349 */
350 /*@}*/
351 /* Test for a certain class of modes. */
352 int mode_is_signed (const ir_mode *mode);
353 int mode_is_float (const ir_mode *mode);
354 int mode_is_int (const ir_mode *mode);
355 int mode_is_character (const ir_mode *mode);
356 int mode_is_reference (const ir_mode *mode);
357 int mode_is_num (const ir_mode *mode);
358 int mode_is_numP (const ir_mode *mode);
359 int mode_is_data (const ir_mode *mode);
360 int mode_is_datab (const ir_mode *mode);
361 int mode_is_dataM (const ir_mode *mode);
362
363 /** Returns true if sm can be converted to lm without loss
364    according to firm definiton */
365 int smaller_mode(const ir_mode *sm, const ir_mode *lm);
366
367 /** mode module initialization, call once before use of any other function **/
368 void init_mode (void);
369
370 #endif /* _IRMODE_H_ */