aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/acidtypes/dwarf.c
blob: f92942865ca193fe1c03fc8c9ec9e18fd4cb1d51 (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>
#include <elf.h>
#include <dwarf.h>
#include "dat.h"

static void ds2acid(Dwarf*, DwarfSym*, Biobuf*, char*);

static ulong
valof(uint ty, DwarfVal *v)
{
	switch(ty){
	default:
fmtinstall('H', encodefmt);
fprint(2, "valof %d %.*H\n", ty, v->b.len, v->b.data);
		return 0;
	case TConstant:
		return v->c;
	}
}

static Type*
xnewtype(uint ty, DwarfSym *s)
{
	Type *t;

	t = typebynum(s->unit+s->uoff, 0);
	t->ty = ty;
	return t;
}

int
dwarf2acid(Dwarf *d, Biobuf *b)
{
	DwarfSym s;

	/* pass over dwarf section pulling out type info */

	if(dwarfenum(d, &s) < 0)
		return -1;

	while(dwarfnextsymat(d, &s, 0) == 1)
		ds2acid(d, &s, b, nil);

	printtypes(b);
	dumpsyms(b);
	freetypes();
	return 0;
}

static void
ds2acid(Dwarf *d, DwarfSym *s, Biobuf *b, char *fn)
{
	int depth;
	Type *t;

	depth = s->depth;

	switch(s->attrs.tag){
	case TagSubroutineType:
		t = xnewtype(Function, s);
		goto Recurse;

	case TagSubprogram:
		fn = s->attrs.name;
		goto Recurse;

	case TagCompileUnit:
	case TagLexDwarfBlock:
	Recurse:
		/* recurse into substructure */
		while(dwarfnextsymat(d, s, depth+1) == 1)
			ds2acid(d, s, b, fn);
		break;

	case TagTypedef:
		t = xnewtype(Typedef, s);
		t->name = s->attrs.name;
		t->sub = typebynum(s->attrs.type, 0);
		break;

	case TagBaseType:
		t = xnewtype(Base, s);
		t->xsizeof = s->attrs.bytesize;
		switch(s->attrs.encoding){
		default:
		case TypeAddress:
			t->printfmt = 'x';
			break;
		case TypeBoolean:
		case TypeUnsigned:
		case TypeSigned:
		case TypeSignedChar:
		case TypeUnsignedChar:
			t->printfmt = 'd';
			break;
		case TypeFloat:
			t->printfmt = 'f';
			break;
		case TypeComplexFloat:
			t->printfmt = 'F';
			break;
		case TypeImaginaryFloat:
			t->printfmt = 'i';
			break;
		}
		break;

	case TagPointerType:
		t = xnewtype(Pointer, s);
		t->sub = typebynum(s->attrs.type, 0);
		break;

	case TagConstType:
	case TagVolatileType:
		t = xnewtype(Defer, s);
		t->sub = typebynum(s->attrs.type, 0);
		break;

	case TagArrayType:
		t = xnewtype(Array, s);
		t->sub = typebynum(s->attrs.type, 0);
		break;

	case TagStructType:
	case TagUnionType:
		t = xnewtype(Aggr, s);
		t->sue = s->attrs.tag==TagStructType ? 's' : 'u';
		t->xsizeof = s->attrs.bytesize;
		t->suename = s->attrs.name;
		t->isunion = s->attrs.tag==TagUnionType;
		while(dwarfnextsymat(d, s, depth+1) == 1){
			if(s->attrs.tag != TagMember){
				ds2acid(d, s, b, fn);
				continue;
			}
			if(!s->attrs.have.name || !s->attrs.have.type)
				continue;
			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] = s->attrs.name;
			if(t->isunion)
				t->val[t->n] = 0;
			else
				t->val[t->n] = valof(s->attrs.have.datamemberloc, &s->attrs.datamemberloc);
			t->t[t->n] = typebynum(s->attrs.type, 0);
			t->n++;
		}
		break;

	case TagEnumerationType:
		t = xnewtype(Enum, s);
		t->sue = 'e';
		t->suename = s->attrs.name;
		t->xsizeof = s->attrs.bytesize;
		while(dwarfnextsymat(d, s, depth+1) == 1){
			if(s->attrs.tag != TagEnumerator){
				ds2acid(d, s, b, fn);
				continue;
			}
			if(!s->attrs.have.name || !s->attrs.have.constvalue)
				continue;
			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] = s->attrs.name;
			t->val[t->n] = valof(s->attrs.have.constvalue, &s->attrs.constvalue);
			t->n++;
		}
		break;

	case TagFormalParameter:
	case TagVariable:
		if(s->attrs.name==nil || s->attrs.type==0)
			break;
		addsymx(fn, s->attrs.name, typebynum(s->attrs.type, 0));
		break;
	}
}