forked from g000001/Starlisp-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.lisp
196 lines (149 loc) · 6.07 KB
/
context.lisp
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
;;; -*- Syntax: COMMON-LISP; MODE: LISP; BASE: 10; PACKAGE: *sim-i; MUSER: YES -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
#| External interface for context stacks.
Each Vp Set has its own context stack.
*current-context-stack*
The current VP Set's stack is bound to *current-context-stack*;
this is done by *WITH-VP-SET and SET-VP-SET.
*css*
The actual bit-vector which is the css is bound to *CSS*.
The variable is bound by things like *WHEN and *ALL,
macros which affect context. It of course also changes
when the current vp set is switched.
*current-css-level*
This indicates how many nested levels of context exist per
vp set. It is bound in the same places *css* is bound.
Create-context-stack
is used to create a context stack with
a given number of processors. It is used by the DEF-VP-SET
and CREATE-VP-SET mechanisms. The context stack becomes
one of the slots of the Vp Set defstruct.
initialize-context-stack
is used to set up the context stack to a state where
every processor is turned on. If *css-current-level*
is not zero this routine will error out as a safety
check.
push-css
Pushes a new context, which is a function of the current
context and its argument, a pvar.
pop-css
Pops a context and restores context to the
previous context. If the context stack underflows
an internal error is signalled.
pop-css-to-level
Restores context to a particular context, indicated
by an index. This allows stack discipline to be
violated within a *ALL or *WHEN and still have it
restored properly when properly exiting the *ALL or *WHEN.
get-context-at-level
Returns the bit-vector at a given level in the stack.
|#
(defvar *initial-context-stack-length* 32)
(defvar *context-stack-length-increment* 32)
(defstruct context-stack
(length 0 :type fixnum)
arrays
)
(defun create-context-stack (number-of-processors)
(declare (type fixnum number-of-processors))
(let ((context-stack (make-context-stack)))
(declare (type context-stack context-stack))
(setf (context-stack-length context-stack) *initial-context-stack-length*)
(setf (context-stack-arrays context-stack) (make-array *initial-context-stack-length*))
(let ((arrays (context-stack-arrays context-stack)))
(dotimes (j *initial-context-stack-length*)
(setf (aref arrays j) (make-array number-of-processors :element-type 'bit))
))
context-stack
))
(defun initialize-context-stack (context-stack)
;; (assert (zerop *css-current-level*) () "You are initializing the context stack but its level is not 0")
(let* ((arrays (context-stack-arrays context-stack))
(first-array (aref arrays 0))
)
(declare (type bit-vector first-array))
(dotimes (j (length first-array))
(setf (sbit first-array j) 1)
)
first-array
))
(defun get-context-at-level (context-stack level)
(assert (< level (context-stack-length context-stack)) () "Internal error. Attempt to get context beyond stack")
(aref (context-stack-arrays context-stack) level)
)
(defun incf-css-level ()
(let ((context-stack *current-context-stack*)
(current-level (incf *css-current-level*))
)
(declare (type context-stack context-stack))
(declare (type fixnum current-level))
(let ((length (context-stack-length context-stack)))
(declare (type fixnum length))
(cond
((< current-level length) nil)
((= current-level length)
(let* ((new-length (+ length *context-stack-length-increment*))
(new-array (make-array new-length))
(old-array (context-stack-arrays context-stack))
)
(dotimes (j new-length)
(declare (fixnum j))
(if (< j length)
(setf (aref new-array j) (aref old-array j))
(setf (aref new-array j) (make-array *number-of-processors-limit* :element-type 'bit))
))))
((> current-level length) (error "Internal error. Current context stack level exceeds length of stack"))
))))
(defun push-css (pvar &optional (always nil))
(safety-check
(when (not (internal-pvarp pvar))
(error "The argument to PUSH-CSS, ~S, is not a PVAR. You may be ~@
using a *WHEN or a *IF with a condition that does not really ~@
evaluate to a PVAR."
pvar
)))
(ecase (pvar-type pvar)
(:general
(new-pvar-check pvar 'internal-*Lisp)
(incf-css-level)
(let* ((pvar-data (pvar-data pvar))
(old-css *css*)
(new-css (get-context-at-level *current-context-stack* *css-current-level*))
)
(declare (type bit-vector old-css new-css))
(declare (type vector pvar-data))
(if always
(dotimes (p *number-of-processors-limit*)
(setf (sbit new-css p) (the bit (if (aref pvar-data p) 1 0)))
)
(dotimes (p *number-of-processors-limit*)
(setf (sbit new-css p) (logand (sbit old-css p) (the bit (if (aref pvar-data p) 1 0))))
))
(setq *css* new-css) ;make the new CSS the current CSS
))
((:array :structure) (push-css t!!))
))
;;; This will just simply pop out of the current context into the previous context
(defun pop-css ()
(decf *css-current-level*)
(if (<= *css-current-level* 0)
(error "Internal error. Context stack underflow. This isn't supposed to happen!!")
)
(setq *css* (get-context-at-level *current-context-stack* *css-current-level*))
)
(defun pop-css-to-level (level)
(assert (>= level 0) () "*LISP internal error. Context stack underflow.")
(assert (<= level *css-current-level*) () "*Lisp internal error. Attempt to position context at or above current context")
(setq *css-current-level* level)
(setq *css* (get-context-at-level *current-context-stack* *css-current-level*))
)
(defun push-css-select-all () (push-css t!! t))
(defun push-css-select-none () (push-css nil!! t))