forked from Leffmann/vbcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasm.c
128 lines (122 loc) · 2.63 KB
/
tasm.c
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
/* Test-language for vbcc. */
#include "supp.h"
struct Var *fv;
struct Typ tint,mfunc;
struct struct_declaration msd; /* initialized to zero */
void raus(void)
{
while(fv){
struct Var *m=fv->next;
free(fv);
fv=m;
}
while(first_ic){
struct IC *m=first_ic->next;
free(first_ic);
first_ic=m;
}
exit(0);
}
void add_IC(struct IC *new)
{
new->next=0;
new->prev=last_ic;
new->change_cnt=new->use_cnt=0;
new->change_list=new->use_list=0;
new->line=0;
new->file=0;
if(!last_ic){
first_ic=new;
}else{
last_ic->next=new;
}
last_ic=new;
}
struct Var *add_var(char *name,struct Typ *t,int sc)
{
struct Var *v=mymalloc(sizeof(*v));
v->vtyp=t;
v->storage_class=sc;
v->reg=0;
v->identifier=name;
v->offset=max_offset;
if(sc==AUTO) max_offset=zmadd(max_offset,sizetab[t->flags&NQ]);
v->priority=1;
v->flags=0;
v->next=fv;
v->clist=0;
v->fi=0;
v->inline_copy=0;
fv=v;
return v;
}
struct Var *add_tmp_var(struct Typ *t)
{
return add_var(empty,t,AUTO);
}
struct Var *get_var(char *name)
{
struct Var *v;char *buf;
for(v=fv;v;v=v->next){
if(!strcmp(name,v->identifier)) return v;
}
buf=mymalloc(strlen(name)+1);
strcpy(buf,name);
return add_var(buf,&tint,AUTO);
}
void read_ics()
{
char s[400],q1[100],q2[100],z[100],op;
struct IC *new;
gets(s);
while(sscanf(s,"%99s = %99s %c %99s",z,q1,&op,q2)==4){
new=new_IC();
switch(op){
case '+': new->code=ADD;break;
case '*': new->code=MULT;break;
case '-': new->code=SUB;break;
case '/': new->code=DIV;break;
default: return;
}
new->typf=INT;
new->q1.flags=new->q2.flags=new->z.flags=VAR;
new->q1.am=new->q2.am=new->z.am=0;
new->q1.val.vmax=l2zm(0L);
new->q2.val.vmax=l2zm(0L);
new->z.val.vmax=l2zm(0L);
new->q1.v=get_var(q1);
new->q2.v=get_var(q2);
new->z.v=get_var(z);
add_IC(new);
gets(s);
}
}
void error(int n,...)
{
printf("error %d\n",n);
raus();
}
void savescratch()
{}
main()
{
struct Var *main;
max_offset=l2zm(0L);
if(!init_cg()) raus();
tint.flags=INT;
tint.next=0;
mfunc.flags=FUNKT;
mfunc.next=∭
mfunc.exact=&msd;
main=add_var("main",&mfunc,EXTERN);
read_ics();
printf("optflags: ");
scanf("%ld",&optflags);
pric(stdout,first_ic);
vl1=vl3=0;
vl2=fv;
optimize(optflags,main);
pric(stdout,first_ic);
gen_code(stdout,first_ic,main,max_offset);
raus();
}