forked from Leffmann/vbcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supp.c
1699 lines (1635 loc) · 41.3 KB
/
supp.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "supp.h"
#include "opt.h"
static char FILE_[]=__FILE__;
#ifndef HAVE_EXT_TYPES
char *typname[]={"strange","char","short","int","long","long long",
"float","double","long double","void",
"pointer","array","struct","union","enum","function",
"bool","vector"};
#endif
char *storage_class_name[]={"strange","auto","register","static","extern","typedef"};
char *ename[]={"strange","sequence","move","set+","set-","set*","set/","set%",
"set&","set^","set|","set<<","set>>","?:","lor","land","or",
"eor","and","eq","neq","lt","le","gt","ge","lsl",
"lsr","add","sub","mul","div","mod","negate",
"not","preinc","postinc","predec","postdec","neg",
"dref-pointer","address-of","cast","call","index",
"dref-struct-pointer","dref-struct","identifier","constant",
"string","member",
"convert","convert-short","convert-int","convert-long",
"convert-float","convert-double","convert-void","convert-pointer",
"convert-uchar","convert-ushort","convert-uint","convert-ulong",
"address-of-array","first-element-of-array","pmult",
"allocreg","freereg","pconstant","test","label","beq","bne",
"blt","bge","ble","bgt","bra","compare","push","pop",
"address-of-struct","add-int-to-pointer","sub-int-from-pointer",
"sub-pointer-from-pointer","push-reg","pop-reg","pop-args",
"save-regs","restore-regs","identifier-label","dc","align",
"colon","get-return","set-return","move-from-reg","move-to-reg",
"nop","bitfield"};
char *empty="";
zchar vchar; zuchar vuchar;
zshort vshort; zushort vushort;
zint vint; zuint vuint;
zlong vlong; zulong vulong;
zllong vllong; zullong vullong;
zmax vmax; zumax vumax;
zfloat vfloat; zdouble vdouble;
zldouble vldouble;
union atyps gval;
#ifndef DEBUG
int DEBUG;
#endif
int label;
int regs[MAXR+1],regused[MAXR+1],simple_scratch[MAXR+1];
struct Var *regsv[MAXR+1];
int goto_used;
int ic_count;
zmax max_offset;
int function_calls,vlas;
int coloring;
int dmalloc;
int disable;
int multiple_ccs;
int lastlabel,return_label;
int only_inline;
struct IC *err_ic;
long maxoptpasses=10;
long optflags;
int optsize,optspeed,unroll_all,stack_check;
int cross_module,final,no_emit;
int debug_info;
long inline_size=100;
long inline_depth=1;
long unroll_size=200;
int fp_assoc,noaliasopt,noitra;
char *filename;
struct IC *first_ic,*last_ic;
int float_used;
bvtype regs_modified[RSIZE/sizeof(bvtype)];
/* Das haette ich gern woanders */
struct Var *vl0,*vl1,*vl2,*vl3;
int align_arguments=1;
zmax stackalign;
int misracheck,misraversion,misracomma,misratok;
int pack_align;
int short_push;
int default_unsigned;
char *emit_buffer[EMIT_BUF_DEPTH];
char *emit_p;
int emit_f,emit_l;
int no_inline_peephole;
struct Typ *new_typ(void)
/* Erzeigt neuen (leeren) Typ. */
{
struct Typ *new=mymalloc(TYPS);
new->flags=0;
new->next=0;
new->exact=0;
new->attr=0;
new->dsize=0;
new->reg=0;
#ifdef HAVE_ECPP
/* removed */
#endif
return new;
}
struct Typ *clone_typ(struct Typ *old)
/* Erzeugt Kopie eines Typs und liefert Zeiger auf Kopie. */
{
struct Typ *new;
if(!old) return 0;
new=new_typ();
*new=*old;
if(old->attr){
new->attr=mymalloc(strlen(old->attr)+1);
strcpy(new->attr,old->attr);
}
if(new->next) new->next=clone_typ(new->next);
return new;
}
struct Var *new_var(void)
{
struct Var *new=mymalloc(sizeof(*new));
new->clist=0;
new->vtyp=0;
new->storage_class=0;
new->reg=0;
new->vattr=0;
new->next=0;
new->flags=0;
new->fi=0;
new->nesting=0;
new->filename=0;
new->line=0;
new->dfilename=0;
new->dline=0;
new->description=0;
new->tunit=0;
new->offset=l2zm(0L);
new->identifier=0;
#ifdef HAVE_TARGET_ATTRIBUTES
new->tattr=0;
#endif
#ifdef ALEX_REG
new->iRegCopyNr = -1;
new->iRegCopyNrHc12V = -1;
#endif
return new;
}
struct IC *new_IC(void)
{
struct IC *p=mymalloc(ICS);
p->change_cnt=0;
p->use_cnt=0;
p->call_cnt=0;
p->arg_cnt=0;
p->use_list=0;
p->change_list=0;
p->call_list=0;
p->arg_list=0;
p->line=0;
p->file=0;
p->flags=0;
p->q1.flags=p->q2.flags=p->z.flags=0;
p->q1.am=p->q2.am=p->z.am=0;
p->q1.val.vmax=p->q2.val.vmax=p->z.val.vmax=l2zm(0L);
p->savedsp=0;
p->typf=p->typf2=0;
#ifdef ALEX_REG
p->iZWebIndex = -1;
p->iQ1WebIndex = -1;
p->iQ2WebIndex = -1;
p->pFlow = NULL;
#endif /* GC_RALLOC */
return p;
}
/* (partially) clones an IC list */
struct IC *clone_ic(struct IC *p)
{
struct IC *new,*first,*last;
first=last=new=0;
while(p){
new=mymalloc(sizeof(*new));
*new=*p;
p->copy=new;
if(new->code==CALL){
int i;
new->arg_list=mymalloc(sizeof(*new->arg_list)*new->arg_cnt);
for(i=0;i<new->arg_cnt;i++)
new->arg_list[i]=p->arg_list[i]->copy;
}
new->prev=last;
new->next=0;
if(!first) first=new;
if(last) last->next=new;
last=new;
p=p->next;
}
return first;
}
void free_IC(struct IC *p)
/* Gibt IC-Liste inkl. Typen frei. */
{
struct IC *merk;
if(DEBUG&1) printf("free_IC()\n");
while(p){
/*if(p->q1.am&&!p->q1.flags) ierror(0);
if(p->q2.am&&!p->q2.flags) ierror(0);
if(p->z.am&&!p->z.flags) ierror(0);*/
if(p->q1.am) free(p->q1.am);
if(p->q2.am) free(p->q2.am);
if(p->z.am) free(p->z.am);
if(p->code==CALL) free(p->arg_list);
merk=p->next;
free(p);
p=merk;
}
}
void move_IC(struct IC *after,struct IC *p)
{
if(p->prev)
p->prev->next=p->next;
else
first_ic=p->next;
if(p->next)
p->next->prev=p->prev;
else
last_ic=p->prev;
insert_IC(after,p);
}
void remove_IC(struct IC *p)
/* Entfernt IC p aus Liste. */
{
if(p->prev) p->prev->next=p->next; else first_ic=p->next;
if(p->next) p->next->prev=p->prev; else last_ic=p->prev;
if(p->q1.am) free(p->q1.am);
if(p->q2.am) free(p->q2.am);
if(p->z.am) free(p->z.am);
free(p);
}
void freetyp(struct Typ *p)
/* Gibt eine Typ-Liste frei, aber keine struct_declaration oder so. */
{
int f;struct Typ *merk;
if(DEBUG&8){printf("freetyp: ");prd(stdout,p);printf("\n");}
while(p){
merk=p->next;
f=p->flags&NQ;
if(merk&&!ISARRAY(f)&&!ISPOINTER(f)&&!ISFUNC(f)&&!ISVECTOR(f))
ierror(0);
free(p->attr);
free(p);
p=merk;
}
}
#ifndef HAVE_TGT_FALIGN
zmax falign(struct Typ *t)
/* Liefert Alignment eines Typs. Funktioniert im Gegensatz zum */
/* align[]-Array auch mit zusammengesetzten Typen. */
{
int i,f; zmax al,alt;
f=t->flags&NQ;
if(ISVECTOR(f)) return szof(t);
al=align[f];
if(ISSCALAR(f)) return al;
if(ISARRAY(f)){
do{
t=t->next;
f=t->flags&NQ;
}while(ISARRAY(f)||ISVECTOR(f));
alt=falign(t);
if(zmleq(al,alt)) return alt; else return al;
}
if(ISUNION(f)||ISSTRUCT(f)){
if(!t->exact) ierror(0);
for(i=0;i<t->exact->count;i++){
alt=(*t->exact->sl)[i].align;
if(!zmleq(alt,al)) al=alt;
}
}
return al;
}
#endif
/* check, whether t is a variable length array */
int is_vlength(struct Typ *t)
{
if(!ISARRAY(t->flags))
return 0;
if(t->dsize)
return 1;
else
return is_vlength(t->next);
}
/* calculate size of a variable length array */
struct Var *vlength_szof(struct Typ *t)
{
struct IC *new;struct Typ *nt;
if(!ISARRAY(t->flags))
ierror(0);
new=new_IC();
new->code=MULT;
new->typf=HAVE_INT_SIZET?(UNSIGNED|INT):(UNSIGNED|LONG);
if(t->dsize){
new->q1.flags=VAR;
new->q1.v=t->dsize;
new->q1.val.vmax=l2zm(0L);
}else{
new->q1.flags=KONST;
#if HAVE_INT_SIZET
new->q1.val.vuint=zum2zui(zm2zum(t->size));
#else
new->q1.val.vulong=zum2zul(zm2zum(t->size));
#endif
}
new->z.flags=VAR;
nt=new_typ();
nt->flags=new->typf;
new->z.v=add_tmp_var(nt);
new->z.val.vmax=l2zm(0L);
if(is_vlength(t->next)){
new->q2.flags=VAR;
new->q2.v=vlength_szof(t->next);
new->q2.val.vmax=l2zm(0L);
}else{
new->q2.flags=KONST;
#if HAVE_INT_SIZET
new->q2.val.vuint=zum2zui(zm2zum(szof(t->next)));
#else
new->q2.val.vulong=zum2zul(zm2zum(szof(t->next)));
#endif
}
add_IC(new);
return new->z.v;
}
/* return the type of a d-dim vector of base type x */
int mkvec(int x,int d)
{
int t=x&NQ,r;
if(d!=2&&d!=3&&d!=4&&d!=8&&d!=16) ierror(0);
switch(t){
case BOOL:
r=VECBOOL;break;
case CHAR:
r=VECCHAR;break;
case SHORT:
r=VECSHORT;break;
case INT:
r=VECINT;break;
case LONG:
r=VECLONG;break;
case FLOAT:
r=VECFLOAT;break;
default:
ierror(0);
}
return (r+d-1)|(x&~NQ);
}
/* return the base type of a vector */
int VECTYPE(int x)
{
int t=x&NQ,r=0;
if(t>=VECBOOL&&t<=VECBOOL+MAXVECDIM)
r=BOOL;
if(t>=VECCHAR&&t<=VECCHAR+MAXVECDIM)
r=CHAR;
if(t>=VECSHORT&&t<=VECSHORT+MAXVECDIM)
r=SHORT;
if(t>=VECINT&&t<=VECINT+MAXVECDIM)
r=INT;
if(t>=VECLONG&&t<=VECLONG+MAXVECDIM)
r=LONG;
if(t>=VECFLOAT&&t<=VECFLOAT+MAXVECDIM)
r=FLOAT;
if(!r) ierror(0);
return r|(x&~NQ);
}
#ifndef HAVE_TGT_SZOF
zmax szof(struct Typ *t)
/* Liefert die benoetigte Groesse eines Typs in Bytes. */
{
int i=t->flags&NQ,j;zmax size,m;
#ifdef HAVE_ECPP
/* removed */
#endif
if(ISSCALAR(i)) return sizetab[i];
if(ISARRAY(i)){
if(is_vlength(t))
return sizetab[POINTER_TYPE(t->next)];
size=zmmult((t->size),szof(t->next));
m=align[ARRAY];
return zmmult(zmdiv(zmadd(size,zmsub(m,l2zm(1L))),m),m); /* align */
}
if(ISVECTOR(i)){
zmax dim=VECDIM(i);
if(zmeqto(dim,l2zm(3L))) dim=l2zm(4L);
return zmmult(dim,sizetab[VECTYPE(i)&NQ]);
}
if(ISUNION(i)){
for(j=0,size=l2zm(0L);j<t->exact->count;j++){
m=szof((*t->exact->sl)[j].styp);
if(zmeqto(m,l2zm(0L))) return l2zm(0L);
if(!zmleq(m,size)) size=m;
}
m=falign(t);
return zmmult(zmdiv(zmadd(size,zmsub(m,l2zm(1L))),m),m); /* align */
}
if(ISSTRUCT(i)){
size=l2zm(0L);
#ifdef HAVE_ECPP
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
#endif
for(j=0;j<t->exact->count;j++){
struct Typ *h=(*t->exact->sl)[j].styp;
if((*t->exact->sl)[j].bfoffset<=0){
m=(*t->exact->sl)[j].align;
#ifdef HAVE_ECPP
/* removed */
#endif
if(zmeqto(m,l2zm(0L))) {prd(stdout,h);ierror(0);}
size=zmmult(zmdiv(zmadd(size,zmsub(m,l2zm(1L))),m),m);
m=szof(h);
/*if(zmeqto(m,l2zm(0L))) return l2zm(0L);*/
size=zmadd(size,m);
}
}
m=falign(t);
return zmmult(zmdiv(zmadd(size,zmsub(m,l2zm(1L))),m),m); /* align */
}
return sizetab[i];
}
zmax struct_offset(struct struct_declaration *sd,const char *identifier)
{
int i=0,intbitfield=-1;zmax offset=l2zm(0),al;
while(i<sd->count&&strcmp((*sd->sl)[i].identifier,identifier)){
if((*sd->sl)[i].bfoffset>=0){
if(i+1<sd->count&&(*sd->sl)[i+1].bfoffset>0){
i++;
continue;
}
}
al=(*sd->sl)[i].align;
offset=zmmult(zmdiv(zmadd(offset,zmsub(al,l2zm(1L))),al),al);
offset=zmadd(offset,szof((*sd->sl)[i].styp));
i++;
}
if(i>=sd->count) {error(23,identifier);return l2zm(0L);}
al=(*sd->sl)[i].align;
offset=zmmult(zmdiv(zmadd(offset,zmsub(al,l2zm(1L))),al),al);
return offset;
}
#ifdef HAVE_ECPP
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
#endif
#endif
#ifndef HAVE_TGT_PRINTVAL
void printval(FILE *f,union atyps *p,int t)
/* Gibt atyps aus. */
{
t&=NU;
if(t==CHAR){fprintf(f,"C");vmax=zc2zm(p->vchar);printzm(f,vmax);}
if(t==(UNSIGNED|CHAR)){fprintf(f,"UC");vumax=zuc2zum(p->vuchar);printzum(f,vumax);}
if(t==SHORT){fprintf(f,"S");vmax=zs2zm(p->vshort);printzm(f,vmax);}
if(t==(UNSIGNED|SHORT)){fprintf(f,"US");vumax=zus2zum(p->vushort);printzum(f,vumax);}
if(t==FLOAT){fprintf(f,"F");vldouble=zf2zld(p->vfloat);printzld(f,vldouble);}
if(t==DOUBLE){fprintf(f,"D");vldouble=zd2zld(p->vdouble);printzld(f,vldouble);}
if(t==LDOUBLE){fprintf(f,"LD");printzld(f,p->vldouble);}
if(t==INT){fprintf(f,"I");vmax=zi2zm(p->vint);printzm(f,vmax);}
if(t==(UNSIGNED|INT)){fprintf(f,"UI");vumax=zui2zum(p->vuint);printzum(f,vumax);}
if(t==LONG){fprintf(f,"L");vmax=zl2zm(p->vlong);printzm(f,vmax);}
if(t==(UNSIGNED|LONG)){fprintf(f,"UL");vumax=zul2zum(p->vulong);printzum(f,vumax);}
if(t==LLONG){fprintf(f,"LL");vmax=zll2zm(p->vllong);printzm(f,vmax);}
if(t==(UNSIGNED|LLONG)){fprintf(f,"ULL");vumax=zull2zum(p->vullong);printzum(f,vumax);}
if(t==MAXINT){fprintf(f,"M");printzm(f,p->vmax);}
if(t==(UNSIGNED|MAXINT)){fprintf(f,"UM");printzum(f,p->vumax);}
/*FIXME*/
if(t==POINTER){fprintf(f,"P");vumax=zul2zum(p->vulong);printzum(f,vumax);}
}
void emitval(FILE *f,union atyps *p,int t)
/* Gibt atyps aus. */
{
t&=NU;
if(t==CHAR){vmax=zc2zm(p->vchar);emitzm(f,vmax);}
if(t==(UNSIGNED|CHAR)){vumax=zuc2zum(p->vuchar);emitzum(f,vumax);}
if(t==SHORT){vmax=zs2zm(p->vshort);emitzm(f,vmax);}
if(t==(UNSIGNED|SHORT)){vumax=zus2zum(p->vushort);emitzum(f,vumax);}
if(t==FLOAT){vldouble=zf2zld(p->vfloat);emitzld(f,vldouble);}
if(t==DOUBLE){vldouble=zd2zld(p->vdouble);emitzld(f,vldouble);}
if(t==LDOUBLE){emitzld(f,p->vldouble);}
if(t==INT){vmax=zi2zm(p->vint);emitzm(f,vmax);}
if(t==(UNSIGNED|INT)){vumax=zui2zum(p->vuint);emitzum(f,vumax);}
if(t==LONG){vmax=zl2zm(p->vlong);emitzm(f,vmax);}
if(t==(UNSIGNED|LONG)){vumax=zul2zum(p->vulong);emitzum(f,vumax);}
if(t==LLONG){vmax=zll2zm(p->vllong);emitzm(f,vmax);}
if(t==(UNSIGNED|LLONG)){vumax=zull2zum(p->vullong);emitzum(f,vumax);}
if(t==MAXINT){emitzm(f,p->vmax);}
if(t==(UNSIGNED|MAXINT)){emitzum(f,p->vumax);}
/*FIXME*/
if(t==POINTER){vumax=zul2zum(p->vulong);emitzum(f,vumax);}
}
#endif
void pric2(FILE *f,struct IC *p)
/* Gibt ein IC aus. */
{
if(p->code>NOP) ierror(0);
if(p->next&&p->next->prev!=p) ierror(0);
if(p->code>=LABEL&&p->code<=BRA){
if(p->code==LABEL)
fprintf(f,"L%d",p->typf);
else{
fprintf(f,"\t%s L%d",ename[p->code],p->typf);
if(p->q1.flags){ fprintf(f,",");probj(f,&p->q1,0);}
}
if(p->code==LABEL&&(p->flags&LOOP_COND_TRUE)) fprintf(f," (while-loop)");
if(p->code==BRA&&(p->flags&LOOP_COND_TRUE)) fprintf(f," (to-loop-test)");
}else{
fprintf(f,"\t%s ",ename[p->code]);
if(p->typf&VOLATILE) fprintf(f,"volatile ");
if(p->typf&CONST) fprintf(f,"const ");
if(p->typf&UNSIGNED) fprintf(f,"unsigned ");
if(p->typf){
if(ISVECTOR(p->typf))
fprintf(f,"%s%d ",typname[VECTYPE(p->typf)&NQ],VECDIM(p->typf));
else
fprintf(f,"%s ",typname[p->typf&NQ]);
}
probj(f,&p->q1,q1typ(p));
if(p->q2.flags){fprintf(f,",");probj(f,&p->q2,q2typ(p));}
if(p->z.flags){fprintf(f,"->");probj(f,&p->z,ztyp(p));}
if(p->code==ASSIGN||p->code==PUSH||p->code==POP||p->code==CALL)
fprintf(f," size=%ld",zm2l(p->q2.val.vmax));
if((p->code==SAVEREGS||p->code==RESTOREREGS)&&p->q1.reg)
fprintf(f," except %s",regnames[p->q1.reg]);
if(p->code==CONVERT)
fprintf(f," from %s%s",(p->typf2&UNSIGNED)?"unsigned ":"",typname[p->typf2&NQ]);
if(p->code==LSHIFT||p->code==RSHIFT)
fprintf(f," shift-type %s%s",(p->typf2&UNSIGNED)?"unsigned ":"",typname[p->typf2&NQ]);
if(p->code==ADDI2P||p->code==SUBIFP||p->code==SUBPFP||p->code==ADDRESS)
fprintf(f," ptype=%s%s",(p->typf2&UNSIGNED)?"unsigned ":"",typname[p->typf2&NQ]);
if(p->code==ASSIGN||p->code==PUSH)
if(p->typf2) fprintf(f," align=%d\n",p->typf2);
}
if(p->code==CALL){
fprintf(f," =>");
if(p->call_cnt==0)
fprintf(f,"(unknown)");
else{
int i;
for(i=0;i<p->call_cnt;i++)
fprintf(f," %s",p->call_list[i].v->identifier);
}
}
fprintf(f,"\n");
#if 0
if(p->code==CALL){
int i;
fprintf(f,"c=%p\n",p);
for(i=0;i<p->arg_cnt;i++){
fprintf(f,"%p!\n",p->arg_list[i]);
fprintf(f,"%02d:",i);
pric2(f,p->arg_list[i]);
}
}
#endif
}
void pric(FILE *f,struct IC *p)
/* Gibt IC-Liste auf dem Bildschirm aus. */
{
while(p){
pric2(f,p);
/* if(p->q1.am||p->q2.am||p->z.am) ierror(0);*/
p=p->next;
}
}
void printzm(FILE *f,zmax x)
/* Konvertiert zmax nach ASCII. */
/* Basiert noch einigermassen auf */
/* Zweierkomplementdarstellung (d.h. -MIN>MAX). */
/* Ausserdem muss max(abs(max))<=max(unsigned max). */
{
zmax zm;zumax zum;
zm=l2zm(0L);
if(zmleq(x,zm)&&!zmeqto(x,zm)){
fprintf(f,"-");zm=zum2zm(t_max(MAXINT));
if(zmleq(x,zmsub(l2zm(0L),zm))&&!zmeqto(x,zmsub(l2zm(0L),zm))){
/* aufpassen, da -x evtl. >LONG_MAX */
zum=t_max(MAXINT);
x=zmadd(x,zm);
}else
zum=ul2zum(0UL);
x=zmsub(l2zm(0L),x);
vumax=zm2zum(x);
zum=zumadd(zum,vumax);
}else
zum=zm2zum(x);
printzum(f,zum);
}
void printzum(FILE *f,zumax x)
/* Konvertiert zumax nach ASCII. */
{
zumax zum;unsigned long l;
zum=ul2zum(10UL);
if(!zumeqto(zumdiv(x,zum),ul2zum(0UL))) printzum(f,zumdiv(x,zum));
zum=zummod(x,zum);l=zum2ul(zum);
fprintf(f,"%c",(int)(l+'0'));
}
void printzld(FILE *f,zldouble x)
/* Konvertiert zdouble nach ASCII, noch nicht fertig. */
{
fprintf(f,"fp-constant");
}
void emitzm(FILE *f,zmax x)
/* Konvertiert zmax nach ASCII. */
/* Basiert noch einigermassen auf */
/* Zweierkomplementdarstellung (d.h. -MIN>MAX). */
/* Ausserdem muss max(abs(max))<=max(unsigned max). */
{
zmax zm;zumax zum;
zm=l2zm(0L);
if(zmleq(x,zm)&&!zmeqto(x,zm)){
emit(f,"-");zm=zum2zm(t_max(MAXINT));
if(zmleq(x,zmsub(l2zm(0L),zm))&&!zmeqto(x,zmsub(l2zm(0L),zm))){
/* aufpassen, da -x evtl. >LONG_MAX */
zum=t_max(MAXINT);
x=zmadd(x,zm);
}else
zum=ul2zum(0UL);
x=zmsub(l2zm(0L),x);
vumax=zm2zum(x);
zum=zumadd(zum,vumax);
}else
zum=zm2zum(x);
emitzum(f,zum);
}
void emitzum(FILE *f,zumax x)
/* Konvertiert zumax nach ASCII. */
{
zumax zum;unsigned long l;
zum=ul2zum(10UL);
if(!zumeqto(zumdiv(x,zum),ul2zum(0UL))) emitzum(f,zumdiv(x,zum));
zum=zummod(x,zum);l=zum2ul(zum);
emit(f,"%c",(int)(l+'0'));
}
void emitzld(FILE *f,zldouble x)
/* Konvertiert zdouble nach ASCII, noch nicht fertig. */
{
emit(f,"fp-constant");
}
static struct memblock {struct memblock *next;void *p;} *first_mb;
static void add_mb(void *p)
{
struct memblock *mb_second=first_mb;
struct memblock *mb=malloc(sizeof(*mb));
if(!mb){
error(12);
raus();
}
first_mb=mb;
mb->next=mb_second;
mb->p=p;
}
static void remove_mb(void *p)
{
struct memblock *mb_prev=0;
struct memblock *mb=first_mb;
while(mb){
if(mb->p==p){
if(mb_prev==0) first_mb=mb->next;
else mb_prev->next=mb->next;
(free)(mb);
return;
}
mb_prev=mb;
mb=mb->next;
}
ierror(0);
}
void *mymalloc(size_t size)
/* Allocate memory and quit on failure. */
{
void *p;
if(dmalloc)
size+=sizeof(size);
else if(size==0)
/* Not very nice, but simplest way to avoid a failure when size==0. */
size=1;
if(!(p=malloc(size))){
error(12);
raus();
}
if(DEBUG&32768){
printf("malloc %p (s=%lu)\n",p,(unsigned long)size);
fflush(stdout);
}
if(DEBUG&65536) add_mb(p);
if(dmalloc){
*(size_t *)p=size;
p=((char *)p)+sizeof(size);
memset(p,0xaa,size-sizeof(size));
}
return p;
}
void *myrealloc(void *p,size_t size)
/* Reallocate memory and quit on failure. */
{
void *new;
if(!p) return mymalloc(size);
if(dmalloc){
size+=sizeof(size);
new=realloc(((char *)p)-sizeof(size),size);
if(!new){
error(12);
raus();
}
if(DEBUG&32768){
printf("realloc %p to %p (s=%lu)\n",p,new,(unsigned long)size);
fflush(stdout);
}
if(DEBUG&65536){
remove_mb(p);
add_mb(new);
}
*(size_t *)new=size;
new=((char *)new)+sizeof(size);
return new;
}else{
new=realloc(p,size);
if(!new){
error(12);
raus();
}
if(DEBUG&32768){
printf("realloc %p to %p (s=%lu)\n",p,new,(unsigned long)size);
fflush(stdout);
}
if(DEBUG&65536){
remove_mb(p);
add_mb(new);
}
return new;
}
}
void myfree(void *p)
{
if(p&&dmalloc){
p=((char*)p)-sizeof(size_t);
memset(p,0xbb,*(size_t *)(p));
}
if(DEBUG&32768){
printf("free %p\n",p);
fflush(stdout);
}
if((DEBUG&65536)&&p) remove_mb(p);
(free)(p); /* supp.h has #define free(x) myfree(x) */
}
char *mystrdup(char *p)
{
char *new=mymalloc(strlen(p)+1);
strcpy(new,p);
return new;
}
/* Testet, ob zwei objs dieselben Register belegen. */
int collides(struct obj *x,struct obj *y)
{
int x1,x2,y1,y2;
if(!(x->flags®)||!(y->flags®)) return 0;
if(reg_pair(x->reg,&rp)){
x1=rp.r1;x2=rp.r2;
}else{
x1=x->reg;x2=-1;
}
if(reg_pair(y->reg,&rp)){
y1=rp.r1;y2=rp.r2;
}else{
y1=y->reg;y2=-2;
}
if(x1==y1||x1==y2||x2==y1||x2==y2)
return 1;
else
return 0;
}
/* Versucht, ein IC so zu drehen, dass q2 und z kein gemeinsames */
/* Register belegen. Liefert Null, wenn das nicht moeglich ist. */
int switch_IC(struct IC *p)
{
int c;
struct obj o;
if(!collides(&p->q2,&p->z)) return 1;
c=p->code;
if((c<OR||c>AND)&&c!=ADD&&c!=MULT&&c!=ADDI2P) return 0;
if(c==ADDI2P&&must_convert(q1typ(p),q2typ(p),0)) return 0;
if(collides(&p->q1,&p->z)) return 0;
o=p->q2;p->q2=p->q1;p->q1=o;
return 1;
}
void probj(FILE *f,struct obj *p,int t)
/* Gibt Objekt auf Bildschirm aus. */
{
if(p->am){ fprintf(f,"[tgt-addressing-mode]");return;}
if(p->flags&DREFOBJ){
fprintf(f,"([");
if(p->dtyp&CONST) fprintf(f,"const ");
if(p->dtyp&VOLATILE) fprintf(f,"volatile ");
fprintf(f,"%s]",typname[p->dtyp&NQ]);
}
if(p->flags&VARADR) fprintf(f,"#");
if(p->flags&VAR) {
printval(f,&p->val,MAXINT);
if(p->flags®){
fprintf(f,"+%s",regnames[p->reg]);
}else if(p->v->storage_class==AUTO||p->v->storage_class==REGISTER){
fprintf(f,"+%ld(FP)", zm2l(p->v->offset));
}else{
if(p->v->storage_class==STATIC){
fprintf(f,"+L%ld",zm2l(p->v->offset));
}else{
fprintf(f,"+_%s",p->v->identifier);
}
}
if(*p->v->identifier)
fprintf(f,"(%s)",p->v->identifier);
else if(p->v->description)
fprintf(f,"(%s)",p->v->description);
else
fprintf(f,"(%p)",(void *)p->v);
if(p->v->reg) fprintf(f,":%s",regnames[abs(p->v->reg)]);
}
if((p->flags®)&&!(p->flags&VAR)) fprintf(f,"%s",regnames[p->reg]);
if(p->flags&KONST){
fprintf(f,"#");
if(p->flags&DREFOBJ)
printval(f,&p->val,p->dtyp&NU);
else
printval(f,&p->val,t&NU);
}
if(p->flags&SCRATCH) fprintf(f,"[S]");
if(p->flags&DREFOBJ) fprintf(f,")");
}
void prl(FILE *o,struct struct_declaration *p)
/* Gibt eine struct_declaration auf dem Bildschirm aus. */
{
int i;
static int recurse=2;
int merk_recurse=recurse;
--recurse;
#ifdef HAVE_ECPP
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
/* removed */
#endif
for(i=0;i<p->count;i++){
fprintf(o," %d.:",i);
if(recurse>=0){
if((*p->sl)[i].identifier)fprintf(o," ident: %s; ",(*p->sl)[i].identifier);
prd(o,(*p->sl)[i].styp);
}
}
recurse=merk_recurse;
}
void prd(FILE *o,struct Typ *p)
/* Gibt einen Typ auf dem Bildschirm aus. */
{
int f;
if(!p) {fprintf(o,"empty type ");return;}
f=p->flags;
/* fprintf(o,"(Sizeof=%ld,flags=%d)",zl2l(szof(p)),f);*/
/* if(type_uncomplete(p)) {fprintf(o,"incomplete ");}*/
if(f&CONST) {fprintf(o,"const ");f&=~CONST;}
if(f&STRINGCONST) {fprintf(o,"string-const ");f&=~STRINGCONST;}
if(f&VOLATILE) {fprintf(o,"volatile ");f&=~VOLATILE;}
if(f&UNSIGNED) {fprintf(o,"unsigned ");f&=~UNSIGNED;}
if(p->attr) fprintf(o,"attr(%s) ",p->attr);
if(p->reg) fprintf(o,"reg(%s) ",regnames[p->reg]);
if(ISFUNC(f)){
fprintf(o,"%s with parameters (",typname[f&NQ]);
prl(o,p->exact);
fprintf(o,") returning ");prd(o,p->next);return;
}
if(ISSTRUCT(f)||ISUNION(f)){
fprintf(o,"%s with components {",typname[f&NQ]);
prl(o,p->exact); fprintf(o,"} ");
return;
}
if(ISPOINTER(f)) {fprintf(o,"%s to ",typname[f&NQ]);prd(o,p->next);return;}
if(ISARRAY(f)) {fprintf(o,"%s [size %ld] of ",typname[f&NQ],zm2l(p->size));prd(o,p->next);return;}
if(ISVECTOR(f)) {fprintf(o,"vector [size %ld] of %s",zm2l(p->size),typname[VECTYPE(f)&NQ]);return;}
fprintf(o,"%s",typname[f&NQ]);
}
void print_var(FILE *o,struct Var *p)
/* Gibt eine Variable aus. */
{
if(p->identifier&&*p->identifier){
fprintf(o, "ident: %s: ", p->identifier);
}
prd(o, p->vtyp);
}
/* returns the first base Type in an compound type */
int get_first_base_type(struct Typ *t)
{
if (!t) return 0;
if (ISARRAY(t->flags)) {
return get_first_base_type(t->next);
} else if ((ISSTRUCT(t->flags)) || (ISUNION(t->flags))) {
return get_first_base_type((*t->exact->sl)[0].styp);
} else return t->flags;
}
#ifndef HAVE_EXT_TYPES
void insert_const(union atyps *p,int t)
/* Traegt Konstante in entprechendes Feld ein. */
{
if(!p) ierror(0);
t&=NU;
if(t==CHAR) {p->vchar=vchar;return;}
if(t==SHORT) {p->vshort=vshort;return;}
if(t==INT) {p->vint=vint;return;}
if(t==LONG) {p->vlong=vlong;return;}
if(t==LLONG) {p->vllong=vllong;return;}
if(t==MAXINT) {p->vmax=vmax;return;}