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