Added `global` pragma that can be used to introduce new global variables from within procs

This commit is contained in:
Zahary Karadjov 2012-04-05 19:20:52 +03:00
commit 6d80583d5a
6 changed files with 22 additions and 10 deletions

View file

@ -7,6 +7,8 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
# included from cgen.nim
const const
RangeExpandLimit = 256 # do not generate ranges RangeExpandLimit = 256 # do not generate ranges
# over 'RangeExpandLimit' elements # over 'RangeExpandLimit' elements
@ -48,16 +50,19 @@ proc loadInto(p: BProc, le, ri: PNode, a: var TLoc) {.inline.} =
proc genSingleVar(p: BProc, a: PNode) = proc genSingleVar(p: BProc, a: PNode) =
var v = a.sons[0].sym var v = a.sons[0].sym
if sfCompileTime in v.flags: return if sfCompileTime in v.flags: return
var targetProc = p
var immediateAsgn = a.sons[2].kind != nkEmpty var immediateAsgn = a.sons[2].kind != nkEmpty
if sfGlobal in v.flags: if sfGlobal in v.flags:
assignGlobalVar(p, v) targetProc = p.module.initProc
genObjectInit(p, cpsInit, v.typ, v.loc, true) assignGlobalVar(targetProc, v)
genObjectInit(targetProc, cpsInit, v.typ, v.loc, true)
else: else:
assignLocalVar(p, v) assignLocalVar(p, v)
initLocalVar(p, v, immediateAsgn) initLocalVar(p, v, immediateAsgn)
if immediateAsgn: if immediateAsgn:
genLineDir(p, a) genLineDir(targetProc, a)
loadInto(p, a.sons[0], a.sons[2], v.loc) loadInto(targetProc, a.sons[0], a.sons[2], v.loc)
proc genClosureVar(p: BProc, a: PNode) = proc genClosureVar(p: BProc, a: PNode) =
var immediateAsgn = a.sons[2].kind != nkEmpty var immediateAsgn = a.sons[2].kind != nkEmpty

View file

@ -221,4 +221,4 @@ proc binaryStrSearch*(x: openarray[string], y: string): int =
# Can we keep this? I'm using it all the time # Can we keep this? I'm using it all the time
template nimdbg*: expr = c.filename.endsWith"nimdbg.nim" template nimdbg*: expr = c.filename.endsWith"nimdbg.nim"
template cnimdbg*: expr = p.module.filename.endsWith"nimdbg.nim"

View file

@ -51,7 +51,7 @@ const
wImportcpp, wImportobjc, wError} wImportcpp, wImportobjc, wError}
varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl,
wMagic, wHeader, wDeprecated, wCompilerProc, wDynLib, wExtern, wMagic, wHeader, wDeprecated, wCompilerProc, wDynLib, wExtern,
wImportcpp, wImportobjc, wError, wNoInit, wCompileTime} wImportcpp, wImportobjc, wError, wNoInit, wCompileTime, wGlobal}
constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl, constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl,
wExtern, wImportcpp, wImportobjc, wError} wExtern, wImportcpp, wImportobjc, wError}
letPragmas* = varPragmas letPragmas* = varPragmas
@ -494,6 +494,9 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
noVal(it) noVal(it)
incl(sym.flags, sfCompileTime) incl(sym.flags, sfCompileTime)
incl(sym.loc.Flags, lfNoDecl) incl(sym.loc.Flags, lfNoDecl)
of wGlobal:
noVal(it)
incl(sym.flags, sfGlobal)
of wMerge: of wMerge:
noval(it) noval(it)
incl(sym.flags, sfMerge) incl(sym.flags, sfMerge)

View file

@ -409,8 +409,10 @@ proc isAssignable(c: PContext, n: PNode): TAssignableResult =
of nkSym: of nkSym:
# don't list 'skLet' here: # don't list 'skLet' here:
if n.sym.kind in {skVar, skResult, skTemp}: if n.sym.kind in {skVar, skResult, skTemp}:
if c.p.owner.id == n.sym.owner.id: result = arLocalLValue if c.p.owner.id == n.sym.owner.id and sfGlobal notin n.sym.flags:
else: result = arLValue result = arLocalLValue
else:
result = arLValue
of nkDotExpr: of nkDotExpr:
if skipTypes(n.sons[0].typ, abstractInst).kind in {tyVar, tyPtr, tyRef}: if skipTypes(n.sons[0].typ, abstractInst).kind in {tyVar, tyPtr, tyRef}:
result = arLValue result = arLValue

View file

@ -58,7 +58,7 @@ type
wWatchPoint, wSubsChar, wWatchPoint, wSubsChar,
wAcyclic, wShallow, wUnroll, wLinearScanEnd, wAcyclic, wShallow, wUnroll, wLinearScanEnd,
wWrite, wPutEnv, wPrependEnv, wAppendEnv, wThreadVar, wEmit, wNoStackFrame, wWrite, wPutEnv, wPrependEnv, wAppendEnv, wThreadVar, wEmit, wNoStackFrame,
wImplicitStatic wImplicitStatic, wGlobal
TSpecialWords* = set[TSpecialWord] TSpecialWords* = set[TSpecialWord]
@ -107,7 +107,7 @@ const
"watchpoint", "watchpoint",
"subschar", "acyclic", "shallow", "unroll", "linearscanend", "subschar", "acyclic", "shallow", "unroll", "linearscanend",
"write", "putenv", "prependenv", "appendenv", "threadvar", "emit", "write", "putenv", "prependenv", "appendenv", "threadvar", "emit",
"nostackframe", "implicitstatic"] "nostackframe", "implicitstatic", "global"]
proc findStr*(a: openarray[string], s: string): int = proc findStr*(a: openarray[string], s: string): int =
for i in countup(low(a), high(a)): for i in countup(low(a), high(a)):

View file

@ -57,6 +57,8 @@ Language Additions
- Added explicit ``static`` sections for enforced compile time evaluation. - Added explicit ``static`` sections for enforced compile time evaluation.
- ``addr`` is now treated like a prefix operator syntactically. - ``addr`` is now treated like a prefix operator syntactically.
- Added ``global`` pragma that can be used to introduce new global variables
from within procs.
2012-02-09 Version 0.8.14 released 2012-02-09 Version 0.8.14 released