3a854c58290aca294d4214b7bf5bd937fef0be21
[libfirm] / ir / tv / strcalc.h
1 /*
2  * Copyright (C) 1995-2007 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  * @summary
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_or(const void *value1, const void *value2, void *buffer);
114
115 /**
116  * buffer = value1 ^ value2
117  */
118 void sc_xor(const void *value1, const void *value2, void *buffer);
119
120 /**
121  * buffer = ~value
122  */
123 void sc_not(const void *value, void *buffer);
124
125 /**
126  * buffer = value1 * value2
127  */
128 void sc_mul(const void *value1, const void *value2, void *buffer);
129
130 /**
131  * buffer = value1 / value2
132  */
133 void sc_div(const void *value1, const void *value2, void *buffer);
134
135 /**
136  * buffer = value1 % value2
137  */
138 void sc_mod(const void *value1, const void *value2, void *buffer);
139
140 /**
141  * buffer = value1 << value2
142  */
143 void sc_shl(const void *value1, const void *value2, int radius, int sign, void *buffer);
144
145 /**
146  * buffer = value1 >>u value2
147  */
148 void sc_shr(const void *value1, const void *value2, int radius, int sign, void *buffer);
149
150 /**
151  * buffer = value1 >>s value2
152  */
153 void sc_shrs(const void *value1, const void *value2, int radius, int sign, void *buffer);
154
155 /**
156  * buffer = value1 <<>> value2
157  */
158 void sc_rot(const void *value1, const void *value2, int radius, int sign, void *buffer);
159
160 /*
161  * function declarations
162  */
163 const void *sc_get_buffer(void);
164 int sc_get_buffer_length(void);
165
166 void sign_extend(void *buffer, ir_mode *mode);
167
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);
170
171 /** create a value from a long */
172 void sc_val_from_long(long l, void *buffer);
173
174 /** create a value form an unsigned long */
175 void sc_val_from_ulong(unsigned long l, void *buffer);
176
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);
181
182 /**
183  * Compares val1 and val2
184  */
185 int  sc_comp(const void *val1, const void *val2);
186
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);
192 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
193
194 /**
195  * Converts a tarval into a string.
196  *
197  * @param val1        the value pointer
198  * @param bits        number of valid bits in this value
199  * @param base        output base
200  * @param signed_mode print it signed (only decimal mode supported
201  */
202 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
203
204 /** Initialize the strcalc module.
205  * Sets up internal data structures and constants
206  * After the first call subsequent calls have no effect
207  *
208  * @param precision_in_bytes Specifies internal precision to be used
209  *   for calculations. The reason for being multiples of 8 eludes me
210  */
211 void init_strcalc(int precision_in_bytes);
212 void finish_strcalc(void);
213 int sc_get_precision(void);
214
215 #endif /* FIRM_TV_STRCALC_H */