blob: fb6374fcd82ee85e1d9345ee890f41053dcf8596 [file] [log] [blame]
PulkoMandy17fc7592022-07-28 18:27:54 +02001/* Test-language for vbcc. */
2
3#include "supp.h"
4
5struct Var *fv;
6
7struct Typ tint,mfunc;
8struct struct_declaration msd; /* initialized to zero */
9
10void raus(void)
11{
12 while(fv){
13 struct Var *m=fv->next;
14 free(fv);
15 fv=m;
16 }
17 while(first_ic){
18 struct IC *m=first_ic->next;
19 free(first_ic);
20 first_ic=m;
21 }
22 exit(0);
23}
24void add_IC(struct IC *new)
25{
26 new->next=0;
27 new->prev=last_ic;
28 new->change_cnt=new->use_cnt=0;
29 new->change_list=new->use_list=0;
30 new->line=0;
31 new->file=0;
32 if(!last_ic){
33 first_ic=new;
34 }else{
35 last_ic->next=new;
36 }
37 last_ic=new;
38}
39struct Var *add_var(char *name,struct Typ *t,int sc)
40{
41 struct Var *v=mymalloc(sizeof(*v));
42 v->vtyp=t;
43 v->storage_class=sc;
44 v->reg=0;
45 v->identifier=name;
46 v->offset=max_offset;
47 if(sc==AUTO) max_offset=zmadd(max_offset,sizetab[t->flags&NQ]);
48 v->priority=1;
49 v->flags=0;
50 v->next=fv;
51 v->clist=0;
52 v->fi=0;
53 v->inline_copy=0;
54 fv=v;
55 return v;
56}
57struct Var *add_tmp_var(struct Typ *t)
58{
59 return add_var(empty,t,AUTO);
60}
61struct Var *get_var(char *name)
62{
63 struct Var *v;char *buf;
64 for(v=fv;v;v=v->next){
65 if(!strcmp(name,v->identifier)) return v;
66 }
67 buf=mymalloc(strlen(name)+1);
68 strcpy(buf,name);
69 return add_var(buf,&tint,AUTO);
70}
71
72void read_ics()
73{
74 char s[400],q1[100],q2[100],z[100],op;
75 struct IC *new;
76 gets(s);
77 while(sscanf(s,"%99s = %99s %c %99s",z,q1,&op,q2)==4){
78 new=new_IC();
79 switch(op){
80 case '+': new->code=ADD;break;
81 case '*': new->code=MULT;break;
82 case '-': new->code=SUB;break;
83 case '/': new->code=DIV;break;
84 default: return;
85 }
86 new->typf=INT;
87 new->q1.flags=new->q2.flags=new->z.flags=VAR;
88 new->q1.am=new->q2.am=new->z.am=0;
89 new->q1.val.vmax=l2zm(0L);
90 new->q2.val.vmax=l2zm(0L);
91 new->z.val.vmax=l2zm(0L);
92 new->q1.v=get_var(q1);
93 new->q2.v=get_var(q2);
94 new->z.v=get_var(z);
95 add_IC(new);
96 gets(s);
97 }
98}
99void error(int n,...)
100{
101 printf("error %d\n",n);
102 raus();
103}
104void savescratch()
105{}
106
107main()
108{
109 struct Var *main;
110 max_offset=l2zm(0L);
111 if(!init_cg()) raus();
112 tint.flags=INT;
113 tint.next=0;
114 mfunc.flags=FUNKT;
115 mfunc.next=∭
116 mfunc.exact=&msd;
117 main=add_var("main",&mfunc,EXTERN);
118 read_ics();
119 printf("optflags: ");
120 scanf("%ld",&optflags);
121 pric(stdout,first_ic);
122 vl1=vl3=0;
123 vl2=fv;
124 optimize(optflags,main);
125 pric(stdout,first_ic);
126 gen_code(stdout,first_ic,main,max_offset);
127 raus();
128}