summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assembly.lisp24
-rw-r--r--cl-forth.lisp21
2 files changed, 36 insertions, 9 deletions
diff --git a/assembly.lisp b/assembly.lisp
index 1522643..e2b62f1 100644
--- a/assembly.lisp
+++ b/assembly.lisp
@@ -116,6 +116,30 @@
"cmovg rcx, rdx"
"push rcx")
+(defop bel ()
+ "push bel")
+
+(defop oku ()
+ "pop rax"
+ "xor rbx, rbx"
+ "mov bl, [rax]"
+ "push rbx")
+
+(defop yaz ()
+ "pop rbx"
+ "pop rax"
+ "mov [rax], bl")
+
+(defop (syscall num) ()
+ (iter (with call-regs = #("rdi" "rsi" "rdx" "r10" "r8" "r9"))
+ (initially (:write "pop rax"))
+ (for i from (- num 1) downto 0)
+ (:write ("pop ~a" (aref call-regs i)))
+ (finally (:write "syscall"))))
+
+(defun gen-header (op str)
+ (format str " ;; -- ~s --~%" op))
+
(defun gen-code (op str)
(let ((op-fn (gethash (car op) *operations*)))
(if (null op-fn)
diff --git a/cl-forth.lisp b/cl-forth.lisp
index a87d0eb..9d66532 100644
--- a/cl-forth.lisp
+++ b/cl-forth.lisp
@@ -2,7 +2,9 @@
(eval-always
(defparameter *identifiers*
- '(+ - dump = ise yoksa yap eş push değiş üst rot düş döngü iken < >))
+ '(+ - dump = ise yoksa yap eş push değiş üst rot düş döngü iken < >
+ syscall-1 syscall-2 syscall-3 syscall-4 syscall-5 syscall-6
+ bel oku yaz))
(defun is-identifier (sym)
(find sym *identifiers*)))
@@ -101,6 +103,9 @@
(assert (eq 'döngü (car top)))
(push (list 'iken i (cadr top)) stack)
(vector-push-extend (list 'iken nil) ops)))
+ ((search "syscall" (string-downcase (string op)))
+ (let ((syscall-num (parse-integer (subseq (string op) 8))))
+ (vector-push-extend (list 'syscall syscall-num) ops)))
(t (vector-push-extend (list op) ops))))
(finally (return ops))))
@@ -192,13 +197,8 @@
;;; COMPILER
-(defun gen-header (op str)
- (format str " ;; -- ~s --~%" op))
-
-;; (defun not-implemented (str)
-;; (format str " ;; -- TODO: not implemented --~%"))
-
-(defun generate-program (program &key (path "output.asm") (compile nil))
+(defun generate-program (program &key (path "output.asm") (compile nil)
+ (mem-cap 640000))
(with-open-file (out path :direction :output
:if-exists :supersede)
(format out "~a~%" "segment .text")
@@ -208,7 +208,10 @@
(iter (for op in-sequence program)
(gen-header op out)
(gen-code op out))
- (gen-code '(exit 0) out))
+ (gen-header '(exit 0) out)
+ (gen-code '(exit 0) out)
+ (format out "~a~%" "segment .bss")
+ (format out "~a ~a~%" "bel: resb" mem-cap))
(when compile
(run `("nasm" "-felf64" ,path))
(let ((name (first (uiop:split-string path :separator '(#\.)))))