refactoring: flags instead of bools

This commit is contained in:
Andreas Rumpf 2016-05-15 23:34:30 +02:00
commit 74aab132bd
6 changed files with 31 additions and 25 deletions

View file

@ -1235,7 +1235,7 @@ proc genOfHelper(p: BProc; dest: PType; a: Rope): Rope =
# unfortunately 'genTypeInfo' sets tfObjHasKids as a side effect, so we # unfortunately 'genTypeInfo' sets tfObjHasKids as a side effect, so we
# have to call it here first: # have to call it here first:
let ti = genTypeInfo(p.module, dest) let ti = genTypeInfo(p.module, dest)
if tfFinal in dest.flags or (p.module.objHasKidsValid and if tfFinal in dest.flags or (objHasKidsValid in p.module.flags and
tfObjHasKids notin dest.flags): tfObjHasKids notin dest.flags):
result = "$1.m_type == $2" % [a, ti] result = "$1.m_type == $2" % [a, ti]
else: else:

View file

@ -107,8 +107,8 @@ proc genMergeInfo*(m: BModule): Rope =
writeIntSet(m.typeInfoMarker, s) writeIntSet(m.typeInfoMarker, s)
s.add("labels:") s.add("labels:")
encodeVInt(m.labels, s) encodeVInt(m.labels, s)
s.add(" hasframe:") s.add(" flags:")
encodeVInt(ord(m.frameDeclared), s) encodeVInt(cast[int](m.flags), s)
s.add(tnl) s.add(tnl)
s.add("*/") s.add("*/")
result = s.rope result = s.rope
@ -222,7 +222,8 @@ proc processMergeInfo(L: var TBaseLexer, m: BModule) =
of "declared": readIntSet(L, m.declaredThings) of "declared": readIntSet(L, m.declaredThings)
of "typeInfo": readIntSet(L, m.typeInfoMarker) of "typeInfo": readIntSet(L, m.typeInfoMarker)
of "labels": m.labels = decodeVInt(L.buf, L.bufpos) of "labels": m.labels = decodeVInt(L.buf, L.bufpos)
of "hasframe": m.frameDeclared = decodeVInt(L.buf, L.bufpos) != 0 of "flags":
m.flags = cast[set[CodegenFlag]](decodeVInt(L.buf, L.bufpos) != 0)
else: internalError("ccgmerge: unknown key: " & k) else: internalError("ccgmerge: unknown key: " & k)
when not defined(nimhygiene): when not defined(nimhygiene):

View file

@ -18,7 +18,7 @@ proc emulatedThreadVars(): bool =
proc accessThreadLocalVar(p: BProc, s: PSym) = proc accessThreadLocalVar(p: BProc, s: PSym) =
if emulatedThreadVars() and not p.threadVarAccessed: if emulatedThreadVars() and not p.threadVarAccessed:
p.threadVarAccessed = true p.threadVarAccessed = true
p.module.usesThreadVars = true incl p.module.flags, usesThreadVars
addf(p.procSec(cpsLocals), "\tNimThreadVars* NimTV;$n", []) addf(p.procSec(cpsLocals), "\tNimThreadVars* NimTV;$n", [])
add(p.procSec(cpsInit), add(p.procSec(cpsInit),
ropecg(p.module, "\tNimTV = (NimThreadVars*) #GetThreadLocalVars();$n")) ropecg(p.module, "\tNimTV = (NimThreadVars*) #GetThreadLocalVars();$n"))
@ -51,7 +51,7 @@ proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
addf(m.s[cfsVars], " $1;$n", [s.loc.r]) addf(m.s[cfsVars], " $1;$n", [s.loc.r])
proc generateThreadLocalStorage(m: BModule) = proc generateThreadLocalStorage(m: BModule) =
if nimtv != nil and (m.usesThreadVars or sfMainModule in m.module.flags): if nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags):
for t in items(nimtvDeps): discard getTypeDesc(m, t) for t in items(nimtvDeps): discard getTypeDesc(m, t)
addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [nimtv]) addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [nimtv])

View file

@ -752,7 +752,7 @@ proc genProcHeader(m: BModule, prc: PSym): Rope =
genCLineDir(result, prc.info) genCLineDir(result, prc.info)
# using static is needed for inline procs # using static is needed for inline procs
if lfExportLib in prc.loc.flags: if lfExportLib in prc.loc.flags:
if m.isHeaderFile: if isHeaderFile in m.flags:
result.add "N_LIB_IMPORT " result.add "N_LIB_IMPORT "
else: else:
result.add "N_LIB_EXPORT " result.add "N_LIB_EXPORT "

View file

@ -64,8 +64,8 @@ proc isSimpleConst(typ: PType): bool =
(t.kind == tyProc and t.callConv == ccClosure) (t.kind == tyProc and t.callConv == ccClosure)
proc useStringh(m: BModule) = proc useStringh(m: BModule) =
if not m.includesStringh: if includesStringh notin m.flags:
m.includesStringh = true incl m.flags, includesStringh
discard lists.includeStr(m.headerFiles, "<string.h>") discard lists.includeStr(m.headerFiles, "<string.h>")
proc useHeader(m: BModule, sym: PSym) = proc useHeader(m: BModule, sym: PSym) =
@ -1011,11 +1011,11 @@ proc genInitCode(m: BModule) =
add(prc, m.postInitProc.s(cpsLocals)) add(prc, m.postInitProc.s(cpsLocals))
add(prc, genSectionEnd(cpsLocals)) add(prc, genSectionEnd(cpsLocals))
if optStackTrace in m.initProc.options and not m.frameDeclared: if optStackTrace in m.initProc.options and frameDeclared notin m.flags:
# BUT: the generated init code might depend on a current frame, so # BUT: the generated init code might depend on a current frame, so
# declare it nevertheless: # declare it nevertheless:
m.frameDeclared = true incl m.flags, frameDeclared
if not m.preventStackTrace: if preventStackTrace notin m.flags:
var procname = makeCString(m.module.name.s) var procname = makeCString(m.module.name.s)
add(prc, initFrame(m.initProc, procname, m.module.info.quotedFilename)) add(prc, initFrame(m.initProc, procname, m.module.info.quotedFilename))
else: else:
@ -1032,7 +1032,7 @@ proc genInitCode(m: BModule) =
add(prc, m.initProc.s(cpsStmts)) add(prc, m.initProc.s(cpsStmts))
add(prc, m.postInitProc.s(cpsStmts)) add(prc, m.postInitProc.s(cpsStmts))
add(prc, genSectionEnd(cpsStmts)) add(prc, genSectionEnd(cpsStmts))
if optStackTrace in m.initProc.options and not m.preventStackTrace: if optStackTrace in m.initProc.options and preventStackTrace notin m.flags:
add(prc, deinitFrame(m.initProc)) add(prc, deinitFrame(m.initProc))
add(prc, deinitGCFrame(m.initProc)) add(prc, deinitGCFrame(m.initProc))
addf(prc, "}$N$N", []) addf(prc, "}$N$N", [])
@ -1105,7 +1105,7 @@ proc rawNewModule(module: PSym, filename: string): BModule =
# no line tracing for the init sections of the system module so that we # no line tracing for the init sections of the system module so that we
# don't generate a TFrame which can confuse the stack botton initialization: # don't generate a TFrame which can confuse the stack botton initialization:
if sfSystemModule in module.flags: if sfSystemModule in module.flags:
result.preventStackTrace = true incl result.flags, preventStackTrace
excl(result.preInitProc.options, optStackTrace) excl(result.preInitProc.options, optStackTrace)
excl(result.postInitProc.options, optStackTrace) excl(result.postInitProc.options, optStackTrace)
@ -1128,9 +1128,11 @@ proc resetModule*(m: BModule) =
m.forwardedProcs = @[] m.forwardedProcs = @[]
m.typeNodesName = getTempName() m.typeNodesName = getTempName()
m.nimTypesName = getTempName() m.nimTypesName = getTempName()
m.preventStackTrace = sfSystemModule in m.module.flags if sfSystemModule in m.module.flags:
incl m.flags, preventStackTrace
else:
excl m.flags, preventStackTrace
nullify m.s nullify m.s
m.usesThreadVars = false
m.typeNodes = 0 m.typeNodes = 0
m.nimTypes = 0 m.nimTypes = 0
nullify m.extensionLoaders nullify m.extensionLoaders
@ -1175,7 +1177,7 @@ proc myOpen(module: PSym): PPassContext =
let f = if headerFile.len > 0: headerFile else: gProjectFull let f = if headerFile.len > 0: headerFile else: gProjectFull
generatedHeader = rawNewModule(module, generatedHeader = rawNewModule(module,
changeFileExt(completeCFilePath(f), hExt)) changeFileExt(completeCFilePath(f), hExt))
generatedHeader.isHeaderFile = true incl generatedHeader.flags, isHeaderFile
proc writeHeader(m: BModule) = proc writeHeader(m: BModule) =
var result = getCopyright(m.filename) var result = getCopyright(m.filename)
@ -1307,7 +1309,7 @@ proc myClose(b: PPassContext, n: PNode): PNode =
registerModuleToMain(m.module) registerModuleToMain(m.module)
if sfMainModule in m.module.flags: if sfMainModule in m.module.flags:
m.objHasKidsValid = true incl m.flags, objHasKidsValid
var disp = generateMethodDispatchers() var disp = generateMethodDispatchers()
for i in 0..sonsLen(disp)-1: genProcAux(m, disp.sons[i].sym) for i in 0..sonsLen(disp)-1: genProcAux(m, disp.sons[i].sym)
genMainProc(m) genMainProc(m)

View file

@ -92,17 +92,20 @@ type
gcFrameType*: Rope # the struct {} we put the GC markers into gcFrameType*: Rope # the struct {} we put the GC markers into
TTypeSeq* = seq[PType] TTypeSeq* = seq[PType]
Codegenflag* = enum
preventStackTrace, # true if stack traces need to be prevented
usesThreadVars, # true if the module uses a thread var
frameDeclared, # hack for ROD support so that we don't declare
# a frame var twice in an init proc
isHeaderFile, # C source file is the header file
includesStringh, # C source file already includes ``<string.h>``
objHasKidsValid # whether we can rely on tfObjHasKids
TCGen = object of TPassContext # represents a C source file TCGen = object of TPassContext # represents a C source file
module*: PSym module*: PSym
filename*: string filename*: string
s*: TCFileSections # sections of the C file s*: TCFileSections # sections of the C file
preventStackTrace*: bool # true if stack traces need to be prevented flags*: set[Codegenflag]
usesThreadVars*: bool # true if the module uses a thread var
frameDeclared*: bool # hack for ROD support so that we don't declare
# a frame var twice in an init proc
isHeaderFile*: bool # C source file is the header file
includesStringh*: bool # C source file already includes ``<string.h>``
objHasKidsValid*: bool # whether we can rely on tfObjHasKids
cfilename*: string # filename of the module (including path, cfilename*: string # filename of the module (including path,
# without extension) # without extension)
typeCache*: TIdTable # cache the generated types typeCache*: TIdTable # cache the generated types