beifg: Let be_ifg_foreach_neighbour() declare the node variable.
[libfirm] / ir / tv / strcalc.h
1 /*
2  * Copyright (C) 1995-2011 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    Provides basic mathematical operations on values represented as strings.
23  * @date     2003
24  * @author   Mathias Heil
25  * @brief
26  *
27  * The module uses a string to represent values, and provides operations
28  * to perform calculations with these values.
29  * Results are stored in an internal buffer, so you have to make a copy
30  * of them if you need to store the result.
31  *
32  */
33 #ifndef FIRM_TV_STRCALC_H
34 #define FIRM_TV_STRCALC_H
35
36 #include "irmode.h"
37
38 #ifdef STRCALC_DEBUG_ALL             /* switch on all debug options */
39 #  ifndef STRCALC_DEBUG
40 #    define STRCALC_DEBUG            /* switch on debug output */
41 #  endif
42 #  ifndef STRCALC_DEBUG_PRINTCOMP    /* print arguments and result of each computation */
43 #    define STRCALC_DEBUG_PRINTCOMP
44 #  endif
45 #  ifndef STRCALC_DEBUG_FULLPRINT
46 #    define STRCALC_DEBUG_FULLPRINT  /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
47 #  endif
48 #  ifndef STRCALC_DEBUG_GROUPPRINT
49 #    define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
50 #  endif
51 #endif
52
53 /*
54  * constants, typedefs, enums
55  */
56
57 #define SC_DEFAULT_PRECISION 64
58
59 enum {
60   SC_0 = 0,
61   SC_1,
62   SC_2,
63   SC_3,
64   SC_4,
65   SC_5,
66   SC_6,
67   SC_7,
68   SC_8,
69   SC_9,
70   SC_A,
71   SC_B,
72   SC_C,
73   SC_D,
74   SC_E,
75   SC_F
76 };
77
78 /**
79  * The output mode for integer values.
80  */
81 enum base_t {
82   SC_hex,   /**< hexadecimal output with small letters */
83   SC_HEX,   /**< hexadecimal output with BIG letters */
84   SC_DEC,   /**< decimal output */
85   SC_OCT,   /**< octal output */
86   SC_BIN    /**< binary output */
87 };
88
89 /**
90  * buffer = value1 + value2
91  */
92 void sc_add(const void *value1, const void *value2, void *buffer);
93
94 /**
95  * buffer = value1 - value2
96  */
97 void sc_sub(const void *value1, const void *value2, void *buffer);
98
99 /**
100  * buffer = -value
101  */
102 void sc_neg(const void *value, void *buffer);
103
104 /**
105  * buffer = value1 & value2
106  */
107 void sc_and(const void *value1, const void *value2, void *buffer);
108
109 /**
110  * buffer = value1 & ~value2
111  */
112 void sc_andnot(const void *value1, const void *value2, void *buffer);
113
114 /**
115  * buffer = value1 | value2
116  */
117 void sc_or(const void *value1, const void *value2, void *buffer);
118
119 /**
120  * buffer = value1 ^ value2
121  */
122 void sc_xor(const void *value1, const void *value2, void *buffer);
123
124 /**
125  * buffer = ~value
126  */
127 void sc_not(const void *value, void *buffer);
128
129 /**
130  * buffer = value1 * value2
131  */
132 void sc_mul(const void *value1, const void *value2, void *buffer);
133
134 /**
135  * buffer = value1 / value2
136  */
137 void sc_div(const void *value1, const void *value2, void *buffer);
138
139 /**
140  * buffer = value1 % value2
141  */
142 void sc_mod(const void *value1, const void *value2, void *buffer);
143
144 /**
145  * div_buffer = value1 / value2
146  * mod_buffer = value1 % value2
147  */
148 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer);
149
150 /**
151  * buffer = value1 << offset
152  */
153 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
154
155 /**
156  * buffer = value1 << value2
157  */
158 void sc_shl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
159
160 /**
161  * buffer = value1 >>u offset
162  */
163 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
164
165 /**
166  * buffer = value1 >>u value2
167  */
168 void sc_shr(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
169
170 /**
171  * buffer = value1 >>s offset
172  */
173 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
174
175 /**
176  * buffer = value1 >>s value2
177  */
178 void sc_shrs(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
179
180 /**
181  * buffer = value1 <<left>> value2
182  */
183 void sc_rotl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
184
185 /**
186  * buffer = 0
187  */
188 void sc_zero(void *buffer);
189
190 /*
191  * function declarations
192  */
193 const void *sc_get_buffer(void);
194 int sc_get_buffer_length(void);
195
196 void sign_extend(void *buffer, ir_mode *mode);
197
198 /**
199  * create an value form a string representation
200  * @return 1 if ok, 0 in case of parse error
201  */
202 int sc_val_from_str(char sign, unsigned base, const char *str,
203                     size_t len, void *buffer);
204
205 /** create a value from a long */
206 void sc_val_from_long(long l, void *buffer);
207
208 /** create a value form an unsigned long */
209 void sc_val_from_ulong(unsigned long l, void *buffer);
210
211 /** converts a value to a long */
212 long sc_val_to_long(const void *val);
213 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
214 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
215
216 /** truncates a value to lowest @p num_bits bits */
217 void sc_truncate(unsigned num_bits, void *buffer);
218
219 /**
220  * Compares val1 and val2
221  */
222 int  sc_comp(const void *val1, const void *val2);
223
224 int sc_get_highest_set_bit(const void *value);
225 int sc_get_lowest_set_bit(const void *value);
226 int sc_is_zero(const void *value);
227 int sc_is_negative(const void *value);
228
229 /**
230  * Return the bits of a tarval at a given byte-offset.
231  *
232  * @param value     the value
233  * @param len       number of valid bits in the value
234  * @param byte_ofs  the byte offset
235  */
236 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
237
238 /**
239  * Converts a tarval into a string.
240  *
241  * @param val1        the value pointer
242  * @param bits        number of valid bits in this value
243  * @param base        output base
244  * @param signed_mode print it signed (only decimal mode supported
245  */
246 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
247
248 /** Initialize the strcalc module.
249  * Sets up internal data structures and constants
250  * After the first call subsequent calls have no effect
251  *
252  * @param precision_in_bytes Specifies internal precision to be used
253  *   for calculations. The reason for being multiples of 8 eludes me
254  */
255 void init_strcalc(int precision_in_bytes);
256 void finish_strcalc(void);
257 int sc_get_precision(void);
258
259 /** Return the bit at a given position. */
260 int sc_get_bit_at(const void *value, unsigned pos);
261
262 /** Set the bit at the specified position. */
263 void sc_set_bit_at(void *value, unsigned pos);
264
265 /* Strange semantics */
266 int sc_had_carry(void);
267
268 #endif /* FIRM_TV_STRCALC_H */