/
stack.c
429 lines (349 loc) · 11 KB
/
stack.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*******************************************************************
** s t a c k . c
** Forth Inspired Command Language
** Author: John Sadler (john_sadler@alum.mit.edu)
** Created: 16 Oct 1997
** $Id: //depot/gamejones/ficl/stack.c#10 $
*******************************************************************/
/*
** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
** All rights reserved.
**
** Get the latest Ficl release at http://ficl.sourceforge.net
**
** I am interested in hearing from anyone who uses Ficl. If you have
** a problem, a success story, a defect, an enhancement request, or
** if you would like to contribute to the Ficl release, please
** contact me by email at the address above.
**
** L I C E N S E and D I S C L A I M E R
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#include <stdlib.h>
#include "ficl.h"
#define STKDEPTH(s) (((s)->top - (s)->base) + 1)
/*
** N O T E: Stack convention:
**
** THIS CHANGED IN FICL 4.0!
**
** top points to the *current* top data value
** push: increment top, store value at top
** pop: fetch value at top, decrement top
** Stack grows from low to high memory
*/
/*******************************************************************
v m C h e c k S t a c k
** Check the parameter stack for underflow or overflow.
** size controls the type of check: if size is zero,
** the function checks the stack state for underflow and overflow.
** If size > 0, checks to see that the stack has room to push
** that many cells. If less than zero, checks to see that the
** stack has room to pop that many cells. If any test fails,
** the function throws (via vmThrow) a VM_ERREXIT exception.
*******************************************************************/
void ficlStackCheck(ficlStack *stack, int popCells, int pushCells)
#if FICL_ROBUST >= 1
{
int nFree = stack->size - STKDEPTH(stack);
// fprintf(stderr,"poCells:%d depth: %d (%p,%p)\n",popCells,STKDEPTH(stack),stack->top,stack->base);
if (popCells > STKDEPTH(stack))
{
ficlVmThrowError(stack->vm, "Error: %s stack underflow", stack->name);
}
if (nFree < pushCells - popCells)
{
ficlVmThrowError(stack->vm, "Error: %s stack overflow", stack->name);
}
return;
}
#else /* FICL_ROBUST >= 1 */
{
FICL_IGNORE(stack);
FICL_IGNORE(popCells);
FICL_IGNORE(pushCells);
}
#endif /* FICL_ROBUST >= 1 */
/*******************************************************************
s t a c k C r e a t e
**
*******************************************************************/
ficlStack *ficlStackCreate(ficlVm *vm, char *name, unsigned size)
{
size_t totalSize = sizeof (ficlStack) + (size * sizeof (ficlCell));
ficlStack *stack = ficlMalloc(totalSize);
FICL_VM_ASSERT(vm, size != 0);
FICL_VM_ASSERT(vm, stack != NULL);
stack->size = size;
stack->frame = NULL;
stack->vm = vm;
stack->name = name;
ficlStackReset(stack);
return stack;
}
/*******************************************************************
s t a c k D e l e t e
**
*******************************************************************/
void ficlStackDestroy(ficlStack *stack)
{
if (stack) {
#if defined(DEBUG)
memset(stack, 0xEF, stack->size * sizeof(ficlCell) + sizeof(ficlStack));
#endif
ficlFree(stack);
}
return;
}
/*******************************************************************
s t a c k D e p t h
**
*******************************************************************/
int ficlStackDepth(ficlStack *stack)
{
return STKDEPTH(stack);
}
/*******************************************************************
s t a c k D r o p
**
*******************************************************************/
void ficlStackDrop(ficlStack *stack, int n)
{
FICL_VM_ASSERT(stack->vm, n > 0);
stack->top -= n;
return;
}
/*******************************************************************
s t a c k F e t c h
**
*******************************************************************/
ficlCell ficlStackFetch(ficlStack *stack, int n)
{
return stack->top[-n];
}
void ficlStackStore(ficlStack *stack, int n, ficlCell c)
{
stack->top[-n] = c;
return;
}
/*******************************************************************
s t a c k G e t T o p
**
*******************************************************************/
ficlCell ficlStackGetTop(ficlStack *stack)
{
return stack->top[0];
}
#if FICL_WANT_LOCALS
/*******************************************************************
s t a c k L i n k
** Link a frame using the stack's frame pointer. Allot space for
** size cells in the frame
** 1) Push frame
** 2) frame = top
** 3) top += size
*******************************************************************/
void ficlStackLink(ficlStack *stack, int size)
{
ficlStackPushPointer(stack, stack->frame);
stack->frame = stack->top + 1;
stack->top += size;
return;
}
/*******************************************************************
s t a c k U n l i n k
** Unink a stack frame previously created by stackLink
** 1) top = frame
** 2) frame = pop()
*******************************************************************/
void ficlStackUnlink(ficlStack *stack)
{
stack->top = stack->frame - 1;
stack->frame = ficlStackPopPointer(stack);
return;
}
#endif /* FICL_WANT_LOCALS */
/*******************************************************************
s t a c k P i c k
**
*******************************************************************/
void ficlStackPick(ficlStack *stack, int n)
{
ficlStackPush(stack, ficlStackFetch(stack, n));
return;
}
/*******************************************************************
s t a c k P o p
**
*******************************************************************/
ficlCell ficlStackPop(ficlStack *stack)
{
return *stack->top--;
}
void *ficlStackPopPointer(ficlStack *stack)
{
return (*stack->top--).p;
}
ficlUnsigned ficlStackPopUnsigned(ficlStack *stack)
{
return (*stack->top--).u;
}
ficlInteger ficlStackPopInteger(ficlStack *stack)
{
return (*stack->top--).i;
}
ficl2Integer ficlStackPop2Integer(ficlStack *stack)
{
ficl2Integer ret;
ficlInteger high = ficlStackPopInteger(stack);
ficlInteger low = ficlStackPopInteger(stack);
FICL_2INTEGER_SET(high, low, ret);
return ret;
}
ficl2Unsigned ficlStackPop2Unsigned(ficlStack *stack)
{
ficl2Unsigned ret;
ficlUnsigned high = ficlStackPopUnsigned(stack);
ficlUnsigned low = ficlStackPopUnsigned(stack);
FICL_2UNSIGNED_SET(high, low, ret);
return ret;
}
#if (FICL_WANT_FLOAT)
ficlFloat ficlStackPopFloat(ficlStack *stack)
{
return (*stack->top--).f;
}
#endif
/*******************************************************************
s t a c k P u s h
**
*******************************************************************/
void ficlStackPush(ficlStack *stack, ficlCell c)
{
*++stack->top = c;
}
void ficlStackPushPointer(ficlStack *stack, void *ptr)
{
*++stack->top = FICL_LVALUE_TO_CELL(ptr);
}
void ficlStackPushInteger(ficlStack *stack, ficlInteger i)
{
*++stack->top = FICL_LVALUE_TO_CELL(i);
}
void ficlStackPushUnsigned(ficlStack *stack, ficlUnsigned u)
{
*++stack->top = FICL_LVALUE_TO_CELL(u);
}
void ficlStackPush2Unsigned(ficlStack *stack, ficl2Unsigned du)
{
ficlStackPushUnsigned(stack, FICL_2UNSIGNED_GET_LOW(du));
ficlStackPushUnsigned(stack, FICL_2UNSIGNED_GET_HIGH(du));
return;
}
void ficlStackPush2Integer(ficlStack *stack, ficl2Integer di)
{
ficlStackPush2Unsigned(stack, FICL_2INTEGER_TO_2UNSIGNED(di));
return;
}
#if (FICL_WANT_FLOAT)
void ficlStackPushFloat(ficlStack *stack, ficlFloat f)
{
*++stack->top = FICL_LVALUE_TO_CELL(f);
}
#endif
/*******************************************************************
s t a c k R e s e t
**
*******************************************************************/
void ficlStackReset(ficlStack *stack)
{
int i;
ficlCell cell;
// fprintf(stderr,"called ficlStackReset()\n");
cell.u = 0xDEADC0DE;
for (i = 0; i < 4; i++)
stack->fence[i] = cell;
stack->top = stack->base - 1;
return;
}
/*******************************************************************
s t a c k R o l l
** Roll nth stack entry to the top (counting from zero), if n is
** >= 0. Drop other entries as needed to fill the hole.
** If n < 0, roll top-of-stack to nth entry, pushing others
** upward as needed to fill the hole.
*******************************************************************/
void ficlStackRoll(ficlStack *stack, int n)
{
ficlCell c;
ficlCell *cell;
if (n == 0)
return;
else if (n > 0)
{
cell = stack->top - n;
c = *cell;
for (;n > 0; --n, cell++)
{
*cell = cell[1];
}
*cell = c;
}
else
{
cell = stack->top;
c = *cell;
for (; n < 0; ++n, cell--)
{
*cell = cell[-1];
}
*cell = c;
}
return;
}
/*******************************************************************
s t a c k S e t T o p
**
*******************************************************************/
void ficlStackSetTop(ficlStack *stack, ficlCell c)
{
FICL_STACK_CHECK(stack, 1, 1);
stack->top[0] = c;
return;
}
void ficlStackWalk(ficlStack *stack, ficlStackWalkFunction callback, void *context, ficlInteger bottomToTop)
{
int i;
int depth;
ficlCell *cell;
FICL_STACK_CHECK(stack, 0, 0);
depth = ficlStackDepth(stack);
cell = bottomToTop ? stack->base : stack->top;
for (i = 0; i < depth; i++)
{
if (callback(context, cell) == FICL_FALSE)
break;
cell += bottomToTop ? 1 : -1;
}
return;
}