Add the ability to pass a value with the -d flag
This allows the end user to use the {.magic: "IntDefine"/"StrDefine"}
pragmas to pass values into code at compile time. This has a nice side
effect of also allowing/requiring a default value to be assigned in the
code (see osalloc.nim/StandaloneHeapSize for an example)
This commit is contained in:
parent
38de553b86
commit
94d1aa5109
7 changed files with 32 additions and 7 deletions
|
|
@ -610,7 +610,7 @@ type
|
||||||
mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl,
|
mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl,
|
||||||
mNHint, mNWarning, mNError,
|
mNHint, mNWarning, mNError,
|
||||||
mInstantiationInfo, mGetTypeInfo, mNGenSym,
|
mInstantiationInfo, mGetTypeInfo, mNGenSym,
|
||||||
mNimvm
|
mNimvm, mIntDefine, mStrDefine
|
||||||
|
|
||||||
# things that we can evaluate safely at compile time, even if not asked for it:
|
# things that we can evaluate safely at compile time, even if not asked for it:
|
||||||
const
|
const
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,13 @@ proc splitSwitch(switch: string, cmd, arg: var string, pass: TCmdLinePass,
|
||||||
elif switch[i] in {':', '=', '['}: arg = substr(switch, i + 1)
|
elif switch[i] in {':', '=', '['}: arg = substr(switch, i + 1)
|
||||||
else: invalidCmdLineOption(pass, switch, info)
|
else: invalidCmdLineOption(pass, switch, info)
|
||||||
|
|
||||||
|
proc hasKeyValuePair(arg: string): bool =
|
||||||
|
for i in 0..arg.high:
|
||||||
|
if arg[i] in {':', '='}:
|
||||||
|
return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
proc processOnOffSwitch(op: TOptions, arg: string, pass: TCmdLinePass,
|
proc processOnOffSwitch(op: TOptions, arg: string, pass: TCmdLinePass,
|
||||||
info: TLineInfo) =
|
info: TLineInfo) =
|
||||||
case whichKeyword(arg)
|
case whichKeyword(arg)
|
||||||
|
|
@ -342,6 +349,10 @@ proc processSwitch(switch, arg: string, pass: TCmdLinePass, info: TLineInfo) =
|
||||||
discard "allow for backwards compatibility, but don't do anything"
|
discard "allow for backwards compatibility, but don't do anything"
|
||||||
of "define", "d":
|
of "define", "d":
|
||||||
expectArg(switch, arg, pass, info)
|
expectArg(switch, arg, pass, info)
|
||||||
|
if hasKeyValuePair(arg):
|
||||||
|
splitSwitch(arg, key, val, pass, info)
|
||||||
|
defineSymbol(key, val)
|
||||||
|
else:
|
||||||
defineSymbol(arg)
|
defineSymbol(arg)
|
||||||
of "undef", "u":
|
of "undef", "u":
|
||||||
expectArg(switch, arg, pass, info)
|
expectArg(switch, arg, pass, info)
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ var gSymbols: StringTableRef
|
||||||
const
|
const
|
||||||
catNone = "false"
|
catNone = "false"
|
||||||
|
|
||||||
proc defineSymbol*(symbol: string) =
|
proc defineSymbol*(symbol: string, value: string = "true") =
|
||||||
gSymbols[symbol] = "true"
|
gSymbols[symbol] = value
|
||||||
|
|
||||||
proc undefSymbol*(symbol: string) =
|
proc undefSymbol*(symbol: string) =
|
||||||
gSymbols[symbol] = catNone
|
gSymbols[symbol] = catNone
|
||||||
|
|
@ -62,6 +62,11 @@ proc isDefined*(symbol: string): bool =
|
||||||
|
|
||||||
proc isDefined*(symbol: PIdent): bool = isDefined(symbol.s)
|
proc isDefined*(symbol: PIdent): bool = isDefined(symbol.s)
|
||||||
|
|
||||||
|
proc lookupSymbol*(symbol: string): string =
|
||||||
|
result = if isDefined(symbol): gSymbols[symbol] else: nil
|
||||||
|
|
||||||
|
proc lookupSymbol*(symbol: PIdent): string = lookupSymbol(symbol.s)
|
||||||
|
|
||||||
iterator definedSymbolNames*: string =
|
iterator definedSymbolNames*: string =
|
||||||
for key, val in pairs(gSymbols):
|
for key, val in pairs(gSymbols):
|
||||||
if val != catNone: yield key
|
if val != catNone: yield key
|
||||||
|
|
|
||||||
|
|
@ -640,6 +640,12 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||||
of mNaN: result = newFloatNodeT(NaN, n)
|
of mNaN: result = newFloatNodeT(NaN, n)
|
||||||
of mInf: result = newFloatNodeT(Inf, n)
|
of mInf: result = newFloatNodeT(Inf, n)
|
||||||
of mNegInf: result = newFloatNodeT(NegInf, n)
|
of mNegInf: result = newFloatNodeT(NegInf, n)
|
||||||
|
of mIntDefine:
|
||||||
|
if isDefined(s.name):
|
||||||
|
result = newIntNodeT(lookupSymbol(s.name).parseInt, n)
|
||||||
|
of mStrDefine:
|
||||||
|
if isDefined(s.name):
|
||||||
|
result = newStrNodeT(lookupSymbol(s.name), n)
|
||||||
else:
|
else:
|
||||||
if sfFakeConst notin s.flags: result = copyTree(s.ast)
|
if sfFakeConst notin s.flags: result = copyTree(s.ast)
|
||||||
of {skProc, skMethod}:
|
of {skProc, skMethod}:
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ type
|
||||||
wColon, wColonColon, wEquals, wDot, wDotDot,
|
wColon, wColonColon, wEquals, wDot, wDotDot,
|
||||||
wStar, wMinus,
|
wStar, wMinus,
|
||||||
wMagic, wThread, wFinal, wProfiler, wObjChecks,
|
wMagic, wThread, wFinal, wProfiler, wObjChecks,
|
||||||
|
wIntDefine, wStrDefine,
|
||||||
|
|
||||||
wDestroy,
|
wDestroy,
|
||||||
|
|
||||||
|
|
@ -121,7 +122,7 @@ const
|
||||||
|
|
||||||
":", "::", "=", ".", "..",
|
":", "::", "=", ".", "..",
|
||||||
"*", "-",
|
"*", "-",
|
||||||
"magic", "thread", "final", "profiler", "objchecks",
|
"magic", "thread", "final", "profiler", "objchecks", "intdefine", "strdefine",
|
||||||
|
|
||||||
"destroy",
|
"destroy",
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ Arguments:
|
||||||
arguments are passed to the program being run (if --run option is selected)
|
arguments are passed to the program being run (if --run option is selected)
|
||||||
Options:
|
Options:
|
||||||
-p, --path:PATH add path to search paths
|
-p, --path:PATH add path to search paths
|
||||||
-d, --define:SYMBOL define a conditional symbol
|
-d, --define:SYMBOL(:VAL) define a conditional symbol
|
||||||
|
(Optionally: Define the value for that symbol)
|
||||||
-u, --undef:SYMBOL undefine a conditional symbol
|
-u, --undef:SYMBOL undefine a conditional symbol
|
||||||
-f, --forceBuild force rebuilding of all modules
|
-f, --forceBuild force rebuilding of all modules
|
||||||
--stackTrace:on|off turn stack tracing on|off
|
--stackTrace:on|off turn stack tracing on|off
|
||||||
|
|
|
||||||
|
|
@ -150,8 +150,9 @@ elif defined(windows):
|
||||||
#VirtualFree(p, size, MEM_DECOMMIT)
|
#VirtualFree(p, size, MEM_DECOMMIT)
|
||||||
|
|
||||||
elif hostOS == "standalone":
|
elif hostOS == "standalone":
|
||||||
|
const StandaloneHeapSize {.magic: "IntDefine"}: int = 1024 * PageSize
|
||||||
var
|
var
|
||||||
theHeap: array[1024*PageSize, float64] # 'float64' for alignment
|
theHeap: array[StandaloneHeapSize, float64] # 'float64' for alignment
|
||||||
bumpPointer = cast[int](addr theHeap)
|
bumpPointer = cast[int](addr theHeap)
|
||||||
|
|
||||||
proc osAllocPages(size: int): pointer {.inline.} =
|
proc osAllocPages(size: int): pointer {.inline.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue