Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / 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.3 1994/06/24 12:06:02 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                 adds_db_str(sprint(buf,
123                        "r%d;%ld;0",
124                        tp->tp_dbindex,
125                        (long)tp->tp_size));
126                 break;
127
128         /* constructed types ... */
129         case POINTER:
130                 addc_db_str('*');
131                 stb_type(tp->tp_up);
132                 break;
133         case ARRAY:
134                 if (tp->tp_size > 0) {
135                         adds_db_str("ar");
136                         stb_type(int_type);
137                         adds_db_str(sprint(buf, ";0;%ld;", tp->tp_size / tp->tp_up->tp_size - 1));
138                         stb_type(tp->tp_up);
139                 }
140                 break;
141         case ENUM:
142                 if (tp->tp_size < 0) {
143                         adds_db_str(sprint(buf,
144                                            "xe%s:",
145                                            tp->tp_idf->id_text));
146                         tp->tp_dbindex = -tp->tp_dbindex;
147                         break;
148                 }
149                 addc_db_str('e');
150                 {
151                         register struct stack_entry *se = local_level->sl_entry;
152
153                         while (se) {
154                                 register struct def     *edef = se->se_idf->id_def;
155                                 while (edef) {
156                                         if (edef->df_type == tp &&
157                                             edef->df_sc == ENUM) {
158                                                 adds_db_str(sprint(buf,
159                                                         "%s:%ld,",
160                                                         se->se_idf->id_text,
161                                                         edef->df_address));
162                                         }
163                                         edef = edef->next;
164                                 }
165                                 se = se->next;
166                         }
167                 }
168                 addc_db_str(';');
169                 break;
170         case STRUCT:
171         case UNION:
172                 if (tp->tp_size < 0) {
173                         adds_db_str(sprint(buf,
174                                            "x%c%s:",
175                                            tp->tp_fund == STRUCT ? 's' : 'u',
176                                            tp->tp_idf->id_text));
177                         tp->tp_dbindex = -tp->tp_dbindex;
178                         break;
179                 }
180                 adds_db_str(sprint(buf,
181                                    "%c%ld",
182                                    tp->tp_fund == STRUCT ? 's' : 'u',
183                                    tp->tp_size));
184                 {
185                         register struct sdef    *sdef = tp->tp_sdef;
186
187                         while (sdef) {
188                                 adds_db_str(sdef->sd_idf->id_text);
189                                 addc_db_str(':');
190                                 if (sdef->sd_type->tp_fund == FIELD) {
191                                         stb_type(sdef->sd_type->tp_up);
192                                         adds_db_str(sprint(buf,
193                                                 ",%ld,%ld;",
194                                                 sdef->sd_offset*8+sdef->sd_type->tp_field->fd_shift,
195                                                 sdef->sd_type->tp_field->fd_width));
196                                 }
197                                 else {
198                                         stb_type(sdef->sd_type);
199                                         adds_db_str(sprint(buf,
200                                                 ",%ld,%ld;",
201                                                 sdef->sd_offset*8,
202                                                 sdef->sd_type->tp_size*8));
203                                 }
204                                 sdef = sdef->sd_sdef;
205                         }
206                 }
207                 addc_db_str(';');
208                 break;
209         case FUNCTION:
210                 addc_db_str('f');
211                 stb_type(tp->tp_up);
212         }
213 }
214
215 stb_tag(tg, str)
216         register struct tag     *tg;
217         char                    *str;
218 {
219         create_db_str();
220         adds_db_str(str);
221         adds_db_str(":T");
222         stb_type(tg->tg_type);
223         addc_db_str(';');
224         C_ms_stb_cst(db_str.base,
225                      N_LSYM,
226                      tg->tg_type == void_type || tg->tg_type->tp_size >= 32767
227                        ? 0
228                        : (int)tg->tg_type->tp_size,
229                      (arith) 0);
230 }
231
232 stb_typedef(tp, str)
233         register struct type    *tp;
234         char                    *str;
235 {
236         create_db_str();
237         adds_db_str(str);
238         adds_db_str(":t");
239         stb_type(tp);
240         addc_db_str(';');
241         C_ms_stb_cst(db_str.base,
242                      N_LSYM,
243                      tp == void_type || tp->tp_size >= 32767
244                        ? 0
245                        : (int)tp->tp_size,
246                      (arith) 0);
247 }
248
249 stb_string(df, kind, str)
250         register struct def     *df;
251         char                    *str;
252 {
253         register struct type    *tp = df->df_type;
254
255         create_db_str();
256         adds_db_str(str);
257         addc_db_str(':');
258         switch(kind) {
259         case FUNCTION:
260                 addc_db_str(df->df_sc == STATIC ? 'f' : 'F');
261                 stb_type(tp->tp_up);
262                 addc_db_str(';');
263                 C_ms_stb_pnam(db_str.base, N_FUN, 1 /* proclevel */, str);
264                 break;
265         default:
266                 if (df->df_sc == FORMAL ||
267                     (df->df_sc == REGISTER && df->df_address >= 0)) {
268                                                 /* value parameter */
269                         addc_db_str('p');
270                         stb_type(tp);
271                         addc_db_str(';');
272                         C_ms_stb_cst(db_str.base, N_PSYM, 0, df->df_address);
273                 }
274                 else if (df->df_sc != AUTO && df->df_sc != REGISTER) {
275                                                 /* global */
276                         int stabtp = df->df_initialized ? N_STSYM : N_LCSYM;
277                         if (df->df_sc == STATIC) {
278                                 if (df->df_level >= L_LOCAL) {
279                                         addc_db_str('V');
280                                 }
281                                 else {
282                                         addc_db_str('S');
283                                 }
284                         }
285                         else {
286                                 addc_db_str('G');
287                         }
288                         stb_type(tp);
289                         addc_db_str(';');
290                         if (df->df_sc == STATIC && df->df_level >= L_LOCAL) {
291                                 C_ms_stb_dlb(db_str.base, stabtp, 0, (label) df->df_address, (arith) 0);
292                         }
293                         else {
294                                 C_ms_stb_dnam(db_str.base, stabtp, 0, str, (arith) 0);
295                         }
296                 }
297                 else {  /* local variable */
298                         stb_type(tp);   /* assign type num to avoid
299                                                    difficult to parse string */
300                         addc_db_str(';');
301                         C_ms_stb_cst(db_str.base, N_LSYM, 0, df->df_address);
302                 }
303                 break;
304         }
305 }
306
307 #endif /* DBSYMTAB */