-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsupp.c
More file actions
2009 lines (1918 loc) · 47.6 KB
/
supp.c
File metadata and controls
2009 lines (1918 loc) · 47.6 KB
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
/* $VER: vbcc (supp.c) $Revision: 1.47 $ */
#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];
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;
IC *err_ic;
long maxoptpasses=30;
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;
long clist_copy_stack=6;
long clist_copy_static=6;
long clist_copy_pointer=20;
long inline_memcpy_sz=INLINEMEMCPY;
int fp_assoc,noaliasopt,noitra;
char *filename;
IC *first_ic,*last_ic;
int float_used;
bvtype regs_modified[RSIZE/sizeof(bvtype)];
/* Das haette ich gern woanders */
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;
static int get_scalar_byte(int f,union atyps *v,zmax n, zuchar *out)
/* Yields byte n of a constant */
{
if(!zmleq(l2zm(0L),n)) ierror(0);
f&=NQ;
if(f>=CHAR&&f<=LLONG){
int j;
eval_const(v,f);
if(LITTLEENDIAN)
j=zm2l(n);
else
j=(int)sizetab[f]-(int)zm2l(n)-1;
while(j){
vumax=zumrshift(vumax,char_bit);
j--;
}
*out=zum2zuc(vumax);
return 1;
}else
return 0;
}
zumax get_clist_int(type *t, const_list *cl, zmax n, int sz,int *state)
/* yields an integer of size sz from offset n */
{
zuchar zuc;
zumax val = ZU0;
if(LITTLEENDIAN)
n=zmadd(n,l2zm((long)(sz-1)));
while(sz){
val=zumlshift(val,char_bit);
if(!get_clist_byte(t,cl,n,&zuc)) {*state=0;return ZU0;}
val=zumadd(val,zuc2zum(zuc));
sz--;
n=LITTLEENDIAN?zmsub(n,Z1):zmadd(n,Z1);
}
*state=1;
return val;
}
int get_clist_byte(type *t,const_list *cl, zmax n, zuchar *out)
/* Yields byte n from a const list */
{
if(!cl){
*out=zum2zuc(ul2zum(0UL));
return 1;
}
if(ISARRAY(t->flags)){
zmax i,j,x;
x=szof(t->next);
i=zmdiv(n,x);
while(1){
if(!cl) break;
if(zmleq(i,cl->idx)) break;
cl=cl->next;
}
if(!cl||!zmeqto(cl->idx,i)){
*out=zum2zuc(ul2zum(0UL));
return 1;
}
return get_clist_byte(t->next,cl->other,zmmod(n,x),out);
}
if(ISUNION(t->flags)){
int i=zm2l(cl->idx);
if(cl->tree) return 0;
return get_clist_byte((*t->exact->sl)[i].styp,cl->other,n,out);
}
if(ISSTRUCT(t->flags)){
zmax al;int fl;type *st;
int i,bfo,bfs;zmax sz;zumax bfval=ul2zum(0UL);
sz=l2zm(0L);
if(cl&&cl->tree){
/* initialized by another */
return 0;
}else{
for(i=0;i<t->exact->count&&cl;i++){
if(!cl->other){ierror(0);}
st=(*t->exact->sl)[i].styp;
al=(*t->exact->sl)[i].align;
if(!(*t->exact->sl)[i].identifier) ierror(0);
bfo=(*t->exact->sl)[i].bfoffset;
if(!zmeqto(zmmod(sz,al),l2zm(0L))){
sz=zmadd(sz,zmsub(al,zmmod(sz,al)));
if(!zmleq(sz,n)){
*out=zum2zuc(ul2zum(0L));
return 1;
}
}
if(bfo>=0){
/* bitfield */
if((*t->exact->sl)[i].identifier[0]){
bfs=(*t->exact->sl)[i].bfsize;
if(zmeqto(l2zm((long)i),cl->idx)){
eval_const(&cl->other->val,st->flags);
cl=cl->next;
}else{
vumax=ul2zum(0UL);
}
vumax=zumand(vumax,zumsub(zumlshift(ul2zum(1UL),ul2zum((unsigned long)bfs)),ul2zum(1UL)));
bfval=zumor(bfval,zumlshift(vumax,ul2zum((unsigned long)bflayout(bfo,bfs,st->flags))));
}
if(i+1>=t->exact->count||(*t->exact->sl)[i+1].bfoffset<=0||!cl){
/* last bitfield in integer */
gval.vumax=bfval;
eval_const(&gval,UNSIGNED|MAXINT);
insert_const(&gval,st->flags&NU);
bfval=ul2zum(0L);
sz=zmadd(sz,szof(st));
if(!zmleq(sz,n))
return get_scalar_byte(st->flags,&gval,zmsub(n,zmsub(sz,szof(st))),out);
}
}else{
sz=zmadd(sz,szof(st));
if(!zmleq(sz,n)){
if(zmeqto(l2zm((long)i),cl->idx)){
return get_clist_byte(st,cl->other,zmsub(n,zmsub(sz,szof(st))),out);
}else{
*out=zum2zuc(ul2zum(0UL));
return 1;
}
}
if(zmeqto(l2zm((long)i),cl->idx))
cl=cl->next;
}
}
}
*out=zum2zuc(ul2zum(0UL));
return 1;
}
if(zmeqto(cl->idx,l2zm(-1L)))
return 0;
else{
if(cl->tree)
return 0;
else
return get_scalar_byte(t->flags,&cl->val,n,out);
}
ierror(0);
}
type *new_typ(void)
/* Erzeigt neuen (leeren) Typ. */
{
type *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;
}
type *clone_typ(type *old)
/* Erzeugt Kopie eines Typs und liefert Zeiger auf Kopie. */
{
type *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;
}
Var *new_var(void)
{
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;
}
IC *new_IC(void)
{
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 */
IC *clone_ic(IC *p)
{
IC *new,*first,*last;
first=last=new=0;
while(p){
new=mymalloc(sizeof(*new));
*new=*p;
p->copy=new;
if(p->q1.am||p->q2.am||p->z.am) ierror(0);
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++){
if(!p->arg_list[i])
ierror(0);
if(!p->arg_list[i]->copy)
ierror(0);
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(IC *p)
/* Gibt IC-Liste inkl. Typen frei. */
{
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&&p->q1.am==p->q2.am)
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(IC *after,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(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(type *p)
/* Gibt eine Typ-Liste frei, aber keine struct_declaration oder so. */
{
int f;type *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(type *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(type *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 */
Var *vlength_szof(type *t)
{
IC *new;type *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(type *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++){
type *h=(*t->exact->sl)[j].styp;
if((*t->exact->sl)[j].bfoffset<=0){
int n;size_t al;
al=(*t->exact->sl)[j].align;
#ifdef HAVE_ECPP
/* removed */
#endif
if(zmeqto(al,l2zm(0L))) {prd(stdout,h);ierror(0);}
m=szof(h);
/* find the largest type in a bitfield */
for(n=j+1;n<t->exact->count&&(*t->exact->sl)[n].bfoffset>0;n++){
size_t tmp=szof((*t->exact->sl)[n].styp);
if(zmleq(m,tmp))
m=tmp;
}
size=zmmult(zmdiv(zmadd(size,zmsub(al,l2zm(1L))),al),al);
/*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_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,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);
}
}
if(p->flags&EFF_IC) fprintf(f," (eff_ic)");
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,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");
}
typedef struct memblock {struct memblock *next;void *p;} memblock;
static memblock *first_mb;
static void add_mb(void *p)
{
memblock *mb_second=first_mb;
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)
{
memblock *mb_prev=0;
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(obj *x,obj *y)
{
int x1,x2,y1,y2;