1 ; Mu: An exploration on making the global structure of programs more accessible.
3 ; "Is it a language, or an operating system, or a virtual machine? Mu."
4 ; (with apologies to Robert Pirsig: http://en.wikipedia.org/wiki/Mu_%28negative%29#In_popular_culture)
8 ; I want to live in a world where I can have an itch to tweak a program, clone
9 ; its open-source repository, orient myself on how it's organized, and make
10 ; the simple change I envisioned, all in an afternoon. This codebase tries to
11 ; make this possible for its readers. (More details: http://akkartik.name/about)
13 ; What helps comprehend the global structure of programs? For starters, let's
14 ; enumerate what doesn't: idiomatic code, adherence to a style guide or naming
15 ; convention, consistent indentation, API documentation for each class, etc.
16 ; These conventional considerations improve matters in the small, but don't
17 ; help understand global organization. They help existing programmers manage
18 ; day-to-day operations, but they can't turn outsider programmers into
19 ; insiders. (Elaboration: http://akkartik.name/post/readable-bad)
21 ; In my experience, two things have improved matters so far: version control
22 ; and automated tests. Version control lets me rewind back to earlier, simpler
23 ; times when the codebase was simpler, when its core skeleton was easier to
24 ; ascertain. Indeed, arguably what came first is by definition the skeleton of
25 ; a program, modulo major rewrites. Once you understand the skeleton, it
26 ; becomes tractable to 'play back' later major features one by one. (Previous
27 ; project that fleshed out this idea: http://akkartik.name/post/wart-layers)
29 ; The second and biggest boost to comprehension comes from tests. Tests are
30 ; good for writers for well-understood reasons: they avoid regressions, and
31 ; they can influence code to be more decoupled and easier to change. In
32 ; addition, tests are also good for the outsider reader because they permit
33 ; active reading. If you can't build a program and run its tests it can't help
34 ; you understand it. It hangs limp at best, and might even be actively
35 ; misleading. If you can run its tests, however, it comes alive. You can step
36 ; through scenarios in a debugger. You can add logging and scan logs to make
37 ; sense of them. You can run what-if scenarios: "why is this line not written
38 ; like this?" Make a change, rerun tests: "Oh, that's why." (Elaboration:
39 ; http://akkartik.name/post/literate-programming)
41 ; However, tests are only useful to the extent that they exist. Think back to
42 ; your most recent codebase. Do you feel comfortable releasing a new version
43 ; just because the tests pass? I'm not aware of any such project. There's just
44 ; too many situations envisaged by the authors that were never encoded in a
45 ; test. Even disciplined authors can't test for performance or race conditions
46 ; or fault tolerance. If a line is phrased just so because of some subtle
47 ; performance consideration, it's hard to communicate to newcomers.
49 ; This isn't an arcane problem, and it isn't just a matter of altruism. As
50 ; more and more such implicit considerations proliferate, and as the original
51 ; authors are replaced by latecomers for day-to-day operations, knowledge is
52 ; actively forgotten and lost. The once-pristine codebase turns into legacy
53 ; code that is hard to modify without expensive and stress-inducing
56 ; How to write tests for performance, fault tolerance, race conditions, etc.?
57 ; How can we state and verify that a codepath doesn't ever perform memory
58 ; allocation, or write to disk? It requires better, more observable primitives
59 ; than we currently have. Modern operating systems have their roots in the
60 ; 70s. Their interfaces were not designed to be testable. They provide no way
61 ; to simulate a full disk, or a specific sequence of writes from different
62 ; threads. We need something better.
64 ; This project tries to move, groping, towards that 'something better', a
65 ; platform that is both thoroughly tested and allows programs written for it
66 ; to be thoroughly tested. It tries to answer the question:
68 ; If Denis Ritchie and Ken Thompson were to set out today to co-design unix
69 ; and C, knowing what we know about automated tests, what would they do
72 ; To try to impose *some* constraints on this gigantic yak-shave, we'll try to
73 ; keep both language and OS as simple as possible, focused entirely on
74 ; permitting more kinds of tests, on first *collecting* all the information
75 ; about implicit considerations in some form so that readers and tools can
76 ; have at least some hope of making sense of it.
78 ; The initial language will be just assembly. We'll try to make it convenient
79 ; to program in with some simple localized rewrite rules inspired by lisp
80 ; macros and literate programming. Programmers will have to do their own
81 ; memory management and register allocation, but we'll provide libraries to
84 ; The initial OS will provide just memory management and concurrency
85 ; primitives. No users or permissions (we don't live on mainframes anymore),
86 ; no kernel- vs user-mode, no virtual memory or process abstraction, all
87 ; threads sharing a single address space (use VMs for security and
88 ; sandboxing). The only use case we care about is getting a test harness to
89 ; run some code, feed it data through blocking channels, stop it and observe
90 ; its internals. The code under test is expected to cooperate in such testing,
91 ; by logging important events for the test harness to observe. (More info:
92 ; http://akkartik.name/post/tracing-tests)
94 ; The common thread here is elimination of abstractions, and it's not an
95 ; accident. Abstractions help insiders manage the evolution of a codebase, but
96 ; they actively hinder outsiders in understanding it from scratch. This
97 ; matters, because the funnel to turn outsiders into insiders is critical to
98 ; the long-term life of a codebase. Perhaps authors should raise their
99 ; estimation of the costs of abstraction, and go against their instincts for
100 ; introducing it. That's what I'll be trying to do: question every abstraction
101 ; before I introduce it. We'll see how it goes.
107 ; Mu is currently built atop Racket and Arc, but this is temporary and
108 ; contingent. We want to keep our options open, whether to port to a different
109 ; host language, and easy to rewrite to native code for any platform. So we'll
110 ; try to avoid 'cheating': relying on the host platform for advanced
113 ; Other than that, we'll say no more about the code, and focus in the rest of
114 ; this file on the scenarios the code cares about.
116 (selective-load "mu.arc" section-level)
117 (ero "running tests in mu.ar.c.t (takes ~30s)")
120 (set allow-raw-addresses*)
124 ; Our language is assembly-like in that functions consist of series of
125 ; statements, and statements consist of an operation and its arguments (input
128 ; oarg1, oarg2, ... <- op arg1, arg2, ...
130 ; Args must be atomic, like an integer or a memory address, they can't be
131 ; expressions doing arithmetic or function calls. But we can have any number
134 ; Since we're building on lisp, our code samples won't look quite like the
135 ; idealized syntax above. For now they will look like this:
138 ; (oarg1 oarg2 ... <- op arg1 arg2 ...)
143 ; Each arg/oarg can contain metadata separated by slashes and colons. In this
144 ; first example below, the only metadata is types: 'integer' for a memory
145 ; location containing an integer, and 'literal' for a value included directly
146 ; in code. (Assembly languages traditionally call them 'immediate' operands.)
147 ; In the future a simple tool will check that the types line up as expected in
148 ; each op. A different tool might add types where they aren't provided.
149 ; Instead of a monolithic compiler I want to build simple, lightweight tools
150 ; that can be combined in various ways, say for using different typecheckers
151 ; in different subsystems.
153 ; In our tests we'll define such mu functions using a call to 'add-code', so
154 ; look for it when reading the code examples. Everything outside 'add-code' is
155 ; just test-harness details that can be skipped at first.
159 (new-trace "literal")
162 (1:integer <- copy 23:literal)
167 (when (~is memory*.1 23)
168 (prn "F - 'copy' writes its lone 'arg' after the instruction name to its lone 'oarg' or output arg before the arrow. After this test, the value 23 is stored in memory address 1."))
172 ; Our basic arithmetic ops can operate on memory locations or literals.
173 ; (Ignore hardware details like registers for now.)
179 (1:integer <- copy 1:literal)
180 (2:integer <- copy 3:literal)
181 (3:integer <- add 1:integer 2:integer)
185 (when (~iso memory* (obj 1 1 2 3 3 4))
186 (prn "F - 'add' operates on two addresses"))
191 (new-trace "add-literal")
194 (1:integer <- add 2:literal 3:literal)
197 (when (~is memory*.1 5)
198 (prn "F - ops can take 'literal' operands (but not return them)"))
201 (new-trace "sub-literal")
204 (1:integer <- subtract 1:literal 3:literal)
208 (when (~is memory*.1 -2)
209 (prn "F - 'subtract'"))
212 (new-trace "mul-literal")
215 (1:integer <- multiply 2:literal 3:literal)
219 (when (~is memory*.1 6)
220 (prn "F - 'multiply'"))
223 (new-trace "div-literal")
226 (1:integer <- divide 8:literal 3:literal)
230 (when (~is memory*.1 (/ real.8 3))
231 (prn "F - 'divide'"))
234 (new-trace "idiv-literal")
237 (1:integer 2:integer <- divide-with-remainder 23:literal 6:literal)
241 (when (~iso memory* (obj 1 3 2 5))
242 (prn "F - 'divide-with-remainder' performs integer division"))
245 (new-trace "dummy-oarg")
249 (_ 2:integer <- divide-with-remainder 23:literal 6:literal)
252 (when (~iso memory* (obj 2 5))
253 (prn "F - '_' oarg can ignore some results"))
256 ; Basic boolean operations: and, or, not
257 ; There are easy ways to encode booleans in binary, but we'll skip past those
261 (new-trace "and-literal")
264 (1:boolean <- and t:literal nil:literal)
269 (when (~is memory*.1 nil)
270 (prn "F - logical 'and' for booleans"))
272 ; Basic comparison operations
275 (new-trace "lt-literal")
278 (1:boolean <- less-than 4:literal 3:literal)
282 (when (~is memory*.1 nil)
283 (prn "F - 'less-than' inequality operator"))
286 (new-trace "le-literal-false")
289 (1:boolean <- lesser-or-equal 4:literal 3:literal)
293 (when (~is memory*.1 nil)
294 (prn "F - 'lesser-or-equal'"))
297 (new-trace "le-literal-true")
300 (1:boolean <- lesser-or-equal 4:literal 4:literal)
304 (when (~is memory*.1 t)
305 (prn "F - 'lesser-or-equal' returns true for equal operands"))
308 (new-trace "le-literal-true-2")
311 (1:boolean <- lesser-or-equal 4:literal 5:literal)
315 (when (~is memory*.1 t)
316 (prn "F - 'lesser-or-equal' - 2"))
318 ; Control flow operations: jump, jump-if, jump-unless
319 ; These introduce a new type -- 'offset' -- for literals that refer to memory
320 ; locations relative to the current location.
323 (new-trace "jump-skip")
326 (1:integer <- copy 8:literal)
328 (2:integer <- copy 3:literal) ; should be skipped
334 (when (~iso memory* (obj 1 8))
335 (prn "F - 'jump' skips some instructions"))
339 (new-trace "jump-target")
342 (1:integer <- copy 8:literal)
344 (2:integer <- copy 3:literal) ; should be skipped
346 (3:integer <- copy 34:literal)
350 (when (~iso memory* (obj 1 8))
351 (prn "F - 'jump' doesn't skip too many instructions"))
355 (new-trace "jump-if-skip")
358 (2:integer <- copy 1:literal)
359 (1:boolean <- equal 1:literal 2:integer)
360 (jump-if 1:boolean 1:offset)
361 (2:integer <- copy 3:literal)
363 (3:integer <- copy 34:literal)
367 (when (~iso memory* (obj 1 t 2 1))
368 (prn "F - 'jump-if' is a conditional 'jump'"))
371 (new-trace "jump-if-fallthrough")
374 (1:boolean <- equal 1:literal 2:literal)
375 (jump-if 3:boolean 1:offset)
376 (2:integer <- copy 3:literal)
378 (3:integer <- copy 34:literal)
382 (when (~iso memory* (obj 1 nil 2 3))
383 (prn "F - if 'jump-if's first arg is false, it doesn't skip any instructions"))
386 (new-trace "jump-if-backward")
389 (1:integer <- copy 2:literal)
390 (2:integer <- copy 1:literal)
392 (2:integer <- add 2:integer 2:integer)
393 (3:boolean <- equal 1:integer 2:integer)
394 (jump-if 3:boolean -3:offset) ; to loop
395 (4:integer <- copy 3:literal)
397 (3:integer <- copy 34:literal)
401 (when (~iso memory* (obj 1 2 2 4 3 nil 4 3))
402 (prn "F - 'jump-if' can take a negative offset to make backward jumps"))
405 (new-trace "jump-label")
408 (1:integer <- copy 2:literal)
409 (2:integer <- copy 1:literal)
411 (2:integer <- add 2:integer 2:integer)
412 (3:boolean <- equal 1:integer 2:integer)
413 (jump-if 3:boolean loop:offset)
414 (4:integer <- copy 3:literal)
416 (3:integer <- copy 34:literal)
419 ;? (= dump-trace* (obj whitelist '("-")))
422 (when (~iso memory* (obj 1 2 2 4 3 nil 4 3))
423 (prn "F - 'jump-if' can take a negative offset to make backward jumps"))
426 ; Data movement relies on addressing modes:
427 ; 'direct' - refers to a memory location; default for most types.
428 ; 'literal' - directly encoded in the code; implicit for some types like 'offset'.
431 (new-trace "direct-addressing")
434 (1:integer <- copy 34:literal)
435 (2:integer <- copy 1:integer)
439 (when (~iso memory* (obj 1 34 2 34))
440 (prn "F - 'copy' performs direct addressing"))
442 ; 'Indirect' addressing refers to an address stored in a memory location.
443 ; Indicated by the metadata '/deref'. Usually requires an address type.
444 ; In the test below, the memory location 1 contains '2', so an indirect read
445 ; of location 1 returns the value of location 2.
448 (new-trace "indirect-addressing")
451 (1:integer-address <- copy 2:literal) ; unsafe; can't do this in general
452 (2:integer <- copy 34:literal)
453 (3:integer <- copy 1:integer-address/deref)
457 (when (~iso memory* (obj 1 2 2 34 3 34))
458 (prn "F - 'copy' performs indirect addressing"))
460 ; Output args can use indirect addressing. In the test below the value is
461 ; stored at the location stored in location 1 (i.e. location 2).
464 (new-trace "indirect-addressing-oarg")
467 (1:integer-address <- copy 2:literal)
468 (2:integer <- copy 34:literal)
469 (1:integer-address/deref <- add 2:integer 2:literal)
473 (when (~iso memory* (obj 1 2 2 36))
474 (prn "F - instructions can perform indirect addressing on output arg"))
476 ;; Compound data types
478 ; Until now we've dealt with scalar types like integers and booleans and
479 ; addresses, where mu looks like other assembly languages. In addition, mu
480 ; provides first-class support for compound types: arrays and and-records.
482 ; 'get' accesses fields in and-records
483 ; 'index' accesses indices in arrays
485 ; Both operations require knowledge about the types being worked on, so all
486 ; types used in mu programs are defined in a single global system-wide table
487 ; (see type* in mu.arc for the complete list of types; we'll add to it over
490 ; first a sanity check that the table of types is consistent
492 (each (typ typeinfo) type*
493 (when typeinfo!and-record
494 (assert (is typeinfo!size (len typeinfo!elems)))
495 (when typeinfo!fields
496 (assert (is typeinfo!size (len typeinfo!fields))))))
499 (new-trace "get-record")
502 (1:integer <- copy 34:literal)
503 (2:boolean <- copy nil:literal)
504 (3:boolean <- get 1:integer-boolean-pair 1:offset)
505 (4:integer <- get 1:integer-boolean-pair 0:offset)
510 (when (~iso memory* (obj 1 34 2 nil 3 nil 4 34))
511 (prn "F - 'get' accesses fields of and-records"))
515 (new-trace "get-indirect")
518 (1:integer <- copy 34:literal)
519 (2:boolean <- copy nil:literal)
520 (3:integer-boolean-pair-address <- copy 1:literal)
521 (4:boolean <- get 3:integer-boolean-pair-address/deref 1:offset)
522 (5:integer <- get 3:integer-boolean-pair-address/deref 0:offset)
527 (when (~iso memory* (obj 1 34 2 nil 3 1 4 nil 5 34))
528 (prn "F - 'get' accesses fields of and-record address"))
531 (new-trace "get-indirect-repeated")
534 (1:integer <- copy 34:literal)
535 (2:integer <- copy 35:literal)
536 (3:integer <- copy 36:literal)
537 (4:integer-point-pair-address <- copy 1:literal) ; unsafe
538 (5:integer-point-pair-address-address <- copy 4:literal) ; unsafe
539 (6:integer-integer-pair <- get 5:integer-point-pair-address-address/deref/deref 1:offset)
540 (8:integer <- get 5:integer-point-pair-address-address/deref/deref 0:offset)
543 (when (~memory-contains 6 '(35 36 34))
544 (prn "F - 'get' can deref multiple times"))
548 (new-trace "get-compound-field")
551 (1:integer <- copy 34:literal)
552 (2:integer <- copy 35:literal)
553 (3:integer <- copy 36:literal)
554 (4:integer-integer-pair <- get 1:integer-point-pair 1:offset)
558 (when (~iso memory* (obj 1 34 2 35 3 36 4 35 5 36))
559 (prn "F - 'get' accesses fields spanning multiple locations"))
562 (new-trace "get-address")
565 (1:integer <- copy 34:literal)
566 (2:boolean <- copy t:literal)
567 (3:boolean-address <- get-address 1:integer-boolean-pair 1:offset)
571 (when (~iso memory* (obj 1 34 2 t 3 2))
572 (prn "F - 'get-address' returns address of fields of and-records"))
575 (new-trace "get-address-indirect")
578 (1:integer <- copy 34:literal)
579 (2:boolean <- copy t:literal)
580 (3:integer-boolean-pair-address <- copy 1:literal)
581 (4:boolean-address <- get-address 3:integer-boolean-pair-address/deref 1:offset)
585 (when (~iso memory* (obj 1 34 2 t 3 1 4 2))
586 (prn "F - 'get-address' accesses fields of and-record address"))
589 (new-trace "index-literal")
592 (1:integer <- copy 2:literal)
593 (2:integer <- copy 23:literal)
594 (3:boolean <- copy nil:literal)
595 (4:integer <- copy 24:literal)
596 (5:boolean <- copy t:literal)
597 (6:integer-boolean-pair <- index 1:integer-boolean-pair-array 1:literal)
601 (when (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 24 7 t))
602 (prn "F - 'index' accesses indices of arrays"))
606 (new-trace "index-direct")
609 (1:integer <- copy 2:literal)
610 (2:integer <- copy 23:literal)
611 (3:boolean <- copy nil:literal)
612 (4:integer <- copy 24:literal)
613 (5:boolean <- copy t:literal)
614 (6:integer <- copy 1:literal)
615 (7:integer-boolean-pair <- index 1:integer-boolean-pair-array 6:integer)
619 (when (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 1 7 24 8 t))
620 (prn "F - 'index' accesses indices of arrays"))
624 (new-trace "index-indirect")
627 (1:integer <- copy 2:literal)
628 (2:integer <- copy 23:literal)
629 (3:boolean <- copy nil:literal)
630 (4:integer <- copy 24:literal)
631 (5:boolean <- copy t:literal)
632 (6:integer <- copy 1:literal)
633 (7:integer-boolean-pair-array-address <- copy 1:literal)
634 (8:integer-boolean-pair <- index 7:integer-boolean-pair-array-address/deref 6:integer)
636 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
640 (when (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 1 7 1 8 24 9 t))
641 (prn "F - 'index' accesses indices of array address"))
645 (new-trace "index-indirect-multiple")
648 (1:integer <- copy 4:literal)
649 (2:integer <- copy 23:literal)
650 (3:integer <- copy 24:literal)
651 (4:integer <- copy 25:literal)
652 (5:integer <- copy 26:literal)
653 (6:integer-array-address <- copy 1:literal) ; unsafe
654 (7:integer-array-address-address <- copy 6:literal) ; unsafe
655 (8:integer <- index 7:integer-array-address-address/deref/deref 1:literal)
658 (when (~is memory*.8 24)
659 (prn "F - 'index' can deref multiple times"))
662 (new-trace "index-address")
665 (1:integer <- copy 2:literal)
666 (2:integer <- copy 23:literal)
667 (3:boolean <- copy nil:literal)
668 (4:integer <- copy 24:literal)
669 (5:boolean <- copy t:literal)
670 (6:integer <- copy 1:literal)
671 (7:integer-boolean-pair-address <- index-address 1:integer-boolean-pair-array 6:integer)
675 (when (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 1 7 4))
676 (prn "F - 'index-address' returns addresses of indices of arrays"))
679 (new-trace "index-address-indirect")
682 (1:integer <- copy 2:literal)
683 (2:integer <- copy 23:literal)
684 (3:boolean <- copy nil:literal)
685 (4:integer <- copy 24:literal)
686 (5:boolean <- copy t:literal)
687 (6:integer <- copy 1:literal)
688 (7:integer-boolean-pair-array-address <- copy 1:literal)
689 (8:integer-boolean-pair-address <- index-address 7:integer-boolean-pair-array-address/deref 6:integer)
693 (when (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 1 7 1 8 4))
694 (prn "F - 'index-address' returns addresses of indices of array addresses"))
696 ; Array values know their length. Record lengths are saved in the types table.
699 (new-trace "len-array")
702 (1:integer <- copy 2:literal)
703 (2:integer <- copy 23:literal)
704 (3:boolean <- copy nil:literal)
705 (4:integer <- copy 24:literal)
706 (5:boolean <- copy t:literal)
707 (6:integer <- length 1:integer-boolean-pair-array)
711 (when (~is memory*.6 2)
712 (prn "F - 'length' of array"))
715 (new-trace "len-array-indirect")
718 (1:integer <- copy 2:literal)
719 (2:integer <- copy 23:literal)
720 (3:boolean <- copy nil:literal)
721 (4:integer <- copy 24:literal)
722 (5:boolean <- copy t:literal)
723 (6:integer-address <- copy 1:literal)
724 (7:integer <- length 6:integer-boolean-pair-array-address/deref)
727 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
730 (when (~is memory*.7 2)
731 (prn "F - 'length' of array address"))
733 ; 'sizeof' is a helper to determine the amount of memory required by a type.
734 ; Only for non-arrays.
737 (new-trace "sizeof-record")
740 (1:integer <- sizeof integer-boolean-pair:literal)
744 (when (~is memory*.1 2)
745 (prn "F - 'sizeof' returns space required by arg"))
748 (new-trace "sizeof-record-not-len")
751 (1:integer <- sizeof integer-point-pair:literal)
755 (when (is memory*.1 2)
756 (prn "F - 'sizeof' is different from number of elems"))
758 ; Regardless of a type's length, you can move it around just like a primitive.
761 (new-trace "copy-record")
764 (1:integer <- copy 34:literal)
765 (2:boolean <- copy nil:literal)
766 (4:boolean <- copy t:literal)
767 (3:integer-boolean-pair <- copy 1:integer-boolean-pair)
771 (when (~iso memory* (obj 1 34 2 nil 3 34 4 nil))
772 (prn "F - ops can operate on records spanning multiple locations"))
775 (new-trace "copy-record2")
778 (1:integer <- copy 34:literal)
779 (2:integer <- copy 35:literal)
780 (3:integer <- copy 36:literal)
781 (4:integer <- copy 0:literal)
782 (5:integer <- copy 0:literal)
783 (6:integer <- copy 0:literal)
784 (4:integer-point-pair <- copy 1:integer-point-pair)
786 ;? (= dump-trace* (obj whitelist '("run" "sizeof")))
789 (when (~iso memory* (obj 1 34 2 35 3 36
792 (prn "F - ops can operate on records with fields spanning multiple locations"))
798 ; A special kind of record is the 'tagged type'. It lets us represent
799 ; dynamically typed values, which save type information in memory rather than
800 ; in the code to use them. This will let us do things like create heterogenous
801 ; lists containing both integers and strings. Tagged values admit two
804 ; 'save-type' - turns a regular value into a tagged-value of the appropriate type
805 ; 'maybe-coerce' - turns a tagged value into a regular value if the type matches
807 ; The payload of a tagged value must occupy just one location. Save pointers
811 (new-trace "tagged-value")
812 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
815 (1:type <- copy integer:literal)
816 (2:integer <- copy 34:literal)
817 (3:integer 4:boolean <- maybe-coerce 1:tagged-value integer:literal)
821 ;? (prn completed-routines*)
822 (each routine completed-routines*
823 (aif rep.routine!error (prn "error - " it)))
825 (when (or (~is memory*.3 34)
827 (prn "F - 'maybe-coerce' copies value only if type tag matches"))
831 (new-trace "tagged-value-2")
835 (1:type <- copy integer-address:literal)
836 (2:integer <- copy 34:literal)
837 (3:boolean 4:boolean <- maybe-coerce 1:tagged-value boolean:literal)
841 (when (or (~is memory*.3 0)
843 (prn "F - 'maybe-coerce' doesn't copy value when type tag doesn't match"))
846 (new-trace "save-type")
849 (1:integer <- copy 34:literal)
850 (2:tagged-value <- save-type 1:integer)
854 (when (~iso memory* (obj 1 34 2 'integer 3 34))
855 (prn "F - 'save-type' saves the type of a value at runtime, turning it into a tagged-value"))
858 (new-trace "init-tagged-value")
861 (1:integer <- copy 34:literal)
862 (2:tagged-value-address <- init-tagged-value integer:literal 1:integer)
863 (3:integer 4:boolean <- maybe-coerce 2:tagged-value-address/deref integer:literal)
865 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1" "sizeof")))
868 (when (or (~is memory*.3 34)
870 (prn "F - 'init-tagged-value' is the converse of 'maybe-coerce'"))
873 ; Now that we can package values together with their types, we can construct a
874 ; dynamically typed list.
881 ; 1 points at first node: tagged-value (int 34)
882 (1:list-address <- new list:literal)
883 (2:tagged-value-address <- list-value-address 1:list-address)
884 (3:type-address <- get-address 2:tagged-value-address/deref type:offset)
885 (3:type-address/deref <- copy integer:literal)
886 (4:location <- get-address 2:tagged-value-address/deref payload:offset)
887 (4:location/deref <- copy 34:literal)
888 (5:list-address-address <- get-address 1:list-address/deref cdr:offset)
889 (5:list-address-address/deref <- new list:literal)
890 ; 6 points at second node: tagged-value (boolean t)
891 (6:list-address <- copy 5:list-address-address/deref)
892 (7:tagged-value-address <- list-value-address 6:list-address)
893 (8:type-address <- get-address 7:tagged-value-address/deref type:offset)
894 (8:type-address/deref <- copy boolean:literal)
895 (9:location <- get-address 7:tagged-value-address/deref payload:offset)
896 (9:location/deref <- copy t:literal)
897 (10:list-address <- get 6:list-address/deref 1:offset)
899 (let routine make-routine!main
900 (enq routine running-routines*)
901 (let first rep.routine!alloc
902 ;? (= dump-trace* (obj whitelist '("run")))
906 (each routine completed-routines*
907 (aif rep.routine!error (prn "error - " it)))
908 (when (or (~all first (map memory* '(1 2 3)))
909 (~is memory*.first 'integer)
910 (~is memory*.4 (+ first 1))
911 (~is (memory* (+ first 1)) 34)
912 (~is memory*.5 (+ first 2))
913 (let second memory*.6
915 (~is (memory* (+ first 2)) second)
916 (~all second (map memory* '(6 7 8)))
917 (~is memory*.second 'boolean)
918 (~is memory*.9 (+ second 1))
919 (~is (memory* (+ second 1)) t)
920 (~is memory*.10 nil))))
921 (prn "F - lists can contain elements of different types"))))
923 (10:list-address <- list-next 1:list-address))
924 (each routine completed-routines*
925 (aif rep.routine!error (prn "error - " it)))
926 (when (~is memory*.10 memory*.6)
927 (prn "F - 'list-next can move a list pointer to the next node"))
930 ; 'init-list' takes a variable number of args and constructs a list containing
931 ; them. Just integers for now.
934 (new-trace "init-list")
937 (1:integer <- init-list 3:literal 4:literal 5:literal)
939 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1" "sizeof")))
944 (when (or (~is memory*.first 'integer)
945 (~is (memory* (+ first 1)) 3)
946 (let second (memory* (+ first 2))
948 (or (~is memory*.second 'integer)
949 (~is (memory* (+ second 1)) 4)
950 (let third (memory* (+ second 2))
952 (or (~is memory*.third 'integer)
953 (~is (memory* (+ third 1)) 5)
954 (~is (memory* (+ third 2) nil)))))))
955 (prn "F - 'init-list' can construct a list of integers")))
963 ; Just like the table of types is centralized, functions are conceptualized as
964 ; a centralized table of operations just like the "primitives" we've seen so
965 ; far. If you create a function you can call it like any other op.
971 (3:integer <- add 1:integer 2:integer)
974 (1:integer <- copy 1:literal)
975 (2:integer <- copy 3:literal)
980 (when (~iso memory* (obj 1 1 2 3 3 4))
981 (prn "F - calling a user-defined function runs its instructions"))
985 (new-trace "new-fn-once")
988 (1:integer <- copy 1:literal)
993 ;? (= dump-trace* (obj whitelist '("run")))
995 (when (~is 2 curr-cycle*)
996 (prn "F - calling a user-defined function runs its instructions exactly once " curr-cycle*))
999 ; User-defined functions communicate with their callers through two
1002 ; 'arg' - to access inputs
1003 ; 'reply' - to return outputs
1006 (new-trace "new-fn-reply")
1009 (3:integer <- add 1:integer 2:integer)
1011 (4:integer <- copy 34:literal)
1014 (1:integer <- copy 1:literal)
1015 (2:integer <- copy 3:literal)
1020 (when (~iso memory* (obj 1 1 2 3 3 4))
1021 (prn "F - 'reply' stops executing the current function"))
1025 (new-trace "new-fn-reply-nested")
1028 (3:integer <- test2)
1034 (2:integer <- copy 34:literal)
1039 (when (~iso memory* (obj 2 34 3 34))
1040 (prn "F - 'reply' stops executing any callers as necessary"))
1044 (new-trace "new-fn-reply-once")
1047 (3:integer <- add 1:integer 2:integer)
1049 (4:integer <- copy 34:literal)
1052 (1:integer <- copy 1:literal)
1053 (2:integer <- copy 3:literal)
1056 ;? (= dump-trace* (obj whitelist '("run")))
1058 (when (~is 5 curr-cycle*)
1059 (prn "F - 'reply' executes instructions exactly once " curr-cycle*))
1063 (new-trace "reply-increments-caller-pc")
1065 '((function callee [
1069 (1:integer <- copy 0:literal)
1070 (2:integer <- copy 0:literal)
1073 (= routine* (make-routine 'caller))
1074 (assert (is 0 pc.routine*))
1075 (push-stack routine* 'callee) ; pretend call was at first instruction of caller
1076 (run-for-time-slice 1)
1077 (when (~is 1 pc.routine*)
1078 (prn "F - 'reply' increments pc in caller (to move past calling instruction)"))
1081 (new-trace "new-fn-arg-sequential")
1084 (4:integer <- next-input)
1085 (5:integer <- next-input)
1086 (3:integer <- add 4:integer 5:integer)
1088 (4:integer <- copy 34:literal)
1091 (1:integer <- copy 1:literal)
1092 (2:integer <- copy 3:literal)
1093 (test1 1:integer 2:integer)
1097 (when (~iso memory* (obj 1 1 2 3 3 4
1098 ; test1's temporaries
1100 (prn "F - 'arg' accesses in order the operands of the most recent function call (the caller)"))
1104 (new-trace "new-fn-arg-random-access")
1105 ;? (set dump-trace*)
1108 (5:integer <- input 1:literal)
1109 (4:integer <- input 0:literal)
1110 (3:integer <- add 4:integer 5:integer)
1112 (4:integer <- copy 34:literal) ; should never run
1115 (1:integer <- copy 1:literal)
1116 (2:integer <- copy 3:literal)
1117 (test1 1:integer 2:integer)
1121 (when (~iso memory* (obj 1 1 2 3 3 4
1122 ; test's temporaries
1124 (prn "F - 'arg' with index can access function call arguments out of order"))
1128 (new-trace "new-fn-arg-random-then-sequential")
1129 ;? (set dump-trace*)
1132 (_ <- input 1:literal)
1133 (1:integer <- next-input) ; takes next arg after index 1
1134 ]) ; should never run
1136 (test1 1:literal 2:literal 3:literal)
1140 (when (~iso memory* (obj 1 3))
1141 (prn "F - 'arg' with index resets index for later calls"))
1145 (new-trace "new-fn-arg-status")
1148 (4:integer 5:boolean <- next-input)
1155 (when (~iso memory* (obj 4 1 5 t))
1156 (prn "F - 'arg' sets a second oarg when arg exists"))
1160 (new-trace "new-fn-arg-missing")
1163 (4:integer <- next-input)
1164 (5:integer <- next-input)
1171 (when (~iso memory* (obj 4 1))
1172 (prn "F - missing 'arg' doesn't cause error"))
1176 (new-trace "new-fn-arg-missing-2")
1179 (4:integer <- next-input)
1180 (5:integer 6:boolean <- next-input)
1187 (when (~iso memory* (obj 4 1 6 nil))
1188 (prn "F - missing 'arg' wipes second oarg when provided"))
1192 (new-trace "new-fn-arg-missing-3")
1195 (4:integer <- next-input)
1196 (5:integer <- copy 34:literal)
1197 (5:integer 6:boolean <- next-input)
1204 (when (~iso memory* (obj 4 1 6 nil))
1205 (prn "F - missing 'arg' consistently wipes its oarg"))
1209 (new-trace "new-fn-arg-missing-4")
1212 ; if given two args, adds them; if given one arg, increments
1213 (4:integer <- next-input)
1214 (5:integer 6:boolean <- next-input)
1216 (break-if 6:boolean)
1217 (5:integer <- copy 1:literal)
1219 (7:integer <- add 4:integer 5:integer)
1224 ;? (set dump-trace*)
1227 (when (~iso memory* (obj 4 34 5 1 6 nil 7 35))
1228 (prn "F - function with optional second arg"))
1232 (new-trace "new-fn-arg-by-value")
1235 (1:integer <- copy 0:literal) ; overwrite caller memory
1236 (2:integer <- next-input)
1237 ]) ; arg not clobbered
1239 (1:integer <- copy 34:literal)
1244 (when (~iso memory* (obj 1 0 2 34))
1245 (prn "F - 'arg' passes by value"))
1248 (new-trace "arg-record")
1251 (4:integer-boolean-pair <- next-input)
1254 (1:integer <- copy 34:literal)
1255 (2:boolean <- copy nil:literal)
1256 (test1 1:integer-boolean-pair)
1259 (when (~iso memory* (obj 1 34 2 nil 4 34 5 nil))
1260 (prn "F - 'arg' can copy records spanning multiple locations"))
1263 (new-trace "arg-record-indirect")
1264 ;? (set dump-trace*)
1267 (4:integer-boolean-pair <- next-input)
1270 (1:integer <- copy 34:literal)
1271 (2:boolean <- copy nil:literal)
1272 (3:integer-boolean-pair-address <- copy 1:literal)
1273 (test1 3:integer-boolean-pair-address/deref)
1277 (when (~iso memory* (obj 1 34 2 nil 3 1 4 34 5 nil))
1278 (prn "F - 'arg' can copy records spanning multiple locations in indirect mode"))
1281 (new-trace "new-fn-reply-oarg")
1284 (4:integer <- next-input)
1285 (5:integer <- next-input)
1286 (6:integer <- add 4:integer 5:integer)
1288 (4:integer <- copy 34:literal)
1291 (1:integer <- copy 1:literal)
1292 (2:integer <- copy 3:literal)
1293 (3:integer <- test1 1:integer 2:integer)
1297 (when (~iso memory* (obj 1 1 2 3 3 4
1298 ; test1's temporaries
1300 (prn "F - 'reply' can take aguments that are returned, or written back into output args of caller"))
1303 (new-trace "new-fn-reply-oarg-multiple")
1306 (4:integer <- next-input)
1307 (5:integer <- next-input)
1308 (6:integer <- add 4:integer 5:integer)
1309 (reply 6:integer 5:integer)
1310 (4:integer <- copy 34:literal)
1313 (1:integer <- copy 1:literal)
1314 (2:integer <- copy 3:literal)
1315 (3:integer 7:integer <- test1 1:integer 2:integer)
1319 (when (~iso memory* (obj 1 1 2 3 3 4 7 3
1320 ; test1's temporaries
1322 (prn "F - 'reply' permits a function to return multiple values at once"))
1324 ; 'prepare-reply' is useful for doing cleanup before exiting a function
1326 (new-trace "new-fn-prepare-reply")
1329 (4:integer <- next-input)
1330 (5:integer <- next-input)
1331 (6:integer <- add 4:integer 5:integer)
1332 (prepare-reply 6:integer 5:integer)
1334 (4:integer <- copy 34:literal)
1337 (1:integer <- copy 1:literal)
1338 (2:integer <- copy 3:literal)
1339 (3:integer 7:integer <- test1 1:integer 2:integer)
1343 (when (~iso memory* (obj 1 1 2 3 3 4 7 3
1344 ; test1's temporaries
1346 (prn "F - without args, 'reply' returns values from previous 'prepare-reply'."))
1348 ; When you have arguments that are both read from and written to, include them
1349 ; redundantly in both ingredients and results. That'll help tools track what
1352 ; To enforce that the result and ingredient must always match, use the
1353 ; 'same-as-arg' property. Results with 'same-as-arg' properties should only be
1354 ; copied to a caller output arg identical to the specified caller arg.
1356 (new-trace "new-fn-same-as-arg")
1359 ; increment the contents of an address
1360 (default-space:space-address <- new space:literal 2:literal)
1361 (x:integer-address <- next-input)
1362 (x:integer-address/deref <- add x:integer-address/deref 1:literal)
1363 (reply x:integer-address/same-as-arg:0)
1366 (2:integer-address <- new integer:literal)
1367 (2:integer-address/deref <- copy 0:literal)
1368 (3:integer-address <- test1 2:integer-address)
1371 (let routine (car completed-routines*)
1372 ;? (prn rep.routine!error) ;? 1
1373 (when (no rep.routine!error)
1374 (prn "F - 'same-as-arg' results must be identical to a given input")))
1381 ;; Structured programming
1383 ; Our jump operators are quite inconvenient to use, so mu provides a
1384 ; lightweight tool called 'convert-braces' to work in a slightly more
1385 ; convenient format with nested braces:
1394 ; Braces are like labels in assembly language, they require no special
1395 ; parsing. The operations 'loop' and 'break' jump to just after the enclosing
1396 ; '{' and '}' respectively.
1398 ; Conditional and unconditional 'loop' and 'break' should give us 80% of the
1399 ; benefits of the control-flow primitives we're used to in other languages,
1400 ; like 'if', 'while', 'for', etc.
1402 ; Compare 'unquoted blocks' using {} with 'quoted blocks' using [] that we've
1403 ; gotten used to seeing. Quoted blocks are used by top-level instructions to
1404 ; provide code without running it.
1407 (new-trace "convert-braces")
1409 ;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
1410 (when (~iso (convert-braces
1411 '((((1 integer)) <- ((copy)) ((0 literal)))
1412 (((2 integer)) <- ((copy)) ((0 literal)))
1413 (((3 integer)) <- ((copy)) ((0 literal)))
1414 { begin ; 'begin' is just a hack because racket turns braces into parens
1415 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1416 (((break-if)) ((4 boolean)))
1417 (((5 integer)) <- ((copy)) ((0 literal)))
1420 '((((1 integer)) <- ((copy)) ((0 literal)))
1421 (((2 integer)) <- ((copy)) ((0 literal)))
1422 (((3 integer)) <- ((copy)) ((0 literal)))
1423 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1424 (((jump-if)) ((4 boolean)) ((1 offset)))
1425 (((5 integer)) <- ((copy)) ((0 literal)))
1427 (prn "F - convert-braces replaces break-if with a jump-if to after the next close-brace"))
1431 (new-trace "convert-braces-empty-block")
1433 ;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
1434 (when (~iso (convert-braces
1435 '((((1 integer)) <- ((copy)) ((0 literal)))
1436 (((2 integer)) <- ((copy)) ((0 literal)))
1437 (((3 integer)) <- ((copy)) ((0 literal)))
1442 '((((1 integer)) <- ((copy)) ((0 literal)))
1443 (((2 integer)) <- ((copy)) ((0 literal)))
1444 (((3 integer)) <- ((copy)) ((0 literal)))
1445 (((jump)) ((0 offset)))
1447 (prn "F - convert-braces works for degenerate blocks"))
1451 (new-trace "convert-braces-nested-break")
1453 (when (~iso (convert-braces
1454 '((((1 integer)) <- ((copy)) ((0 literal)))
1455 (((2 integer)) <- ((copy)) ((0 literal)))
1456 (((3 integer)) <- ((copy)) ((0 literal)))
1458 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1459 (((break-if)) ((4 boolean)))
1461 (((5 integer)) <- ((copy)) ((0 literal)))
1465 '((((1 integer)) <- ((copy)) ((0 literal)))
1466 (((2 integer)) <- ((copy)) ((0 literal)))
1467 (((3 integer)) <- ((copy)) ((0 literal)))
1468 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1469 (((jump-if)) ((4 boolean)) ((1 offset)))
1470 (((5 integer)) <- ((copy)) ((0 literal)))
1472 (prn "F - convert-braces balances braces when converting break"))
1475 (new-trace "convert-braces-repeated-jump")
1477 ;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
1478 (when (~iso (convert-braces
1479 '((((1 integer)) <- ((copy)) ((0 literal)))
1482 (((2 integer)) <- ((copy)) ((0 literal)))
1486 (((3 integer)) <- ((copy)) ((0 literal)))
1488 (((4 integer)) <- ((copy)) ((0 literal)))))
1489 '((((1 integer)) <- ((copy)) ((0 literal)))
1490 (((jump)) ((1 offset)))
1491 (((2 integer)) <- ((copy)) ((0 literal)))
1492 (((jump)) ((1 offset)))
1493 (((3 integer)) <- ((copy)) ((0 literal)))
1494 (((4 integer)) <- ((copy)) ((0 literal)))))
1495 (prn "F - convert-braces handles jumps on jumps"))
1499 (new-trace "convert-braces-nested-loop")
1501 (when (~iso (convert-braces
1502 '((((1 integer)) <- ((copy)) ((0 literal)))
1503 (((2 integer)) <- ((copy)) ((0 literal)))
1505 (((3 integer)) <- ((copy)) ((0 literal)))
1507 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1509 (((loop-if)) ((4 boolean)))
1510 (((5 integer)) <- ((copy)) ((0 literal)))
1513 '((((1 integer)) <- ((copy)) ((0 literal)))
1514 (((2 integer)) <- ((copy)) ((0 literal)))
1515 (((3 integer)) <- ((copy)) ((0 literal)))
1516 (((4 boolean)) <- ((not-equal)) ((1 integer)) ((3 integer)))
1517 (((jump-if)) ((4 boolean)) ((-3 offset)))
1518 (((5 integer)) <- ((copy)) ((0 literal)))
1520 (prn "F - convert-braces balances braces when converting 'loop'"))
1523 (new-trace "convert-braces-label")
1525 (when (~iso (convert-braces
1526 '((((1 integer)) <- ((copy)) ((0 literal)))
1528 (((2 integer)) <- ((copy)) ((0 literal)))))
1529 '((((1 integer)) <- ((copy)) ((0 literal)))
1531 (((2 integer)) <- ((copy)) ((0 literal)))))
1532 (prn "F - convert-braces skips past labels"))
1536 (new-trace "convert-braces-label-increments-offset")
1538 (when (~iso (convert-braces
1539 '((((1 integer)) <- ((copy)) ((0 literal)))
1544 (((2 integer)) <- ((copy)) ((0 literal)))))
1545 '((((1 integer)) <- ((copy)) ((0 literal)))
1546 (((jump)) ((1 offset)))
1548 (((2 integer)) <- ((copy)) ((0 literal)))))
1549 (prn "F - convert-braces treats labels as instructions"))
1553 (new-trace "convert-braces-label-increments-offset2")
1555 ;? (= dump-trace* (obj whitelist '("c{0" "c{1")))
1556 (when (~iso (convert-braces
1557 '((((1 integer)) <- ((copy)) ((0 literal)))
1562 (((2 integer)) <- ((copy)) ((0 literal)))
1565 (((3 integer)) <- ((copy)) ((0 literal)))
1567 (((4 integer)) <- ((copy)) ((0 literal)))))
1568 '((((1 integer)) <- ((copy)) ((0 literal)))
1569 (((jump)) ((1 offset)))
1571 (((2 integer)) <- ((copy)) ((0 literal)))
1572 (((jump)) ((1 offset)))
1573 (((3 integer)) <- ((copy)) ((0 literal)))
1574 (((4 integer)) <- ((copy)) ((0 literal)))))
1575 (prn "F - convert-braces treats labels as instructions - 2"))
1579 (new-trace "break-multiple")
1581 ;? (= dump-trace* (obj whitelist '("-")))
1582 (when (~iso (convert-braces
1583 '((((1 integer)) <- ((copy)) ((0 literal)))
1586 (((break)) ((2 blocks)))
1588 (((2 integer)) <- ((copy)) ((0 literal)))
1589 (((3 integer)) <- ((copy)) ((0 literal)))
1590 (((4 integer)) <- ((copy)) ((0 literal)))
1591 (((5 integer)) <- ((copy)) ((0 literal)))
1593 '((((1 integer)) <- ((copy)) ((0 literal)))
1594 (((jump)) ((4 offset)))
1595 (((2 integer)) <- ((copy)) ((0 literal)))
1596 (((3 integer)) <- ((copy)) ((0 literal)))
1597 (((4 integer)) <- ((copy)) ((0 literal)))
1598 (((5 integer)) <- ((copy)) ((0 literal)))))
1599 (prn "F - 'break' can take an extra arg with number of nested blocks to exit"))
1604 ;? (set dump-trace*)
1605 (when (~iso (convert-braces
1606 '((((1 integer)) <- ((copy)) ((0 literal)))
1607 (((2 integer)) <- ((copy)) ((0 literal)))
1609 (((3 integer)) <- ((copy)) ((0 literal)))
1612 '((((1 integer)) <- ((copy)) ((0 literal)))
1613 (((2 integer)) <- ((copy)) ((0 literal)))
1614 (((3 integer)) <- ((copy)) ((0 literal)))
1615 (((jump)) ((-2 offset)))))
1616 (prn "F - 'loop' jumps to start of containing block"))
1619 ; todo: fuzz-test invariant: convert-braces offsets should be robust to any
1620 ; number of inner blocks inside but not around the loop block.
1623 (new-trace "loop-nested")
1624 ;? (set dump-trace*)
1625 (when (~iso (convert-braces
1626 '((((1 integer)) <- ((copy)) ((0 literal)))
1627 (((2 integer)) <- ((copy)) ((0 literal)))
1629 (((3 integer)) <- ((copy)) ((0 literal)))
1631 (((4 integer)) <- ((copy)) ((0 literal)))
1635 '((((1 integer)) <- ((copy)) ((0 literal)))
1636 (((2 integer)) <- ((copy)) ((0 literal)))
1637 (((3 integer)) <- ((copy)) ((0 literal)))
1638 (((4 integer)) <- ((copy)) ((0 literal)))
1639 (((jump)) ((-3 offset)))))
1640 (prn "F - 'loop' correctly jumps back past nested braces"))
1643 (new-trace "loop-multiple")
1645 ;? (= dump-trace* (obj whitelist '("-")))
1646 (when (~iso (convert-braces
1647 '((((1 integer)) <- ((copy)) ((0 literal)))
1649 (((2 integer)) <- ((copy)) ((0 literal)))
1650 (((3 integer)) <- ((copy)) ((0 literal)))
1652 (((loop)) ((2 blocks)))
1655 '((((1 integer)) <- ((copy)) ((0 literal)))
1656 (((2 integer)) <- ((copy)) ((0 literal)))
1657 (((3 integer)) <- ((copy)) ((0 literal)))
1658 (((jump)) ((-3 offset)))))
1659 (prn "F - 'loop' can take an extra arg with number of nested blocks to exit"))
1663 (new-trace "convert-labels")
1665 (when (~iso (convert-labels
1667 (((jump)) ((loop offset)))))
1669 (((jump)) ((-2 offset)))))
1670 (prn "F - 'convert-labels' rewrites jumps to labels"))
1674 ; A big convenience high-level languages provide is the ability to name memory
1675 ; locations. In mu, a lightweight tool called 'convert-names' provides this
1679 (new-trace "convert-names")
1681 ;? (set dump-trace*)
1682 (when (~iso (convert-names
1683 '((((x integer)) <- ((copy)) ((0 literal)))
1684 (((y integer)) <- ((copy)) ((0 literal)))
1685 (((z integer)) <- ((copy)) ((0 literal)))))
1686 '((((1 integer)) <- ((copy)) ((0 literal)))
1687 (((2 integer)) <- ((copy)) ((0 literal)))
1688 (((3 integer)) <- ((copy)) ((0 literal)))))
1689 (prn "F - convert-names renames symbolic names to integer locations"))
1692 (new-trace "convert-names-compound")
1694 (when (~iso (convert-names
1695 ; copying 0 into pair is meaningless; just for testing
1696 '((((x integer-boolean-pair)) <- ((copy)) ((0 literal)))
1697 (((y integer)) <- ((copy)) ((0 literal)))))
1698 '((((1 integer-boolean-pair)) <- ((copy)) ((0 literal)))
1699 (((3 integer)) <- ((copy)) ((0 literal)))))
1700 (prn "F - convert-names increments integer locations by the size of the type of the previous var"))
1703 (new-trace "convert-names-nil")
1705 ;? (set dump-trace*)
1706 (when (~iso (convert-names
1707 '((((x integer)) <- ((copy)) ((0 literal)))
1708 (((y integer)) <- ((copy)) ((0 literal)))
1709 ; nil location is meaningless; just for testing
1710 (((nil integer)) <- ((copy)) ((0 literal)))))
1711 '((((1 integer)) <- ((copy)) ((0 literal)))
1712 (((2 integer)) <- ((copy)) ((0 literal)))
1713 (((nil integer)) <- ((copy)) ((0 literal)))))
1714 (prn "F - convert-names never renames nil"))
1717 (new-trace "convert-names-string")
1718 ;? (set dump-trace*)
1719 (when (~iso (convert-names
1720 '((((1 integer-address)) <- ((new)) "foo")))
1721 '((((1 integer-address)) <- ((new)) "foo")))
1722 (prn "convert-names passes through raw strings (just a convenience arg for 'new')"))
1725 (new-trace "convert-names-raw")
1727 (when (~iso (convert-names
1728 '((((x integer)) <- ((copy)) ((0 literal)))
1729 (((y integer) (raw)) <- ((copy)) ((0 literal)))))
1730 '((((1 integer)) <- ((copy)) ((0 literal)))
1731 (((y integer) (raw)) <- ((copy)) ((0 literal)))))
1732 (prn "F - convert-names never renames raw operands"))
1735 (new-trace "convert-names-literal")
1737 (when (~iso (convert-names
1738 ; meaningless; just for testing
1739 '((((x literal)) <- ((copy)) ((0 literal)))))
1740 '((((x literal)) <- ((copy)) ((0 literal)))))
1741 (prn "F - convert-names never renames literals"))
1744 (new-trace "convert-names-literal-2")
1746 (when (~iso (convert-names
1747 '((((x boolean)) <- ((copy)) ((x literal)))))
1748 '((((1 boolean)) <- ((copy)) ((x literal)))))
1749 (prn "F - convert-names never renames literals, even when the name matches a variable"))
1751 ; kludgy support for 'fork' below
1753 (new-trace "convert-names-functions")
1755 (when (~iso (convert-names
1756 '((((x integer)) <- ((copy)) ((0 literal)))
1757 (((y integer)) <- ((copy)) ((0 literal)))
1758 ; meaningless; just for testing
1759 (((z fn)) <- ((copy)) ((0 literal)))))
1760 '((((1 integer)) <- ((copy)) ((0 literal)))
1761 (((2 integer)) <- ((copy)) ((0 literal)))
1762 (((z fn)) <- ((copy)) ((0 literal)))))
1763 (prn "F - convert-names never renames fns"))
1766 (new-trace "convert-names-record-fields")
1768 ;? (= dump-trace* (obj whitelist '("cn0")))
1769 (when (~iso (convert-names
1770 '((((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset)))))
1771 '((((1 integer)) <- ((get)) ((34 integer-boolean-pair)) ((1 offset)))))
1772 (prn "F - convert-names replaces record field offsets"))
1775 (new-trace "convert-names-record-fields-ambiguous")
1777 (when (errsafe (convert-names
1778 '((((bool boolean)) <- ((copy)) ((t literal)))
1779 (((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset))))))
1780 (prn "F - convert-names doesn't allow offsets and variables with the same name in a function"))
1783 (new-trace "convert-names-record-fields-ambiguous-2")
1785 (when (errsafe (convert-names
1786 '((((x integer)) <- ((get)) ((34 integer-boolean-pair)) ((bool offset)))
1787 (((bool boolean)) <- ((copy)) ((t literal))))))
1788 (prn "F - convert-names doesn't allow offsets and variables with the same name in a function - 2"))
1791 (new-trace "convert-names-record-fields-indirect")
1793 ;? (= dump-trace* (obj whitelist '("cn0")))
1794 (when (~iso (convert-names
1795 '((((x integer)) <- ((get)) ((34 integer-boolean-pair-address) (deref)) ((bool offset)))))
1796 '((((1 integer)) <- ((get)) ((34 integer-boolean-pair-address) (deref)) ((1 offset)))))
1797 (prn "F - convert-names replaces field offsets for record addresses"))
1801 (new-trace "convert-names-record-fields-multiple")
1803 (when (~iso (convert-names
1804 '((((2 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((bool offset)))
1805 (((3 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((bool offset)))))
1806 '((((2 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((1 offset)))
1807 (((3 boolean)) <- ((get)) ((1 integer-boolean-pair)) ((1 offset)))))
1808 (prn "F - convert-names replaces field offsets with multiple mentions"))
1812 (new-trace "convert-names-label")
1814 (when (~iso (convert-names
1815 '((((1 integer)) <- ((copy)) ((0 literal)))
1817 '((((1 integer)) <- ((copy)) ((0 literal)))
1819 (prn "F - convert-names skips past labels"))
1826 ; A rudimentary memory allocator. Eventually we want to write this in mu.
1828 ; No deallocation yet; let's see how much code we can build in mu before we
1829 ; feel the need for it.
1832 (new-trace "new-primitive")
1835 (1:integer-address <- new integer:literal)
1837 (let routine make-routine!main
1838 (enq routine running-routines*)
1839 (let before rep.routine!alloc
1840 ;? (set dump-trace*)
1843 (when (~iso memory*.1 before)
1844 (prn "F - 'new' returns current high-water mark"))
1845 (when (~iso rep.routine!alloc (+ before 1))
1846 (prn "F - 'new' on primitive types increments high-water mark by their size"))))
1850 (new-trace "new-array-literal")
1853 (1:type-array-address <- new type-array:literal 5:literal)
1855 (let routine make-routine!main
1856 (enq routine running-routines*)
1857 (let before rep.routine!alloc
1860 (when (~iso memory*.1 before)
1861 (prn "F - 'new' on array with literal size returns current high-water mark"))
1862 (when (~iso rep.routine!alloc (+ before 6))
1863 (prn "F - 'new' on primitive arrays increments high-water mark by their size"))))
1866 (new-trace "new-array-direct")
1869 (1:integer <- copy 5:literal)
1870 (2:type-array-address <- new type-array:literal 1:integer)
1872 (let routine make-routine!main
1873 (enq routine running-routines*)
1874 (let before rep.routine!alloc
1877 (when (~iso memory*.2 before)
1878 (prn "F - 'new' on array with variable size returns current high-water mark"))
1879 (when (~iso rep.routine!alloc (+ before 6))
1880 (prn "F - 'new' on primitive arrays increments high-water mark by their (variable) size"))))
1883 (new-trace "new-allocation-chunk")
1886 (1:integer-address <- new integer:literal)
1888 ; start allocating from address 30, in chunks of 10 locations each
1889 (= Memory-allocated-until 30
1890 Allocation-chunk 10)
1891 (let routine make-routine!main
1892 (assert:is rep.routine!alloc 30)
1893 (assert:is rep.routine!alloc-max 40)
1894 ; pretend the current chunk is full
1895 (= rep.routine!alloc 40)
1896 (enq routine running-routines*)
1898 (each routine completed-routines*
1899 (aif rep.routine!error (prn "error - " it)))
1900 (when (~is rep.routine!alloc 41)
1901 (prn "F - 'new' can allocate past initial routine memory"))
1902 (when (~is rep.routine!alloc-max 50)
1903 (prn "F - 'new' updates upper bound for routine memory @rep.routine!alloc-max")))
1906 (new-trace "new-skip")
1909 (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
1911 ; start allocating from address 30, in chunks of 10 locations each
1912 (= Memory-allocated-until 30
1913 Allocation-chunk 10)
1914 (let routine make-routine!main
1915 (assert:is rep.routine!alloc 30)
1916 (assert:is rep.routine!alloc-max 40)
1917 ; pretend the current chunk has just one location left
1918 (= rep.routine!alloc 39)
1919 (enq routine running-routines*)
1920 ; request 2 locations
1922 (each routine completed-routines*
1923 (aif rep.routine!error (prn "error - " it)))
1924 (when (or (~is memory*.1 40)
1925 (~is rep.routine!alloc 42)
1926 (~is rep.routine!alloc-max 50)
1927 (~is Memory-allocated-until 50))
1928 (prn "F - 'new' skips past current chunk if insufficient space")))
1931 (new-trace "new-skip-noncontiguous")
1934 (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
1936 ; start allocating from address 30, in chunks of 10 locations each
1937 (= Memory-allocated-until 30
1938 Allocation-chunk 10)
1939 (let routine make-routine!main
1940 (assert:is rep.routine!alloc 30)
1941 (assert:is rep.routine!alloc-max 40)
1942 ; pretend the current chunk has just one location left
1943 (= rep.routine!alloc 39)
1944 ; pretend we allocated more memory since we created the routine
1945 (= Memory-allocated-until 90)
1946 (enq routine running-routines*)
1947 ; request 2 locations
1949 (each routine completed-routines*
1950 (aif rep.routine!error (prn "error - " it)))
1951 (when (or (~is memory*.1 90)
1952 (~is rep.routine!alloc 92)
1953 (~is rep.routine!alloc-max 100)
1954 (~is Memory-allocated-until 100))
1955 (prn "F - 'new' allocates a new chunk if insufficient space")))
1958 (new-trace "new-array-skip-noncontiguous")
1961 (1:integer-array-address <- new integer-array:literal 4:literal)
1963 ; start allocating from address 30, in chunks of 10 locations each
1964 (= Memory-allocated-until 30
1965 Allocation-chunk 10)
1966 (let routine make-routine!main
1967 (assert:is rep.routine!alloc 30)
1968 (assert:is rep.routine!alloc-max 40)
1969 ; pretend the current chunk has just one location left
1970 (= rep.routine!alloc 39)
1971 ; pretend we allocated more memory since we created the routine
1972 (= Memory-allocated-until 90)
1973 (enq routine running-routines*)
1974 ; request 4 locations
1976 (each routine completed-routines*
1977 (aif rep.routine!error (prn "error - " it)))
1978 ;? (prn memory*.1) ;? 1
1979 ;? (prn rep.routine) ;? 1
1980 ;? (prn Memory-allocated-until) ;? 1
1981 (when (or (~is memory*.1 90)
1982 (~is rep.routine!alloc 95)
1983 (~is rep.routine!alloc-max 100)
1984 (~is Memory-allocated-until 100))
1985 (prn "F - 'new-array' allocates a new chunk if insufficient space")))
1989 ; Even though our memory locations can now have names, the names are all
1990 ; globals, accessible from any function. To isolate functions from their
1991 ; callers we need local variables, and mu provides them using a special
1992 ; variable called default-space. When you initialize such a variable (likely
1993 ; with a call to our just-defined memory allocator) mu interprets memory
1994 ; locations as offsets from its value. If default-space is set to 1000, for
1995 ; example, reads and writes to memory location 1 will really go to 1001.
1997 ; 'default-space' is itself hard-coded to be function-local; it's nil in a new
1998 ; function, and it's restored when functions return to their callers. But the
1999 ; actual space allocation is independent. So you can define closures, or do
2000 ; even more funky things like share locals between two coroutines.
2003 (new-trace "set-default-space")
2006 (default-space:space-address <- new space:literal 2:literal)
2007 (1:integer <- copy 23:literal)
2009 (let routine make-routine!main
2010 (enq routine running-routines*)
2011 (let before rep.routine!alloc
2012 ;? (set dump-trace*)
2015 (when (~and (~is 23 memory*.1)
2016 (is 23 (memory* (+ before 2))))
2017 (prn "F - default-space implicitly modifies variable locations"))))
2021 (new-trace "set-default-space-skips-offset")
2024 (default-space:space-address <- new space:literal 2:literal)
2025 (1:integer <- copy 23:offset)
2027 (let routine make-routine!main
2028 (enq routine running-routines*)
2029 (let before rep.routine!alloc
2030 ;? (set dump-trace*)
2033 (when (~and (~is 23 memory*.1)
2034 (is 23 (memory* (+ before 2))))
2035 (prn "F - default-space skips 'offset' types just like literals"))))
2038 (new-trace "default-space-bounds-check")
2041 (default-space:space-address <- new space:literal 2:literal)
2042 (2:integer <- copy 23:literal)
2044 ;? (set dump-trace*)
2047 (let routine (car completed-routines*)
2048 (when (no rep.routine!error)
2049 (prn "F - default-space checks bounds")))
2052 (new-trace "default-space-and-get-indirect")
2055 (default-space:space-address <- new space:literal 5:literal)
2056 (1:integer-boolean-pair-address <- new integer-boolean-pair:literal)
2057 (2:integer-address <- get-address 1:integer-boolean-pair-address/deref 0:offset)
2058 (2:integer-address/deref <- copy 34:literal)
2059 (3:integer/raw <- get 1:integer-boolean-pair-address/deref 0:offset)
2061 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "cvt0" "cvt1")))
2064 ;? (prn completed-routines*)
2065 (each routine completed-routines*
2066 (aif rep.routine!error (prn "error - " it)))
2067 (when (~is 34 memory*.3)
2068 (prn "F - indirect 'get' works in the presence of default-space"))
2072 (new-trace "default-space-and-index-indirect")
2075 (default-space:space-address <- new space:literal 5:literal)
2076 (1:integer-array-address <- new integer-array:literal 4:literal)
2077 (2:integer-address <- index-address 1:integer-array-address/deref 2:offset)
2078 (2:integer-address/deref <- copy 34:literal)
2079 (3:integer/raw <- index 1:integer-array-address/deref 2:offset)
2081 ;? (= dump-trace* (obj whitelist '("run" "array-info")))
2084 ;? (prn completed-routines*)
2085 (each routine completed-routines*
2086 (aif rep.routine!error (prn "error - " it)))
2087 (when (~is 34 memory*.3)
2088 (prn "F - indirect 'index' works in the presence of default-space"))
2092 (new-trace "convert-names-default-space")
2094 (when (~iso (convert-names
2095 '((((x integer)) <- ((copy)) ((4 literal)))
2096 (((y integer)) <- ((copy)) ((2 literal)))
2097 ; unsafe in general; don't write random values to 'default-space'
2098 (((default-space integer)) <- ((add)) ((x integer)) ((y integer)))))
2099 '((((1 integer)) <- ((copy)) ((4 literal)))
2100 (((2 integer)) <- ((copy)) ((2 literal)))
2101 (((default-space integer)) <- ((add)) ((1 integer)) ((2 integer)))))
2102 (prn "F - convert-names never renames default-space"))
2105 (new-trace "suppress-default-space")
2108 (default-space:space-address <- new space:literal 2:literal)
2109 (1:integer/raw <- copy 23:literal)
2111 (let routine make-routine!main
2112 (enq routine running-routines*)
2113 (let before rep.routine!alloc
2114 ;? (set dump-trace*)
2117 (when (~and (is 23 memory*.1)
2118 (~is 23 (memory* (+ before 1))))
2119 (prn "F - default-space skipped for locations with metadata 'raw'"))))
2123 (new-trace "array-copy-indirect-scoped")
2126 (10:integer <- copy 30:literal) ; pretend allocation
2127 (default-space:space-address <- copy 10:literal) ; unsafe
2128 (1:integer <- copy 2:literal) ; raw location 12
2129 (2:integer <- copy 23:literal)
2130 (3:boolean <- copy nil:literal)
2131 (4:integer <- copy 24:literal)
2132 (5:boolean <- copy t:literal)
2133 (6:integer-boolean-pair-array-address <- copy 12:literal) ; unsafe
2134 (7:integer-boolean-pair-array <- copy 6:integer-boolean-pair-array-address/deref)
2136 ;? (set dump-trace*)
2137 ;? (= dump-trace* (obj whitelist '("run" "mem" "sizeof")))
2140 (each routine completed-routines*
2141 (aif rep.routine!error (prn "error - " it)))
2142 (when (~iso memory*.18 2) ; variable 7
2143 (prn "F - indirect array copy in the presence of 'default-space'"))
2147 (new-trace "len-array-indirect-scoped")
2150 (10:integer <- copy 30:literal) ; pretend allocation
2151 (default-space:space-address <- copy 10:literal) ; unsafe
2152 (1:integer <- copy 2:literal) ; raw location 12
2153 (2:integer <- copy 23:literal)
2154 (3:boolean <- copy nil:literal)
2155 (4:integer <- copy 24:literal)
2156 (5:boolean <- copy t:literal)
2157 (6:integer-address <- copy 12:literal) ; unsafe
2158 (7:integer <- length 6:integer-boolean-pair-array-address/deref)
2160 ;? (= dump-trace* (obj whitelist '("run" "addr" "sz" "array-len")))
2163 (when (~iso memory*.18 2)
2164 (prn "F - 'len' accesses length of array address"))
2168 (new-trace "default-space-shared")
2170 '((function init-counter [
2171 (default-space:space-address <- new space:literal 30:literal)
2172 (1:integer <- copy 3:literal) ; initialize to 3
2173 (reply default-space:space-address)
2175 (function increment-counter [
2176 (default-space:space-address <- next-input)
2177 (1:integer <- add 1:integer 1:literal) ; increment
2181 (1:space-address <- init-counter)
2182 (2:integer <- increment-counter 1:space-address)
2183 (3:integer <- increment-counter 1:space-address)
2186 (each routine completed-routines*
2187 (aif rep.routine!error (prn "error - " it)))
2189 (when (or (~is memory*.2 4)
2191 (prn "F - multiple calls to a function can share locals"))
2195 (new-trace "default-space-closure")
2197 '((function init-counter [
2198 (default-space:space-address <- new space:literal 30:literal)
2199 (1:integer <- copy 3:literal) ; initialize to 3
2200 (reply default-space:space-address)
2202 (function increment-counter [
2203 (default-space:space-address <- new space:literal 30:literal)
2204 (0:space-address <- next-input) ; share outer space
2205 (1:integer/space:1 <- add 1:integer/space:1 1:literal) ; increment
2206 (1:integer <- copy 34:literal) ; dummy
2207 (reply 1:integer/space:1)
2210 (1:space-address <- init-counter)
2211 (2:integer <- increment-counter 1:space-address)
2212 (3:integer <- increment-counter 1:space-address)
2214 ;? (set dump-trace*)
2216 (each routine completed-routines*
2217 (aif rep.routine!error (prn "error - " it)))
2219 (when (or (~is memory*.2 4)
2221 (prn "F - closures using /space metadata"))
2225 (new-trace "default-space-closure-with-names")
2227 '((function init-counter [
2228 (default-space:space-address <- new space:literal 30:literal)
2229 (x:integer <- copy 23:literal)
2230 (y:integer <- copy 3:literal) ; correct copy of y
2231 (reply default-space:space-address)
2233 (function increment-counter [
2234 (default-space:space-address <- new space:literal 30:literal)
2235 (0:space-address/names:init-counter <- next-input) ; outer space must be created by 'init-counter' above
2236 (y:integer/space:1 <- add y:integer/space:1 1:literal) ; increment
2237 (y:integer <- copy 34:literal) ; dummy
2238 (reply y:integer/space:1)
2241 (1:space-address/names:init-counter <- init-counter)
2242 (2:integer <- increment-counter 1:space-address/names:init-counter)
2243 (3:integer <- increment-counter 1:space-address/names:init-counter)
2245 ;? (set dump-trace*)
2247 (each routine completed-routines*
2248 (aif rep.routine!error (prn "error - " it)))
2250 (when (or (~is memory*.2 4)
2252 (prn "F - /names to name variables in outer spaces"))
2256 (new-trace "default-space-shared-with-names")
2259 (default-space:space-address <- new space:literal 30:literal)
2260 (x:integer <- copy 3:literal)
2261 (y:integer <- copy 4:literal)
2262 (reply default-space:space-address)
2265 (default-space:space-address/names:f <- next-input)
2266 (y:integer <- add y:integer 1:literal)
2267 (x:integer <- add x:integer 2:literal)
2268 (reply x:integer y:integer)
2271 (1:space-address <- f)
2272 (2:integer 3:integer <- g 1:space-address)
2275 (each routine completed-routines*
2276 (aif rep.routine!error (prn "error - " it)))
2277 (when (or (~is memory*.2 5)
2279 (prn "F - override names for the default space"))
2282 (new-trace "default-space-shared-with-extra-names")
2285 (default-space:space-address <- new space:literal 30:literal)
2286 (x:integer <- copy 3:literal)
2287 (y:integer <- copy 4:literal)
2288 (reply default-space:space-address)
2291 (default-space:space-address/names:f <- next-input)
2292 (y:integer <- add y:integer 1:literal)
2293 (x:integer <- add x:integer 2:literal)
2294 (z:integer <- add x:integer y:integer)
2298 (1:space-address <- f)
2299 (2:integer <- g 1:space-address)
2302 (each routine completed-routines*
2303 (aif rep.routine!error (prn "error - " it)))
2304 (when (~is memory*.2 10)
2305 (prn "F - shared spaces can add new names"))
2308 (new-trace "default-space-shared-extra-names-dont-overlap-bindings")
2311 (default-space:space-address <- new space:literal 30:literal)
2312 (x:integer <- copy 3:literal)
2313 (y:integer <- copy 4:literal)
2314 (reply default-space:space-address)
2317 (default-space:space-address/names:f <- next-input)
2318 (y:integer <- add y:integer 1:literal)
2319 (x:integer <- add x:integer 2:literal)
2320 (z:integer <- copy 2:literal)
2321 (reply x:integer y:integer)
2324 (1:space-address <- f)
2325 (2:integer 3:integer <- g 1:space-address)
2328 (each routine completed-routines*
2329 (aif rep.routine!error (prn "error - " it)))
2330 ;? (prn memory*) ;? 1
2331 (when (or (~is memory*.2 5)
2333 (prn "F - new names in shared spaces don't override old ones"))
2342 ; Putting it all together, here's how you define generic functions that run
2343 ; different code based on the types of their args.
2346 (new-trace "dispatch-clause")
2347 ;? (set dump-trace*)
2350 ; doesn't matter too much how many locals you allocate space for (here 20)
2351 ; if it's slightly too many -- memory is plentiful
2352 ; if it's too few -- mu will raise an error
2353 (default-space:space-address <- new space:literal 20:literal)
2354 (first-arg-box:tagged-value-address <- next-input)
2355 ; if given integers, add them
2357 (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
2358 (break-unless match?:boolean)
2359 (second-arg-box:tagged-value-address <- next-input)
2360 (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
2361 (result:integer <- add first-arg:integer second-arg:integer)
2362 (reply result:integer)
2367 (1:tagged-value-address <- init-tagged-value integer:literal 34:literal)
2368 (2:tagged-value-address <- init-tagged-value integer:literal 3:literal)
2369 (3:integer <- test1 1:tagged-value-address 2:tagged-value-address)
2373 (when (~is memory*.3 37)
2374 (prn "F - an example function that checks that its oarg is an integer"))
2378 (new-trace "dispatch-multiple-clauses")
2379 ;? (set dump-trace*)
2382 (default-space:space-address <- new space:literal 20:literal)
2383 (first-arg-box:tagged-value-address <- next-input)
2384 ; if given integers, add them
2386 (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
2387 (break-unless match?:boolean)
2388 (second-arg-box:tagged-value-address <- next-input)
2389 (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
2390 (result:integer <- add first-arg:integer second-arg:integer)
2391 (reply result:integer)
2393 ; if given booleans, or them (it's a silly kind of generic function)
2395 (first-arg:boolean match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref boolean:literal)
2396 (break-unless match?:boolean)
2397 (second-arg-box:tagged-value-address <- next-input)
2398 (second-arg:boolean <- maybe-coerce second-arg-box:tagged-value-address/deref boolean:literal)
2399 (result:boolean <- or first-arg:boolean second-arg:boolean)
2400 (reply result:integer)
2405 (1:tagged-value-address <- init-tagged-value boolean:literal t:literal)
2406 (2:tagged-value-address <- init-tagged-value boolean:literal nil:literal)
2407 (3:boolean <- test1 1:tagged-value-address 2:tagged-value-address)
2409 ;? (each stmt function*!test-fn
2412 ;? (wipe dump-trace*)
2414 (when (~is memory*.3 t)
2415 (prn "F - an example function that can do different things (dispatch) based on the type of its args or oargs"))
2419 (new-trace "dispatch-multiple-calls")
2422 (default-space:space-address <- new space:literal 20:literal)
2423 (first-arg-box:tagged-value-address <- next-input)
2424 ; if given integers, add them
2426 (first-arg:integer match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref integer:literal)
2427 (break-unless match?:boolean)
2428 (second-arg-box:tagged-value-address <- next-input)
2429 (second-arg:integer <- maybe-coerce second-arg-box:tagged-value-address/deref integer:literal)
2430 (result:integer <- add first-arg:integer second-arg:integer)
2431 (reply result:integer)
2433 ; if given booleans, or them (it's a silly kind of generic function)
2435 (first-arg:boolean match?:boolean <- maybe-coerce first-arg-box:tagged-value-address/deref boolean:literal)
2436 (break-unless match?:boolean)
2437 (second-arg-box:tagged-value-address <- next-input)
2438 (second-arg:boolean <- maybe-coerce second-arg-box:tagged-value-address/deref boolean:literal)
2439 (result:boolean <- or first-arg:boolean second-arg:boolean)
2440 (reply result:integer)
2445 (1:tagged-value-address <- init-tagged-value boolean:literal t:literal)
2446 (2:tagged-value-address <- init-tagged-value boolean:literal nil:literal)
2447 (3:boolean <- test1 1:tagged-value-address 2:tagged-value-address)
2448 (10:tagged-value-address <- init-tagged-value integer:literal 34:literal)
2449 (11:tagged-value-address <- init-tagged-value integer:literal 3:literal)
2450 (12:integer <- test1 10:tagged-value-address 11:tagged-value-address)
2454 (when (~and (is memory*.3 t) (is memory*.12 37))
2455 (prn "F - different calls can exercise different clauses of the same function"))
2457 ; We can also dispatch based on the type of the operands or results at the
2461 (new-trace "dispatch-otype")
2464 (4:type <- otype 0:offset)
2466 (5:boolean <- equal 4:type integer:literal)
2467 (break-unless 5:boolean)
2468 (6:integer <- next-input)
2469 (7:integer <- next-input)
2470 (8:integer <- add 6:integer 7:integer)
2475 (1:integer <- test1 1:literal 3:literal)
2479 (when (~iso memory*.1 4)
2480 (prn "F - an example function that checks that its oarg is an integer"))
2484 (new-trace "dispatch-otype-multiple-clauses")
2485 ;? (set dump-trace*)
2488 (4:type <- otype 0:offset)
2490 ; integer needed? add args
2491 (5:boolean <- equal 4:type integer:literal)
2492 (break-unless 5:boolean)
2493 (6:integer <- next-input)
2494 (7:integer <- next-input)
2495 (8:integer <- add 6:integer 7:integer)
2499 ; boolean needed? 'or' args
2500 (5:boolean <- equal 4:type boolean:literal)
2501 (break-unless 5:boolean 4:offset)
2502 (6:boolean <- next-input)
2503 (7:boolean <- next-input)
2504 (8:boolean <- or 6:boolean 7:boolean)
2508 (1:boolean <- test1 t:literal t:literal)
2510 ;? (each stmt function*!test1
2513 ;? (wipe dump-trace*)
2515 (when (~is memory*.1 t)
2516 (prn "F - an example function that can do different things (dispatch) based on the type of its args or oargs"))
2520 (new-trace "dispatch-otype-multiple-calls")
2523 (4:type <- otype 0:offset)
2525 (5:boolean <- equal 4:type integer:literal)
2526 (break-unless 5:boolean)
2527 (6:integer <- next-input)
2528 (7:integer <- next-input)
2529 (8:integer <- add 6:integer 7:integer)
2533 (5:boolean <- equal 4:type boolean:literal)
2534 (break-unless 5:boolean)
2535 (6:boolean <- next-input)
2536 (7:boolean <- next-input)
2537 (8:boolean <- or 6:boolean 7:boolean)
2541 (1:boolean <- test1 t:literal t:literal)
2542 (2:integer <- test1 3:literal 4:literal)
2546 (when (~and (is memory*.1 t) (is memory*.2 7))
2547 (prn "F - different calls can exercise different clauses of the same function"))
2555 ; A rudimentary process scheduler. You can 'run' multiple functions at once,
2556 ; and they share the virtual processor.
2558 ; There's also a 'fork' primitive to let functions create new threads of
2559 ; execution (we call them routines).
2561 ; Eventually we want to allow callers to influence how much of their CPU they
2562 ; give to their 'children', or to rescind a child's running privileges.
2565 (new-trace "scheduler")
2569 (1:integer <- copy 3:literal)
2572 (2:integer <- copy 4:literal)
2575 (when (~iso 2 curr-cycle*)
2576 (prn "F - scheduler didn't run the right number of instructions: " curr-cycle*))
2577 (when (~iso memory* (obj 1 3 2 4))
2578 (prn "F - scheduler runs multiple functions: " memory*))
2579 (check-trace-contents "scheduler orders functions correctly"
2583 (check-trace-contents "scheduler orders schedule and run events correctly"
2591 (new-trace "scheduler-alternate")
2595 (1:integer <- copy 0:literal)
2596 (1:integer <- copy 0:literal)
2599 (2:integer <- copy 0:literal)
2600 (2:integer <- copy 0:literal)
2602 ;? (= dump-trace* (obj whitelist '("schedule")))
2603 (= scheduling-interval* 1)
2605 (check-trace-contents "scheduler alternates between routines"
2613 (new-trace "scheduler-sleep")
2617 (1:integer <- copy 0:literal)
2620 (2:integer <- copy 0:literal)
2622 ; add one baseline routine to run (empty running-routines* handled below)
2623 (enq make-routine!f1 running-routines*)
2624 (assert (is 1 len.running-routines*))
2626 (let routine make-routine!f2
2627 (= rep.routine!sleep '(until 23))
2628 (set sleeping-routines*.routine))
2629 ; not yet time for it to wake up
2631 ;? (set dump-trace*)
2632 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
2633 (update-scheduler-state)
2634 (when (~is 1 len.running-routines*)
2635 (prn "F - scheduler lets routines sleep"))
2638 (new-trace "scheduler-wakeup")
2642 (1:integer <- copy 0:literal)
2645 (2:integer <- copy 0:literal)
2647 ; add one baseline routine to run (empty running-routines* handled below)
2648 (enq make-routine!f1 running-routines*)
2649 (assert (is 1 len.running-routines*))
2651 (let routine make-routine!f2
2652 (= rep.routine!sleep '(until 23))
2653 (set sleeping-routines*.routine))
2654 ; time for it to wake up
2656 (update-scheduler-state)
2657 (when (~is 2 len.running-routines*)
2658 (prn "F - scheduler wakes up sleeping routines at the right time"))
2661 (new-trace "scheduler-sleep-location")
2665 (1:integer <- copy 0:literal)
2668 (2:integer <- copy 0:literal)
2670 ; add one baseline routine to run (empty running-routines* handled below)
2671 (enq make-routine!f1 running-routines*)
2672 (assert (is 1 len.running-routines*))
2673 ; blocked routine waiting for location 23 to change
2674 (let routine make-routine!f2
2675 (= rep.routine!sleep '(until-location-changes 23 0))
2676 (set sleeping-routines*.routine))
2677 ; leave memory location 23 unchanged
2680 ;? (prn running-routines*)
2681 ;? (prn sleeping-routines*)
2682 ;? (set dump-trace*)
2683 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
2684 (update-scheduler-state)
2685 ;? (prn running-routines*)
2686 ;? (prn sleeping-routines*)
2687 ; routine remains blocked
2688 (when (~is 1 len.running-routines*)
2689 (prn "F - scheduler lets routines block on locations"))
2693 (new-trace "scheduler-wakeup-location")
2697 (1:integer <- copy 0:literal)
2700 (2:integer <- copy 0:literal)
2702 ; add one baseline routine to run (empty running-routines* handled below)
2703 (enq make-routine!f1 running-routines*)
2704 (assert (is 1 len.running-routines*))
2705 ; blocked routine waiting for location 23 to change
2706 (let routine make-routine!f2
2707 (= rep.routine!sleep '(until-location-changes 23 0))
2708 (set sleeping-routines*.routine))
2709 ; change memory location 23
2711 (update-scheduler-state)
2713 (when (~is 2 len.running-routines*)
2714 (prn "F - scheduler unblocks routines blocked on locations"))
2717 (new-trace "scheduler-skip")
2721 (1:integer <- copy 0:literal)
2723 ; running-routines* is empty
2724 (assert (empty running-routines*))
2726 (let routine make-routine!f1
2727 (= rep.routine!sleep '(until 34))
2728 (set sleeping-routines*.routine))
2729 ; long time left for it to wake up
2731 (update-scheduler-state)
2732 ;? (prn curr-cycle*)
2733 (assert (is curr-cycle* 35))
2734 (when (~is 1 len.running-routines*)
2735 (prn "F - scheduler skips ahead to earliest sleeping routines when nothing to run"))
2738 (new-trace "scheduler-deadlock")
2742 (1:integer <- copy 0:literal)
2744 (assert (empty running-routines*))
2745 (assert (empty completed-routines*))
2747 (let routine make-routine!f1
2748 (= rep.routine!sleep '(until-location-changes 23 0))
2749 (set sleeping-routines*.routine))
2750 ; location it's waiting on is 'unchanged'
2752 (update-scheduler-state)
2753 (assert (~empty completed-routines*))
2754 ;? (prn completed-routines*)
2755 (let routine completed-routines*.0
2756 (when (~posmatch "deadlock" rep.routine!error)
2757 (prn "F - scheduler detects deadlock")))
2761 (new-trace "scheduler-deadlock2")
2765 (1:integer <- copy 0:literal)
2767 ; running-routines* is empty
2768 (assert (empty running-routines*))
2770 (let routine make-routine!f1
2771 (= rep.routine!sleep '(until-location-changes 23 0))
2772 (set sleeping-routines*.routine))
2773 ; but is about to become ready
2775 (update-scheduler-state)
2776 (when (~empty completed-routines*)
2777 (prn "F - scheduler ignores sleeping but ready threads when detecting deadlock"))
2779 ; Helper routines are just to sidestep the deadlock test; they stop running
2780 ; when there's no non-helper routines left to run.
2782 ; Be careful not to overuse them. In particular, the component under test
2783 ; should never run in a helper routine; that'll make interrupting and
2784 ; restarting it very brittle.
2786 (new-trace "scheduler-helper")
2790 (1:integer <- copy 0:literal)
2792 ; just a helper routine
2793 (= routine* make-routine!f1)
2794 (set rep.routine*!helper)
2795 ;? (= dump-trace* (obj whitelist '("schedule")))
2796 (update-scheduler-state)
2797 (when (or (~empty running-routines*) (~empty sleeping-routines*))
2798 (prn "F - scheduler stops when there's only helper routines left"))
2801 (new-trace "scheduler-helper-sleeping")
2805 (1:integer <- copy 0:literal)
2807 ; just a helper routine
2808 (let routine make-routine!f1
2809 (set rep.routine!helper)
2810 (= rep.routine!sleep '(until-location-changes 23 nil))
2811 (set sleeping-routines*.routine))
2812 ;? (= dump-trace* (obj whitelist '("schedule")))
2813 ;? (prn "1 " running-routines*)
2814 ;? (prn sleeping-routines*)
2815 (update-scheduler-state)
2816 ;? (prn "2 " running-routines*)
2817 ;? (prn sleeping-routines*)
2818 (when (or (~empty running-routines*) (~empty sleeping-routines*))
2819 (prn "F - scheduler stops when there's only sleeping helper routines left"))
2822 (new-trace "scheduler-termination")
2826 (1:integer <- copy 0:literal)
2829 (update-scheduler-state)
2830 (check-trace-doesnt-contain "scheduler helper check shouldn't trigger unless necessary"
2831 '(("schedule" "just helpers left")))
2833 ; both running and sleeping helpers
2834 ; running helper and sleeping non-helper
2835 ; sleeping helper and running non-helper
2838 (new-trace "scheduler-account-slice")
2839 ; function running an infinite loop
2843 (1:integer <- copy 0:literal)
2847 (let routine make-routine!f1
2848 (= rep.routine!limit 10)
2849 (enq routine running-routines*))
2850 (= scheduling-interval* 20)
2852 (when (or (empty completed-routines*)
2853 (~is -10 ((rep completed-routines*.0) 'limit)))
2854 (prn "F - when given a low cycle limit, a routine runs to end of time slice"))
2857 (new-trace "scheduler-account-slice-multiple")
2858 ; function running an infinite loop
2862 (1:integer <- copy 0:literal)
2866 (let routine make-routine!f1
2867 (= rep.routine!limit 100)
2868 (enq routine running-routines*))
2869 (= scheduling-interval* 20)
2871 (when (or (empty completed-routines*)
2872 (~is -0 ((rep completed-routines*.0) 'limit)))
2873 (prn "F - when given a high limit, a routine successfully stops after multiple time slices"))
2876 (new-trace "scheduler-account-run-while-asleep")
2878 ; f1 needs 4 cycles of sleep time, 4 cycles of work
2880 (sleep for-some-cycles:literal 4:literal)
2881 (i:integer <- copy 0:literal)
2882 (i:integer <- copy 0:literal)
2883 (i:integer <- copy 0:literal)
2884 (i:integer <- copy 0:literal)
2886 (let routine make-routine!f1
2887 (= rep.routine!limit 6) ; enough time excluding sleep
2888 (enq routine running-routines*))
2889 (= scheduling-interval* 1)
2890 ;? (= dump-trace* (obj whitelist '("schedule")))
2892 ; if time slept counts against limit, routine doesn't have time to complete
2893 (when (ran-to-completion 'f1)
2894 (prn "F - time slept counts against a routine's cycle limit"))
2898 (new-trace "scheduler-account-stop-on-preempt")
2900 '((function baseline [
2901 (i:integer <- copy 0:literal)
2903 (done?:boolean <- greater-or-equal i:integer 10:literal)
2904 (break-if done?:boolean)
2905 (1:integer <- add i:integer 1:literal)
2910 (i:integer <- copy 0:literal)
2912 (done?:boolean <- greater-or-equal i:integer 6:literal)
2913 (break-if done?:boolean)
2914 (1:integer <- add i:integer 1:literal)
2918 (let routine make-routine!baseline
2919 (enq routine running-routines*))
2920 ; now add the routine we care about
2921 (let routine make-routine!f1
2922 (= rep.routine!limit 40) ; less than 2x time f1 needs to complete
2923 (enq routine running-routines*))
2924 (= scheduling-interval* 1)
2925 ; if baseline's time were to count against f1's limit, it wouldn't be able to
2927 (when (~ran-to-completion 'f1)
2928 (prn "F - preempted time doesn't count against a routine's limit"))
2932 (new-trace "scheduler-sleep-timeout")
2934 '((function baseline [
2935 (i:integer <- copy 0:literal)
2937 (done?:boolean <- greater-or-equal i:integer 10:literal)
2938 (break-if done?:boolean)
2939 (1:integer <- add i:integer 1:literal)
2944 (sleep for-some-cycles:literal 10:literal) ; less time than baseline would take to run
2946 ; add baseline routine to prevent cycle-skipping
2947 (let routine make-routine!baseline
2948 (enq routine running-routines*))
2949 ; now add the routine we care about
2950 (let routine make-routine!f1
2951 (= rep.routine!limit 4) ; less time than f1 would take to run
2952 (enq routine running-routines*))
2953 (= scheduling-interval* 1)
2954 ;? (= dump-trace* (obj whitelist '("schedule")))
2956 (when (ran-to-completion 'f1)
2957 (prn "F - sleeping routines can time out"))
2964 (sleep for-some-cycles:literal 1:literal)
2965 (1:integer <- copy 0:literal)
2966 (1:integer <- copy 0:literal)
2969 (2:integer <- copy 0:literal)
2970 (2:integer <- copy 0:literal)
2972 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
2974 (check-trace-contents "scheduler handles sleeping routines"
2976 ("run" "sleeping until 2")
2977 ("schedule" "pushing f1 to sleep queue")
2980 ("schedule" "waking up f1")
2986 (new-trace "sleep-long")
2989 (sleep for-some-cycles:literal 20:literal)
2990 (1:integer <- copy 0:literal)
2991 (1:integer <- copy 0:literal)
2994 (2:integer <- copy 0:literal)
2995 (2:integer <- copy 0:literal)
2997 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
2999 (check-trace-contents "scheduler progresses sleeping routines when there are no routines left to run"
3001 ("run" "sleeping until 21")
3002 ("schedule" "pushing f1 to sleep queue")
3005 ("schedule" "waking up f1")
3011 (new-trace "sleep-location")
3014 ; waits for memory location 1 to be set, before computing its successor
3015 (1:integer <- copy 0:literal)
3016 (sleep until-location-changes:literal 1:integer)
3017 (2:integer <- add 1:integer 1:literal)
3020 (sleep for-some-cycles:literal 30:literal)
3021 (1:integer <- copy 3:literal) ; set to value
3023 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
3024 ;? (set dump-trace*)
3026 ;? (prn int-canon.memory*)
3027 (each routine completed-routines*
3028 (aif rep.routine!error (prn "error - " it)))
3029 (when (~is memory*.2 4) ; successor of value
3030 (prn "F - sleep can block on a memory location"))
3034 (new-trace "sleep-scoped-location")
3037 ; waits for memory location 1 to be changed, before computing its successor
3038 (10:integer <- copy 5:literal) ; array of locals
3039 (default-space:space-address <- copy 10:literal)
3040 (1:integer <- copy 23:literal) ; really location 12
3041 (sleep until-location-changes:literal 1:integer)
3042 (2:integer <- add 1:integer 1:literal)
3045 (sleep for-some-cycles:literal 30:literal)
3046 (12:integer <- copy 3:literal) ; set to value
3048 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
3050 (when (~is memory*.13 4) ; successor of value
3051 (prn "F - sleep can block on a scoped memory location"))
3058 (1:integer <- copy 4:literal)
3064 (when (~iso memory*.1 4)
3065 (prn "F - fork works"))
3068 (new-trace "fork-returns-id")
3071 (1:integer <- copy 4:literal)
3074 (2:integer <- fork f1:fn)
3078 (when (no memory*.2)
3079 (prn "F - fork returns a pid for the new routine"))
3082 (new-trace "fork-returns-unique-id")
3085 (1:integer <- copy 4:literal)
3088 (2:integer <- fork f1:fn)
3089 (3:integer <- fork f1:fn)
3092 (when (or (no memory*.2)
3094 (is memory*.2 memory*.3))
3095 (prn "F - fork returns a unique pid everytime"))
3098 (new-trace "fork-with-args")
3101 (2:integer <- next-input)
3104 (fork f1:fn nil:literal/globals nil:literal/limit 4:literal)
3107 (when (~iso memory*.2 4)
3108 (prn "F - fork can pass args"))
3111 (new-trace "fork-copies-args")
3114 (2:integer <- next-input)
3117 (default-space:space-address <- new space:literal 5:literal)
3118 (x:integer <- copy 4:literal)
3119 (fork f1:fn nil:literal/globals nil:literal/limit x:integer)
3120 (x:integer <- copy 0:literal) ; should be ignored
3123 (when (~iso memory*.2 4)
3124 (prn "F - fork passes args by value"))
3127 (new-trace "fork-global")
3130 (1:integer/raw <- copy 2:integer/space:global)
3133 (default-space:space-address <- new space:literal 5:literal)
3134 (2:integer <- copy 4:literal)
3135 (fork f1:fn default-space:space-address/globals nil:literal/limit)
3138 (each routine completed-routines*
3139 (awhen rep.routine!error (prn "error - " it)))
3140 (when (~iso memory*.1 4)
3141 (prn "F - fork can take a space of global variables to access"))
3144 (new-trace "fork-limit")
3152 (fork f1:fn nil:literal/globals 30:literal/limit)
3154 (= scheduling-interval* 5)
3156 (each routine completed-routines*
3157 (awhen rep.routine!error (prn "error - " it)))
3158 (when (ran-to-completion 'f1)
3159 (prn "F - fork can specify a maximum cycle limit"))
3162 (new-trace "fork-then-wait")
3170 (1:integer/routine-id <- fork f1:fn nil:literal/globals 30:literal/limit)
3171 (sleep until-routine-done:literal 1:integer/routine-id)
3172 (2:integer <- copy 34:literal)
3174 (= scheduling-interval* 5)
3175 ;? (= dump-trace* (obj whitelist '("schedule")))
3177 (each routine completed-routines*
3178 (awhen rep.routine!error (prn "error - " it)))
3179 (check-trace-contents "scheduler orders functions correctly"
3180 '(("schedule" "pushing main to sleep queue")
3181 ("schedule" "scheduling f1")
3182 ("schedule" "ran out of time")
3183 ("schedule" "waking up main")
3187 ; todo: Haven't yet written several tests
3188 ; that restarting a routine works
3192 ; running multiple routines in tandem
3193 ; first example using these features: read-move-incomplete in chessboard-cursor.arc.t
3195 ; The scheduler needs to keep track of the call stack for each routine.
3196 ; Eventually we'll want to save this information in mu's address space itself,
3197 ; along with the types array, the magic buffers for args and oargs, and so on.
3199 ; Eventually we want the right stack-management primitives to build delimited
3200 ; continuations in mu.
3202 ; Routines can throw errors.
3204 (new-trace "array-bounds-check")
3207 (1:integer <- copy 2:literal)
3208 (2:integer <- copy 23:literal)
3209 (3:integer <- copy 24:literal)
3210 (4:integer <- index 1:integer-array 2:literal)
3212 ;? (set dump-trace*)
3215 (let routine (car completed-routines*)
3216 (when (no rep.routine!error)
3217 (prn "F - 'index' throws an error if out of bounds")))
3225 ; Mu synchronizes using channels rather than locks, like Erlang and Go.
3227 ; The two ends of a channel will usually belong to different routines, but
3228 ; each end should only be used by a single one. Don't try to read from or
3229 ; write to it from multiple routines at once.
3231 ; To avoid locking, writer and reader will never write to the same location.
3232 ; So channels will include fields in pairs, one for the writer and one for the
3235 ; The core circular buffer contains values at index 'first-full' up to (but
3236 ; not including) index 'first-empty'. The reader always modifies it at
3237 ; first-full, while the writer always modifies it at first-empty.
3239 (new-trace "channel-new")
3242 (1:channel-address <- init-channel 3:literal)
3243 (2:integer <- get 1:channel-address/deref first-full:offset)
3244 (3:integer <- get 1:channel-address/deref first-free:offset)
3246 ;? (set dump-trace*)
3249 (when (or (~is 0 memory*.2)
3251 (prn "F - 'init-channel' initializes 'first-full and 'first-free to 0"))
3254 (new-trace "channel-write")
3257 (1:channel-address <- init-channel 3:literal)
3258 (2:integer <- copy 34:literal)
3259 (3:tagged-value <- save-type 2:integer)
3260 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3261 (5:integer <- get 1:channel-address/deref first-full:offset)
3262 (6:integer <- get 1:channel-address/deref first-free:offset)
3264 ;? (prn function*!write)
3265 ;? (set dump-trace*)
3266 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
3267 ;? (= dump-trace* (obj whitelist '("jump")))
3268 ;? (= dump-trace* (obj whitelist '("run" "reply")))
3270 (each routine completed-routines*
3271 (aif rep.routine!error (prn "error - " it)))
3272 ;? (prn canon.memory*)
3273 (when (or (~is 0 memory*.5)
3275 (prn "F - 'write' enqueues item to channel"))
3279 (new-trace "channel-read")
3282 (1:channel-address <- init-channel 3:literal)
3283 (2:integer <- copy 34:literal)
3284 (3:tagged-value <- save-type 2:integer)
3285 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3286 (5:tagged-value 1:channel-address/deref <- read 1:channel-address)
3287 (7:integer <- maybe-coerce 5:tagged-value integer:literal)
3288 (8:integer <- get 1:channel-address/deref first-full:offset)
3289 (9:integer <- get 1:channel-address/deref first-free:offset)
3291 ;? (set dump-trace*)
3292 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
3294 ;? (prn int-canon.memory*)
3295 (when (~is memory*.7 34)
3296 (prn "F - 'read' returns written value"))
3297 (when (or (~is 1 memory*.8)
3299 (prn "F - 'read' dequeues item from channel"))
3302 (new-trace "channel-write-wrap")
3305 ; channel with 1 slot
3306 (1:channel-address <- init-channel 1:literal)
3308 (2:integer <- copy 34:literal)
3309 (3:tagged-value <- save-type 2:integer)
3310 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3311 ; first-free will now be 1
3312 (5:integer <- get 1:channel-address/deref first-free:offset)
3314 (_ 1:channel-address/deref <- read 1:channel-address)
3315 ; write a second value; verify that first-free wraps around to 0.
3316 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3317 (6:integer <- get 1:channel-address/deref first-free:offset)
3319 ;? (set dump-trace*)
3320 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
3322 ;? (prn canon.memory*)
3323 (when (or (~is 1 memory*.5)
3325 (prn "F - 'write' can wrap pointer back to start"))
3328 (new-trace "channel-read-wrap")
3331 ; channel with 1 slot
3332 (1:channel-address <- init-channel 1:literal)
3334 (2:integer <- copy 34:literal)
3335 (3:tagged-value <- save-type 2:integer)
3336 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3338 (_ 1:channel-address/deref <- read 1:channel-address)
3339 ; first-full will now be 1
3340 (5:integer <- get 1:channel-address/deref first-full:offset)
3341 ; write a second value
3342 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3343 ; read second value; verify that first-full wraps around to 0.
3344 (_ 1:channel-address/deref <- read 1:channel-address)
3345 (6:integer <- get 1:channel-address/deref first-full:offset)
3347 ;? (set dump-trace*)
3348 ;? (= dump-trace* (obj blacklist '("sz" "mem" "addr" "array-len" "cvt0" "cvt1")))
3350 ;? (prn canon.memory*)
3351 (when (or (~is 1 memory*.5)
3353 (prn "F - 'read' can wrap pointer back to start"))
3356 (new-trace "channel-new-empty-not-full")
3359 (1:channel-address <- init-channel 3:literal)
3360 (2:boolean <- empty? 1:channel-address/deref)
3361 (3:boolean <- full? 1:channel-address/deref)
3363 ;? (set dump-trace*)
3366 (when (or (~is t memory*.2)
3367 (~is nil memory*.3))
3368 (prn "F - a new channel is always empty, never full"))
3371 (new-trace "channel-write-not-empty")
3374 (1:channel-address <- init-channel 3:literal)
3375 (2:integer <- copy 34:literal)
3376 (3:tagged-value <- save-type 2:integer)
3377 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3378 (5:boolean <- empty? 1:channel-address/deref)
3379 (6:boolean <- full? 1:channel-address/deref)
3381 ;? (set dump-trace*)
3384 (when (or (~is nil memory*.5)
3385 (~is nil memory*.6))
3386 (prn "F - a channel after writing is never empty"))
3389 (new-trace "channel-write-full")
3392 (1:channel-address <- init-channel 1:literal)
3393 (2:integer <- copy 34:literal)
3394 (3:tagged-value <- save-type 2:integer)
3395 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3396 (5:boolean <- empty? 1:channel-address/deref)
3397 (6:boolean <- full? 1:channel-address/deref)
3399 ;? (set dump-trace*)
3402 (when (or (~is nil memory*.5)
3404 (prn "F - a channel after writing may be full"))
3407 (new-trace "channel-read-not-full")
3410 (1:channel-address <- init-channel 3:literal)
3411 (2:integer <- copy 34:literal)
3412 (3:tagged-value <- save-type 2:integer)
3413 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3414 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3415 (_ 1:channel-address/deref <- read 1:channel-address)
3416 (5:boolean <- empty? 1:channel-address/deref)
3417 (6:boolean <- full? 1:channel-address/deref)
3419 ;? (set dump-trace*)
3422 (when (or (~is nil memory*.5)
3423 (~is nil memory*.6))
3424 (prn "F - a channel after reading is never full"))
3427 (new-trace "channel-read-empty")
3430 (1:channel-address <- init-channel 3:literal)
3431 (2:integer <- copy 34:literal)
3432 (3:tagged-value <- save-type 2:integer)
3433 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3434 (_ 1:channel-address/deref <- read 1:channel-address)
3435 (5:boolean <- empty? 1:channel-address/deref)
3436 (6:boolean <- full? 1:channel-address/deref)
3438 ;? (set dump-trace*)
3441 (when (or (~is t memory*.5)
3442 (~is nil memory*.6))
3443 (prn "F - a channel after reading may be empty"))
3445 ; The key property of channels; writing to a full channel blocks the current
3446 ; routine until it creates space. Ditto reading from an empty channel.
3449 (new-trace "channel-read-block")
3452 (1:channel-address <- init-channel 3:literal)
3453 ; channel is empty, but receives a read
3454 (2:tagged-value 1:channel-address/deref <- read 1:channel-address)
3456 ;? (set dump-trace*)
3457 ;? (= dump-trace* (obj whitelist '("run" "schedule")))
3459 ;? (prn int-canon.memory*)
3460 ;? (prn sleeping-routines*)
3461 ;? (prn completed-routines*)
3462 ; read should cause the routine to sleep, and
3463 ; the sole sleeping routine should trigger the deadlock detector
3464 (let routine (car completed-routines*)
3465 (when (or (no routine)
3466 (no rep.routine!error)
3467 (~posmatch "deadlock" rep.routine!error))
3468 (prn "F - 'read' on empty channel blocks (puts the routine to sleep until the channel gets data)")))
3472 (new-trace "channel-write-block")
3475 (1:channel-address <- init-channel 1:literal)
3476 (2:integer <- copy 34:literal)
3477 (3:tagged-value <- save-type 2:integer)
3478 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3479 ; channel has capacity 1, but receives a second write
3480 (1:channel-address/deref <- write 1:channel-address 3:tagged-value)
3482 ;? (set dump-trace*)
3483 ;? (= dump-trace* (obj whitelist '("run" "schedule" "addr")))
3485 ;? (prn int-canon.memory*)
3486 ;? (prn running-routines*)
3487 ;? (prn sleeping-routines*)
3488 ;? (prn completed-routines*)
3489 ; second write should cause the routine to sleep, and
3490 ; the sole sleeping routine should trigger the deadlock detector
3491 (let routine (car completed-routines*)
3492 (when (or (no routine)
3493 (no rep.routine!error)
3494 (~posmatch "deadlock" rep.routine!error))
3495 (prn "F - 'write' on full channel blocks (puts the routine to sleep until the channel gets data)")))
3499 (new-trace "channel-handoff")
3501 '((function consumer [
3502 (default-space:space-address <- new space:literal 30:literal)
3503 (chan:channel-address <- init-channel 3:literal) ; create a channel
3504 (fork producer:fn nil:literal/globals nil:literal/limit chan:channel-address) ; fork a routine to produce a value in it
3505 (1:tagged-value/raw <- read chan:channel-address) ; wait for input on channel
3507 (function producer [
3508 (default-space:space-address <- new space:literal 30:literal)
3509 (n:integer <- copy 24:literal)
3510 (ochan:channel-address <- next-input)
3511 (x:tagged-value <- save-type n:integer)
3512 (ochan:channel-address/deref <- write ochan:channel-address x:tagged-value)
3514 ;? (set dump-trace*)
3515 ;? (= dump-trace* (obj whitelist '("schedule" "run" "addr")))
3516 ;? (= dump-trace* (obj whitelist '("-")))
3519 (each routine completed-routines*
3520 (aif rep.routine!error (prn "error - " it)))
3521 (when (~is 24 memory*.2) ; location 1 contains tagged-value x above
3522 (prn "F - channels are meant to be shared between routines"))
3526 (new-trace "channel-handoff-routine")
3528 '((function consumer [
3529 (default-space:space-address <- new space:literal 30:literal)
3530 (1:channel-address <- init-channel 3:literal) ; create a channel
3531 (fork producer:fn default-space:space-address/globals nil:literal/limit) ; pass it as a global to another routine
3532 (1:tagged-value/raw <- read 1:channel-address) ; wait for input on channel
3534 (function producer [
3535 (default-space:space-address <- new space:literal 30:literal)
3536 (n:integer <- copy 24:literal)
3537 (x:tagged-value <- save-type n:integer)
3538 (1:channel-address/space:global/deref <- write 1:channel-address/space:global x:tagged-value)
3541 (each routine completed-routines*
3542 (aif rep.routine!error (prn "error - " it)))
3543 (when (~is 24 memory*.2) ; location 1 contains tagged-value x above
3544 (prn "F - channels are meant to be shared between routines"))
3550 ;; Separating concerns
3552 ; Lightweight tools can also operate on quoted lists of statements surrounded
3553 ; by square brackets. In the example below, we mimic Go's 'defer' keyword
3554 ; using 'convert-quotes'. It lets us write code anywhere in a function, but
3555 ; have it run just before the function exits. Great for keeping code to
3556 ; reclaim memory or other resources close to the code to allocate it. (C++
3557 ; programmers know this as RAII.) We'll use 'defer' when we build a memory
3558 ; deallocation routine like C's 'free'.
3560 ; More powerful reorderings are also possible like in Literate Programming or
3561 ; Aspect-Oriented Programming; one advantage of prohibiting arbitrarily nested
3562 ; code is that we can naturally name 'join points' wherever we want.
3565 (new-trace "convert-quotes-defer")
3567 (when (~iso (convert-quotes
3568 '((1:integer <- copy 4:literal)
3570 (3:integer <- copy 6:literal)
3572 (2:integer <- copy 5:literal)))
3573 '((1:integer <- copy 4:literal)
3574 (2:integer <- copy 5:literal)
3575 (3:integer <- copy 6:literal)))
3576 (prn "F - convert-quotes can handle 'defer'"))
3579 (new-trace "convert-quotes-defer-reply")
3581 (when (~iso (convert-quotes
3582 '((1:integer <- copy 0:literal)
3584 (5:integer <- copy 0:literal)
3586 (2:integer <- copy 0:literal)
3588 (3:integer <- copy 0:literal)
3589 (4:integer <- copy 0:literal)))
3590 '((1:integer <- copy 0:literal)
3591 (2:integer <- copy 0:literal)
3592 (5:integer <- copy 0:literal)
3594 (3:integer <- copy 0:literal)
3595 (4:integer <- copy 0:literal)
3596 (5:integer <- copy 0:literal)))
3597 (prn "F - convert-quotes inserts code at early exits"))
3600 (new-trace "convert-quotes-defer-reply-arg")
3602 (when (~iso (convert-quotes
3603 '((1:integer <- copy 0:literal)
3605 (5:integer <- copy 0:literal)
3607 (2:integer <- copy 0:literal)
3609 (3:integer <- copy 0:literal)
3610 (4:integer <- copy 0:literal)))
3611 '((1:integer <- copy 0:literal)
3612 (2:integer <- copy 0:literal)
3613 (prepare-reply 2:literal)
3614 (5:integer <- copy 0:literal)
3616 (3:integer <- copy 0:literal)
3617 (4:integer <- copy 0:literal)
3618 (5:integer <- copy 0:literal)))
3619 (prn "F - convert-quotes inserts code at early exits"))
3622 (new-trace "convert-quotes-label")
3624 (when (~iso (convert-quotes
3625 '((1:integer <- copy 4:literal)
3627 (2:integer <- copy 5:literal)))
3628 '((1:integer <- copy 4:literal)
3630 (2:integer <- copy 5:literal)))
3631 (prn "F - convert-quotes can handle labels"))
3634 (new-trace "before")
3638 (2:integer <- copy 0:literal)
3640 (when (~iso (as cons before*!label1)
3643 (2:integer <- copy 0:literal))))
3644 (prn "F - 'before' records fragments of code to insert before labels"))
3646 (when (~iso (insert-code
3647 '((1:integer <- copy 0:literal)
3649 (3:integer <- copy 0:literal)))
3650 '((1:integer <- copy 0:literal)
3651 (2:integer <- copy 0:literal)
3653 (3:integer <- copy 0:literal)))
3654 (prn "F - 'insert-code' can insert fragments before labels"))
3657 (new-trace "before-multiple")
3661 (2:integer <- copy 0:literal)
3664 (3:integer <- copy 0:literal)
3666 (when (~iso (as cons before*!label1)
3669 (2:integer <- copy 0:literal))
3671 (3:integer <- copy 0:literal))))
3672 (prn "F - 'before' records fragments in order"))
3674 (when (~iso (insert-code
3675 '((1:integer <- copy 0:literal)
3677 (4:integer <- copy 0:literal)))
3678 '((1:integer <- copy 0:literal)
3679 (2:integer <- copy 0:literal)
3680 (3:integer <- copy 0:literal)
3682 (4:integer <- copy 0:literal)))
3683 (prn "F - 'insert-code' can insert multiple fragments in order before label"))
3686 (new-trace "before-scoped")
3689 '((before f/label1 [ ; label1 only inside function f
3690 (2:integer <- copy 0:literal)
3692 (when (~iso (insert-code
3693 '((1:integer <- copy 0:literal)
3695 (3:integer <- copy 0:literal))
3697 '((1:integer <- copy 0:literal)
3698 (2:integer <- copy 0:literal)
3700 (3:integer <- copy 0:literal)))
3701 (prn "F - 'insert-code' can insert fragments before labels just in specified functions"))
3704 (new-trace "before-scoped2")
3707 '((before f/label1 [ ; label1 only inside function f
3708 (2:integer <- copy 0:literal)
3710 (when (~iso (insert-code
3711 '((1:integer <- copy 0:literal)
3713 (3:integer <- copy 0:literal)))
3714 '((1:integer <- copy 0:literal)
3716 (3:integer <- copy 0:literal)))
3717 (prn "F - 'insert-code' ignores labels not in specified functions"))
3724 (2:integer <- copy 0:literal)
3726 (when (~iso (as cons after*!label1)
3729 (2:integer <- copy 0:literal))))
3730 (prn "F - 'after' records fragments of code to insert after labels"))
3732 (when (~iso (insert-code
3733 '((1:integer <- copy 0:literal)
3735 (3:integer <- copy 0:literal)))
3736 '((1:integer <- copy 0:literal)
3738 (2:integer <- copy 0:literal)
3739 (3:integer <- copy 0:literal)))
3740 (prn "F - 'insert-code' can insert fragments after labels"))
3743 (new-trace "after-multiple")
3747 (2:integer <- copy 0:literal)
3750 (3:integer <- copy 0:literal)
3752 (when (~iso (as cons after*!label1)
3755 (3:integer <- copy 0:literal))
3757 (2:integer <- copy 0:literal))))
3758 (prn "F - 'after' records fragments in *reverse* order"))
3760 (when (~iso (insert-code
3761 '((1:integer <- copy 0:literal)
3763 (4:integer <- copy 0:literal)))
3764 '((1:integer <- copy 0:literal)
3766 (3:integer <- copy 0:literal)
3767 (2:integer <- copy 0:literal)
3768 (4:integer <- copy 0:literal)))
3769 (prn "F - 'insert-code' can insert multiple fragments in order after label"))
3772 (new-trace "before-after")
3776 (2:integer <- copy 0:literal)
3779 (3:integer <- copy 0:literal)
3781 (when (and (~iso (as cons before*!label1)
3784 (2:integer <- copy 0:literal))))
3785 (~iso (as cons after*!label1)
3788 (3:integer <- copy 0:literal)))))
3789 (prn "F - 'before' and 'after' fragments work together"))
3791 (when (~iso (insert-code
3792 '((1:integer <- copy 0:literal)
3794 (4:integer <- copy 0:literal)))
3795 '((1:integer <- copy 0:literal)
3796 (2:integer <- copy 0:literal)
3798 (3:integer <- copy 0:literal)
3799 (4:integer <- copy 0:literal)))
3800 (prn "F - 'insert-code' can insert multiple fragments around label"))
3803 (new-trace "before-after-multiple")
3807 (2:integer <- copy 0:literal)
3808 (3:integer <- copy 0:literal)
3811 (4:integer <- copy 0:literal)
3814 (5:integer <- copy 0:literal)
3817 (6:integer <- copy 0:literal)
3818 (7:integer <- copy 0:literal)
3820 (when (or (~iso (as cons before*!label1)
3823 (2:integer <- copy 0:literal)
3824 (3:integer <- copy 0:literal))
3826 (5:integer <- copy 0:literal))))
3827 (~iso (as cons after*!label1)
3830 (6:integer <- copy 0:literal)
3831 (7:integer <- copy 0:literal))
3833 (4:integer <- copy 0:literal)))))
3834 (prn "F - multiple 'before' and 'after' fragments at once"))
3836 (when (~iso (insert-code
3837 '((1:integer <- copy 0:literal)
3839 (8:integer <- copy 0:literal)))
3840 '((1:integer <- copy 0:literal)
3841 (2:integer <- copy 0:literal)
3842 (3:integer <- copy 0:literal)
3843 (5:integer <- copy 0:literal)
3845 (6:integer <- copy 0:literal)
3846 (7:integer <- copy 0:literal)
3847 (4:integer <- copy 0:literal)
3848 (8:integer <- copy 0:literal)))
3849 (prn "F - 'insert-code' can insert multiple fragments around label - 2"))
3852 (new-trace "before-after-independent")
3858 (2:integer <- copy 0:literal)
3861 (3:integer <- copy 0:literal)
3864 (4:integer <- copy 0:literal)
3867 (5:integer <- copy 0:literal)
3869 (list before*!label1 after*!label1))
3874 (2:integer <- copy 0:literal)
3877 (4:integer <- copy 0:literal)
3880 (3:integer <- copy 0:literal)
3883 (5:integer <- copy 0:literal)
3885 (list before*!label1 after*!label1)))
3886 (prn "F - order matters between 'before' and between 'after' fragments, but not *across* 'before' and 'after' fragments"))
3889 (new-trace "before-after-braces")
3891 (= function* (table))
3894 (1:integer <- copy 0:literal)
3901 ;? (= dump-trace* (obj whitelist '("cn0")))
3903 (when (~iso function*!f1
3905 (((1 integer)) <- ((copy)) ((0 literal)))))
3906 (prn "F - before/after works inside blocks"))
3909 (new-trace "before-after-any-order")
3911 (= function* (table))
3919 (1:integer <- copy 0:literal)
3922 (when (~iso function*!f1
3924 (((1 integer)) <- ((copy)) ((0 literal)))))
3925 (prn "F - before/after can come after the function they need to modify"))
3929 (new-trace "multiple-defs")
3931 (= function* (table))
3934 (1:integer <- copy 0:literal)
3937 (2:integer <- copy 0:literal)
3940 (when (~iso function*!f1
3941 '((((2 integer)) <- ((copy)) ((0 literal)))
3942 (((1 integer)) <- ((copy)) ((0 literal)))))
3943 (prn "F - multiple 'def' of the same function add clauses"))
3948 (= function* (table))
3951 (1:integer <- copy 0:literal)
3954 (2:integer <- copy 0:literal)
3957 (when (~iso function*!f1
3958 '((((2 integer)) <- ((copy)) ((0 literal)))))
3959 (prn "F - 'def!' clears all previous clauses"))
3970 (new-trace "string-new")
3973 (1:string-address <- new string:literal 5:literal)
3975 (let routine make-routine!main
3976 (enq routine running-routines*)
3977 (let before rep.routine!alloc
3979 (when (~iso rep.routine!alloc (+ before 5 1))
3980 (prn "F - 'new' allocates arrays of bytes for strings"))))
3982 ; Convenience: initialize strings using string literals
3984 (new-trace "string-literal")
3987 (1:string-address <- new "hello")
3989 (let routine make-routine!main
3990 (enq routine running-routines*)
3991 (let before rep.routine!alloc
3992 ;? (set dump-trace*)
3993 ;? (= dump-trace* (obj whitelist '("schedule" "run" "addr")))
3995 (when (~iso rep.routine!alloc (+ before 5 1))
3996 (prn "F - 'new' allocates arrays of bytes for string literals"))
3997 (when (~memory-contains-array before "hello")
3998 (prn "F - 'new' initializes allocated memory to string literal"))))
4001 (new-trace "string-equal")
4004 (1:string-address <- new "hello")
4005 (2:string-address <- new "hello")
4006 (3:boolean <- string-equal 1:string-address 2:string-address)
4009 (when (~iso memory*.3 t)
4010 (prn "F - 'string-equal'"))
4013 (new-trace "string-equal-empty")
4016 (1:string-address <- new "")
4017 (2:string-address <- new "")
4018 (3:boolean <- string-equal 1:string-address 2:string-address)
4021 (when (~iso memory*.3 t)
4022 (prn "F - 'string-equal' works on empty strings"))
4025 (new-trace "string-equal-compare-with-empty")
4028 (1:string-address <- new "a")
4029 (2:string-address <- new "")
4030 (3:boolean <- string-equal 1:string-address 2:string-address)
4033 (when (~iso memory*.3 nil)
4034 (prn "F - 'string-equal' compares correctly with empty strings"))
4037 (new-trace "string-equal-compares-length")
4040 (1:string-address <- new "a")
4041 (2:string-address <- new "ab")
4042 (3:boolean <- string-equal 1:string-address 2:string-address)
4045 (when (~iso memory*.3 nil)
4046 (prn "F - 'string-equal' handles differing lengths"))
4049 (new-trace "string-equal-compares-initial-element")
4052 (1:string-address <- new "aa")
4053 (2:string-address <- new "ba")
4054 (3:boolean <- string-equal 1:string-address 2:string-address)
4057 (when (~iso memory*.3 nil)
4058 (prn "F - 'string-equal' handles inequal final byte"))
4061 (new-trace "string-equal-compares-final-element")
4064 (1:string-address <- new "ab")
4065 (2:string-address <- new "aa")
4066 (3:boolean <- string-equal 1:string-address 2:string-address)
4069 (when (~iso memory*.3 nil)
4070 (prn "F - 'string-equal' handles inequal final byte"))
4073 (new-trace "string-equal-reflexive")
4076 (1:string-address <- new "ab")
4077 (3:boolean <- string-equal 1:string-address 1:string-address)
4080 (when (~iso memory*.3 t)
4081 (prn "F - 'string-equal' handles identical pointer"))
4084 (new-trace "strcat")
4087 (1:string-address <- new "hello,")
4088 (2:string-address <- new " world!")
4089 (3:string-address <- strcat 1:string-address 2:string-address)
4091 ;? (= dump-trace* (obj whitelist '("run"))) ;? 1
4093 (when (~memory-contains-array memory*.3 "hello, world!")
4094 (prn "F - 'strcat' concatenates strings"))
4098 (new-trace "interpolate")
4101 (1:string-address <- new "hello, _!")
4102 (2:string-address <- new "abc")
4103 (3:string-address <- interpolate 1:string-address 2:string-address)
4105 ;? (= dump-trace* (obj whitelist '("run")))
4107 (when (~memory-contains-array memory*.3 "hello, abc!")
4108 (prn "F - 'interpolate' splices strings"))
4111 (new-trace "interpolate-empty")
4114 (1:string-address <- new "hello!")
4115 (2:string-address <- new "abc")
4116 (3:string-address <- interpolate 1:string-address 2:string-address)
4118 ;? (= dump-trace* (obj whitelist '("run")))
4120 (when (~memory-contains-array memory*.3 "hello!")
4121 (prn "F - 'interpolate' without underscore returns template"))
4124 (new-trace "interpolate-at-start")
4127 (1:string-address <- new "_, hello!")
4128 (2:string-address <- new "abc")
4129 (3:string-address <- interpolate 1:string-address 2:string-address)
4131 ;? (= dump-trace* (obj whitelist '("run")))
4133 (when (~memory-contains-array memory*.3 "abc, hello")
4134 (prn "F - 'interpolate' splices strings at start"))
4137 (new-trace "interpolate-at-end")
4140 (1:string-address <- new "hello, _")
4141 (2:string-address <- new "abc")
4142 (3:string-address <- interpolate 1:string-address 2:string-address)
4144 ;? (= dump-trace* (obj whitelist '("run")))
4146 (when (~memory-contains-array memory*.3 "hello, abc")
4147 (prn "F - 'interpolate' splices strings at start"))
4150 (new-trace "interpolate-varargs")
4153 (1:string-address <- new "hello, _, _, and _!")
4154 (2:string-address <- new "abc")
4155 (3:string-address <- new "def")
4156 (4:string-address <- new "ghi")
4157 (5:string-address <- interpolate 1:string-address 2:string-address 3:string-address 4:string-address)
4159 ;? (= dump-trace* (obj whitelist '("run")))
4160 ;? (= dump-trace* (obj whitelist '("run" "array-info")))
4161 ;? (set dump-trace*)
4164 ;? (up i 1 (+ 1 (memory* memory*.5))
4165 ;? (prn (memory* (+ memory*.5 i))))
4166 (when (~memory-contains-array memory*.5 "hello, abc, def, and ghi!")
4167 (prn "F - 'interpolate' splices in any number of strings"))
4170 (new-trace "string-find-next")
4173 (1:string-address <- new "a/b")
4174 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4177 (when (~is memory*.2 1)
4178 (prn "F - 'find-next' finds first location of a character"))
4181 (new-trace "string-find-next-empty")
4184 (1:string-address <- new "")
4185 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4188 (each routine completed-routines*
4189 (aif rep.routine!error (prn "error - " it)))
4190 (when (~is memory*.2 0)
4191 (prn "F - 'find-next' finds first location of a character"))
4194 (new-trace "string-find-next-initial")
4197 (1:string-address <- new "/abc")
4198 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4201 (when (~is memory*.2 0)
4202 (prn "F - 'find-next' handles prefix match"))
4205 (new-trace "string-find-next-final")
4208 (1:string-address <- new "abc/")
4209 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4213 (when (~is memory*.2 3)
4214 (prn "F - 'find-next' handles suffix match"))
4217 (new-trace "string-find-next-missing")
4220 (1:string-address <- new "abc")
4221 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4225 (when (~is memory*.2 3)
4226 (prn "F - 'find-next' handles no match"))
4229 (new-trace "string-find-next-invalid-index")
4232 (1:string-address <- new "abc")
4233 (2:integer <- find-next 1:string-address ((#\/ literal)) 4:literal)
4235 ;? (= dump-trace* (obj whitelist '("run")))
4237 (each routine completed-routines*
4238 (aif rep.routine!error (prn "error - " it)))
4240 (when (~is memory*.2 4)
4241 (prn "F - 'find-next' skips invalid index (past end of string)"))
4244 (new-trace "string-find-next-first")
4247 (1:string-address <- new "ab/c/")
4248 (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal)
4251 (when (~is memory*.2 2)
4252 (prn "F - 'find-next' finds first of multiple options"))
4255 (new-trace "string-find-next-second")
4258 (1:string-address <- new "ab/c/")
4259 (2:integer <- find-next 1:string-address ((#\/ literal)) 3:literal)
4262 (when (~is memory*.2 4)
4263 (prn "F - 'find-next' finds second of multiple options"))
4266 (new-trace "match-at")
4269 (1:string-address <- new "abc")
4270 (2:string-address <- new "ab")
4271 (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
4274 (when (~is memory*.3 t)
4275 (prn "F - 'match-at' matches substring at given index"))
4278 (new-trace "match-at-reflexive")
4281 (1:string-address <- new "abc")
4282 (3:boolean <- match-at 1:string-address 1:string-address 0:literal)
4285 (when (~is memory*.3 t)
4286 (prn "F - 'match-at' always matches a string at itself at index 0"))
4289 (new-trace "match-at-outside-bounds")
4292 (1:string-address <- new "abc")
4293 (2:string-address <- new "a")
4294 (3:boolean <- match-at 1:string-address 2:string-address 4:literal)
4297 (when (~is memory*.3 nil)
4298 (prn "F - 'match-at' always fails to match outside the bounds of the text"))
4301 (new-trace "match-at-empty-pattern")
4304 (1:string-address <- new "abc")
4305 (2:string-address <- new "")
4306 (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
4309 (when (~is memory*.3 t)
4310 (prn "F - 'match-at' always matches empty pattern"))
4313 (new-trace "match-at-empty-pattern-outside-bounds")
4316 (1:string-address <- new "abc")
4317 (2:string-address <- new "")
4318 (3:boolean <- match-at 1:string-address 2:string-address 4:literal)
4321 (when (~is memory*.3 nil)
4322 (prn "F - 'match-at' matches empty pattern -- unless index is out of bounds"))
4325 (new-trace "match-at-empty-text")
4328 (1:string-address <- new "")
4329 (2:string-address <- new "abc")
4330 (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
4333 (when (~is memory*.3 nil)
4334 (prn "F - 'match-at' never matches empty text"))
4337 (new-trace "match-at-empty-against-empty")
4340 (1:string-address <- new "")
4341 (3:boolean <- match-at 1:string-address 1:string-address 0:literal)
4344 (when (~is memory*.3 t)
4345 (prn "F - 'match-at' never matches empty text -- unless pattern is also empty"))
4348 (new-trace "match-at-inside-bounds")
4351 (1:string-address <- new "abc")
4352 (2:string-address <- new "bc")
4353 (3:boolean <- match-at 1:string-address 2:string-address 1:literal)
4356 (when (~is memory*.3 t)
4357 (prn "F - 'match-at' matches inner substring"))
4360 (new-trace "match-at-inside-bounds-2")
4363 (1:string-address <- new "abc")
4364 (2:string-address <- new "bc")
4365 (3:boolean <- match-at 1:string-address 2:string-address 0:literal)
4368 (when (~is memory*.3 nil)
4369 (prn "F - 'match-at' matches inner substring - 2"))
4372 (new-trace "find-substring")
4375 (1:string-address <- new "abc")
4376 (2:string-address <- new "bc")
4377 (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
4380 ;? (prn memory*.3) ;? 1
4381 (when (~is memory*.3 1)
4382 (prn "F - 'find-substring' returns index of match"))
4385 (new-trace "find-substring-2")
4388 (1:string-address <- new "abcd")
4389 (2:string-address <- new "bc")
4390 (3:integer <- find-substring 1:string-address 2:string-address 1:literal)
4393 (when (~is memory*.3 1)
4394 (prn "F - 'find-substring' returns provided index if it matches"))
4397 (new-trace "find-substring-no-match")
4400 (1:string-address <- new "abc")
4401 (2:string-address <- new "bd")
4402 (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
4405 (when (~is memory*.3 3)
4406 (prn "F - 'find-substring' returns out-of-bounds index on no-match"))
4409 (new-trace "find-substring-suffix-match")
4412 (1:string-address <- new "abcd")
4413 (2:string-address <- new "cd")
4414 (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
4417 (when (~is memory*.3 2)
4418 (prn "F - 'find-substring' returns provided index if it matches"))
4421 (new-trace "find-substring-suffix-match-2")
4424 (1:string-address <- new "abcd")
4425 (2:string-address <- new "cde")
4426 (3:integer <- find-substring 1:string-address 2:string-address 0:literal)
4429 (when (~is memory*.3 4)
4430 (prn "F - 'find-substring' returns provided index if it matches"))
4435 (new-trace "string-split")
4438 (1:string-address <- new "a/b")
4439 (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
4441 ;? (set dump-trace*)
4443 (each routine completed-routines*
4444 (aif rep.routine!error (prn "error - " it)))
4446 ;? (prn base " " memory*.base)
4447 (when (or (~is memory*.base 2)
4448 ;? (do1 nil prn.111)
4449 (~memory-contains-array (memory* (+ base 1)) "a")
4450 ;? (do1 nil prn.111)
4451 (~memory-contains-array (memory* (+ base 2)) "b"))
4452 (prn "F - 'split' cuts string at delimiter")))
4455 (new-trace "string-split2")
4458 (1:string-address <- new "a/b/c")
4459 (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
4461 ;? (set dump-trace*)
4463 (each routine completed-routines*
4464 (aif rep.routine!error (prn "error - " it)))
4466 ;? (prn base " " memory*.base)
4467 (when (or (~is memory*.base 3)
4468 ;? (do1 nil prn.111)
4469 (~memory-contains-array (memory* (+ base 1)) "a")
4470 ;? (do1 nil prn.111)
4471 (~memory-contains-array (memory* (+ base 2)) "b")
4472 ;? (do1 nil prn.111)
4473 (~memory-contains-array (memory* (+ base 3)) "c"))
4474 (prn "F - 'split' cuts string at two delimiters")))
4477 (new-trace "string-split-missing")
4480 (1:string-address <- new "abc")
4481 (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
4484 (each routine completed-routines*
4485 (aif rep.routine!error (prn "error - " it)))
4487 (when (or (~is memory*.base 1)
4488 (~memory-contains-array (memory* (+ base 1)) "abc"))
4489 (prn "F - 'split' handles missing delimiter")))
4492 (new-trace "string-split-empty")
4495 (1:string-address <- new "")
4496 (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
4498 ;? (= dump-trace* (obj whitelist '("run")))
4500 (each routine completed-routines*
4501 (aif rep.routine!error (prn "error - " it)))
4503 ;? (prn base " " memory*.base)
4504 (when (~is memory*.base 0)
4505 (prn "F - 'split' handles empty string")))
4508 (new-trace "string-split-empty-piece")
4511 (1:string-address <- new "a/b//c")
4512 (2:string-address-array-address <- split 1:string-address ((#\/ literal)))
4515 (each routine completed-routines*
4516 (aif rep.routine!error (prn "error - " it)))
4518 (when (or (~is memory*.base 4)
4519 (~memory-contains-array (memory* (+ base 1)) "a")
4520 (~memory-contains-array (memory* (+ base 2)) "b")
4521 (~memory-contains-array (memory* (+ base 3)) "")
4522 (~memory-contains-array (memory* (+ base 4)) "c"))
4523 (prn "F - 'split' cuts string at two delimiters")))
4527 (new-trace "string-split-first")
4530 (1:string-address <- new "a/b")
4531 (2:string-address 3:string-address <- split-first 1:string-address ((#\/ literal)))
4534 (each routine completed-routines*
4535 (aif rep.routine!error (prn "error - " it)))
4536 (when (or (~memory-contains-array memory*.2 "a")
4537 (~memory-contains-array memory*.3 "b"))
4538 (prn "F - 'split-first' cuts string at first occurrence of delimiter"))
4541 (new-trace "string-split-first-at-substring")
4544 (1:string-address <- new "a//b")
4545 (2:string-address <- new "//")
4546 (3:string-address 4:string-address <- split-first-at-substring 1:string-address 2:string-address)
4549 (each routine completed-routines*
4550 (aif rep.routine!error (prn "error - " it)))
4551 ;? (prn int-canon.memory*) ;? 1
4552 (when (or (~memory-contains-array memory*.3 "a")
4553 (~memory-contains-array memory*.4 "b"))
4554 (prn "F - 'split-first-at-substring' is like split-first but with a string delimiter"))
4557 (new-trace "string-copy")
4560 (1:string-address <- new "abc")
4561 (2:string-address <- string-copy 1:string-address 1:literal 3:literal)
4564 (each routine completed-routines*
4565 (aif rep.routine!error (prn "error - " it)))
4566 (when (~memory-contains-array memory*.2 "bc")
4567 (prn "F - 'string-copy' returns a copy of a substring"))
4570 (new-trace "string-copy-out-of-bounds")
4573 (1:string-address <- new "abc")
4574 (2:string-address <- string-copy 1:string-address 2:literal 4:literal)
4577 (each routine completed-routines*
4578 (aif rep.routine!error (prn "error - " it)))
4579 (when (~memory-contains-array memory*.2 "c")
4580 (prn "F - 'string-copy' stops at bounds"))
4583 (new-trace "string-copy-out-of-bounds-2")
4586 (1:string-address <- new "abc")
4587 (2:string-address <- string-copy 1:string-address 3:literal 3:literal)
4590 (each routine completed-routines*
4591 (aif rep.routine!error (prn "error - " it)))
4592 (when (~memory-contains-array memory*.2 "")
4593 (prn "F - 'string-copy' returns empty string when range is out of bounds"))
4599 (1:integer <- min 3:literal 4:literal)
4602 (each routine completed-routines*
4603 (aif rep.routine!error (prn "error - " it)))
4604 ;? (prn int-canon.memory*) ;? 1
4605 (when (~is memory*.1 3)
4606 (prn "F - 'min' returns smaller of two numbers"))
4611 (new-trace "integer-to-decimal-string")
4614 (1:string-address/raw <- integer-to-decimal-string 34:literal)
4616 ;? (set dump-trace*)
4617 ;? (= dump-trace* (obj whitelist '("run")))
4620 (when (~memory-contains-array base "34")
4621 (prn "F - converting integer to decimal string")))
4624 (new-trace "integer-to-decimal-string-zero")
4627 (1:string-address/raw <- integer-to-decimal-string 0:literal)
4631 (when (~memory-contains-array base "0")
4632 (prn "F - converting zero to decimal string")))
4635 (new-trace "integer-to-decimal-string-negative")
4638 (1:string-address/raw <- integer-to-decimal-string -237:literal)
4642 (when (~memory-contains-array base "-237")
4643 (prn "F - converting negative integer to decimal string")))
4645 ; fake screen for tests; prints go to a string
4647 (new-trace "fake-screen-empty")
4650 (default-space:space-address <- new space:literal 30:literal/capacity)
4651 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4652 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4655 (each routine completed-routines*
4656 (awhen rep.routine!error
4657 (prn "error - " it)))
4658 (when (~memory-contains-array memory*.5
4669 (prn "F - fake screen starts out with all spaces"))
4671 ; fake keyboard for tests; must initialize keys in advance
4673 (new-trace "fake-keyboard")
4676 (default-space:space-address <- new space:literal 30:literal)
4677 (s:string-address <- new "foo")
4678 (x:keyboard-address <- init-keyboard s:string-address)
4679 (1:character-address/raw <- read-key x:keyboard-address)
4682 (when (~is memory*.1 #\f)
4683 (prn "F - 'read-key' reads character from provided 'fake keyboard' string"))
4685 ; fake keyboard for tests; must initialize keys in advance
4687 (new-trace "fake-keyboard2")
4690 (default-space:space-address <- new space:literal 30:literal)
4691 (s:string-address <- new "foo")
4692 (x:keyboard-address <- init-keyboard s:string-address)
4693 (1:character-address/raw <- read-key x:keyboard-address)
4694 (1:character-address/raw <- read-key x:keyboard-address)
4697 (when (~is memory*.1 #\o)
4698 (prn "F - 'read-key' advances cursor in provided string"))
4700 ; to receive input line by line, run send-keys-buffered-to-stdin
4702 (new-trace "buffer-stdin-until-newline")
4705 (default-space:space-address <- new space:literal 30:literal)
4706 (s:string-address <- new "foo")
4707 (k:keyboard-address <- init-keyboard s:string-address)
4708 (stdin:channel-address <- init-channel 1:literal)
4709 (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
4710 (buffered-stdin:channel-address <- init-channel 1:literal)
4711 (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
4712 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4713 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4714 (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
4715 (sleep until-routine-done:literal r:integer/routine)
4717 ;? (set dump-trace*) ;? 3
4718 ;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 0
4720 ;? (prn int-canon.memory*) ;? 0
4721 (each routine completed-routines*
4722 (awhen rep.routine!error
4723 (prn "error - " it)))
4724 (when (~memory-contains-array memory*.5
4735 (prn "F - 'buffer-lines' prints nothing until newline is encountered"))
4739 (new-trace "print-buffered-contents-on-newline")
4742 (default-space:space-address <- new space:literal 30:literal)
4743 (s:string-address <- new "foo\nline2")
4744 (k:keyboard-address <- init-keyboard s:string-address)
4745 (stdin:channel-address <- init-channel 1:literal)
4746 (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
4747 (buffered-stdin:channel-address <- init-channel 1:literal)
4748 (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
4749 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4750 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4751 (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
4752 (sleep until-routine-done:literal r:integer/routine)
4754 ;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
4756 (each routine completed-routines*
4757 (awhen rep.routine!error
4758 (prn "error - " it)))
4759 (when (~memory-contains-array memory*.5
4770 (prn "F - 'buffer-lines' prints lines to screen"))
4773 (new-trace "print-buffered-contents-right-at-newline")
4776 (default-space:space-address <- new space:literal 30:literal)
4777 (s:string-address <- new "foo\n")
4778 (k:keyboard-address <- init-keyboard s:string-address)
4779 (stdin:channel-address <- init-channel 1:literal)
4780 (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
4781 (buffered-stdin:channel-address <- init-channel 1:literal)
4782 (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
4783 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4784 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4785 (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
4786 (sleep until-routine-done:literal r:integer/routine)
4787 ; hack: give helper some time to finish printing
4788 (sleep for-some-cycles:literal 500:literal)
4790 ;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
4792 (each routine completed-routines*
4793 (awhen rep.routine!error
4794 (prn "error - " it)))
4795 (when (~memory-contains-array memory*.5
4806 (prn "F - 'buffer-lines' prints lines to screen immediately on newline"))
4809 (new-trace "buffered-contents-skip-backspace")
4812 (default-space:space-address <- new space:literal 30:literal)
4813 (s:string-address <- new "fooa\b\nline2")
4814 (k:keyboard-address <- init-keyboard s:string-address)
4815 (stdin:channel-address <- init-channel 1:literal)
4816 (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
4817 (buffered-stdin:channel-address <- init-channel 1:literal)
4818 (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
4819 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4820 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4821 (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
4822 (sleep until-routine-done:literal r:integer/routine)
4824 ;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
4826 (each routine completed-routines*
4827 (awhen rep.routine!error
4828 (prn "error - " it)))
4829 (when (~memory-contains-array memory*.5
4840 (prn "F - 'buffer-lines' handles backspace"))
4843 (new-trace "buffered-contents-ignore-excess-backspace")
4846 (default-space:space-address <- new space:literal 30:literal)
4847 (s:string-address <- new "a\b\bfoo\n")
4848 (k:keyboard-address <- init-keyboard s:string-address)
4849 (stdin:channel-address <- init-channel 1:literal)
4850 (fork send-keys-to-stdin:fn nil:literal/globals nil:literal/limit k:keyboard-address stdin:channel-address)
4851 (buffered-stdin:channel-address <- init-channel 1:literal)
4852 (r:integer/routine <- fork buffer-lines:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
4853 (screen:terminal-address <- init-fake-terminal 20:literal 10:literal)
4854 (5:string-address/raw <- get screen:terminal-address/deref data:offset)
4855 (fork-helper send-prints-to-stdout:fn nil:literal/globals nil:literal/limit screen:terminal-address buffered-stdin:channel-address)
4856 (sleep until-routine-done:literal r:integer/routine)
4857 ; hack: give helper some time to finish printing
4858 (sleep for-some-cycles:literal 500:literal)
4860 ;? (= dump-trace* (obj whitelist '("schedule" "run"))) ;? 1
4862 (each routine completed-routines*
4863 (awhen rep.routine!error
4864 (prn "error - " it)))
4865 ;? (prn memory*.5) ;? 1
4866 (when (~memory-contains-array memory*.5
4877 (prn "F - 'buffer-lines' ignores backspace when there's nothing to backspace over"))
4882 (new-trace "parse-and-record")
4889 (when (~iso type*!foo (obj size 3 and-record t elems '((string) (integer) (boolean)) fields '(x y z)))
4890 (prn "F - 'add-code' can add new and-records"))
4892 ;; unit tests for various helpers
4895 (prn "== tokenize-args")
4896 (assert:iso '((a b) (c d))
4897 (tokenize-arg 'a:b/c:d))
4898 ; numbers are not symbols
4899 (assert:iso '((a b) (1 d))
4900 (tokenize-arg 'a:b/1:d))
4901 ; special symbols are skipped
4908 (assert:iso (tokenize-arg:tokenize-arg 'a:b/c:d)
4909 (tokenize-arg 'a:b/c:d))
4912 (assert:iso '((((default-space space-address)) <- ((new)) ((space literal)) ((30 literal)))
4915 '((default-space:space-address <- new space:literal 30:literal)
4919 (assert:iso '((((default-space space-address)) <- ((new)) ((space literal)) ((30 literal)))
4923 (((a b)) <- ((op)) ((c d)) ((e f)))
4926 '((default-space:space-address <- new space:literal 30:literal)
4936 (when (~iso 0 (space '((4 integer))))
4937 (prn "F - 'space' is 0 by default"))
4938 (when (~iso 1 (space '((4 integer) (space 1))))
4939 (prn "F - 'space' picks up space when available"))
4940 (when (~iso 'global (space '((4 integer) (space global))))
4941 (prn "F - 'space' understands routine-global space"))
4944 (prn "== absolutize")
4946 (when (~iso '((4 integer)) (absolutize '((4 integer))))
4947 (prn "F - 'absolutize' works without routine"))
4948 (= routine* make-routine!foo)
4949 (when (~iso '((4 integer)) (absolutize '((4 integer))))
4950 (prn "F - 'absolutize' works without default-space"))
4951 (= rep.routine*!call-stack.0!default-space 10)
4952 (= memory*.10 5) ; bounds check for default-space
4953 (when (~iso '((15 integer) (raw))
4954 (absolutize '((4 integer))))
4955 (prn "F - 'absolutize' works with default-space"))
4956 (absolutize '((5 integer)))
4957 (when (~posmatch "no room" rep.routine*!error)
4958 (prn "F - 'absolutize' checks against default-space bounds"))
4959 (when (~iso '((_ integer)) (absolutize '((_ integer))))
4960 (prn "F - 'absolutize' passes dummy args right through"))
4961 (when (~iso '((default-space integer)) (absolutize '((default-space integer))))
4962 (prn "F - 'absolutize' passes 'default-space' right through"))
4964 (= memory*.20 5) ; pretend array
4965 (= rep.routine*!globals 20) ; provide it to routine global
4966 (when (~iso '((22 integer) (raw))
4967 (absolutize '((1 integer) (space global))))
4968 (prn "F - 'absolutize' handles variables in the global space"))
4974 (when (~iso '((4 integer))
4975 (deref '((3 integer-address)
4977 (prn "F - 'deref' handles simple addresses"))
4978 (when (~iso '((4 integer) (deref))
4979 (deref '((3 integer-address)
4982 (prn "F - 'deref' deletes just one deref"))
4984 (when (~iso '((5 integer))
4985 (deref:deref '((3 integer-address-address)
4988 (prn "F - 'deref' can be chained"))
4989 (when (~iso '((5 integer) (foo))
4990 (deref:deref '((3 integer-address-address)
4994 (prn "F - 'deref' skips junk"))
5001 (when (~is 4 (addr '((4 integer))))
5002 (prn "F - directly addressed operands are their own address"))
5004 (when (~is 4 (addr '((4 integer-address))))
5005 (prn "F - directly addressed operands are their own address - 2"))
5006 (when (~is 4 (addr '((4 literal))))
5007 (prn "F - 'addr' doesn't understand literals"))
5011 (when (~is 23 (addr '((4 integer-address) (deref))))
5012 (prn "F - 'addr' works with indirectly-addressed 'deref'"))
5015 (when (~is 23 (addr '((3 integer-address-address) (deref) (deref))))
5016 (prn "F - 'addr' works with multiple 'deref'"))
5018 (= routine* make-routine!foo)
5019 (when (~is 4 (addr '((4 integer))))
5020 (prn "F - directly addressed operands are their own address inside routines"))
5021 (when (~is 4 (addr '((4 integer-address))))
5022 (prn "F - directly addressed operands are their own address inside routines - 2"))
5023 (when (~is 4 (addr '((4 literal))))
5024 (prn "F - 'addr' doesn't understand literals inside routines"))
5026 (when (~is 23 (addr '((4 integer-address) (deref))))
5027 (prn "F - 'addr' works with indirectly-addressed 'deref' inside routines"))
5030 (= rep.routine*!call-stack.0!default-space 10)
5032 (= memory*.10 5) ; bounds check for default-space
5034 (when (~is 15 (addr '((4 integer))))
5035 (prn "F - directly addressed operands in routines add default-space"))
5037 (when (~is 15 (addr '((4 integer-address))))
5038 (prn "F - directly addressed operands in routines add default-space - 2"))
5039 (when (~is 15 (addr '((4 literal))))
5040 (prn "F - 'addr' doesn't understand literals"))
5042 (when (~is 23 (addr '((4 integer-address) (deref))))
5043 (prn "F - 'addr' adds default-space before 'deref', not after"))
5047 (prn "== array-len")
5050 (when (~is 4 (array-len '((35 integer-boolean-pair-array))))
5051 (prn "F - 'array-len'"))
5053 (when (~is 4 (array-len '((34 integer-boolean-pair-array-address) (deref))))
5054 (prn "F - 'array-len'"))
5060 ;? (set dump-trace*)
5062 (when (~is 1 (sizeof '((_ integer))))
5063 (prn "F - 'sizeof' works on primitives"))
5064 (when (~is 1 (sizeof '((_ integer-address))))
5065 (prn "F - 'sizeof' works on addresses"))
5066 (when (~is 2 (sizeof '((_ integer-boolean-pair))))
5067 (prn "F - 'sizeof' works on and-records"))
5068 (when (~is 3 (sizeof '((_ integer-point-pair))))
5069 (prn "F - 'sizeof' works on and-records with and-record fields"))
5072 (when (~is 1 (sizeof '((34 integer))))
5073 (prn "F - 'sizeof' works on primitive operands"))
5074 (when (~is 1 (sizeof '((34 integer-address))))
5075 (prn "F - 'sizeof' works on address operands"))
5076 (when (~is 2 (sizeof '((34 integer-boolean-pair))))
5077 (prn "F - 'sizeof' works on and-record operands"))
5078 (when (~is 3 (sizeof '((34 integer-point-pair))))
5079 (prn "F - 'sizeof' works on and-record operands with and-record fields"))
5080 (when (~is 2 (sizeof '((34 integer-boolean-pair-address) (deref))))
5081 (prn "F - 'sizeof' works on pointers to and-records"))
5082 (= memory*.35 4) ; size of array
5084 ;? (= dump-trace* (obj whitelist '("sizeof" "array-len")))
5085 (when (~is 9 (sizeof '((34 integer-boolean-pair-array-address) (deref))))
5086 (prn "F - 'sizeof' works on pointers to arrays"))
5091 (when (~is 24 (sizeof '((4 integer-array))))
5092 (prn "F - 'sizeof' reads array lengths from memory"))
5094 (when (~is 24 (sizeof '((3 integer-array-address) (deref))))
5095 (prn "F - 'sizeof' handles pointers to arrays"))
5097 (= routine* make-routine!foo)
5098 (when (~is 24 (sizeof '((4 integer-array))))
5099 (prn "F - 'sizeof' reads array lengths from memory inside routines"))
5100 (= rep.routine*!call-stack.0!default-space 10)
5101 (= memory*.10 5) ; bounds check for default-space
5102 (when (~is 35 (sizeof '((4 integer-array))))
5103 (prn "F - 'sizeof' reads array lengths from memory using default-space"))
5104 (= memory*.35 4) ; size of array
5106 ;? (= dump-trace* (obj whitelist '("sizeof")))
5107 (aif rep.routine*!error (prn "error - " it))
5108 (when (~is 9 (sizeof '((4 integer-boolean-pair-array-address) (deref))))
5109 (prn "F - 'sizeof' works on pointers to arrays using default-space"))
5115 (when (~is 4 (m '((4 literal))))
5116 (prn "F - 'm' avoids reading memory for literals"))
5117 (when (~is 4 (m '((4 offset))))
5118 (prn "F - 'm' avoids reading memory for offsets"))
5120 (when (~is 34 (m '((4 integer))))
5121 (prn "F - 'm' reads memory for simple types"))
5123 (when (~is 34 (m '((3 integer-address) (deref))))
5124 (prn "F - 'm' redirects addresses"))
5126 (when (~is 34 (m '((2 integer-address-address) (deref) (deref))))
5127 (prn "F - 'm' multiply redirects addresses"))
5128 (when (~iso (annotate 'record '(34 nil)) (m '((4 integer-boolean-pair))))
5129 (prn "F - 'm' supports compound records"))
5132 (when (~iso (annotate 'record '(34 35 36)) (m '((4 integer-point-pair))))
5133 (prn "F - 'm' supports records with compound fields"))
5134 (when (~iso (annotate 'record '(34 35 36)) (m '((3 integer-point-pair-address) (deref))))
5135 (prn "F - 'm' supports indirect access to records"))
5137 (when (~iso (annotate 'record '(2 35 36)) (m '((4 integer-array))))
5138 (prn "F - 'm' supports access to arrays"))
5139 (when (~iso (annotate 'record '(2 35 36)) (m '((3 integer-array-address) (deref))))
5140 (prn "F - 'm' supports indirect access to arrays"))
5142 (= routine* make-routine!foo)
5143 (= memory*.10 5) ; fake array
5145 (= rep.routine*!globals 10)
5146 (when (~iso 34 (m '((1 integer) (space global))))
5147 (prn "F - 'm' supports access to per-routine globals"))
5152 (setm '((4 integer)) 34)
5153 (when (~is 34 memory*.4)
5154 (prn "F - 'setm' writes primitives to memory"))
5155 (setm '((3 integer-address)) 4)
5156 (when (~is 4 memory*.3)
5157 (prn "F - 'setm' writes addresses to memory"))
5158 (setm '((3 integer-address) (deref)) 35)
5159 (when (~is 35 memory*.4)
5160 (prn "F - 'setm' redirects writes"))
5162 (setm '((2 integer-address-address) (deref) (deref)) 36)
5163 (when (~is 36 memory*.4)
5164 (prn "F - 'setm' multiply redirects writes"))
5166 (setm '((4 integer-integer-pair)) (annotate 'record '(23 24)))
5167 (when (~memory-contains 4 '(23 24))
5168 (prn "F - 'setm' writes compound records"))
5169 (assert (is memory*.7 nil))
5171 (setm '((7 integer-point-pair)) (annotate 'record '(23 24 25)))
5172 (when (~memory-contains 7 '(23 24 25))
5173 (prn "F - 'setm' writes records with compound fields"))
5174 (= routine* make-routine!foo)
5175 (setm '((4 integer-point-pair)) (annotate 'record '(33 34)))
5176 (when (~posmatch "incorrect size" rep.routine*!error)
5177 (prn "F - 'setm' checks size of target"))
5179 (setm '((3 integer-point-pair-address) (deref)) (annotate 'record '(43 44 45)))
5180 (when (~memory-contains 4 '(43 44 45))
5181 (prn "F - 'setm' supports indirect writes to records"))
5182 (setm '((2 integer-point-pair-address-address) (deref) (deref)) (annotate 'record '(53 54 55)))
5183 (when (~memory-contains 4 '(53 54 55))
5184 (prn "F - 'setm' supports multiply indirect writes to records"))
5185 (setm '((4 integer-array)) (annotate 'record '(2 31 32)))
5186 (when (~memory-contains 4 '(2 31 32))
5187 (prn "F - 'setm' writes arrays"))
5188 (setm '((3 integer-array-address) (deref)) (annotate 'record '(2 41 42)))
5189 (when (~memory-contains 4 '(2 41 42))
5190 (prn "F - 'setm' supports indirect writes to arrays"))
5191 (= routine* make-routine!foo)
5192 (setm '((4 integer-array)) (annotate 'record '(2 31 32 33)))
5193 (when (~posmatch "invalid array" rep.routine*!error)
5194 (prn "F - 'setm' checks that array written is well-formed"))
5195 (= routine* make-routine!foo)
5197 ;? (= dump-trace* (obj whitelist '("sizeof" "mem")))
5198 (setm '((4 integer-boolean-pair-array)) (annotate 'record '(2 31 nil 32 nil 33)))
5199 (when (~posmatch "invalid array" rep.routine*!error)
5200 (prn "F - 'setm' checks that array of records is well-formed"))
5201 (= routine* make-routine!foo)
5203 (setm '((4 integer-boolean-pair-array)) (annotate 'record '(2 31 nil 32 nil)))
5204 (when (posmatch "invalid array" rep.routine*!error)
5205 (prn "F - 'setm' checks that array of records is well-formed - 2"))
5208 (reset) ; end file with this to persist the trace for the final test