Skip to content

Commit

Permalink
fbc: #cmdline - reset global state for static variables
Browse files Browse the repository at this point in the history
- some fbc procedures are optimized with use of a static variable to hold state
- reset these variables on a parser restart by tracking number of times the parser/fbc has been restarted
  • Loading branch information
jayrm committed Sep 10, 2021
1 parent 735ea06 commit 80fc968
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 24 deletions.
8 changes: 4 additions & 4 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ Version 1.09.0
- objinfo: check ELF files for little endian or big endian signature
- fbc: __thiscall declaration support for gcc/gas 32-bit x86 calling g++ classes from fbc passing first argument in ECX register, caller cleans up stack except on win32 x86
- fbc: add __FB_OPTIMIZE__ intrinisic define - always defined and will have a value from 0 to 3 to indicate the optimization level passed to the backend compiler
- github #341: fbc: #cmdline "args..." directive to specify command line arguments within source files, restarting the parser from some options
- github #341: fbc: #cmdline "args..." directive to specify command line arguments within source files, restarting the parser for some options, and restarting fbc for others
- github #341: fbc: #cmdline "-end" will trigger a parser or fbc restart if needed. fbc can't detect the end of reading all the #cmdlines to do this automatically
- github #341: fbc: #cmdline "-restart" will always trigger an fbc restart
- github #341: fbc: only process #cmdline in the first source module (which must be specified on the real command line)
- github #341: fbc: add '-z nocmdline' command line option to ignore #cmdline directives - allows overriding source directives with real fbc command line
- github #341: fbc: #cmdline "-restart" will always trigger one fbc restart
- github #341: fbc: only process #cmdline in the first pass of the source module (which must be specified on the real command line)
- github #341: fbc: '-z nocmdline' command line option to ignore #cmdline directives, allows overriding source directives with real fbc command line, show warning if used with '-w all'

[fixed]
- github #315: set parameters when calling SCREENCONTROL (was broken in fbc 1.08.0 due to new LONG/LONGINT SCREENCONTROL API's)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/fb.bas
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,8 @@ sub fbRestartCloseRequest( byval filter as FB_RESTART_FLAGS )
'' update status, action is completed
env.restart_status or= (env.restart_action and filter)

env.pass_id += 1

'' clear the action
env.restart_action and= not filter
end sub
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/fb.bi
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@ declare sub fbRestartBeginRequest( byval flags as FB_RESTART_FLAGS )
declare sub fbRestartAcceptRequest( byval flags as FB_RESTART_FLAGS )
declare sub fbRestartCloseRequest( byval flags as FB_RESTART_FLAGS )

#macro fbRestartableStaticVariable( datatype, varname, defaultvalue )
'' create a local static variable, but reset it to the default if fb is restarted
static as integer pass_id
static as datatype varname = defaultvalue
if( pass_id <> env.pass_id ) then
pass_id = env.pass_id
varname = defaultvalue
end if
#endmacro

declare sub fbGlobalInit()
declare sub fbAddIncludePath(byref path as string)
declare sub fbAddPreDefine(byref def as string)
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/fbc.bas
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,10 @@ end sub
hPrintOptions( fbc.verbose )
fbcEnd( 1 )
end if
else
if( fbc.showversion ) then
hPrintVersion( fbc.verbose )
end if
end if

fbcDeterminePrefix( )
Expand Down
1 change: 1 addition & 0 deletions src/compiler/fbint.bi
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ type FBENV
restart_request as FB_RESTART_FLAGS '' request parser or fbc restart on a trigger later (e.g #cmdline "-end")
restart_action as FB_RESTART_FLAGS '' restart as soon as possible
restart_status as FB_RESTART_FLAGS '' current status of restarts #lang/#cmdline/parser/fbc
pass_id as integer '' number of restarts

'' Lists to collect #inclibs and #libpaths
libs as TSTRSET
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/parser-decl-proc-params.bas
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ private function hParamDecl _
) as FBSYMBOL ptr

static as zstring * FB_MAXNAMELEN+1 idTB(0 to FB_MAXARGRECLEVEL-1)
static as integer reclevel = 0
fbRestartableStaticVariable( integer, reclevel, 0 )

dim as zstring ptr id = any
dim as integer dtype = any, mode = any, attrib = any, dimensions = any
dim as integer readid = any, dotpos = any, doskip = any
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser-decl-symb-init.bas
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private function hArrayInit _
end function

private function hUDTInit( byref ctx as FB_INITCTX ) as integer
static as integer rec_cnt
fbRestartableStaticVariable( integer, rec_cnt, 0 )

dim as integer elm_cnt = any
dim as longint lgt = any, baseofs = any, pad_lgt = any
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/rtl-gfx.bas
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,8 @@ private function hPorts_cb _
byval sym as FBSYMBOL ptr _
) as integer

static as integer libsAdded = FALSE
'' minor optimization to avoid having to lookup env.libs hash
fbRestartableStaticVariable( integer, libsAdded, FALSE )

if( libsadded = FALSE ) then
libsAdded = TRUE
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/rtl-math.bas
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,16 @@ function rtlMathBop _
end function

private function hRndCallback( byval sym as FBSYMBOL ptr ) as integer
static as integer added = FALSE
'' minor optimization to avoid having to lookup env.libs hash
fbRestartableStaticVariable( integer, libsAdded, FALSE )

if( added = FALSE ) then
added = TRUE
if( libsAdded = FALSE ) then
libsAdded = TRUE
select case env.clopt.target
case FB_COMPTARGET_WIN32, FB_COMPTARGET_CYGWIN
fbAddLib( "advapi32" )
end select
end if

return TRUE
return TRUE
end function
8 changes: 4 additions & 4 deletions src/compiler/rtl-print.bas
Original file line number Diff line number Diff line change
Expand Up @@ -1200,11 +1200,11 @@ function rtlPrinter_cb _
byval sym as FBSYMBOL ptr _
) as integer

static as integer libsAdded = FALSE
'' minor optimization to avoid having to lookup env.libs hash
fbRestartableStaticVariable( integer, libsAdded, FALSE )

if( libsadded = FALSE ) then

libsAdded = TRUE
libsAdded = TRUE

select case env.clopt.target
case FB_COMPTARGET_WIN32, FB_COMPTARGET_CYGWIN
Expand All @@ -1214,6 +1214,6 @@ function rtlPrinter_cb _

end if

function = TRUE
function = TRUE

end function
17 changes: 9 additions & 8 deletions src/compiler/rtl-system.bas
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,15 @@ private function hThreadCall_cb _
byval sym as FBSYMBOL ptr _
) as integer

static as integer libsAdded = FALSE
'' minor optimization to avoid having to lookup env.libs hash
fbRestartableStaticVariable( integer, libsAdded, FALSE )

if( libsadded = FALSE ) then
libsAdded = TRUE
fbAddLib( "ffi" )
end if

return hMultithread_cb( sym )
return hMultithread_cb( sym )
end function

'':::::
Expand All @@ -752,18 +753,18 @@ function rtlAtExit _
byval procexpr as ASTNODE ptr _
) as ASTNODE ptr static

dim as ASTNODE ptr proc
dim as ASTNODE ptr proc

function = NULL

'' atexit( proc )
proc = astNewCALL( PROCLOOKUP( ATEXIT ) )
proc = astNewCALL( PROCLOOKUP( ATEXIT ) )

if( astNewARG( proc, procexpr ) = NULL ) then
exit function
end if
if( astNewARG( proc, procexpr ) = NULL ) then
exit function
end if

function = proc
function = proc

end function

Expand Down
3 changes: 3 additions & 0 deletions src/compiler/symb-mangling.bas
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,10 @@ private sub hMangleNamespace _
end sub

private sub hMangleVariable( byval sym as FBSYMBOL ptr )
'' local optimization for 'id' allocation - it's always initialized by logic below
static as string id

'' !!!TODO!!! should varcounter reset between modules and on restarts with fbRestartableStaticVariable()?
static as integer varcounter
dim as string mangled
dim as zstring ptr p = any
Expand Down
2 changes: 1 addition & 1 deletion todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ o -exx should catch...
[X] painful to add
[X] added as '#cmdline "args..."'
[ ] painful to complete
[ ] don't allow #cmdline if any line was parsed already
[ ] don't allow #cmdline if any line was parsed already that emits backend code
[X] add '-z nocmdline' to ignore #cmdline directives
[X] reset parser for command line options that require it
[X] reset fbc for command line options that require it (-target, -arch, -lib, etc)
Expand Down

0 comments on commit 80fc968

Please sign in to comment.