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 
81 /* hashtab stuff */
82 #define HASHSIZE 9973
83 #define HASH_CON(p) ((p[0] ^ p[1]) % HASHSIZE)
84 #define HASH_ID(hv, p, len)                            \
85   hv = p[(len)-1] | (*p << 16) | (p[(len) >> 1] << 8); \
86   if ((int)(len) > 3)                                  \
87     hv ^= (p[1] << 4);                                 \
88   hv %= HASHSIZE;
89 #define HASH_STR(hv, p, len)     \
90   if (len) {                     \
91     /*hv =*/HASH_ID(hv, p, len); \
92   } else                         \
93     hv = 0;
94 
95 /* limits */
96 #define MAX_NMPTR 134217728
97 
98 /* for exclusive use by NEWSYM */
99 void realloc_sym_storage();
100 
101 /* symbol creation macros */
102 #define NEWSYM(sptr)         \
103   sptr = (SPTR)STG_NEXT(stb);
104 
105 #define LINKSYM(sptr, hashval)        \
106   HASHLKP(sptr, stb.hashtb[hashval]); \
107   stb.hashtb[hashval] = sptr
108 
109 #define ADDSYM(sptr, hashval) \
110   NEWSYM(sptr);               \
111   LINKSYM(sptr, hashval)
112 
113 /*  symbol table typedef declarations */
114 
115 typedef struct SYM {
116   SYMTYPE stype : 8;
117   SC_KIND sc : 8;
118   unsigned b3 : 8;
119   unsigned b4 : 8;
120   INT dtype;
121   SPTR hashlk;
122   SPTR symlk;
123   INT scope;
124   INT nmptr;
125   unsigned f1 : 1, f2 : 1, f3 : 1, f4 : 1, f5 : 1, f6 : 1, f7 : 1, f8 : 1;
126   unsigned f9 : 1, f10 : 1, f11 : 1, f12 : 1, f13 : 1, f14 : 1, f15 : 1, f16 : 1;
127   unsigned f17 : 1, f18 : 1, f19 : 1, f20 : 1, f21 : 1, f22 : 1, f23 : 1, f24 : 1;
128   unsigned f25 : 1, f26 : 1, f27 : 1, f28 : 1, f29 : 1, f30 : 1, f31 : 1, f32 : 1;
129   unsigned f33 : 1, f34 : 1, f35 : 1, f36 : 1, f37 : 1, f38 : 1, f39 : 1, f40 : 1;
130   unsigned f41 : 1, f42 : 1, f43 : 1, f44 : 1, f45 : 1, f46 : 1, f47 : 1, f48 : 1;
131   unsigned f49 : 1, f50 : 1, f51 : 1, f52 : 1, f53 : 1, f54 : 1, f55 : 1, f56 : 1;
132   unsigned f57 : 1, f58 : 1, f59 : 1, f60 : 1, f61 : 1, f62 : 1, f63 : 1, f64 : 1;
133   INT w9;
134   ISZ_T w10;
135   INT w11;
136   INT w12;
137   INT w13;
138   ISZ_T w14;
139   INT w15;
140   INT w16;
141   INT w17;
142   INT w18;
143   INT w19;
144   INT w20;
145   INT w21;
146   INT w22;
147   INT w23;
148   INT w24;
149   INT w25;
150   INT w26;
151   INT w27;
152   INT w28;
153   INT uname;
154   INT w30;
155   INT w31;
156   INT w32;
157   unsigned f65 : 1, f66 : 1, f67 : 1, f68 : 1, f69 : 1, f70 : 1, f71 : 1, f72 : 1;
158   unsigned f73 : 1, f74 : 1, f75 : 1, f76 : 1, f77 : 1, f78 : 1, f79 : 1, f80 : 1;
159   unsigned f81 : 1, f82 : 1, f83 : 1, f84 : 1, f85 : 1, f86 : 1, f87 : 1, f88 : 1;
160   unsigned f89 : 1, f90 : 1, f91 : 1, f92 : 1, f93 : 1, f94 : 1, f95 : 1, f96 : 1;
161   INT w34;
162   INT w35;
163   INT w36;
164   unsigned f97 : 1, f98 : 1, f99 : 1, f100 : 1, f101 : 1, f102 : 1, f103 : 1,
165       f104 : 1;
166   unsigned f105 : 1, f106 : 1, f107 : 1, f108 : 1, f109 : 1, f110 : 1, f111 : 1,
167       f112 : 1;
168   unsigned f113 : 1, f114 : 1, f115 : 1, f116 : 1, f117 : 1, f118 : 1, f119 : 1,
169       f120 : 1;
170   unsigned f121 : 1, f122 : 1, f123 : 1, f124 : 1, f125 : 1, f126 : 1, f127 : 1,
171       f128 : 1;
172   INT lineno;
173   INT w39;
174   INT w40;
175 } SYM;
176 
177 /*   symbol table data declarations:  */
178 typedef struct {
179   const char *stypes[ST_MAX + 1];
180   OVCLASS ovclass[ST_MAX + 1];
181   const char *ocnames[OC_MAX + 1];
182   const char *scnames[SC_MAX + 1];
183   const char *tynames[TY_MAX + 1];
184   SPTR i0, i1;
185   SPTR k0, k1;
186   SPTR flt0, dbl0, quad0;
187   SPTR fltm0, dblm0, quadm0; /* floating point minus 0 */
188   SPTR flt1, dbl1, quad1;
189   SPTR flt2, dbl2, quad2;
190   SPTR flthalf, dblhalf, quadhalf;
191   struct{
192     STG_MEMBERS(ISZ_T);
193   }dt;
194   int curr_scope;
195   SPTR hashtb[HASHSIZE + 1];
196   SPTR firstusym, firstosym;
197   STG_MEMBERS(SYM);
198   char *n_base;
199   int n_size;
200   int namavl;
201   int lbavail;
202   int lb_string_avail;
203   INT *w_base;
204   int w_size;
205   int wrdavl;
206 #ifdef LONG_DOUBLE_FLOAT128
207   /* __float128 0.0, -0.0, 1.0, .5, and 2.0 */
208   SPTR float128_0, float128_m0, float128_1;
209   SPTR float128_half, float128_2;
210 #endif
211   /* These members are the integer, real, complex, and logical dtypes which
212    * are the default data types for the target.  The value of one of these
213    * members will be one of the respective DT_xxx values.  The macros DT_INT,
214    * DT_REAL, DT_CMPLX, and DT_LOG are #define'd to these members; these
215    * definitions occur in symtab.h.
216    * dt_ptr (macro DT_PTR) is the integer type which should be used for
217    * cray pointers.  The problem is for some machines (SGI), the default
218    * integer type is 32 bits, but the pointer type is 64 bits.
219    */
220   DTYPE dt_int;    /* default integer - DT_INT   */
221   DTYPE dt_real;   /* default real    - DT_REAL  */
222   DTYPE dt_cmplx;  /* default cmplx   - DT_CMPLX */
223   DTYPE dt_log;    /* default logical - DT_LOG   */
224   DTYPE dt_dble;   /* default double precision - DT_DBLE */
225   DTYPE dt_dcmplx; /* default double cmplx - DT_DCMPLX */
226   DTYPE dt_ptr;    /* default pointer integer - DT_PTR */
227   /* The following members are the default integer, real, complex, and
228    * logical dtypes as specified by the user.  Normally these are set to
229    * the target default data types.  However, the user may override these
230    * with options -i8 and/or -r8.
231   */
232   struct {
233     DTYPE dt_int;
234     DTYPE dt_real;
235     DTYPE dt_cmplx;
236     DTYPE dt_log;
237   } user;
238 } STB;
239 
240 extern STB stb;
241 
242 #ifdef __cplusplus
SymConval1(SPTR sptr)243 inline SPTR SymConval1(SPTR sptr) {
244   return static_cast<SPTR>(CONVAL1G(sptr));
245 }
SymConval2(SPTR sptr)246 inline SPTR SymConval2(SPTR sptr) {
247   return static_cast<SPTR>(CONVAL2G(sptr));
248 }
249 #else
250 #define SymConval1 CONVAL1G
251 #define SymConval2 CONVAL2G
252 #endif
253 
254 /** mode parameter for installsym_ex. */
255 typedef enum IS_MODE {
256   /* Create new symbol if it does not already exist. */
257   IS_GET,
258   /* Create new symbol always and do NOT insert it in the hash table. */
259   IS_QUICK
260 } IS_MODE;
261 
262 void sym_init_first(void);
263 SPTR lookupsym(const char *, int);
264 SPTR lookupsymbol(const char *);
265 SPTR lookupsymf(const char *, ...);
266 #define installsym(name, olength) installsym_ex(name, olength, IS_GET)
267 SPTR installsym_ex(const char *name, int olength, IS_MODE mode);
268 int putsname(const char *, int);
269 char *local_sname(char *);
270 void add_fp_constants(void);
271 bool is_flt0(SPTR sptr);
272 bool is_dbl0(SPTR sptr);
273 bool is_quad0(SPTR sptr);
274 bool is_x87_0(SPTR sptr);
275 bool is_doubledouble_0(SPTR sptr);
276 bool is_cmplx_flt0(SPTR sptr);
277 bool is_creal_flt0(SPTR sptr);
278 bool is_cimag_flt0(SPTR sptr);
279 bool is_cmplx_dbl0(SPTR sptr);
280 bool is_cmplx_quad0(SPTR sptr);
281 bool is_cmplx_x87_0(SPTR sptr);
282 bool is_cmplx_doubledouble_0(SPTR sptr);
283 
284 void put_err(int sev, const char *txt);
285 
286 void symini_errfatal(int n);
287 void symini_error(int n, int s, int l, const char *c1, const char *c2);
288 void symini_interr(const char *txt, int val, int sev);
289 
290 #if defined(__cplusplus)
291 }
292 #endif
293 
294 #endif // SYMACC_H_
295