2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Provides basic mathematical operations on values represented as strings.
24 * @author Mathias Heil
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.
33 #ifndef FIRM_TV_STRCALC_H
34 #define FIRM_TV_STRCALC_H
38 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
39 # ifndef STRCALC_DEBUG
40 # define STRCALC_DEBUG /* switch on debug output */
42 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
43 # define STRCALC_DEBUG_PRINTCOMP
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) */
48 # ifndef STRCALC_DEBUG_GROUPPRINT
49 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
54 * constants, typedefs, enums
57 #define SC_DEFAULT_PRECISION 64
79 * The output mode for integer values.
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 */
90 * buffer = value1 + value2
92 void sc_add(const void *value1, const void *value2, void *buffer);
95 * buffer = value1 - value2
97 void sc_sub(const void *value1, const void *value2, void *buffer);
102 void sc_neg(const void *value, void *buffer);
105 * buffer = value1 & value2
107 void sc_and(const void *value1, const void *value2, void *buffer);
110 * buffer = value1 & ~value2
112 void sc_andnot(const void *value1, const void *value2, void *buffer);
115 * buffer = value1 | value2
117 void sc_or(const void *value1, const void *value2, void *buffer);
120 * buffer = value1 ^ value2
122 void sc_xor(const void *value1, const void *value2, void *buffer);
127 void sc_not(const void *value, void *buffer);
130 * buffer = value1 * value2
132 void sc_mul(const void *value1, const void *value2, void *buffer);
135 * buffer = value1 / value2
137 void sc_div(const void *value1, const void *value2, void *buffer);
140 * buffer = value1 % value2
142 void sc_mod(const void *value1, const void *value2, void *buffer);
145 * div_buffer = value1 / value2
146 * mod_buffer = value1 % value2
148 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer);
151 * buffer = value1 << offset
153 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
156 * buffer = value1 << value2
158 void sc_shl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
161 * buffer = value1 >>u offset
163 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
166 * buffer = value1 >>u value2
168 void sc_shr(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
171 * buffer = value1 >>s offset
173 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
176 * buffer = value1 >>s value2
178 void sc_shrs(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
181 * buffer = value1 <<left>> value2
183 void sc_rotl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
188 void sc_zero(void *buffer);
191 * function declarations
193 const void *sc_get_buffer(void);
194 int sc_get_buffer_length(void);
196 void sign_extend(void *buffer, ir_mode *mode);
199 * create an value form a string representation
200 * @return 1 if ok, 0 in case of parse error
202 int sc_val_from_str(char sign, unsigned base, const char *str,
203 size_t len, void *buffer);
205 /** create a value from a long */
206 void sc_val_from_long(long l, void *buffer);
208 /** create a value form an unsigned long */
209 void sc_val_from_ulong(unsigned long l, void *buffer);
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);
216 /** truncates a value to lowest @p num_bits bits */
217 void sc_truncate(unsigned num_bits, void *buffer);
220 * Compares val1 and val2
222 int sc_comp(const void *val1, const void *val2);
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);
230 * Return the bits of a tarval at a given byte-offset.
232 * @param value the value
233 * @param len number of valid bits in the value
234 * @param byte_ofs the byte offset
236 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
239 * Converts a tarval into a string.
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
246 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
248 /** Initialize the strcalc module.
249 * Sets up internal data structures and constants
250 * After the first call subsequent calls have no effect
252 * @param precision_in_bytes Specifies internal precision to be used
253 * for calculations. The reason for being multiples of 8 eludes me
255 void init_strcalc(int precision_in_bytes);
256 void finish_strcalc(void);
257 int sc_get_precision(void);
259 /** Return the bit at a given position. */
260 int sc_get_bit_at(const void *value, unsigned pos);
262 /** Set the bit at the specified position. */
263 void sc_set_bit_at(void *value, unsigned pos);
265 /* Strange semantics */
266 int sc_had_carry(void);
268 #endif /* FIRM_TV_STRCALC_H */