blob: 5d7308a10a9c23144c6b167b8663a284de7964bd [file] [log] [blame]
PulkoMandy17fc7592022-07-28 18:27:54 +02001/* Test-language for vbcc. */
2
3#include "supp.h"
4
5#include <ctype.h>
6#include <stdio.h>
7
8struct Var *fv;
9
10struct Typ tint,mfunc;
11struct struct_declaration msd; /* initialized to zero */
12
13FILE *file;
14char *next;
15
16struct obj expression(),factor(),scalar();
17
18void raus(void)
19{
20 while(fv){
21 struct Var *m=fv->next;
22 free(fv);
23 fv=m;
24 }
25 while(first_ic){
26 struct IC *m=first_ic->next;
27 free(first_ic);
28 first_ic=m;
29 }
30 exit(0);
31}
32void add_IC(struct IC *new)
33{
34 new->next=0;
35 new->prev=last_ic;
36 new->change_cnt=new->use_cnt=0;
37 new->change_list=new->use_list=0;
38 new->line=0;
39 new->file=0;
40 new->q1.am=new->q2.am=new->z.am=0;
41 if(!last_ic){
42 first_ic=new;
43 }else{
44 last_ic->next=new;
45 }
46 last_ic=new;
47}
48struct Var *add_var(char *name,struct Typ *t,int sc)
49{
50 struct Var *v=mymalloc(sizeof(*v));
51 v->vtyp=t;
52 v->storage_class=sc;
53 v->reg=0;
54 v->identifier=name;
55 v->offset=max_offset;
56 if(sc==AUTO) max_offset=zmadd(max_offset,sizetab[t->flags&NQ]);
57 v->priority=1;
58 v->flags=0;
59 v->next=fv;
60 v->clist=0;
61 v->fi=0;
62 v->inline_copy=0;
63 v->nesting=1;
64 fv=v;
65 return v;
66}
67struct Var *add_tmp_var(struct Typ *t)
68{
69 return add_var(empty,t,AUTO);
70}
71struct Var *get_var(char *name)
72{
73 struct Var *v;char *buf;
74 puts("getvar");
75 for(v=fv;v;v=v->next){
76 if(!strcmp(name,v->identifier)) return v;
77 }
78 buf=mymalloc(strlen(name)+1);
79 strcpy(buf,name);
80 return add_var(buf,&tint,AUTO);
81}
82
83char *identifier(void)
84{
85 static char id[1024];
86 char *s=id;
87 puts("identifier");
88 while(isalnum(*next)) *s++=*next++;
89 puts("done");
90 return id;
91}
92struct obj scalar(void)
93{
94 struct obj o;
95 zmax val;
96 puts("scalar");
97 if(isdigit(*next)){
98 o.flags=KONST;
99 val=l2zm(0L);
100 while(isdigit(*next)){
101 val=zmmult(val,l2zm(10L));
102 val=zmadd(val,l2zm((long)(*next-'0')));
103 next++;
104 }
105 o.val.vint=zm2zi(val);
106 return o;
107 }
108 if(*next=='('){
109 next++;
110 o=expression();
111 next++;
112 return o;
113 }
114 o.flags=VAR;
115 o.val.vmax=l2zm(0L);
116 o.v=get_var(identifier());
117 return o;
118}
119struct obj factor(void)
120{
121 struct obj o;
122 struct IC *new;
123 puts("factor");
124 o=scalar();
125 while(*next=='*'||*next=='/'){
126 new=new_IC();
127 if(*next=='*') new->code=MULT; else new->code=DIV;
128 next++;
129 new->typf=INT;
130 new->q1=o;
131 new->q2=scalar();
132 o.flags=VAR;
133 o.v=add_tmp_var(&tint);
134 o.val.vmax=l2zm(0L);
135 new->z=o;
136 add_IC(new);
137 }
138 return o;
139}
140struct obj expression(void)
141{
142 struct obj o;
143 struct IC *new;
144 puts("expression");
145 o=factor();
146 while(*next=='+'||*next=='-'){
147 new=new_IC();
148 if(*next=='+') new->code=ADD; else new->code=SUB;
149 next++;
150 new->typf=INT;
151 new->q1=o;
152 new->q2=factor();
153 o.flags=VAR;
154 o.v=add_tmp_var(&tint);
155 o.val.vmax=l2zm(0L);
156 new->z=o;
157 add_IC(new);
158 }
159 return o;
160}
161void compile(void)
162{
163 struct IC *new;
164 char line[1024],*s;
165 struct obj o,last;
166 puts("compile");
167 while(fgets(line,1023,file)){
168 next=line;
169 s=identifier();
170 if(*next=='='){
171 struct Var *v=get_var(s);
172 next++;
173 o=expression();
174 new=new_IC();
175 new->code=ASSIGN;
176 new->typf=INT;
177 new->q1=o;
178 new->z.flags=VAR;
179 new->z.v=v;
180 new->z.val.vmax=l2zm(0L);
181 new->q2.flags=0;
182 new->q2.val.vmax=sizetab[INT];
183 last=new->z;
184 add_IC(new);
185 continue;
186 }
187 }
188 new=new_IC();
189 new->code=SETRETURN;
190 new->typf=INT;
191 new->q1=last;
192 new->q2.flags=new->z.flags=0;
193 new->q2.val.vmax=sizetab[INT];
194 new->z.reg=freturn(&tint);
195 if(!new->z.reg) puts("problem!");
196 add_IC(new);
197}
198void error(int n,...)
199{
200 printf("error %d\n",n);
201 raus();
202}
203void savescratch()
204{}
205
206main(int argc,char **argv)
207{
208 struct Var *main;
209 max_offset=l2zm(0L);
210 if(!init_cg()) raus();
211 tint.flags=INT;
212 tint.next=0;
213 mfunc.flags=FUNKT;
214 mfunc.next=&tint;
215 mfunc.exact=&msd;
216 main=add_var("main",&mfunc,EXTERN);
217 file=fopen(argv[1],"r");
218 if(!file) {printf("Error opening file\n");raus();}
219 compile();
220 scanf("%ld",&optflags);
221 pric(stdout,first_ic);
222 vl1=vl3=0;
223 vl2=fv;
224 optimize(optflags,main);
225 pric(stdout,first_ic);
226 gen_code(stdout,first_ic,main,max_offset);
227 raus();
228}
229