plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

dot.c (2217B)


      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*
     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
     26 odot(Node *n, Node *r)
     27 {
     28 	char *s;
     29 	Type *t;
     30 	Node res;
     31 	u64int 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.store.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.store.comt, s);
     45 	if(t == 0)
     46 		error("no tag for (expr).%s", s);
     47 
     48 	/* Propagate types */
     49 	if(t->type)
     50 		r->store.comt = t->type->lt;
     51 
     52 	addr = res.store.u.ival+t->offset;
     53 	if(t->fmt == 'a') {
     54 		r->op = OCONST;
     55 		r->store.fmt = 'a';
     56 		r->type = TINT;
     57 		r->store.u.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
     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->store.u.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->store.fmt;
    100 		}
    101 
    102 		*tail = t;
    103 		tail = &t->next;
    104 	}
    105 }
    106 
    107 void
    108 defcomplex(Node *tn, Node *m)
    109 {
    110 	tail = &tn->sym->lt;
    111 	base = tn->sym;
    112 	buildtype(m, 0);
    113 }
    114 
    115 void
    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->store.comt = type->lt;
    131 		v->store.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 }