tv.h 25.2 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Michael Beck's avatar
Michael Beck committed
20
/**
Michael Beck's avatar
Michael Beck committed
21
22
23
24
25
26
 * @file
 * @brief    Representation of and static computations on target machine
 *           values.
 * @date     2003
 * @author   Mathias Heil
 * @version  $Id$
yb9976's avatar
yb9976 committed
27
 * @brief
Michael Beck's avatar
Michael Beck committed
28
 *   Tarvals represent target machine values.  They are typed by modes.
29
 *   Tarvals only represent values of mode_sort:
Michael Beck's avatar
Michael Beck committed
30
31
32
33
34
 *    - int_number,
 *    - float_number,
 *    - boolean,
 *    - reference,
 *    - character
35
36
37
 *
 *   In case of references the module accepts an entity to represent the
 *   value.
Michael Beck's avatar
Michael Beck committed
38
 *   Furthermore, computations and conversions of these values can
39
40
41
42
43
44
 *   be performed.
 *
 * HISTORY
 *    The original tv module originated in the fiasco compiler written ...
 *    This is the new version, described in the tech report 1999-14 by ...
 *
Michael Beck's avatar
Michael Beck committed
45
 * @sa
46
47
 *    Techreport 1999-14
 *    irmode.h for the modes definitions
Michael Beck's avatar
Michael Beck committed
48
 *    irnode.h for the pn_Cmp table
Michael Beck's avatar
Michael Beck committed
49
50
51
52
53
54
 */
#ifndef FIRM_TV_TV_H
#define FIRM_TV_TV_H

#include "firm_types.h"
#include "irnode.h"
Christian Schäfer's avatar
Christian Schäfer committed
55

56
57
#include "begin.h"

58
/* ************************ Constructors for tarvals ************************ */
59
60
61

/**
 * Constructor function for new tarvals.
62
 *
63
64
65
 * @param str   The string representing the target value
 * @param len   The length of the string
 * @param mode  The mode requested for the result tarval
66
 *
67
68
69
70
 * This function creates a new tarval representing the value represented
 * by a CString, aka char array. If a tarval representing this value already
 * exists, this tarval is returned instead of a new one. So tarvals are
 * directly comparable since their representation is unique.
71
 *
72
 * This function accepts the following strings:
73
 *
74
75
76
77
 * if mode is int_number:
 *  - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
 *  - 0[0-7]*            (octal representation)
 *  - (+|-)?[1-9][0-9]*  (decimal representation)
78
 *
Christoph Mallon's avatar
Christoph Mallon committed
79
 * if mode is float_number:
80
81
82
83
84
85
86
87
88
89
90
91
 *  - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
 *
 * if mode is boolean: true, True, TRUE ... False... 0, 1,
 *
 * if mode is reference: hexadecimal of decimal number as int
 *
 * if mode is character: hex or dec
 *
 * Leading and/or trailing spaces are ignored
 *
 * @return
 *   A tarval of proper type representing the requested value is returned.
92
93
94
95
 *   Tarvals are unique, so for any value/mode pair at most one tarval will
 *   exist, which will be returned upon further requests with an identical
 *   value/mode pair.
 *
96
97
98
 * @note
 *   If the string is not representable in the given mode an assertion is
 *   thrown in assert build.
99
 *
100
 * @sa
101
 *   irmode.h for predefined modes
102
103
104
 *   new_tarval_from_long()
 *   new_tarval_from_double()
 */
Matthias Braun's avatar
Matthias Braun committed
105
106
FIRM_API ir_tarval *new_tarval_from_str(const char *str, size_t len,
                                        ir_mode *mode);
Christian Schäfer's avatar
Christian Schäfer committed
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * Construct a new tarval from a given string.
 *
 * @param str   The string representing the target value
 * @param len   The length of the string
 * @param sign  is -1 or 1 depending on the numbers sign
 * @param base  number system base.
 *              binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
 *              are supported.
 * @param mode  The mode requested for the result tarval
 *
 * @return
 *   A tarval with the given mode. If overflow settings are set to
 *   TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
 *   represented in the given mode.
 *   Return bad if the number couldn't successfully be parsed.
 */
Matthias Braun's avatar
Matthias Braun committed
125
126
127
FIRM_API ir_tarval *new_integer_tarval_from_str(const char *str, size_t len,
                                                char sign, unsigned char base,
                                                ir_mode *mode);
128

129
130
/**
 * Constructor function for new tarvals
131
 *
132
133
 * @param l     The long representing the value
 * @param mode  The mode requested for the result tarval
134
 *
135
136
137
138
 * This function creates a new tarval representing the value represented
 * by a long integer. If a tarval representing this value already exists,
 * this tarval is returned instead of a new one. So tarvals are directly
 * comparable since their representation is unique.
139
 *
140
141
 * @return
 *   A tarval of proper type representing the requested value is returned.
142
143
144
145
 *   Tarvals are unique, so for any value/mode pair at most one tarval will
 *   exist, which will be returned upon further requests with an identical
 *   value/mode pair.
 *
146
147
148
 * @note
 *   If the long is not representable in the given mode an assertion is
 *   thrown in assert build.
149
 *
150
 * @sa
151
 *   irmode.h for predefined modes
152
153
 *   new_tarval_from_str()
 *   new_tarval_from_double()
154
 *
155
 */
Matthias Braun's avatar
Matthias Braun committed
156
FIRM_API ir_tarval *new_tarval_from_long(long l, ir_mode *mode);
157

Götz Lindenmaier's avatar
Götz Lindenmaier committed
158
159
/** Return value as long if possible.
 *
160
161
 * This returns a long int with the value represented value, or
 * gibberish, depending on the size of long int and the size of the
162
 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
163
164
 * get_mode_max(mode_Ls).
 * This will overflow silently, so use only if you know what
165
 * you are doing! (better check with tarval_is_long()...)
Götz Lindenmaier's avatar
Götz Lindenmaier committed
166
 * Works only for int modes, even not for character modes!
167
 */
Matthias Braun's avatar
Matthias Braun committed
168
FIRM_API long get_tarval_long(ir_tarval *tv);
169

170
/**
Götz Lindenmaier's avatar
Götz Lindenmaier committed
171
 * This validates if get_tarval_long() will return a satisfying
172
173
 * result. I.e. if tv is an int_number and between min, max
 * of long int (signed!)
174
175
 *
 * @param tv    the tarval
176
 */
Matthias Braun's avatar
Matthias Braun committed
177
FIRM_API int tarval_is_long(ir_tarval *tv);
178

179
180
/**
 * Constructor function for new tarvals.
181
 *
182
 * @param d     The (long) double representing the value
183
 * @param mode  The mode requested for the result tarval
184
 *
185
 * This function creates a new tarval representing the value represented
186
 * by a (long) double. If a tarval representing this value already exists,
187
188
189
 * this tarval is returned instead of a new one. So tarvals are directly
 * comparable since their representation is unique.
 * Only modes of sort float_number can be constructed this way.
190
 *
191
192
 * @return
 *   A tarval of proper type representing the requested value is returned.
193
194
195
196
 *   Tarvals are unique, so for any value/mode pair at most one tarval will
 *   exist, which will be returned upon further requests with an identical
 *   value/mode pair.
 *
197
 * @note
198
 *   If the (long) double is not representable in the given mode an assertion
199
 *   is thrown. This will happen for any mode not of sort float_number.
200
 *
201
 * @sa
202
 *   irmode.h for predefined values
203
204
205
 *   new_tarval_from_str()
 *   new_tarval_from_long()
 */
206
207
208
209
210
211
FIRM_API ir_tarval *new_tarval_from_double(double d, ir_mode *mode);

/**
 * same as new_tarval_from_double(), but with a long double argument
 */
FIRM_API ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode);
212
213
214
215
216
217
218

/**
 * This returns a double with the value represented value, or
 * gibberish, depending on the size of double and the size of the
 * stored value.
 * This will overflow silently, so use only if you know what
 * you are doing! (better check with tarval_is_long...)
219
220
 *
 * @param tv    the tarval
221
 */
222
223
224
225
226
227
FIRM_API double get_tarval_double(ir_tarval *tv);

/**
 * same as get_tarval_double but returns a long double value
 */
FIRM_API long double get_tarval_long_double(ir_tarval *tv);
228
229
230
231
232

/**
 * This validates if tarval_to_double() will return a satisfying
 * result. I.e. if tv is an float_number and between min, max
 * of double
233
234
 *
 * @param tv    the tarval
235
 */
Matthias Braun's avatar
Matthias Braun committed
236
FIRM_API int tarval_is_double(ir_tarval *tv);
237

238
239
240

/** ********** Access routines for tarval fields ********** **/

241
/*
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 * NAME
 *   get_tarval_mode
 *   get_tarval_ ...
 *
 * SYNOPSIS
 *   ir_mode *get_tarval_mode(tarval *tv)
 *   ...
 *
 * DESCRIPTION
 *    These are access function for tarval struct members. It is encouraged
 *   to use them instead of direct access to the struct fields.
 *
 * PARAMETERS
 *   tv - The tarval to access fields of
 *
 * RESULT
 *   get_tv_mode: The mode of the tarval
 *
 * SEE ALSO
 *   the struct tarval
262
263
 */

264
265
266
267
268
/**
 * Returns the mode of the tarval.
 *
 * @param tv    the tarval
 */
Matthias Braun's avatar
Matthias Braun committed
269
FIRM_API ir_mode *get_tarval_mode(const ir_tarval *tv);
270

Michael Beck's avatar
Michael Beck committed
271
272
/**
 * Returns 1 if tv is negative
273
 *
274
 * @param tv    the tarval
275
 */
Matthias Braun's avatar
Matthias Braun committed
276
FIRM_API int tarval_is_negative(ir_tarval *tv);
277

Michael Beck's avatar
Michael Beck committed
278
279
280
/**
 * Returns 1 if tv is null
 *
281
 * @param tv    the tarval
Michael Beck's avatar
Michael Beck committed
282
 */
Matthias Braun's avatar
Matthias Braun committed
283
FIRM_API int tarval_is_null(ir_tarval *tv);
Michael Beck's avatar
Michael Beck committed
284

Michael Beck's avatar
Michael Beck committed
285
286
287
/**
 * Returns 1 if tv is the "one"
 *
288
 * @param tv    the tarval
Michael Beck's avatar
Michael Beck committed
289
 */
Matthias Braun's avatar
Matthias Braun committed
290
FIRM_API int tarval_is_one(ir_tarval *tv);
Michael Beck's avatar
Michael Beck committed
291

Michael Beck's avatar
Michael Beck committed
292
293
294
/**
 * Returns 1 if tv is the "minus one"
 *
295
 * @param tv    the tarval
Michael Beck's avatar
Michael Beck committed
296
 */
Matthias Braun's avatar
Matthias Braun committed
297
FIRM_API int tarval_is_minus_one(ir_tarval *tv);
Michael Beck's avatar
Michael Beck committed
298

299
/**
300
301
 * returns non-zero if all bits in the tarval are set
 */
Matthias Braun's avatar
Matthias Braun committed
302
FIRM_API int tarval_is_all_one(ir_tarval *tv);
303

304
305
306
307
/**
 * Return non-zero if the tarval is a constant (ie. NOT
 * a reserved tarval like bad, undef, reachable etc.)
 */
Matthias Braun's avatar
Matthias Braun committed
308
FIRM_API int tarval_is_constant(ir_tarval *tv);
309

310
/** The 'bad' tarval. */
Matthias Braun's avatar
Matthias Braun committed
311
FIRM_API ir_tarval *tarval_bad;
312
/** Returns the 'bad' tarval. */
Matthias Braun's avatar
Matthias Braun committed
313
FIRM_API ir_tarval *get_tarval_bad(void);
314

315
/** The 'undefined' tarval. */
Matthias Braun's avatar
Matthias Braun committed
316
FIRM_API ir_tarval *tarval_undefined;
317
/** Returns the 'undefined' tarval. */
Matthias Braun's avatar
Matthias Braun committed
318
FIRM_API ir_tarval *get_tarval_undefined(void);
319
320

/** The mode_b tarval 'false'. */
Matthias Braun's avatar
Matthias Braun committed
321
FIRM_API ir_tarval *tarval_b_false;
322
/** Returns the mode_b tarval 'false'. */
Matthias Braun's avatar
Matthias Braun committed
323
FIRM_API ir_tarval *get_tarval_b_false(void);
324
325

/** The mode_b tarval 'true'. */
Matthias Braun's avatar
Matthias Braun committed
326
FIRM_API ir_tarval *tarval_b_true;
327
/** Returns the mode_b tarval 'true'. */
Matthias Braun's avatar
Matthias Braun committed
328
FIRM_API ir_tarval *get_tarval_b_true(void);
329

330
/** The mode_X tarval 'unreachable'. */
Matthias Braun's avatar
Matthias Braun committed
331
FIRM_API ir_tarval *tarval_unreachable;
332
/** Returns the mode_X tarval 'unreachable'. */
Matthias Braun's avatar
Matthias Braun committed
333
FIRM_API ir_tarval *get_tarval_unreachable(void);
334
335

/** The mode_X tarval 'reachable'. */
Matthias Braun's avatar
Matthias Braun committed
336
FIRM_API ir_tarval *tarval_reachable;
337
/** Returns the mode_X tarval 'reachable'. */
Matthias Braun's avatar
Matthias Braun committed
338
FIRM_API ir_tarval *get_tarval_reachable(void);
339

340
341
342
343
344
345
346
347
348
349
/** The 'top' tarval. This is just another name for the 'undefined' tarval. */
#define tarval_top          tarval_undefined
/** Returns the 'top' tarval. */
#define get_tarval_top()    get_tarval_undefined()

/** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
#define tarval_bottom       tarval_bad
/** Returns the 'bottom' tarval. */
#define get_tarval_bottom() get_tarval_bad()

350
351
352
353
/* These functions calculate and return a tarval representing the requested
 * value.
 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
 * functions, but these are stored on initialization of the irmode module and
Michael Beck's avatar
Michael Beck committed
354
 * therefore the irmode functions should be preferred to the functions below. */
355
356

/** Returns the maximum value of a given mode. */
Matthias Braun's avatar
Matthias Braun committed
357
FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
358
359

/** Returns the minimum value of a given mode. */
Matthias Braun's avatar
Matthias Braun committed
360
FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
361

362
363
/** Returns the 0 value (additive neutral) of a given mode.
    For reference modes, the NULL value is returned (old tarval_P_void) */
Matthias Braun's avatar
Matthias Braun committed
364
FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
365
366

/** Returns the 1 value (multiplicative neutral) of a given mode. */
Matthias Braun's avatar
Matthias Braun committed
367
FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
368

369
370
/** Returns the -1 value (multiplicative neutral) of a given mode.
 *  Returns tarval bad for unsigned modes */
Matthias Braun's avatar
Matthias Braun committed
371
FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
372

Matthias Braun's avatar
Matthias Braun committed
373
374
/** returns the value where all bits are 1 of a given mode.
 * returns tarval_bad for float modes */
Matthias Braun's avatar
Matthias Braun committed
375
FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
Matthias Braun's avatar
Matthias Braun committed
376

377
/** Return quite nan for float_number modes. */
Matthias Braun's avatar
Matthias Braun committed
378
FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
379
380

/** Return +inf for float_number modes. */
Matthias Braun's avatar
Matthias Braun committed
381
FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
382

383
/** Return -inf for float_number modes. */
Matthias Braun's avatar
Matthias Braun committed
384
FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
385
386

/* ******************** Arithmetic operations on tarvals ******************** */
387

388
typedef enum tarval_int_overflow_mode_t {
389
390
391
	TV_OVERFLOW_BAD,      /**< tarval module will return tarval_bad if a overflow occurs */
	TV_OVERFLOW_WRAP,     /**< tarval module will overflow will be ignored, wrap around occurs */
	TV_OVERFLOW_SATURATE  /**< tarval module will saturate the overflow */
392
393
394
395
} tarval_int_overflow_mode_t;

/**
 * Sets the overflow mode for integer operations.
396
397
 *
 * @param ov_mode  one of teh overflow modes
398
 */
Michael Beck's avatar
Michael Beck committed
399
FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
400
401
402
403

/**
 * Get the overflow mode for integer operations.
 */
Michael Beck's avatar
Michael Beck committed
404
FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
405

406
407
/**
 * Compares two tarvals
408
 *
Michael Beck's avatar
Michael Beck committed
409
 * Compare a with b and return a pn_Cmp describing the relation
Michael Beck's avatar
Michael Beck committed
410
 * between a and b.  This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
411
 * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
412
 *
413
414
 * @param a   the first tarval to be compared
 * @param b   the second tarval to be compared
415
 *
416
 * @return
Michael Beck's avatar
Michael Beck committed
417
 *   The pn_Cmp best describing the relation between a and b is returned.
418
 *   This means the mode with the least bits set is returned, e.g. if the
Michael Beck's avatar
Michael Beck committed
419
 *   tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
420
421
 *   indicates 'greater or equal'
 *
422
 * @sa
Michael Beck's avatar
Michael Beck committed
423
 *    irnode.h for the definition of pn_Cmp
424
 */
Matthias Braun's avatar
Matthias Braun committed
425
FIRM_API pn_Cmp tarval_cmp(ir_tarval *a, ir_tarval *b);
426

427
428
/**
 * Converts a tarval to another mode.
429
 *
Michael Beck's avatar
Michael Beck committed
430
 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
431
432
 * 'mode' is wider than the mode of src, as defined in the firm documentation
 * and as returned by the function mode_is_smaller defined in irmode.h.
433
 *
434
435
 * @param src    The tarval to convert
 * @param mode   Tho mode to convert to
436
 *
437
438
 * @return
 *   If a tarval of mode 'mode' with the result of the conversion of the 'src'
439
440
441
 *   tarvals value already exists, it will be returned, else a new tarval is
 *   constructed and returned
 *
442
 * @note
443
 *    Illegal convertions will trigger a panic
444
 *
445
 * @sa
446
447
 *    FIRM documentation for conversion rules
 *    mode_is_smaller defined in irmode.h
448
 */
Matthias Braun's avatar
Matthias Braun committed
449
FIRM_API ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *mode);
450

451
452
453
/*
 * These function implement basic computations representable as opcodes
 * in FIRM nodes.
454
455
456
457
458
459
 *
 * PARAMETERS
 *    tarval_neg:
 *    traval_abs:
 *      a - the tarval to operate on
 *
Michael Beck's avatar
Michael Beck committed
460
 *    all others:
461
462
463
464
 *      a - the first operand tarval
 *      b - the second operand tarval
 *
 * RESULT
Michael Beck's avatar
Michael Beck committed
465
 *    If necessary a new tarval is constructed for the resulting value,
466
467
468
469
 *   or the one already carrying the computation result is retrieved and
 *   returned as result.
 *
 * NOTES
470
 *   The order the arguments are given in is important, imagine postfix
471
 *   notation.
472
473
474
475
 *   Illegal operations will trigger an assertion.
 *   The sort member of the struct mode defines which operations are valid
 */

476
477
478
479
480
481
482
/**
 * Bitwise Negation of a tarval.
 *
 * @param a  the first tarval
 *
 * @return ~a or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
483
FIRM_API ir_tarval *tarval_not(ir_tarval *a);
484

485
486
487
488
489
490
491
/**
 * Arithmetic Negation of a tarval.
 *
 * @param a  the first tarval
 *
 * @return -a or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
492
FIRM_API ir_tarval *tarval_neg(ir_tarval *a);
493

494
495
496
497
498
499
500
501
/**
 * Addition of two tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a + b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
502
FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b);
503

504
505
506
507
508
509
510
511
512
/**
 * Subtraction from a tarval.
 *
 * @param a         the first tarval
 * @param b         the second tarval
 * @param dst_mode  the mode of the result, needed for mode_P - mode_P, else NULL
 *
 * @return a - b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
513
FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode);
514

515
516
517
518
519
520
521
522
/**
 * Multiplication of tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a * b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
523
FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
524

525
526
527
528
529
530
531
532
/**
 * Division of two floating point tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a / b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
533
FIRM_API ir_tarval *tarval_quo(ir_tarval *a, ir_tarval *b);
534

535
536
537
538
539
540
541
542
/**
 * Integer division of two tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a / b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
543
FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
544

545
546
547
548
549
550
551
552
/**
 * Remainder of integer division.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a % b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
553
FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
554

555
556
557
558
559
560
561
562
563
/**
 * Integer division AND remainder.
 *
 * @param a        the first tarval
 * @param b        the second tarval
 * @param mod_res  after return, contains the remainder result, a % b or tarval_bad
 *
 * @return a / b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
564
FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
Michael Beck's avatar
Michael Beck committed
565

566
567
568
569
570
571
572
/**
 * Absolute value of a tarval.
 *
 * @param a  the first tarval
 *
 * @return |a| or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
573
FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
574

575
576
577
578
579
580
581
582
/**
 * Bitwise and of two integer tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a & b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
583
FIRM_API ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b);
584

585
586
587
588
589
590
591
592
/**
 * Bitwise and not of two integer tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a & ~b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
593
FIRM_API ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b);
594

595
596
597
598
599
600
601
602
/**
 * Bitwise or of two integer tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a | b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
603
FIRM_API ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b);
604

605
606
607
608
609
610
611
612
/**
 * Bitwise exclusive or of two integer tarvals.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a ^ b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
613
FIRM_API ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b);
614

615
616
617
618
619
620
621
622
/**
 * Logical Left shift.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a << b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
623
FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
624

625
626
627
628
629
630
631
632
/**
 * Unsigned (logical) right shift.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a >>u b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
633
FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
634

635
636
637
638
639
640
641
642
/**
 * Signed (arithmetic) right shift.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
 * @return a >>s b or tarval_bad
 */
Matthias Braun's avatar
Matthias Braun committed
643
FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
644

645
646
647
648
649
650
/**
 * Rotation to left.
 *
 * @param a  the first tarval
 * @param b  the second tarval
 *
651
 * @return a \<\<L\>\> b or tarval_bad
652
 */
Matthias Braun's avatar
Matthias Braun committed
653
FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
654

655
656
657
/**
 * Returns the carry flag of the last operation.
 */
Michael Beck's avatar
Michael Beck committed
658
FIRM_API int tarval_carry(void);
659

660
661
/* *********** Output of tarvals *********** */

Michael Beck's avatar
Michael Beck committed
662
663
664
665
666
667
/**
 * The output mode for tarval values.
 *
 * Some modes allow more that one representation, for instance integers
 * can be represented hex or decimal. Of course it would be enough to have
 * one and let every backend convert it into the 'right' one.
Michael Beck's avatar
Michael Beck committed
668
 * However, we can do this in the tarval much simpler...
Michael Beck's avatar
Michael Beck committed
669
 */
670
typedef enum {
671
672
673
674
675
676
677
	TVO_NATIVE,       /**< the default output mode, depends on the mode */
	TVO_HEX,          /**< use hex representation, always possible */
	TVO_DECIMAL,      /**< use decimal representation */
	TVO_OCTAL,        /**< use octal representation */
	TVO_BINARY,       /**< use binary representation */
	TVO_FLOAT,        /**< use floating point representation (i.e 1.342e-2)*/
	TVO_HEXFLOAT      /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
678
} tv_output_mode;
Michael Beck's avatar
Michael Beck committed
679
680
681

/**
 * This structure contains helper information to format the output
Götz Lindenmaier's avatar
Götz Lindenmaier committed
682
 * of a tarval of a mode.
Michael Beck's avatar
Michael Beck committed
683
684
 */
typedef struct tarval_mode_info {
685
686
687
688
689
	tv_output_mode mode_output;  /**< if != TVO_NATIVE select a special mode */
	const char *mode_prefix;     /**< if set, this prefix will be printed
	                                  before a value of this mode */
	const char *mode_suffix;     /**< if set, this suffix will be printed
	                                  after a value of this mode */
Michael Beck's avatar
Michael Beck committed
690
691
692
693
694
} tarval_mode_info;

/**
 * Specify the output options of one mode.
 *
Michael Beck's avatar
Michael Beck committed
695
 * This functions stores the mode info, so DO NOT DESTROY it.
Michael Beck's avatar
Michael Beck committed
696
 *
697
698
 * @param mode      a ir_mode that should be associated
 * @param modeinfo  the output format info
Michael Beck's avatar
Michael Beck committed
699
 *
Michael Beck's avatar
fixed:    
Michael Beck committed
700
 * @return zero on success.
Michael Beck's avatar
Michael Beck committed
701
 */
Michael Beck's avatar
Michael Beck committed
702
FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
703
                                           const tarval_mode_info *modeinfo);
Michael Beck's avatar
Michael Beck committed
704

Michael Beck's avatar
fixed:    
Michael Beck committed
705
706
707
/**
 * Returns the output options of one mode.
 *
Michael Beck's avatar
Michael Beck committed
708
 * This functions returns the mode info of a given mode.
Michael Beck's avatar
fixed:    
Michael Beck committed
709
 *
710
 * @param mode      a ir_mode that should be associated
Michael Beck's avatar
fixed:    
Michael Beck committed
711
712
713
 *
 * @return the output option
 */
Michael Beck's avatar
Michael Beck committed
714
FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
Michael Beck's avatar
fixed:    
Michael Beck committed
715

716
717
/**
 * Returns Bit representation of a tarval value, as string of '0' and '1'
718
 *
719
 * @param tv   The tarval
720
 *
721
722
 * This function returns a printable bit representation of any value
 * stored as tarval. This representation is a null terminated C string.
723
 *
724
725
 * @return
 *   As usual in C a pointer to a char is returned. The length of the
726
727
728
 *   returned string if fixed, just read as many chars as the mode defines
 *   as size.
 *
729
730
731
 * @note
 *   The string is allocated using malloc() and is free()ed on the next call
 *   of this function.
Michael Beck's avatar
Michael Beck committed
732
 *   The string consists of the ASCII characters '0' and '1' and is
733
 *   null terminated
734
 *
735
 * @sa
736
737
 *    irmode.h for the definition of the ir_mode struct
 *    the size member of aforementioned struct
738
 */
Matthias Braun's avatar
Matthias Braun committed
739
FIRM_API char *get_tarval_bitpattern(ir_tarval *tv);
740

741
742
743
744
745
746
747
748
/**
 * Returns the bitpattern of the bytes_ofs byte.
 *
 * This function succeeds even if the mode of the tarval uses lesser bits
 * than requested, in that case the bitpattern is filled with zero bits.
 *
 * To query a 32bit value the following code can be used:
 *
749
 * val0 = tarval_sub_bits(tv, 0);  <- lowest bits
750
751
 * val1 = tarval_sub_bits(tv, 1);
 * val2 = tarval_sub_bits(tv, 2);
752
 * val3 = tarval_sub_bits(tv, 3);  <- highest bits
753
754
755
756
 *
 * Because this is the bit representation of the target machine, only the following
 * operations are legal on the result:
 *
Michael Beck's avatar
Michael Beck committed
757
 * - concatenation (endian dependence MUST be handled by the CALLER)
758
 * - bitwise logical operations to select/mask bits
759
 *
760
 * @param tv        the tarval
761
 * @param byte_ofs  the byte offset from lower to higher
762
763
 *
 * @note
Michael Beck's avatar
Michael Beck committed
764
 *   The result of this function is undefined if the mode is neither integer nor float.
765
 */
Matthias Braun's avatar
Matthias Braun committed
766
FIRM_API unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs);
767

768
769
770
/**
 * Returns non-zero if a given (integer) tarval has only one single bit
 * set.
771
772
 *
 * @param tv    the tarval
773
 */
Matthias Braun's avatar
Matthias Braun committed
774
FIRM_API int tarval_is_single_bit(ir_tarval *tv);
775

776
777
778
779
780
781
782
/**
 * Return the number of set bits in a given (integer) tarval.
 *
 * @param tv    the tarval
 *
 * @return number of set bits or -1 on error
 */
Matthias Braun's avatar
Matthias Braun committed
783
FIRM_API int get_tarval_popcount(ir_tarval *tv);
784
785
786
787
788
789
790
791

/**
 * Return the number of the lowest set bit in a given (integer) tarval.
 *
 * @param tv    the tarval
 *
 * @return number of lowest set bit or -1 on error
 */
Matthias Braun's avatar
Matthias Braun committed
792
FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
793

794
/**
795
796
797
798
799
 * Output a tarval to a string buffer.
 *
 * @param buf     the output buffer
 * @param buflen  the length of the buffer
 * @param tv      the tarval
800
 */
Matthias Braun's avatar
Matthias Braun committed
801
FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
802

803
/**
804
805
806
 * Output a tarval to stdio.
 *
 * @param tv    the tarval
807
 */
Matthias Braun's avatar
Matthias Braun committed
808
FIRM_API int tarval_printf(ir_tarval *tv);
809

Michael Beck's avatar
Michael Beck committed
810
811
812
/**
 * Returns non-zero if the mantissa of a floating point IEEE-754
 * tarval is zero (i.e. 1.0Exxx)
813
814
 *
 * @param tv    the tarval
Michael Beck's avatar
Michael Beck committed
815
 */
Matthias Braun's avatar
Matthias Braun committed
816
FIRM_API int tarval_ieee754_zero_mantissa(ir_tarval *tv);
Michael Beck's avatar
Michael Beck committed
817
818
819
820

/**
 * Returns the exponent of a floating point IEEE-754
 * tarval.
821
822
 *
 * @param tv    the tarval
Michael Beck's avatar
Michael Beck committed
823
 */
Matthias Braun's avatar
Matthias Braun committed
824
FIRM_API int tarval_ieee754_get_exponent(ir_tarval *tv);
Michael Beck's avatar
Michael Beck committed
825

826
827
828
829
830
/**
 * Check if the tarval can be converted to the given mode without
 * precision loss.
 *
 * @param tv    the tarval
831
 * @param mode  the mode to convert to
832
 */
Matthias Braun's avatar
Matthias Braun committed
833
FIRM_API int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode);
834

835
836
837
838
839
840
841
/**
 * Set the immediate precision for IEEE-754 results. Set this to
 * 0 to get the same precision as the operands.
 * For x87 compatibility, set this to 80.
 *
 * @return the old setting
 */
Michael Beck's avatar
Michael Beck committed
842
FIRM_API unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
843

844
845
846
/**
 *  Returns non-zero if the result of the last IEEE-754 operation was exact.
 */
Michael Beck's avatar
Michael Beck committed
847
FIRM_API unsigned tarval_ieee754_get_exact(void);
848

849
850
851
852
/**
 * Return the size of the mantissa in bits (including possible
 * implicit bits) for the given mode.
 */
Michael Beck's avatar
Michael Beck committed
853
FIRM_API unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
854

855
856
857
/**
 * Enable/Disable floating point constant folding.
 */
Michael Beck's avatar
Michael Beck committed
858
FIRM_API void tarval_enable_fp_ops(int enable);
859
860

/** returns 0/1 if floating point folding is enable/disabled */
Michael Beck's avatar
Michael Beck committed
861
FIRM_API int tarval_fp_ops_enabled(void);
862

863
864
/**
 * Check if its the a floating point NaN.
865
866
 *
 * @param tv    the tarval
867
 */
Matthias Braun's avatar
Matthias Braun committed
868
FIRM_API int tarval_is_NaN(ir_tarval *tv);
869
870
871

/**
 * Check if its the a floating point +inf.
872
873
 *
 * @param tv    the tarval
874
 */
Matthias Braun's avatar
Matthias Braun committed
875
FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
876
877
878

/**
 * Check if its the a floating point -inf.
879
880
 *
 * @param tv    the tarval
881
 */
Matthias Braun's avatar
Matthias Braun committed
882
FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
883
884
885

/**
 * Check if the tarval represents a finite value, ie neither NaN nor inf.
886
887
 *
 * @param tv    the tarval
888
 */
Matthias Braun's avatar
Matthias Braun committed
889
FIRM_API int tarval_is_finite(ir_tarval *tv);
890

Michael Beck's avatar
Michael Beck committed
891
892
893
894
895
896
897
898
/**
 *   Checks whether a pointer points to a tarval.
 *
 *   @param thing     an arbitrary pointer
 *
 *   @return
 *       true if the thing is a tarval, else false
 */
Michael Beck's avatar
Michael Beck committed
899
FIRM_API int is_tarval(const void *thing);
900
901

#include "end.h"
Michael Beck's avatar
Michael Beck committed
902

903
#endif