UNPKG

2.91 kBtext/coffeescriptView Raw
1# _______
2# | | <- stack
3# | |
4# |_______|
5# | | <- stack + tos
6# | |
7# | |
8# |_______|
9# | | <- frame
10# |_______|
11# <- stack + TOS
12#
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.
16#
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.
20
21
22
23tos = 0
24
25# p is a U
26nil_symbols = 0
27push = (p) ->
28 if !p?
29 debugger
30 if p.isZero?
31 debugger
32
33 #console.log "pushing "
34 #console.log print_list(p)
35
36 if p == symbol(NIL)
37 nil_symbols++
38 if DEBUG then console.log "pushing symbol(NIL) #" + nil_symbols
39 #if nil_symbols == 111
40 # debugger
41 if (tos >= frame)
42 stop("stack overflow")
43 stack[tos++] = p
44
45# returns a U
46
47moveTos = (stackPos) ->
48 if tos <= stackPos
49 # we are moving the stack pointer
50 # "up" the stack (as if we were doing a push)
51 tos = stackPos
52 return
53 # we are moving the stack pointer
54 # "down" the stack i.e. as if we were
55 # doing a pop, we can zero-
56 # out all the elements that we pass
57 # so we can reclaim the memory
58 while tos > stackPos
59 stack[tos] = null
60 tos--
61 return
62
63top = ->
64 stack[tos-1]
65
66pop = ->
67 #popsNum++
68 #console.log "pop #" + popsNum
69 if (tos == 0)
70 debugger
71 stop("stack underflow")
72 if !stack[tos-1]?
73 debugger
74 elementToBeReturned = stack[--tos]
75
76 # give a chance to the garbage
77 # collection to reclaim space
78 # This is JS-specific, it would
79 # actually make the C garbage
80 # collector useless.
81 stack[tos] = null
82
83 return elementToBeReturned
84
85# n is an integer
86push_frame = (n) ->
87 i = 0
88 frame -= n
89 if (frame < tos)
90 debugger
91 stop("frame overflow, circular reference?")
92 for i in [0...n]
93 stack[frame + i] = symbol(NIL)
94
95# n is an integer
96pop_frame = (n) ->
97 frame += n
98 if (frame > TOS)
99 stop("frame underflow")
100
101save = ->
102 frame -= 10
103 if (frame < tos)
104 debugger
105 stop("frame overflow, circular reference?")
106 stack[frame + 0] = p0
107 stack[frame + 1] = p1
108 stack[frame + 2] = p2
109 stack[frame + 3] = p3
110 stack[frame + 4] = p4
111 stack[frame + 5] = p5
112 stack[frame + 6] = p6
113 stack[frame + 7] = p7
114 stack[frame + 8] = p8
115 stack[frame + 9] = p9
116
117restore = ->
118 if (frame > TOS - 10)
119 stop("frame underflow")
120 p0 = stack[frame + 0]
121 p1 = stack[frame + 1]
122 p2 = stack[frame + 2]
123 p3 = stack[frame + 3]
124 p4 = stack[frame + 4]
125 p5 = stack[frame + 5]
126 p6 = stack[frame + 6]
127 p7 = stack[frame + 7]
128 p8 = stack[frame + 8]
129 p9 = stack[frame + 9]
130 frame += 10
131
132# Local U * is OK here because there is no functional path to the garbage collector.
133
134swap = ->
135 #U *p, *q
136 # p and q are both Us
137 p = pop()
138 q = pop()
139 push(p)
140 push(q)
141
142# Local U * is OK here because there is no functional path to the garbage collector.
143
144dupl = ->
145 #U *p
146 p = pop()
147 push(p)
148 push(p)
149
150$.dupl = dupl
151$.swap = swap
152$.restore = restore
153$.save = save
154$.push = push
155$.pop = pop