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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
;; what happens when our virtual machine starts up
(= initialization-fns* (queue))
(def reset ()
(each f (as cons initialization-fns*)
(f)))
(mac on-init body
`(enq (fn () ,@body)
initialization-fns*))
(mac init-fn (name . body)
`(enq (fn () (= (function* ',name) (convert-names:convert-braces ',body)))
initialization-fns*))
; things that a future assembler will need separate memory for:
; code; types; args channel
(def clear ()
(= types* (table))
(= memory* (table))
(= function* (table)))
(enq clear initialization-fns*)
(on-init
(= types* (obj
; Each type must be scalar or array, sum or product or primitive
type (obj size 1) ; implicitly scalar and primitive
type-address (obj size 1 address t elem 'type)
type-array (obj array t elem 'type)
type-array-address (obj size 1 address t elem 'type-array)
location (obj size 1 address t elem 'location) ; assume it points to an atom
integer (obj size 1)
boolean (obj size 1)
boolean-address (obj size 1 address t)
byte (obj size 1)
;? string (obj array t elem 'byte) ; inspired by Go
character (obj size 1) ; int32 like a Go rune
character-address (obj size 1 address t elem 'character)
string (obj size 1) ; temporary hack
; isolating function calls
scope (obj array t elem 'location) ; by convention index 0 points to outer scope
scope-address (obj size 1 address t elem 'scope)
; arrays consist of an integer length followed by the right number of elems
integer-array (obj array t elem 'integer)
integer-address (obj size 1 address t elem 'integer) ; pointer to int
; records consist of a series of elems, corresponding to a list of types
integer-boolean-pair (obj size 2 record t elems '(integer boolean))
integer-boolean-pair-address (obj size 1 address t elem 'integer-boolean-pair)
integer-boolean-pair-array (obj array t elem 'integer-boolean-pair)
integer-integer-pair (obj size 2 record t elems '(integer integer))
integer-point-pair (obj size 2 record t elems '(integer integer-integer-pair))
; tagged-values are the foundation of dynamic types
tagged-value (obj size 2 record t elems '(type location))
tagged-value-address (obj size 1 address t elem 'tagged-value)
; heterogeneous lists
list (obj size 2 record t elems '(tagged-value list-address))
list-address (obj size 1 address t elem 'list)
list-address-address (obj size 1 address t elem 'list-address)
; editor
line (obj array t elem 'character)
line-address (obj size 1 address t elem 'line)
line-address-address (obj size 1 address t elem 'line-address)
screen (obj array t elem 'line-address)
screen-address (obj size 1 address t elem 'screen)
)))
;; persisting and checking traces for each test
(= traces* (queue))
(= trace-dir* ".traces/")
(ensure-dir trace-dir*)
(= curr-trace-file* nil)
(on-init
(awhen curr-trace-file*
;? (prn "reset: " it)
(tofile (+ trace-dir* it)
(each (label trace) (as cons traces*)
(pr label ": " trace))))
(= curr-trace-file* nil)
(= traces* (queue)))
(def new-trace (filename)
;? (prn "new-trace " filename)
(= curr-trace-file* filename))
(= dump-trace* nil)
(def trace (label . args)
;? (prn "trace: " dump-trace*)
(when (and dump-trace* (~pos label dump-trace*!blacklist))
(apply prn label ": " args))
(enq (list label (apply tostring:prn args))
traces*))
(def check-trace-contents (msg expected-contents)
(unless (trace-contents-match expected-contents)
(prn "F - " msg)
(prn " trace contents")
(print-trace-contents-mismatch expected-contents)))
(def trace-contents-match (expected-contents)
(each (label msg) (as cons traces*)
(when (and expected-contents
(is label expected-contents.0.0)
(posmatch expected-contents.0.1 msg))
(pop expected-contents)))
(no expected-contents))
(def print-trace-contents-mismatch (expected-contents)
(each (label msg) (as cons traces*)
(whenlet (expected-label expected-msg) expected-contents.0
(if (and (is label expected-label)
(posmatch expected-msg msg))
(do (pr " * ")
(pop expected-contents))
(pr " "))
(pr label ": " msg)))
(prn " couldn't find")
(each (expected-label expected-msg) expected-contents
(prn " ! " expected-label ": " expected-msg)))
(def add-fns (fns)
(each (name . body) fns
(= function*.name (convert-braces body))))
;; running mu
(mac v (operand) ; for value
`(,operand 0))
(def metadata (operand)
cdr.operand)
(def ty (operand)
operand.1) ; assume type is always first bit of metadata, and it's always present
(def typeinfo (operand)
(types* ty.operand))
(def sz (operand)
(trace "sz" operand)
(if (is 'literal ty.operand)
'literal
(pos 'deref metadata.operand)
(do (assert typeinfo.operand!address "tried to deref non-address @operand")
(sz (list (m `(,(v operand) location))
typeinfo.operand!elem)))
(let-or it typeinfo.operand (err "no such type: @operand")
(if it!array
array-len.operand
it!size))))
(defextend sz (typename) (isa typename 'sym)
(or types*.typename!size
(err "type @typename doesn't have a size: " (tostring:pr types*.typename))))
(def addr (loc)
;? (trace "addr" loc)
(ret result v.loc
(unless (pos 'global metadata.loc)
(whenlet base rep.routine*!call-stack.0!default-scope
(if (< result memory*.base)
(++ result base)
(die "addr: no room for var @result"))))
(when (pos 'deref metadata.loc)
(zap memory* result))))
(def addrs (n sz)
(accum yield
(repeat sz
(yield n)
(++ n))))
(def m (loc) ; read memory, respecting metadata
(point return
(if (in ty.loc 'literal 'offset)
(return v.loc))
(when (is v.loc 'default-scope)
(return rep.routine*!call-stack.0!default-scope))
(trace "m" loc)
(assert (isa v.loc 'int) "addresses must be numeric (problem in convert-names?) @loc")
(if (is 1 sz.loc)
(memory* addr.loc)
:else
(annotate 'record
(map memory* (addrs addr.loc sz.loc))))))
(def setm (loc val) ; set memory, respecting metadata
(point return
(when (is v.loc 'default-scope)
(assert (is 1 sz.loc) "can't store compounds in default-scope @loc")
(= rep.routine*!call-stack.0!default-scope val)
(return))
(assert (isa v.loc 'int) "can't store to non-numeric address (problem in convert-names?)")
(trace "setm" loc " <= " val)
(let n sz.loc
(trace "setm" "size of " loc " is " n)
(assert n "setm: can't compute type of @loc")
(if (is 1 n)
(do (assert (~isa val 'record) "setm: record of size 1?! @val")
(= (memory* addr.loc) val))
(do (assert (isa val 'record) "setm: non-record of size >1?! @val")
(each (dest src) (zip (addrs addr.loc n)
(rep val))
(= (memory* dest) src)))))))
(def array-len (operand)
;? (prn operand)
;? (prn (memory* 1000))
(if typeinfo.operand!array
(m `(,v.operand integer))
(and typeinfo.operand!address (pos 'deref metadata.operand))
(array-len (m operand) typeinfo.operand!elem)
:else
(err "can't take len of non-array @operand")))
(def array-ref-addr (operand idx)
(assert typeinfo.operand!array "aref-addr: not an array @operand")
(unless (< -1 idx (array-len operand))
(die "aref-addr: out of bounds index @idx for @operand of size @array-len.operand"))
(withs (elem typeinfo.operand!elem
offset (+ 1 (* idx sz.elem)))
(+ v.operand offset)))
(def array-ref (operand idx)
(assert typeinfo.operand!array "aref: not an array @operand")
(unless (< -1 idx (array-len operand))
(die "aref: out of bounds index @idx for @operand of size @array-len.operand"))
(withs (elem typeinfo.operand!elem
offset (+ 1 (* idx sz.elem)))
(m `(,(+ v.operand offset) ,elem))))
; data structure: routine
; runtime state for a serial thread of execution
(def make-routine (fn-name)
(annotate 'routine (obj call-stack (list
(obj fn-name fn-name pc 0 caller-arg-idx 0)))))
(defextend empty (x) (isa x 'routine)
(no rep.x!call-stack))
(def stack (routine)
((rep routine) 'call-stack))
(mac push-stack (routine op)
`(push (obj fn-name ,op pc 0 caller-arg-idx 0)
((rep ,routine) 'call-stack)))
(mac pop-stack (routine)
`(pop ((rep ,routine) 'call-stack)))
(def top (routine)
stack.routine.0)
(def body (routine (o idx 0))
(function* stack.routine.idx!fn-name))
(mac pc (routine (o idx 0)) ; assignable
`((((rep ,routine) 'call-stack) ,idx) 'pc))
(mac caller-arg-idx (routine (o idx 0)) ; assignable
`((((rep ,routine) 'call-stack) ,idx) 'caller-arg-idx))
(= scheduling-interval* 500)
(def parse-instr (instr)
(iflet delim (pos '<- instr)
(list (cut instr 0 delim) ; oargs
(instr (+ delim 1)) ; op
(cut instr (+ delim 2))) ; args
(list nil instr.0 cdr.instr)))
(mac caller-args (routine) ; assignable
`((((rep ,routine) 'call-stack) 0) 'args))
(mac results (routine) ; assignable
`((((rep ,routine) 'call-stack) 0) 'results))
(on-init
(= running-routines* (queue))
(= completed-routines* (queue))
(= routine* nil)
(= abort-routine* (parameter nil)))
; like arc's 'point' but you can also call ((abort-routine*)) in nested calls
(mac routine-mark body
(w/uniq (g p)
`(ccc (fn (,g)
(parameterize abort-routine* (fn ((o ,p)) (,g ,p))
,@body)))))
(def run fn-names
(ret result 0
(each it fn-names
(enq make-routine.it running-routines*))
; simple round-robin scheduler
(while (~empty running-routines*)
(= routine* deq.running-routines*)
(trace "schedule" top.routine*!fn-name)
(whenlet insts-run (routine-mark:run-for-time-slice scheduling-interval*)
(= result (+ result insts-run)))
(if (~empty routine*)
(enq routine* running-routines*)
(enq-limit routine* completed-routines*)))))
(def die (msg)
(= rep.routine*!error msg)
(= rep.routine*!stack-trace rep.routine*!call-stack)
(wipe rep.routine*!call-stack)
((abort-routine*)))
($:require "charterm/main.rkt")
(def run-for-time-slice (time-slice)
;? (prn "AAA")
(point return
;? (prn "BBB")
(for ninstrs 0 (< ninstrs time-slice) (++ ninstrs)
;? (prn "CCC " pc.routine* " " routine* " " (len body.routine*))
(if (empty body.routine*) (err "@stack.routine*.0!fn-name not defined"))
(while (>= pc.routine* (len body.routine*))
(pop-stack routine*)
(if empty.routine* (return ninstrs))
(++ pc.routine*))
(trace "run" "-- " (sort (compare < string:car) (as cons memory*)))
(trace "run" top.routine*!fn-name " " pc.routine* ": " (body.routine* pc.routine*))
;? (trace "run" routine*)
(let (oarg op arg) (parse-instr (body.routine* pc.routine*))
;? (prn op " " arg " -> " oarg)
(let tmp
(case op
; arithmetic
add
(do (trace "add" (m arg.0) " " (m arg.1))
(+ (m arg.0) (m arg.1))
)
sub
(- (m arg.0) (m arg.1))
mul
(* (m arg.0) (m arg.1))
div
(/ (real (m arg.0)) (m arg.1))
idiv
(list (trunc:/ (m arg.0) (m arg.1))
(mod (m arg.0) (m arg.1)))
; boolean
and
(and (m arg.0) (m arg.1))
or
(or (m arg.0) (m arg.1))
not
(not (m arg.0))
; comparison
eq
(is (m arg.0) (m arg.1))
neq
(do (trace "neq" (m arg.0) " " (m arg.1))
(~is (m arg.0) (m arg.1))
)
lt
(< (m arg.0) (m arg.1))
gt
(> (m arg.0) (m arg.1))
le
(<= (m arg.0) (m arg.1))
ge
(>= (m arg.0) (m arg.1))
; control flow
jump
(do (= pc.routine* (+ 1 pc.routine* (v arg.0)))
;? (trace "jump" "jumping to " pc.routine*)
(continue))
jump-if
(when (is t (m arg.0))
(= pc.routine* (+ 1 pc.routine* (v arg.1)))
;? (trace "jump-if" "jumping to " pc.routine*)
(continue))
jump-unless ; convenient helper
(unless (is t (m arg.0))
(= pc.routine* (+ 1 pc.routine* (v arg.1)))
;? (trace "jump-unless" "jumping to " pc.routine*)
(continue))
; data management: scalars, arrays, records
copy
(m arg.0)
get
(with (base (addr arg.0) ; integer (non-symbol) memory location including metadata
basetype (typeinfo arg.0)
idx (v arg.1)) ; literal integer
(assert (is 'offset (ty arg.1)) "second arg to 'get' must have type 'offset' @arg.1")
(when (pos 'deref (metadata arg.0))
(assert basetype!address "get: base has deref but isn't an address @arg.0 @basetype")
(= basetype (types* basetype!elem)))
(assert basetype!record "get on invalid type @base")
(assert (< -1 idx (len basetype!elems)) "get: out-of-bounds access @idx vs @(len basetype!elems)")
(m `(,(+ base
(apply + (map sz
(firstn idx basetype!elems))))
,basetype!elems.idx
global)))
get-address
(with (base (addr arg.0) ; integer (non-symbol) memory location including metadata
basetype (typeinfo arg.0)
idx (v arg.1)) ; literal integer
(assert (is 'offset (ty arg.1)) "second arg to 'get-addr' must have type 'offset' @arg.1")
(when (pos 'deref (metadata arg.0))
(assert basetype!address "get-addr: base has deref but isn't an address @arg.0 @basetype")
(assert basetype!address)
(= basetype (types* basetype!elem)))
(assert basetype!record "get-addr on invalid type @base")
(assert (< -1 idx (len basetype!elems)) "get-addr: out-of-bounds access @idx vs @(len basetype!elems)")
(+ base
(apply + (map sz
(firstn idx basetype!elems)))))
index
(with (base arg.0 ; integer (non-symbol) memory location including metadata
idx (m arg.1))
;? (prn "processing index: @base @idx")
(when typeinfo.base!address
(assert (pos 'deref metadata.base) "index: array has deref but isn't an address @base")
(= base (list (memory* v.base) typeinfo.base!elem)))
;? (prn "after maybe deref: @base @idx")
;? (prn Memory-in-use-until ": " memory*)
(assert typeinfo.base!array "index on invalid type @arg.0 => @base")
(array-ref base idx))
index-address
(with (base arg.0
idx (m arg.1))
(when typeinfo.base!address
(assert (pos 'deref metadata.base) "index-addr: array has deref but isn't an address @base")
(= base (list (memory* v.base) typeinfo.base!elem)))
(assert typeinfo.base!array "index-addr on invalid type @arg.0 => @base")
(array-ref-addr base idx))
new
(let type (v arg.0)
(assert (is 'literal (ty arg.0)) "new: second arg @arg.0 must be literal")
(if (no types*.type) (err "no such type @type"))
(if types*.type!array
(new-array type (m arg.1))
(new-scalar type)))
sizeof
(sizeof (m arg.0))
len
(let base arg.0
(if typeinfo.base!array
array-len.base
-1))
; tagged-values require one primitive
save-type
(annotate 'record `(,(ty arg.0) ,(m arg.0)))
; multiprocessing
run
(run (v arg.0))
fork
(enq (make-routine (v arg.0)) running-routines*)
; todo: errors should stall a process and let its parent
; inspect it
assert
(assert (m arg.0))
; text interaction
cls
(do1 nil ($.charterm-clear-screen))
cll
(do1 nil ($.charterm-clear-line))
cursor
(do1 nil ($.charterm-cursor (m arg.0) (m arg.1)))
print-primitive
(do1 nil ((if ($.current-charterm) $.charterm-display pr) (m arg.0)))
getc
(and ($.charterm-byte-ready?) ($.charterm-read-key))
bold-mode
(do1 nil ($.charterm-bold))
non-bold-mode
(do1 nil ($.charterm-normal))
console-on
(do1 nil (if (no ($.current-charterm)) ($.open-charterm)))
console-off
(do1 nil (if ($.current-charterm) ($.close-charterm)))
; user-defined functions
arg
(let idx (if arg
arg.0
(do1 caller-arg-idx.routine*
(++ caller-arg-idx.routine*)))
(trace "arg" arg " " idx " " caller-args.routine*)
(if (len> caller-args.routine* idx)
(list caller-args.routine*.idx t)
(list nil nil)))
prepare-reply
(prepare-reply arg)
reply
(do (when arg
(prepare-reply arg))
(let results results.routine*
(pop-stack routine*)
(if empty.routine* (return ninstrs))
(let (caller-oargs _ _) (parse-instr (body.routine* pc.routine*))
(trace "reply" arg " " caller-oargs)
(each (dest val) (zip caller-oargs results)
(trace "reply" val " => " dest)
(setm dest val)))
(++ pc.routine*)
(while (>= pc.routine* (len body.routine*))
(pop-stack routine*)
(when empty.routine* (return ninstrs))
(++ pc.routine*))
(continue)))
; else try to call as a user-defined function
(do (if function*.op
(let callee-args (accum yield
(each a arg
(yield (m a))))
(push-stack routine* op)
(= caller-args.routine* callee-args))
(err "no such op @op"))
(continue))
)
; opcode generated some value, stored in 'tmp'
; copy to output args
;? (prn "store: " tmp " " oarg)
(if (acons tmp)
(for i 0 (< i (min len.tmp len.oarg)) ++.i
(setm oarg.i tmp.i))
(when oarg ; must be a list
(trace "run" "writing to oarg " tmp " => " oarg.0)
(setm oarg.0 tmp)))
)
(++ pc.routine*)))
(return time-slice)))
(def prepare-reply (args)
(= results.routine*
(accum yield
(each a args
(yield (m a))))))
(enq (fn () (= Memory-in-use-until 1000))
initialization-fns*)
(def new-scalar (type)
(ret result Memory-in-use-until
(++ Memory-in-use-until sizeof.type)))
(def new-array (type size)
;? (prn "new array: @type @size")
(ret result Memory-in-use-until
(++ Memory-in-use-until (+ 1 (* (sizeof types*.type!elem) size)))
(= (memory* result) size)))
(def sizeof (type)
(trace "sizeof" type)
(if (~or types*.type!record types*.type!array)
types*.type!size
types*.type!record
(sum idfn
(accum yield
(each elem types*.type!elems
(yield sizeof.elem))))
:else
(err "no such type @type")))
;; desugar structured assembly based on blocks
(def convert-braces (instrs)
(let locs () ; list of information on each brace: (open/close pc)
(let pc 0
(loop (instrs instrs)
(each instr instrs
(if (~is 'begin instr.0)
(do
(trace "cvt0" pc " " instr " -- " locs)
(++ pc))
; hack: racket replaces curlies with parens, so we need the
; keyword begin to delimit blocks.
; ultimately there'll be no nesting and curlies will just be in a
; line by themselves.
(do
;? (prn `(open ,pc))
(push `(open ,pc) locs)
(recur cdr.instr)
;? (prn `(close ,pc))
(push `(close ,pc) locs))))))
(zap rev locs)
;? (prn locs)
(with (pc 0
stack ()) ; elems are pcs
(accum yield
(loop (instrs instrs)
(each instr instrs
(point continue
(let delim (or (pos '<- instr) -1)
(with (oarg (if (>= delim 0)
(cut instr 0 delim))
op (instr (+ delim 1))
arg (cut instr (+ delim 2)))
(trace "cvt1" pc " " op " " oarg)
(case op
begin
(do
(push pc stack)
(assert (is oarg nil) "begin: can't take oarg @instr")
(recur arg)
(pop stack)
(continue))
break
(do
(assert (is oarg nil) "break: can't take oarg @instr")
(assert (is arg nil) "break: can't take arg @instr")
(yield `(jump (,(close-offset pc locs) offset))))
break-if
(do
;? (prn "break-if: " instr)
(assert (is oarg nil) "break-if: can't take oarg @instr")
(yield `(jump-if ,arg.0 (,(close-offset pc locs) offset))))
break-unless
(do
;? (prn "break-if: " instr)
(assert (is oarg nil) "break-unless: can't take oarg @instr")
(yield `(jump-unless ,arg.0 (,(close-offset pc locs) offset))))
continue
(do
(assert (is oarg nil) "continue: can't take oarg @instr")
(assert (is arg nil) "continue: can't take arg @instr")
(yield `(jump (,(- stack.0 1 pc) offset))))
continue-if
(do
(trace "cvt0" "continue-if: " instr " => " (- stack.0 1))
(assert (is oarg nil) "continue-if: can't take oarg @instr")
(yield `(jump-if ,arg.0 (,(- stack.0 1 pc) offset))))
continue-unless
(do
(trace "cvt0" "continue-if: " instr " => " (- stack.0 1))
(assert (is oarg nil) "continue-unless: can't take oarg @instr")
(yield `(jump-unless ,arg.0 (,(- stack.0 1 pc) offset))))
;else
(yield instr))))
(++ pc))))))))
(def close-offset (pc locs)
(let close 0
(with (stacksize 0
done nil)
(each (state loc) locs
;? (prn " :" close " " state " - " loc)
(if (< loc pc)
nil ; do nothing
(no done)
(do
; first time
(when (and (is 0 stacksize) (~is loc pc))
(++ stacksize))
(if (is 'open state) (++ stacksize) (-- stacksize))
; last time
(when (is 0 stacksize)
(= close loc)
(set done))))))
(- close pc 1)))
;; convert symbolic names to integer offsets
(def convert-names (instrs)
(let offset (table)
(let idx 1
(each instr instrs
(let (oargs op args) (parse-instr instr)
(each arg args
(when (maybe-add arg offset idx)
(err "use before set: @arg")
(++ idx)))
(each arg oargs
(when (maybe-add arg offset idx)
(++ idx))))))
(each instr instrs
(let (oargs op args) (parse-instr instr)
(each arg args
(when (offset v.arg)
(zap offset v.arg)))
(each arg oargs
(when (offset v.arg)
(zap offset v.arg)))))
instrs))
(def maybe-add (arg offset idx)
(unless (or (in ty.arg 'literal 'offset)
(offset v.arg)
(~isa v.arg 'sym)
(in v.arg 'nil 'default-scope)
(pos 'global metadata.arg))
(= (offset v.arg) idx)))
;; literate tangling system for reordering code
(def convert-quotes (instrs)
(let deferred (queue)
(each instr instrs
(case instr.0
defer
(let (q qinstrs) instr.1
(assert (is 'make-br-fn q) "defer: first arg must be [quoted]")
(each qinstr qinstrs
(enq qinstr deferred)))))
(accum yield
(each instr instrs
(unless (in instr.0 'defer) ; keep sync'd with case clauses above
(yield instr)))
(each instr (as cons deferred)
(yield instr)))))
;; system software
(init-fn maybe-coerce
((default-scope scope-address) <- new (scope literal) (30 literal))
((x tagged-value-address) <- new (tagged-value literal))
((x tagged-value-address deref) <- arg)
((p type) <- arg)
((xtype type) <- get (x tagged-value-address deref) (0 offset))
((match? boolean) <- eq (xtype type) (p type))
{ begin
(break-if (match? boolean))
(reply (0 literal) (nil literal))
}
((xvalue location) <- get (x tagged-value-address deref) (1 offset))
(reply (xvalue location) (match? boolean)))
(init-fn new-tagged-value
((default-scope scope-address) <- new (scope literal) (30 literal))
((xtype type) <- arg)
((xtypesize integer) <- sizeof (xtype type))
((xcheck boolean) <- eq (xtypesize integer) (1 literal))
(assert (xcheck boolean))
; todo: check that arg 0 matches the type? or is that for the future typechecker?
((result tagged-value-address) <- new (tagged-value literal))
((resulttype location) <- get-address (result tagged-value-address deref) (0 offset))
((resulttype location deref) <- copy (xtype type))
((locaddr location) <- get-address (result tagged-value-address deref) (1 offset))
((locaddr location deref) <- arg)
(reply (result tagged-value-address)))
(init-fn list-next ; list-address -> list-address
((default-scope scope-address) <- new (scope literal) (30 literal))
((base list-address) <- arg)
((result list-address) <- get (base list-address deref) (1 offset))
(reply (result list-address)))
(init-fn list-value-address ; list-address -> tagged-value-address
((default-scope scope-address) <- new (scope literal) (30 literal))
((base list-address) <- arg)
((result tagged-value-address) <- get-address (base list-address deref) (0 offset))
(reply (result tagged-value-address)))
(init-fn new-list
((default-scope scope-address) <- new (scope literal) (30 literal))
((new-list-result list-address) <- new (list literal))
((curr list-address) <- copy (new-list-result list-address))
{ begin
((curr-value integer) (exists? boolean) <- arg)
(break-unless (exists? boolean))
((next list-address-address) <- get-address (curr list-address deref) (1 offset))
((next list-address-address deref) <- new (list literal))
((curr list-address) <- list-next (curr list-address))
((dest tagged-value-address) <- list-value-address (curr list-address))
((dest tagged-value-address deref) <- save-type (curr-value integer))
(continue)
}
((new-list-result list-address) <- list-next (new-list-result list-address)) ; memory leak
(reply (new-list-result list-address)))
; drop all traces while processing above functions
(on-init
(= traces* (queue)))
(def prn2 (msg . args)
(pr msg)
(apply prn args))
;; after loading all files, start at 'main'
(reset)
(awhen cdr.argv
(map add-fns:readfile it)
(run 'main)
(if ($.current-charterm) ($.close-charterm))
(prn memory*))
|