summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-11-25 15:33:58 +0100
committerAraq <rumpf_a@web.de>2011-11-25 15:33:58 +0100
commit2de98d9e0526ff33bc392152bc615f1abf34ebd0 (patch)
tree018d93c74f723ba4800f6c76d3bd33737d91bb59
parented9c7761c4e37ec22ebd81acf16e3137d064cfc9 (diff)
downloadNim-2de98d9e0526ff33bc392152bc615f1abf34ebd0.tar.gz
new pragma: 'noinit'
-rwxr-xr-xcompiler/ast.nim3
-rwxr-xr-xcompiler/cgen.nim15
-rwxr-xr-xcompiler/pragmas.nim18
-rwxr-xr-xcompiler/wordrecg.nim4
-rwxr-xr-xdoc/manual.txt9
-rwxr-xr-xweb/news.txt2
6 files changed, 34 insertions, 17 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 3fdf5f6e3..d80f2289e 100755
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -236,7 +236,8 @@ type
 
 const
   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
   TTypeKind* = enum  # order is important!
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 2b75edeff..ca58e4b1c 100755
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -74,9 +74,9 @@ proc fillLoc(a: var TLoc, k: TLocKind, typ: PType, r: PRope, s: TStorageLoc) =
     a.s = s
     if a.r == nil: a.r = r
   
-proc isSimpleConst(typ: PType): bool = 
-  result = not (skipTypes(typ, abstractVar).kind in
-      {tyTuple, tyObject, tyArray, tyArrayConstr, tySet, tySequence})
+proc isSimpleConst(typ: PType): bool =
+  result = skipTypes(typ, abstractVar).kind notin
+      {tyTuple, tyObject, tyArray, tyArrayConstr, tySet, tySequence}
 
 proc useHeader(m: BModule, sym: PSym) = 
   if lfHeader in sym.loc.Flags: 
@@ -263,10 +263,11 @@ proc zeroTemp(p: BProc, loc: TLoc) =
       appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n", 
            [addrLoc(loc), genTypeInfo(p.module, loc.t)])
 
-proc initVariable(p: BProc, v: PSym) = 
-  var b = containsGarbageCollectedRef(v.typ)
-  if b or v.ast == nil: 
-    zeroVar(p, v.loc, b)
+proc initVariable(p: BProc, v: PSym) =
+  if sfNoInit notin v.flags:
+    var b = containsGarbageCollectedRef(v.typ)
+    if b or v.ast == nil:
+      zeroVar(p, v.loc, b)
     
 proc initTemp(p: BProc, tmp: var TLoc) = 
   if containsGarbageCollectedRef(tmp.t) or isInvalidReturnType(tmp.t):
diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim
index 6c3a7ba67..5eb6263e9 100755
--- a/compiler/pragmas.nim
+++ b/compiler/pragmas.nim
@@ -32,12 +32,13 @@ const
   iteratorPragmas* = {FirstCallConv..LastCallConv, wNosideEffect, wSideEffect, 
     wImportc, wExportc, wNodecl, wMagic, wDeprecated, wBorrow, wExtern,
     wImportcpp, wImportobjc, wError, wDiscardable}
-  stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, 
-    wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, wLinedir, 
-    wStacktrace, wLinetrace, wOptimization, wHint, wWarning, wError, wFatal, 
-    wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop, wBreakpoint, 
-    wCheckpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated, wFloatChecks,
-    wInfChecks, wNanChecks, wPragma, wEmit, wUnroll, wLinearScanEnd}
+  stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks,
+    wBoundchecks, wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
+    wLinedir, wStacktrace, wLinetrace, wOptimization, wHint, wWarning, wError,
+    wFatal, wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop,
+    wBreakpoint, wCheckpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated,
+    wFloatChecks, wInfChecks, wNanChecks, wPragma, wEmit, wUnroll,
+    wLinearScanEnd}
   lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, 
     wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader, 
     wDeprecated, wExtern, wThread, wImportcpp, wImportobjc, wNoStackFrame}
@@ -48,7 +49,7 @@ const
     wImportcpp, wImportobjc, wError}
   varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, 
     wMagic, wHeader, wDeprecated, wCompilerProc, wDynLib, wExtern,
-    wImportcpp, wImportobjc, wError}
+    wImportcpp, wImportobjc, wError, wNoInit}
   constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl,
     wExtern, wImportcpp, wImportobjc, wError}
   procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNosideEffect,
@@ -556,6 +557,9 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
           of wDiscardable:
             noVal(it)
             if sym != nil: incl(sym.flags, sfDiscardable)
+          of wNoInit:
+            noVal(it)
+            if sym != nil: incl(sym.flags, sfNoInit)
           of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, 
              wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, 
              wLinedir, wStacktrace, wLinetrace, wOptimization, wByRef,
diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim
index 5f8a2b3fd..43b0f522c 100755
--- a/compiler/wordrecg.nim
+++ b/compiler/wordrecg.nim
@@ -50,7 +50,7 @@ type
     wHints, wOptimization, wSpeed, wSize, wNone, 
     wDeadCodeElim, wSafecode, 
     wPragma,
-    wCompileTime,
+    wCompileTime, wNoInit,
     wPassc, wPassl, wBorrow, wDiscardable,
     wFieldChecks, 
     wCheckPoint, wSubsChar, 
@@ -96,7 +96,7 @@ const
     "optimization", "speed", "size", "none", 
     "deadcodeelim", "safecode", 
     "pragma",
-    "compiletime", 
+    "compiletime", "noinit",
     "passc", "passl", "borrow", "discardable", "fieldchecks",
     "checkpoint",
     "subschar", "acyclic", "shallow", "unroll", "linearscanend",
diff --git a/doc/manual.txt b/doc/manual.txt
index 6d8286dd7..c2ae2c757 100755
--- a/doc/manual.txt
+++ b/doc/manual.txt
@@ -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

 ~~~~~~~~~~~~~

 

@@ -1781,6 +1789,7 @@ sugar for:
   result = expr

   return result

 

+

 ``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

 value of the procedure. It is automatically declared by the compiler. As all

diff --git a/web/news.txt b/web/news.txt
index b9442d098..cfc6fcb15 100755
--- a/web/news.txt
+++ b/web/news.txt
@@ -65,6 +65,8 @@ Language Additions
   a compile-time error.
 - There is a new ``discardable`` pragma that can be used to mark a routine 
   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``.
 - 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