-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodules.clj
292 lines (270 loc) · 10.9 KB
/
modules.clj
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
(ns piplin.modules
(:use [piplin types protocols])
(:use [clojure.string :only [join]]
[clojure.set :only [map-invert]])
(:use [slingshot.slingshot :only [throw+]])
(:refer-clojure :exclude [replace cast])
(:use [clojure.string :only [join replace split]])
(:use [swiss-arrows.core :only [-<> -<>>]])
(:use [clojure.pprint :only [pprint]])
(:require [plumbing.graph :as graph]
[plumbing.core :as plumb]))
(defn make-port*
"Takes a keyword name, an owning module token, and
the port's type and returns the port."
[name piplin-type port-type]
(alter-value (mkast piplin-type :port []
#(throw+
(error "no longer using sim-fn")))
merge
{:port name
:port-type port-type}))
(declare make-input-map)
(def ^:dynamic *module-path* [])
(defn walk-expr
[expr visit combine]
(let [x (visit expr)]
(if-let [args (:args (value expr))]
(let [subexprs (vals args)]
(reduce combine x
(map #(walk-expr % visit combine)
subexprs)))
x)))
(def ^:dynamic *sim-state*)
(defn make-sim-fn
"Takes a map-zipper of the ast of an expr
and walks along the expr. It returns a function
that computes the expr
and takes no args (needed ports come via binding).
The function collects its args into a map which it
binds before invoking the function so that the
ports can get their values at the bottom."
[expr]
(let [[my-sim-fn my-args]
(if (pipinst? expr)
[#(identity expr) []]
(-> expr
meta
:sim-factory))]
(let [args (:args (value expr))
arg-fns (map #(make-sim-fn (val %)) args)
arg-map (zipmap (keys args)
arg-fns)
fn-vec (map #(get arg-map %) my-args)]
(if (= (:op (value expr))
:port)
(condp = (:port-type (value expr))
:register
(let [path (conj *module-path* (:port (value expr)))]
(fn []
(get *sim-state* (:port (value expr)))))
(throw (ex-info "Invalid :port-type" (value expr))))
(fn []
(apply my-sim-fn (map #(%) fn-vec)))))))
(defn push-down-map
"Takes a map and a keyword maps all values maps with the
key as the keyword and the value as the old value."
[m kw]
(plumb/for-map [[k v] m]
k {kw v}))
;;This stores the path of the current module--used for outputing hierarchical data
(def ^:dynamic *current-module* [])
;;This is an atom containing a map of state element paths to a map
;;of their sim fns (:fn) and initial values (:init)
(def ^:dynamic *state-elements*)
;TODO: automatically cast the return values of fnks
;that are being assigned to regs
(defn modulize
([computation state]
(assert (map? computation)
"You forgot to include the register map")
(modulize
(-<> (RuntimeException.)
.getStackTrace
^java.lang.StackTraceElement (aget 2)
.getLineNumber
(str "module_" <>)
keyword
)
computation state))
([module-name computation state]
(doseq [[k v] state
:when (not= (kindof v) :array)]
(assert (computation k)
(str "Missing register definition for " k)))
^::module
(fn [& inputs]
(assert (every? keyword? (take-nth 2 inputs)))
(binding [*current-module* (conj *current-module*
module-name)]
(let [state-renames (plumb/for-map [k (keys state)]
k (keyword (name (gensym))))
reverse-renames (map-invert state-renames)
renamed-computation
(plumb/map-keys #(or (state-renames %) %)
computation)
init-map (push-down-map state ::init)
register-ports (plumb/for-map [[k v] state]
k (make-port*
(conj *current-module* k)
(typeof v)
:register))
port-map (push-down-map register-ports ::port)
result (-<>> (graph/run
renamed-computation
(if (seq inputs)
(apply assoc register-ports inputs)
register-ports))
(plumb/map-keys
#(or (reverse-renames %) %)))
result-fns (plumb/for-map [[k v] result
:when (and (typeof v)
(not= (-> v value :op) :array-store))]
k v)
store-fns (plumb/for-map [[k v] result
:when (= (-> v value :op) :array-store)
:let [{:keys [array index write-enable v]}
(-> v value :args)]]
k {::index index
::write-enable? write-enable
::value v
::dest array})
fn-map (push-down-map result-fns ::fn)
state-elements (->> (merge-with
merge
init-map
fn-map
store-fns
port-map)
(plumb/map-keys
#(conj *current-module* %)))]
(when (bound? #'*state-elements*)
(swap! *state-elements* merge state-elements))
;We actually want to refer to registers, not their inputs,
;in this map
(merge result-fns register-ports))))))
(defn store?
"Returns true if the given map represents a store ast node"
[value]
(contains? value ::write-enable?))
(defn register?
"Returns true if the given map represents a register ast node"
[value]
(contains? value ::port))
(defn wire?
"Returns true if the given map represents a wire ast node"
[value]
(not (or (store? value) (register? value))))
(defn compile-root
[module & inputs]
(assert (::module (meta module)) "Must pass a module as first argument")
(binding [*state-elements* (atom {})]
(apply module inputs)
(with-meta @*state-elements*
{::compiled true})))
(defn find-exprs
[compiled-module pred]
(apply concat
(map #(-> % second
::fn
(walk-expr (fn [expr]
(if (pred expr)
[expr]))
concat))
compiled-module)))
(defn find-inputs
[compiled-module]
(find-exprs compiled-module
#(= :input (-> % value :port-type))))
(def ^{:arglists (list '[name type])}
input #(make-port* %1 %2 :input))
(defn make-port->mem-name
"Takes a module and return a map from port to
the memory's keyword name."
[compiled-module]
(let [mems (->> compiled-module
(filter #(-> % second ::init kindof
(= :array))))]
(plumb/for-map [[name {port ::port}] mems]
port name)))
(defn compute-store-fns
"Given a map of memory registers and a list of store ops,
make a map from memory names to their simulation-store fns."
[registers stores]
;;TODO: check that registers is a subset of arrays in stores
(let [stores (map (fn [{dest ::dest :as reg}]
(assoc reg
::dest
(-> dest value :port)))
stores)
reg->stores (group-by ::dest stores)]
(plumb/for-map [[reg {f ::fn}] registers
:let [stores (->> (reg->stores reg)
(map (comp
(partial map make-sim-fn)
(juxt ::index
::write-enable?
::value))))
f (make-sim-fn f)]]
reg (fn []
(reduce (fn [mem [i we v]]
(if (we)
(assoc mem (i) (v))
mem))
(*sim-state* reg)
stores)))))
(defn module-keys-by-type
"Takes a compiled module and returns a map containing
reg-keys, store-keys, and wire-keys. These are useful
to compilers, as they're the 3 kinds of runnable code."
[compiled-module]
(let [reg-keys (->> compiled-module
(filter (comp register? second))
(map first))
store-keys (->> compiled-module
(filter (comp store? second))
(map first))
wire-keys (->> compiled-module
(filter (comp wire? second))
(map first))]
{:reg-keys reg-keys
:store-keys store-keys
:wire-keys wire-keys}))
(defn sim
[compiled-module cycles]
(assert (::compiled (meta compiled-module))
"Module must be compiled")
(assert (empty? (find-inputs compiled-module))
"Cannot have any input ports during simulation")
(let [{:keys [reg-keys store-keys wire-keys]} (module-keys-by-type compiled-module)
wire-fns (plumb/map-vals (comp make-sim-fn ::fn)
(select-keys compiled-module wire-keys))
reg-fns (plumb/for-map [[k {f ::fn}] (select-keys compiled-module reg-keys)
:when f]
k (make-sim-fn f))
store-fns (compute-store-fns (select-keys compiled-module reg-keys)
(vals (select-keys compiled-module store-keys)))
port->mem-name (make-port->mem-name compiled-module)
reg-inits (plumb/map-vals
::init
(select-keys compiled-module reg-keys))
inits (binding [*sim-state* reg-inits]
(merge reg-inits
(plumb/map-vals #(%) wire-fns)))]
(loop [state inits
cycles cycles
history [inits]]
(if (zero? cycles)
history
(let [reg-state (binding [*sim-state* state]
(merge state
(plumb/map-vals #(%) store-fns)
(plumb/map-vals #(%) reg-fns)
))
wire-state (binding [*sim-state* reg-state]
(merge reg-state
(plumb/map-vals #(%) wire-fns)))]
(recur
wire-state
(dec cycles)
(conj history wire-state)))))))