irmode.h 13.6 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
/**
Matthias Braun's avatar
Matthias Braun committed
7
8
9
10
 * @file
 * @brief   Data modes of operations.
 * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil,
 *          Michael Beck
Matthias Braun's avatar
Matthias Braun committed
11
12
13
14
15
16
17
 */
#ifndef FIRM_IR_IRMODE_H
#define FIRM_IR_IRMODE_H

#include "firm_types.h"
#include "begin.h"

Matthias Braun's avatar
Matthias Braun committed
18
19
#include <stddef.h>

Matthias Braun's avatar
Matthias Braun committed
20
21
/**
 * @defgroup ir_mode Value Modes
Matthias Braun's avatar
Matthias Braun committed
22
 *  This module specifies the modes that type the firm nodes.  It defines
yb9976's avatar
yb9976 committed
23
24
 *  a data structure that describes a mode and implements constructors and
 *  access routines to this data structure. Further it defines a set of
Matthias Braun's avatar
Matthias Braun committed
25
 *  predefined modes.
Michael Beck's avatar
Michael Beck committed
26
 *
Matthias Braun's avatar
Matthias Braun committed
27
 *  SEE ALSO:
28
 *    UKA tech report 1999-44 for more information about modes.
Matthias Braun's avatar
Matthias Braun committed
29
 * @{
30
 */
Christian Schäfer's avatar
Christian Schäfer committed
31

32
33
34
/**
 * These values represent the different arithmetic operations possible with a
 * mode.
35
 */
36
typedef enum ir_mode_arithmetic {
37
	irma_none = 1,            /**< For modes for which no representation is
38
	                               specified. */
39
40
41
42
43
44
	irma_twos_complement = 2, /**< Values of the mode are represented as two's
	                               complement. Only legal for modes of sort
	                               int_number and reference. */
	irma_ieee754 = 256,       /**< Values of the mode are represented according
	                               to ieee754 floating point standard.  Only
	                               legal for modes of sort float_number. */
yb9976's avatar
yb9976 committed
45
	irma_x86_extended_float,  /**< x86 extended floating point values */
Matthias Braun's avatar
Matthias Braun committed
46
	irma_last = irma_x86_extended_float,
47
} ir_mode_arithmetic;
48

49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Specifies what happens when a float value is converted to an integer and
 * overflow happens.
 */
typedef enum float_int_conversion_overflow_style_t {
	ir_overflow_indefinite,  /**< the integer indefinite value (=INT_MIN) is
	                              returned. (e.g. x86 does this) */
	ir_overflow_min_max,     /**< INT_MIN/INT_MAX is returned depending on the
	                              sign of the floatingpoint number. (e.g. sparc
	                              does this). */
} float_int_conversion_overflow_style_t;

61
/**
62
 * Creates a new mode.
63
 *
64
 * @param name          the name of the mode to be created
65
 * @param arithmetic    arithmetic operations possible with a mode
66
67
 * @param bit_size      number of bits this mode allocate
 * @param sign          non-zero if this is a signed mode
68
 * @param modulo_shift  Is ignored for modes other than integer.
69
70
71
72
 *
 * This function constructs a new mode given by the parameters.
 * If the parameters match an already defined mode, this mode is returned
 * (including the default modes).
73
 *
74
 * @return
75
 *   The new mode or NULL on error.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 */
FIRM_API ir_mode *new_int_mode(const char *name,
                               ir_mode_arithmetic arithmetic,
                               unsigned bit_size, int sign,
                               unsigned modulo_shift);

/**
 * Create a new reference mode.
 *
 * Reference modes are always unsigned.
 */
FIRM_API ir_mode *new_reference_mode(const char *name,
                                     ir_mode_arithmetic arithmetic,
                                     unsigned bit_size,
                                     unsigned modulo_shift);

/**
 * Create a new ieee754 float mode.
94
 *
95
96
97
98
 * float-modes are always signed and have no modulo shift.
 * @param name          the name of the mode to be created
 * @param arithmetic    arithmetic/representation of the mode
 * @param exponent_size size of exponent in bits
Matthias Braun's avatar
Matthias Braun committed
99
100
 * @param mantissa_size size of mantissa in bits (including explicit one in
 *                      irma_x86_extended_float)
101
 * @param int_conv_overflow Semantic on float to integer conversion overflow.
102
 */
103
104
105
FIRM_API ir_mode *new_float_mode(const char *name,
                                 ir_mode_arithmetic arithmetic,
                                 unsigned exponent_size,
106
107
108
                                 unsigned mantissa_size,
                                 float_int_conversion_overflow_style_t
                                     int_conv_overflow);
109

110
111
112
113
/**
 * Creates a new mode for data values which are not used to perform arithmetic.
 * Arithmetic will be set to irma_none.
 */
114
FIRM_API ir_mode *new_non_arithmetic_mode(const char *name, unsigned bit_size);
115

116
/** Returns the ident* of the mode */
Michael Beck's avatar
Michael Beck committed
117
FIRM_API ident *get_mode_ident(const ir_mode *mode);
118

119
/** Returns the null-terminated name of this mode. */
Michael Beck's avatar
Michael Beck committed
120
FIRM_API const char *get_mode_name(const ir_mode *mode);
121

122
/** Returns the size of values of the mode in bits. */
Michael Beck's avatar
Michael Beck committed
123
FIRM_API unsigned get_mode_size_bits(const ir_mode *mode);
124

125
126
/** Returns the size of values of the mode in bytes.
 *  If the size is not dividable by 8 returns -1. */
Michael Beck's avatar
Michael Beck committed
127
FIRM_API unsigned get_mode_size_bytes(const ir_mode *mode);
128

yb9976's avatar
yb9976 committed
129
/** Returns the signedness of a mode.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
130
 *
yb9976's avatar
yb9976 committed
131
 * Returns the signedness of a mode: 1 if mode is signed. */
Michael Beck's avatar
Michael Beck committed
132
FIRM_API int get_mode_sign(const ir_mode *mode);
133

134
/** Returns the arithmetic of a mode */
Michael Beck's avatar
Michael Beck committed
135
FIRM_API ir_mode_arithmetic get_mode_arithmetic(const ir_mode *mode);
136

137
/** Returns the modulo shift attribute.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
138
139
 *
 *  Attribute modulo shift specifies for modes of kind irms_int_number
140
141
142
 *  whether shift applies modulo to value of bits to shift.  Zero for
 *  modes that are not integer.
 */
Michael Beck's avatar
Michael Beck committed
143
FIRM_API unsigned int get_mode_modulo_shift(const ir_mode *mode);
144

145
146
147
148
/**
 * Returns the smallest representable value of a given mode.
 *
 * For modes of the sort float_number this is the most negative value
Michael Beck's avatar
Michael Beck committed
149
 * bigger than -infinite.
150
 */
yb9976's avatar
yb9976 committed
151
FIRM_API ir_tarval *get_mode_min(const ir_mode *mode);
152

153
/**
yb9976's avatar
yb9976 committed
154
 * Returns the biggest representable value of a given mode.
155
156
 *
 * For modes of the sort float_number this is the largest value lower
Michael Beck's avatar
Michael Beck committed
157
 * than infinite.
158
 */
yb9976's avatar
yb9976 committed
159
FIRM_API ir_tarval *get_mode_max(const ir_mode *mode);
160

161
162
163
164
165
/**
 * Returns the value Zero represented in this mode.
 *
 * Zero is the additive neutral element and as such
 * is defined only for modes allowing addition, i.e.
166
 * op_pin_state_floats and ints, and references (NULL-Pointer)
167
168
 * else returns tarval_bad.
 */
169
FIRM_API ir_tarval *get_mode_null(const ir_mode *mode);
170

171
172
173
/**
 * Returns the value One, represented in this mode.
 *
174
175
 * One, being the multiplicative neutral element,
 * is defined only for modes allowing multiplication,
Michael Beck's avatar
Michael Beck committed
176
 * i.e. ints and floats.
177
 */
178
FIRM_API ir_tarval *get_mode_one(const ir_mode *mode);
179

Matthias Braun's avatar
Matthias Braun committed
180
181
182
183
/**
 * Returns the value where all bits are One, represented in this mode.
 *
 * All One is defined only for modes integer, reference and boolean modes
184
 * This represents the value -1 for signed modes with irma_twos_complement.
Matthias Braun's avatar
Matthias Braun committed
185
 */
yb9976's avatar
yb9976 committed
186
FIRM_API ir_tarval *get_mode_all_one(const ir_mode *mode);
Matthias Braun's avatar
Matthias Braun committed
187

188
/**
189
 * Returns a positive infinite value of a mode.
190
 *
191
192
193
 * This is only valid for float_numbers, other modes will result in tarval_bad.
 * There are typically multiple possible representations of infinity, don't
 * compare with this value but use tarval_is_plus_inf() instead.
194
 */
195
FIRM_API ir_tarval *get_mode_infinite(const ir_mode *mode);
196

197
/**
198
 * Returns a NAN value of a given mode.
199
 *
200
201
202
 * This is only valid for float_numbers, other modes will result in tarval_bad.
 * There are typically multiple possible representations of NaN, don't compare
 * with this value but use tarval_is_NaN() instead.
203
 */
204
FIRM_API ir_tarval *get_mode_NAN(const ir_mode *mode);
205

Michael Beck's avatar
Michael Beck committed
206
FIRM_API ir_mode *mode_M; /**< memory */
207

208
209
FIRM_API ir_mode *mode_F;   /**< ieee754 binary32 float (single precision) */
FIRM_API ir_mode *mode_D;   /**< ieee754 binary64 float (double precision) */
210
211
212
213
214
215
216
217
FIRM_API ir_mode *mode_Bs;  /**< int8 */
FIRM_API ir_mode *mode_Bu;  /**< uint8 */
FIRM_API ir_mode *mode_Hs;  /**< int16 */
FIRM_API ir_mode *mode_Hu;  /**< uint16 */
FIRM_API ir_mode *mode_Is;  /**< int32 */
FIRM_API ir_mode *mode_Iu;  /**< uint32 */
FIRM_API ir_mode *mode_Ls;  /**< int64 */
FIRM_API ir_mode *mode_Lu;  /**< uint64 */
Michael Beck's avatar
Michael Beck committed
218
FIRM_API ir_mode *mode_P;   /**< pointer */
Christian Schäfer's avatar
Christian Schäfer committed
219

220
221
222
223
224
225
226
227
228
/**
 * This mode represents (parts of) the processor status flag queried in
 * conditional jumps or predicated code.
 *
 * Do not confuse this with boolean variables found in some languages. You
 * cannot perform any operations like And, Or, Not, Phi, etc. on mode_b
 * values (although some of these constructs can be legalized by lower_mode_b().
 */
FIRM_API ir_mode *mode_b;
229

Michael Beck's avatar
Michael Beck committed
230
231
FIRM_API ir_mode *mode_X;  /**< execution */
FIRM_API ir_mode *mode_BB; /**< block */
232

Michael Beck's avatar
Michael Beck committed
233
234
235
FIRM_API ir_mode *mode_T;  /**< tuple (none) */
FIRM_API ir_mode *mode_ANY;/**< undefined mode */
FIRM_API ir_mode *mode_BAD;/**< bad mode */
236

237
/** Returns float mode */
Michael Beck's avatar
Michael Beck committed
238
FIRM_API ir_mode *get_modeF(void);
239
/** Returns double mode */
Michael Beck's avatar
Michael Beck committed
240
FIRM_API ir_mode *get_modeD(void);
241
/** Returns byte signed mode */
Michael Beck's avatar
Michael Beck committed
242
FIRM_API ir_mode *get_modeBs(void);
243
/** Returns byte unsigned mode */
Michael Beck's avatar
Michael Beck committed
244
FIRM_API ir_mode *get_modeBu(void);
245
/** Returns halfword signed mode */
Michael Beck's avatar
Michael Beck committed
246
FIRM_API ir_mode *get_modeHs(void);
247
/** Returns halfword unsigned mode */
Michael Beck's avatar
Michael Beck committed
248
FIRM_API ir_mode *get_modeHu(void);
249
/** Returns integer signed mode */
Michael Beck's avatar
Michael Beck committed
250
FIRM_API ir_mode *get_modeIs(void);
251
/** Returns integer unsigned mode */
Michael Beck's avatar
Michael Beck committed
252
FIRM_API ir_mode *get_modeIu(void);
253
/** Returns long signed mode */
Michael Beck's avatar
Michael Beck committed
254
FIRM_API ir_mode *get_modeLs(void);
255
/** Returns long unsigned mode */
Michael Beck's avatar
Michael Beck committed
256
FIRM_API ir_mode *get_modeLu(void);
257
/** Returns pointer mode */
Michael Beck's avatar
Michael Beck committed
258
FIRM_API ir_mode *get_modeP(void);
259
/** Returns internal boolean mode */
Michael Beck's avatar
Michael Beck committed
260
FIRM_API ir_mode *get_modeb(void);
261
/** Returns control-flow mode */
Michael Beck's avatar
Michael Beck committed
262
FIRM_API ir_mode *get_modeX(void);
263
/** Returns Basic-Block mode */
Michael Beck's avatar
Michael Beck committed
264
FIRM_API ir_mode *get_modeBB(void);
265
/** Returns memory mode */
Michael Beck's avatar
Michael Beck committed
266
FIRM_API ir_mode *get_modeM(void);
267
/** Returns tuple mode */
Michael Beck's avatar
Michael Beck committed
268
FIRM_API ir_mode *get_modeT(void);
269
/** Returns ANY mode */
Michael Beck's avatar
Michael Beck committed
270
FIRM_API ir_mode *get_modeANY(void);
271
/** Returns BAD mode */
Michael Beck's avatar
Michael Beck committed
272
FIRM_API ir_mode *get_modeBAD(void);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
273

274
275
/** Sets the machine specific pointer mode. */
FIRM_API void set_modeP(ir_mode *p);
Michael Beck's avatar
fixed:    
Michael Beck committed
276

277
/** Returns 1 if @p mode is signed, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
278
FIRM_API int mode_is_signed (const ir_mode *mode);
yb9976's avatar
yb9976 committed
279
/** Returns 1 if @p mode is for floating point numbers, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
280
FIRM_API int mode_is_float (const ir_mode *mode);
281
/** Returns 1 if @p mode is for integer numbers, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
282
FIRM_API int mode_is_int (const ir_mode *mode);
283
/** Returns 1 if @p mode is for references/pointers, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
284
FIRM_API int mode_is_reference (const ir_mode *mode);
285
/** Returns 1 if @p mode is for numeric values, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
286
FIRM_API int mode_is_num (const ir_mode *mode);
287
/** Returns 1 if @p mode is for data values, 0 otherwise */
Michael Beck's avatar
Michael Beck committed
288
FIRM_API int mode_is_data (const ir_mode *mode);
289

290
/**
291
292
 * Returns true if a value of mode @p sm can be converted to mode @p lm without
 * loss.
293
 *
294
 * That is the interpretation of the numbers does not changes, so a signed
295
296
297
 * integer mode is never smaller than an unsigned integer mode since the
 * unsigned mode can't represent negative numbers in a way that they are
 * interpreted as negative numbers.
298
299
300
 *
 * @see values_in_mode()
 */
Michael Beck's avatar
Michael Beck committed
301
FIRM_API int smaller_mode(const ir_mode *sm, const ir_mode *lm);
302

303
/**
304
305
 * Returns true if no information is lost when converting a value of mode @p sm
 * into mode @p lm (and back to mode @p sm).
306
 *
307
 * So the interpretation of the values may change in the intermediate mode @p lm
308
309
 * (for example when converting negative signed integer numbers into unsigned
 * integers) but after a conversion back they are exactly the same value.
310
311
312
 *
 * @see smaller_mode()
 */
Michael Beck's avatar
Michael Beck committed
313
FIRM_API int values_in_mode(const ir_mode *sm, const ir_mode *lm);
314

315
316
/**
 * Returns a matching unsigned mode for a given integer signed mode.
Michael Beck's avatar
Michael Beck committed
317
 * Returns NULL if no matching mode exists.
318
 */
Michael Beck's avatar
Michael Beck committed
319
FIRM_API ir_mode *find_unsigned_mode(const ir_mode *mode);
320
321
322

/**
 * Returns a matching signed mode for a given integer unsigned mode.
Michael Beck's avatar
Michael Beck committed
323
 * Returns NULL if no matching mode exists.
324
 */
Michael Beck's avatar
Michael Beck committed
325
FIRM_API ir_mode *find_signed_mode(const ir_mode *mode);
326

Michael Beck's avatar
Michael Beck committed
327
328
329
330
/**
 * Returns an integer mode with 2*n bits for a given integer mode with n bits.
 * Returns NULL if no matching mode exists.
 */
Michael Beck's avatar
Michael Beck committed
331
FIRM_API ir_mode *find_double_bits_int_mode(const ir_mode *mode);
Michael Beck's avatar
Michael Beck committed
332

333
/**
334
335
336
 * Returns non-zero if the given mode has negative zeros, i.e. +0 and -0 exist.
 * Note that for comparisons +0 and -0 are considered equal, the sign only
 * shows in expressions like 1/x which results in +inf/-inf.
337
 */
338
FIRM_API int mode_has_signed_zero(const ir_mode *mode);
339

340
341
342
/**
 * Returns non-zero if the given mode might overflow on unary Minus.
 */
Michael Beck's avatar
Michael Beck committed
343
FIRM_API int mode_overflow_on_unary_Minus(const ir_mode *mode);
344

Michael Beck's avatar
Michael Beck committed
345
/**
346
 * Returns non-zero if the mode has a reversed wrap-around
Michael Beck's avatar
Michael Beck committed
347
348
349
350
 * logic, especially (a + x) - x == a.
 * This is normally true for integer modes, not for floating
 * point modes.
 */
Michael Beck's avatar
Michael Beck committed
351
FIRM_API int mode_wrap_around(const ir_mode *mode);
Michael Beck's avatar
Michael Beck committed
352

353
/**
354
 * Returns the unsigned integer equivalent mode for an reference mode.
355
 */
yb9976's avatar
yb9976 committed
356
FIRM_API ir_mode *get_reference_mode_unsigned_eq(const ir_mode *mode);
357
358
359
360

/**
 * Sets the unsigned integer equivalent mode for an reference mode.
 */
Michael Beck's avatar
Michael Beck committed
361
FIRM_API void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode);
362

363
/**
Matthias Braun's avatar
Matthias Braun committed
364
365
 * Returns size of bits used for to encode the mantissa (for float modes).
 * This includes the leading one for modes with irma_x86_extended_float.
366
367
368
369
 */
FIRM_API unsigned get_mode_mantissa_size(const ir_mode *mode);

/**
370
 * Returns size of exponent in bits (for float modes)
371
372
373
 */
FIRM_API unsigned get_mode_exponent_size(const ir_mode *mode);

374
375
376
377
378
379
/**
 * Returns semantic on float to integer conversion overflow.
 */
FIRM_API float_int_conversion_overflow_style_t get_mode_float_int_overflow(
		const ir_mode *mode);

380
381
/**
 * Returns non-zero if the cast from mode src to mode dst is a
yb9976's avatar
yb9976 committed
382
 * reinterpret cast (i.e. only the bit pattern is reinterpreted,
383
384
 * no conversion is done)
 */
Michael Beck's avatar
Michael Beck committed
385
FIRM_API int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst);
386

387
388
389
/**
 * Returns the primitive type matching the given mode
 */
Michael Beck's avatar
Michael Beck committed
390
FIRM_API ir_type *get_type_for_mode(const ir_mode *mode);
391

Matthias Braun's avatar
Matthias Braun committed
392
393
394
395
396
397
/** Returns number of known modes. */
FIRM_API size_t ir_get_n_modes(void);

/** Returns known mode number @p num. */
FIRM_API ir_mode *ir_get_mode(size_t num);

Matthias Braun's avatar
Matthias Braun committed
398
399
/** @} */

400
#include "end.h"
401

Matthias Braun's avatar
Matthias Braun committed
402
#endif