2014-10-10 22:09:16 +00:00
|
|
|
;; what happens when our virtual machine starts up
|
2014-08-19 17:31:58 +00:00
|
|
|
(= initialization-fns* (queue))
|
|
|
|
(def reset ()
|
|
|
|
(each f (as cons initialization-fns*)
|
|
|
|
(f)))
|
|
|
|
|
2014-08-29 02:53:41 +00:00
|
|
|
(mac on-init body
|
|
|
|
`(enq (fn () ,@body)
|
|
|
|
initialization-fns*))
|
|
|
|
|
2014-10-10 22:04:14 +00:00
|
|
|
(mac init-fn (name . body)
|
2014-11-01 00:35:24 +00:00
|
|
|
`(enq (fn () (= (function* ',name) (convert-names:convert-braces ',body)))
|
2014-10-10 22:04:14 +00:00
|
|
|
initialization-fns*))
|
|
|
|
|
|
|
|
; things that a future assembler will need separate memory for:
|
|
|
|
; code; types; args channel
|
|
|
|
(def clear ()
|
|
|
|
(= types* (table))
|
|
|
|
(= memory* (table))
|
|
|
|
(= function* (table)))
|
|
|
|
(enq clear initialization-fns*)
|
|
|
|
|
2014-10-28 18:32:00 +00:00
|
|
|
(on-init
|
|
|
|
(= types* (obj
|
|
|
|
; Each type must be scalar or array, sum or product or primitive
|
|
|
|
type (obj size 1) ; implicitly scalar and primitive
|
|
|
|
type-address (obj size 1 address t elem 'type)
|
|
|
|
type-array (obj array t elem 'type)
|
|
|
|
type-array-address (obj size 1 address t elem 'type-array)
|
|
|
|
location (obj size 1 address t elem 'location) ; assume it points to an atom
|
|
|
|
integer (obj size 1)
|
|
|
|
boolean (obj size 1)
|
|
|
|
boolean-address (obj size 1 address t)
|
|
|
|
byte (obj size 1)
|
|
|
|
;? string (obj array t elem 'byte) ; inspired by Go
|
|
|
|
character (obj size 1) ; int32 like a Go rune
|
|
|
|
character-address (obj size 1 address t elem 'character)
|
|
|
|
string (obj size 1) ; temporary hack
|
2014-10-29 17:33:43 +00:00
|
|
|
; isolating function calls
|
|
|
|
scope (obj array t elem 'location) ; by convention index 0 points to outer scope
|
|
|
|
scope-address (obj size 1 address t elem 'scope)
|
2014-10-28 18:32:00 +00:00
|
|
|
; arrays consist of an integer length followed by the right number of elems
|
|
|
|
integer-array (obj array t elem 'integer)
|
2014-11-01 09:23:32 +00:00
|
|
|
integer-array-address (obj size 1 address t elem 'integer-array)
|
2014-10-28 18:32:00 +00:00
|
|
|
integer-address (obj size 1 address t elem 'integer) ; pointer to int
|
|
|
|
; records consist of a series of elems, corresponding to a list of types
|
|
|
|
integer-boolean-pair (obj size 2 record t elems '(integer boolean))
|
|
|
|
integer-boolean-pair-address (obj size 1 address t elem 'integer-boolean-pair)
|
|
|
|
integer-boolean-pair-array (obj array t elem 'integer-boolean-pair)
|
2014-11-01 09:16:16 +00:00
|
|
|
integer-boolean-pair-array-address (obj size 1 address t elem 'integer-boolean-pair-array)
|
2014-10-28 18:32:00 +00:00
|
|
|
integer-integer-pair (obj size 2 record t elems '(integer integer))
|
|
|
|
integer-point-pair (obj size 2 record t elems '(integer integer-integer-pair))
|
|
|
|
; tagged-values are the foundation of dynamic types
|
|
|
|
tagged-value (obj size 2 record t elems '(type location))
|
|
|
|
tagged-value-address (obj size 1 address t elem 'tagged-value)
|
|
|
|
; heterogeneous lists
|
|
|
|
list (obj size 2 record t elems '(tagged-value list-address))
|
|
|
|
list-address (obj size 1 address t elem 'list)
|
|
|
|
list-address-address (obj size 1 address t elem 'list-address)
|
2014-10-28 18:41:59 +00:00
|
|
|
; editor
|
|
|
|
line (obj array t elem 'character)
|
|
|
|
line-address (obj size 1 address t elem 'line)
|
|
|
|
line-address-address (obj size 1 address t elem 'line-address)
|
|
|
|
screen (obj array t elem 'line-address)
|
|
|
|
screen-address (obj size 1 address t elem 'screen)
|
2014-10-28 18:32:00 +00:00
|
|
|
)))
|
|
|
|
|
2014-10-10 22:09:16 +00:00
|
|
|
;; persisting and checking traces for each test
|
2014-10-07 17:26:14 +00:00
|
|
|
(= traces* (queue))
|
|
|
|
(= trace-dir* ".traces/")
|
|
|
|
(ensure-dir trace-dir*)
|
|
|
|
(= curr-trace-file* nil)
|
2014-08-29 02:53:41 +00:00
|
|
|
(on-init
|
2014-10-07 17:26:14 +00:00
|
|
|
(awhen curr-trace-file*
|
|
|
|
;? (prn "reset: " it)
|
|
|
|
(tofile (+ trace-dir* it)
|
|
|
|
(each (label trace) (as cons traces*)
|
|
|
|
(pr label ": " trace))))
|
|
|
|
(= curr-trace-file* nil)
|
2014-08-29 03:42:15 +00:00
|
|
|
(= traces* (queue)))
|
2014-10-07 17:26:14 +00:00
|
|
|
|
|
|
|
(def new-trace (filename)
|
2014-11-01 00:36:29 +00:00
|
|
|
;? (prn "new-trace " filename)
|
2014-10-07 17:26:14 +00:00
|
|
|
(= curr-trace-file* filename))
|
|
|
|
|
2014-10-07 20:26:01 +00:00
|
|
|
(= dump-trace* nil)
|
2014-08-29 02:53:41 +00:00
|
|
|
(def trace (label . args)
|
2014-10-07 20:26:01 +00:00
|
|
|
;? (prn "trace: " dump-trace*)
|
2014-11-01 04:16:17 +00:00
|
|
|
(when (or (is dump-trace* t)
|
|
|
|
(and dump-trace* (~pos label dump-trace*!blacklist)))
|
2014-11-01 00:35:24 +00:00
|
|
|
(apply prn label ": " args))
|
2014-08-29 03:42:15 +00:00
|
|
|
(enq (list label (apply tostring:prn args))
|
|
|
|
traces*))
|
|
|
|
|
|
|
|
(def check-trace-contents (msg expected-contents)
|
|
|
|
(unless (trace-contents-match expected-contents)
|
|
|
|
(prn "F - " msg)
|
|
|
|
(prn " trace contents")
|
|
|
|
(print-trace-contents-mismatch expected-contents)))
|
|
|
|
|
|
|
|
(def trace-contents-match (expected-contents)
|
|
|
|
(each (label msg) (as cons traces*)
|
|
|
|
(when (and expected-contents
|
|
|
|
(is label expected-contents.0.0)
|
|
|
|
(posmatch expected-contents.0.1 msg))
|
|
|
|
(pop expected-contents)))
|
|
|
|
(no expected-contents))
|
|
|
|
|
|
|
|
(def print-trace-contents-mismatch (expected-contents)
|
|
|
|
(each (label msg) (as cons traces*)
|
|
|
|
(whenlet (expected-label expected-msg) expected-contents.0
|
|
|
|
(if (and (is label expected-label)
|
|
|
|
(posmatch expected-msg msg))
|
|
|
|
(do (pr " * ")
|
|
|
|
(pop expected-contents))
|
|
|
|
(pr " "))
|
|
|
|
(pr label ": " msg)))
|
|
|
|
(prn " couldn't find")
|
|
|
|
(each (expected-label expected-msg) expected-contents
|
|
|
|
(prn " ! " expected-label ": " expected-msg)))
|
2014-08-29 02:53:41 +00:00
|
|
|
|
2014-07-06 08:41:37 +00:00
|
|
|
(def add-fns (fns)
|
|
|
|
(each (name . body) fns
|
2014-11-01 04:16:17 +00:00
|
|
|
(= function*.name (convert-names:convert-braces body))))
|
2014-07-06 07:07:03 +00:00
|
|
|
|
2014-10-10 22:09:16 +00:00
|
|
|
;; running mu
|
2014-10-29 07:18:58 +00:00
|
|
|
(mac v (operand) ; for value
|
|
|
|
`(,operand 0))
|
2014-07-31 08:53:14 +00:00
|
|
|
|
2014-08-21 02:49:05 +00:00
|
|
|
(def metadata (operand)
|
|
|
|
cdr.operand)
|
2014-07-31 09:27:41 +00:00
|
|
|
|
2014-08-21 02:49:05 +00:00
|
|
|
(def ty (operand)
|
|
|
|
operand.1) ; assume type is always first bit of metadata, and it's always present
|
2014-07-31 08:53:14 +00:00
|
|
|
|
2014-08-22 02:55:16 +00:00
|
|
|
(def typeinfo (operand)
|
2014-11-01 09:16:16 +00:00
|
|
|
(or (types* ty.operand)
|
|
|
|
(err "unknown type @operand")))
|
2014-08-22 02:55:16 +00:00
|
|
|
|
2014-08-21 02:49:05 +00:00
|
|
|
(def sz (operand)
|
2014-10-11 18:17:04 +00:00
|
|
|
(trace "sz" operand)
|
2014-10-11 17:09:41 +00:00
|
|
|
(if (is 'literal ty.operand)
|
|
|
|
'literal
|
2014-10-11 18:17:04 +00:00
|
|
|
(pos 'deref metadata.operand)
|
2014-11-01 00:50:38 +00:00
|
|
|
(do (assert typeinfo.operand!address "tried to deref non-address @operand")
|
2014-10-11 18:17:04 +00:00
|
|
|
(sz (list (m `(,(v operand) location))
|
|
|
|
typeinfo.operand!elem)))
|
2014-10-11 17:09:41 +00:00
|
|
|
(let-or it typeinfo.operand (err "no such type: @operand")
|
|
|
|
(if it!array
|
|
|
|
array-len.operand
|
|
|
|
it!size))))
|
2014-08-21 07:29:55 +00:00
|
|
|
(defextend sz (typename) (isa typename 'sym)
|
2014-10-07 06:39:13 +00:00
|
|
|
(or types*.typename!size
|
|
|
|
(err "type @typename doesn't have a size: " (tostring:pr types*.typename))))
|
2014-08-21 00:43:15 +00:00
|
|
|
|
2014-08-27 05:05:30 +00:00
|
|
|
(def addr (loc)
|
2014-11-01 00:35:24 +00:00
|
|
|
;? (trace "addr" loc)
|
2014-10-30 05:39:53 +00:00
|
|
|
(ret result v.loc
|
2014-10-31 00:39:41 +00:00
|
|
|
(unless (pos 'global metadata.loc)
|
2014-10-31 23:11:12 +00:00
|
|
|
(whenlet base rep.routine*!call-stack.0!default-scope
|
2014-10-31 00:39:41 +00:00
|
|
|
(if (< result memory*.base)
|
|
|
|
(++ result base)
|
|
|
|
(die "addr: no room for var @result"))))
|
2014-10-30 07:41:09 +00:00
|
|
|
(when (pos 'deref metadata.loc)
|
2014-10-30 05:39:53 +00:00
|
|
|
(zap memory* result))))
|
2014-08-20 06:37:50 +00:00
|
|
|
|
|
|
|
(def addrs (n sz)
|
|
|
|
(accum yield
|
|
|
|
(repeat sz
|
|
|
|
(yield n)
|
|
|
|
(++ n))))
|
|
|
|
|
2014-08-27 05:05:30 +00:00
|
|
|
(def m (loc) ; read memory, respecting metadata
|
2014-10-29 17:29:30 +00:00
|
|
|
(point return
|
2014-10-31 00:21:47 +00:00
|
|
|
(if (in ty.loc 'literal 'offset)
|
2014-10-29 17:29:30 +00:00
|
|
|
(return v.loc))
|
2014-11-01 00:35:24 +00:00
|
|
|
(when (is v.loc 'default-scope)
|
|
|
|
(return rep.routine*!call-stack.0!default-scope))
|
2014-11-01 04:04:22 +00:00
|
|
|
(trace "m" loc)
|
|
|
|
(assert (isa v.loc 'int) "addresses must be numeric (problem in convert-names?) @loc")
|
2014-10-29 17:29:30 +00:00
|
|
|
(if (is 1 sz.loc)
|
|
|
|
(memory* addr.loc)
|
|
|
|
:else
|
|
|
|
(annotate 'record
|
|
|
|
(map memory* (addrs addr.loc sz.loc))))))
|
2014-07-27 17:55:08 +00:00
|
|
|
|
2014-08-27 05:05:30 +00:00
|
|
|
(def setm (loc val) ; set memory, respecting metadata
|
2014-10-29 17:33:43 +00:00
|
|
|
(point return
|
|
|
|
(when (is v.loc 'default-scope)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is 1 sz.loc) "can't store compounds in default-scope @loc")
|
2014-10-31 23:11:12 +00:00
|
|
|
(= rep.routine*!call-stack.0!default-scope val)
|
2014-10-29 17:33:43 +00:00
|
|
|
(return))
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (isa v.loc 'int) "can't store to non-numeric address (problem in convert-names?)")
|
2014-10-29 17:33:43 +00:00
|
|
|
(trace "setm" loc " <= " val)
|
|
|
|
(let n sz.loc
|
|
|
|
(trace "setm" "size of " loc " is " n)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert n "setm: can't compute type of @loc")
|
2014-10-29 17:33:43 +00:00
|
|
|
(if (is 1 n)
|
2014-11-01 22:06:24 +00:00
|
|
|
(do (assert (~isa val 'record) "setm: record of size 1 @val")
|
2014-10-29 17:33:43 +00:00
|
|
|
(= (memory* addr.loc) val))
|
2014-11-01 22:06:24 +00:00
|
|
|
(do (assert (isa val 'record) "setm: non-record of size >1 @val")
|
2014-10-29 17:33:43 +00:00
|
|
|
(each (dest src) (zip (addrs addr.loc n)
|
|
|
|
(rep val))
|
|
|
|
(= (memory* dest) src)))))))
|
2014-07-31 09:27:41 +00:00
|
|
|
|
2014-11-01 04:07:47 +00:00
|
|
|
; (operand field-offset) -> (base-addr field-type)
|
|
|
|
; operand can be a deref address
|
|
|
|
; operand can be scope-based
|
|
|
|
; base-addr returned is always global
|
|
|
|
(def record-info (operand field-offset)
|
|
|
|
(assert (is 'offset (ty field-offset)) "record index @field-offset must have type 'offset'")
|
|
|
|
(with (base addr.operand
|
|
|
|
basetype typeinfo.operand
|
|
|
|
idx (v field-offset))
|
|
|
|
(when (pos 'deref metadata.operand)
|
2014-11-01 09:17:23 +00:00
|
|
|
(assert basetype!address "@operand requests deref, but it's not an address of a record")
|
2014-11-01 04:07:47 +00:00
|
|
|
(= basetype (types* basetype!elem)))
|
|
|
|
(assert basetype!record "get on non-record @operand")
|
2014-11-01 09:17:23 +00:00
|
|
|
(assert (< -1 idx (len basetype!elems)) "@idx is out of bounds of record @operand")
|
2014-11-01 04:07:47 +00:00
|
|
|
(list (+ base (apply + (map sz (firstn idx basetype!elems))))
|
|
|
|
basetype!elems.idx)))
|
|
|
|
|
2014-11-01 09:23:32 +00:00
|
|
|
(def array-info (operand offset)
|
|
|
|
(with (base addr.operand
|
|
|
|
basetype typeinfo.operand
|
|
|
|
idx (m offset))
|
|
|
|
(when (pos 'deref metadata.operand)
|
|
|
|
(assert basetype!address "@operand requests deref, but it's not an address of an array")
|
2014-11-01 09:28:00 +00:00
|
|
|
(= basetype (types* basetype!elem)))
|
2014-11-01 09:23:32 +00:00
|
|
|
(assert basetype!array "index on non-array @operand")
|
|
|
|
(unless (< -1 idx array-len.operand)
|
|
|
|
(die "@idx is out of bounds of array @operand"))
|
|
|
|
(list (+ base
|
|
|
|
1 ; for array size
|
|
|
|
(* idx (sz basetype!elem)))
|
|
|
|
basetype!elem)))
|
2014-08-22 02:55:16 +00:00
|
|
|
|
2014-11-01 09:28:00 +00:00
|
|
|
(def array-len (operand)
|
|
|
|
(if typeinfo.operand!array
|
|
|
|
(m `(,v.operand integer))
|
|
|
|
(and typeinfo.operand!address (pos 'deref metadata.operand))
|
|
|
|
(m `(,v.operand integer-address ,@(cut operand 2)))
|
|
|
|
:else
|
|
|
|
(err "can't take len of non-array @operand")))
|
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
; data structure: routine
|
|
|
|
; runtime state for a serial thread of execution
|
2014-08-28 19:44:01 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(def make-routine (fn-name)
|
|
|
|
(annotate 'routine (obj call-stack (list
|
2014-08-29 02:54:37 +00:00
|
|
|
(obj fn-name fn-name pc 0 caller-arg-idx 0)))))
|
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(defextend empty (x) (isa x 'routine)
|
2014-08-29 02:23:38 +00:00
|
|
|
(no rep.x!call-stack))
|
2014-08-28 19:44:01 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(def stack (routine)
|
|
|
|
((rep routine) 'call-stack))
|
2014-08-28 19:44:01 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(mac push-stack (routine op)
|
2014-08-29 02:23:38 +00:00
|
|
|
`(push (obj fn-name ,op pc 0 caller-arg-idx 0)
|
2014-10-29 20:05:08 +00:00
|
|
|
((rep ,routine) 'call-stack)))
|
2014-08-29 02:23:38 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(mac pop-stack (routine)
|
|
|
|
`(pop ((rep ,routine) 'call-stack)))
|
2014-08-29 02:23:38 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(def top (routine)
|
|
|
|
stack.routine.0)
|
2014-08-29 02:23:38 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(def body (routine (o idx 0))
|
|
|
|
(function* stack.routine.idx!fn-name))
|
2014-08-29 02:23:38 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(mac pc (routine (o idx 0)) ; assignable
|
|
|
|
`((((rep ,routine) 'call-stack) ,idx) 'pc))
|
2014-08-29 02:23:38 +00:00
|
|
|
|
2014-10-29 20:05:08 +00:00
|
|
|
(mac caller-arg-idx (routine (o idx 0)) ; assignable
|
|
|
|
`((((rep ,routine) 'call-stack) ,idx) 'caller-arg-idx))
|
2014-08-28 19:44:01 +00:00
|
|
|
|
2014-10-05 17:31:38 +00:00
|
|
|
(= scheduling-interval* 500)
|
2014-08-28 19:44:01 +00:00
|
|
|
|
|
|
|
(def parse-instr (instr)
|
|
|
|
(iflet delim (pos '<- instr)
|
|
|
|
(list (cut instr 0 delim) ; oargs
|
|
|
|
(instr (+ delim 1)) ; op
|
|
|
|
(cut instr (+ delim 2))) ; args
|
|
|
|
(list nil instr.0 cdr.instr)))
|
|
|
|
|
2014-10-31 22:21:07 +00:00
|
|
|
(mac caller-args (routine) ; assignable
|
|
|
|
`((((rep ,routine) 'call-stack) 0) 'args))
|
2014-08-28 19:44:01 +00:00
|
|
|
|
2014-10-31 23:18:13 +00:00
|
|
|
(mac results (routine) ; assignable
|
|
|
|
`((((rep ,routine) 'call-stack) 0) 'results))
|
|
|
|
|
2014-10-30 05:19:03 +00:00
|
|
|
(on-init
|
|
|
|
(= running-routines* (queue))
|
|
|
|
(= completed-routines* (queue))
|
2014-10-30 07:37:03 +00:00
|
|
|
(= routine* nil)
|
|
|
|
(= abort-routine* (parameter nil)))
|
|
|
|
|
|
|
|
; like arc's 'point' but you can also call ((abort-routine*)) in nested calls
|
|
|
|
(mac routine-mark body
|
|
|
|
(w/uniq (g p)
|
|
|
|
`(ccc (fn (,g)
|
|
|
|
(parameterize abort-routine* (fn ((o ,p)) (,g ,p))
|
|
|
|
,@body)))))
|
2014-08-29 03:44:16 +00:00
|
|
|
|
2014-08-31 18:27:58 +00:00
|
|
|
(def run fn-names
|
2014-08-28 23:47:09 +00:00
|
|
|
(ret result 0
|
2014-08-31 18:27:58 +00:00
|
|
|
(each it fn-names
|
2014-10-29 20:10:40 +00:00
|
|
|
(enq make-routine.it running-routines*))
|
2014-08-29 03:44:16 +00:00
|
|
|
; simple round-robin scheduler
|
2014-10-29 20:10:40 +00:00
|
|
|
(while (~empty running-routines*)
|
|
|
|
(= routine* deq.running-routines*)
|
2014-10-29 20:05:08 +00:00
|
|
|
(trace "schedule" top.routine*!fn-name)
|
2014-10-30 07:37:03 +00:00
|
|
|
(whenlet insts-run (routine-mark:run-for-time-slice scheduling-interval*)
|
2014-10-29 17:26:25 +00:00
|
|
|
(= result (+ result insts-run)))
|
2014-10-29 20:05:08 +00:00
|
|
|
(if (~empty routine*)
|
2014-10-29 20:10:40 +00:00
|
|
|
(enq routine* running-routines*)
|
|
|
|
(enq-limit routine* completed-routines*)))))
|
2014-08-28 23:47:09 +00:00
|
|
|
|
2014-10-30 05:19:03 +00:00
|
|
|
(def die (msg)
|
|
|
|
(= rep.routine*!error msg)
|
|
|
|
(= rep.routine*!stack-trace rep.routine*!call-stack)
|
2014-10-30 07:37:03 +00:00
|
|
|
(wipe rep.routine*!call-stack)
|
|
|
|
((abort-routine*)))
|
2014-10-30 05:19:03 +00:00
|
|
|
|
2014-10-05 03:18:55 +00:00
|
|
|
($:require "charterm/main.rkt")
|
|
|
|
|
2014-10-29 17:26:25 +00:00
|
|
|
(def run-for-time-slice (time-slice)
|
2014-08-28 19:44:01 +00:00
|
|
|
;? (prn "AAA")
|
2014-08-28 23:40:28 +00:00
|
|
|
(point return
|
2014-08-28 19:44:01 +00:00
|
|
|
;? (prn "BBB")
|
2014-08-28 23:47:09 +00:00
|
|
|
(for ninstrs 0 (< ninstrs time-slice) (++ ninstrs)
|
2014-10-29 20:05:08 +00:00
|
|
|
;? (prn "CCC " pc.routine* " " routine* " " (len body.routine*))
|
|
|
|
(if (empty body.routine*) (err "@stack.routine*.0!fn-name not defined"))
|
|
|
|
(while (>= pc.routine* (len body.routine*))
|
|
|
|
(pop-stack routine*)
|
|
|
|
(if empty.routine* (return ninstrs))
|
|
|
|
(++ pc.routine*))
|
2014-10-12 17:31:57 +00:00
|
|
|
(trace "run" "-- " (sort (compare < string:car) (as cons memory*)))
|
2014-10-29 20:05:08 +00:00
|
|
|
(trace "run" top.routine*!fn-name " " pc.routine* ": " (body.routine* pc.routine*))
|
2014-11-01 00:35:24 +00:00
|
|
|
;? (trace "run" routine*)
|
2014-10-29 20:05:08 +00:00
|
|
|
(let (oarg op arg) (parse-instr (body.routine* pc.routine*))
|
2014-10-07 15:32:45 +00:00
|
|
|
;? (prn op " " arg " -> " oarg)
|
2014-08-28 19:44:01 +00:00
|
|
|
(let tmp
|
2014-07-31 10:46:05 +00:00
|
|
|
(case op
|
2014-10-10 22:04:14 +00:00
|
|
|
; arithmetic
|
2014-07-31 10:46:05 +00:00
|
|
|
add
|
2014-10-07 20:26:01 +00:00
|
|
|
(do (trace "add" (m arg.0) " " (m arg.1))
|
2014-07-31 10:46:05 +00:00
|
|
|
(+ (m arg.0) (m arg.1))
|
2014-10-07 20:26:01 +00:00
|
|
|
)
|
2014-07-31 10:46:05 +00:00
|
|
|
sub
|
|
|
|
(- (m arg.0) (m arg.1))
|
|
|
|
mul
|
|
|
|
(* (m arg.0) (m arg.1))
|
|
|
|
div
|
|
|
|
(/ (real (m arg.0)) (m arg.1))
|
|
|
|
idiv
|
2014-08-27 05:00:23 +00:00
|
|
|
(list (trunc:/ (m arg.0) (m arg.1))
|
|
|
|
(mod (m arg.0) (m arg.1)))
|
2014-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
; boolean
|
2014-07-31 10:46:05 +00:00
|
|
|
and
|
|
|
|
(and (m arg.0) (m arg.1))
|
|
|
|
or
|
|
|
|
(or (m arg.0) (m arg.1))
|
|
|
|
not
|
|
|
|
(not (m arg.0))
|
2014-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
; comparison
|
2014-07-31 10:46:05 +00:00
|
|
|
eq
|
|
|
|
(is (m arg.0) (m arg.1))
|
|
|
|
neq
|
2014-10-07 20:26:01 +00:00
|
|
|
(do (trace "neq" (m arg.0) " " (m arg.1))
|
2014-07-31 10:46:05 +00:00
|
|
|
(~is (m arg.0) (m arg.1))
|
2014-10-07 20:26:01 +00:00
|
|
|
)
|
2014-07-31 10:46:05 +00:00
|
|
|
lt
|
|
|
|
(< (m arg.0) (m arg.1))
|
|
|
|
gt
|
|
|
|
(> (m arg.0) (m arg.1))
|
|
|
|
le
|
|
|
|
(<= (m arg.0) (m arg.1))
|
|
|
|
ge
|
|
|
|
(>= (m arg.0) (m arg.1))
|
2014-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
; control flow
|
2014-10-15 00:51:30 +00:00
|
|
|
jump
|
2014-10-29 20:05:08 +00:00
|
|
|
(do (= pc.routine* (+ 1 pc.routine* (v arg.0)))
|
|
|
|
;? (trace "jump" "jumping to " pc.routine*)
|
2014-07-31 10:46:05 +00:00
|
|
|
(continue))
|
2014-10-15 00:51:30 +00:00
|
|
|
jump-if
|
2014-07-31 10:46:05 +00:00
|
|
|
(when (is t (m arg.0))
|
2014-10-29 20:05:08 +00:00
|
|
|
(= pc.routine* (+ 1 pc.routine* (v arg.1)))
|
|
|
|
;? (trace "jump-if" "jumping to " pc.routine*)
|
2014-10-15 01:24:46 +00:00
|
|
|
(continue))
|
|
|
|
jump-unless ; convenient helper
|
|
|
|
(unless (is t (m arg.0))
|
2014-10-29 20:05:08 +00:00
|
|
|
(= pc.routine* (+ 1 pc.routine* (v arg.1)))
|
|
|
|
;? (trace "jump-unless" "jumping to " pc.routine*)
|
2014-07-12 04:29:43 +00:00
|
|
|
(continue))
|
2014-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
; data management: scalars, arrays, records
|
2014-07-31 10:46:05 +00:00
|
|
|
copy
|
|
|
|
(m arg.0)
|
2014-08-20 04:33:48 +00:00
|
|
|
get
|
2014-11-01 04:07:47 +00:00
|
|
|
(let (addr type) (record-info arg.0 arg.1)
|
2014-11-01 09:23:32 +00:00
|
|
|
;? (prn addr " " type)
|
2014-11-01 04:07:47 +00:00
|
|
|
(m `(,addr ,type global)))
|
2014-10-05 18:34:23 +00:00
|
|
|
get-address
|
2014-11-01 04:07:47 +00:00
|
|
|
(let (addr _) (record-info arg.0 arg.1)
|
|
|
|
addr)
|
2014-10-06 03:03:03 +00:00
|
|
|
index
|
2014-11-01 09:23:32 +00:00
|
|
|
(let (addr type) (array-info arg.0 arg.1)
|
|
|
|
;? (prn arg.0 " " arg.1 " => " addr " " type)
|
|
|
|
(m `(,addr ,type global)))
|
2014-10-06 03:03:03 +00:00
|
|
|
index-address
|
2014-11-01 09:23:32 +00:00
|
|
|
(let (addr _) (array-info arg.0 arg.1)
|
|
|
|
addr)
|
2014-08-31 18:20:28 +00:00
|
|
|
new
|
|
|
|
(let type (v arg.0)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is 'literal (ty arg.0)) "new: second arg @arg.0 must be literal")
|
2014-10-29 17:27:28 +00:00
|
|
|
(if (no types*.type) (err "no such type @type"))
|
2014-08-31 18:20:28 +00:00
|
|
|
(if types*.type!array
|
2014-10-07 05:58:06 +00:00
|
|
|
(new-array type (m arg.1))
|
2014-08-31 18:20:28 +00:00
|
|
|
(new-scalar type)))
|
2014-10-05 18:32:25 +00:00
|
|
|
sizeof
|
2014-10-12 19:01:04 +00:00
|
|
|
(sizeof (m arg.0))
|
2014-10-05 18:32:25 +00:00
|
|
|
len
|
|
|
|
(let base arg.0
|
2014-11-01 09:16:16 +00:00
|
|
|
(if (or typeinfo.base!array typeinfo.base!address)
|
2014-10-05 18:32:25 +00:00
|
|
|
array-len.base
|
|
|
|
-1))
|
2014-10-05 03:19:12 +00:00
|
|
|
|
2014-10-24 18:38:02 +00:00
|
|
|
; tagged-values require one primitive
|
|
|
|
save-type
|
2014-10-25 09:32:30 +00:00
|
|
|
(annotate 'record `(,(ty arg.0) ,(m arg.0)))
|
2014-10-24 18:38:02 +00:00
|
|
|
|
2014-10-05 03:19:12 +00:00
|
|
|
; multiprocessing
|
|
|
|
run
|
|
|
|
(run (v arg.0))
|
|
|
|
fork
|
2014-10-29 20:10:40 +00:00
|
|
|
(enq (make-routine (v arg.0)) running-routines*)
|
2014-10-12 19:01:04 +00:00
|
|
|
; todo: errors should stall a process and let its parent
|
|
|
|
; inspect it
|
|
|
|
assert
|
|
|
|
(assert (m arg.0))
|
2014-10-05 03:19:12 +00:00
|
|
|
|
2014-10-05 06:00:19 +00:00
|
|
|
; text interaction
|
|
|
|
cls
|
|
|
|
(do1 nil ($.charterm-clear-screen))
|
|
|
|
cll
|
|
|
|
(do1 nil ($.charterm-clear-line))
|
|
|
|
cursor
|
|
|
|
(do1 nil ($.charterm-cursor (m arg.0) (m arg.1)))
|
2014-10-15 00:27:10 +00:00
|
|
|
print-primitive
|
2014-10-05 17:44:14 +00:00
|
|
|
(do1 nil ((if ($.current-charterm) $.charterm-display pr) (m arg.0)))
|
2014-10-05 06:00:19 +00:00
|
|
|
getc
|
2014-10-08 07:38:08 +00:00
|
|
|
(and ($.charterm-byte-ready?) ($.charterm-read-key))
|
2014-10-05 06:00:19 +00:00
|
|
|
bold-mode
|
|
|
|
(do1 nil ($.charterm-bold))
|
|
|
|
non-bold-mode
|
|
|
|
(do1 nil ($.charterm-normal))
|
2014-10-05 17:44:14 +00:00
|
|
|
console-on
|
2014-10-15 00:21:39 +00:00
|
|
|
(do1 nil (if (no ($.current-charterm)) ($.open-charterm)))
|
2014-10-05 17:44:14 +00:00
|
|
|
console-off
|
|
|
|
(do1 nil (if ($.current-charterm) ($.close-charterm)))
|
2014-10-05 06:00:19 +00:00
|
|
|
|
2014-10-10 22:04:14 +00:00
|
|
|
; user-defined functions
|
|
|
|
arg
|
|
|
|
(let idx (if arg
|
2014-11-01 04:16:17 +00:00
|
|
|
(do (assert (is 'literal (ty arg.0)))
|
|
|
|
(v arg.0))
|
2014-10-29 20:05:08 +00:00
|
|
|
(do1 caller-arg-idx.routine*
|
|
|
|
(++ caller-arg-idx.routine*)))
|
|
|
|
(trace "arg" arg " " idx " " caller-args.routine*)
|
|
|
|
(if (len> caller-args.routine* idx)
|
2014-10-31 22:21:07 +00:00
|
|
|
(list caller-args.routine*.idx t)
|
2014-10-12 17:49:08 +00:00
|
|
|
(list nil nil)))
|
2014-10-31 23:22:21 +00:00
|
|
|
prepare-reply
|
2014-10-31 23:24:17 +00:00
|
|
|
(prepare-reply arg)
|
2014-07-31 10:46:05 +00:00
|
|
|
reply
|
2014-10-31 23:24:17 +00:00
|
|
|
(do (when arg
|
|
|
|
(prepare-reply arg))
|
2014-10-31 23:18:13 +00:00
|
|
|
(let results results.routine*
|
2014-10-29 20:05:08 +00:00
|
|
|
(pop-stack routine*)
|
2014-10-31 23:18:13 +00:00
|
|
|
(if empty.routine* (return ninstrs))
|
|
|
|
(let (caller-oargs _ _) (parse-instr (body.routine* pc.routine*))
|
|
|
|
(trace "reply" arg " " caller-oargs)
|
|
|
|
(each (dest val) (zip caller-oargs results)
|
|
|
|
(trace "reply" val " => " dest)
|
|
|
|
(setm dest val)))
|
|
|
|
(++ pc.routine*)
|
|
|
|
(while (>= pc.routine* (len body.routine*))
|
|
|
|
(pop-stack routine*)
|
|
|
|
(when empty.routine* (return ninstrs))
|
|
|
|
(++ pc.routine*))
|
|
|
|
(continue)))
|
2014-10-10 22:04:14 +00:00
|
|
|
; else try to call as a user-defined function
|
2014-10-05 18:33:25 +00:00
|
|
|
(do (if function*.op
|
2014-11-01 00:35:24 +00:00
|
|
|
(let callee-args (accum yield
|
|
|
|
(each a arg
|
|
|
|
(yield (m a))))
|
|
|
|
(push-stack routine* op)
|
|
|
|
(= caller-args.routine* callee-args))
|
2014-10-05 18:33:25 +00:00
|
|
|
(err "no such op @op"))
|
2014-08-28 19:44:01 +00:00
|
|
|
(continue))
|
2014-07-31 10:46:05 +00:00
|
|
|
)
|
2014-08-26 17:17:55 +00:00
|
|
|
; opcode generated some value, stored in 'tmp'
|
2014-10-10 22:04:14 +00:00
|
|
|
; copy to output args
|
2014-08-28 19:44:01 +00:00
|
|
|
;? (prn "store: " tmp " " oarg)
|
2014-07-31 10:46:05 +00:00
|
|
|
(if (acons tmp)
|
|
|
|
(for i 0 (< i (min len.tmp len.oarg)) ++.i
|
|
|
|
(setm oarg.i tmp.i))
|
|
|
|
(when oarg ; must be a list
|
2014-10-11 18:17:04 +00:00
|
|
|
(trace "run" "writing to oarg " tmp " => " oarg.0)
|
2014-07-31 10:46:05 +00:00
|
|
|
(setm oarg.0 tmp)))
|
2014-08-28 19:44:01 +00:00
|
|
|
)
|
2014-10-29 20:05:08 +00:00
|
|
|
(++ pc.routine*)))
|
2014-08-28 23:47:09 +00:00
|
|
|
(return time-slice)))
|
2014-07-06 07:07:03 +00:00
|
|
|
|
2014-10-31 23:24:17 +00:00
|
|
|
(def prepare-reply (args)
|
|
|
|
(= results.routine*
|
|
|
|
(accum yield
|
|
|
|
(each a args
|
|
|
|
(yield (m a))))))
|
|
|
|
|
2014-08-26 19:20:08 +00:00
|
|
|
(enq (fn () (= Memory-in-use-until 1000))
|
|
|
|
initialization-fns*)
|
|
|
|
(def new-scalar (type)
|
|
|
|
(ret result Memory-in-use-until
|
|
|
|
(++ Memory-in-use-until sizeof.type)))
|
|
|
|
|
|
|
|
(def new-array (type size)
|
2014-10-07 06:39:13 +00:00
|
|
|
;? (prn "new array: @type @size")
|
2014-08-26 19:20:08 +00:00
|
|
|
(ret result Memory-in-use-until
|
2014-10-30 05:39:21 +00:00
|
|
|
(++ Memory-in-use-until (+ 1 (* (sizeof types*.type!elem) size)))
|
|
|
|
(= (memory* result) size)))
|
2014-08-26 19:20:08 +00:00
|
|
|
|
|
|
|
(def sizeof (type)
|
2014-10-12 19:01:04 +00:00
|
|
|
(trace "sizeof" type)
|
2014-08-26 19:20:08 +00:00
|
|
|
(if (~or types*.type!record types*.type!array)
|
|
|
|
types*.type!size
|
|
|
|
types*.type!record
|
|
|
|
(sum idfn
|
|
|
|
(accum yield
|
|
|
|
(each elem types*.type!elems
|
2014-10-29 17:27:28 +00:00
|
|
|
(yield sizeof.elem))))
|
|
|
|
:else
|
|
|
|
(err "no such type @type")))
|
2014-08-26 19:20:08 +00:00
|
|
|
|
2014-10-10 22:09:16 +00:00
|
|
|
;; desugar structured assembly based on blocks
|
2014-10-10 22:04:14 +00:00
|
|
|
|
2014-07-17 16:02:43 +00:00
|
|
|
(def convert-braces (instrs)
|
|
|
|
(let locs () ; list of information on each brace: (open/close pc)
|
|
|
|
(let pc 0
|
|
|
|
(loop (instrs instrs)
|
|
|
|
(each instr instrs
|
|
|
|
(if (~is 'begin instr.0)
|
|
|
|
(do
|
2014-10-07 20:26:01 +00:00
|
|
|
(trace "cvt0" pc " " instr " -- " locs)
|
2014-07-17 16:02:43 +00:00
|
|
|
(++ pc))
|
2014-07-17 16:21:27 +00:00
|
|
|
; hack: racket replaces curlies with parens, so we need the
|
|
|
|
; keyword begin to delimit blocks.
|
|
|
|
; ultimately there'll be no nesting and curlies will just be in a
|
|
|
|
; line by themselves.
|
2014-07-17 16:02:43 +00:00
|
|
|
(do
|
|
|
|
;? (prn `(open ,pc))
|
|
|
|
(push `(open ,pc) locs)
|
|
|
|
(recur cdr.instr)
|
|
|
|
;? (prn `(close ,pc))
|
|
|
|
(push `(close ,pc) locs))))))
|
|
|
|
(zap rev locs)
|
|
|
|
;? (prn locs)
|
|
|
|
(with (pc 0
|
|
|
|
stack ()) ; elems are pcs
|
|
|
|
(accum yield
|
|
|
|
(loop (instrs instrs)
|
|
|
|
(each instr instrs
|
2014-10-07 20:26:01 +00:00
|
|
|
(point continue
|
2014-07-17 16:02:43 +00:00
|
|
|
(let delim (or (pos '<- instr) -1)
|
|
|
|
(with (oarg (if (>= delim 0)
|
|
|
|
(cut instr 0 delim))
|
|
|
|
op (instr (+ delim 1))
|
|
|
|
arg (cut instr (+ delim 2)))
|
2014-10-07 20:26:01 +00:00
|
|
|
(trace "cvt1" pc " " op " " oarg)
|
2014-07-17 16:02:43 +00:00
|
|
|
(case op
|
|
|
|
begin
|
|
|
|
(do
|
|
|
|
(push pc stack)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "begin: can't take oarg @instr")
|
2014-07-17 16:02:43 +00:00
|
|
|
(recur arg)
|
2014-10-07 20:26:01 +00:00
|
|
|
(pop stack)
|
|
|
|
(continue))
|
2014-07-17 16:21:27 +00:00
|
|
|
break
|
|
|
|
(do
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "break: can't take oarg @instr")
|
|
|
|
(assert (is arg nil) "break: can't take arg @instr")
|
2014-10-15 00:51:30 +00:00
|
|
|
(yield `(jump (,(close-offset pc locs) offset))))
|
|
|
|
break-if
|
2014-07-17 16:02:43 +00:00
|
|
|
(do
|
2014-10-15 00:51:30 +00:00
|
|
|
;? (prn "break-if: " instr)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "break-if: can't take oarg @instr")
|
2014-10-15 00:51:30 +00:00
|
|
|
(yield `(jump-if ,arg.0 (,(close-offset pc locs) offset))))
|
2014-10-15 01:24:46 +00:00
|
|
|
break-unless
|
|
|
|
(do
|
|
|
|
;? (prn "break-if: " instr)
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "break-unless: can't take oarg @instr")
|
2014-10-15 01:24:46 +00:00
|
|
|
(yield `(jump-unless ,arg.0 (,(close-offset pc locs) offset))))
|
2014-07-17 16:21:27 +00:00
|
|
|
continue
|
|
|
|
(do
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "continue: can't take oarg @instr")
|
|
|
|
(assert (is arg nil) "continue: can't take arg @instr")
|
2014-10-25 09:32:30 +00:00
|
|
|
(yield `(jump (,(- stack.0 1 pc) offset))))
|
2014-10-15 00:51:30 +00:00
|
|
|
continue-if
|
2014-07-17 16:21:27 +00:00
|
|
|
(do
|
2014-10-15 00:51:30 +00:00
|
|
|
(trace "cvt0" "continue-if: " instr " => " (- stack.0 1))
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "continue-if: can't take oarg @instr")
|
2014-10-15 00:51:30 +00:00
|
|
|
(yield `(jump-if ,arg.0 (,(- stack.0 1 pc) offset))))
|
2014-10-15 01:24:46 +00:00
|
|
|
continue-unless
|
|
|
|
(do
|
|
|
|
(trace "cvt0" "continue-if: " instr " => " (- stack.0 1))
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is oarg nil) "continue-unless: can't take oarg @instr")
|
2014-10-15 01:24:46 +00:00
|
|
|
(yield `(jump-unless ,arg.0 (,(- stack.0 1 pc) offset))))
|
2014-07-17 16:02:43 +00:00
|
|
|
;else
|
|
|
|
(yield instr))))
|
2014-10-07 20:26:01 +00:00
|
|
|
(++ pc))))))))
|
2014-07-17 16:02:43 +00:00
|
|
|
|
|
|
|
(def close-offset (pc locs)
|
|
|
|
(let close 0
|
|
|
|
(with (stacksize 0
|
|
|
|
done nil)
|
|
|
|
(each (state loc) locs
|
|
|
|
;? (prn " :" close " " state " - " loc)
|
|
|
|
(if (< loc pc)
|
|
|
|
nil ; do nothing
|
|
|
|
(no done)
|
|
|
|
(do
|
|
|
|
; first time
|
|
|
|
(when (and (is 0 stacksize) (~is loc pc))
|
|
|
|
(++ stacksize))
|
|
|
|
(if (is 'open state) (++ stacksize) (-- stacksize))
|
|
|
|
; last time
|
|
|
|
(when (is 0 stacksize)
|
|
|
|
(= close loc)
|
|
|
|
(set done))))))
|
|
|
|
(- close pc 1)))
|
|
|
|
|
2014-10-29 07:18:58 +00:00
|
|
|
;; convert symbolic names to integer offsets
|
|
|
|
|
|
|
|
(def convert-names (instrs)
|
|
|
|
(let offset (table)
|
|
|
|
(let idx 1
|
|
|
|
(each instr instrs
|
|
|
|
(let (oargs op args) (parse-instr instr)
|
|
|
|
(each arg args
|
|
|
|
(when (maybe-add arg offset idx)
|
|
|
|
(err "use before set: @arg")
|
|
|
|
(++ idx)))
|
|
|
|
(each arg oargs
|
|
|
|
(when (maybe-add arg offset idx)
|
|
|
|
(++ idx))))))
|
|
|
|
(each instr instrs
|
|
|
|
(let (oargs op args) (parse-instr instr)
|
|
|
|
(each arg args
|
|
|
|
(when (offset v.arg)
|
|
|
|
(zap offset v.arg)))
|
|
|
|
(each arg oargs
|
|
|
|
(when (offset v.arg)
|
|
|
|
(zap offset v.arg)))))
|
|
|
|
instrs))
|
|
|
|
|
|
|
|
(def maybe-add (arg offset idx)
|
|
|
|
(unless (or (in ty.arg 'literal 'offset)
|
|
|
|
(offset v.arg)
|
2014-10-31 00:34:34 +00:00
|
|
|
(~isa v.arg 'sym)
|
2014-10-31 01:45:44 +00:00
|
|
|
(in v.arg 'nil 'default-scope)
|
|
|
|
(pos 'global metadata.arg))
|
2014-10-29 07:18:58 +00:00
|
|
|
(= (offset v.arg) idx)))
|
|
|
|
|
2014-10-29 15:55:00 +00:00
|
|
|
;; literate tangling system for reordering code
|
|
|
|
|
|
|
|
(def convert-quotes (instrs)
|
|
|
|
(let deferred (queue)
|
|
|
|
(each instr instrs
|
|
|
|
(case instr.0
|
|
|
|
defer
|
|
|
|
(let (q qinstrs) instr.1
|
2014-11-01 00:50:38 +00:00
|
|
|
(assert (is 'make-br-fn q) "defer: first arg must be [quoted]")
|
2014-10-29 15:55:00 +00:00
|
|
|
(each qinstr qinstrs
|
|
|
|
(enq qinstr deferred)))))
|
|
|
|
(accum yield
|
|
|
|
(each instr instrs
|
|
|
|
(unless (in instr.0 'defer) ; keep sync'd with case clauses above
|
|
|
|
(yield instr)))
|
|
|
|
(each instr (as cons deferred)
|
|
|
|
(yield instr)))))
|
|
|
|
|
2014-10-11 17:09:41 +00:00
|
|
|
;; system software
|
|
|
|
|
|
|
|
(init-fn maybe-coerce
|
2014-11-01 00:35:24 +00:00
|
|
|
((default-scope scope-address) <- new (scope literal) (30 literal))
|
|
|
|
((x tagged-value-address) <- new (tagged-value literal))
|
|
|
|
((x tagged-value-address deref) <- arg)
|
|
|
|
((p type) <- arg)
|
|
|
|
((xtype type) <- get (x tagged-value-address deref) (0 offset))
|
|
|
|
((match? boolean) <- eq (xtype type) (p type))
|
2014-10-11 17:09:41 +00:00
|
|
|
{ begin
|
2014-11-01 00:35:24 +00:00
|
|
|
(break-if (match? boolean))
|
2014-10-28 07:29:58 +00:00
|
|
|
(reply (0 literal) (nil literal))
|
2014-10-11 17:09:41 +00:00
|
|
|
}
|
2014-11-01 00:35:24 +00:00
|
|
|
((xvalue location) <- get (x tagged-value-address deref) (1 offset))
|
|
|
|
(reply (xvalue location) (match? boolean)))
|
2014-10-11 17:09:41 +00:00
|
|
|
|
2014-10-12 19:01:04 +00:00
|
|
|
(init-fn new-tagged-value
|
2014-11-01 00:35:24 +00:00
|
|
|
((default-scope scope-address) <- new (scope literal) (30 literal))
|
|
|
|
((xtype type) <- arg)
|
|
|
|
((xtypesize integer) <- sizeof (xtype type))
|
|
|
|
((xcheck boolean) <- eq (xtypesize integer) (1 literal))
|
|
|
|
(assert (xcheck boolean))
|
2014-10-12 19:01:04 +00:00
|
|
|
; todo: check that arg 0 matches the type? or is that for the future typechecker?
|
2014-11-01 00:35:24 +00:00
|
|
|
((result tagged-value-address) <- new (tagged-value literal))
|
|
|
|
((resulttype location) <- get-address (result tagged-value-address deref) (0 offset))
|
|
|
|
((resulttype location deref) <- copy (xtype type))
|
|
|
|
((locaddr location) <- get-address (result tagged-value-address deref) (1 offset))
|
|
|
|
((locaddr location deref) <- arg)
|
|
|
|
(reply (result tagged-value-address)))
|
2014-10-12 19:01:04 +00:00
|
|
|
|
2014-10-13 01:04:29 +00:00
|
|
|
(init-fn list-next ; list-address -> list-address
|
2014-11-01 00:35:24 +00:00
|
|
|
((default-scope scope-address) <- new (scope literal) (30 literal))
|
|
|
|
((base list-address) <- arg)
|
|
|
|
((result list-address) <- get (base list-address deref) (1 offset))
|
|
|
|
(reply (result list-address)))
|
2014-10-13 01:04:29 +00:00
|
|
|
|
|
|
|
(init-fn list-value-address ; list-address -> tagged-value-address
|
2014-11-01 00:35:24 +00:00
|
|
|
((default-scope scope-address) <- new (scope literal) (30 literal))
|
|
|
|
((base list-address) <- arg)
|
|
|
|
((result tagged-value-address) <- get-address (base list-address deref) (0 offset))
|
|
|
|
(reply (result tagged-value-address)))
|
2014-10-12 21:27:26 +00:00
|
|
|
|
2014-10-25 09:32:30 +00:00
|
|
|
(init-fn new-list
|
2014-11-01 00:35:24 +00:00
|
|
|
((default-scope scope-address) <- new (scope literal) (30 literal))
|
|
|
|
((new-list-result list-address) <- new (list literal))
|
|
|
|
((curr list-address) <- copy (new-list-result list-address))
|
2014-10-25 09:32:30 +00:00
|
|
|
{ begin
|
2014-11-01 00:35:24 +00:00
|
|
|
((curr-value integer) (exists? boolean) <- arg)
|
|
|
|
(break-unless (exists? boolean))
|
|
|
|
((next list-address-address) <- get-address (curr list-address deref) (1 offset))
|
|
|
|
((next list-address-address deref) <- new (list literal))
|
|
|
|
((curr list-address) <- list-next (curr list-address))
|
|
|
|
((dest tagged-value-address) <- list-value-address (curr list-address))
|
|
|
|
((dest tagged-value-address deref) <- save-type (curr-value integer))
|
2014-10-25 09:32:30 +00:00
|
|
|
(continue)
|
|
|
|
}
|
2014-11-01 00:35:24 +00:00
|
|
|
((new-list-result list-address) <- list-next (new-list-result list-address)) ; memory leak
|
|
|
|
(reply (new-list-result list-address)))
|
2014-10-25 09:32:30 +00:00
|
|
|
|
2014-10-11 17:09:41 +00:00
|
|
|
; drop all traces while processing above functions
|
|
|
|
(on-init
|
|
|
|
(= traces* (queue)))
|
|
|
|
|
2014-10-29 04:17:40 +00:00
|
|
|
(def prn2 (msg . args)
|
|
|
|
(pr msg)
|
|
|
|
(apply prn args))
|
|
|
|
|
2014-10-10 22:09:16 +00:00
|
|
|
;; after loading all files, start at 'main'
|
2014-08-22 18:05:51 +00:00
|
|
|
(reset)
|
2014-07-06 08:41:37 +00:00
|
|
|
(awhen cdr.argv
|
2014-07-27 17:09:00 +00:00
|
|
|
(map add-fns:readfile it)
|
2014-08-28 19:44:01 +00:00
|
|
|
(run 'main)
|
2014-10-05 17:44:14 +00:00
|
|
|
(if ($.current-charterm) ($.close-charterm))
|
2014-07-06 07:07:03 +00:00
|
|
|
(prn memory*))
|