Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / stack.cpp
blob81f41595f052c16542f2a7ac0a002ef54e0da20e
1 // _______
2 // | | <- stack
3 // | |
4 // |_______|
5 // | | <- stack + tos
6 // | |
7 // | |
8 // |_______|
9 // | | <- frame
10 // |_______|
11 // <- stack + TOS
13 // The stack grows from low memory towards high memory. This is so that
14 // multiple expressions can be pushed on the stack and then accessed as an
15 // array.
17 // The frame area holds local variables and grows from high memory towards
18 // low memory. The frame area makes local variables visible to the garbage
19 // collector.
21 #include "stdafx.h"
22 #include "defs.h"
24 U **frame, **stack;
25 int tos;
27 void
28 push(U *p)
30 if (stack + tos >= frame)
31 stop("stack overflow");
32 stack[tos++] = p;
35 U *
36 pop()
38 if (tos == 0)
39 stop("stack underflow");
40 return stack[--tos];
43 void
44 push_frame(int n)
46 int i;
47 frame -= n;
48 if (frame < stack + tos)
49 stop("frame overflow, circular reference?");
50 for (i = 0; i < n; i++)
51 frame[i] = symbol(NIL);
54 void
55 pop_frame(int n)
57 frame += n;
58 if (frame > stack + TOS)
59 stop("frame underflow");
62 void
63 save(void)
65 frame -= 10;
66 if (frame < stack + tos)
67 stop("frame overflow, circular reference?");
68 frame[0] = p0;
69 frame[1] = p1;
70 frame[2] = p2;
71 frame[3] = p3;
72 frame[4] = p4;
73 frame[5] = p5;
74 frame[6] = p6;
75 frame[7] = p7;
76 frame[8] = p8;
77 frame[9] = p9;
80 void
81 restore(void)
83 if (frame > stack + TOS - 10)
84 stop("frame underflow");
85 p0 = frame[0];
86 p1 = frame[1];
87 p2 = frame[2];
88 p3 = frame[3];
89 p4 = frame[4];
90 p5 = frame[5];
91 p6 = frame[6];
92 p7 = frame[7];
93 p8 = frame[8];
94 p9 = frame[9];
95 frame += 10;
98 // Local U * is OK here because there is no functional path to the garbage collector.
100 void
101 swap(void)
103 U *p, *q;
104 p = pop();
105 q = pop();
106 push(p);
107 push(q);
110 // Local U * is OK here because there is no functional path to the garbage collector.
112 void
113 dupl(void)
115 U *p;
116 p = pop();
117 push(p);
118 push(p);