forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
5695 lines (4605 loc) · 176 KB
/
compiler.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
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the GAP to C compiler.
*/
#include "compiler.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "code.h"
#include "error.h"
#include "exprs.h"
#include "gvars.h"
#include "integer.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "plist.h"
#include "records.h"
#include "stats.h"
#include "stringobj.h"
#include "sysopt.h"
#include "sysstr.h"
#include "vars.h"
#include <stdarg.h>
/****************************************************************************
**
*F * * * * * * * * * * * * * compilation flags * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V CompFastIntArith . . option to emit code that handles small ints. faster
*/
static Int CompFastIntArith;
/****************************************************************************
**
*V CompFastPlainLists . option to emit code that handles plain lists faster
*/
static Int CompFastPlainLists;
/****************************************************************************
**
*V CompFastListFuncs . . option to emit code that inlines calls to functions
*/
static Int CompFastListFuncs;
/****************************************************************************
**
*V CompCheckTypes . . . . option to emit code that assumes all types are ok.
*/
static Int CompCheckTypes;
/****************************************************************************
**
*V CompCheckListElements . option to emit code that assumes list elms exist
*/
static Int CompCheckListElements;
/****************************************************************************
**
*V CompPass . . . . . . . . . . . . . . . . . . . . . . . . . compiler pass
**
** 'CompPass' holds the number of the current pass.
**
** The compiler does two passes over the source.
**
** In the first pass it only collects information but emits no code.
**
** It finds out which global variables and record names are used, so that
** the compiler can output code to define and initialize global variables
** 'G_<name>' resp. 'R_<name>' to hold their identifiers.
**
** It finds out which arguments and local variables are used as higher
** variables from inside local functions, so that the compiler can output
** code to allocate and manage a stack frame for them.
**
** It finds out how many temporary variables are used, so that the compiler
** can output code to define corresponding local variables.
**
** In the second pass it emits code.
**
** The only difference between the first pass and the second pass is that
** 'Emit' emits no code during the first pass. While this causes many
** unnecessary computations during the first pass, the advantage is that
** the two passes are guaranteed to do exactly the same computations.
*/
static Int CompPass;
/****************************************************************************
**
*F * * * * * * * * * * * * temp, C, local functions * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V compilerMagic1 . . . . . . . . . . . . . . . . . . . . . current magic1
*/
static Int compilerMagic1;
/****************************************************************************
**
*V compilerMagic2 . . . . . . . . . . . . . . . . . . . . . current magic2
*/
static Obj compilerMagic2;
/****************************************************************************
**
*T CVar . . . . . . . . . . . . . . . . . . . . . . . type for C variables
**
** A C variable represents the result of compiling an expression. There are
** three cases (distinguished by the least significant two bits).
**
** If the expression is an immediate integer expression, the C variable
** contains the value of the immediate integer expression.
**
** If the expression is an immediate reference to a local variable, the C
** variable contains the index of the local variable.
**
** Otherwise the expression compiler emits code that puts the value of the
** expression into a temporary variable, and the C variable contains the
** index of that temporary variable.
*/
typedef UInt CVar;
#define IS_INTG_CVAR(c) ((((UInt)(c)) & 0x03) == 0x01)
#define INTG_CVAR(c) (((Int)(c)) >> 2)
#define CVAR_INTG(i) ((((UInt)(i)) << 2) + 0x01)
#define IS_TEMP_CVAR(c) ((((UInt)(c)) & 0x03) == 0x02)
#define TEMP_CVAR(c) (((UInt)(c)) >> 2)
#define CVAR_TEMP(l) ((((UInt)(l)) << 2) + 0x02)
#define IS_LVAR_CVAR(c) ((((UInt)(c)) & 0x03) == 0x03)
#define LVAR_CVAR(c) (((UInt)(c)) >> 2)
#define CVAR_LVAR(l) ((((UInt)(l)) << 2) + 0x03)
/****************************************************************************
**
*F SetInfoCVar( <cvar>, <type> ) . . . . . . . set the type of a C variable
*F GetInfoCVar( <cvar> ) . . . . . . . . . . . get the type of a C variable
*F HasInfoCVar( <cvar>, <type> ) . . . . . . . test the type of a C variable
**
*F NewInfoCVars() . . . . . . . . . allocate a new info bag for C variables
*F CopyInfoCVars( <dst>, <src> ) . . copy between info bags for C variables
*F MergeInfoCVars( <dst>, <src> ) . . . merge two info bags for C variables
*F IsEqInfoCVars( <dst>, <src> ) . . . compare two info bags for C variables
**
** With each function we associate a C variables information bag. In this
** bag we store the number of the function, the number of local variables,
** the number of local variables that are used as higher variables, the
** number of temporaries used, the current number of used temporaries.
**
** Furthermore for each local variable and temporary we store what we know
** about this local variable or temporary, i.e., whether the variable has an
** assigned value, whether that value is an integer, a boolean, etc.
**
** 'SetInfoCVar' sets the information for the C variable <cvar>.
** 'GetInfoCVar' gets the information for the C variable <cvar>.
** 'HasInfoCVar' returns true if the C variable <cvar> has the type <type>.
**
** 'NewInfoCVars' creates a new C variables information bag.
** 'CopyInfoCVars' copies the C variables information from <src> to <dst>.
** 'MergeInfoCVars' merges the C variables information from <src> to <dst>,
** i.e., if there are two paths to a certain place in the source and <dst>
** is the information gathered along one path and <src> is the information
** gathered along the other path, then 'MergeInfoCVars' stores in <dst> the
** information for that point (independent of the path travelled).
** 'IsEqInfoCVars' returns true if <src> and <dst> contain the same
** information.
**
** Note that the numeric values for the types are defined such that if
** <type1> implies <type2>, then <type1> is a bitwise superset of <type2>.
*/
typedef UInt4 LVar;
#define INFO_FEXP(fexp) PROF_FUNC(fexp)
#define SET_INFO_FEXP(fexp,x) SET_PROF_FUNC(fexp,x)
#define NEXT_INFO(info) PTR_BAG(info)[1]
#define NR_INFO(info) (*((Int*)(PTR_BAG(info)+2)))
#define NLVAR_INFO(info) (*((Int*)(PTR_BAG(info)+3)))
#define NHVAR_INFO(info) (*((Int*)(PTR_BAG(info)+4)))
#define NTEMP_INFO(info) (*((Int*)(PTR_BAG(info)+5)))
#define CTEMP_INFO(info) (*((Int*)(PTR_BAG(info)+6)))
#define TNUM_LVAR_INFO(info,i) (*((Int*)(PTR_BAG(info)+7+(i))))
#define TNUM_TEMP_INFO(info,i) \
(*((Int*)(PTR_BAG(info)+7+NLVAR_INFO(info)+(i))))
#define SIZE_INFO(nlvar,ntemp) (sizeof(Int) * (1 + 7 + (nlvar) + (ntemp)))
#define W_UNUSED 0 // TEMP is currently unused
#define W_HIGHER (1<<0) // LVAR is used as higher variable
#define W_UNKNOWN ((1<<1) | W_HIGHER)
#define W_UNBOUND ((1<<2) | W_UNKNOWN)
#define W_BOUND ((1<<3) | W_UNKNOWN)
#define W_INT ((1<<4) | W_BOUND)
#define W_INT_SMALL ((1<<5) | W_INT)
#define W_INT_POS ((1<<6) | W_INT)
#define W_BOOL ((1<<7) | W_BOUND)
#define W_FUNC ((1<<8) | W_BOUND)
#define W_LIST ((1<<9) | W_BOUND)
#define W_INT_SMALL_POS (W_INT_SMALL | W_INT_POS)
static void SetInfoCVar(CVar cvar, UInt type)
{
Bag info; // its info bag
// get the information bag
info = INFO_FEXP( CURR_FUNC() );
// set the type of a temporary
if ( IS_TEMP_CVAR(cvar) ) {
TNUM_TEMP_INFO( info, TEMP_CVAR(cvar) ) = type;
}
// set the type of a lvar (but do not change if it is a higher variable)
else if ( IS_LVAR_CVAR(cvar)
&& TNUM_LVAR_INFO( info, LVAR_CVAR(cvar) ) != W_HIGHER ) {
TNUM_LVAR_INFO( info, LVAR_CVAR(cvar) ) = type;
}
}
static Int GetInfoCVar(CVar cvar)
{
Bag info; // its info bag
// get the information bag
info = INFO_FEXP( CURR_FUNC() );
// get the type of an integer
if ( IS_INTG_CVAR(cvar) ) {
return ((0 < INTG_CVAR(cvar)) ? W_INT_SMALL_POS : W_INT_SMALL);
}
// get the type of a temporary
else if ( IS_TEMP_CVAR(cvar) ) {
return TNUM_TEMP_INFO( info, TEMP_CVAR(cvar) );
}
// get the type of a lvar
else if ( IS_LVAR_CVAR(cvar) ) {
return TNUM_LVAR_INFO( info, LVAR_CVAR(cvar) );
}
// hmm, avoid warning by compiler
else {
return 0;
}
}
static Int HasInfoCVar(CVar cvar, Int type)
{
return ((GetInfoCVar( cvar ) & type) == type);
}
static Bag NewInfoCVars(void)
{
Bag old;
Bag new;
old = INFO_FEXP( CURR_FUNC() );
new = NewBag( TNUM_BAG(old), SIZE_BAG(old) );
return new;
}
static void CopyInfoCVars(Bag dst, Bag src)
{
Int i;
if ( SIZE_BAG(dst) < SIZE_BAG(src) ) ResizeBag( dst, SIZE_BAG(src) );
if ( SIZE_BAG(src) < SIZE_BAG(dst) ) ResizeBag( src, SIZE_BAG(dst) );
NR_INFO(dst) = NR_INFO(src);
NLVAR_INFO(dst) = NLVAR_INFO(src);
NHVAR_INFO(dst) = NHVAR_INFO(src);
NTEMP_INFO(dst) = NTEMP_INFO(src);
CTEMP_INFO(dst) = CTEMP_INFO(src);
for ( i = 1; i <= NLVAR_INFO(src); i++ ) {
TNUM_LVAR_INFO(dst,i) = TNUM_LVAR_INFO(src,i);
}
for ( i = 1; i <= NTEMP_INFO(dst) && i <= NTEMP_INFO(src); i++ ) {
TNUM_TEMP_INFO(dst,i) = TNUM_TEMP_INFO(src,i);
}
}
static void MergeInfoCVars(Bag dst, Bag src)
{
Int i;
if ( SIZE_BAG(dst) < SIZE_BAG(src) ) ResizeBag( dst, SIZE_BAG(src) );
if ( SIZE_BAG(src) < SIZE_BAG(dst) ) ResizeBag( src, SIZE_BAG(dst) );
if ( NTEMP_INFO(dst)<NTEMP_INFO(src) ) NTEMP_INFO(dst)=NTEMP_INFO(src);
for ( i = 1; i <= NLVAR_INFO(src); i++ ) {
TNUM_LVAR_INFO(dst,i) &= TNUM_LVAR_INFO(src,i);
}
for ( i = 1; i <= NTEMP_INFO(dst) && i <= NTEMP_INFO(src); i++ ) {
TNUM_TEMP_INFO(dst,i) &= TNUM_TEMP_INFO(src,i);
}
}
static BOOL IsEqInfoCVars(Bag dst, Bag src)
{
Int i;
if ( SIZE_BAG(dst) < SIZE_BAG(src) ) ResizeBag( dst, SIZE_BAG(src) );
if ( SIZE_BAG(src) < SIZE_BAG(dst) ) ResizeBag( src, SIZE_BAG(dst) );
for ( i = 1; i <= NLVAR_INFO(src); i++ ) {
if ( TNUM_LVAR_INFO(dst,i) != TNUM_LVAR_INFO(src,i) ) {
return FALSE;
}
}
for ( i = 1; i <= NTEMP_INFO(dst) && i <= NTEMP_INFO(src); i++ ) {
if ( TNUM_TEMP_INFO(dst,i) != TNUM_TEMP_INFO(src,i) ) {
return FALSE;
}
}
return TRUE;
}
/****************************************************************************
**
*F NewTemp( <name> ) . . . . . . . . . . . . . . . allocate a new temporary
*F FreeTemp( <temp> ) . . . . . . . . . . . . . . . . . . free a temporary
**
** 'NewTemp' allocates a new temporary variable (<name> is currently
** ignored).
**
** 'FreeTemp' frees the temporary <temp>.
**
** Currently allocations and deallocations of temporaries are done in a
** strict nested (laff -- last allocated, first freed) order. This means we
** do not have to search for unused temporaries.
*/
typedef UInt4 Temp;
static Temp NewTemp(const Char * name)
{
Temp temp; // new temporary, result
Bag info; // information bag
// get the information bag
info = INFO_FEXP( CURR_FUNC() );
// take the next available temporary
CTEMP_INFO( info )++;
temp = CTEMP_INFO( info );
// maybe make room for more temporaries
if ( NTEMP_INFO( info ) < temp ) {
if ( SIZE_BAG(info) < SIZE_INFO( NLVAR_INFO(info), temp ) ) {
ResizeBag( info, SIZE_INFO( NLVAR_INFO(info), temp+7 ) );
}
NTEMP_INFO( info ) = temp;
}
TNUM_TEMP_INFO( info, temp ) = W_UNKNOWN;
// return the temporary
return temp;
}
static void FreeTemp(Temp temp)
{
Bag info; // information bag
// get the information bag
info = INFO_FEXP( CURR_FUNC() );
// check that deallocations happens in the correct order
if ( temp != CTEMP_INFO( info ) && CompPass == 2 ) {
Pr("PROBLEM: freeing t_%d, should be t_%d\n",(Int)temp,CTEMP_INFO(info));
}
// free the temporary
TNUM_TEMP_INFO( info, temp ) = W_UNUSED;
CTEMP_INFO( info )--;
}
/****************************************************************************
**
*F CompSetUseHVar( <hvar> ) . . . . . . . . register use of higher variable
*F CompGetUseHVar( <hvar> ) . . . . . . . . get use mode of higher variable
*F GetLevlHVar( <hvar> ) . . . . . . . . . . . get level of higher variable
*F GetIndxHVar( <hvar> ) . . . . . . . . . . . get index of higher variable
**
** 'CompSetUseHVar' register (during pass 1) that the variable <hvar> is
** used as higher variable, i.e., is referenced from inside a local
** function. Such variables must be allocated in a stack frame bag (and
** cannot be mapped to C variables).
**
** 'CompGetUseHVar' returns nonzero if the variable <hvar> is used as higher
** variable.
**
** 'GetLevlHVar' returns the level of the higher variable <hvar>, i.e., the
** number of frames that must be walked upwards for the one containing
** <hvar>. This may be properly smaller than 'LEVEL_HVAR(<hvar>)', because
** only those compiled functions that have local variables that are used as
** higher variables allocate a stack frame.
**
** 'GetIndxHVar' returns the index of the higher variable <hvar>, i.e., the
** position of <hvar> in the stack frame. This may be properly smaller than
** 'INDEX_HVAR(<hvar>)', because only those local variable that are used as
** higher variables are allocated in a stack frame.
*/
typedef UInt4 HVar;
static void CompSetUseHVar(HVar hvar)
{
Bag info; // its info bag
Int i; // loop variable
// only mark in pass 1
if ( CompPass != 1 ) return;
// walk up
info = INFO_FEXP( CURR_FUNC() );
for ( i = 1; i <= (hvar >> 16); i++ ) {
info = NEXT_INFO( info );
}
// set mark
if ( TNUM_LVAR_INFO( info, (hvar & 0xFFFF) ) != W_HIGHER ) {
TNUM_LVAR_INFO( info, (hvar & 0xFFFF) ) = W_HIGHER;
NHVAR_INFO(info) = NHVAR_INFO(info) + 1;
}
}
static Int CompGetUseHVar(HVar hvar)
{
Bag info; // its info bag
Int i; // loop variable
// walk up
info = INFO_FEXP( CURR_FUNC() );
for ( i = 1; i <= (hvar >> 16); i++ ) {
info = NEXT_INFO( info );
}
// get mark
return (TNUM_LVAR_INFO( info, (hvar & 0xFFFF) ) == W_HIGHER);
}
static UInt GetLevlHVar(HVar hvar)
{
UInt levl; // level of higher variable
Bag info; // its info bag
Int i; // loop variable
// walk up
levl = 0;
info = INFO_FEXP( CURR_FUNC() );
levl++;
for ( i = 1; i <= (hvar >> 16); i++ ) {
info = NEXT_INFO( info );
levl++;
}
// return level (the number steps to go up)
return levl - 1;
}
static UInt GetIndxHVar(HVar hvar)
{
UInt indx; // index of higher variable
Bag info; // its info bag
Int i; // loop variable
// walk up
info = INFO_FEXP( CURR_FUNC() );
for ( i = 1; i <= (hvar >> 16); i++ ) {
info = NEXT_INFO( info );
}
// walk right
indx = 0;
for ( i = 1; i <= (hvar & 0xFFFF); i++ ) {
if ( TNUM_LVAR_INFO( info, i ) == W_HIGHER ) indx++;
}
// return the index
return indx;
}
/****************************************************************************
**
*F CompSetUseGVar( <gvar>, <mode> ) . . . . register use of global variable
*F CompGetUseGVar( <gvar> ) . . . . . . . . get use mode of global variable
**
** 'CompSetUseGVar' registers (during pass 1) the use of the global variable
** with identifier <gvar>.
**
** 'CompGetUseGVar' returns the bitwise OR of all the <mode> arguments for
** the global variable with identifier <gvar>.
**
** Currently the interpretation of the <mode> argument is as follows
**
** If '<mode> & COMP_USE_GVAR_ID' is nonzero, then the produced code shall
** define and initialize 'G_<name>' with the identifier of the global
** variable (which may be different from <gvar> by the time the compiled
** code is actually run).
**
** If '<mode> & COMP_USE_GVAR_COPY' is nonzero, then the produced code shall
** define and initialize 'GC_<name>' as a copy of the global variable
** (see 'InitCopyGVar' in 'gvars.h').
**
** If '<mode> & COMP_USE_GVAR_FOPY' is nonzero, then the produced code shall
** define and initialize 'GF_<name>' as a function copy of the global
** variable (see 'InitFopyGVar' in 'gvars.h').
*/
typedef UInt GVar;
#define COMP_USE_GVAR_ID (1 << 0)
#define COMP_USE_GVAR_COPY (1 << 1)
#define COMP_USE_GVAR_FOPY (1 << 2)
static Bag CompInfoGVar;
static void CompSetUseGVar(GVar gvar, UInt mode)
{
// only mark in pass 1
if ( CompPass != 1 ) return;
// resize if necessary
if ( SIZE_OBJ(CompInfoGVar)/sizeof(UInt) <= gvar ) {
ResizeBag( CompInfoGVar, sizeof(UInt)*(gvar+1) );
}
// or with <mode>
((UInt*)PTR_BAG(CompInfoGVar))[gvar] |= mode;
}
static UInt CompGetUseGVar(GVar gvar)
{
return ((UInt*)PTR_BAG(CompInfoGVar))[gvar];
}
/****************************************************************************
**
*F CompSetUseRNam( <rnam>, <mode> ) . . . . . . register use of record name
*F CompGetUseRNam( <rnam> ) . . . . . . . . . . get use mode of record name
**
** 'CompSetUseRNam' registers (during pass 1) the use of the record name
** with identifier <rnam>. 'CompGetUseRNam' returns the bitwise OR of all
** the <mode> arguments for the global variable with identifier <rnam>.
**
** Currently the interpretation of the <mode> argument is as follows
**
** If '<mode> & COMP_USE_RNAM_ID' is nonzero, then the produced code shall
** define and initialize 'R_<name>' with the identifier of the record name
** (which may be different from <rnam> when the time the compiled code is
** actually run).
*/
typedef UInt RNam;
#define COMP_USE_RNAM_ID (1 << 0)
static Bag CompInfoRNam;
static void CompSetUseRNam(RNam rnam, UInt mode)
{
// only mark in pass 1
if ( CompPass != 1 ) return;
// resize if necessary
if ( SIZE_OBJ(CompInfoRNam)/sizeof(UInt) <= rnam ) {
ResizeBag( CompInfoRNam, sizeof(UInt)*(rnam+1) );
}
// or with <mode>
((UInt*)PTR_BAG(CompInfoRNam))[rnam] |= mode;
}
static UInt CompGetUseRNam(RNam rnam)
{
return ((UInt*)PTR_BAG(CompInfoRNam))[rnam];
}
/****************************************************************************
**
*F Emit( <fmt>, ... ) . . . . . . . . . . . . . . . . . . . . . . emit code
**
** 'Emit' outputs the string <fmt> and the other arguments, which must
** correspond to the '%' format elements in <fmt>. Nothing is actually
** outputted if 'CompPass' is not 2.
**
** 'Emit' supports the following '%' format elements:
** - '%d' formats an integer,
** - '%s' formats a string,
** - '%S' formats a string with all the necessary escapes,
** - '%g' formats a GAP string,
** - '%G' formats a GAP string with all the necessary escapes,
** - '%C' does the same but uses only valid C escapes,
** - '%n' formats a name ('_' is converted to '__', special characters are
** converted to '_<hex1><hex2>')
** - '%c' formats a C variable ('INTOBJ_INT(<int>)' for integers, 'a_<name>'
** for arguments, 'l_<name>' for locals, 't_<nr>' for temporaries),
** - '%i' formats a C variable as an integer ('<int>' for integers, and for
** everything else the same as INT_INTOBJ(%c) would produce
** - '%%' outputs a single '%'.
*/
static Int EmitIndent;
static Int EmitIndent2;
static void Emit(const char * fmt, ...)
{
Int narg; // number of arguments
va_list ap; // argument list pointer
Int dint; // integer argument
CVar cvar; // C variable argument
Char * string; // string argument
const Char * p; // loop variable
const Char * hex = "0123456789ABCDEF";
// are we in pass 2?
if ( CompPass != 2 ) return;
// get the information bag
narg = NARG_FUNC( CURR_FUNC() );
if (narg < 0) {
narg = -narg;
}
// loop over the format string
va_start( ap, fmt );
for ( p = fmt; *p != '\0'; p++ ) {
// print an indent, except for preprocessor commands
if ( *fmt != '#' ) {
if ( 0 < EmitIndent2 && *p == '}' ) EmitIndent2--;
while ( 0 < EmitIndent2-- ) Pr(" ", 0, 0);
}
// format an argument
if ( *p == '%' ) {
p++;
// emit an integer
if ( *p == 'd' ) {
dint = va_arg( ap, Int );
Pr("%d", dint, 0);
}
// emit a C string
else if ( *p == 's' || *p == 'S' ) {
const Char f[] = { '%', *p, 0 };
string = va_arg( ap, Char* );
Pr(f, (Int)string, 0);
}
// emit a GAP string
else if ( *p == 'g' || *p == 'G' || *p == 'C' ) {
const Char f[] = { '%', *p, 0 };
Obj str = va_arg( ap, Obj );
Pr(f, (Int)str, 0);
}
// emit a name
else if ( *p == 'n' ) {
Obj str = va_arg( ap, Obj );
UInt i = 0;
Char c;
while ((c = CONST_CSTR_STRING(str)[i++])) {
if ( IsAlpha(c) || IsDigit(c) ) {
Pr("%c", (Int)c, 0);
}
else if ( c == '_' ) {
Pr("__", 0, 0);
}
else {
Pr("_%c%c",hex[((UInt)c)/16],hex[((UInt)c)%16]);
}
}
}
// emit a C variable
else if ( *p == 'c' ) {
cvar = va_arg( ap, CVar );
if ( IS_INTG_CVAR(cvar) ) {
Int x = INTG_CVAR(cvar);
if (x >= -(1 << 28) && x < (1 << 28))
Pr("INTOBJ_INT(%d)", x, 0);
else
Pr("ObjInt_Int8(%d)", x, 0);
}
else if ( IS_TEMP_CVAR(cvar) ) {
Pr("t_%d", TEMP_CVAR(cvar), 0);
}
else if ( LVAR_CVAR(cvar) <= narg ) {
Emit( "a_%n", NAME_LVAR( LVAR_CVAR(cvar) ) );
}
else {
Emit( "l_%n", NAME_LVAR( LVAR_CVAR(cvar) ) );
}
}
// emit a C variable
else if ( *p == 'i' ) {
cvar = va_arg( ap, CVar );
if ( IS_INTG_CVAR(cvar) ) {
Pr("%d", INTG_CVAR(cvar), 0);
}
else if ( IS_TEMP_CVAR(cvar) ) {
Pr("Int_ObjInt(t_%d)", TEMP_CVAR(cvar), 0);
}
else if ( LVAR_CVAR(cvar) <= narg ) {
Emit( "Int_ObjInt(a_%n)", NAME_LVAR( LVAR_CVAR(cvar) ) );
}
else {
Emit( "Int_ObjInt(l_%n)", NAME_LVAR( LVAR_CVAR(cvar) ) );
}
}
// emit a '%'
else if ( *p == '%' ) {
Pr("%%", 0, 0);
}
// what
else {
Pr("%%illegal format statement", 0, 0);
}
}
else if ( *p == '{' ) {
Pr("{", 0, 0);
EmitIndent++;
}
else if ( *p == '}' ) {
Pr("}", 0, 0);
EmitIndent--;
}
else if ( *p == '\n' ) {
Pr("\n", 0, 0);
EmitIndent2 = EmitIndent;
}
else {
Pr("%c", (Int)(*p), 0);
}
}
va_end( ap );
}
/****************************************************************************
**
*F * * * * * * * * * * * * * * compile checks * * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F CompCheckBound( <obj>, <name> ) emit code to check that <obj> has a value
*/
static void CompCheckBound(CVar obj, Obj name)
{
if ( ! HasInfoCVar( obj, W_BOUND ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_BOUND( %c, \"%g\" );\n", obj, name );
}
SetInfoCVar( obj, W_BOUND );
}
}
/****************************************************************************
**
*F CompCheckFuncResult( <obj> ) . emit code to check that <obj> has a value
*/
static void CompCheckFuncResult(CVar obj)
{
if ( ! HasInfoCVar( obj, W_BOUND ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_FUNC_RESULT( %c );\n", obj );
}
SetInfoCVar( obj, W_BOUND );
}
}
/****************************************************************************
**
*F CompCheckIntSmall( <obj> ) emit code to check that <obj> is a small int
*/
static void CompCheckIntSmall(CVar obj)
{
if ( ! HasInfoCVar( obj, W_INT_SMALL ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_INT_SMALL( %c );\n", obj );
}
SetInfoCVar( obj, W_INT_SMALL );
}
}
/****************************************************************************
**
*F CompCheckIntSmallPos( <obj> ) emit code to check that <obj> is a position
*/
static void CompCheckIntSmallPos(CVar obj)
{
if ( ! HasInfoCVar( obj, W_INT_SMALL_POS ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_INT_SMALL_POS( %c );\n", obj );
}
SetInfoCVar( obj, W_INT_SMALL_POS );
}
}
/****************************************************************************
**
*F CompCheckIntPos( <obj> ) emit code to check that <obj> is a position
*/
static void CompCheckIntPos(CVar obj)
{
if ( ! HasInfoCVar( obj, W_INT_POS ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_INT_POS( %c );\n", obj );
}
SetInfoCVar( obj, W_INT_POS );
}
}
/****************************************************************************
**
*F CompCheckBool( <obj> ) . . . emit code to check that <obj> is a boolean
*/
static void CompCheckBool(CVar obj)
{
if ( ! HasInfoCVar( obj, W_BOOL ) ) {
if ( CompCheckTypes ) {
Emit( "CHECK_BOOL( %c );\n", obj );
}
SetInfoCVar( obj, W_BOOL );
}
}
/****************************************************************************
**
*F * * * * * * * * * * * * compile expressions * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F CompExpr( <expr> ) . . . . . . . . . . . . . . . . compile an expression
**
** 'CompExpr' compiles the expression <expr> and returns the C variable that
** will contain the result.
*/
static CVar (*CompExprFuncs[256])(Expr expr);
static CVar CompExpr(Expr expr)
{
return (* CompExprFuncs[ TNUM_EXPR(expr) ])( expr );
}
/****************************************************************************
**
*F CompUnknownExpr( <expr> ) . . . . . . . . . . . . log unknown expression
*/
static CVar CompUnknownExpr(Expr expr)
{
Emit( "CANNOT COMPILE EXPRESSION OF TNUM %d;\n", TNUM_EXPR(expr) );
return 0;
}
/****************************************************************************
**
*F CompBoolExpr( <expr> ) . . . . . . . compile bool expr and return C bool
*/
static CVar (*CompBoolExprFuncs[256])(Expr expr);
static CVar CompBoolExpr(Expr expr)
{
return (* CompBoolExprFuncs[ TNUM_EXPR(expr) ])( expr );
}
/****************************************************************************
**
*F CompUnknownBool( <expr> ) . . . . . . . . . . use 'CompExpr' and convert
*/
static CVar CompUnknownBool(Expr expr)
{
CVar res; // result
CVar val; // value of expression
// allocate a new temporary for the result
res = CVAR_TEMP( NewTemp( "res" ) );
// compile the expression and check that the value is boolean
val = CompExpr( expr );
CompCheckBool( val );
// emit code to store the C boolean value in the result
Emit( "%c = (Obj)(UInt)(%c != False);\n", res, val );
// we know that the result is boolean (should be 'W_CBOOL')
SetInfoCVar( res, W_BOOL );
// free the temporary
if ( IS_TEMP_CVAR( val ) ) FreeTemp( TEMP_CVAR( val ) );
return res;
}
/****************************************************************************
**
*V G_Length . . . . . . . . . . . . . . . . . . . . . . . function 'Length'
*/
static GVar G_Length;
/****************************************************************************
**
*F CompFunccall0to6Args( <expr> ) . . . EXPR_FUNCCALL_0ARGS...EXPR_FUNCCALL_6ARGS
*/
static CVar CompRefGVarFopy(Expr expr);
static CVar CompFunccall0to6Args(Expr expr)
{
CVar result; // result, result
CVar func; // function
CVar args [8]; // arguments
Int narg; // number of arguments
Int i; // loop variable
// special case to inline 'Length'
if ( CompFastListFuncs
&& TNUM_EXPR( FUNC_CALL(expr) ) == EXPR_REF_GVAR
&& READ_EXPR( FUNC_CALL(expr), 0 ) == G_Length
&& NARG_SIZE_CALL(SIZE_EXPR(expr)) == 1 ) {
result = CVAR_TEMP( NewTemp( "result" ) );
args[1] = CompExpr( ARGI_CALL(expr,1) );
if ( CompFastPlainLists ) {
Emit( "C_LEN_LIST_FPL( %c, %c )\n", result, args[1] );
}
else {
Emit( "C_LEN_LIST( %c, %c )\n", result, args[1] );
}
SetInfoCVar( result, W_INT_SMALL );
if ( IS_TEMP_CVAR( args[1] ) ) FreeTemp( TEMP_CVAR( args[1] ) );
return result;
}
// allocate a temporary for the result
result = CVAR_TEMP( NewTemp( "result" ) );
// compile the reference to the function
if ( TNUM_EXPR( FUNC_CALL(expr) ) == EXPR_REF_GVAR ) {
func = CompRefGVarFopy( FUNC_CALL(expr) );
}
else {
func = CompExpr( FUNC_CALL(expr) );
}
// compile the argument expressions
narg = NARG_SIZE_CALL(SIZE_EXPR(expr));
for ( i = 1; i <= narg; i++ ) {
args[i] = CompExpr( ARGI_CALL(expr,i) );
}