1 /*
2  * Copyright (c) 1994-2018, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef SYMACC_H_
19 #define SYMACC_H_
20 
21 #include "scutil.h"
22 #include "gbldefs.h"
23 #include "global.h"
24 struct SYM;
25 #include "symtab.h"
26 #include "sharedefs.h"
27 
28 /**
29  * \file
30  * \brief Various definitions for symacc.
31  */
32 
33 #ifdef __cplusplus
34 /** Must have same layout as a T* */
35 template <typename T, typename Index> class IndexBy
36 {
37   T *rep;
38 
39 public:
40   T &operator[](Index index)
41   {
42     return rep[index];
43   }
44   /* Rest of operators are required to make macro NEED work.
45      Their functionality is deliberately minimized with intent
46      to minimize accidental use outside macro NEED.  It would
47      be better that "operator void*" have the explicit keyword, but
48      Microsoft 10.0 Open Tools does not support that C++11 feature. */
49   operator char*() const
50   {
51     return reinterpret_cast<char*>(rep);
52   }
53   void operator=(T *ptr)
54   {
55     rep = ptr;
56   }
57   bool operator!() const
58   {
59     return !rep;
60   }
61   void *operator+(int offset) const
62   {
63     return reinterpret_cast<void *>(rep + offset);
64   }
65 };
66 
67 #define INDEX_BY(T, Index) IndexBy<T, Index>
68 #else
69 #define INDEX_BY(T, Index) T *
70 #endif
71 
72 #if defined(__cplusplus)
73 extern "C" {
74 #endif
75 
76 /* FIXME: down the file there are declarations that depend on ST_MAX
77    etc. and not guarded by #ifdef INIT.  Either INIT is always
78    defined or there exist alternative definitions for these values
79    somewhere.  This needs to be unified and cleaned.  */
80 #ifdef INIT
81 #define ST_MAX 1
82 #define SC_MAX 1
83 #define TY_MAX 1
84 #define OC_MAX 1
85 #define NMPTRP(s, v) (stb.stg_base[s].nmptr = (v))
86 #define HASHLKP(s, v) (stb.stg_base[s].hashlk = (v))
87 #define NMPTRG(s) stb.stg_base[s].nmptr
88 #define HASHLKG(s) stb.stg_base[s].hashlk
89 #define SYMNAME(s) (stb.n_base + stb.stg_base[s].nmptr)
90 #endif
91 
92 /* hashtab stuff */
93 #define HASHSIZE 9973
94 #define HASH_CON(p) ((p[0] ^ p[1]) % HASHSIZE)
95 #define HASH_ID(hv, p, len)                            \
96   hv = p[(len)-1] | (*p << 16) | (p[(len) >> 1] << 8); \
97   if ((int)(len) > 3)                                  \
98     hv ^= (p[1] << 4);                                 \
99   hv %= HASHSIZE;
100 #define HASH_STR(hv, p, len)     \
101   if (len) {                     \
102     /*hv =*/HASH_ID(hv, p, len); \
103   } else                         \
104     hv = 0;
105 
106 /* limits */
107 #define MAX_NMPTR 134217728
108 
109 /* for exclusive use by NEWSYM */
110 void realloc_sym_storage();
111 
112 /* symbol creation macros */
113 #ifdef UTILSYMTAB
114 #define NEWSYM(sptr)         \
115   sptr = (SPTR)stb.stg_avail++; \
116   if (sptr >= stb.stg_size)    \
117     realloc_sym_storage();   \
118   BZERO(&stb.stg_base[sptr], char, sizeof(SYM))
119 
120 #else
121 #define NEWSYM(sptr)         \
122   sptr = (SPTR)STG_NEXT(stb);
123 
124 #endif
125 
126 #define LINKSYM(sptr, hashval)        \
127   HASHLKP(sptr, stb.hashtb[hashval]); \
128   stb.hashtb[hashval] = sptr
129 
130 #define ADDSYM(sptr, hashval) \
131   NEWSYM(sptr);               \
132   LINKSYM(sptr, hashval)
133 
134 /*  symbol table typedef declarations */
135 
136 #ifndef PGHPF
137 typedef struct SYM {
138   SYMTYPE stype : 8;
139   SC_KIND sc : 8;
140   unsigned b3 : 8;
141   unsigned b4 : 8;
142   DTYPE dtype;
143   SPTR hashlk;
144   SPTR symlk;
145   INT scope;
146   INT nmptr;
147   unsigned f1 : 1, f2 : 1, f3 : 1, f4 : 1, f5 : 1, f6 : 1, f7 : 1, f8 : 1;
148   unsigned f9 : 1, f10 : 1, f11 : 1, f12 : 1, f13 : 1, f14 : 1, f15 : 1, f16 : 1;
149   unsigned f17 : 1, f18 : 1, f19 : 1, f20 : 1, f21 : 1, f22 : 1, f23 : 1, f24 : 1;
150   unsigned f25 : 1, f26 : 1, f27 : 1, f28 : 1, f29 : 1, f30 : 1, f31 : 1, f32 : 1;
151   INT w8;
152   INT w9;
153   ISZ_T w10;
154   INT w11;
155   INT w12;
156   INT w13;
157   ISZ_T w14;
158   INT w15;
159   INT w16;
160   INT w17;
161   INT w18;
162   unsigned f33 : 1, f34 : 1, f35 : 1, f36 : 1, f37 : 1, f38 : 1, f39 : 1, f40 : 1;
163   unsigned f41 : 1, f42 : 1, f43 : 1, f44 : 1, f45 : 1, f46 : 1, f47 : 1, f48 : 1;
164   unsigned f49 : 1, f50 : 1, f51 : 1, f52 : 1, f53 : 1, f54 : 1, f55 : 1, f56 : 1;
165   unsigned f57 : 1, f58 : 1, f59 : 1, f60 : 1, f61 : 1, f62 : 1, f63 : 1, f64 : 1;
166   INT w20;
167   unsigned f65 : 1, f66 : 1, f67 : 1, f68 : 1, f69 : 1, f70 : 1, f71 : 1, f72 : 1;
168   unsigned f73 : 1, f74 : 1, f75 : 1, f76 : 1, f77 : 1, f78 : 1, f79 : 1, f80 : 1;
169   unsigned f81 : 1, f82 : 1, f83 : 1, f84 : 1, f85 : 1, f86 : 1, f87 : 1, f88 : 1;
170   unsigned f89 : 1, f90 : 1, f91 : 1, f92 : 1, f93 : 1, f94 : 1, f95 : 1, f96 : 1;
171   INT w22;
172   INT w23;
173   INT w24;
174   unsigned f97 : 1, f98 : 1, f99 : 1, f100 : 1, f101 : 1, f102 : 1, f103 : 1,
175       f104 : 1;
176   unsigned f105 : 1, f106 : 1, f107 : 1, f108 : 1, f109 : 1, f110 : 1, f111 : 1,
177       f112 : 1;
178   unsigned f113 : 1, f114 : 1, f115 : 1, f116 : 1, f117 : 1, f118 : 1, f119 : 1,
179       f120 : 1;
180   unsigned f121 : 1, f122 : 1, f123 : 1, f124 : 1, f125 : 1, f126 : 1, f127 : 1,
181       f128 : 1;
182   INT w26;
183   INT w27;
184   INT w28;
185   INT w29;
186   INT w30;
187   INT w31;
188   INT w32;
189 } SYM;
190 #endif
191 
192 #ifdef PGHPF
193 typedef struct SYM {
194   SYMTYPE stype : 8;
195   SC_KIND sc : 8;
196   unsigned b3 : 8;
197   unsigned b4 : 8;
198   INT dtype;
199   SPTR hashlk;
200   SPTR symlk;
201   INT scope;
202   INT nmptr;
203   unsigned f1 : 1, f2 : 1, f3 : 1, f4 : 1, f5 : 1, f6 : 1, f7 : 1, f8 : 1;
204   unsigned f9 : 1, f10 : 1, f11 : 1, f12 : 1, f13 : 1, f14 : 1, f15 : 1, f16 : 1;
205   unsigned f17 : 1, f18 : 1, f19 : 1, f20 : 1, f21 : 1, f22 : 1, f23 : 1, f24 : 1;
206   unsigned f25 : 1, f26 : 1, f27 : 1, f28 : 1, f29 : 1, f30 : 1, f31 : 1, f32 : 1;
207 #if defined(INTIS64)
208   unsigned fldum : 32;
209 #endif
210   unsigned f33 : 1, f34 : 1, f35 : 1, f36 : 1, f37 : 1, f38 : 1, f39 : 1, f40 : 1;
211   unsigned f41 : 1, f42 : 1, f43 : 1, f44 : 1, f45 : 1, f46 : 1, f47 : 1, f48 : 1;
212   unsigned f49 : 1, f50 : 1, f51 : 1, f52 : 1, f53 : 1, f54 : 1, f55 : 1, f56 : 1;
213   unsigned f57 : 1, f58 : 1, f59 : 1, f60 : 1, f61 : 1, f62 : 1, f63 : 1, f64 : 1;
214 #if defined(INTIS64)
215   unsigned fldum2 : 32;
216 #endif
217   INT w9;
218   ISZ_T w10;
219   INT w11;
220   INT w12;
221   INT w13;
222   ISZ_T w14;
223   INT w15;
224   INT w16;
225   INT w17;
226   INT w18;
227   INT w19;
228   INT w20;
229   INT w21;
230   INT w22;
231   INT w23;
232   INT w24;
233   INT w25;
234   INT w26;
235   INT w27;
236   INT w28;
237   INT uname;
238   INT w30;
239   INT w31;
240   INT w32;
241   unsigned f65 : 1, f66 : 1, f67 : 1, f68 : 1, f69 : 1, f70 : 1, f71 : 1, f72 : 1;
242   unsigned f73 : 1, f74 : 1, f75 : 1, f76 : 1, f77 : 1, f78 : 1, f79 : 1, f80 : 1;
243   unsigned f81 : 1, f82 : 1, f83 : 1, f84 : 1, f85 : 1, f86 : 1, f87 : 1, f88 : 1;
244   unsigned f89 : 1, f90 : 1, f91 : 1, f92 : 1, f93 : 1, f94 : 1, f95 : 1, f96 : 1;
245 #if defined(INTIS64)
246   unsigned fldum3 : 32;
247 #endif
248   INT w34;
249   INT w35;
250   INT w36;
251   unsigned f97 : 1, f98 : 1, f99 : 1, f100 : 1, f101 : 1, f102 : 1, f103 : 1,
252       f104 : 1;
253   unsigned f105 : 1, f106 : 1, f107 : 1, f108 : 1, f109 : 1, f110 : 1, f111 : 1,
254       f112 : 1;
255   unsigned f113 : 1, f114 : 1, f115 : 1, f116 : 1, f117 : 1, f118 : 1, f119 : 1,
256       f120 : 1;
257   unsigned f121 : 1, f122 : 1, f123 : 1, f124 : 1, f125 : 1, f126 : 1, f127 : 1,
258       f128 : 1;
259 #if defined(INTIS64)
260   unsigned fldum4 : 32;
261 #endif
262   INT lineno;
263   INT w39;
264   INT w40;
265 } SYM;
266 #endif
267 
268 /*   symbol table data declarations:  */
269 typedef struct {
270   const char *stypes[ST_MAX + 1];
271   OVCLASS ovclass[ST_MAX + 1];
272   const char *ocnames[OC_MAX + 1];
273   const char *scnames[SC_MAX + 1];
274   const char *tynames[TY_MAX + 1];
275   SPTR i0, i1;
276   SPTR k0, k1;
277   SPTR flt0, dbl0, quad0;
278   SPTR fltm0, dblm0, quadm0; /* floating point minus 0 */
279   SPTR flt1, dbl1, quad1;
280   SPTR flt2, dbl2, quad2;
281   SPTR flthalf, dblhalf, quadhalf;
282   struct{
283     STG_MEMBERS(ISZ_T);
284   }dt;
285   int curr_scope;
286   SPTR hashtb[HASHSIZE + 1];
287   SPTR firstusym, firstosym;
288   STG_MEMBERS(SYM);
289   char *n_base;
290   int n_size;
291   int namavl;
292   int lbavail;
293   int lb_string_avail;
294   INT *w_base;
295   int w_size;
296   int wrdavl;
297 #ifdef PGC
298   /* signed/unsigned char: DT_SCHAR/DT_UCHAR.  Macro DT_CHAR is aliased
299    * to this member.  WARNING:  value is not defined until scan_init().
300    */
301   int dt_char;
302 #endif
303 #ifdef LONG_DOUBLE_X87
304   int x87_0, x87_m0, x87_1, x87_2; /* 80-bit X87 0.0, -0.0, and 1.0 */
305 #endif
306 #ifdef DOUBLE_DOUBLE
307   /* double-double 0.0, -0.0, and 1.0 */
308   int doubledouble_0, doubledouble_m0, doubledouble_1;
309 #endif
310 #ifdef LONG_DOUBLE_FLOAT128
311   /* __float128 0.0, -0.0, 1.0, .5, and 2.0 */
312   SPTR float128_0, float128_m0, float128_1;
313   SPTR float128_half, float128_2;
314 #endif
315 #ifdef PGHPF
316   /* These members are the integer, real, complex, and logical dtypes which
317    * are the default data types for the target.  The value of one of these
318    * members will be one of the respective DT_xxx values.  The macros DT_INT,
319    * DT_REAL, DT_CMPLX, and DT_LOG are #define'd to these members; these
320    * definitions occur in symtab.h.
321    * dt_ptr (macro DT_PTR) is the integer type which should be used for
322    * cray pointers.  The problem is for some machines (SGI), the default
323    * integer type is 32 bits, but the pointer type is 64 bits.
324    */
325   DTYPE dt_int;    /* default integer - DT_INT   */
326   DTYPE dt_real;   /* default real    - DT_REAL  */
327   DTYPE dt_cmplx;  /* default cmplx   - DT_CMPLX */
328   DTYPE dt_log;    /* default logical - DT_LOG   */
329   DTYPE dt_dble;   /* default double precision - DT_DBLE */
330   DTYPE dt_dcmplx; /* default double cmplx - DT_DCMPLX */
331   DTYPE dt_ptr;    /* default pointer integer - DT_PTR */
332   /* The following members are the default integer, real, complex, and
333    * logical dtypes as specified by the user.  Normally these are set to
334    * the target default data types.  However, the user may override these
335    * with options -i8 and/or -r8.
336   */
337   struct {
338     DTYPE dt_int;
339     DTYPE dt_real;
340     DTYPE dt_cmplx;
341     DTYPE dt_log;
342   } user;
343 #endif
344 } STB;
345 
346 extern STB stb;
347 
348 #ifdef __cplusplus
SymConval1(SPTR sptr)349 inline SPTR SymConval1(SPTR sptr) {
350   return static_cast<SPTR>(CONVAL1G(sptr));
351 }
SymConval2(SPTR sptr)352 inline SPTR SymConval2(SPTR sptr) {
353   return static_cast<SPTR>(CONVAL2G(sptr));
354 }
355 #else
356 #define SymConval1 CONVAL1G
357 #define SymConval2 CONVAL2G
358 #endif
359 
360 /** mode parameter for installsym_ex. */
361 typedef enum IS_MODE {
362   /* Create new symbol if it does not already exist. */
363   IS_GET,
364   /* Create new symbol always and do NOT insert it in the hash table. */
365   IS_QUICK
366 } IS_MODE;
367 
368 void sym_init_first(void);
369 SPTR lookupsym(const char *, int);
370 SPTR lookupsymbol(const char *);
371 SPTR lookupsymf(const char *, ...);
372 #define installsym(name, olength) installsym_ex(name, olength, IS_GET)
373 SPTR installsym_ex(const char *name, int olength, IS_MODE mode);
374 int putsname(const char *, int);
375 char *local_sname(char *);
376 void add_fp_constants(void);
377 bool is_flt0(SPTR sptr);
378 bool is_dbl0(SPTR sptr);
379 bool is_quad0(SPTR sptr);
380 bool is_x87_0(SPTR sptr);
381 bool is_doubledouble_0(SPTR sptr);
382 bool is_cmplx_flt0(SPTR sptr);
383 bool is_creal_flt0(SPTR sptr);
384 bool is_cimag_flt0(SPTR sptr);
385 bool is_cmplx_dbl0(SPTR sptr);
386 bool is_cmplx_quad0(SPTR sptr);
387 bool is_cmplx_x87_0(SPTR sptr);
388 bool is_cmplx_doubledouble_0(SPTR sptr);
389 
390 void put_err(int sev, const char *txt);
391 
392 #ifdef UTILSYMTAB
393 #undef assert
394 #define assert(cond, txt, val, sev) \
395   if (cond); else symini_interr((txt), (val), (sev))
396 #endif
397 
398 void symini_errfatal(int n);
399 void symini_error(int n, int s, int l, const char *c1, const char *c2);
400 void symini_interr(const char *txt, int val, int sev);
401 
402 #if defined(__cplusplus)
403 }
404 #endif
405 
406 #endif // SYMACC_H_
407