about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-10-29 00:18:58 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-10-29 00:30:46 -0700
commit48cd1ae766dc9dc24785d2b19f963a70df869a01 (patch)
tree5436a0553f7ab7b32dc5a90dadb6391fcd215439
parentdae4f6d15ce0d5a22e4492a0f8540507c49f32a0 (diff)
downloadmu-48cd1ae766dc9dc24785d2b19f963a70df869a01.tar.gz
163 - start of a simple assembler
-rw-r--r--mu.arc34
-rw-r--r--mu.arc.t13
2 files changed, 45 insertions, 2 deletions
diff --git a/mu.arc b/mu.arc
index e4e22e6f..795a320a 100644
--- a/mu.arc
+++ b/mu.arc
@@ -117,8 +117,8 @@
     (= function*.name (convert-braces body))))
 
 ;; running mu
-(def v (operand)  ; for value
-  operand.0)
+(mac v (operand)  ; for value
+  `(,operand 0))
 
 (def metadata (operand)
   cdr.operand)
@@ -610,6 +610,36 @@
                   (set done))))))
     (- close pc 1)))
 
+;; convert symbolic names to integer offsets
+
+(def convert-names (instrs)
+  (let offset (table)
+    (let idx 1
+      (each instr instrs
+        (let (oargs op args)  (parse-instr instr)
+          (each arg args
+            (when (maybe-add arg offset idx)
+              (err "use before set: @arg")
+              (++ idx)))
+          (each arg oargs
+            (when (maybe-add arg offset idx)
+              (++ idx))))))
+    (each instr instrs
+      (let (oargs op args)  (parse-instr instr)
+        (each arg args
+          (when (offset v.arg)
+            (zap offset v.arg)))
+        (each arg oargs
+          (when (offset v.arg)
+            (zap offset v.arg)))))
+    instrs))
+
+(def maybe-add (arg offset idx)
+  (unless (or (in ty.arg 'literal 'offset)
+              (offset v.arg)
+              (~isa v.arg 'sym))
+    (= (offset v.arg) idx)))
+
 ;; system software
 
 (init-fn maybe-coerce
diff --git a/mu.arc.t b/mu.arc.t
index 8009ad28..63445058 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -1340,4 +1340,17 @@
 ; Eventually we want the right stack-management primitives to build delimited
 ; continuations in mu.
 
+; ---
+
+(reset)
+(new-trace "convert-names-local")
+(if (~iso (convert-names
+            '(((x integer) <- copy (4 literal))
+              ((y integer) <- copy (2 literal))
+              ((z integer) <- add (x integer) (y integer))))
+          '(((1 integer) <- copy (4 literal))
+            ((2 integer) <- copy (2 literal))
+            ((3 integer) <- add (1 integer) (2 integer))))
+  (prn "F - convert-names renames symbolic names to integer offsets"))
+
 (reset)  ; end file with this to persist the trace for the final test