added Id tag
[libfirm] / ir / tv / tv.h
1 /* Declarations for Target Values.
2    Copyright (C) 1995, 1996 Christian von Roques
3 */
4
5 /* $Id$ */
6
7 /*
8 Discussion of new interface, proposals by Prof. Waite:
9 (email of 13.6.2001)
10 > 1. You say that you plan to replace the tv module.  That replacement is
11 >    absolutely essential for an ANSI C translator:  Section 6.1.3.2 of the
12 >    standard says that the representation of an integer_constant depends
13 >    upon its value as well as any suffixes that are attached to it.  The
14 >    possible Firm modes for such a constant are i, I, l, and L.  The
15 >    current tv module provides only one integer conversion routine, and
16 >    that requires conversion by the client.  Since the type of the value
17 >    argument is long, this may preclude the representation of an unsigned
18 >    long constant.
19 >
20 >    There is a similar problem with floating constants.  Floating
21 >    constants can be suffixed in C, and the mode depends upon the suffix.
22 >    It can indicate that the constant is of type long double, which your
23 >    current tv module is incapable of representing.
24 >
25 >    Your tv module interface accepts two kinds of information: modes and
26 >    values.  Values obtained from the program text might be uninterpreted
27 >    strings, strings interpreted as integers, and strings interpreted as
28 >    reals.  Values provided by the compiler are usually integers.  Modes are
29 >    always Firm modes.  It seems to me that the tv module should provide
30 >    tarval* constructors for three of the four kinds of values.  Each of these
31 >    constructors should have an ir_mode parameter and one or more parameters
32 >    appropriate for the kind of value.  As is currently the case, one
33 >    constructor should be provided for both compiler-generated integers and
34 >    source strings interpreted as integers.  (This avoids problems of
35 >    different conversion radices -- the client does the conversion.)  For
36 >    symmetry, the constructor for source strings interpreted as reals should
37 >    accept a long double parameter and require the client to do the
38 >    conversion.
39
40 */
41
42 #ifndef _TV_H
43 #define _TV_H
44
45 # include "irmode.h"
46 # include "entity.h"
47 # include "bool.h"
48
49 #ifndef _TARVAL_TYPEDEF_
50 #define _TARVAL_TYPEDEF_
51 typedef struct tarval tarval;
52 #endif
53
54 #include "gmp.h"
55 #undef __need_size_t            /* erroneously defined by 1.3.2's gmp.h */
56
57 /* how to represent target types on host */
58 typedef float  tarval_f;
59 typedef double tarval_d;
60 typedef long   tarval_chil;
61 typedef unsigned long tarval_CHIL;
62 typedef MP_INT tarval_Z;
63 typedef struct {
64   /* if ent then xname is missing or mangled from ent,
65      else if xname then xname is a linker symbol that is not mangled
66        from an entity,
67      else this is tarval_p_void.
68      if this tarval describes a symbolic address of another tarval, tv points
69      to this val */
70   const char *xname;
71   entity *ent;
72   tarval *tv;
73 } tarval_p;
74 typedef struct {
75   unsigned char *p;             /* may contain embedded 0, not 0-terminated */
76   size_t n;
77 } tarval_s;
78 typedef tarval_s tarval_B;
79
80 struct tarval {
81   union {
82     tarval_f f;                 /* float */
83     tarval_d d;                 /* double */
84     tarval_chil chil;           /* signed integral */
85     tarval_CHIL CHIL;           /* unsigned integral */
86     tarval_Z Z;                 /* universal int */
87     tarval_p p;                 /* pointer */
88     bool b;                     /* boolean */
89     tarval_B B;                 /* universal bits */
90     tarval_s s;                 /* string */
91   } u;
92   ir_mode *mode;
93 };
94
95 extern tarval *tarval_bad;
96 /* We should have a tarval_undefined */
97 extern tarval *tarval_b_false;
98 extern tarval *tarval_b_true;
99 extern tarval *tarval_d_NaN;
100 extern tarval *tarval_d_Inf;
101 extern tarval *tarval_p_void;
102 extern tarval *tarval_mode_null[];
103 extern tarval *tarval_mode_min[];
104 extern tarval *tarval_mode_max[];
105
106 void tarval_init_1 (void);
107 void tarval_init_2 (void);
108
109 /* Hash function on tarvals */
110 unsigned tarval_hash (tarval *);
111
112 /* ************************ Constructors for tarvals ************************ */
113 tarval *tarval_Z_from_str (const char *, size_t, int base);
114 tarval *tarval_B_from_str (const char *, size_t);
115 tarval *tarval_d_from_str (const char *, size_t);
116 tarval *tarval_s_from_str (const char *, size_t);
117 tarval *tarval_S_from_str (const char *, size_t);
118 tarval *tarval_from_long  (ir_mode *, long);
119 tarval *tarval_p_from_str (const char *);
120 tarval *tarval_p_from_entity (entity *);
121
122 tarval *tarval_convert_to (tarval *, ir_mode *);
123
124 /* Building an irm_C, irm_s, irm_S or irm_B target value step by step. */
125 void tarval_start (void);
126 void tarval_append (const char *, size_t);
127 void tarval_append1 (char);
128 tarval *tarval_finish_as (ir_mode *);
129 tarval *tarval_cancel (void); /* returns tarval_bad */
130
131 \f
132 /* The flags for projecting a comparison result */
133 typedef enum {
134   irpn_False=0,         /* false */
135   irpn_Eq,              /* equal */
136   irpn_Lt,              /* less */
137   irpn_Le,              /* less or equal */
138   irpn_Gt,              /* greater */
139   irpn_Ge,              /* greater of equal */
140   irpn_Lg,              /* less or greater */
141   irpn_Leg,             /* less, equal or greater = ordered */
142   irpn_Uo,              /* unordered */
143   irpn_Ue,              /* unordered or equal */
144   irpn_Ul,              /* unordered or less */
145   irpn_Ule,             /* unordered, less or equal */
146   irpn_Ug,              /* unordered or greater */
147   irpn_Uge,             /* unordered, greater or equal */
148   irpn_Ne,              /* unordered, less or greater = not equal */
149   irpn_True,            /* true */
150   irpn_notmask = irpn_Leg
151 } ir_pncmp;
152
153 /* ******************** Arithmethic operations on tarvals ******************** */
154 /* Compare a with b and return an ir_pncmp describing the relation
155    between a and b.  This is either Uo, Lt, Eq, Gt, or False if a or b
156    are symbolic pointers which can not be compared at all. */
157 ir_pncmp tarval_comp (tarval *a, tarval *b);
158
159 tarval *tarval_neg (tarval *a);
160 tarval *tarval_add (tarval *, tarval *);
161 tarval *tarval_sub (tarval *, tarval *);
162 tarval *tarval_mul (tarval *, tarval *);
163 tarval *tarval_quo (tarval *, tarval *);
164 tarval *tarval_div (tarval *, tarval *);
165 tarval *tarval_mod (tarval *, tarval *);
166 tarval *tarval_abs (tarval *);
167 tarval *tarval_and (tarval *, tarval *);
168 tarval *tarval_or  (tarval *, tarval *);
169 tarval *tarval_eor (tarval *, tarval *);
170 tarval *tarval_shl (tarval *, tarval *);
171 tarval *tarval_shr (tarval *, tarval *);
172
173 /* Identifying some tarvals */
174 long tarval_classify (tarval *);
175 long tarval_ord (tarval *, int *fail);
176
177 /* moved to tv_t.h
178    int tarval_print (XP_PAR1, const xprintf_info *, XP_PARN); */
179
180 /* return a mode-specific value */
181
182 tarval_f tv_val_f (tarval *tv);
183 tarval_d tv_val_d (tarval *tv);
184 tarval_chil tv_val_chil (tarval *tv);
185 tarval_CHIL tv_val_CHIL (tarval *tv);
186 tarval_Z tv_val_Z (tarval *tv);
187 tarval_p tv_val_p (tarval *tv);
188 bool     tv_val_b (tarval *tv);
189 tarval_B tv_val_B (tarval *tv);
190 tarval_s tv_val_s (tarval *tv);
191
192 #ifdef NDEBUG
193 #define TARVAL_VRFY(val) ((void)0)
194 #else
195 #define TARVAL_VRFY(val) _tarval_vrfy ((val))
196 extern void _tarval_vrfy (const tarval *);
197 #endif
198
199 #ifdef STATS
200 void tarval_stats (void);
201 #else
202 #define tarval_stats() ((void)0)
203 #endif
204
205 ir_mode *get_tv_mode (tarval *tv);
206
207 #endif