-
Notifications
You must be signed in to change notification settings - Fork 130
/
fb.bas
1807 lines (1520 loc) · 45.9 KB
/
fb.bas
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
'' fb - main interface
''
'' chng: sep/2004 written [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "parser.bi"
#include once "lex.bi"
#include once "rtl.bi"
#include once "ast.bi"
#include once "ir.bi"
#include once "objinfo.bi"
type FB_LANG_INFO
name as const zstring ptr
options as FB_LANG_OPT
end type
declare sub parserInit ( )
declare sub parserEnd ( )
declare sub parserSetCtx ( )
'' globals
dim shared env as FBENV
dim shared infileTb( ) as FBFILE
dim shared as FB_LANG_INFO langTb(0 to FB_LANGS-1) = _
{ _
( _
@"fb", _
FB_LANG_OPT_MT or _
FB_LANG_OPT_SCOPE or _
FB_LANG_OPT_NAMESPC or _
FB_LANG_OPT_EXTERN or _
FB_LANG_OPT_FUNCOVL or _
FB_LANG_OPT_OPEROVL or _
FB_LANG_OPT_CLASS or _
FB_LANG_OPT_INITIALIZER or _
FB_LANG_OPT_AUTOVAR or _
FB_LANG_OPT_SINGERRLINE or _
FB_LANG_OPT_QUIRKFUNC _
) _
, _
( _
@"deprecated", _
FB_LANG_OPT_MT or _
FB_LANG_OPT_SCOPE or _
FB_LANG_OPT_NAMESPC or _
FB_LANG_OPT_EXTERN or _
FB_LANG_OPT_FUNCOVL or _
FB_LANG_OPT_INITIALIZER or _
FB_LANG_OPT_CALL or _
FB_LANG_OPT_LET or _
FB_LANG_OPT_PERIODS or _
FB_LANG_OPT_NUMLABEL or _
FB_LANG_OPT_IMPLICIT or _
FB_LANG_OPT_DEFTYPE or _
FB_LANG_OPT_SUFFIX or _
FB_LANG_OPT_METACMD or _
FB_LANG_OPT_OPTION or _
FB_LANG_OPT_ONERROR or _
FB_LANG_OPT_QUIRKFUNC _
) _
, _
( _
@"fblite", _
FB_LANG_OPT_MT or _
FB_LANG_OPT_NAMESPC or _
FB_LANG_OPT_EXTERN or _
FB_LANG_OPT_FUNCOVL or _
FB_LANG_OPT_INITIALIZER or _
FB_LANG_OPT_GOSUB or _
FB_LANG_OPT_CALL or _
FB_LANG_OPT_LET or _
FB_LANG_OPT_PERIODS or _
FB_LANG_OPT_NUMLABEL or _
FB_LANG_OPT_IMPLICIT or _
FB_LANG_OPT_DEFTYPE or _
FB_LANG_OPT_SUFFIX or _
FB_LANG_OPT_METACMD or _
FB_LANG_OPT_OPTION or _
FB_LANG_OPT_ONERROR or _
FB_LANG_OPT_QUIRKFUNC _
) _
, _
( _
@"qb", _
FB_LANG_OPT_GOSUB or _
FB_LANG_OPT_CALL or _
FB_LANG_OPT_LET or _
FB_LANG_OPT_PERIODS or _
FB_LANG_OPT_NUMLABEL or _
FB_LANG_OPT_IMPLICIT or _
FB_LANG_OPT_DEFTYPE or _
FB_LANG_OPT_SUFFIX or _
FB_LANG_OPT_METACMD or _
FB_LANG_OPT_OPTION or _
FB_LANG_OPT_ONERROR or _
FB_LANG_OPT_QUIRKFUNC _
) _
}
dim shared as FBTARGET targetinfo(0 to FB_COMPTARGETS-1) = _
{ _
( _
@"win32", _ '' id
FB_DATATYPE_USHORT, _ '' wchar
FB_FUNCMODE_STDCALL, _ '' fbcall
FB_FUNCMODE_STDCALL, _ '' stdcall
0 or FB_TARGETOPT_EXPORT _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_COFF _
), _
( _
@"cygwin", _
FB_DATATYPE_USHORT, _
FB_FUNCMODE_STDCALL, _
FB_FUNCMODE_STDCALL, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_EXPORT _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_COFF _
), _
( _
@"linux", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_STACKALIGN16 _
or FB_TARGETOPT_ELF _
), _
( _
@"android", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_STACKALIGN16 _
or FB_TARGETOPT_ELF _
), _
( _
@"dos", _
FB_DATATYPE_UBYTE, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_COFF _
), _
( _
@"xbox", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_STDCALL, _
FB_FUNCMODE_STDCALL, _
0 or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_COFF _
), _
( _
@"freebsd", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_RETURNINFLTS _
or FB_TARGETOPT_ELF _
), _
( _
@"dragonfly", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_ELF _
), _
( _
@"solaris", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_ELF _
), _
( _
@"openbsd", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_RETURNINFLTS _
or FB_TARGETOPT_ELF _
), _
( _
@"darwin", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_STACKALIGN16 _
or FB_TARGETOPT_MACHO _
), _
( _
@"netbsd", _
FB_DATATYPE_ULONG, _
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
or FB_TARGETOPT_RETURNINFLTS _
or FB_TARGETOPT_ELF _
), _
( _
@"js", _
FB_DATATYPE_ULONG, _ '' wchar
FB_FUNCMODE_CDECL, _
FB_FUNCMODE_STDCALL_MS, _
0 or FB_TARGETOPT_UNIX _
or FB_TARGETOPT_CALLEEPOPSHIDDENPTR _
or FB_TARGETOPT_RETURNINREGS _
) _
}
type FBCPUFAMILYINFO
id as zstring ptr
defaultcputype as integer
end type
dim shared as FBCPUFAMILYINFO cpufamilyinfo(0 to FB_CPUFAMILY__COUNT-1) = _
{ _
(@"x86" , FB_DEFAULT_CPUTYPE_X86 ), _
(@"x86_64" , FB_DEFAULT_CPUTYPE_X86_64 ), _
(@"arm" , FB_DEFAULT_CPUTYPE_ARM ), _
(@"aarch64" , FB_DEFAULT_CPUTYPE_AARCH64), _
(@"powerpc" , FB_DEFAULT_CPUTYPE_PPC ), _
(@"powerpc64" , FB_DEFAULT_CPUTYPE_PPC64 ), _
(@"powerpc64le", FB_DEFAULT_CPUTYPE_PPC64LE), _
(@"asmjs" , FB_DEFAULT_CPUTYPE_ASMJS ) _
}
type FBCPUTYPEINFO
gccarch as zstring ptr '' gcc -march argument (used for -gen gcc), or NULL if same as fbcarch
fbcarch as zstring ptr '' fbc -arch argument
family as integer '' enum FB_CPUFAMILY
bits as integer '' 32 or 64
bigendian as integer '' endianness - TRUE=big endian, FALSE=little endian
end type
dim shared as FBCPUTYPEINFO cputypeinfo(0 to FB_CPUTYPE__COUNT-1) = _
{ _
( @"i386" , @"386" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_386
( @"i486" , @"486" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_486
( @"i586" , @"586" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_586
( @"i686" , @"686" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_686
( NULL , @"athlon" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_ATHLON
( NULL , @"athlon-xp" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_ATHLONXP
( NULL , @"athlon-fx" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_ATHLONFX
( NULL , @"k8-sse3" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_ATHLONSSE3
( NULL , @"pentium-mmx" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_PENTIUMMMX
( NULL , @"pentium2" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_PENTIUM2
( NULL , @"pentium3" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_PENTIUM3
( NULL , @"pentium4" , FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_PENTIUM4
( @"prescott", @"pentium4-sse3", FB_CPUFAMILY_X86 , 32, FALSE ), _ '' FB_CPUTYPE_PENTIUMSSE3
( NULL , @"x86-64" , FB_CPUFAMILY_X86_64 , 64, FALSE ), _ '' FB_CPUTYPE_X86_64
( NULL , @"armv5te" , FB_CPUFAMILY_ARM , 32, FALSE ), _ '' FB_CPUTYPE_ARMV5TE
( NULL , @"armv6" , FB_CPUFAMILY_ARM , 32, FALSE ), _ '' FB_CPUTYPE_ARMV6
( NULL , @"armv6+fp" , FB_CPUFAMILY_ARM , 32, FALSE ), _ '' FB_CPUTYPE_ARMV6_FP
( NULL , @"armv7-a" , FB_CPUFAMILY_ARM , 32, FALSE ), _ '' FB_CPUTYPE_ARMV7A
( NULL , @"armv7-a+fp" , FB_CPUFAMILY_ARM , 32, FALSE ), _ '' FB_CPUTYPE_ARMV7A_FP
( @"armv8-a" , @"aarch64" , FB_CPUFAMILY_AARCH64, 64, FALSE ), _ '' FB_CPUTYPE_AARCH64
( NULL , @"powerpc" , FB_CPUFAMILY_PPC , 32, TRUE ), _ '' FB_CPUTYPE_PPC
( NULL , @"powerpc64" , FB_CPUFAMILY_PPC64 , 64, TRUE ), _ '' FB_CPUTYPE_PPC64
( NULL , @"powerpc64le" , FB_CPUFAMILY_PPC64LE, 64, FALSE ), _ '' FB_CPUTYPE_PPC64LE
( NULL , @"asmjs" , FB_CPUFAMILY_ASMJS , 32, FALSE ) _ '' FB_CPUTYPE_ASMJS
}
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' interface
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'':::::
private function hFindIncFile _
( _
byval incfilehash as THASH ptr, _
byval filename as zstring ptr _
) as zstring ptr static
dim as string fname
#if defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
fname = ucase( *filename )
#else
fname = *filename
#endif
function = hashLookup( incfilehash, fname )
end function
'' Add a string to filenamehash if missing, and add a copy of the filenamehash
'' entry to incfilehash if missing, unless incfilehash is NULL.
'' Invariant: incfilehash is a subset of filenamehash.
private function hAddIncFile _
( _
byval incfilehash as THASH ptr, _
byval filenamehash as THASH ptr, _
byval filename as zstring ptr _
) as zstring ptr static
dim as zstring ptr fname, res
dim as uinteger index
fname = xallocate( len( *filename ) + 1 )
#if defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
hUcase( filename, fname )
#else
*fname = *filename
#endif
index = hashHash( fname )
res = hashLookupEx( filenamehash, fname, index )
if( res = NULL ) then
hashAdd( filenamehash, fname, fname, index )
else
deallocate( fname )
fname = res
end if
if( incfilehash ) then
res = hashLookupEx( incfilehash, fname, index )
if( res = NULL ) then
hashAdd( incfilehash, fname, fname, index )
end if
end if
function = fname
end function
'':::::
function fbGetLangOptions _
( _
byval lang as FB_LANG _
) as FB_LANG_OPT
function = langTb(lang).options
end function
'':::::
function fbGetLangName _
( _
byval lang as FB_LANG _
) as string
function = *langTb(lang).name
end function
'':::::
function fbGetBackendName _
( _
byval lang as FB_BACKEND _
) as string
select case env.clopt.backend
case FB_BACKEND_GAS
function = "gas"
case FB_BACKEND_GCC
function = "gcc"
case FB_BACKEND_LLVM
function = "llvm"
case FB_BACKEND_GAS64
function = "gas64"
end select
end function
sub fbInit _
( _
byval ismain as integer, _
byval entry as zstring ptr, _
byval module_count as integer _
)
strsetInit( @env.libs, FB_INITLIBNODES \ 4 )
strsetInit( @env.libpaths, FB_INITLIBNODES \ 4 )
'' when starting a compile, reset the restart requests
'' env.restart_status preserves state from previous runs
'' and would have been initialized to 0 (FB_RESTART_NONE)
'' but we need to clear the PARSER_LANG status because
'' #lang directives should only apply to current module
'' env.restart_count is preserved between runs (passes) so don't
'' re-initialize it here.
env.restart_request = FB_RESTART_NONE
env.restart_action = FB_RESTART_NONE
env.restart_status and= not (FB_RESTART_PARSER_LANG or FB_RESTART_PARSER_MT)
redim infileTb( 0 to FB_MAXINCRECLEVEL-1 )
env.includerec = 0
env.main.proc = NULL
env.entry = *entry
env.module_count = module_count
env.opt.explicit = (env.clopt.lang = FB_LANG_FB)
'' data type remapping
if( env.clopt.lang <> FB_LANG_QB ) then
'' In FB, the INTEGER keyword produces FB_DATATYPE_INTEGER
env.lang.integerkeyworddtype = FB_DATATYPE_INTEGER
'' In FB, both 16bit/32bit number literals are made INTEGERs,
'' and floats are DOUBLE by default.
env.lang.int15literaldtype = FB_DATATYPE_INTEGER
env.lang.int16literaldtype = FB_DATATYPE_UINT
env.lang.int31literaldtype = FB_DATATYPE_INTEGER
env.lang.int32literaldtype = FB_DATATYPE_UINT
if( fbIs64bit( ) ) then
env.lang.int63literaldtype = FB_DATATYPE_INTEGER
env.lang.int64literaldtype = FB_DATATYPE_UINT
else
env.lang.int63literaldtype = FB_DATATYPE_LONGINT
env.lang.int64literaldtype = FB_DATATYPE_ULONGINT
end if
env.lang.floatliteraldtype = FB_DATATYPE_DOUBLE
else
'' In QB, the INTEGER keyword produces FB_DATATYPE_SHORT
'' (Note: FB_DATATYPE_INTEGER remains unchanged, it's just that
'' FB_DATATYPE_SHORT is being used instead in some places)
env.lang.integerkeyworddtype = FB_DATATYPE_SHORT
'' In QB, 16bit number literals are made SHORTs (i.e. QB's
'' 16bit INTEGERs), and 32bit number literals are 32bit LONGs,
'' not FB 32bit/64bit INTEGERs. Floats are SINGLEs by default.
env.lang.int15literaldtype = FB_DATATYPE_SHORT
env.lang.int16literaldtype = FB_DATATYPE_USHORT
env.lang.int31literaldtype = FB_DATATYPE_LONG
env.lang.int32literaldtype = FB_DATATYPE_ULONG
env.lang.int63literaldtype = FB_DATATYPE_LONGINT
env.lang.int64literaldtype = FB_DATATYPE_ULONGINT
env.lang.floatliteraldtype = FB_DATATYPE_SINGLE
end if
env.opt.parammode = FB_PARAMMODE_BYREF
env.opt.procpublic = TRUE
env.opt.escapestr = FALSE
env.opt.dynamic = FALSE
env.opt.base = 0
env.opt.gosub = (env.clopt.lang = FB_LANG_QB)
env.fbctinf_started = FALSE
'' Leading underscore needed on ASM symbols?
'' Yes for dos/cygwin-x86/win32/xbox, but not win64/cygwin-x86_64/linux-*/etc.
env.underscoreprefix = FALSE
select case( env.clopt.target )
case FB_COMPTARGET_DOS, FB_COMPTARGET_XBOX, FB_COMPTARGET_DARWIN
env.underscoreprefix = TRUE
case FB_COMPTARGET_CYGWIN, FB_COMPTARGET_WIN32
env.underscoreprefix = not fbIs64bit( )
end select
'' set by symbDataInit()
env.pointersize = 0
parserSetCtx( )
symbInit( ismain )
errInit( )
astInit( )
irInit( )
'' After symbInit(), we can use typeGetSize()
if( sizeof( wstring ) = typeGetSize( env.target.wchar ) ) then
env.wcharconv = FB_WCHARCONV_ALWAYS
else
env.wcharconv = FB_WCHARCONV_NEVER
end if
#if ( __FB_DEBUG__ <> 0 ) andalso defined( __FB_WIN32__ )
select case( env.clopt.target )
case FB_COMPTARGET_JS
'' !!!TODO!!! - FB_WCHARCONV_WARNING needs to show warnings on conversions
'' !!!TODO!!! - FB_WCHARCONV_WARNING probably not correct to force a value
'' but setting it helps with development and testing
'' where sizeof(host-wstring) <> sizeof(target-wstring)
env.wcharconv = FB_WCHARCONV_WARNING
end select
#endif
hashInit( @env.filenamehash, FB_INITINCFILES )
hashInit( @env.incfilehash, FB_INITINCFILES, FALSE )
hashInit( @env.inconcehash, FB_INITINCFILES, FALSE )
stackNew( @parser.stmt.stk, FB_INITSTMTSTACKNODES, len( FB_CMPSTMTSTK ), FALSE )
lexInit( LEX_TKCTX_CONTEXT_INIT )
parserInit( )
rtlInit( )
'' env.inited indicates that lexer/parser/compiler/rtl is initialized
'' intent is to help debug internal functions where we general want to
'' ignore function calls (breakpoints) while fbc itself is starting up
env.inited = TRUE
end sub
sub fbEnd()
env.inited = FALSE
rtlEnd( )
parserEnd( )
lexEnd( )
stackFree( @parser.stmt.stk )
hashEnd( @env.filenamehash )
hashEnd( @env.inconcehash )
hashEnd( @env.incfilehash )
irEnd( )
astEnd( )
errEnd( )
symbEnd( )
erase infileTb
strsetEnd(@env.libs)
strsetEnd(@env.libpaths)
end sub
private sub hUpdateLangOptions( )
env.lang.opt = langTb(env.clopt.lang).options
end sub
private sub hUpdateTargetOptions( )
env.target = targetinfo(env.clopt.target)
if( env.clopt.returninflts ) then
env.target.options or= FB_TARGETOPT_RETURNINFLTS
end if
end sub
sub fbGlobalInit()
strlistInit(@env.predefines, FB_INITINCFILES)
strlistInit(@env.preincludes, FB_INITINCFILES)
strlistInit(@env.includepaths, FB_INITINCFILES)
'' default settings
env.clopt.outtype = FB_DEFAULT_OUTTYPE
env.clopt.pponly = FALSE
env.clopt.backend = FB_DEFAULT_BACKEND
env.clopt.target = FB_DEFAULT_TARGET
env.clopt.cputype = FB_DEFAULT_CPUTYPE
env.clopt.fputype = FB_DEFAULT_FPUTYPE
env.clopt.fpmode = FB_DEFAULT_FPMODE
env.clopt.vectorize = FB_DEFAULT_VECTORIZELEVEL
env.clopt.optlevel = 0
env.clopt.asmsyntax = FB_ASMSYNTAX_INTEL
env.clopt.lang = FB_DEFAULT_LANG
env.clopt.forcelang = FALSE
env.clopt.debug = FALSE
env.clopt.debuginfo = FALSE
env.clopt.assertions = FALSE
env.clopt.errorcheck = FALSE
env.clopt.extraerrchk = FALSE
env.clopt.errlocation = FALSE
env.clopt.arrayboundchk = FALSE
env.clopt.nullptrchk = FALSE
env.clopt.unwindinfo = FALSE
env.clopt.resumeerr = FALSE
env.clopt.profile = FALSE
env.clopt.warninglevel = FB_WARNINGMSGS_DEFAULT_LEVEL
env.clopt.showerror = TRUE
env.clopt.maxerrors = FB_DEFAULT_MAXERRORS
env.clopt.pdcheckopt = FB_PDCHECK_NONE
env.clopt.gosubsetjmp = FALSE
env.clopt.export = FALSE
env.clopt.multithreaded = FALSE
env.clopt.fbgfx = FALSE
env.clopt.pic = FALSE
env.clopt.msbitfields = FALSE
env.clopt.stacksize = 0 '' default will be set by fbSetOption() called from hParseArgs()
env.clopt.objinfo = TRUE
env.clopt.showincludes = FALSE
env.clopt.modeview = FB_DEFAULT_MODEVIEW
env.clopt.nocmdline = FALSE
env.clopt.returninflts = FALSE
env.restart_request = FB_RESTART_NONE
env.restart_action = FB_RESTART_NONE
env.restart_status = FB_RESTART_NONE
env.restart_lang = FB_LANG_INVALID
env.restart_count = 0
env.module_count = 0
hUpdateLangOptions( )
hUpdateTargetOptions( )
end sub
sub fbAddIncludePath(byref path as string)
strlistAppend(@env.includepaths, path)
end sub
sub fbAddPreDefine(byref def as string)
strlistAppend(@env.predefines, def)
end sub
sub fbAddPreInclude(byref file as string)
strlistAppend(@env.preincludes, file)
end sub
sub fbSetOption( byval opt as integer, byval value as integer )
select case as const( opt )
case FB_COMPOPT_OUTTYPE
env.clopt.outtype = value
case FB_COMPOPT_PPONLY
env.clopt.pponly = value
case FB_COMPOPT_BACKEND
env.clopt.backend = value
case FB_COMPOPT_TARGET
env.clopt.target = value
hUpdateTargetOptions( )
case FB_COMPOPT_CPUTYPE
env.clopt.cputype = value
case FB_COMPOPT_FPUTYPE
env.clopt.fputype = value
case FB_COMPOPT_FPMODE
env.clopt.fpmode = value
case FB_COMPOPT_VECTORIZE
env.clopt.vectorize = value
case FB_COMPOPT_OPTIMIZELEVEL
env.clopt.optlevel = value
case FB_COMPOPT_ASMSYNTAX
env.clopt.asmsyntax = value
case FB_COMPOPT_LANG
env.clopt.lang = value
hUpdateLangOptions( )
case FB_COMPOPT_FORCELANG
env.clopt.forcelang = value
case FB_COMPOPT_RESTART_LANG
env.restart_lang = value
case FB_COMPOPT_DEBUG
env.clopt.debug = value
case FB_COMPOPT_DEBUGINFO
env.clopt.debuginfo = value
case FB_COMPOPT_ASSERTIONS
env.clopt.assertions = value
case FB_COMPOPT_ERRORCHECK
env.clopt.errorcheck = value
case FB_COMPOPT_RESUMEERROR
env.clopt.resumeerr = value
case FB_COMPOPT_EXTRAERRCHECK
env.clopt.extraerrchk = value
case FB_COMPOPT_ERRLOCATION
env.clopt.errlocation = value
case FB_COMPOPT_ARRAYBOUNDCHECK
env.clopt.arrayboundchk = value
case FB_COMPOPT_NULLPTRCHECK
env.clopt.nullptrchk = value
case FB_COMPOPT_UNWINDINFO
env.clopt.unwindinfo = value
case FB_COMPOPT_PROFILE
env.clopt.profile = value
case FB_COMPOPT_WARNINGLEVEL
env.clopt.warninglevel = value
case FB_COMPOPT_SHOWERROR
env.clopt.showerror = value
case FB_COMPOPT_MAXERRORS
env.clopt.maxerrors = value
case FB_COMPOPT_PEDANTICCHK
env.clopt.pdcheckopt = value
case FB_COMPOPT_GOSUBSETJMP
env.clopt.gosubsetjmp = value
case FB_COMPOPT_VALISTASPTR
env.clopt.valistasptr = value
case FB_COMPOPT_NOTHISCALL
env.clopt.nothiscall = value
case FB_COMPOPT_NOFASTCALL
env.clopt.nofastcall = value
case FB_COMPOPT_FBRT
env.clopt.fbrt = value
case FB_COMPOPT_EXPORT
env.clopt.export = value
case FB_COMPOPT_MSBITFIELDS
env.clopt.msbitfields = value
case FB_COMPOPT_MULTITHREADED
env.clopt.multithreaded = value
case FB_COMPOPT_FBGFX
env.clopt.fbgfx = value
case FB_COMPOPT_PIC
env.clopt.pic = value
case FB_COMPOPT_STACKSIZE
'' check stacksize and value was never set yet?
if( value < 0 ) then
if( env.clopt.stacksize = 0 ) then
env.clopt.stacksize = iif( fbIs64bit(), FB_DEFSTACKSIZE64, FB_DEFSTACKSIZE32 )
end if
else
'' ensure current stacksize is at least the minimum size
if( fbIs64bit() ) then
env.clopt.stacksize = iif( value < FB_MINSTACKSIZE64, FB_MINSTACKSIZE64, value )
else
env.clopt.stacksize = iif( value < FB_MINSTACKSIZE32, FB_MINSTACKSIZE32, value )
end if
end if
case FB_COMPOPT_OBJINFO
env.clopt.objinfo = value
case FB_COMPOPT_SHOWINCLUDES
env.clopt.showincludes = value
case FB_COMPOPT_MODEVIEW
env.clopt.modeview = value
case FB_COMPOPT_NOCMDLINE
env.clopt.nocmdline = value
case FB_COMPOPT_RETURNINFLTS
env.clopt.returninflts = value
hUpdateTargetOptions( )
end select
end sub
function fbGetOption( byval opt as integer ) as integer
select case as const( opt )
case FB_COMPOPT_OUTTYPE
function = env.clopt.outtype
case FB_COMPOPT_PPONLY
function = env.clopt.pponly
case FB_COMPOPT_BACKEND
function = env.clopt.backend
case FB_COMPOPT_TARGET
function = env.clopt.target
case FB_COMPOPT_CPUTYPE
function = env.clopt.cputype
case FB_COMPOPT_FPUTYPE
function = env.clopt.fputype
case FB_COMPOPT_FPMODE
function = env.clopt.fpmode
case FB_COMPOPT_VECTORIZE
function = env.clopt.vectorize
case FB_COMPOPT_OPTIMIZELEVEL
function = env.clopt.optlevel
case FB_COMPOPT_ASMSYNTAX
function = env.clopt.asmsyntax
case FB_COMPOPT_LANG
function = env.clopt.lang
case FB_COMPOPT_FORCELANG
function = env.clopt.forcelang
case FB_COMPOPT_RESTART_LANG
function = env.restart_lang
case FB_COMPOPT_DEBUG
function = env.clopt.debug
case FB_COMPOPT_DEBUGINFO
function = env.clopt.debuginfo
case FB_COMPOPT_ASSERTIONS
function = env.clopt.assertions
case FB_COMPOPT_ERRORCHECK
function = env.clopt.errorcheck
case FB_COMPOPT_RESUMEERROR
function = env.clopt.resumeerr
case FB_COMPOPT_EXTRAERRCHECK
function = env.clopt.extraerrchk
case FB_COMPOPT_ERRLOCATION
function = env.clopt.errlocation
case FB_COMPOPT_ARRAYBOUNDCHECK
function = env.clopt.arrayboundchk
case FB_COMPOPT_NULLPTRCHECK
function = env.clopt.nullptrchk
case FB_COMPOPT_UNWINDINFO
function = env.clopt.unwindinfo
case FB_COMPOPT_PROFILE
function = env.clopt.profile
case FB_COMPOPT_WARNINGLEVEL
function = env.clopt.warninglevel
case FB_COMPOPT_SHOWERROR
function = env.clopt.showerror
case FB_COMPOPT_MAXERRORS
function = env.clopt.maxerrors
case FB_COMPOPT_PEDANTICCHK
function = env.clopt.pdcheckopt
case FB_COMPOPT_GOSUBSETJMP
function = env.clopt.gosubsetjmp
case FB_COMPOPT_VALISTASPTR
function = env.clopt.valistasptr
case FB_COMPOPT_NOTHISCALL
function = env.clopt.nothiscall
case FB_COMPOPT_NOFASTCALL
function = env.clopt.nofastcall
case FB_COMPOPT_FBRT
function = env.clopt.fbrt
case FB_COMPOPT_EXPORT
function = env.clopt.export
case FB_COMPOPT_MSBITFIELDS
function = env.clopt.msbitfields
case FB_COMPOPT_MULTITHREADED
function = env.clopt.multithreaded
case FB_COMPOPT_FBGFX
function = env.clopt.fbgfx
case FB_COMPOPT_PIC
function = env.clopt.pic
case FB_COMPOPT_STACKSIZE
function = env.clopt.stacksize
case FB_COMPOPT_OBJINFO
function = env.clopt.objinfo
case FB_COMPOPT_SHOWINCLUDES
function = env.clopt.showincludes
case FB_COMPOPT_MODEVIEW
function = env.clopt.modeview
case FB_COMPOPT_NOCMDLINE
function = env.clopt.nocmdline
case FB_COMPOPT_RETURNINFLTS
function = env.clopt.returninflts
case else
function = 0
end select
end function
'':::::
sub fbChangeOption(byval opt as integer, byval value as integer)
select case as const opt
case FB_COMPOPT_MSBITFIELDS
fbSetOption( opt, value )
case FB_COMPOPT_LANG
'' If not yet in the desired mode
if( value <> fbGetOption( opt ) ) then
'' Not module level? then error
if( parser.scope <> FB_MAINSCOPE ) then
if( fbIsModLevel( ) = FALSE ) then
errReport( FB_ERRMSG_ILLEGALINSIDEASUB )
else
errReport( FB_ERRMSG_ILLEGALINSIDEASCOPE )
end if
'' module level
else
'' If -forcelang is enabled, ignore #lang directives
if( env.clopt.forcelang ) then
errReportWarn( FB_WARNINGMSG_CMDLINEOVERRIDES )
else
'' Never restarted due to #lang?
if( ( env.restart_status and FB_RESTART_PARSER_LANG ) = 0 ) then
fbSetOption( opt, value )
'' Tell parser to restart as soon as possible
fbRestartBeginRequest( FB_RESTART_PARSER_LANG )
fbRestartAcceptRequest( FB_RESTART_PARSER_LANG )
'' and don't show any more errors until we've restarted
errHideFurtherErrors()
'' Second pass? Show a warning and ignore
else
errReportWarn( FB_WARNINGMSG_DIRECTIVEIGNORED )
end if
end if
end if
end if
case else
errReport( FB_ERRMSG_INTERNAL )
end select
end sub
'':::::
function fbIsCrossComp _
( _
) as integer
function = (env.clopt.target <> FB_DEFAULT_TARGET)
end function
'' Build the FB target name for the given OS/arch combination.
private function hGetTargetId _
( _
byval os as integer, _ '' FB_COMPTARGET_*
byval cputype as integer _ '' FB_CPUTYPE_*
) as string
'' Target names for bin/ and lib/ subdirectories, display to the user
'' (fbc compiled for ...), FB release package names, etc.
''
'' Before 64bit/ARM support was added, FB simply used
'' dos
'' win32
'' linux
'' freebsd
'' ...
''
'' but nowadays we want to support 64bit and ARM and potentially more,
'' and the target names should be unique, so that the lib/ directories
'' can co-exist for cross-compiling. These names are also useful for
'' FB release packages, and they can be shown to the user if fbc wants
'' to tell about host/target systems (e.g. in -v output), etc.
'' dos
'' win32
'' win64
'' xbox
'' linux-x86
'' linux-x86_64
'' linux-arm
'' linux-aarch64
'' freebsd-x86
'' freebsd-x86_64
'' freebsd-powerpc | freebsd-ppc
'' freebsd-powerpc64 | freebsd-ppc64
'' freebsd-powerpc64le | freebsd-ppc64le
'' ...
var cpufamily = cputypeinfo(cputype).family
var osid = *targetinfo(os).id
'' win32/dos/xbox
select case( os )
case FB_COMPTARGET_WIN32, FB_COMPTARGET_DOS, FB_COMPTARGET_XBOX
if( cpufamily = FB_CPUFAMILY_X86 ) then
return osid
end if
end select
'' win64
if( (os = FB_COMPTARGET_WIN32) and (cpufamily = FB_CPUFAMILY_X86_64) ) then
return "win64"
end if
'' linux-x86, linux-x86_64, etc.
function = osid + "-" + *cpufamilyinfo(cpufamily).id
end function
function fbGetTargetId( ) as string
function = hGetTargetId( env.clopt.target, env.clopt.cputype )
end function
function fbGetHostId( ) as string
function = hGetTargetId( FB_DEFAULT_TARGET, FB_DEFAULT_CPUTYPE )
end function
function fbIdentifyOs( byref osid as string ) as integer
for i as integer = 0 to FB_COMPTARGETS-1
if( osid = *targetinfo(i).id ) then
return i
end if
next
function = -1
end function
function fbIdentifyCpuFamily( byref cpufamilyid as string ) as integer
for i as integer = 0 to FB_CPUFAMILY__COUNT-1
if( cpufamilyid = *cpufamilyinfo(i).id ) then
return i
end if
next
function = -1
end function
function fbDefaultCpuTypeFromCpuFamilyId( byval os as integer, byref cpufamilyid as string ) as integer
var cpufamily = fbIdentifyCpuFamily( cpufamilyid )
if( cpufamily >= 0 ) then
if( (os = FB_COMPTARGET_ANDROID) and _
(cpufamily = FB_CPUFAMILY_ARM) ) then
'' Special case: our default arm cpu should be armv5te on android
return FB_CPUTYPE_ARMV5TE
end if
return cpufamilyinfo(cpufamily).defaultcputype
end if
function = -1
end function