compiler warns when you use GC'ed memory and '--gc:none'

This commit is contained in:
Araq
2014-01-19 20:24:44 +01:00
parent 73c6efdf66
commit 15859d94ec
5 changed files with 69 additions and 50 deletions

View File

@@ -847,6 +847,9 @@ proc genEcho(p: BProc, n: PNode) =
linefmt(p, cpsStmts, "printf($1$2);$n",
makeCString(repeatStr(n.len-1, "%s") & tnl), args)
proc gcUsage(n: PNode) =
if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree)
proc genStrConcat(p: BProc, e: PNode, d: var TLoc) =
# <Nimrod code>
# s = 'Hello ' & name & ', how do you feel?' & 'z'
@@ -888,6 +891,7 @@ proc genStrConcat(p: BProc, e: PNode, d: var TLoc) =
keepAlive(p, tmp)
else:
genAssignment(p, d, tmp, {needToKeepAlive}) # no need for deep copying
gcUsage(e)
proc genStrAppend(p: BProc, e: PNode, d: var TLoc) =
# <Nimrod code>
@@ -925,6 +929,7 @@ proc genStrAppend(p: BProc, e: PNode, d: var TLoc) =
rdLoc(dest), lens, toRope(L))
keepAlive(p, dest)
app(p.s(cpsStmts), appends)
gcUsage(e)
proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
# seq &= x -->
@@ -945,6 +950,7 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
initLoc(dest, locExpr, b.t, OnHeap)
dest.r = rfmt(nil, "$1->data[$1->$2-1]", rdLoc(a), lenField())
genAssignment(p, dest, b, {needToCopy, afDestIsNil})
gcUsage(e)
proc genReset(p: BProc, n: PNode) =
var a: TLoc
@@ -987,6 +993,7 @@ proc genNew(p: BProc, e: PNode) =
rawGenNew(p, a, se.rdLoc)
else:
rawGenNew(p, a, nil)
gcUsage(e)
proc genNewSeqAux(p: BProc, dest: TLoc, length: PRope) =
let seqtype = skipTypes(dest.t, abstractVarRange)
@@ -1010,6 +1017,7 @@ proc genNewSeq(p: BProc, e: PNode) =
initLocExpr(p, e.sons[1], a)
initLocExpr(p, e.sons[2], b)
genNewSeqAux(p, a, b.rdLoc)
gcUsage(e)
proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
var tmp: TLoc
@@ -1021,6 +1029,7 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
rawGenNew(p, tmp, nil)
t = t.sons[0].skipTypes(abstractInst)
r = ropef("(*$1)", r)
gcUsage(e)
discard getTypeDesc(p.module, t)
for i in 1 .. <e.len:
let it = e.sons[i]
@@ -1059,6 +1068,7 @@ proc genSeqConstr(p: BProc, t: PNode, d: var TLoc) =
arr.r = rfmt(nil, "$1->data[$2]", rdLoc(d), intLiteral(i))
arr.s = OnHeap # we know that sequences are on the heap
expr(p, t.sons[i], arr)
gcUsage(t)
proc genArrToSeq(p: BProc, t: PNode, d: var TLoc) =
var elem, a, arr: TLoc
@@ -1099,6 +1109,7 @@ proc genNewFinalize(p: BProc, e: PNode) =
genAssignment(p, a, b, {needToKeepAlive}) # set the object type:
bt = skipTypes(refType.sons[0], abstractRange)
genObjectInit(p, cpsStmts, bt, a, false)
gcUsage(e)
proc genOf(p: BProc, x: PNode, typ: PType, d: var TLoc) =
var a: TLoc
@@ -1176,6 +1187,7 @@ proc genRepr(p: BProc, e: PNode, d: var TLoc) =
else:
putIntoDest(p, d, e.typ, ropecg(p.module, "#reprAny($1, $2)",
[addrLoc(a), genTypeInfo(p.module, t)]))
gcUsage(e)
proc genGetTypeInfo(p: BProc, e: PNode, d: var TLoc) =
var t = skipTypes(e.sons[1].typ, abstractVarRange)
@@ -1187,6 +1199,7 @@ proc genDollar(p: BProc, n: PNode, d: var TLoc, frmt: string) =
a.r = ropecg(p.module, frmt, [rdLoc(a)])
if d.k == locNone: getTemp(p, n.typ, d)
genAssignment(p, d, a, {needToKeepAlive})
gcUsage(n)
proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
var a = e.sons[1]
@@ -1227,10 +1240,12 @@ proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) =
rdLoc(a), rdLoc(b), getTypeDesc(p.module, t),
getTypeDesc(p.module, t.sons[0])])
keepAlive(p, a)
gcUsage(e)
proc genSetLengthStr(p: BProc, e: PNode, d: var TLoc) =
binaryStmt(p, e, d, "$1 = #setLengthStr($1, $2);$n")
keepAlive(p, d)
gcUsage(e)
proc genSwap(p: BProc, e: PNode, d: var TLoc) =
# swap(a, b) -->
@@ -1462,6 +1477,7 @@ proc convCStrToStr(p: BProc, n: PNode, d: var TLoc) =
initLocExpr(p, n.sons[0], a)
putIntoDest(p, d, skipTypes(n.typ, abstractVar),
ropecg(p.module, "#cstrToNimstr($1)", [rdLoc(a)]))
gcUsage(n)
proc genStrEquals(p: BProc, e: PNode, d: var TLoc) =
var x: TLoc

View File

@@ -953,7 +953,7 @@ proc genAsgn(c: PCtx; le, ri: PNode; requiresCopy: bool) =
c.gABx(le, whichAsgnOpc(le, opcWrGlobal), tmp, s.position)
else:
internalAssert s.position > 0 or (s.position == 0 and
s.kind in {skParam, skResult})
s.kind in {skParam,skResult,skForVar})
var dest: TRegister = s.position + ord(s.kind == skParam)
gen(c, ri, dest)
else:
@@ -1014,7 +1014,8 @@ proc genRdVar(c: PCtx; n: PNode; dest: var TDest) =
else:
c.gABx(n, opcLdGlobal, dest, s.position)
else:
if s.position > 0 or (s.position == 0 and s.kind in {skParam, skResult}):
if s.position > 0 or (s.position == 0 and
s.kind in {skParam,skResult,skForVar}):
if dest < 0:
dest = s.position + ord(s.kind == skParam)
else:

View File

@@ -114,10 +114,10 @@ when defined(boehmgc):
proc alloc0(size: int): pointer =
result = alloc(size)
zeroMem(result, size)
proc realloc(p: Pointer, newsize: int): pointer =
proc realloc(p: pointer, newsize: int): pointer =
result = boehmRealloc(p, newsize)
if result == nil: raiseOutOfMem()
proc dealloc(p: Pointer) = boehmDealloc(p)
proc dealloc(p: pointer) = boehmDealloc(p)
proc allocShared(size: int): pointer =
result = boehmAlloc(size)
@@ -125,26 +125,26 @@ when defined(boehmgc):
proc allocShared0(size: int): pointer =
result = alloc(size)
zeroMem(result, size)
proc reallocShared(p: Pointer, newsize: int): pointer =
proc reallocShared(p: pointer, newsize: int): pointer =
result = boehmRealloc(p, newsize)
if result == nil: raiseOutOfMem()
proc deallocShared(p: Pointer) = boehmDealloc(p)
proc deallocShared(p: pointer) = boehmDealloc(p)
#boehmGCincremental()
proc GC_disable() = boehmGC_disable()
proc GC_enable() = boehmGC_enable()
proc GC_fullCollect() = boehmGCfullCollect()
proc GC_setStrategy(strategy: TGC_Strategy) = nil
proc GC_enableMarkAndSweep() = nil
proc GC_disableMarkAndSweep() = nil
proc GC_setStrategy(strategy: TGC_Strategy) = discard
proc GC_enableMarkAndSweep() = discard
proc GC_disableMarkAndSweep() = discard
proc GC_getStatistics(): string = return ""
proc getOccupiedMem(): int = return boehmGetHeapSize()-boehmGetFreeBytes()
proc getFreeMem(): int = return boehmGetFreeBytes()
proc getTotalMem(): int = return boehmGetHeapSize()
proc setStackBottom(theStackBottom: pointer) = nil
proc setStackBottom(theStackBottom: pointer) = discard
proc initGC() =
when defined(macosx): boehmGCinit()
@@ -160,8 +160,8 @@ when defined(boehmgc):
proc growObj(old: pointer, newsize: int): pointer =
result = realloc(old, newsize)
proc nimGCref(p: pointer) {.compilerproc, inline.} = nil
proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil
proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} =
dest[] = src
@@ -173,15 +173,15 @@ when defined(boehmgc):
type
TMemRegion = object {.final, pure.}
proc Alloc(r: var TMemRegion, size: int): pointer =
proc alloc(r: var TMemRegion, size: int): pointer =
result = boehmAlloc(size)
if result == nil: raiseOutOfMem()
proc Alloc0(r: var TMemRegion, size: int): pointer =
proc alloc0(r: var TMemRegion, size: int): pointer =
result = alloc(size)
zeroMem(result, size)
proc Dealloc(r: var TMemRegion, p: Pointer) = boehmDealloc(p)
proc deallocOsPages(r: var TMemRegion) {.inline.} = nil
proc deallocOsPages() {.inline.} = nil
proc dealloc(r: var TMemRegion, p: Pointer) = boehmDealloc(p)
proc deallocOsPages(r: var TMemRegion) {.inline.} = discard
proc deallocOsPages() {.inline.} = discard
include "system/cellsets"
elif defined(nogc) and defined(useMalloc):
@@ -193,10 +193,10 @@ elif defined(nogc) and defined(useMalloc):
proc alloc0(size: int): pointer =
result = alloc(size)
zeroMem(result, size)
proc realloc(p: Pointer, newsize: int): pointer =
proc realloc(p: pointer, newsize: int): pointer =
result = crealloc(p, newsize)
if result == nil: raiseOutOfMem()
proc dealloc(p: Pointer) = cfree(p)
proc dealloc(p: pointer) = cfree(p)
proc allocShared(size: int): pointer =
result = cmalloc(size)
@@ -204,26 +204,26 @@ elif defined(nogc) and defined(useMalloc):
proc allocShared0(size: int): pointer =
result = alloc(size)
zeroMem(result, size)
proc reallocShared(p: Pointer, newsize: int): pointer =
proc reallocShared(p: pointer, newsize: int): pointer =
result = crealloc(p, newsize)
if result == nil: raiseOutOfMem()
proc deallocShared(p: Pointer) = cfree(p)
proc deallocShared(p: pointer) = cfree(p)
proc GC_disable() = nil
proc GC_enable() = nil
proc GC_fullCollect() = nil
proc GC_setStrategy(strategy: TGC_Strategy) = nil
proc GC_enableMarkAndSweep() = nil
proc GC_disableMarkAndSweep() = nil
proc GC_disable() = discard
proc GC_enable() = discard
proc GC_fullCollect() = discard
proc GC_setStrategy(strategy: TGC_Strategy) = discard
proc GC_enableMarkAndSweep() = discard
proc GC_disableMarkAndSweep() = discard
proc GC_getStatistics(): string = return ""
proc getOccupiedMem(): int = nil
proc getFreeMem(): int = nil
proc getTotalMem(): int = nil
proc getOccupiedMem(): int = discard
proc getFreeMem(): int = discard
proc getTotalMem(): int = discard
proc setStackBottom(theStackBottom: pointer) = nil
proc setStackBottom(theStackBottom: pointer) = discard
proc initGC() = nil
proc initGC() = discard
proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
result = alloc(size)
@@ -235,8 +235,8 @@ elif defined(nogc) and defined(useMalloc):
proc growObj(old: pointer, newsize: int): pointer =
result = realloc(old, newsize)
proc nimGCref(p: pointer) {.compilerproc, inline.} = nil
proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil
proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} =
dest[] = src
@@ -248,13 +248,13 @@ elif defined(nogc) and defined(useMalloc):
type
TMemRegion = object {.final, pure.}
proc Alloc(r: var TMemRegion, size: int): pointer =
proc alloc(r: var TMemRegion, size: int): pointer =
result = alloc(size)
proc Alloc0(r: var TMemRegion, size: int): pointer =
proc alloc0(r: var TMemRegion, size: int): pointer =
result = alloc0(size)
proc Dealloc(r: var TMemRegion, p: Pointer) = Dealloc(p)
proc deallocOsPages(r: var TMemRegion) {.inline.} = nil
proc deallocOsPages() {.inline.} = nil
proc dealloc(r: var TMemRegion, p: pointer) = dealloc(p)
proc deallocOsPages(r: var TMemRegion) {.inline.} = discard
proc deallocOsPages() {.inline.} = discard
elif defined(nogc):
# Even though we don't want the GC, we cannot simply use C's memory manager
@@ -268,13 +268,13 @@ elif defined(nogc):
include "system/alloc"
proc initGC() = nil
proc GC_disable() = nil
proc GC_enable() = nil
proc GC_fullCollect() = nil
proc GC_setStrategy(strategy: TGC_Strategy) = nil
proc GC_enableMarkAndSweep() = nil
proc GC_disableMarkAndSweep() = nil
proc initGC() = discard
proc GC_disable() = discard
proc GC_enable() = discard
proc GC_fullCollect() = discard
proc GC_setStrategy(strategy: TGC_Strategy) = discard
proc GC_enableMarkAndSweep() = discard
proc GC_disableMarkAndSweep() = discard
proc GC_getStatistics(): string = return ""
@@ -287,9 +287,9 @@ elif defined(nogc):
proc growObj(old: pointer, newsize: int): pointer =
result = realloc(old, newsize)
proc setStackBottom(theStackBottom: pointer) = nil
proc nimGCref(p: pointer) {.compilerproc, inline.} = nil
proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil
proc setStackBottom(theStackBottom: pointer) = discard
proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} =
dest[] = src

View File

@@ -3,7 +3,6 @@ version 0.9.4
- test&finish first class iterators:
* nested iterators
- ``--gc:none`` should complain about when you use the GC
- ensure (ref T)(a, b) works as a type conversion and type constructor
- document new templating symbol binding rules
- make '--implicitStatic:on' the default

View File

@@ -38,6 +38,8 @@ Changes affecting backwards compatibility
- ``macros.dumpTree`` and ``macros.dumpLisp`` have been made ``immediate``,
``dumpTreeImm`` and ``dumpLispImm`` are now deprecated.
- The ``nil`` statement has been deprecated, use an empty ``discard`` instead.
- ``sockets.select`` now prunes sockets that are **not** ready from the list
of sockets given to it.
Compiler Additions
@@ -54,6 +56,7 @@ Compiler Additions
- The old evaluation engine has been replaced by a proper register based
virtual machine. This fixes numerous bugs for ``nimrod i`` and for macro
evaluation.
- ``--gc:none`` produces warnings when code uses the GC.
Language Additions