added memory manipulation and syscalls

This commit is contained in:
2024-07-27 16:42:24 +03:00
parent 2d94db0990
commit d1481f2770
2 changed files with 36 additions and 9 deletions

View File

@@ -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)

View File

@@ -2,7 +2,9 @@
(eval-always
(defparameter *identifiers*
'(+ - dump = ise yoksa yap push değiş üst rot düş döngü iken < >))
'(+ - dump = ise yoksa yap 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 '(#\.)))))