progress on 'spawn'

This commit is contained in:
Araq 2014-08-10 03:17:30 +02:00
commit 76011e40ef
5 changed files with 33 additions and 10 deletions

View file

@ -185,14 +185,14 @@ proc callProc(a: PNode): PNode =
# - a proc returning non GC'ed memory --> pass as hidden 'var' parameter # - a proc returning non GC'ed memory --> pass as hidden 'var' parameter
# - not in a parallel environment --> requires a flowVar for memory safety # - not in a parallel environment --> requires a flowVar for memory safety
type type
TSpawnResult = enum TSpawnResult* = enum
srVoid, srFlowVar, srByVar srVoid, srFlowVar, srByVar
TFlowVarKind = enum TFlowVarKind = enum
fvInvalid # invalid type T for 'FlowVar[T]' fvInvalid # invalid type T for 'FlowVar[T]'
fvGC # FlowVar of a GC'ed type fvGC # FlowVar of a GC'ed type
fvBlob # FlowVar of a blob type fvBlob # FlowVar of a blob type
proc spawnResult(t: PType; inParallel: bool): TSpawnResult = proc spawnResult*(t: PType; inParallel: bool): TSpawnResult =
if t.isEmptyType: srVoid if t.isEmptyType: srVoid
elif inParallel and not containsGarbageCollectedRef(t): srByVar elif inParallel and not containsGarbageCollectedRef(t): srByVar
else: srFlowVar else: srFlowVar

View file

@ -16,7 +16,7 @@ import
procfind, lookups, rodread, pragmas, passes, semdata, semtypinst, sigmatch, procfind, lookups, rodread, pragmas, passes, semdata, semtypinst, sigmatch,
intsets, transf, vmdef, vm, idgen, aliases, cgmeth, lambdalifting, intsets, transf, vmdef, vm, idgen, aliases, cgmeth, lambdalifting,
evaltempl, patterns, parampatterns, sempass2, pretty, semmacrosanity, evaltempl, patterns, parampatterns, sempass2, pretty, semmacrosanity,
semparallel semparallel, lowerings
# implementation # implementation

View file

@ -1643,10 +1643,10 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result = setMs(n, s) result = setMs(n, s)
result.sons[1] = semExpr(c, n.sons[1]) result.sons[1] = semExpr(c, n.sons[1])
if not result[1].typ.isEmptyType: if not result[1].typ.isEmptyType:
if c.inParallelStmt > 0: if spawnResult(result[1].typ, c.inParallelStmt > 0) == srFlowVar:
result.typ = result[1].typ
else:
result.typ = createFlowVar(c, result[1].typ, n.info) result.typ = createFlowVar(c, result[1].typ, n.info)
else:
result.typ = result[1].typ
result.add instantiateCreateFlowVarCall(c, result[1].typ, n.info).newSymNode result.add instantiateCreateFlowVarCall(c, result[1].typ, n.info).newSymNode
else: result = semDirectOp(c, n, flags) else: result = semDirectOp(c, n, flags)

View file

@ -23,7 +23,7 @@
import import
ast, astalgo, idents, lowerings, magicsys, guards, sempass2, msgs, ast, astalgo, idents, lowerings, magicsys, guards, sempass2, msgs,
renderer renderer, types
from trees import getMagic from trees import getMagic
from strutils import `%` from strutils import `%`
@ -406,12 +406,17 @@ proc transformSpawn(owner: PSym; n, barrier: PNode): PNode =
if result.isNil: if result.isNil:
result = newNodeI(nkStmtList, n.info) result = newNodeI(nkStmtList, n.info)
result.add n result.add n
let t = b[1][0].typ.sons[0]
if spawnResult(t, true) == srByVar:
result.add wrapProcForSpawn(owner, m, b.typ, barrier, it[0]) result.add wrapProcForSpawn(owner, m, b.typ, barrier, it[0])
it.sons[it.len-1] = emptyNode it.sons[it.len-1] = emptyNode
else:
it.sons[it.len-1] = wrapProcForSpawn(owner, m, b.typ, barrier, nil)
if result.isNil: result = n if result.isNil: result = n
of nkAsgn, nkFastAsgn: of nkAsgn, nkFastAsgn:
let b = n[1] let b = n[1]
if getMagic(b) == mSpawn: if getMagic(b) == mSpawn and (let t = b[1][0].typ.sons[0];
spawnResult(t, true) == srByVar):
let m = transformSlices(b) let m = transformSlices(b)
return wrapProcForSpawn(owner, m, b.typ, barrier, n[0]) return wrapProcForSpawn(owner, m, b.typ, barrier, n[0])
result = transformSpawnSons(owner, n, barrier) result = transformSpawnSons(owner, n, barrier)

View file

@ -0,0 +1,18 @@
discard """
output: '''13 abc'''
"""
type
PBinaryTree = ref object
le, ri: PBinaryTree
value: int
proc main =
var x: PBinaryTree
deepCopy(x, PBinaryTree(ri: PBinaryTree(le: PBinaryTree(value: 13))))
var y: string
deepCopy y, "abc"
echo x.ri.le.value, " ", y
main()