new pragma: 'noinit'

This commit is contained in:
Araq 2011-11-25 15:33:58 +01:00
commit 2de98d9e05
6 changed files with 34 additions and 17 deletions

View file

@ -236,7 +236,8 @@ type
const const
sfFakeConst* = sfDeadCodeElim # const cannot be put into a data section sfFakeConst* = sfDeadCodeElim # const cannot be put into a data section
sfDispatcher* = sfDeadCodeElim # copied method symbol is the dispatcher sfDispatcher* = sfDeadCodeElim # copied method symbol is the dispatcher
sfNoInit* = sfMainModule # don't generate code to init the variable
type type
TTypeKind* = enum # order is important! TTypeKind* = enum # order is important!

View file

@ -74,9 +74,9 @@ proc fillLoc(a: var TLoc, k: TLocKind, typ: PType, r: PRope, s: TStorageLoc) =
a.s = s a.s = s
if a.r == nil: a.r = r if a.r == nil: a.r = r
proc isSimpleConst(typ: PType): bool = proc isSimpleConst(typ: PType): bool =
result = not (skipTypes(typ, abstractVar).kind in result = skipTypes(typ, abstractVar).kind notin
{tyTuple, tyObject, tyArray, tyArrayConstr, tySet, tySequence}) {tyTuple, tyObject, tyArray, tyArrayConstr, tySet, tySequence}
proc useHeader(m: BModule, sym: PSym) = proc useHeader(m: BModule, sym: PSym) =
if lfHeader in sym.loc.Flags: if lfHeader in sym.loc.Flags:
@ -263,10 +263,11 @@ proc zeroTemp(p: BProc, loc: TLoc) =
appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n", appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
[addrLoc(loc), genTypeInfo(p.module, loc.t)]) [addrLoc(loc), genTypeInfo(p.module, loc.t)])
proc initVariable(p: BProc, v: PSym) = proc initVariable(p: BProc, v: PSym) =
var b = containsGarbageCollectedRef(v.typ) if sfNoInit notin v.flags:
if b or v.ast == nil: var b = containsGarbageCollectedRef(v.typ)
zeroVar(p, v.loc, b) if b or v.ast == nil:
zeroVar(p, v.loc, b)
proc initTemp(p: BProc, tmp: var TLoc) = proc initTemp(p: BProc, tmp: var TLoc) =
if containsGarbageCollectedRef(tmp.t) or isInvalidReturnType(tmp.t): if containsGarbageCollectedRef(tmp.t) or isInvalidReturnType(tmp.t):

View file

@ -32,12 +32,13 @@ const
iteratorPragmas* = {FirstCallConv..LastCallConv, wNosideEffect, wSideEffect, iteratorPragmas* = {FirstCallConv..LastCallConv, wNosideEffect, wSideEffect,
wImportc, wExportc, wNodecl, wMagic, wDeprecated, wBorrow, wExtern, wImportc, wExportc, wNodecl, wMagic, wDeprecated, wBorrow, wExtern,
wImportcpp, wImportobjc, wError, wDiscardable} wImportcpp, wImportobjc, wError, wDiscardable}
stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, wLinedir, wBoundchecks, wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wStacktrace, wLinetrace, wOptimization, wHint, wWarning, wError, wFatal, wLinedir, wStacktrace, wLinetrace, wOptimization, wHint, wWarning, wError,
wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop, wBreakpoint, wFatal, wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop,
wCheckpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated, wFloatChecks, wBreakpoint, wCheckpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated,
wInfChecks, wNanChecks, wPragma, wEmit, wUnroll, wLinearScanEnd} wFloatChecks, wInfChecks, wNanChecks, wPragma, wEmit, wUnroll,
wLinearScanEnd}
lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl,
wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader, wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader,
wDeprecated, wExtern, wThread, wImportcpp, wImportobjc, wNoStackFrame} wDeprecated, wExtern, wThread, wImportcpp, wImportobjc, wNoStackFrame}
@ -48,7 +49,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} wImportcpp, wImportobjc, wError, wNoInit}
constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl, constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl,
wExtern, wImportcpp, wImportobjc, wError} wExtern, wImportcpp, wImportobjc, wError}
procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNosideEffect, procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNosideEffect,
@ -556,6 +557,9 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
of wDiscardable: of wDiscardable:
noVal(it) noVal(it)
if sym != nil: incl(sym.flags, sfDiscardable) if sym != nil: incl(sym.flags, sfDiscardable)
of wNoInit:
noVal(it)
if sym != nil: incl(sym.flags, sfNoInit)
of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wLinedir, wStacktrace, wLinetrace, wOptimization, wByRef, wLinedir, wStacktrace, wLinetrace, wOptimization, wByRef,

View file

@ -50,7 +50,7 @@ type
wHints, wOptimization, wSpeed, wSize, wNone, wHints, wOptimization, wSpeed, wSize, wNone,
wDeadCodeElim, wSafecode, wDeadCodeElim, wSafecode,
wPragma, wPragma,
wCompileTime, wCompileTime, wNoInit,
wPassc, wPassl, wBorrow, wDiscardable, wPassc, wPassl, wBorrow, wDiscardable,
wFieldChecks, wFieldChecks,
wCheckPoint, wSubsChar, wCheckPoint, wSubsChar,
@ -96,7 +96,7 @@ const
"optimization", "speed", "size", "none", "optimization", "speed", "size", "none",
"deadcodeelim", "safecode", "deadcodeelim", "safecode",
"pragma", "pragma",
"compiletime", "compiletime", "noinit",
"passc", "passl", "borrow", "discardable", "fieldchecks", "passc", "passl", "borrow", "discardable", "fieldchecks",
"checkpoint", "checkpoint",
"subschar", "acyclic", "shallow", "unroll", "linearscanend", "subschar", "acyclic", "shallow", "unroll", "linearscanend",

View file

@ -1526,6 +1526,14 @@ T = enum cast[T](0); this may be an invalid value
============================ ============================================== ============================ ==============================================
The implicit initialization can be avoided for optimization reasons with the
`noinit`:idx: pragma:
.. code-block:: nimrod
var
a {.noInit.}: array [0..1023, char]
Const section Const section
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
@ -1781,6 +1789,7 @@ sugar for:
result = expr result = expr
return result return result
``return`` without an expression is a short notation for ``return result`` if ``return`` without an expression is a short notation for ``return result`` if
the proc has a return type. The `result`:idx: variable is always the return the proc has a return type. The `result`:idx: variable is always the return
value of the procedure. It is automatically declared by the compiler. As all value of the procedure. It is automatically declared by the compiler. As all

View file

@ -65,6 +65,8 @@ Language Additions
a compile-time error. a compile-time error.
- There is a new ``discardable`` pragma that can be used to mark a routine - There is a new ``discardable`` pragma that can be used to mark a routine
so that its result can be discarded implicitely. so that its result can be discarded implicitely.
- Added a new ``noinit`` pragma to prevent automatic initialization to zero
of variables.
- Constants can now have the type ``seq``. - Constants can now have the type ``seq``.
- There is a new user-definable syntactic construct ``a{i, ...}`` - There is a new user-definable syntactic construct ``a{i, ...}``
that has no semantics yet for built-in types and so can be overloaded to your that has no semantics yet for built-in types and so can be overloaded to your