2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Provides basic mathematical operations on values represented as strings.
10 * @author Mathias Heil
13 * The module uses a string to represent values, and provides operations
14 * to perform calculations with these values.
15 * Results are stored in an internal buffer, so you have to make a copy
16 * of them if you need to store the result.
19 #ifndef FIRM_TV_STRCALC_H
20 #define FIRM_TV_STRCALC_H
24 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
25 # ifndef STRCALC_DEBUG
26 # define STRCALC_DEBUG /* switch on debug output */
28 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
29 # define STRCALC_DEBUG_PRINTCOMP
31 # ifndef STRCALC_DEBUG_FULLPRINT
32 # define STRCALC_DEBUG_FULLPRINT /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
34 # ifndef STRCALC_DEBUG_GROUPPRINT
35 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
40 * constants, typedefs, enums
43 #define SC_DEFAULT_PRECISION 64
65 * The output mode for integer values.
68 SC_hex, /**< hexadecimal output with small letters */
69 SC_HEX, /**< hexadecimal output with BIG letters */
70 SC_DEC, /**< decimal output */
71 SC_OCT, /**< octal output */
72 SC_BIN /**< binary output */
76 * buffer = value1 + value2
78 void sc_add(const void *value1, const void *value2, void *buffer);
81 * buffer = value1 - value2
83 void sc_sub(const void *value1, const void *value2, void *buffer);
88 void sc_neg(const void *value, void *buffer);
91 * buffer = value1 & value2
93 void sc_and(const void *value1, const void *value2, void *buffer);
96 * buffer = value1 & ~value2
98 void sc_andnot(const void *value1, const void *value2, void *buffer);
101 * buffer = value1 | value2
103 void sc_or(const void *value1, const void *value2, void *buffer);
106 * buffer = value1 ^ value2
108 void sc_xor(const void *value1, const void *value2, void *buffer);
113 void sc_not(const void *value, void *buffer);
116 * buffer = value1 * value2
118 void sc_mul(const void *value1, const void *value2, void *buffer);
121 * buffer = value1 / value2
123 void sc_div(const void *value1, const void *value2, void *buffer);
126 * buffer = value1 % value2
128 void sc_mod(const void *value1, const void *value2, void *buffer);
131 * div_buffer = value1 / value2
132 * mod_buffer = value1 % value2
134 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer);
137 * buffer = value1 << offset
139 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
142 * buffer = value1 << value2
144 void sc_shl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
147 * buffer = value1 >>u offset
149 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
152 * buffer = value1 >>u value2
154 void sc_shr(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
157 * buffer = value1 >>s offset
159 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
162 * buffer = value1 >>s value2
164 void sc_shrs(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
167 * buffer = value1 <<left>> value2
169 void sc_rotl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
174 void sc_zero(void *buffer);
177 * function declarations
179 const void *sc_get_buffer(void);
180 int sc_get_buffer_length(void);
182 void sign_extend(void *buffer, ir_mode *mode);
185 * create an value form a string representation
186 * @return 1 if ok, 0 in case of parse error
188 int sc_val_from_str(char sign, unsigned base, const char *str,
189 size_t len, void *buffer);
191 /** create a value from a long */
192 void sc_val_from_long(long l, void *buffer);
194 /** create a value form an unsigned long */
195 void sc_val_from_ulong(unsigned long l, void *buffer);
197 /** converts a value to a long */
198 long sc_val_to_long(const void *val);
199 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
200 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
202 /** truncates a value to lowest @p num_bits bits */
203 void sc_truncate(unsigned num_bits, void *buffer);
206 * Compares val1 and val2
208 ir_relation sc_comp(void const *val1, void const *val2);
210 int sc_get_highest_set_bit(const void *value);
211 int sc_get_lowest_set_bit(const void *value);
212 int sc_is_zero(const void *value);
213 int sc_is_negative(const void *value);
216 * Return the bits of a tarval at a given byte-offset.
218 * @param value the value
219 * @param len number of valid bits in the value
220 * @param byte_ofs the byte offset
222 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
225 * Converts a tarval into a string.
227 * @param val1 the value pointer
228 * @param bits number of valid bits in this value
229 * @param base output base
230 * @param signed_mode print it signed (only decimal mode supported
232 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
234 /** Initialize the strcalc module.
235 * Sets up internal data structures and constants
236 * After the first call subsequent calls have no effect
238 * @param precision_in_bytes Specifies internal precision to be used
239 * for calculations. The reason for being multiples of 8 eludes me
241 void init_strcalc(int precision_in_bytes);
242 void finish_strcalc(void);
243 int sc_get_precision(void);
245 /** Return the bit at a given position. */
246 int sc_get_bit_at(const void *value, unsigned pos);
248 /** Set the bit at the specified position. */
249 void sc_set_bit_at(void *value, unsigned pos);
251 /* Strange semantics */
252 int sc_had_carry(void);
254 #endif /* FIRM_TV_STRCALC_H */