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