Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / stab.c
1 /*
2  * (c) copyright 1990 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7
8 /* D E B U G G E R   S Y M B O L   T A B L E */
9
10 /* $Id: stab.c,v 1.6 1994/06/27 08:02:27 ceriel Exp $ */
11
12 #include        "dbsymtab.h"
13
14 #ifdef  DBSYMTAB
15
16 #include        <alloc.h>
17 #include        <em_arith.h>
18 #include        <em_label.h>
19 #include        <em_code.h>
20 #include        <flt_arith.h>
21 #include        <stb.h>
22
23 #include        "LLlex.h"
24 #include        "stack.h"
25 #include        "def.h"
26 #include        "type.h"
27 #include        "struct.h"
28 #include        "field.h"
29 #include        "idf.h"
30 #include        "Lpars.h"
31 #include        "level.h"
32
33 extern long     full_mask[];
34 extern char     *sprint();
35
36 #define INCR_SIZE       64
37
38 static struct db_str {
39         unsigned        sz;
40         char            *base;
41         char            *currpos;
42 } db_str;
43
44 static
45 create_db_str()
46 {
47         if (! db_str.base) {
48                 db_str.base = Malloc(INCR_SIZE);
49                 db_str.sz = INCR_SIZE;
50         }
51         db_str.currpos = db_str.base;
52 }
53
54 static
55 addc_db_str(c)
56         int     c;
57 {
58         int df = db_str.currpos - db_str.base;
59         if (df >= db_str.sz-1) {
60                 db_str.sz += INCR_SIZE;
61                 db_str.base = Realloc(db_str.base, db_str.sz);
62                 db_str.currpos = db_str.base + df;
63         }
64         *db_str.currpos++ = c;
65         *db_str.currpos = '\0';
66 }
67
68 static
69 adds_db_str(s)
70         char    *s;
71 {
72         while (*s) addc_db_str(*s++);
73 }
74
75 static
76 stb_type(tp)
77         register struct type    *tp;
78 {
79         char            buf[128];
80         static int      stb_count;
81         long            l;
82
83         if (tp->tp_dbindex > 0) {
84                 adds_db_str(sprint(buf, "%d", tp->tp_dbindex));
85                 return;
86         }
87         if (tp->tp_dbindex < 0 && tp->tp_size < 0) {
88                 adds_db_str(sprint(buf, "%d", -tp->tp_dbindex));
89                 return;
90         }
91         if (tp->tp_dbindex <= 0) {
92                 tp->tp_dbindex = ++stb_count;
93         }
94         adds_db_str(sprint(buf, "%d=", tp->tp_dbindex));
95         switch(tp->tp_fund) {
96         /* simple types ... */
97         case VOID:
98                 adds_db_str(sprint(buf, "%d", void_type->tp_dbindex));
99                 break;
100         case INT:
101         case LONG:
102         case CHAR:
103         case SHORT:
104                  l = full_mask[(int)tp->tp_size];
105                 if (tp->tp_unsigned) {
106                         adds_db_str(sprint(buf,
107                                 "r%d;0;%ld",
108                                 tp->tp_dbindex,
109                                 l));
110                 }
111                 else {
112                         l &= ~ (1L << ((int)tp->tp_size * 8 - 1));
113                         adds_db_str(sprint(buf,
114                                 "r%d;%ld;%ld",
115                                 tp->tp_dbindex,
116                                 -l-1,
117                                 l));
118                 }
119                 break;
120         case FLOAT:
121         case DOUBLE:
122         case LNGDBL:
123                 adds_db_str(sprint(buf,
124                        "r%d;%ld;0",
125                        tp->tp_dbindex,
126                        (long)tp->tp_size));
127                 break;
128
129         /* constructed types ... */
130         case POINTER:
131                 addc_db_str('*');
132                 stb_type(tp->tp_up);
133                 break;
134         case ARRAY:
135                 if (tp->tp_size > 0) {
136                         adds_db_str("ar");
137                         stb_type(int_type);
138                         adds_db_str(sprint(buf, ";0;%ld;", tp->tp_size / tp->tp_up->tp_size - 1));
139                         stb_type(tp->tp_up);
140                 }
141                 break;
142         case ENUM:
143                 if (tp->tp_size < 0) {
144                         adds_db_str(sprint(buf,
145                                            "xe%s:",
146                                            tp->tp_idf->id_text));
147                         tp->tp_dbindex = -tp->tp_dbindex;
148                         break;
149                 }
150                 addc_db_str('e');
151                 {
152                         register struct stack_entry *se = local_level->sl_entry;
153
154                         while (se) {
155                                 register struct def     *edef = se->se_idf->id_def;
156                                 while (edef) {
157                                         if (edef->df_type == tp &&
158                                             edef->df_sc == ENUM) {
159                                                 adds_db_str(sprint(buf,
160                                                         "%s:%ld,",
161                                                         se->se_idf->id_text,
162                                                         edef->df_address));
163                                         }
164                                         edef = edef->next;
165                                 }
166                                 se = se->next;
167                         }
168                 }
169                 addc_db_str(';');
170                 break;
171         case STRUCT:
172         case UNION:
173                 if (tp->tp_size < 0) {
174                         adds_db_str(sprint(buf,
175                                            "x%c%s:",
176                                            tp->tp_fund == STRUCT ? 's' : 'u',
177                                            tp->tp_idf->id_text));
178                         tp->tp_dbindex = -tp->tp_dbindex;
179                         break;
180                 }
181                 adds_db_str(sprint(buf,
182                                    "%c%ld",
183                                    tp->tp_fund == STRUCT ? 's' : 'u',
184                                    tp->tp_size));
185                 {
186                         register struct sdef    *sdef = tp->tp_sdef;
187
188                         while (sdef) {
189                                 adds_db_str(sdef->sd_idf->id_text);
190                                 addc_db_str(':');
191                                 if (sdef->sd_type->tp_fund == FIELD) {
192                                         stb_type(sdef->sd_type->tp_up);
193                                         adds_db_str(sprint(buf,
194                                                 ",%ld,%ld;",
195                                                 sdef->sd_offset*8+sdef->sd_type->tp_field->fd_shift,
196                                                 sdef->sd_type->tp_field->fd_width));
197                                 }
198                                 else {
199                                         stb_type(sdef->sd_type);
200                                         adds_db_str(sprint(buf,
201                                                 ",%ld,%ld;",
202                                                 sdef->sd_offset*8,
203                                                 sdef->sd_type->tp_size*8));
204                                 }
205                                 sdef = sdef->sd_sdef;
206                         }
207                 }
208                 addc_db_str(';');
209                 break;
210         case FUNCTION:
211                 addc_db_str('f');
212                 stb_type(tp->tp_up);
213         }
214 }
215
216 stb_tag(tg, str)
217         register struct tag     *tg;
218         char                    *str;
219 {
220         create_db_str();
221         adds_db_str(str);
222         adds_db_str(":T");
223         stb_type(tg->tg_type);
224         addc_db_str(';');
225         C_ms_stb_cst(db_str.base,
226                      N_LSYM,
227                      tg->tg_type == void_type || tg->tg_type->tp_size >= 32767
228                        ? 0
229                        : (int)tg->tg_type->tp_size,
230                      (arith) 0);
231 }
232
233 stb_typedef(tp, str)
234         register struct type    *tp;
235         char                    *str;
236 {
237         create_db_str();
238         adds_db_str(str);
239         adds_db_str(":t");
240         stb_type(tp);
241         addc_db_str(';');
242         C_ms_stb_cst(db_str.base,
243                      N_LSYM,
244                      tp == void_type || tp->tp_size >= 32767
245                        ? 0
246                        : (int)tp->tp_size,
247                      (arith) 0);
248 }
249
250 stb_string(df, kind, str)
251         register struct def     *df;
252         char                    *str;
253 {
254         register struct type    *tp = df->df_type;
255
256         create_db_str();
257         adds_db_str(str);
258         addc_db_str(':');
259         switch(kind) {
260         case FUNCTION:
261                 addc_db_str(df->df_sc == STATIC ? 'f' : 'F');
262                 stb_type(tp->tp_up);
263                 addc_db_str(';');
264                 C_ms_stb_pnam(db_str.base, N_FUN, 1 /* proclevel */, str);
265                 break;
266         default:
267                 if (df->df_sc == FORMAL ||
268                     (df->df_sc == REGISTER && df->df_address >= 0)) {
269                                                 /* value parameter */
270                         addc_db_str('p');
271                         stb_type(tp);
272                         addc_db_str(';');
273                         C_ms_stb_cst(db_str.base, N_PSYM, 0, df->df_address);
274                 }
275                 else if (df->df_sc != AUTO && df->df_sc != REGISTER) {
276                                                 /* global */
277                         int stabtp = df->df_initialized ? N_STSYM : N_LCSYM;
278                         if (df->df_sc == STATIC) {
279                                 if (df->df_level >= L_LOCAL) {
280                                         addc_db_str('V');
281                                 }
282                                 else {
283                                         addc_db_str('S');
284                                 }
285                         }
286                         else {
287                                 addc_db_str('G');
288                         }
289                         stb_type(tp);
290                         addc_db_str(';');
291                         if (df->df_sc == STATIC && df->df_level >= L_LOCAL) {
292                                 C_ms_stb_dlb(db_str.base, stabtp, 0, (label) df->df_address, (arith) 0);
293                         }
294                         else {
295                                 C_ms_stb_dnam(db_str.base, stabtp, 0, str, (arith) 0);
296                         }
297                 }
298                 else {  /* local variable */
299                         stb_type(tp);   /* assign type num to avoid
300                                                    difficult to parse string */
301                         addc_db_str(';');
302                         C_ms_stb_cst(db_str.base, N_LSYM, 0, df->df_address);
303                 }
304                 break;
305         }
306 }
307
308 #endif /* DBSYMTAB */