xref: /plan9/sys/src/cmd/acid/dot.c (revision 4de34a7edde43207e841ec91ecd12e6cf5f5ebe7)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <mach.h>
6 #define Extern extern
7 #include "acid.h"
8 
9 Type*
srch(Type * t,char * s)10 srch(Type *t, char *s)
11 {
12 	Type *f;
13 
14 	f = 0;
15 	while(t) {
16 		if(strcmp(t->tag->name, s) == 0) {
17 			if(f == 0 || t->depth < f->depth)
18 				f = t;
19 		}
20 		t = t->next;
21 	}
22 	return f;
23 }
24 
25 void
odot(Node * n,Node * r)26 odot(Node *n, Node *r)
27 {
28 	char *s;
29 	Type *t;
30 	Node res;
31 	uvlong addr;
32 
33 	s = n->sym->name;
34 	if(s == 0)
35 		fatal("dodot: no tag");
36 
37 	expr(n->left, &res);
38 	if(res.comt == 0)
39 		error("no type specified for (expr).%s", s);
40 
41 	if(res.type != TINT)
42 		error("pointer must be integer for (expr).%s", s);
43 
44 	t = srch(res.comt, s);
45 	if(t == 0)
46 		error("no tag for (expr).%s", s);
47 
48 	/* Propagate types */
49 	if(t->type)
50 		r->comt = t->type->lt;
51 
52 	addr = res.ival+t->offset;
53 	if(t->fmt == 'a') {
54 		r->op = OCONST;
55 		r->fmt = 'a';
56 		r->type = TINT;
57 		r->ival = addr;
58 	}
59 	else
60 		indir(cormap, addr, t->fmt, r);
61 
62 }
63 
64 static Type **tail;
65 static Lsym *base;
66 
67 void
buildtype(Node * m,int d)68 buildtype(Node *m, int d)
69 {
70 	Type *t;
71 
72 	if(m == ZN)
73 		return;
74 
75 	switch(m->op) {
76 	case OLIST:
77 		buildtype(m->left, d);
78 		buildtype(m->right, d);
79 		break;
80 
81 	case OCTRUCT:
82 		buildtype(m->left, d+1);
83 		break;
84 	default:
85 		t = malloc(sizeof(Type));
86 		t->next = 0;
87 		t->depth = d;
88 		t->tag = m->sym;
89 		t->base = base;
90 		t->offset = m->ival;
91 		if(m->left) {
92 			t->type = m->left->sym;
93 			t->fmt = 'a';
94 		}
95 		else {
96 			t->type = 0;
97 			if(m->right)
98 				t->type = m->right->sym;
99 			t->fmt = m->fmt;
100 		}
101 
102 		*tail = t;
103 		tail = &t->next;
104 	}
105 }
106 
107 void
defcomplex(Node * tn,Node * m)108 defcomplex(Node *tn, Node *m)
109 {
110 	tail = &tn->sym->lt;
111 	base = tn->sym;
112 	buildtype(m, 0);
113 }
114 
115 void
decl(Node * n)116 decl(Node *n)
117 {
118 	Node *l;
119 	Value *v;
120 	Frtype *f;
121 	Lsym *type;
122 
123 	type = n->sym;
124 	if(type->lt == 0)
125 		error("%s is not a complex type", type->name);
126 
127 	l = n->left;
128 	if(l->op == ONAME) {
129 		v = l->sym->v;
130 		v->comt = type->lt;
131 		v->fmt = 'a';
132 		return;
133 	}
134 
135 	/*
136 	 * Frame declaration
137 	 */
138 	for(f = l->sym->local; f; f = f->next) {
139 		if(f->var == l->left->sym) {
140 			f->type = n->sym->lt;
141 			return;
142 		}
143 	}
144 	f = malloc(sizeof(Frtype));
145 	if(f == 0)
146 		fatal("out of memory");
147 
148 	f->type = type->lt;
149 
150 	f->var = l->left->sym;
151 	f->next = l->sym->local;
152 	l->sym->local = f;
153 }
154