add cast to XtCalloc() call
[nedit-bw.git] / consolidate-ops.patch
blobe481a220786b6edfeb8b68e6c5db6ecb4b1e8a85
1 Subject: consolidate macro ops
3 This patch consolidates the macro op definition and declaration, plus a little
4 documentation.
6 Its done by a new header "ops.h" where all ops are defined. This header is
7 than included in the right place with a definition of the OP() macro.
9 The documentation idea is borrowed from code from Tony and slightly adjusted.
11 ---
13 source/Makefile.dependencies | 18 +++----
14 source/interpret.c | 108 +++++--------------------------------------
15 source/interpret.h | 16 ++----
16 source/ops.h | 48 +++++++++++++++++++
17 4 files changed, 78 insertions(+), 112 deletions(-)
19 diff --quilt old/source/Makefile.dependencies new/source/Makefile.dependencies
20 --- old/source/Makefile.dependencies
21 +++ new/source/Makefile.dependencies
22 @@ -12,30 +12,30 @@ highlight.o: highlight.c highlight.h ned
23 textP.h regularExp.h highlightData.h preferences.h window.h \
24 ../util/misc.h ../util/DialogF.h
25 highlightData.o: highlightData.c highlightData.h nedit.h textBuf.h \
26 highlight.h regularExp.h preferences.h help.h help_topic.h window.h \
27 regexConvert.h ../util/misc.h ../util/DialogF.h ../util/managedList.h
28 -interpret.o: interpret.c interpret.h nedit.h textBuf.h rbTree.h menu.h \
29 +interpret.o: interpret.c interpret.h ops.h nedit.h textBuf.h rbTree.h menu.h \
30 text.h
31 linkdate.o: linkdate.c
32 macro.o: macro.c macro.h nedit.h textBuf.h text.h window.h preferences.h \
33 - interpret.h rbTree.h parse.h search.h server.h shell.h smartIndent.h \
34 + interpret.h ops.h rbTree.h parse.h search.h server.h shell.h smartIndent.h \
35 userCmds.h selection.h tags.h calltips.h textDisp.h ../util/DialogF.h \
36 ../util/misc.h ../util/fileUtils.h ../util/utils.h highlight.h \
37 highlightData.h rangeset.h
38 menu.o: menu.c menu.h nedit.h textBuf.h text.h file.h window.h search.h \
39 selection.h undo.h shift.h help.h help_topic.h preferences.h tags.h \
40 - userCmds.h shell.h macro.h highlight.h highlightData.h interpret.h \
41 + userCmds.h shell.h macro.h highlight.h highlightData.h interpret.h ops.h \
42 rbTree.h smartIndent.h windowTitle.h ../util/getfiles.h \
43 ../util/DialogF.h ../util/misc.h ../util/fileUtils.h ../util/utils.h
44 nc.o: nc.c server_common.h ../util/fileUtils.h ../util/utils.h \
45 ../util/prefFile.h ../util/system.h ../util/clearcase.h
46 nedit.o: nedit.c nedit.h textBuf.h file.h preferences.h regularExp.h \
47 - selection.h tags.h menu.h macro.h server.h window.h interpret.h \
48 + selection.h tags.h menu.h macro.h server.h window.h interpret.h ops.h \
49 rbTree.h parse.h help.h help_topic.h ../util/misc.h \
50 ../util/printUtils.h ../util/fileUtils.h ../util/getfiles.h
51 -parse_noyacc.o: parse_noyacc.c parse.h interpret.h nedit.h textBuf.h \
52 +parse_noyacc.o: parse_noyacc.c parse.h interpret.h ops.h nedit.h textBuf.h \
53 rbTree.h
54 preferences.o: preferences.c preferences.h nedit.h textBuf.h text.h \
55 search.h window.h userCmds.h highlight.h highlightData.h help.h \
56 help_topic.h regularExp.h smartIndent.h windowTitle.h server.h tags.h \
57 ../util/prefFile.h ../util/misc.h ../util/DialogF.h \
58 @@ -54,14 +54,14 @@ server.o: server.c server.h window.h ned
59 macro.h menu.h preferences.h server_common.h ../util/fileUtils.h \
60 ../util/utils.h
61 server_common.o: server_common.c textBuf.h nedit.h server_common.h \
62 ../util/utils.h
63 shell.o: shell.c shell.h nedit.h textBuf.h text.h window.h preferences.h \
64 - file.h macro.h interpret.h rbTree.h ../util/DialogF.h ../util/misc.h
65 + file.h macro.h interpret.h ops.h rbTree.h ../util/DialogF.h ../util/misc.h
66 shift.o: shift.c shift.h nedit.h textBuf.h text.h window.h
67 smartIndent.o: smartIndent.c smartIndent.h nedit.h textBuf.h text.h \
68 - preferences.h interpret.h rbTree.h macro.h window.h parse.h shift.h \
69 + preferences.h interpret.h ops.h rbTree.h macro.h window.h parse.h shift.h \
70 help.h help_topic.h ../util/DialogF.h ../util/misc.h
71 tags.o: tags.c tags.h nedit.h textBuf.h text.h window.h file.h \
72 preferences.h search.h selection.h calltips.h textDisp.h \
73 ../util/DialogF.h ../util/fileUtils.h ../util/misc.h ../util/utils.h
74 text.o: text.c text.h textBuf.h textP.h textDisp.h textSel.h textDrag.h \
75 @@ -72,17 +72,17 @@ textDisp.o: textDisp.c textDisp.h textBu
76 textDrag.o: textDrag.c textDrag.h text.h textBuf.h textDisp.h textP.h
77 textSel.o: textSel.c textSel.h textP.h textBuf.h textDisp.h text.h
78 undo.o: undo.c undo.h nedit.h textBuf.h text.h search.h window.h file.h \
79 userCmds.h preferences.h
80 userCmds.o: userCmds.c userCmds.h nedit.h textBuf.h text.h preferences.h \
81 - window.h menu.h shell.h macro.h file.h interpret.h rbTree.h parse.h \
82 + window.h menu.h shell.h macro.h file.h interpret.h ops.h rbTree.h parse.h \
83 ../util/DialogF.h ../util/misc.h ../util/managedList.h
84 window.o: window.c window.h nedit.h textBuf.h textSel.h text.h textDisp.h \
85 textP.h menu.h file.h search.h undo.h preferences.h selection.h \
86 server.h shell.h macro.h highlight.h smartIndent.h userCmds.h nedit.bm \
87 n.bm windowTitle.h ../util/clearcase.h ../util/misc.h \
88 ../util/fileUtils.h ../util/utils.h
89 windowTitle.o: windowTitle.c windowTitle.h nedit.h textBuf.h \
90 preferences.h help.h help_topic.h ../util/prefFile.h ../util/misc.h \
91 ../util/DialogF.h ../util/utils.h ../util/fileUtils.h \
92 ../util/clearcase.h
93 -parse.c: parse.h textBuf.h nedit.h rbTree.h interpret.h
94 +parse.c: parse.h textBuf.h nedit.h rbTree.h interpret.h ops.h
95 diff --quilt old/source/interpret.c new/source/interpret.c
96 --- old/source/interpret.c
97 +++ new/source/interpret.c
98 @@ -80,54 +80,16 @@ static const char CVSID[] = "$Id: interp
99 enum opStatusCodes {STAT_OK=2, STAT_DONE, STAT_ERROR, STAT_PREEMPT};
101 static void addLoopAddr(Inst *addr);
102 static void saveContext(RestartData *context);
103 static void restoreContext(RestartData *context);
104 -static int returnNoVal(void);
105 -static int returnVal(void);
107 +#define OP(name, fn) static int fn(void);
108 +#include "ops.h"
109 +#undef OP
110 static int returnValOrNone(int valOnStack);
111 -static int pushSymVal(void);
112 -static int pushArgVal(void);
113 -static int pushArgCount(void);
114 -static int pushArgArray(void);
115 -static int pushArraySymVal(void);
116 -static int dupStack(void);
117 -static int add(void);
118 -static int subtract(void);
119 -static int multiply(void);
120 -static int divide(void);
121 -static int modulo(void);
122 -static int negate(void);
123 -static int increment(void);
124 -static int decrement(void);
125 -static int gt(void);
126 -static int lt(void);
127 -static int ge(void);
128 -static int le(void);
129 -static int eq(void);
130 -static int ne(void);
131 -static int bitAnd(void);
132 -static int bitOr(void);
133 -static int and(void);
134 -static int or(void);
135 -static int not(void);
136 -static int power(void);
137 -static int concat(void);
138 -static int assign(void);
139 -static int callSubroutine(void);
140 -static int fetchRetVal(void);
141 -static int branch(void);
142 -static int branchTrue(void);
143 -static int branchFalse(void);
144 -static int branchNever(void);
145 -static int arrayRef(void);
146 -static int arrayAssign(void);
147 -static int arrayRefAndAssignSetup(void);
148 -static int beginArrayIter(void);
149 -static int arrayIter(void);
150 -static int inArray(void);
151 -static int deleteArrayElement(void);
153 static void freeSymbolTable(Symbol *symTab);
154 static int errCheck(const char *s);
155 static int execError(const char *s1, const char *s2);
156 static rbTreeNode *arrayEmptyAllocator(void);
157 static rbTreeNode *arrayAllocateNode(rbTreeNode *src);
158 @@ -205,17 +167,15 @@ static WindowInfo *FocusWindow; /* w
159 static int PreemptRequest; /* passes preemption requests from called
160 routines back up to the interpreter */
162 /* Array for mapping operations to functions for performing the operations
163 Must correspond to the enum called "operations" in interpret.h */
164 -static int (*OpFns[N_OPS])() = {returnNoVal, returnVal, pushSymVal, dupStack,
165 - add, subtract, multiply, divide, modulo, negate, increment, decrement,
166 - gt, lt, ge, le, eq, ne, bitAnd, bitOr, and, or, not, power, concat,
167 - assign, callSubroutine, fetchRetVal, branch, branchTrue, branchFalse,
168 - branchNever, arrayRef, arrayAssign, beginArrayIter, arrayIter, inArray,
169 - deleteArrayElement, pushArraySymVal,
170 - arrayRefAndAssignSetup, pushArgVal, pushArgCount, pushArgArray};
171 +static int (*OpFns[])() = {
172 +#define OP(name, fn) fn,
173 +#include "ops.h"
174 +#undef OP
177 /* Stack-> symN-sym0(FP), argArray, nArgs, oldFP, retPC, argN-arg1, next, ... */
178 #define FP_ARG_ARRAY_CACHE_INDEX (-1)
179 #define FP_ARG_COUNT_INDEX (-2)
180 #define FP_OLD_FP_INDEX (-3)
181 @@ -3027,54 +2987,14 @@ static void dumpVal(DataValue dv)
182 #endif /* #ifdef DEBUG_DISASSEMBLER */
184 #ifdef DEBUG_DISASSEMBLER /* For debugging code generation */
185 static void disasm(Inst *inst, int nInstr)
187 - static const char *opNames[N_OPS] = {
188 - "RETURN_NO_VAL", /* returnNoVal */
189 - "RETURN", /* returnVal */
190 - "PUSH_SYM", /* pushSymVal */
191 - "DUP", /* dupStack */
192 - "ADD", /* add */
193 - "SUB", /* subtract */
194 - "MUL", /* multiply */
195 - "DIV", /* divide */
196 - "MOD", /* modulo */
197 - "NEGATE", /* negate */
198 - "INCR", /* increment */
199 - "DECR", /* decrement */
200 - "GT", /* gt */
201 - "LT", /* lt */
202 - "GE", /* ge */
203 - "LE", /* le */
204 - "EQ", /* eq */
205 - "NE", /* ne */
206 - "BIT_AND", /* bitAnd */
207 - "BIT_OR", /* bitOr */
208 - "AND", /* and */
209 - "OR", /* or */
210 - "NOT", /* not */
211 - "POWER", /* power */
212 - "CONCAT", /* concat */
213 - "ASSIGN", /* assign */
214 - "SUBR_CALL", /* callSubroutine */
215 - "FETCH_RET_VAL", /* fetchRetVal */
216 - "BRANCH", /* branch */
217 - "BRANCH_TRUE", /* branchTrue */
218 - "BRANCH_FALSE", /* branchFalse */
219 - "BRANCH_NEVER", /* branchNever */
220 - "ARRAY_REF", /* arrayRef */
221 - "ARRAY_ASSIGN", /* arrayAssign */
222 - "BEGIN_ARRAY_ITER", /* beginArrayIter */
223 - "ARRAY_ITER", /* arrayIter */
224 - "IN_ARRAY", /* inArray */
225 - "ARRAY_DELETE", /* deleteArrayElement */
226 - "PUSH_ARRAY_SYM", /* pushArraySymVal */
227 - "ARRAY_REF_ASSIGN_SETUP", /* arrayRefAndAssignSetup */
228 - "PUSH_ARG", /* $arg[expr] */
229 - "PUSH_ARG_COUNT", /* $arg[] */
230 - "PUSH_ARG_ARRAY" /* $arg */
231 + static const char *opNames[] = {
232 +#define OP(name, fn) #name,
233 +#include "ops.h"
234 +#undef OP
236 int i, j;
238 printf("\n");
239 for (i = 0; i < nInstr; ++i) {
240 diff --quilt old/source/interpret.h new/source/interpret.h
241 --- old/source/interpret.h
242 +++ new/source/interpret.h
243 @@ -37,19 +37,17 @@
244 events passed to action routines. Tells
245 them that they were called from a macro */
247 enum symTypes {CONST_SYM, GLOBAL_SYM, LOCAL_SYM, ARG_SYM, PROC_VALUE_SYM,
248 C_FUNCTION_SYM, MACRO_FUNCTION_SYM, ACTION_ROUTINE_SYM};
249 -#define N_OPS 43
250 -enum operations {OP_RETURN_NO_VAL, OP_RETURN, OP_PUSH_SYM, OP_DUP, OP_ADD,
251 - OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_NEGATE, OP_INCR, OP_DECR, OP_GT, OP_LT,
252 - OP_GE, OP_LE, OP_EQ, OP_NE, OP_BIT_AND, OP_BIT_OR, OP_AND, OP_OR, OP_NOT,
253 - OP_POWER, OP_CONCAT, OP_ASSIGN, OP_SUBR_CALL, OP_FETCH_RET_VAL, OP_BRANCH,
254 - OP_BRANCH_TRUE, OP_BRANCH_FALSE, OP_BRANCH_NEVER, OP_ARRAY_REF,
255 - OP_ARRAY_ASSIGN, OP_BEGIN_ARRAY_ITER, OP_ARRAY_ITER, OP_IN_ARRAY,
256 - OP_ARRAY_DELETE, OP_PUSH_ARRAY_SYM, OP_ARRAY_REF_ASSIGN_SETUP, OP_PUSH_ARG,
257 - OP_PUSH_ARG_COUNT, OP_PUSH_ARG_ARRAY};
259 +enum operations {
260 +#define OP(name, fn) OP_##name,
261 +#include "ops.h"
262 +#undef OP
263 + N_OPS
266 enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG};
268 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
270 diff --quilt /dev/null new/source/ops.h
271 --- /dev/null
272 +++ new/source/ops.h
273 @@ -0,0 +1,48 @@
274 +#ifndef OP
275 +#error define OP() before including this file
276 +#endif
278 +/* op name function arguments operation */
279 +OP(RETURN_NO_VAL, returnNoVal) /* rewind */
280 +OP(RETURN, returnVal) /* pop(ret), rewind, push(ret) */
281 +OP(PUSH_SYM, pushSymVal) /* sym */ /* push(sym.v) */
282 +OP(DUP, dupStack) /* pop(v), push(v,v) */
283 +OP(ADD, add) /* pop(v2,v1), push(v1 + v2) */
284 +OP(SUB, subtract) /* pop(v2,v1), push(v1 - v2) */
285 +OP(MUL, multiply) /* pop(v2,v1), push(v1 * v2) */
286 +OP(DIV, divide) /* pop(v2,v1), push(v1 / v2) */
287 +OP(MOD, modulo) /* pop(v2,v1), push(v1 % v2) */
288 +OP(NEGATE, negate) /* pop(v), push(-v) */
289 +OP(INCR, increment) /* pop(v), push(v + 1) */
290 +OP(DECR, decrement) /* pop(v), push(v - 1) */
291 +OP(GT, gt) /* pop(v2,v1), push(v1 > v2) */
292 +OP(LT, lt) /* pop(v2,v1), push(v1 < v2) */
293 +OP(GE, ge) /* pop(v2,v1), push(v1 >= v2) */
294 +OP(LE, le) /* pop(v2,v1), push(v1 <= v2) */
295 +OP(EQ, eq) /* pop(v2,v1), push(v1 == v2) */
296 +OP(NE, ne) /* pop(v2,v1), push(v1 != v2) */
297 +OP(BIT_AND, bitAnd) /* pop(v2,v1), push(v1 & v2) */
298 +OP(BIT_OR, bitOr) /* pop(v2,v1), push(v1 | v2) */
299 +OP(AND, and) /* pop(v2,v1), push(v1 && v2) */
300 +OP(OR, or) /* pop(v2,v1), push(v1 || v2) */
301 +OP(NOT, not) /* pop(v), push(!v) */
302 +OP(POWER, power) /* pop(v2,v1), push(v1 ** v2) */
303 +OP(CONCAT, concat) /* pop(s2,s1), push(s1 s2) */
304 +OP(ASSIGN, assign) /* sym */ /* pop(v), sym.v = v */
305 +OP(SUBR_CALL, callSubroutine) /* sym */ /* ret = call(sym), if PC == fetchRetVal: push(ret) */
306 +OP(FETCH_RET_VAL, fetchRetVal) /* internal error */
307 +OP(BRANCH, branch) /* off */ /* PC += off */
308 +OP(BRANCH_TRUE, branchTrue) /* off */ /* pop(v), if v: PC += off */
309 +OP(BRANCH_FALSE, branchFalse) /* off */ /* pop(v), if !v: PC += off */
310 +OP(BRANCH_NEVER, branchNever) /* off */ /* */
311 +OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
312 +OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
313 +OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
314 +OP(ARRAY_ITER, arrayIter) /* k,it,pc */ /* it ? (k.v=it.k, it++) : PC = pc */
315 +OP(IN_ARRAY, inArray) /* pop(a,k), push(a[k]?1:0) */
316 +OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
317 +OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */
318 +OP(ARRAY_REF_ASSIGN_SETUP, arrayRefAndAssignSetup) /*op,N*/ /* pop(v,kN..a), a[k1..kN] op= v */
319 +OP(PUSH_ARG, pushArgVal) /* pop(num), push($num) */
320 +OP(PUSH_ARG_COUNT, pushArgCount) /* push($n_args) */
321 +OP(PUSH_ARG_ARRAY, pushArgArray) /* push($args) */