Added mode_sort prefix irms_, added new_ir_mode() function.
[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.
13  *
14  * SEE ALSO:
15  *    UKA tech report 1999-44 for more information about modes.
16  *
17  */
18 #ifndef _IRMODE_H_
19 #define _IRMODE_H_
20
21 #include "ident.h"
22
23 #ifndef _TARVAL_TYPEDEF_
24 #define _TARVAL_TYPEDEF_
25   typedef struct tarval tarval;
26 #endif
27
28 /**
29  * Contains relevant information about a mode.
30  *
31  * Neccessary information about a mode is stored in this struct
32  * which is used by the tarval modul to perform calculations
33  * and comparisons of values of a such described mode.
34  *
35  * ATTRIBUTES:
36  *  -  modecode code: An unambigous int for the mode
37  *  -  ident *name:             Name of this mode
38  *  -  mode_sort sort:          sort of mode specifying possible usage kategories
39  *  -  int    size:             size of the mode in Bits.
40  *  -  int    align:            byte alignment
41  *  -  unsigned sign:1:         signedness of this mode
42  *  -  ... more to come
43  *
44  * SEE ALSO:
45  *    The tech report 1999-44 describing FIRM and predefined modes
46  *    tarval.h
47  */
48 typedef struct ir_mode ir_mode;
49
50 /* ********** Predefined modes ********** */
51
52 /**
53  * Predefined mode according to tech report 1999-14.
54  */
55 typedef enum { /* irm is short for `ir mode' */
56   irm_BB,                       /**< basic block */
57   irm_X,                        /**< execution */
58   irm_F,                        /**< float(32) */
59   irm_D,                        /**< double(64) */
60   irm_E,                        /**< extended(80) */
61   irm_Bs,                       /**< signed byte(8) */
62   irm_Bu,                       /**< unsigned byte(8) */
63   irm_Hs,                       /**< signed short(16) */
64   irm_Hu,                       /**< unsigned short(16) */
65   irm_Is,                       /**< signed int(32) */
66   irm_Iu,                       /**< unsigned int(32) */
67   irm_Ls,                       /**< signed long(64) */
68   irm_Lu,                       /**< unsigned long(64) */
69   irm_C,                        /**< character */
70   irm_P,                        /**< pointer */
71   irm_b,                        /**< internal boolean */
72   irm_M,                        /**< memory */
73   irm_T,                        /**< tuple */
74   irm_U,                        /**< unicode character */
75   irm_max                       /**< maximum value for modecode */
76 } modecode;
77
78 /** These values represent the different arithmetics used to
79  *  manipulate values.
80  */
81 typedef enum {
82   /* Predefined sorts of modes */
83   irms_auxiliary,         /**< Only for Firm use, predefined. */
84   irms_internal_boolean,  /**< Internal boolean representation.
85                                Storing to memory impossible, convert first. */
86   /** user-extensible sorts of modes **/
87   irms_int_number,        /**< A mode to represent int numbers.
88                                Integer computations can be performed. */
89   irms_float_number,      /**< A mode to represent float numbers.
90                                Floating point computations can be performed. */
91   irms_reference,         /**< A mode to represent entities.
92                                Restricted int computations can be performed */
93   irms_character          /**< A mode to represent characters/symbols
94                                ?? Are computations allowed? as int?? */
95 } mode_sort;
96
97 /* ********** Constructor for user defined modes **************** */
98 /**
99  * Creates a new mode.
100  *
101  * @param name          the name of the mode to be created
102  * @param sort          the mode_sort of teh mode to be created
103  * @param bit_size      number of bits this mode allocate
104  * @param align         the byte alignment for an entity of this mode (in bits)
105  * @param sign          non-zero if this is a signed mode
106  *
107  * This function constructs a new mode given by the parameters.
108  * If the parameters match an already defined mode, this mode is returned
109  * (including the default modes).
110  * If the mode is newly allocated, a new unique mode_code is choosen.
111  * Also, special value tarvals will be calculated such as null,
112  * min, max and can be retrieved using the get_mode_* fuctions
113  *
114  * @return
115  *      The new mode or NULL on error.
116  *
117  * @note
118  *      FIRM modes are unique independant of its name. So, you cannot expect
119  *      that the returned mode will have the mode name.
120  *      It is allowed to construct the default modes. So, a call
121  *      new_ir_mode("Int", irms_int_number, 32, 4, 1) will return mode_Is.
122  */
123 ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int align, int sign);
124
125 /* ********** Access methods to read mode information *********** */
126
127 /** Returns the classification of the mode */
128 modecode get_mode_modecode(ir_mode *mode);
129
130 /** Returns the ident* of the mode */
131 ident *get_mode_ident(ir_mode *mode);
132
133 /** Returns the null-terminated name of this mode. */
134 const char *get_mode_name(ir_mode *mode);
135
136 /** Returns a coarse classification of the mode. */
137 mode_sort get_mode_sort(ir_mode *mode);
138
139 /** Returns the size of values of the mode in bits. */
140 int get_mode_size_bits(ir_mode *mode);
141
142 /** Returns the size of values of the mode in bytes.  If the size is not
143     dividable by 8 returns -1. */
144 int get_mode_size_bytes(ir_mode *mode);
145
146 /** Returns the alignment of values of the mode in bytes. */
147 int get_mode_align(ir_mode *mode);
148
149 /** Returns the signess of a mode */
150 int get_mode_sign (ir_mode *mode);
151
152 /**
153  * Returns the smallest representable value of a given mode.
154  *
155  * For modes of the sort float_number this is the most negative value
156  * bigger than -infinit.
157  */
158 tarval *get_mode_min(ir_mode *mode);
159
160 /**
161  * Returns the biggest representable value o f a given mode.
162  *
163  * For modes of the sort float_number this is the largest value lower
164  * than infinit.
165  */
166 tarval *get_mode_max(ir_mode *mode);
167
168 /**
169  * Returns the value Zero represented in this mode.
170  *
171  * Zero is the additive neutral element and as such
172  * is defined only for modes allowing addition, i.e.
173  * floats and ints, and references (NULL-Pointer)
174  * else returns tarval_bad.
175  */
176 tarval *get_mode_null(ir_mode *mode);
177
178 /**
179  * Returns the value One, represented in this mode.
180  *
181  * One, being the multiplicative neutral element,
182  * is defined only for modes allowing multiplication,
183  * i.e. ints and floats.
184  */
185 tarval *get_mode_one(ir_mode *mode);
186
187 /**
188  * Returns the positive infinite value of a mode.
189  *
190  * This is only valid for float_numbers, other modes
191  * will result in tarval_bad.
192  */
193 tarval *get_mode_infinite(ir_mode *mode);
194
195 /**
196  * Returns the NAN value of a given mode.
197  *
198  * This is only valid for float_numbers, other modes
199  * will result in tarval_bad.
200  */
201 tarval *get_mode_NAN(ir_mode *mode);
202
203 /* -- Auxiliary modes necessary for the Firm representation -- */
204 extern ir_mode *mode_T;  /**< tuple (none) */
205 extern ir_mode *mode_X;  /**< execution */
206 extern ir_mode *mode_M;  /**< memory */
207 extern ir_mode *mode_BB; /**< block */
208
209 /* -- A set of predifined, numerical modes according to Techreport 1999-44 -- */
210 extern ir_mode *mode_F;  /**< signed float(32) */
211 extern ir_mode *mode_D;  /**< signed double(64) */
212 extern ir_mode *mode_E;  /**< signed extended(80) */
213 extern ir_mode *mode_Bs; /**< signed byte (former char) */
214 extern ir_mode *mode_Bu; /**< unsigned byte (former char) */
215 extern ir_mode *mode_Hs; /**< signed short integer */
216 extern ir_mode *mode_Hu; /**< unsigened short integer */
217 extern ir_mode *mode_Is; /**< signed integer */
218 extern ir_mode *mode_Iu; /**< unsigned integer */
219 extern ir_mode *mode_Ls; /**< signed long integer */
220 extern ir_mode *mode_Lu; /**< unsigned long integer */
221
222 extern ir_mode *mode_b;  /**< internal boolean */
223 extern ir_mode *mode_C;  /**< 8 bit char */
224 extern ir_mode *mode_U;  /**< 16 bit unicode char */
225 extern ir_mode *mode_P;  /**< pointer */
226
227 /*@{*/
228 /** Access routines for JNI Interface */
229 ir_mode *get_modeT(void);
230 ir_mode *get_modeF(void);
231 ir_mode *get_modeD(void);
232 ir_mode *get_modeE(void);
233 ir_mode *get_modeBs(void);
234 ir_mode *get_modeBu(void);
235 ir_mode *get_modeHs(void);
236 ir_mode *get_modeHu(void);
237 ir_mode *get_modeIs(void);
238 ir_mode *get_modeIu(void);
239 ir_mode *get_modeLs(void);
240 ir_mode *get_modeLu(void);
241 ir_mode *get_modeC(void);
242 ir_mode *get_modeU(void);
243 ir_mode *get_modeP(void);
244 ir_mode *get_modeb(void);
245 ir_mode *get_modeX(void);
246 ir_mode *get_modeM(void);
247 ir_mode *get_modeBB(void);
248
249 /**
250    Functions to check, whether a modecode is signed, float, int, num, data,
251    datab or dataM.
252
253    For more exact definitions read the corresponding pages
254    in the firm documentation or the followingenumeration
255
256    The set of "float" is defined as:
257    float = {irm_F, irm_D, irm_E}
258
259    The set of "int" is defined as:
260    int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
261
262    The set of "num" is defined as:
263    num   = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
264             irm_Is, irm_Iu, irm_Ls, irm_Lu}
265             = {float || int}
266
267    The set of "data" is defined as:
268    data  = {irm_F, irm_D, irm_E irm_Bs, irm_Bu, irm_Hs, irm_Hu,
269             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P}
270             = {num || irm_C || irm_P}
271
272    The set of "datab" is defined as:
273    datab = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
274             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_b}
275             = {data || irm_b }
276
277    The set of "dataM" is defined as:
278    dataM = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
279             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_M}
280             = {data || irm_M}
281 */
282 /*@}*/
283 /* Test for a certain class of modes. */
284 int mode_is_signed (ir_mode *mode);
285 int mode_is_float (ir_mode *mode);
286 int mode_is_int (ir_mode *mode);
287 int mode_is_num (ir_mode *mode);
288 int mode_is_data (ir_mode *mode);
289 int mode_is_datab (ir_mode *mode);
290 int mode_is_dataM (ir_mode *mode);
291 /** Returns true if sm can be converted to lm without loss
292    according to firm definiton */
293 int smaller_mode(ir_mode *sm, ir_mode *lm);
294
295 /** mode module initialization, call once before use of any other function **/
296 void init_mode (void);
297
298 #endif /* _IRMODE_H_ */