From 025c958e2f8b1499f23a5ae1c1be23452572b326 Mon Sep 17 00:00:00 2001 From: mRnea Date: Wed, 24 Jul 2024 12:09:26 +0300 Subject: branching (if else) added, as interpretation and compilation --- assembly.lisp | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'assembly.lisp') diff --git a/assembly.lisp b/assembly.lisp index 3e80686..bdee15c 100644 --- a/assembly.lisp +++ b/assembly.lisp @@ -9,14 +9,17 @@ ((listp el) `(format nil ,@el)))) lst)))) -(defmacro defop (op-name args &body asm-strings) +(defmacro defop (op-name (&key (indent 4) args) &body asm-strings) `(setf (gethash ',op-name *operations*) (lambda (out-stream ,@args) - (format out-stream "~{ ~a~%~}" + (format out-stream + ,(format nil "~~{~a~~a~~%~~}" + (make-string indent :initial-element + #\Space)) ,(normalize-op-list asm-strings))))) -(defop push (a) +(defop push (:args (a)) ("push ~d" a)) (defop + () @@ -35,6 +38,38 @@ "pop rdi" "call dump") +(defop = () + "mov rcx, 0" + "mov rdx, 1" + "pop rax" + "pop rbx" + "cmp rax, rbx" + "cmove rcx, rdx" + "push rcx") + +(defop exit (:args (exit-code)) + "mov rax, 60" + ("mov rdi, ~a" exit-code) + "syscall") + +(defop ise (:args (label-num)) + "pop rax" + "test rax, rax" + ("jz et_~a" label-num)) + +(defop yoksa (:args (yap-num ise-num) :indent 0) + (" jmp et_~a" yap-num) + ("et_~a:" ise-num)) + +(defop yap (:args (label-num) :indent 0) + ("et_~a:" label-num)) + +(defun gen-code (op str) + (let ((op-fn (gethash (car op) *operations*))) + (if (null op-fn) + (error "~s is not a valid op" op) + (apply op-fn str (cdr op))))) + (defun gen-dump (str) (format str "~{~a~%~}" '("dump:" -- cgit v1.2.3