-
Notifications
You must be signed in to change notification settings - Fork 2
/
smlrc.c
9444 lines (8432 loc) · 249 KB
/
smlrc.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
/*
Copyright (c) 2012-2016, Alexey Frunze
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************/
/* */
/* Smaller C */
/* */
/* A simple and small single-pass C compiler */
/* */
/* Produces 16/32-bit 80386 assembly output for NASM. */
/* Produces 32-bit MIPS assembly output for gcc/as. */
/* Produces 32-bit TR3200 assembly output for vasm. */
/* */
/* Main file */
/* */
/*****************************************************************************/
// Making most functions static helps with code optimization,
// use that to further reduce compiler's code size on RetroBSD.
#include "ff.h"
#include "videoout.h"
#include "xprintf.h"
#include "ff_wrap.h"
#include "nativec.h"
#define printf2(fmt,...) fprintf(OutFile,(fmt),##__VA_ARGS__)
#ifndef STATIC
#define STATIC
#else
#undef STATIC
#define STATIC static
#endif
#ifdef NO_EXTRAS
#define NO_PPACK
#define NO_TYPEDEF_ENUM
#define NO_FUNC_
#define NO_EXTRA_WARNS
#define NO_FOR_DECL
#define NO_STRUCT_BY_VAL
#define NO_FP
#endif
// Passing and returning structures by value is currenly supported
// on x86 and MIPS only
#ifdef TR3200
#ifndef NO_STRUCT_BY_VAL
#define NO_STRUCT_BY_VAL
#endif
#endif
#ifndef __SMALLER_C__
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#if UINT_MAX >= 0xFFFFFFFF
#define CAN_COMPILE_32BIT
#endif
#else // #ifndef __SMALLER_C__
#define NULL 0
#define size_t unsigned int
#define CHAR_BIT (8)
#ifdef __SMALLER_C_SCHAR__
#define CHAR_MIN (-128)
#define CHAR_MAX (127)
#endif
#ifdef __SMALLER_C_UCHAR__
#define CHAR_MIN (0)
#define CHAR_MAX (255)
#endif
#ifndef __SMALLER_C_SCHAR__
#ifndef __SMALLER_C_UCHAR__
#error __SMALLER_C_SCHAR__ or __SMALLER_C_UCHAR__ must be defined
#endif
#endif
#ifdef __SMALLER_C_16__
#define INT_MAX (32767)
#define INT_MIN (-32767-1)
#define UINT_MAX (65535u)
#define UINT_MIN (0u)
#endif
#ifdef __SMALLER_C_32__
#define INT_MAX (2147483647)
#define INT_MIN (-2147483647-1)
#define UINT_MAX (4294967295u)
#define UINT_MIN (0u)
#define CAN_COMPILE_32BIT
#endif
#ifndef __SMALLER_C_16__
#ifndef __SMALLER_C_32__
#error __SMALLER_C_16__ or __SMALLER_C_32__ must be defined
#endif
#endif
#define EXIT_FAILURE 1
void exit(int);
int atoi(const char*);
size_t strlen(const char*);
char* strcpy(char*, const char*);
char* strchr(const char*, int);
int strcmp(const char*, const char*);
int strncmp(const char*, const char*, size_t);
void* memmove(void*, void*, size_t);
void* memcpy(void*, const void*, size_t);
void* memset(void*, int, size_t);
int memcmp(void*, void*, size_t);
int isspace(int);
int isdigit(int);
int isalpha(int);
int isalnum(int);
#ifndef FILE
#define FILE void
#endif
#ifndef EOF
#define EOF (-1)
#endif
static
int puts2(char*);
struct fpos_t_
{
union
{
unsigned short halves[2]; // for 16-bit memory models without 32-bit longs
int align; // for alignment on machine word boundary
} u;
}; // keep in sync with stdio.h !!!
#define fpos_t struct fpos_t_
#endif // #ifndef __SMALLER_C__
// Floating point requires 32 bits
#ifndef CAN_COMPILE_32BIT
#ifndef NO_FP
#define NO_FP
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// all public macros
#ifndef MAX_IDENT_LEN
#define MAX_IDENT_LEN 63
#endif
#ifndef MAX_STRING_LEN
#define MAX_STRING_LEN 255
#endif
#define MAX_CHAR_QUEUE_LEN (MAX_STRING_LEN + 1)
#ifndef MAX_MACRO_TABLE_LEN
#define MAX_MACRO_TABLE_LEN (8192)
#endif
#ifndef MAX_IDENT_TABLE_LEN
#define MAX_IDENT_TABLE_LEN (4096+1024+512) // must be greater than MAX_IDENT_LEN
#endif
#ifndef SYNTAX_STACK_MAX
#define SYNTAX_STACK_MAX (2048+1024)
#endif
#ifndef MAX_FILE_NAME_LEN
#define MAX_FILE_NAME_LEN 30
#endif
#ifndef NO_PREPROCESSOR
#define MAX_INCLUDES 8
#define PREP_STACK_SIZE 8
#define MAX_SEARCH_PATH 256
#else
#define MAX_INCLUDES 1
#define PREP_STACK_SIZE 1
#define MAX_SEARCH_PATH 1
#undef MAX_STRING_LEN
#define MAX_STRING_LEN (MAX_FILE_NAME_LEN + 3)
#endif
/* +-~* /% &|^! << >> && || < <= > >= == != () *[] ++ -- = += -= ~= *= /= %= &= |= ^= <<= >>= {} ,;: -> ... */
#define tokEof 0
#define tokNumInt 1
#define tokNumUint 2
#define tokLitStr 3
#define tokLShift 4
#define tokRShift 5
#define tokLogAnd 6
#define tokLogOr 7
#define tokEQ 8
#define tokNEQ 9
#define tokLEQ 10
#define tokGEQ 11
#define tokInc 12
#define tokDec 13
#define tokArrow 14
#define tokEllipsis 15
#define tokIdent 16
#define tokVoid 17
#define tokChar 18
#define tokInt 19
#define tokReturn 20
#define tokGoto 21
#define tokIf 22
#define tokElse 23
#define tokWhile 24
#define tokCont 25
#define tokBreak 26
#define tokSizeof 27
#define tokAssignMul 'A'
#define tokAssignDiv 'B'
#define tokAssignMod 'C'
#define tokAssignAdd 'D'
#define tokAssignSub 'E'
#define tokAssignLSh 'F'
#define tokAssignRSh 'G'
#define tokAssignAnd 'H'
#define tokAssignXor 'I'
#define tokAssignOr 'J'
#define tokFloat 'a'
#define tokDouble 'b'
#define tokLong 'c'
#define tokShort 'd'
#define tokUnsigned 'e'
#define tokSigned 'f'
#define tokConst 'g'
#define tokVolatile 'h'
#define tokRestrict 'i'
#define tokStatic 'j'
#define tokInline 'k'
#define tokExtern 'l'
#define tokAuto 'm'
#define tokRegister 'n'
#define tokTypedef 'o'
#define tokEnum 'p'
#define tokStruct 'q'
#define tokUnion 'r'
#define tokDo 's'
#define tokFor 't'
#define tokSwitch 'u'
#define tokCase 'v'
#define tokDefault 'w'
#define tok_Bool 'x'
#define tok_Complex 'y'
#define tok_Imagin 'z'
#define tok_Asm '`'
/* Pseudo-tokens (converted from others or generated) */
#define tokURShift 28
#define tokUDiv 29
#define tokUMod 30
#define tokAssignURSh 31
#define tokAssignUDiv '@'
#define tokAssignUMod 'K'
#define tokComma '0'
#define tokIfNot 'L'
#define tokUnaryAnd 'M'
#define tokUnaryStar 'N'
#define tokUnaryPlus 'O'
#define tokUnaryMinus 'P'
#define tokPostInc 'Q'
#define tokPostDec 'R'
#define tokPostAdd 'S'
#define tokPostSub 'T'
#define tokULess 'U'
#define tokUGreater 'V'
#define tokULEQ 'W'
#define tokUGEQ 'X'
#define tokLocalOfs 'Y'
#define tokShortCirc 'Z'
#define tokSChar 0x80
#define tokUChar 0x81
#define tokUShort 0x82
#define tokULong 0x83
//#define tokLongLong 0x84
//#define tokULongLong 0x85
//#define tokLongDbl 0x86
#define tokGotoLabel 0x8F
#define tokStructPtr 0x90
#define tokTag 0x91
#define tokMemberIdent 0x92
#define tokEnumPtr 0x93
#define tokIntr 0x94
#define tokNumFloat 0x95
//#define FormatFlat 0
#define FormatSegmented 1
//#define FormatSegTurbo 2
#define FormatSegHuge 3
#define SymVoidSynPtr 0
#define SymIntSynPtr 1
#define SymUintSynPtr 2
#define SymFloatSynPtr 3
#define SymFuncPtr 4
#ifndef STACK_SIZE
#define STACK_SIZE 129
#endif
#define SymFxn 1
#define SymGlobalVar 2
#define SymGlobalArr 3
#define SymLocalVar 4
#define SymLocalArr 5
// all public prototypes
STATIC
unsigned truncUint(unsigned);
STATIC
int truncInt(int);
STATIC
int GetToken(void);
STATIC
const char* GetTokenName(int token);
#ifndef NO_PREPROCESSOR
#ifndef NO_ANNOTATIONS
STATIC
void DumpMacroTable(void);
#endif
#endif
STATIC
int AddIdent(char* name);
STATIC
int FindIdent(char* name);
#ifndef NO_ANNOTATIONS
STATIC
void DumpIdentTable(void);
#endif
STATIC
char* lab2str(char* p, int n);
STATIC
void GenInit(void);
STATIC
void GenFin(void);
STATIC
int GenInitParams(int argc, const char** argv, int* idx);
STATIC
void GenInitFinalize(void);
STATIC
void GenStartCommentLine(void);
STATIC
void GenWordAlignment(int bss);
STATIC
void GenLabel(char* Label, int Static);
STATIC
void GenNumLabel(int Label);
STATIC
void GenZeroData(unsigned Size, int bss);
STATIC
void GenIntData(int Size, int Val);
STATIC
void GenStartAsciiString(void);
STATIC
void GenAddrData(int Size, char* Label, int ofs);
STATIC
void GenJumpUncond(int Label);
STATIC
void GenJumpIfZero(int Label);
STATIC
void GenJumpIfNotZero(int Label);
STATIC
void GenJumpIfEqual(int val, int Label);
STATIC
void GenFxnProlog(void);
STATIC
void GenFxnEpilog(void);
void GenIsrProlog(void);
void GenIsrEpilog(void);
STATIC
int GenMaxLocalsSize(void);
STATIC
void GenDumpChar(int ch);
STATIC
void GenExpr(void);
STATIC
void PushSyntax(int t);
STATIC
void PushSyntax2(int t, int v);
#ifndef NO_ANNOTATIONS
STATIC
void DumpSynDecls(void);
#endif
STATIC
void push2(int v, int v2);
STATIC
void ins2(int pos, int v, int v2);
STATIC
void ins(int pos, int v);
STATIC
void del(int pos, int cnt);
STATIC
int TokenStartsDeclaration(int t, int params);
STATIC
int ParseDecl(int tok, unsigned structInfo[4], int cast, int label);
STATIC
void ShiftChar(void);
STATIC
void error(const char* format, ...);
STATIC
void warning(const char* format, ...);
STATIC
void errorFile(const char* n);
STATIC
void errorFileName(void);
STATIC
void errorInternal(int n);
STATIC
void errorChrStr(void);
STATIC
void errorStrLen(void);
STATIC
void errorUnexpectedToken(int tok);
STATIC
void errorDirective(void);
STATIC
void errorCtrlOutOfScope(void);
STATIC
void errorDecl(void);
STATIC
void errorVarSize(void);
STATIC
void errorInit(void);
STATIC
void errorUnexpectedVoid(void);
STATIC
void errorOpType(void);
STATIC
void errorNotLvalue(void);
STATIC
void errorNotConst(void);
STATIC
void errorLongExpr(void);
#ifndef NO_FP
STATIC
void warnFloat2Int(void);
#endif
STATIC
int FindSymbol(char* s);
STATIC
int SymType(int SynPtr);
STATIC
int FindTaggedDecl(char* s, int start, int* CurScope);
#ifndef NO_TYPEDEF_ENUM
STATIC
int FindTypedef(char* s, int* CurScope, int forUse);
#endif
STATIC
int GetDeclSize(int SyntaxPtr, int SizeForDeref);
STATIC
int ParseExpr(int tok, int* GotUnary, int* ExprTypeSynPtr, int* ConstExpr, int* ConstVal, int option, int option2);
STATIC
int GetFxnInfo(int ExprTypeSynPtr, int* MinParams, int* MaxParams, int* ReturnExprTypeSynPtr, int* FirstParamSynPtr);
#ifndef NO_PREPROCESSOR
#endif
#ifndef MAX_GOTO_LABELS
#define MAX_GOTO_LABELS 16
#endif
#ifndef MAX_CASES
#define MAX_CASES 128
#endif
#define OPERATOR_STACK_SIZE STACK_SIZE
typedef struct{
int _opstack[OPERATOR_STACK_SIZE][2];
int _stack[STACK_SIZE][2];
int _PrepStack[PREP_STACK_SIZE][2];
char _CharQueues[MAX_INCLUDES][3];
int _LineNos[MAX_INCLUDES];
int _LinePoss[MAX_INCLUDES];
char _SysSearchPaths[MAX_SEARCH_PATH];
char _FileNames[MAX_INCLUDES][MAX_FILE_NAME_LEN + 1];
FILE* _Files[MAX_INCLUDES];
int _Cases[MAX_CASES][2]; // [0] is case constant, [1] is case label number
char _gotoLabStat[MAX_GOTO_LABELS];
int _gotoLabels[MAX_GOTO_LABELS][2];
char _IdentTable[MAX_IDENT_TABLE_LEN];
char _MacroTable[MAX_MACRO_TABLE_LEN];
char _CharQueue[MAX_CHAR_QUEUE_LEN];
char _TokenValueString[MAX_STRING_LEN + 1];
char _TokenIdentName[MAX_IDENT_LEN + 1];
char _SearchPaths[MAX_SEARCH_PATH];
unsigned char _SyntaxStack0[SYNTAX_STACK_MAX];
int _SyntaxStack1[SYNTAX_STACK_MAX];
int _verbose;
int _warnings;
int _warnCnt;
int _TokenValueInt;
int _TokenIdentNameLen;
unsigned _TokenStringLen;
int _CharQueueLen;
int _MacroTableLen;
int _IdentTableLen;
int _gotoLabCnt;
int _CasesCnt;
int _FileCnt;
FILE* _OutFile;
int _SysSearchPathsLen;
int _SearchPathsLen;
int _PrepSp;
int _ExprLevel;
int _sp;
int _opsp;
int _StructCpyLabel; // label of the function to copy structures/unions
int _StructPushLabel; // label of the function to push structures/unions onto the stack
int _CurFxnSyntaxPtr;
int _CurFxnParamCntMin;
int _CurFxnParamCntMax;
int _CurFxnLocalOfs; // negative
int _CurFxnMinLocalOfs; // negative
int _CurFxnReturnExprTypeSynPtr;
int _CurFxnEpilogLabel;
char* _CurFxnName;
int _CurFxnNameLabel;
int _Main; // if inside main()
int _ParseLevel; // Parse level/scope (file:0, fxn:1+)
int _ParamLevel; // 1+ if parsing params, 0 otherwise
int _SyntaxStackCnt;
int _expr3Sp;
int _expr3Stack[STACK_SIZE / 2][2];
} global_hugevar;
global_hugevar *br;
#define opstack (br->_opstack)
#define stack (br->_stack)
#define PrepStack (br->_PrepStack)
#define CharQueues (br->_CharQueues)
#define LineNos (br->_LineNos)
#define LinePoss (br->_LinePoss)
#define SysSearchPaths (br->_SysSearchPaths)
#define FileNames (br->_FileNames)
#define Files (br->_Files)
#define Cases (br->_Cases)
#define gotoLabStat (br->_gotoLabStat)
#define gotoLabels (br->_gotoLabels)
#define IdentTable (br->_IdentTable)
#define MacroTable (br->_MacroTable)
#define CharQueue (br->_CharQueue)
#define TokenValueString (br->_TokenValueString)
#define TokenIdentName (br->_TokenIdentName)
#define SearchPaths (br->_SearchPaths)
#define SyntaxStack0 (br->_SyntaxStack0)
#define SyntaxStack1 (br->_SyntaxStack1)
#define verbose (br->_verbose)
#define warnings (br->_warnings)
#define warnCnt (br->_warnCnt)
#define TokenValueInt (br->_TokenValueInt)
#define TokenIdentNameLen (br->_TokenIdentNameLen)
#define TokenStringLen (br->_TokenStringLen)
#define CharQueueLen (br->_CharQueueLen)
#define MacroTableLen (br->_MacroTableLen)
#define IdentTableLen (br->_IdentTableLen)
#define gotoLabCnt (br->_gotoLabCnt)
#define CasesCnt (br->_CasesCnt)
#define FileCnt (br->_FileCnt)
#define OutFile (br->_OutFile)
#define SysSearchPathsLen (br->_SysSearchPathsLen)
#define SearchPathsLen (br->_SearchPathsLen)
#define PrepSp (br->_PrepSp)
#define ExprLevel (br->_ExprLevel)
#define sp (br->_sp)
#define opsp (br->_opsp)
#define StructCpyLabel (br->_StructCpyLabel)
#define StructPushLabel (br->_StructPushLabel)
#define CurFxnSyntaxPtr (br->_CurFxnSyntaxPtr)
#define CurFxnParamCntMin (br->_CurFxnParamCntMin)
#define CurFxnParamCntMax (br->_CurFxnParamCntMax)
#define CurFxnLocalOfs (br->_CurFxnLocalOfs)
#define CurFxnMinLocalOfs (br->_CurFxnMinLocalOfs)
#define CurFxnReturnExprTypeSynPtr (br->_CurFxnReturnExprTypeSynPtr)
#define CurFxnEpilogLabel (br->_CurFxnEpilogLabel)
#define CurFxnName (br->_CurFxnName)
#define CurFxnNameLabel (br->_CurFxnNameLabel)
#define Main (br->_Main)
#define ParseLevel (br->_ParseLevel)
#define ParamLevel (br->_ParamLevel)
#define SyntaxStackCnt (br->_SyntaxStackCnt)
#define expr3Sp (br->_expr3Sp)
#define expr3Stack (br->_expr3Stack)
int LineNo = 1;
int LinePos = 1;
int GenExterns = 1;
int UseBss = 1;
int UseLeadingUnderscores = 1;
char *FileHeader = "";
int CharIsSigned = 1;
int SizeOfWord = 2;
int LabelCnt = 1; // label counter for jumps
int PrepDontSkipTokens = 1;
int OutputFormat;
char* CodeHeaderFooter[2] = { "", "" };
char* DataHeaderFooter[2] = { "", "" };
char* RoDataHeaderFooter[2] = { "", "" };
char* BssHeaderFooter[2] = { "", "" };
char** CurHeaderFooter;
// all code
STATIC
unsigned truncUint(unsigned n)
{
// Truncate n to SizeOfWord * 8 bits
if (SizeOfWord == 2)
n &= ~(~0u << 8 << 8);
#ifdef CAN_COMPILE_32BIT
else if (SizeOfWord == 4)
n &= ~(~0u << 8 << 12 << 12);
#endif
return n;
}
STATIC
int truncInt(int n)
{
// Truncate n to SizeOfWord * 8 bits and then sign-extend it
unsigned un = n;
if (SizeOfWord == 2)
{
un &= ~(~0u << 8 << 8);
un |= (((un >> 8 >> 7) & 1) * ~0u) << 8 << 8;
}
#ifdef CAN_COMPILE_32BIT
else if (SizeOfWord == 4)
{
un &= ~(~0u << 8 << 12 << 12);
un |= (((un >> 8 >> 12 >> 11) & 1) * ~0u) << 8 << 12 << 12;
}
#endif
return (int)un;
}
// prep.c code
#ifndef NO_PREPROCESSOR
STATIC
int FindMacro(char* name)
{
int i;
for (i = 0; i < MacroTableLen; )
{
if (!strcmp(MacroTable + i + 1, name))
return i + 1 + MacroTable[i];
i = i + 1 + MacroTable[i]; // skip id
i = i + 1 + MacroTable[i]; // skip ex
}
return -1;
}
STATIC
int UndefineMacro(char* name)
{
int i;
for (i = 0; i < MacroTableLen; )
{
if (!strcmp(MacroTable + i + 1, name))
{
int len = 1 + MacroTable[i]; // id part len
len = len + 1 + MacroTable[i + len]; // + ex part len
memmove(MacroTable + i,
MacroTable + i + len,
MacroTableLen - i - len);
MacroTableLen -= len;
return 1;
}
i = i + 1 + MacroTable[i]; // skip id
i = i + 1 + MacroTable[i]; // skip ex
}
return 0;
}
STATIC
void AddMacroIdent(char* name)
{
int l = strlen(name);
if (l >= 127)
error("Macro identifier too long '%s'\n", name);
if (MAX_MACRO_TABLE_LEN - MacroTableLen < l + 3)
error("Macro table exhausted\n");
MacroTable[MacroTableLen++] = l + 1; // idlen
strcpy(MacroTable + MacroTableLen, name);
MacroTableLen += l + 1;
MacroTable[MacroTableLen] = 0; // exlen
}
STATIC
void AddMacroExpansionChar(char e)
{
if (e == '\0')
{
// finalize macro definition entry
// remove trailing space first
while (MacroTable[MacroTableLen] &&
strchr(" \t", MacroTable[MacroTableLen + MacroTable[MacroTableLen]]))
MacroTable[MacroTableLen]--;
MacroTableLen += 1 + MacroTable[MacroTableLen];
return;
}
if (MacroTableLen + 1 + MacroTable[MacroTableLen] >= MAX_MACRO_TABLE_LEN)
error("Macro table exhausted\n");
if (MacroTable[MacroTableLen] >= 127)
error("Macro definition too long\n");
MacroTable[MacroTableLen + 1 + MacroTable[MacroTableLen]] = e;
MacroTable[MacroTableLen]++;
}
STATIC
void DefineMacro(char* name, char* expansion)
{
AddMacroIdent(name);
do
{
AddMacroExpansionChar(*expansion);
} while (*expansion++ != '\0');
}
#ifndef NO_ANNOTATIONS
STATIC
void DumpMacroTable(void)
{
int i, j;
puts2("");
GenStartCommentLine(); printf2("Macro table:\n");
for (i = 0; i < MacroTableLen; )
{
GenStartCommentLine(); printf2("Macro %s = ", MacroTable + i + 1);
i = i + 1 + MacroTable[i]; // skip id
printf2("`");
j = MacroTable[i++];
while (j--)
printf2("%c", MacroTable[i++]);
printf2("`\n");
}
GenStartCommentLine(); printf2("Bytes used: %d/%d\n\n", MacroTableLen, MAX_MACRO_TABLE_LEN);
}
#endif
#endif // #ifndef NO_PREPROCESSOR
STATIC
int FindIdent(char* name)
{
int i;
for (i = IdentTableLen; i > 0; )
{
i -= 1 + IdentTable[i - 1];
if (!strcmp(IdentTable + i, name))
return i;
}
return -1;
}
STATIC
int AddIdent(char* name)
{
int i, len;
if ((i = FindIdent(name)) >= 0)
return i;
i = IdentTableLen;
len = strlen(name);
if (len >= 127)
error("Identifier too long\n");
if (MAX_IDENT_TABLE_LEN - IdentTableLen < len + 2)
error("Identifier table exhausted\n");
strcpy(IdentTable + IdentTableLen, name);
IdentTableLen += len + 1;
IdentTable[IdentTableLen++] = len + 1;
return i;
}
STATIC
int AddNumericIdent(int n)
{
char s[1 + (2 + CHAR_BIT * sizeof n) / 3];
char *p = s + sizeof s;
*--p = '\0';
p = lab2str(p, n);
return AddIdent(p);
}
STATIC
int AddGotoLabel(char* name, int label)
{
int i;
for (i = 0; i < gotoLabCnt; i++)
{
if (!strcmp(IdentTable + gotoLabels[i][0], name))
{
if (gotoLabStat[i] & label)
error("Redefinition of label '%s'\n", name);
gotoLabStat[i] |= 2*!label + label;
return gotoLabels[i][1];
}
}
if (gotoLabCnt >= MAX_GOTO_LABELS)
error("Goto table exhausted\n");
gotoLabels[gotoLabCnt][0] = AddIdent(name);
gotoLabels[gotoLabCnt][1] = LabelCnt++;
gotoLabStat[gotoLabCnt] = 2*!label + label;
return gotoLabels[gotoLabCnt++][1];
}
STATIC
void UndoNonLabelIdents(int len)
{
int i;
IdentTableLen = len;
for (i = 0; i < gotoLabCnt; i++)
if (gotoLabels[i][0] >= len)
{
char* pfrom = IdentTable + gotoLabels[i][0];
char* pto = IdentTable + IdentTableLen;
int l = strlen(pfrom) + 2;
memmove(pto, pfrom, l);
IdentTableLen += l;
gotoLabels[i][0] = pto - IdentTable;
}
}
STATIC
void AddCase(int val, int label)
{
if (CasesCnt >= MAX_CASES)
error("Case table exhausted\n");
Cases[CasesCnt][0] = val;
Cases[CasesCnt++][1] = label;
}
#ifndef NO_ANNOTATIONS
STATIC
void DumpIdentTable(void)
{
int i;
puts2("");
GenStartCommentLine(); printf2("Identifier table:\n");
for (i = 0; i < IdentTableLen; )
{
GenStartCommentLine(); printf2("Ident %s\n", IdentTable + i);
i += strlen(IdentTable + i) + 2;
}
GenStartCommentLine(); printf2("Bytes used: %d/%d\n\n", IdentTableLen, MAX_IDENT_TABLE_LEN);
}
#endif
char* rws[] =
{
"break", "case", "char", "continue", "default", "do", "else",
"extern", "for", "if", "int", "return", "signed", "sizeof",
"static", "switch", "unsigned", "void", "while", "asm", "auto",
"const", "double", "enum", "float", "goto", "inline", "long",
"register", "restrict", "short", "struct", "typedef", "union",
"volatile", "_Bool", "_Complex", "_Imaginary",
"__interrupt"
};
unsigned char rwtk[] =
{
tokBreak, tokCase, tokChar, tokCont, tokDefault, tokDo, tokElse,
tokExtern, tokFor, tokIf, tokInt, tokReturn, tokSigned, tokSizeof,
tokStatic, tokSwitch, tokUnsigned, tokVoid, tokWhile, tok_Asm, tokAuto,
tokConst, tokDouble, tokEnum, tokFloat, tokGoto, tokInline, tokLong,
tokRegister, tokRestrict, tokShort, tokStruct, tokTypedef, tokUnion,
tokVolatile, tok_Bool, tok_Complex, tok_Imagin,
tokIntr
};
STATIC
int GetTokenByWord(char* word)
{
unsigned i;
for (i = 0; i < sizeof rws / sizeof rws[0]; i++)
if (!strcmp(rws[i], word))
return rwtk[i];
return tokIdent;
}
const
unsigned char tktk[] =
{
tokEof,
// Single-character operators and punctuators:
'+', '-', '~', '*', '/', '%', '&', '|', '^', '!',
'<', '>', '(', ')', '[', ']',
'{', '}', '=', ',', ';', ':', '.', '?',
// Multi-character operators and punctuators:
tokLShift, tokLogAnd, tokEQ, tokLEQ, tokInc, tokArrow, tokAssignMul,
tokAssignMod, tokAssignSub, tokAssignRSh, tokAssignXor,
tokRShift, tokLogOr, tokNEQ, tokGEQ, tokDec, tokEllipsis,
tokAssignDiv, tokAssignAdd, tokAssignLSh, tokAssignAnd, tokAssignOr,
// Some of the above tokens get converted into these in the process:
tokUnaryAnd, tokUnaryPlus, tokPostInc, tokPostAdd,
tokULess, tokULEQ, tokURShift, tokUDiv, tokUMod, tokComma,
tokUnaryStar, tokUnaryMinus, tokPostDec, tokPostSub,
tokUGreater, tokUGEQ, tokAssignURSh, tokAssignUDiv, tokAssignUMod,
// Helper (pseudo-)tokens:
tokNumInt, tokLitStr, tokLocalOfs, tokNumUint, tokIdent, tokShortCirc,
tokSChar, tokShort, tokLong, tokUChar, tokUShort, tokULong, tokNumFloat,
};
const
char* tks[] =
{
"<EOF>",
// Single-character operators and punctuators:
"+", "-", "~", "*", "/", "%", "&", "|", "^", "!",
"<", ">", "(", ")", "[", "]",
"{", "}", "=", ",", ";", ":", ".", "?",
// Multi-character operators and punctuators: