Skip to content

Commit

Permalink
fbc: add '-z nobuiltins' command line option
Browse files Browse the repository at this point in the history
- '-z nobuiltins' command line option disables all built-in procedure
  definitions except for those that are required by current version.
- procedures fb_Init(), fb_InitSignals(), __main(), fb_end(), _monstartup()
  are required since they are referenced by rtlAppInit() and rtlExitApp()
  and currently this generated code can not be disabled.
- internal: added FB_RTL_OPT_REQUIRED to indicate required procedures
- other procedures are still required for fbc to compile user code fully,
  but it depends on the user code to be compiled.  Therefore it is
  possible to provide declarations and definitions for the of the other
  built-in procedures needed in the user source code itself if the
  declaration is made before it is actually referenced by the compiler
  and defined before linking the executable
  • Loading branch information
jayrm committed Feb 24, 2024
1 parent 74a9856 commit 0cd8514
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Version 1.20.0
- fbc: __FB_BUILD_FORK_ID__ intrinsic define. By default is an empty string. Can be customized by makefile
- fbc: '-print fork-id' command line option to report the fork-id version of fbc
- fbc: '-nolib fbrt0.o' and '-nolib fbrt0pic.o' can be used to omit fb's startup files from the link. Implied by '-nodeflibs'.
- fbc: '-z nobuiltins' command line option to disable all non-required built-in procedure definitions

[fixed]
- github #410: give consistent floating point comparisons results involving NaNs.
Expand Down
3 changes: 3 additions & 0 deletions doc/fbc.1
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ Don't use '__fastcall' calling convention
\fB\-z no-thiscall\fR
Don't use '__thiscall' calling convention
.TP
\fB\-z nobuiltins\fR
Disable all non-required builtin procedure definitions
.TP
\fB\-z nocmdline\fR
Disable #cmdline source directives
.TP
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/fb.bas
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ sub fbGlobalInit()
env.clopt.modeview = FB_DEFAULT_MODEVIEW
env.clopt.nocmdline = FALSE
env.clopt.returninflts = FALSE
env.clopt.nobuiltins = FALSE

env.restart_request = FB_RESTART_NONE
env.restart_action = FB_RESTART_NONE
Expand Down Expand Up @@ -738,6 +739,8 @@ sub fbSetOption( byval opt as integer, byval value as integer )
case FB_COMPOPT_RETURNINFLTS
env.clopt.returninflts = value
hUpdateTargetOptions( )
case FB_COMPOPT_NOBUILTINS
env.clopt.nobuiltins = value
end select
end sub

Expand Down Expand Up @@ -836,6 +839,8 @@ function fbGetOption( byval opt as integer ) as integer
function = env.clopt.nocmdline
case FB_COMPOPT_RETURNINFLTS
function = env.clopt.returninflts
case FB_COMPOPT_NOBUILTINS
function = env.clopt.nobuiltins

case else
function = 0
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/fb.bi
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ enum FB_COMPOPT
FB_COMPOPT_MODEVIEW '' enum: FB_MODEVIEW (__FB_GUI__)
FB_COMPOPT_NOCMDLINE '' boolean: -z nocmdline, disable #cmdline directives
FB_COMPOPT_RETURNINFLTS '' boolean: -z retinflts, enable returning some structs in floating point registers
FB_COMPOPT_NOBUILTINS '' boolean: -z nobuiltins, disable all non-required builtin procedure definitions

FB_COMPOPTIONS
end enum
Expand Down Expand Up @@ -333,6 +334,7 @@ type FBCMMLINEOPT
modeview as FB_MODEVIEW
nocmdline as integer '' dissallow #cmdline directive? (default = false)
returninflts as integer '' enable returning some structs in floating point registers
nobuiltins as integer '' disable all non-required builtin procedure definitions
end type

'' features allowed in the selected language
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/fbc.bas
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,8 @@ private sub handleOpt _
fbSetOption( FB_COMPOPT_NOCMDLINE, TRUE )
case "retinflts"
fbSetOption( FB_COMPOPT_RETURNINFLTS, TRUE )
case "nobuiltins"
fbSetOption( FB_COMPOPT_NOBUILTINS, TRUE )
case else
hFatalInvalidOption( arg, is_source )
end select
Expand Down Expand Up @@ -4520,6 +4522,7 @@ private sub hPrintOptions( byval verbose as integer )
print " -z gosub-setjmp Use setjmp/longjmp to implement GOSUB"
print " -z no-thiscall Don't use '__thiscall' calling convention"
print " -z no-fastcall Don't use '__fastcall' calling convention"
print " -z nobuiltins Disable all non-required builtin procedure definitions"
print " -z nocmdline Disable #cmdline source directives"
print " -z retinflts Enable returning some types in floating point registers"
print " -z valist-as-ptr Use pointer expressions to implement CVA_*() macros"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rtl-profile.bas
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
( _
@FB_RTL_PROFILEMONSTARTUP, @"_monstartup", _
FB_DATATYPE_VOID, FB_FUNCMODE_CDECL, _
NULL, FB_RTL_OPT_NONE, _
NULL, FB_RTL_OPT_REQUIRED, _
0 _
), _
( NULL ) _
Expand Down
45 changes: 25 additions & 20 deletions src/compiler/rtl-system.bas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare function hThreadCall_cb ( byval sym as FBSYMBOL ptr ) as integer
( _
@FB_RTL_INIT, NULL, _
FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
NULL, FB_RTL_OPT_NONE, _
NULL, FB_RTL_OPT_REQUIRED, _
3, _
{ _
( FB_DATATYPE_LONG, FB_PARAMMODE_BYVAL, FALSE ), _
Expand All @@ -36,21 +36,21 @@ declare function hThreadCall_cb ( byval sym as FBSYMBOL ptr ) as integer
( _
@FB_RTL_INITSIGNALS, NULL, _
FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
NULL, FB_RTL_OPT_NONE, _
NULL, FB_RTL_OPT_REQUIRED, _
0 _
), _
/' sub __main cdecl( ) '/ _
( _
@FB_RTL_INITCRTCTOR, @"__main", _
FB_DATATYPE_VOID, FB_FUNCMODE_CDECL, _
NULL, FB_RTL_OPT_NONE, _
NULL, FB_RTL_OPT_REQUIRED, _
0 _
), _
/' sub fb_End( byval errlevel as const long ) '/ _
( _
@FB_RTL_END, NULL, _
FB_DATATYPE_VOID, FB_FUNCMODE_FBCALL, _
NULL, FB_RTL_OPT_NONE, _
NULL, FB_RTL_OPT_REQUIRED, _
1, _
{ _
( typeSetIsConst( FB_DATATYPE_LONG ), FB_PARAMMODE_BYVAL, FALSE ) _
Expand Down Expand Up @@ -660,14 +660,17 @@ function rtlInitApp _
is_exe = (env.clopt.outtype <> FB_OUTTYPE_DYNAMICLIB)

if( env.clopt.backend = FB_BACKEND_GAS ) then
'' call __monstartup() on win32/cygwin if profiling
select case( env.clopt.target )
case FB_COMPTARGET_WIN32, FB_COMPTARGET_CYGWIN
if( env.clopt.profile ) then
'' __monstartup()
rtlProfileCall_monstartup( )
end if
end select

if( env.clopt.nobuiltins = FALSE ) then
'' call __monstartup() on win32/cygwin if profiling
select case( env.clopt.target )
case FB_COMPTARGET_WIN32, FB_COMPTARGET_CYGWIN
if( env.clopt.profile ) then
'' __monstartup()
rtlProfileCall_monstartup( )
end if
end select
end if

'' call default CRT0 constructors (only required for Win32)
if( env.clopt.target = FB_COMPTARGET_WIN32 ) then
Expand All @@ -683,15 +686,17 @@ function rtlInitApp _
astNewARG( proc, astNewCONSTi( env.clopt.lang ) )
astAdd( proc )

'' Error checking enabled and not a DLL? And emscripten doesn't have signals
if( env.clopt.errorcheck andalso is_exe andalso ( env.clopt.target <> FB_COMPTARGET_JS ) ) then
'' fb_InitSignals( )
astAdd( astNewCALL( PROCLOOKUP( INITSIGNALS ), NULL ) )
if( env.clopt.nobuiltins = FALSE ) then
'' Error checking enabled and not a DLL? And emscripten doesn't have signals
if( env.clopt.errorcheck andalso is_exe andalso ( env.clopt.target <> FB_COMPTARGET_JS ) ) then
'' fb_InitSignals( )
astAdd( astNewCALL( PROCLOOKUP( INITSIGNALS ), NULL ) )

'' Checking the CPU for features on x86
if( fbGetCpuFamily( ) = FB_CPUFAMILY_X86 ) then
'' Check CPU type
rtlX86CpuCheck( )
'' Checking the CPU for features on x86
if( fbGetCpuFamily( ) = FB_CPUFAMILY_X86 ) then
'' Check CPU type
rtlX86CpuCheck( )
end if
end if
end if

Expand Down
13 changes: 12 additions & 1 deletion src/compiler/rtl.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ sub rtlAddIntrinsicProcs( byval procdef as const FB_RTL_PROCDEF ptr )
end if
end if

if( (procdef->options and FB_RTL_OPT_REQUIRED) = 0 ) then
if( env.clopt.nobuiltins ) then
doadd = FALSE
end if
end if

if( doadd ) then
dim as FBSYMBOL ptr proc = symbPreAddProc( NULL )

Expand Down Expand Up @@ -255,9 +261,14 @@ sub rtlAddIntrinsicProcs( byval procdef as const FB_RTL_PROCDEF ptr )
palias = pname
end if

dim symb_opts as FB_SYMBOPT = FB_SYMBOPT_NONE
if( env.clopt.nobuiltins = FALSE ) then
symb_opts or= FB_SYMBOPT_DECLARING or FB_SYMBOPT_RTL
end if

proc = symbAddProc( proc, pname, palias, _
procdef->dtype, NULL, attrib, pattrib, callconv, _
FB_SYMBOPT_DECLARING or FB_SYMBOPT_RTL )
symb_opts )

if( proc <> NULL ) then
symbSetProcCallback( proc, procdef->callback )
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rtl.bi
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ enum FB_RTL_OPT
FB_RTL_OPT_MT = &h00000004 '' needs the multithreaded rtlib

FB_RTL_OPT_ASSERTONLY = &h00000010 '' only if asserts are enabled
'' = &h00000020
FB_RTL_OPT_REQUIRED = &h00000020 '' required even if -z nobuiltins was given
FB_RTL_OPT_STRSUFFIX = &h00000040 '' has a $ suffix (-lang qb only)
FB_RTL_OPT_NOQB = &h00000080 '' anything but -lang qb
FB_RTL_OPT_QBONLY = &h00000100 '' -lang qb only
Expand Down

0 comments on commit 0cd8514

Please sign in to comment.