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