ir_mode: simplify interface, improve float-mode handling
[libfirm] / ir / ir / irmode_t.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Data modes of operations -- private header.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil,
24  *          Michael Beck
25  * @version $Id$
26  */
27 #ifndef FIRM_IR_IRMODE_T_H
28 #define FIRM_IR_IRMODE_T_H
29
30 #include <assert.h>
31 #include "irtypes.h"
32 #include "irmode.h"
33
34 /* ------------------------------- *
35  * inline functions                *
36  * ------------------------------- */
37 static inline ir_mode *get_modeP_code_(void) { return mode_P_code; }
38
39 static inline ir_mode *get_modeP_data_(void) { return mode_P_data; }
40
41 static inline ident *get_mode_ident_(const ir_mode *mode) { return mode->name; }
42
43 static inline ir_mode_sort get_mode_sort_(const ir_mode *mode) { return mode->sort; }
44
45 static inline unsigned get_mode_size_bits_(const ir_mode *mode) { return mode->size; }
46
47 static inline unsigned get_mode_size_bytes_(const ir_mode *mode)
48 {
49         unsigned size = get_mode_size_bits_(mode);
50         if ((size & 7) != 0) return (unsigned) -1;
51         return size >> 3;
52 }
53
54 static inline int get_mode_sign_(const ir_mode *mode) { return mode->sign; }
55
56 static inline ir_mode_arithmetic get_mode_arithmetic_(const ir_mode *mode) { return mode->arithmetic; }
57
58 static inline unsigned int get_mode_modulo_shift_(const ir_mode *mode) { return mode->modulo_shift; }
59
60 static inline void *get_mode_link_(const ir_mode *mode) { return mode->link; }
61
62 static inline void set_mode_link_(ir_mode *mode, void *l) { mode->link = l; }
63
64 /* Functions to check, whether a mode is signed, float, int, num, data,
65    datab or dataM. For more exact definitions read the corresponding pages
66    in the firm documentation or the following enumeration
67
68    The set of "float" is defined as:
69    ---------------------------------
70    float = {irm_F, irm_D, irm_E}
71
72    The set of "int" is defined as:
73    -------------------------------
74    int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
75
76    The set of "num" is defined as:
77    -------------------------------
78    num   = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
79             irm_Is, irm_Iu, irm_Ls, irm_Lu}
80             = {float || int}
81
82    The set of "data" is defined as:
83    -------------------------------
84    data  = {irm_F, irm_D, irm_E irm_Bs, irm_Bu, irm_Hs, irm_Hu,
85             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P}
86             = {num || irm_C || irm_U || irm_P}
87
88    The set of "datab" is defined as:
89    ---------------------------------
90    datab = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
91             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_b}
92             = {data || irm_b }
93
94    The set of "dataM" is defined as:
95    ---------------------------------
96    dataM = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
97             irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_M}
98             = {data || irm_M}
99 */
100
101 static inline int mode_is_signed_(const ir_mode *mode)
102 {
103         return mode->sign;
104 }
105
106 static inline int mode_is_float_(const ir_mode *mode)
107 {
108         return (get_mode_sort_(mode) == irms_float_number);
109 }
110
111 static inline int mode_is_int_(const ir_mode *mode)
112 {
113         return (get_mode_sort_(mode) == irms_int_number);
114 }
115
116 static inline int mode_is_reference_(const ir_mode *mode)
117 {
118         return (get_mode_sort_(mode) == irms_reference);
119 }
120
121 static inline int mode_is_num_(const ir_mode *mode)
122 {
123         return (get_mode_sort_(mode) & irmsh_is_num);
124 }
125
126 static inline int mode_is_data_(const ir_mode *mode)
127 {
128         return (get_mode_sort_(mode) & irmsh_is_data);
129 }
130
131 static inline int mode_is_datab_(const ir_mode *mode)
132 {
133         return (get_mode_sort_(mode) & irmsh_is_datab);
134 }
135
136 static inline int mode_is_dataM_(const ir_mode *mode)
137 {
138         return (get_mode_sort_(mode) & irmsh_is_dataM);
139 }
140
141 static inline ir_type *get_type_for_mode_(const ir_mode *mode)
142 {
143         return mode->type;
144 }
145
146 static inline unsigned get_mode_mantissa_size_(const ir_mode *mode)
147 {
148         return mode->float_desc.mantissa_size;
149 }
150
151 static inline unsigned get_mode_exponent_size_(const ir_mode *mode)
152 {
153         return mode->float_desc.exponent_size;
154 }
155
156 /** mode module initialization, call once before use of any other function **/
157 void init_mode(void);
158
159 /** mode module finalization. frees all memory.  */
160 void finish_mode(void);
161
162 #define get_modeP_code()               get_modeP_code_()
163 #define get_modeP_data()               get_modeP_data_()
164 #define get_mode_ident(mode)           get_mode_ident_(mode)
165 #define get_mode_sort(mode)            get_mode_sort_(mode)
166 #define get_mode_size_bits(mode)       get_mode_size_bits_(mode)
167 #define get_mode_size_bytes(mode)      get_mode_size_bytes_(mode)
168 #define get_mode_sign(mode)            get_mode_sign_(mode)
169 #define get_mode_arithmetic(mode)      get_mode_arithmetic_(mode)
170 #define get_mode_modulo_shift(mode)    get_mode_modulo_shift_(mode)
171 #define get_mode_link(mode)            get_mode_link_(mode)
172 #define set_mode_link(mode, l)         set_mode_link_(mode, l)
173 #define mode_is_signed(mode)           mode_is_signed_(mode)
174 #define mode_is_float(mode)            mode_is_float_(mode)
175 #define mode_is_int(mode)              mode_is_int_(mode)
176 #define mode_is_reference(mode)        mode_is_reference_(mode)
177 #define mode_is_num(mode)              mode_is_num_(mode)
178 #define mode_is_data(mode)             mode_is_data_(mode)
179 #define mode_is_datab(mode)            mode_is_datab_(mode)
180 #define mode_is_dataM(mode)            mode_is_dataM_(mode)
181 #define get_type_for_mode(mode)        get_type_for_mode_(mode)
182 #define get_mode_mantissa_size(mode)   get_mode_mantissa_size_(mode)
183 #define get_mode_exponent_size(mode)   get_mode_exponent_size_(mode)
184
185 #endif