2 * Copyright (C) 1995-2008 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
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.
34 #ifndef FIRM_TV_STRCALC_H
35 #define FIRM_TV_STRCALC_H
39 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
40 # ifndef STRCALC_DEBUG
41 # define STRCALC_DEBUG /* switch on debug output */
43 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
44 # define STRCALC_DEBUG_PRINTCOMP
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) */
49 # ifndef STRCALC_DEBUG_GROUPPRINT
50 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
55 * constants, typedefs, enums
58 #define SC_DEFAULT_PRECISION 64
80 * The output mode for integer values.
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 */
91 * buffer = value1 + value2
93 void sc_add(const void *value1, const void *value2, void *buffer);
96 * buffer = value1 - value2
98 void sc_sub(const void *value1, const void *value2, void *buffer);
103 void sc_neg(const void *value, void *buffer);
106 * buffer = value1 & value2
108 void sc_and(const void *value1, const void *value2, void *buffer);
111 * buffer = value1 | value2
113 void sc_or(const void *value1, const void *value2, void *buffer);
116 * buffer = value1 ^ value2
118 void sc_xor(const void *value1, const void *value2, void *buffer);
123 void sc_not(const void *value, void *buffer);
126 * buffer = value1 * value2
128 void sc_mul(const void *value1, const void *value2, void *buffer);
131 * buffer = value1 / value2
133 void sc_div(const void *value1, const void *value2, void *buffer);
136 * buffer = value1 % value2
138 void sc_mod(const void *value1, const void *value2, void *buffer);
141 * div_buffer = value1 / value2
142 * mod_buffer = value1 % value2
144 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer);
147 * buffer = value1 << offset
149 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
152 * buffer = value1 << value2
154 void sc_shl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
157 * buffer = value1 >>u offset
159 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
162 * buffer = value1 >>u value2
164 void sc_shr(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
167 * buffer = value1 >>s value2
169 void sc_shrs(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
172 * buffer = value1 <<left>> value2
174 void sc_rotl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
179 void sc_zero(void *buffer);
182 * function declarations
184 const void *sc_get_buffer(void);
185 int sc_get_buffer_length(void);
187 void sign_extend(void *buffer, ir_mode *mode);
189 /** create an value form a string representation */
190 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode);
192 /** create a value from a long */
193 void sc_val_from_long(long l, void *buffer);
195 /** create a value form an unsigned long */
196 void sc_val_from_ulong(unsigned long l, void *buffer);
198 /** converts a value to a long */
199 long sc_val_to_long(const void *val);
200 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
201 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
203 /** truncates a value to lowest @p num_bits bits */
204 void sc_truncate(unsigned num_bits, void *buffer);
207 * Compares val1 and val2
209 int sc_comp(const void *val1, const void *val2);
211 int sc_get_highest_set_bit(const void *value);
212 int sc_get_lowest_set_bit(const void *value);
213 int sc_is_zero(const void *value);
214 int sc_is_negative(const void *value);
215 int sc_had_carry(void);
218 * Return the bits of a tarval at a given byte-offset.
220 * @param value the value
221 * @param len number of valid bits in the value
222 * @param byte_ofs the byte offset
224 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
227 * Converts a tarval into a string.
229 * @param val1 the value pointer
230 * @param bits number of valid bits in this value
231 * @param base output base
232 * @param signed_mode print it signed (only decimal mode supported
234 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
236 /** Initialize the strcalc module.
237 * Sets up internal data structures and constants
238 * After the first call subsequent calls have no effect
240 * @param precision_in_bytes Specifies internal precision to be used
241 * for calculations. The reason for being multiples of 8 eludes me
243 void init_strcalc(int precision_in_bytes);
244 void finish_strcalc(void);
245 int sc_get_precision(void);
247 #endif /* FIRM_TV_STRCALC_H */