-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstaaplc.rkt
299 lines (219 loc) · 7.71 KB
/
staaplc.rkt
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
#!/usr/bin/env racket
#lang racket/base
;; The 'Staapl Compiler' command line tool.
;;
;; Provides a standard command line interface to all (compilation)
;; functionality. It is invoked as:
;;
;; racket -l staapl/staaplc -- <arg> ...
;; The dictionary file produced is a mzscheme module that when invoked
;; produces a REPL to a live target. This makes it possible to lift
;; any kind of behaviour from project source files to actual REPL
;; behaviour.
;; However, it can be interpreted as a data file by observing the
;; following structure:
;; * The first line contains the PLT Scheme #lang construct and can
;; be skipped using the 'read-line function.
;; * The first scheme form accessible using 'read is a 'begin form
;; containing only data definitions (define <name> (quote <datum>))
;; * The second form accessible through 'read contains a 'require
;; statement which imports the bindings for the target language in
;; which the project is written, and interaction code.
;; * Subsequent forms contain opaque scheme code necessary to
;; configure and optionally start the console command interpreter
;; using the data provided in the previous forms.
(require scheme/pretty ;; racket/pretty prints quotes!
racket/cmdline
staapl/tools
)
(define-syntax-rule (flags: name ...)
(begin (define name (make-parameter #f)) ...))
(flags: output-hex
output-dict
output-code
console
device
baud
filename
output-asm
debug-script
dict-suffix
debug-suffix
version-tag
live-module
dtc-enable
)
;; Defaults
; (device "/dev/staapl0")
(baud #f)
(dict-suffix ".dict")
(debug-suffix ".rkt")
(live-module #f)
(define (get-arguments)
(filename
(command-line
#:program "staaplc"
#:once-each
[("-o" "--output-hex") filename "Output Intel HEX file."
(output-hex filename)]
[("--version-tag") filename "Version tag."
(version-tag filename)]
[("--live-module") module "Live interaction module."
(live-module (string->symbol module))]
[("--output-code") filename "Output s-expression code dump."
(output-code filename)]
[("--output-asm") filename "Output assembly code."
(output-asm filename)]
[("-c" "--comm") filename "Console port. (default: pickit2)" (console (string->symbol filename))]
[("-d" "--device") filename "Console system device. (default: /dev/staapl0)" (device filename)]
[("--baud") number "Console baud rate. (default from source file)" (baud (string->number number))]
[("-d" "--output-dict") filename "Output dictionary file."
(output-dict filename)]
#:args (fname)
fname)))
(define (out param template suffix)
(let ((p (param)))
(unless p
(param
(let-values (((base name _) (split-path template)))
(path-replace-suffix name suffix))))))
;; (*) The extension .ss is too confusing. This is a generated file,
;; which should be removable by a simple rm *.<ext> in a Makefile.
(define (absolute param)
(let ((p (param)))
(param
(if (absolute-path? p)
(string->path p)
(path->complete-path p)))))
(define (dir-of path)
(let-values (((base name _) (split-path path)))
base))
(define (requirements-compile kernel-path)
(let ((forms
`((file ,(path->string kernel-path))
staapl/live)))
forms))
(define (requirements-live kernel-path)
(let ((forms
(append
(requirements-compile kernel-path)
`(;; FIXME: if kernel doesn't include dtc.rkt live-pic18-dtc doesn't
;; seem to pull in code properly.
,(live-module)
readline/rep
))))
forms))
(define (library)
(case (live-module)
((staapl/pic18/live
staapl/pic18/live-dtc) `(library "pic18"))
((staapl/arm/live) `(library "arm"))
(else '())))
(define (process-arguments)
(out output-hex (filename) ".hex")
(out output-dict (filename) (dict-suffix)) ;; (*)
(out debug-script (filename) (debug-suffix))
;; Why do these need to be absolute?
(absolute filename)
(absolute output-hex)
(absolute output-dict)
(absolute debug-script)
)
(define (warnf . args)
(display "WARNING: ")
(apply printf args))
(define (spec-from-source param id)
(unless (param)
(let ((v (eval `(macro-constant ',id))))
(printf "~a = ~a\n" id v)
(when v
(param v)))))
;; Figure out console config
(define (console-spec)
;; Unless overridden by command line arguments, get the console
;; specs from the Forth source files.
(define (device-string x)
(if (symbol? x) (symbol->string x) x))
`(begin
(define console-device (or (command-line-tty) ,(device-string (device))))
(console ',(console) console-device ,(baud))))
;; formatting
(define (save text [code #f])
(when text
(display text)
(newline))
(when code
(pretty-print code)))
(define (instantiate-and-save)
;;(printf "in: ~a\n" (filename))
;;(printf "out: ~a ~a\n" (output-hex) (output-dict))
(unless (file-exists? (filename))
(printf "input file not found: ~a\n" (filename))
(exit 1))
(parameterize
((current-namespace
(make-base-namespace)))
;; Load necessary code.
(eval `(require ,@(requirements-compile (filename))))
;; Get config from source macros if not defined.
(spec-from-source console 'console-type)
(spec-from-source device 'console-device)
(spec-from-source baud 'console-baud)
(spec-from-source live-module 'live-module)
;; Load live libray for target
(eval `(require ,@(requirements-live (filename))))
;; Optionally print assembler code.
(when (output-asm)
(with-output-to-file/safe
(output-asm)
(lambda () (eval '(code-print)))))
;; Save binary output.
(with-output-to-file/safe
(output-hex)
(lambda () (eval '(write-ihex (code->binary)))))
;; Dump binary code chunks in hex format.
(when (output-code)
(with-output-to-file/safe
(output-code)
(lambda () (eval '(write (code->binary))))))
;; Save interaction script.
(let* ((reqs `(require ,@(requirements-live (filename))))
(boot-run
`(begin
(define version-tag ',(version-tag))
(define-namespace-anchor anchor)
,(console-spec)
(forth-begin-prefix ',(library))
(empty/run
',(requirements-live (filename))
anchor)))
;; FIXME: no longer supported
;; Load host debug script into toplevel namespace on
;; a clean target.
;; (when-file ',(path->string (debug-script)) load)))))
(save-module
(lambda ()
(save "#!/usr/bin/env racket")
(save "#lang scheme/load") ;; (2)
(save ";; Language" reqs)
(save ";; Console" boot-run))))
(with-output-to-file/safe
(output-dict)
save-module))))
;; (1) Saving the addresses is not necessary if source code and target
;; are kept in sync. When the interactive script is started,
;; everything will be simply re-compiled. However, to enable a
;; scenario where code has changed internally, but the procedure
;; _interface_ hasn't, we save the addresses as they are on the
;; target.
;; (2) For interactive development we're using a toplevel namespace
;; instead of a (static) module namespace.
;; Toplevel actions
(define (main)
(get-arguments)
(process-arguments)
(instantiate-and-save))
;; TESTING
;(current-command-line-arguments
; (vector "-m" "/home/tom/staapl/app/1220-8.fm"))
(main)