summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-05-30 13:15:54 +0200
committerAraq <rumpf_a@web.de>2014-05-30 13:15:54 +0200
commitea16aca09ec47e3d4393437dea4f398922acaba0 (patch)
tree4c53b3178710a6afd6ad3a533f136116de1afcfd /compiler
parent6470bd8f87b860c555556a2a965f6c8077e993ad (diff)
downloadNim-ea16aca09ec47e3d4393437dea4f398922acaba0.tar.gz
correct code generation for tforstmt
Diffstat (limited to 'compiler')
-rw-r--r--compiler/lowerings.nim54
1 files changed, 34 insertions, 20 deletions
diff --git a/compiler/lowerings.nim b/compiler/lowerings.nim
index 13d4bf60e..5636d423f 100644
--- a/compiler/lowerings.nim
+++ b/compiler/lowerings.nim
@@ -160,8 +160,8 @@ We generate roughly this:
 
 proc f_wrapper(args) =
   barrierEnter(args.barrier)  # for parallel statement
-  var a = args.a # copy strings/seqs; thread transfer; not generated for
-                 # the 'parallel' statement
+  var a = args.a # thread transfer; deepCopy or shallowCopy or no copy
+                 # depending on whether we're in a 'parallel' statement
   var b = args.b
 
   args.prom = nimCreatePromise(thread, sizeof(T)) # optional
@@ -199,9 +199,9 @@ proc createNimCreatePromiseCall(prom, threadParam: PNode): PNode =
 proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
                        varSection, call, barrier, prom: PNode): PSym =
   var body = newNodeI(nkStmtList, f.info)
-  body.add varSection
   if barrier != nil:
     body.add callCodeGenProc("barrierEnter", barrier)
+  body.add varSection
   if prom != nil:
     body.add createNimCreatePromiseCall(prom, threadParam.newSymNode)
     if barrier == nil:
@@ -248,6 +248,17 @@ proc createCastExpr(argsParam: PSym; objType: PType): PNode =
   result.typ = newType(tyPtr, objType.owner)
   result.typ.rawAddSon(objType)
 
+proc addLocalVar(varSection: PNode; owner: PSym; typ: PType; v: PNode): PSym =
+  result = newSym(skTemp, getIdent(genPrefix), owner, varSection.info)
+  result.typ = typ
+  incl(result.flags, sfFromGeneric)
+
+  var vpart = newNodeI(nkIdentDefs, varSection.info, 3)
+  vpart.sons[0] = newSymNode(result)
+  vpart.sons[1] = ast.emptyNode
+  vpart.sons[2] = v
+  varSection.add vpart
+
 proc setupArgsForConcurrency(n: PNode; objType: PType; scratchObj: PSym, 
                              castExpr, call, varSection, result: PNode) =
   let formals = n[0].typ.n
@@ -267,16 +278,8 @@ proc setupArgsForConcurrency(n: PNode; objType: PType; scratchObj: PSym,
     objType.addField(field)
     result.add newFastAsgnStmt(newDotExpr(scratchObj, field), n[i])
 
-    var temp = newSym(skTemp, tmpName, objType.owner, n.info)
-    temp.typ = argType
-    incl(temp.flags, sfFromGeneric)
-
-    var vpart = newNodeI(nkIdentDefs, n.info, 3)
-    vpart.sons[0] = newSymNode(temp)
-    vpart.sons[1] = ast.emptyNode
-    vpart.sons[2] = indirectAccess(castExpr, field, n.info)
-    varSection.add vpart
-    
+    let temp = addLocalVar(varSection, objType.owner, argType,
+                           indirectAccess(castExpr, field, n.info))    
     call.add(newSymNode(temp))
 
 proc getRoot*(n: PNode): PSym =
@@ -310,9 +313,11 @@ proc genHigh(n: PNode): PNode =
     result.sons[1] = n
 
 proc setupArgsForParallelism(n: PNode; objType: PType; scratchObj: PSym;
-                             castExpr, call, result: PNode) =
+                             castExpr, call, varSection, result: PNode) =
   let formals = n[0].typ.n
   let tmpName = getIdent(genPrefix)
+  # we need to copy the foreign scratch object fields into local variables
+  # for correctness: These are called 'threadLocal' here.
   for i in 1 .. <n.len:
     let n = n[i]
     let argType = skipTypes(if i < formals.len: formals[i].typ else: n.typ,
@@ -344,7 +349,9 @@ proc setupArgsForParallelism(n: PNode; objType: PType; scratchObj: PSym;
         result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldA), n[2])
         result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldB), n[3])
 
-        slice.sons[2] = indirectAccess(castExpr, fieldA, n.info)
+        let threadLocal = addLocalVar(varSection, objType.owner, fieldA.typ,
+                                      indirectAccess(castExpr, fieldA, n.info))
+        slice.sons[2] = threadLocal.newSymNode
       else:
         let a = genAddrOf(n)
         field.typ = a.typ
@@ -353,9 +360,12 @@ proc setupArgsForParallelism(n: PNode; objType: PType; scratchObj: PSym;
         result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldB), genHigh(n))
 
         slice.sons[2] = newIntLit(0)
-        
+      # the array itself does not need to go through a thread local variable:
       slice.sons[1] = genDeref(indirectAccess(castExpr, field, n.info))
-      slice.sons[3] = indirectAccess(castExpr, fieldB, n.info)
+
+      let threadLocal = addLocalVar(varSection, objType.owner, fieldB.typ,
+                                    indirectAccess(castExpr, fieldB, n.info))
+      slice.sons[3] = threadLocal.newSymNode
       call.add slice
     elif (let size = computeSize(argType); size < 0 or size > 16) and
         n.getRoot != nil:
@@ -364,13 +374,17 @@ proc setupArgsForParallelism(n: PNode; objType: PType; scratchObj: PSym;
       field.typ = a.typ
       objType.addField(field)
       result.add newFastAsgnStmt(newDotExpr(scratchObj, field), a)
-      call.add(genDeref(indirectAccess(castExpr, field, n.info)))
+      let threadLocal = addLocalVar(varSection, objType.owner, field.typ,
+                                    indirectAccess(castExpr, field, n.info))
+      call.add(genDeref(threadLocal.newSymNode))
     else:
       # boring case
       field.typ = argType
       objType.addField(field)
       result.add newFastAsgnStmt(newDotExpr(scratchObj, field), n)
-      call.add(indirectAccess(castExpr, field, n.info))
+      let threadLocal = addLocalVar(varSection, objType.owner, field.typ,
+                                    indirectAccess(castExpr, field, n.info))
+      call.add(threadLocal.newSymNode)
 
 proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType; 
                        barrier, dest: PNode = nil): PNode =
@@ -438,7 +452,7 @@ proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType;
   if barrier.isNil:
     setupArgsForConcurrency(n, objType, scratchObj, castExpr, call, varSection, result)
   else: 
-    setupArgsForParallelism(n, objType, scratchObj, castExpr, call, result)
+    setupArgsForParallelism(n, objType, scratchObj, castExpr, call, varSection, result)
 
   var barrierAsExpr: PNode = nil
   if barrier != nil:
ss='alt'>
ee9a9237 ^
33352536 ^




6030d7e2 ^





33352536 ^
9d27e966 ^
33352536 ^

6030d7e2 ^

33352536 ^



6030d7e2 ^
33352536 ^
261b1b80 ^
33352536 ^

261b1b80 ^
33352536 ^




6030d7e2 ^
33352536 ^
cf02c20b ^
ee9a9237 ^
33352536 ^

7a583220 ^
33352536 ^

6030d7e2 ^
f3612481 ^
71eb22a5 ^
7a583220 ^
33352536 ^

80f53f4a ^


1639687b ^
33352536 ^
80f53f4a ^
261b1b80 ^
6030d7e2 ^
33352536 ^


1639687b ^
33352536 ^
80f53f4a ^








6030d7e2 ^
f3612481 ^

7a583220 ^
33352536 ^
6030d7e2 ^
33352536 ^
6030d7e2 ^
33352536 ^


6030d7e2 ^


71eb22a5 ^
64e8faba ^

33352536 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
9d27e966 ^
ee9a9237 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
33352536 ^
93be389b ^
33352536 ^
6030d7e2 ^
328a8e11 ^
ee9a9237 ^
6030d7e2 ^

9d27e966 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
7a583220 ^
33352536 ^


6030d7e2 ^
f3612481 ^
71eb22a5 ^
7a583220 ^
33352536 ^

6030d7e2 ^
ee9a9237 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
cf02c20b ^
ee9a9237 ^
33352536 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^
7a583220 ^
33352536 ^

6030d7e2 ^
f3612481 ^
71eb22a5 ^
7a583220 ^
33352536 ^

9d27e966 ^
ee9a9237 ^
6030d7e2 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
cf02c20b ^
7a583220 ^
33352536 ^

6030d7e2 ^
6ee77ba7 ^
ee9a9237 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214