drops and reorders
[nedit-bw.git] / aryiter-on-stack.patch
blob6498872d5987820e4af70db3654733c5082e48a7
1 ---
3 source/interpret.c | 181 +++++++++++++++++------------------------------------
4 source/interpret.h | 1
5 source/ops.h | 8 +-
6 source/parse.y | 40 ++++-------
7 4 files changed, 81 insertions(+), 149 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -835,24 +835,6 @@ Symbol *InstallMultiAssignExpr(void)
16 -** install an array iteration symbol
17 -** it is tagged as an integer but holds an array node pointer
18 -*/
19 -#define ARRAY_ITER_SYM_PREFIX "aryiter #"
20 -Symbol *InstallIteratorSymbol(void)
22 - char symbolName[sizeof(ARRAY_ITER_SYM_PREFIX) + TYPE_INT_STR_SIZE(int)];
23 - DataValue value;
24 - static int interatorNameIndex;
26 - sprintf(symbolName, ARRAY_ITER_SYM_PREFIX "%d", interatorNameIndex);
27 - ++interatorNameIndex;
28 - value.tag = INT_TAG;
29 - value.val.arrayPtr = NULL;
30 - return(InstallSymbol(symbolName, LOCAL_SYM, value));
33 -/*
34 ** Lookup a constant string by its value. This allows reuse of string
35 ** constants and fixing a leak in the interpreter.
37 @@ -3562,10 +3544,10 @@ static int arrayRefAndAssignSetup(void)
39 ** setup symbol values for array iteration in interpreter
41 -** Before: Prog-> [iter], ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
42 +** Before: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
43 ** TheStack-> [arrayVal], next, ...
44 -** After: Prog-> iter, [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
45 -** TheStack-> [next], ...
46 +** After: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
47 +** TheStack-> [iter], next, ...
48 ** Where:
49 ** iter is a symbol which gives the position of the iterator value in
50 ** the stack frame
51 @@ -3573,30 +3555,24 @@ static int arrayRefAndAssignSetup(void)
53 static int beginArrayIter(void)
55 - Symbol *iterator;
56 - DataValue *iteratorValPtr;
57 + DataValue iterator;
58 DataValue arrayVal;
60 DISASM_RT();
61 STACKDUMP(1, 3);
63 - GET_SYM(iterator);
65 POP(arrayVal);
67 - if (iterator->type == LOCAL_SYM) {
68 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
69 - }
70 - else {
71 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
72 - }
74 - iteratorValPtr->tag = INT_TAG;
75 if (arrayVal.tag != ARRAY_TAG) {
76 EXEC_ERROR("can't iterate non-array", NULL);
79 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
80 + /* maybe a new ARYITER_TAG */
81 + iterator.tag = NO_TAG;
82 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
84 + PUSH(iterator);
86 return STAT_OK;
89 @@ -3606,10 +3582,10 @@ static int beginArrayIter(void)
90 ** this allows iterators to progress even if you delete any node in the array
91 ** except the item just after the current key
93 -** Before: Prog-> iter, ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
94 -** TheStack-> [next], ...
95 -** After: Prog-> iter, ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, [next], ...
96 -** TheStack-> [next], ... (unchanged)
97 +** Before: Prog-> ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), endLoopBranch, next, ...
98 +** TheStack-> [iter], ...
99 +** After: Prog-> ARRAY_ITER, withVal, iterVarKey(, iterVarVal), endLoopBranch, [next], ...
100 +** TheStack-> [iter], ... (unchanged)
101 ** Where:
102 ** iter is a symbol which gives the position of the iterator value in
103 ** the stack frame (set up by OP_BEGIN_ARRAY_ITER); that value refers
104 @@ -3626,10 +3602,9 @@ static int beginArrayIter(void)
106 static int arrayIter(void)
108 - Symbol *iterator;
109 Symbol *keySym;
110 Symbol *valSym;
111 - DataValue *iteratorValPtr;
112 + DataValue iterator;
113 DataValue *keyValPtr;
114 DataValue *valPtr;
115 SparseArrayEntry *thisEntry;
116 @@ -3637,16 +3612,17 @@ static int arrayIter(void)
117 int withVal;
119 DISASM_RT();
120 - STACKDUMP(0, 4);
121 + STACKDUMP(1, 4);
123 GET_IMMED(withVal);
124 GET_SYM(keySym);
125 if (withVal) {
126 GET_SYM(valSym);
128 - GET_SYM(iterator);
129 GET_BRANCH(branchAddr);
131 + POP(iterator);
133 if (keySym->type == LOCAL_SYM) {
134 keyValPtr = &FP_GET_SYM_VAL(FrameP, keySym);
136 @@ -3671,14 +3647,7 @@ static int arrayIter(void)
137 valPtr->tag = NO_TAG;
140 - if (iterator->type == LOCAL_SYM) {
141 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
143 - else {
144 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
147 - thisEntry = iteratorValPtr->val.arrayPtr;
148 + thisEntry = iterator.val.arrayPtr;
149 if (thisEntry && thisEntry->nodePtrs.color != -1) {
150 /* set key */
151 keyValPtr->tag = STRING_TAG;
152 @@ -3691,52 +3660,48 @@ static int arrayIter(void)
155 /* advance iterator */
156 - iteratorValPtr->val.arrayPtr = arrayIterateNext(thisEntry);
157 + iterator.val.arrayPtr = arrayIterateNext(thisEntry);
159 else {
160 JUMP(branchAddr);
163 + PUSH(iterator);
165 return STAT_OK;
169 -** Before: Prog-> [iter], ARRAY_ITER_ARRAY, withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
170 +** Before: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
171 ** TheStack-> [arrayVal], nDims, next, ...
172 -** After: Prog-> iter, [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
173 -** TheStack-> [nDims], next, ...
174 +** After: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
175 +** TheStack-> [iter], nDims, next, ...
177 static int beginArrayIterArray(void)
179 - Symbol *iterator;
180 - DataValue *iteratorValPtr;
181 + DataValue iterator;
182 DataValue arrayVal;
183 int nDims;
185 DISASM_RT();
186 STACKDUMP(2, 3);
188 - GET_SYM(iterator);
190 POP(arrayVal);
191 PEEK_INT(nDims, 0);
193 - if (iterator->type == LOCAL_SYM) {
194 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
196 - else {
197 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
200 if (nDims < 0) {
201 EXEC_ERROR("bad multi dimension", NULL);
204 - iteratorValPtr->tag = INT_TAG;
205 if (arrayVal.tag != ARRAY_TAG) {
206 EXEC_ERROR("can't iterate non-array", NULL);
209 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
210 + /* maybe a new ARYITER_TAG */
211 + iterator.tag = NO_TAG;
212 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
214 + PUSH(iterator);
216 return STAT_OK;
218 @@ -3795,17 +3760,16 @@ static Boolean splitKeyIntoArray(const c
222 -** Before: Prog-> iter, ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), iter, endLoopBranch, next, ...
223 -** TheStack-> [nDims], next, ...
224 -** After: Prog-> iter, ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), iter, endLoopBranch, [next], ...
225 -** TheStack-> [nDims], next, ... (unchanged)
226 +** Before: Prog-> ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), endLoopBranch, next, ...
227 +** TheStack-> [iter], nDims, next, ...
228 +** After: Prog-> ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), endLoopBranch, [next], ...
229 +** TheStack-> [iter], nDims, next, ... (unchanged)
231 static int arrayIterArray(void)
233 - Symbol *iterator;
234 Symbol *keyArraySym;
235 Symbol *valSym;
236 - DataValue *iteratorValPtr;
237 + DataValue iterator;
238 DataValue *keyArrayPtr;
239 DataValue *valPtr;
240 SparseArrayEntry *thisEntry;
241 @@ -3815,16 +3779,16 @@ static int arrayIterArray(void)
242 Boolean keyFound = False;
244 DISASM_RT();
245 - STACKDUMP(1, 4);
246 + STACKDUMP(2, 4);
248 GET_IMMED(withVal);
249 GET_SYM(keyArraySym);
250 if (withVal) {
251 GET_SYM(valSym);
253 - GET_SYM(iterator);
254 GET_BRANCH(branchAddr);
256 + POP(iterator);
257 PEEK_INT(nDims, 0);
259 if (keyArraySym->type == LOCAL_SYM) {
260 @@ -3852,14 +3816,7 @@ static int arrayIterArray(void)
261 valPtr->tag = NO_TAG;
264 - if (iterator->type == LOCAL_SYM) {
265 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
267 - else {
268 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
271 - thisEntry = iteratorValPtr->val.arrayPtr;
272 + thisEntry = iterator.val.arrayPtr;
273 while (thisEntry && thisEntry->nodePtrs.color != -1) {
275 /* check if this is an nDims key, but only if requested */
276 @@ -3889,12 +3846,14 @@ static int arrayIterArray(void)
277 thisEntry = arrayIterateNext(thisEntry);
278 break;
280 - iteratorValPtr->val.arrayPtr = thisEntry;
281 + iterator.val.arrayPtr = thisEntry;
283 if (!keyFound && (!thisEntry || thisEntry->nodePtrs.color == -1)) {
284 JUMP(branchAddr);
287 + PUSH(iterator);
289 return STAT_OK;
292 @@ -4593,39 +4552,26 @@ static void disasmInternal(Inst *inst, i
294 break;
296 - case OP_BEGIN_ARRAY_ITER:
297 - case OP_BEGIN_ARRAY_ITER_ARRAY:
298 - CHECK_OPERANDS(1, SYM_INST);
299 - printd(" in");
300 - dumpInst(&inst[i+1], "aryIter");
301 - ++i;
302 - break;
304 case OP_ARRAY_ITER:
305 CHECK_OPERANDS(1, IMMED_INST);
306 if (!inst[i+1].val.immed) {
307 /* without val */
308 - CHECK_OPERANDS(4, IMMED_INST,
309 - SYM_INST, SYM_INST, BRANCH_INST);
310 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
311 dumpInst(&inst[i+2], "key");
312 - printd(" :=");
313 - dumpInst(&inst[i+3], "aryIter");
314 - printd("++");
315 - dumpInst(&inst[i+4], "end-loop");
316 - i += 4;
317 + printd(" := aryIter++");
318 + dumpInst(&inst[i+3], "end-loop");
319 + i += 3;
321 else {
322 /* with val */
323 - CHECK_OPERANDS(5, IMMED_INST,
324 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
325 + CHECK_OPERANDS(4, IMMED_INST,
326 + SYM_INST, SYM_INST, BRANCH_INST);
327 dumpInst(&inst[i+2], "key");
328 printd(" =");
329 dumpInst(&inst[i+3], "val");
330 - printd(" :=");
331 - dumpInst(&inst[i+4], "aryIter");
332 - printd("++");
333 - dumpInst(&inst[i+5], "end-loop");
334 - i += 5;
335 + printd(" := aryIter++");
336 + dumpInst(&inst[i+4], "end-loop");
337 + i += 4;
339 break;
341 @@ -4633,27 +4579,22 @@ static void disasmInternal(Inst *inst, i
342 CHECK_OPERANDS(1, IMMED_INST);
343 if (!inst[i+1].val.immed) {
344 /* without val */
345 - CHECK_OPERANDS(4, IMMED_INST,
346 - SYM_INST, SYM_INST, BRANCH_INST);
347 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
348 dumpInst(&inst[i+2], "keyArr");
349 - printd(" :=");
350 - dumpInst(&inst[i+3], "aryIter");
351 - printd("++");
352 - dumpInst(&inst[i+4], "end-loop");
353 - i += 4;
354 + printd(" := aryIter++");
355 + dumpInst(&inst[i+3], "end-loop");
356 + i += 3;
358 else {
359 /* with val */
360 - CHECK_OPERANDS(5, IMMED_INST,
361 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
362 + CHECK_OPERANDS(4, IMMED_INST,
363 + SYM_INST, SYM_INST, BRANCH_INST);
364 dumpInst(&inst[i+2], "keyArr");
365 printd("[] =");
366 dumpInst(&inst[i+3], "val");
367 - printd(" :=");
368 - dumpInst(&inst[i+4], "aryIter");
369 - printd("++");
370 - dumpInst(&inst[i+5], "end-loop");
371 - i += 5;
372 + printd(" := aryIter++");
373 + dumpInst(&inst[i+4], "end-loop");
374 + i += 4;
376 break;
378 diff --quilt old/source/parse.y new/source/parse.y
379 --- old/source/parse.y
380 +++ new/source/parse.y
381 @@ -256,71 +256,63 @@ stmt: ';' blank
382 ADD_OP(OP_BRANCH); ADD_BR_OFF($3); SET_BR_OFF($5, GetPC());
384 | for '(' blank SYMBOL IN blank arrayexpr blank ')' {
385 - Symbol *iterSym = InstallIteratorSymbol();
386 ADD_OP(OP_BEGIN_ARRAY_ITER);
387 - ADD_SYM(iterSym);
388 ADD_OP(OP_ARRAY_ITER);
389 ADD_IMMED(0); /* without val symbol */
390 ADD_SYM($4);
391 - ADD_SYM(iterSym);
392 ADD_BR_OFF(0);
394 blank block {
395 ADD_OP(OP_BRANCH);
396 - ADD_BR_OFF($7+2);
397 - SET_BR_OFF($7+6, GetPC());
398 - FillLoopAddrs(GetPC(), $7+2);
399 + ADD_BR_OFF($7+1);
400 + SET_BR_OFF($7+4, GetPC());
401 + FillLoopAddrs(GetPC(), $7+1);
402 + ADD_OP(OP_POP); /* remove iter from stack */
404 | for '(' blank SYMBOL KEYVAL SYMBOL IN blank arrayexpr blank ')' {
405 - Symbol *iterSym = InstallIteratorSymbol();
406 ADD_OP(OP_BEGIN_ARRAY_ITER);
407 - ADD_SYM(iterSym);
408 ADD_OP(OP_ARRAY_ITER);
409 ADD_IMMED(1); /* with val symbol */
410 ADD_SYM($4);
411 ADD_SYM($6);
412 - ADD_SYM(iterSym);
413 ADD_BR_OFF(0);
415 blank block {
416 ADD_OP(OP_BRANCH);
417 - ADD_BR_OFF($9+2);
418 - SET_BR_OFF($9+7, GetPC());
419 - FillLoopAddrs(GetPC(), $9+2);
420 + ADD_BR_OFF($9+1);
421 + SET_BR_OFF($9+5, GetPC());
422 + FillLoopAddrs(GetPC(), $9+1);
423 + ADD_OP(OP_POP); /* remove iter from stack */
425 | for '(' blank SYMBOL '[' numexpropt ']' IN blank arrayexpr blank ')' {
426 - Symbol *iterSym = InstallIteratorSymbol();
427 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
428 - ADD_SYM(iterSym);
429 ADD_OP(OP_ARRAY_ITER_ARRAY);
430 ADD_IMMED(0); /* without val symbol */
431 ADD_SYM($4);
432 - ADD_SYM(iterSym);
433 ADD_BR_OFF(0);
435 blank block {
436 ADD_OP(OP_BRANCH);
437 - ADD_BR_OFF($10+2);
438 - SET_BR_OFF($10+6, GetPC());
439 - FillLoopAddrs(GetPC(), $10+2);
440 + ADD_BR_OFF($10+1);
441 + SET_BR_OFF($10+4, GetPC());
442 + FillLoopAddrs(GetPC(), $10+1);
443 + ADD_OP(OP_POP); /* remove iter from stack */
444 ADD_OP(OP_POP); /* remove nDim from stack */
446 | for '(' blank SYMBOL '[' numexpropt ']' KEYVAL SYMBOL IN blank arrayexpr blank ')' {
447 - Symbol *iterSym = InstallIteratorSymbol();
448 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
449 - ADD_SYM(iterSym);
450 ADD_OP(OP_ARRAY_ITER_ARRAY);
451 ADD_IMMED(1); /* with val symbol */
452 ADD_SYM($4);
453 ADD_SYM($9);
454 - ADD_SYM(iterSym);
455 ADD_BR_OFF(0);
457 blank block {
458 ADD_OP(OP_BRANCH);
459 - ADD_BR_OFF($12+2);
460 - SET_BR_OFF($12+7, GetPC());
461 - FillLoopAddrs(GetPC(), $12+2);
462 + ADD_BR_OFF($12+1);
463 + SET_BR_OFF($12+5, GetPC());
464 + FillLoopAddrs(GetPC(), $12+1);
465 + ADD_OP(OP_POP); /* remove iter from stack */
466 ADD_OP(OP_POP); /* remove nDim from stack */
468 | BREAK stmtend blank {
469 diff --quilt old/source/ops.h new/source/ops.h
470 --- old/source/ops.h
471 +++ new/source/ops.h
472 @@ -40,10 +40,10 @@ OP(BRANCH_NEVER, branchNever)
473 OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
474 OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
475 OP(ARRAY_ASSIGN_NEXT, arrayAssignNext) /* pop(v, a), a[a.nextidx] = v */
476 -OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
477 -OP(ARRAY_ITER, arrayIter) /*w,k[,v],it,pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
478 -OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /*it*/ /* */
479 -OP(ARRAY_ITER_ARRAY, arrayIterArray) /*w,ka[,v],it,pc*/ /* top(N),while it: (if dim(it.k)==N: (ka.v=split(it.k),(w?v.v=it.v:),return),it++), PC=pc */
480 +OP(BEGIN_ARRAY_ITER, beginArrayIter) /* pop(a), it=a.begin, push(it) */
481 +OP(ARRAY_ITER, arrayIter) /*w,k[,v],pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
482 +OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /* pop(a), it=a.begin, push(it) */
483 +OP(ARRAY_ITER_ARRAY, arrayIterArray) /*w,ka[,v],pc*/ /* top(N),while it: (if dim(it.k)==N: (ka.v=split(it.k),(w?v.v=it.v:),return),it++), PC=pc */
484 OP(IN_ARRAY, inArray) /* pop(a,k), push(a[k]?1:0) */
485 OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
486 OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */
487 diff --quilt old/source/interpret.h new/source/interpret.h
488 --- old/source/interpret.h
489 +++ new/source/interpret.h
490 @@ -161,7 +161,6 @@ int AddImmediate(int immed, char **msg);
491 int AddBranchOffset(Inst *to, char **msg);
492 int SetBranchOffset(Inst *from, Inst *to, char **msg);
493 Inst *GetPC(void);
494 -Symbol *InstallIteratorSymbol(void);
495 Symbol *LookupStringConstSymbol(const char *value);
496 Symbol *InstallStringConstSymbol(const char *str);
497 Symbol *LookupSymbol(const char *name);