re-fresh
[nedit-bw.git] / more-stack-ops.patch
blob4bca4eebd8b9f9a530bcf5035ceb0f282e439f64
1 ---
3 source/interpret.c | 94 ++++++++++++++++++++++++++++++++++++++++-------------
4 source/ops.h | 5 +-
5 source/parse.y | 2 -
6 3 files changed, 76 insertions(+), 25 deletions(-)
8 diff --quilt old/source/interpret.c new/source/interpret.c
9 --- old/source/interpret.c
10 +++ new/source/interpret.c
11 @@ -1297,7 +1297,7 @@ static void addToGlobalSymTab(Symbol *sy
12 #define OK_TO_PUSH(n) \
13 (StackP + (n) <= (TheStack + STACK_SIZE))
15 - #define PUSH_CHECK(n) \
16 +#define PUSH_CHECK(n) \
17 do { \
18 if (!OK_TO_PUSH(n)) { \
19 EXEC_ERROR(StackOverflowMsg, ""); \
20 @@ -1314,28 +1314,64 @@ static void addToGlobalSymTab(Symbol *sy
21 } \
22 } while (0)
24 +#define _POP() (*--StackP)
25 +#define _PUSH() (*StackP++)
26 +#define _PEEK(index) (*(StackP - (index) - 1))
28 #define POP(dataVal) \
29 do { \
30 POP_CHECK(1); \
31 - dataVal = *--StackP; \
32 + dataVal = _POP(); \
33 } while (0)
36 #define PUSH(dataVal) \
37 do { \
38 PUSH_CHECK(1); \
39 - *StackP++ = dataVal; \
40 + _PUSH() = dataVal; \
41 } while (0)
43 #define PEEK(dataVal, peekIndex) \
44 do { \
45 PEEK_CHECK(peekIndex); \
46 - dataVal = *(StackP - (peekIndex) - 1); \
47 + dataVal = _PEEK(peekIndex); \
48 } while (0)
50 #define SUBST(dataVal, substIndex) \
51 do { \
52 PEEK_CHECK(substIndex); \
53 - *(StackP - (substIndex) - 1) = dataVal; \
54 + _PEEK(substIndex) = dataVal; \
55 + } while (0)
57 +#define MOVE(from, to) \
58 + do { \
59 + PEEK_CHECK(from); \
60 + PEEK_CHECK(to); \
61 + _PEEK(to) = _PEEK(from); \
62 + } while (0)
64 +#define PEEK_PUSH(index) \
65 + do { \
66 + int __i; \
67 + DataValue __val; \
68 + PEEK(__val, index); \
69 + /* shift down elements */ \
70 + for (__i = (index); __i > 0; __i--) { \
71 + MOVE(__i - 1, __i); \
72 + } \
73 + /* put on top */ \
74 + SUBST(__val, 0); \
75 + } while (0)
77 +#define PEEK_POP(value, index) \
78 + do { \
79 + int __i; \
80 + PEEK(value, index); \
81 + /* shift down elements */ \
82 + for (__i = (index); __i > 0; __i--) { \
83 + MOVE(__i - 1, __i); \
84 + } \
85 + /* pop */ \
86 + _POP(); \
87 } while (0)
89 #define TO_INT(dataVal, number) \
90 @@ -1984,15 +2020,14 @@ static int dupStack(void)
94 -** copy the value at stack postion index onto the stack
95 -** Before: Prog-> [index], next, ...
96 -** TheStack-> val0, ... valIndex, next, ...
97 -** After: Prog-> index, [next], ...
98 -** TheStack-> valIndex, val0, ... valIndex, next, ...
99 +** brings index on top of the stack and shifts above elements down
100 +** Before: Prog-> [Index], next, ...
101 +** TheStack-> val0, ... val{Index-1}, valIndex, next, ...
102 +** After: Prog-> Index, [next], ...
103 +** TheStack-> valIndex, val0, ... val{Index-1}, next, ...
105 static int peekPush(void)
107 - DataValue value;
108 int index;
110 DISASM_RT();
111 @@ -2001,8 +2036,7 @@ static int peekPush(void)
113 STACKDUMP(index + 1, 3);
115 - PEEK(value, index);
116 - PUSH(value);
117 + PEEK_PUSH(index);
119 return STAT_OK;
121 @@ -2025,15 +2059,31 @@ static int peekPop(void)
123 STACKDUMP(index + 1, 3);
125 - PEEK_CHECK(index);
126 + PEEK_POP(value, index);
128 - /* shift down elements */
129 - for (i = index; i > 0; i--) {
130 - PEEK(value, i - 1);
131 - SUBST(value, i);
133 - /* decr stack */
134 - POP(value);
135 + return STAT_OK;
139 +** duplicate the value at stack postion index onto the stack
140 +** Before: Prog-> [index], next, ...
141 +** TheStack-> val0, ... valIndex, next, ...
142 +** After: Prog-> index, [next], ...
143 +** TheStack-> valIndex, val0, ... valIndex, next, ...
145 +static int peekDup(void)
147 + DataValue value;
148 + int index;
150 + DISASM_RT();
152 + GET_IMMED(index);
154 + STACKDUMP(index + 1, 3);
156 + PEEK(value, index);
157 + PUSH(value);
159 return STAT_OK;
161 diff --quilt old/source/ops.h new/source/ops.h
162 --- old/source/ops.h
163 +++ new/source/ops.h
164 @@ -10,8 +10,9 @@ OP(PUSH_IMMED, pushImmed)
165 OP(PUSH_STRING, pushString) /* str */ /* push(str) */
166 OP(POP, popStack) /* pop(v) */
167 OP(DUP, dupStack) /* pop(v), push(v,v) */
168 -OP(PEEK_PUSH, peekPush) /* n */ /* peek(v, n), push(v) */
169 -OP(PEEK_POP, peekPop) /* n */ /* pop(v0,..,vn), push(v0,...,v{n-1}) */
170 +OP(PEEK_PUSH, peekPush) /* n */ /* pop(v0,..,vn), push(v{n-1},...,v0,vn) */
171 +OP(PEEK_POP, peekPop) /* n */ /* pop(v0,..,vn), push(v{n-1},...,v0) */
172 +OP(PEEK_DUP, peekDup) /* n */ /* peek(v, n), push(v) */
173 OP(ADD, add) /* pop(v2,v1), push(v1 + v2) */
174 OP(SUB, subtract) /* pop(v2,v1), push(v1 - v2) */
175 OP(MUL, multiply) /* pop(v2,v1), push(v1 * v2) */
176 diff --quilt old/source/parse.y new/source/parse.y
177 --- old/source/parse.y
178 +++ new/source/parse.y
179 @@ -602,7 +602,7 @@ lventry: SYMBOL {
181 | initarraylv key {
182 /* above the rvalue is the array + nDim strings */
183 - ADD_OP(OP_PEEK_PUSH); ADD_IMMED($2 + 1);
184 + ADD_OP(OP_PEEK_DUP); ADD_IMMED($2 + 1);
185 $$ = GetPC();
186 ADD_OP(OP_ARRAY_ASSIGN); ADD_IMMED($2);