aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/acid/dot.c
blob: 80ec2d020ba73f393959e7c11ac52d8890099709 (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
#include <mach.h>
#define Extern extern
#include "acid.h"

Type*
srch(Type *t, char *s)
{
	Type *f;

	f = 0;
	while(t) {
		if(strcmp(t->tag->name, s) == 0) {
			if(f == 0 || t->depth < f->depth)
				f = t;
		}
		t = t->next;
	}
	return f;
}

void
odot(Node *n, Node *r)
{
	char *s;
	Type *t;
	Node res;
	u64int addr;

	s = n->sym->name;
	if(s == 0)
		fatal("dodot: no tag");

	expr(n->left, &res);
	if(res.store.comt == 0)
		error("no type specified for (expr).%s", s);

	if(res.type != TINT)
		error("pointer must be integer for (expr).%s", s);

	t = srch(res.store.comt, s);
	if(t == 0)
		error("no tag for (expr).%s", s);

	/* Propagate types */
	if(t->type)
		r->store.comt = t->type->lt;

	addr = res.store.u.ival+t->offset;
	if(t->fmt == 'a') {
		r->op = OCONST;
		r->store.fmt = 'a';
		r->type = TINT;
		r->store.u.ival = addr;
	}
	else
		indir(cormap, addr, t->fmt, r);

}

static Type **tail;
static Lsym *base;

void
buildtype(Node *m, int d)
{
	Type *t;

	if(m == ZN)
		return;

	switch(m->op) {
	case OLIST:
		buildtype(m->left, d);
		buildtype(m->right, d);
		break;

	case OCTRUCT:
		buildtype(m->left, d+1);
		break;
	default:
		t = malloc(sizeof(Type));
		t->next = 0;
		t->depth = d;
		t->tag = m->sym;
		t->base = base;
		t->offset = m->store.u.ival;
		if(m->left) {
			t->type = m->left->sym;
			t->fmt = 'a';
		}
		else {
			t->type = 0;
			if(m->right)
				t->type = m->right->sym;
			t->fmt = m->store.fmt;
		}

		*tail = t;
		tail = &t->next;
	}
}

void
defcomplex(Node *tn, Node *m)
{
	tail = &tn->sym->lt;
	base = tn->sym;
	buildtype(m, 0);
}

void
decl(Node *n)
{
	Node *l;
	Value *v;
	Frtype *f;
	Lsym *type;

	type = n->sym;
	if(type->lt == 0)
		error("%s is not a complex type", type->name);

	l = n->left;
	if(l->op == ONAME) {
		v = l->sym->v;
		v->store.comt = type->lt;
		v->store.fmt = 'a';
		return;
	}

	/*
	 * Frame declaration
	 */
	for(f = l->sym->local; f; f = f->next) {
		if(f->var == l->left->sym) {
			f->type = n->sym->lt;
			return;
		}
	}
	f = malloc(sizeof(Frtype));
	if(f == 0)
		fatal("out of memory");

	f->type = type->lt;

	f->var = l->left->sym;
	f->next = l->sym->local;
	l->sym->local = f;
}