2 * Copyright (C) 1995-2007 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 * buffer = value1 << value2
143 void sc_shl(const void *value1, const void *value2, int radius, int sign, void *buffer);
146 * buffer = value1 >>u value2
148 void sc_shr(const void *value1, const void *value2, int radius, int sign, void *buffer);
151 * buffer = value1 >>s value2
153 void sc_shrs(const void *value1, const void *value2, int radius, int sign, void *buffer);
156 * buffer = value1 <<>> value2
158 void sc_rot(const void *value1, const void *value2, int radius, int sign, void *buffer);
161 * function declarations
163 const void *sc_get_buffer(void);
164 int sc_get_buffer_length(void);
166 void sign_extend(void *buffer, ir_mode *mode);
168 /** create an value form a string representation */
169 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode);
171 /** create a value from a long */
172 void sc_val_from_long(long l, void *buffer);
174 /** create a value form an unsigned long */
175 void sc_val_from_ulong(unsigned long l, void *buffer);
177 /** converts a value to a long */
178 long sc_val_to_long(const void *val);
179 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
180 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
183 * Compares val1 and val2
185 int sc_comp(const void *val1, const void *val2);
187 int sc_get_highest_set_bit(const void *value);
188 int sc_get_lowest_set_bit(const void *value);
189 int sc_is_zero(const void *value);
190 int sc_is_negative(const void *value);
191 int sc_had_carry(void);
194 * Return the bits of a tarval at a given byte-offset.
196 * @param value the value
197 * @param len number of valid bits in the value
198 * @param byte_ofs the byte offset
200 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
203 * Converts a tarval into a string.
205 * @param val1 the value pointer
206 * @param bits number of valid bits in this value
207 * @param base output base
208 * @param signed_mode print it signed (only decimal mode supported
210 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
212 /** Initialize the strcalc module.
213 * Sets up internal data structures and constants
214 * After the first call subsequent calls have no effect
216 * @param precision_in_bytes Specifies internal precision to be used
217 * for calculations. The reason for being multiples of 8 eludes me
219 void init_strcalc(int precision_in_bytes);
220 void finish_strcalc(void);
221 int sc_get_precision(void);
223 #endif /* FIRM_TV_STRCALC_H */