aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/acidtypes/stabs.c
blob: 5ce0d461159029e90d4d2fcd68ff3082e877b6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include <u.h>
#include <errno.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>
#include <stabs.h>
#include <ctype.h>
#include "dat.h"

static jmp_buf kaboom;

static Type *parsename(char*, char**);
static Type *parseinfo(char*, char**);
static int parsenum(char*, int*, int*, char**);
static int parseattr(char*, char**, char**);
static Type *parsedefn(char *p, Type *t, char **pp);
static int parsebound(char**);
static vlong parsebigint(char**);

typedef struct Ftypes Ftypes;
struct Ftypes
{
	Ftypes *down;
	Ftypes *next;
	char *file;
	TypeList *list;
};

Ftypes *fstack;
Ftypes *allftypes;

static char*
estrndup(char *s, int n)
{
	char *t;

	t = emalloc(n+1);
	memmove(t, s, n);
	return t;
}

static char*
mkpath(char *dir, char *name)
{
	char *s;
	if(name[0] == '/' || dir == nil)
		return estrdup(name);
	else{
		s = emalloc(strlen(dir)+strlen(name)+1);
		strcpy(s, dir);
		strcat(s, name);
		return s;
	}
}

static Ftypes*
mkftypes(char *dir, char *name)
{
	Ftypes *f;

	f = emalloc(sizeof(*f));
	f->file = mkpath(dir, name);
	f->next = allftypes;
	allftypes = f;
	return f;
}

static Ftypes*
findftypes(char *dir, char *name)
{
	char *s;
	Ftypes *f, *found;

	found = nil;
	s = mkpath(dir, name);
	for(f=allftypes; f; f=f->next)
		if(strcmp(f->file, s) == 0)
			found = f;
	return found;
}

static void
oops(void)
{
	longjmp(kaboom, 1);
}

/* find a : but skip over :: */
static char*
findcolon(char *p)
{
	while((p = strchr(p, ':')) != nil && *(p+1) == ':')
		p += 2;
	if(p == nil)
		oops();
	return p;
}

static void
semi(char **p)
{
	if(**p != ';')
		oops();
	(*p)++;
}

static void
comma(char **p)
{
	if(**p != ',')
		oops();
	(*p)++;
}

static int
parseint(char **pp)
{
	if(!isdigit((uchar)**pp))
		oops();
	return strtol(*pp, pp, 10);
}

/*
	name ::= symbol_opt info
 */
static Type*
parsename(char *desc, char **pp)
{
	if(*desc == 'c')
		return nil;

	if(isdigit((uchar)*desc) || *desc=='-' || *desc=='(')
		return parseinfo(desc, pp);
	if(*desc == 0)
		oops();
	return parseinfo(desc+1, pp);
}

/*
	info ::= num | num '=' attr* defn
 */
static Type*
parseinfo(char *desc, char **pp)
{
	int n1, n2;
	Type *t;
	char *attr;

	n1 = n2 = 0;
	parsenum(desc, &n1, &n2, &desc);
	t = typebynum(n1, n2);
	if(*desc != '='){
		*pp = desc;
		return t;
	}
	desc++;
	if(fstack)
		fstack->list = mktl(t, fstack->list);
	while(parseattr(desc, &attr, &desc) >= 0){
		if(*attr == 's')
			t->xsizeof = atoi(attr+1)/8;
	}
	return parsedefn(desc, t, pp);
}

/*
	num ::= integer | '(' integer ',' integer ')'
*/
static int
parsenum(char *p, int *n1, int *n2, char **pp)
{
	if(isdigit((uchar)*p)){
		*n1 = strtol(p, &p, 10);
		*n2 = 0;
		*pp = p;
		return 0;
	}
	if(*p == '('){
		*n1 = strtol(p+1, &p, 10);
		if(*p != ',')
			oops();
		*n2 = strtol(p+1, &p, 10);
		if(*p != ')')
			oops();
		*pp = p+1;
		return 0;
	}
	oops();
	return -1;
}

/*
	attr ::= '@' text ';'

	text is 
		'a' integer (alignment)
		'p' integer (pointer class)
		'P' (packed type)
		's' integer (size of type in bits)
		'S' (string instead of array of chars)
*/
static int
parseattr(char *p, char **text, char **pp)
{
	if(*p != '@')
		return -1;
	*text = p+1;
	if((p = strchr(p, ';')) == nil)
		oops();
	*pp = p+1;
	return 0;
}


typedef struct Basic Basic;
struct Basic
{
	int size;
	int fmt;
};

static Basic baseints[] =
{
	0, 0,
/*1*/	4, 'd',	/* int32 */
/*2*/	1, 'd',	/* char8 */
/*3*/	2, 'd',	/* int16 */
/*4*/	4, 'd',	/* long int32 */
/*5*/	1, 'x',	/* uchar8 */
/*6*/	1, 'd',	/* schar8 */
/*7*/	2, 'x',	/* uint16 */
/*8*/	4, 'x',	/* uint32 */
/*9*/	4, 'x',	/* uint32 */
/*10*/	4, 'x',	/* ulong32 */
/*11*/	0, 0,		/* void */
/*12*/	4, 'f',		/* float */
/*13*/	8, 'f',		/* double */
/*14*/	10, 'f',	/* long double */
/*15*/	4, 'd',	/* int32 */
/*16*/	4, 'd',	/* bool32 */	
/*17*/	2, 'f',		/* short real */
/*18*/	4, 'f',		/* real */
/*19*/	4, 'x',	/* stringptr */
/*20*/	1, 'd',	/* character8 */
/*21*/	1, 'x',	/* logical*1 */
/*22*/	2, 'x',	/* logical*2 */
/*23*/	4, 'X',	/* logical*4 */
/*24*/	4, 'X',	/* logical32 */
/*25*/	8, 'F',		/* complex (two single) */
/*26*/	16, 'F',	/* complex (two double) */
/*27*/	1, 'd',	/* integer*1 */
/*28*/	2, 'd',	/* integer*2 */
/*29*/	4, 'd',	/* integer*4 */
/*30*/	2, 'x',	/* wide char */
/*31*/	8, 'd',	/* int64 */
/*32*/	8, 'x',	/* uint64 */
/*33*/	8, 'x',	/* logical*8 */
/*34*/	8, 'd',	/* integer*8 */
};

static Basic basefloats[] =
{
	0,0,
/*1*/	4, 'f',	/* 32-bit */
/*2*/	8, 'f',	/* 64-bit */
/*3*/	8, 'F',	/* complex */
/*4*/	4, 'F',	/* complex16 */
/*5*/	8, 'F', /* complex32 */
/*6*/	10, 'f',	/* long double */
};

/*
	defn ::= info
		| 'b' ('u' | 's') 'c'? width; offset; nbits; 		(builtin, signed/unsigned, char/not, width in bytes, offset & nbits of type)
		| 'w'		(aix wide char type, not used)
		| 'R' fptype; bytes;			(fptype 1=32-bit, 2=64-bit, 3=complex, 4=complex16, 5=complex32, 6=long double)
		| 'g' typeinfo ';' nbits 		(aix floating, not used)
		| 'c' typeinfo ';' nbits		(aix complex, not used)
		| 'b' typeinfo ';' bytes		(ibm, no idea)
		| 'B' typeinfo		(volatile typref)
		| 'd' typeinfo		(file of typeref)
		| 'k' typeinfo		(const typeref)
		| 'M' typeinfo ';' length		(multiple instance type, fortran)
		| 'S' typeinfo		(set, typeref must have small number of values)
		| '*' typeinfo		(pointer to typeref)
		| 'x' ('s'|'u'|'e') name ':'	(struct, union, enum reference.  name can have '::' in it)
		| 'r' typeinfo ';' low ';' high ';'		(subrange.  typeref can be type being defined for base types!)
				low and high are bounds
				if bound is octal power of two, it's a big negative number
		| ('a'|'P') indextypedef arraytypeinfo 	(array, index should be range type)
					indextype is type definition not typeinfo (need not say typenum=)
					P means packed array
		| 'A' arraytypeinfo		(open array (no index bounds))
		| 'D' dims ';' typeinfo		(dims-dimensional dynamic array)
		| 'E' dims ';' typeinfo		(subarray of N-dimensional array)
		| 'n' typeinfo ';' bytes		(max length string)
		| 'z' typeinfo ';' bytes		(no idea what difference is from 'n')
		| 'N'					(pascal stringptr)
		| 'e' (name ':' bigint ',')* ';'	(enum listing)
		| ('s'|'u') bytes (name ':' type ',' bitoffset ',' bitsize ';')* ';'		(struct/union defn)
							tag is given as name in stabs entry, with 'T' symbol
		| 'f' typeinfo ';' 			(function returning type)
		| 'f' rettypeinfo ',' paramcount ';' (typeinfo ',' (0|1) ';')* ';'
		| 'p' paramcount ';' (typeinfo ',' (0|1) ';')* ';'
		| 'F' rettypeinfo ',' paramcount ';' (name ':' typeinfo ',' (0|1) ';')* ';'
		| 'R' paramcount ';' (name ':' typeinfo ',' (0|1) ';')* ';'
							(the 0 or 1 is pass-by-reference vs pass-by-value)
							(the 0 or 1 is pass-by-reference vs pass-by-value)
*/

static Type*
parsedefn(char *p, Type *t, char **pp)
{
	char c, *name;
	int ischar, namelen, n, wid, offset, bits, sign;
	long val;
	Type *tt;

	if(*p == '(' || isdigit((uchar)*p)){
		t->ty = Defer;
		t->sub = parseinfo(p, pp);
		return t;
	}

	switch(c = *p){
	case '-':	/* builtin */
		n = strtol(p+1, &p, 10);
		if(n >= nelem(baseints) || n < 0)
			n = 0;
		t->ty = Base;
		t->xsizeof = baseints[n].size;
		t->printfmt = baseints[n].fmt;
		break;
	case 'b':	/* builtin */
		p++;
		if(*p != 'u' && *p != 's')
			oops();
		sign = (*p == 's');
		p++;
		ischar = 0;
		if(*p == 'c'){
			ischar = 1;
			p++;
		}
		wid = parseint(&p);
		semi(&p);
		offset = parseint(&p);
		semi(&p);
		bits = parseint(&p);
		semi(&p);
		t->ty = Base;
		t->xsizeof = wid;
		if(sign == 1)
			t->printfmt = 'd';
		else
			t->printfmt = 'x';
		USED(bits);
		USED(ischar);
		break;
	case 'R':	/* fp type */
		n = parseint(&p);
		semi(&p);
		wid = parseint(&p);
		semi(&p);
		t->ty = Base;
		t->xsizeof = wid;
		if(n < 0 || n >= nelem(basefloats))
			n = 0;
		t->xsizeof = basefloats[n].size;
		t->printfmt = basefloats[n].fmt;
		break;
	case 'r':	/* subrange */
		t->ty = Range;
		t->sub = parseinfo(p+1, &p);
		if(*(p-1) == ';' && *p != ';')
			p--;
		semi(&p);
		t->lo = parsebound(&p);
		semi(&p);
		t->hi = parsebound(&p);
		semi(&p);
		break;
	case 'B':	/* volatile */
	case 'k':	/* const */
		t->ty = Defer;
		t->sub = parseinfo(p+1, &p);
		break;
	case '*':	/* pointer */
	case 'A':	/* open array */
	case '&':	/* reference */	/* guess - C++? (rob) */
		t->ty = Pointer;
		t->sub = parseinfo(p+1, &p);
		break;
	case 'a':	/* array */		
	case 'P':	/* packed array */
		t->ty = Pointer;
		tt = newtype();
		parsedefn(p+1, tt, &p);	/* index type */
		if(*p == ';')
			p++;
		tt = newtype();
		t->sub = tt;
		parsedefn(p, tt, &p);	/* element type */
		break;
	case 'e':	/* enum listing */
		p++;
		t->sue = 'e';
		t->ty = Enum;
		while(*p != ';'){
			name = p;
			p = findcolon(p)+1;
			namelen = (p-name)-1;
			val = parsebigint(&p);
			comma(&p);
			if(t->n%32 == 0){
				t->tname = erealloc(t->tname, (t->n+32)*sizeof(t->tname[0]));
				t->val = erealloc(t->val, (t->n+32)*sizeof(t->val[0]));
			}
			t->tname[t->n] = estrndup(name, namelen);
			t->val[t->n] = val;
			t->n++;
		}
		semi(&p);
		break;
		
	case 's':	/* struct */
	case 'u':	/* union */
		p++;
		t->sue = c;
		t->ty = Aggr;
		n = parseint(&p);
		while(*p != ';'){
			name = p;
			p = findcolon(p)+1;
			namelen = (p-name)-1;
			tt = parseinfo(p, &p);
			comma(&p);
			offset = parseint(&p);
			comma(&p);
			bits = parseint(&p);
			semi(&p);
			if(t->n%32 == 0){
				t->tname = erealloc(t->tname, (t->n+32)*sizeof(t->tname[0]));
				t->val = erealloc(t->val, (t->n+32)*sizeof(t->val[0]));
				t->t = erealloc(t->t, (t->n+32)*sizeof(t->t[0]));
			}
			t->tname[t->n] = estrndup(name, namelen);
			t->val[t->n] = offset;
			t->t[t->n] = tt;
			t->n++;
		}
		semi(&p);
		break;

	case 'x':	/* struct, union, enum reference */
		p++;
		t->ty = Defer;
		if(*p != 's' && *p != 'u' && *p != 'e')
			oops();
		c = *p;
		name = p+1;
		p = findcolon(p+1);
		name = estrndup(name, p-name);
		t->sub = typebysue(c, name);
		p++;
		break;

#if 0	/* AIX */
	case 'f':	/* function */
	case 'p':	/* procedure */
	case 'F':	/* Pascal function */
	/* case 'R':	/* Pascal procedure */
		/*
		 * Even though we don't use the info, we have
		 * to parse it in case it is embedded in other descriptions.
		 */
		t->ty = Function;
		p++;
		if(c == 'f' || c == 'F'){
			t->sub = parseinfo(p, &p);
			if(*p != ','){
				if(*p == ';')
					p++;
				break;
			}
			comma(&p);
		}
		n = parseint(&p);	/* number of params */
		semi(&p);
		while(*p != ';'){
			if(c == 'F' || c == 'R'){
				name = p;		/* parameter name */
				p = findcolon(p)+1;
			}
			parseinfo(p, &p);	/* param type */
			comma(&p);
			parseint(&p);	/* bool: passed by value? */
			semi(&p);
		}
		semi(&p);
		break;
#endif

	case 'f':	/* static function */
	case 'F':	/* global function */
		t->ty = Function;
		p++;
		t->sub = parseinfo(p, &p);
		break;

	/*
	 * We'll never see any of this stuff.
	 * When we do, we can worry about it.
	 */
	case 'D':	/* n-dimensional array */
	case 'E':	/* subarray of n-dimensional array */
	case 'M':	/* fortran multiple instance type */
	case 'N':	/* pascal string ptr */
	case 'S':	/* set */
	case 'c':	/* aix complex */
	case 'd':	/* file of */
	case 'g':	/* aix float */
	case 'n':	/* max length string */
	case 'w':	/* aix wide char */
	case 'z':	/* another max length string */
	default:
		fprint(2, "unsupported type char %c (%d)\n", *p, *p);
		oops();
	}
	*pp = p;
	return t;
}

/*
	bound ::= 
		'A' offset			(bound is on stack by ref at offset offset from arg list)
		| 'T' offset			(bound is on stack by val at offset offset from arg list)
		| 'a' regnum		(bound passed by reference in register)
		| 't' regnum		(bound passed by value in register)
		| 'J'				(no bound)
		| bigint
*/
static int
parsebound(char **pp)
{
	char *p;
	int n;

	n = 0;
	p = *pp;
	switch(*p){
	case 'A':	/* bound is on stack by reference at offset n from arg list */
	case 'T':	/* bound is on stack by value at offset n from arg list */
	case 'a':	/* bound is passed by reference in register n */
	case 't':	/* bound is passed by value in register n */
		p++;
		parseint(&p);
		break;
	case 'J':	/* no bound */
		p++;
		break;
	default:
		n = parsebigint(&p);
		break;
	}
	*pp = p;
	return n;
}

/*
	bigint ::= '-'? decimal
		| 0 octal
		| -1
 */
static vlong
parsebigint(char **pp)
{
	char *p;
	int n, neg;

	p = *pp;
	if(*p == '0'){
		errno = 0;
		n = strtoll(p, &p, 8);
		if(errno)
			n = 0;
		goto out;
	}
	neg = 0;
	if(*p == '-'){
		neg = 1;
		p++;
	}
	if(!isdigit((uchar)*p))
		oops();
	n = strtol(p, &p, 10);
	if(neg)
		n = -n;

out:
	*pp = p;
	return n;
}

int
stabs2acid(Stab *stabs, Biobuf *b)
{
	volatile int fno, i;
	char c, *desc, *p;
	char *volatile dir, *volatile fn, *volatile name;
	Ftypes *f;
	Type *t, *tt;
	StabSym sym;

	dir = nil;
	fno = 0;
	fn = nil;
	for(i=0; stabsym(stabs, i, &sym)>=0; i++){
		if(verbose)
			print("%d %s\n", sym.type, sym.name);
		switch(sym.type){
		case N_SO:
			if(sym.name){
				if(sym.name[0] && sym.name[strlen(sym.name)-1] == '/')
					dir = sym.name;
			}
			denumber();
			fstack = nil;
			fno = 0;
			break;
		case N_BINCL:
			fno++;
			f = mkftypes(dir, sym.name);
			f->down = fstack;
			fstack = f;
			break;
		case N_EINCL:
			if(fstack)
				fstack = fstack->down;
			break;
		case N_EXCL:
			fno++;
			if((f = findftypes(dir, sym.name)) == nil){
				static int cannotprint;
				
				if(cannotprint++ == 0)
					fprint(2, "cannot find remembered %s\n", sym.name);
				continue;
			}
			renumber(f->list, fno);
			break;
		case N_GSYM:
		case N_FUN:
		case N_PSYM:
		case N_LSYM:
		case N_LCSYM:
		case N_STSYM:
		case N_RSYM:
			name = sym.name;
			if(name == nil){
				if(sym.type==N_FUN)
					fn = nil;
				continue;
			}
			if((p = findcolon(name)) == nil)
				continue;
			name = estrndup(name, p-name);
			desc = ++p;
			c = *desc;
			if(c == 'c'){
				fprint(2, "skip constant %s\n", name);
				continue;
			}
			if(setjmp(kaboom)){
				static int cannotparse;
				
				if(cannotparse++ == 0)
					fprint(2, "cannot parse %s\n", name);
				continue;
			}
			t = parsename(desc, &p);
			if(t == nil)
				continue;
			if(*p != 0){
				static int extradesc;
				
				if(extradesc++ == 0)
					fprint(2, "extra desc '%s' in '%s'\n", p, desc);
			}
			/* void is defined as itself */
			if(t->ty==Defer && t->sub==t && strcmp(name, "void")==0){
				t->ty = Base;
				t->xsizeof = 0;
				t->printfmt = '0';
			}
			if(*name==' ' && *(name+1) == 0)
				*name = 0;
			/* attach names to structs, unions, enums */
			if(c=='T' && *name && t->sue){
				t->suename = name;
				if(t->name == nil)
					t->name = name;
				tt = typebysue(t->sue, name);
				tt->ty = Defer;
				tt->sub = t;
			}
			if(c=='t'){
				tt = newtype();
				tt->ty = Typedef;
				tt->name = name;
				tt->sub = t;
			}
			/* define base c types */
			if(t->ty==None || t->ty==Range){
				if(strcmp(name, "char") == 0){
					t->ty = Base;
					t->xsizeof = 1;
					t->printfmt = 'x';
				}
				if(strcmp(name, "int") == 0){
					t->ty = Base;
					t->xsizeof = 4;
					t->printfmt = 'd';
				}
			}
			/* record declaration in list for later. */
			if(c != 't' && c != 'T')
			switch(sym.type){
			case N_GSYM:
				addsymx(nil, name, t);
				break;
			case N_FUN:
				fn = name;
				break;
			case N_PSYM:
			case N_LSYM:
			case N_LCSYM:
			case N_STSYM:
			case N_RSYM:
				addsymx(fn, name, t);
				break;
			}
			break;
		}
if(1) print("");
	}

	printtypes(b);
	dumpsyms(b);
	freetypes();

	return 0;
}