Remove volatiles when compiling with ARC/ORC (#19545)

This removes volatiles on ARC/ORC targets in NimMain and PreMainInner.
This avoids an issue where they couldn't be optimised out on
microcontrollers leading to larger code. Since the stack bottom doesn't
have to be initialised this way when using ARC or ORC (or None, which is
also covered by this PR) these can be safely removed.
This commit is contained in:
PMunch 2022-02-25 10:43:03 +01:00 • committed by GitHub
commit 9a49451124
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1363,16 +1363,25 @@ proc genMainProc(m: BModule) =
# The use of a volatile function pointer to call Pre/NimMainInner # The use of a volatile function pointer to call Pre/NimMainInner
# prevents inlining of the NimMainInner function and dependent # prevents inlining of the NimMainInner function and dependent
# functions, which might otherwise merge their stack frames. # functions, which might otherwise merge their stack frames.
PreMainBody = "$N" &
PreMainVolatileBody =
"\tvoid (*volatile inner)(void);$N" &
"\tinner = PreMainInner;$N" &
"$1" &
"\t(*inner)();$N"
PreMainNonVolatileBody =
"$1" &
"\tPreMainInner();$N"
PreMainBodyStart = "$N" &
"N_LIB_PRIVATE void PreMainInner(void) {$N" & "N_LIB_PRIVATE void PreMainInner(void) {$N" &
"$2" & "$2" &
"}$N$N" & "}$N$N" &
PosixCmdLine & PosixCmdLine &
"N_LIB_PRIVATE void PreMain(void) {$N" & "N_LIB_PRIVATE void PreMain(void) {$N"
"\tvoid (*volatile inner)(void);$N" &
"\tinner = PreMainInner;$N" & PreMainBodyEnd =
"$1" &
"\t(*inner)();$N" &
"}$N$N" "}$N$N"
MainProcs = MainProcs =
@ -1385,17 +1394,32 @@ proc genMainProc(m: BModule) =
"$1" & "$1" &
"}$N$N" "}$N$N"
NimMainProc = NimMainVolatileBody =
"N_CDECL(void, $5NimMain)(void) {$N" &
"\tvoid (*volatile inner)(void);$N" & "\tvoid (*volatile inner)(void);$N" &
"$4" & "$4" &
"\tinner = NimMainInner;$N" & "\tinner = NimMainInner;$N" &
"$2" & "$2" &
"\t(*inner)();$N" & "\t(*inner)();$N"
NimMainNonVolatileBody =
"$4" &
"$2" &
"\tNimMainInner();$N"
NimMainProcStart =
"N_CDECL(void, $5NimMain)(void) {$N"
NimMainProcEnd =
"}$N$N" "}$N$N"
NimMainProc = NimMainProcStart & NimMainVolatileBody & NimMainProcEnd
NimSlimMainProc = NimMainProcStart & NimMainNonVolatileBody & NimMainProcEnd
NimMainBody = NimMainInner & NimMainProc NimMainBody = NimMainInner & NimMainProc
NimSlimMainBody = NimMainInner & NimSlimMainProc
PosixCMain = PosixCMain =
"int main(int argc, char** args, char** env) {$N" & "int main(int argc, char** args, char** env) {$N" &
"\tcmdLine = args;$N" & "\tcmdLine = args;$N" &
@ -1456,10 +1480,13 @@ proc genMainProc(m: BModule) =
m.includeHeader("<libc/component.h>") m.includeHeader("<libc/component.h>")
let initStackBottomCall = let initStackBottomCall =
if m.config.target.targetOS == osStandalone or m.config.selectedGC == gcNone: "".rope if m.config.target.targetOS == osStandalone or m.config.selectedGC in {gcNone, gcArc, gcOrc}: "".rope
else: ropecg(m, "\t#initStackBottomWith((void *)&inner);$N", []) else: ropecg(m, "\t#initStackBottomWith((void *)&inner);$N", [])
inc(m.labels) inc(m.labels)
appcg(m, m.s[cfsProcs], PreMainBody, [m.g.mainDatInit, m.g.otherModsInit]) if m.config.selectedGC notin {gcNone, gcArc, gcOrc}:
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit])
else:
appcg(m, m.s[cfsProcs], PreMainBodyStart & PreMainNonVolatileBody & PreMainBodyEnd, [m.g.mainDatInit, m.g.otherModsInit])
if m.config.target.targetOS == osWindows and if m.config.target.targetOS == osWindows and
m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}: m.config.globalOptions * {optGenGuiApp, optGenDynLib} != {}:
@ -1484,9 +1511,14 @@ proc genMainProc(m: BModule) =
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
else: else:
if m.config.selectedGC notin {gcNone, gcArc, gcOrc}:
const nimMain = NimMainBody const nimMain = NimMainBody
appcg(m, m.s[cfsProcs], nimMain, appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix]) [m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
else:
const nimMain = NimSlimMainBody
appcg(m, m.s[cfsProcs], nimMain,
[m.g.mainModInit, initStackBottomCall, m.labels, preMainCode, m.config.nimMainPrefix])
if optNoMain notin m.config.globalOptions: if optNoMain notin m.config.globalOptions:
if m.config.cppCustomNamespace.len > 0: if m.config.cppCustomNamespace.len > 0: