summaryrefslogtreecommitdiff
path: root/src/util.lisp
diff options
context:
space:
mode:
authorSiddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com>2025-04-12 13:06:51 -0400
committerGitHub <noreply@github.com>2025-04-12 13:06:51 -0400
commitfc20e7e7276b712f1e8db773b9215f900e877169 (patch)
treecaecdd1499d2e391cd5bd2dcde3aebfade002a09 /src/util.lisp
parent5dbf0b63988b42c112ca0087cbbbb090566df5c1 (diff)
parent639098b1ea82be82bd18a4af415458fcbaf5e20b (diff)
Merge pull request #8 from bdunahu/bdunahu
Add write raw bytes stage
Diffstat (limited to 'src/util.lisp')
-rw-r--r--src/util.lisp15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/util.lisp b/src/util.lisp
index f3035fe..e52d7aa 100644
--- a/src/util.lisp
+++ b/src/util.lisp
@@ -4,6 +4,9 @@
"Returns t if FILE is extended with .asm, nil otherwise."
(string= (pathname-type file) "asm"))
+(defun generate-file-name (file)
+ "Given a .asm file, generates an identically named .rv file."
+ (subseq file 0 (- (length file) 4)))
(defun format-as-binary (num len)
"Formats NUM as a binary number, and pads to LEN with zeros."
@@ -12,7 +15,15 @@
(let ((max-val (1- (expt 2 len))))
(format nil "~V,'0b" len (logand num max-val))))
+(defun word-to-bytes (int)
+ "Given a 32-bit integer, outputs a list of 4 bytes."
+ (list (logand #xff (ash int -24))
+ (logand #xff (ash int -16))
+ (logand #xff (ash int -8))
+ (logand #xff int)))
+
(defun insert-in-middle (list element)
+ "Inserts ELEMENT in the second slot of LIST."
(append (list (car list)) (list element) (cdr list)))
(defun iota (n)
@@ -48,7 +59,7 @@ of the elements from both lists. Returns nil if the lists are not equal size."
(alexandria:if-let ((value (gethash name variable-table)))
value
(progn (maphash #'(lambda (k v) (format t "~A => ~A~%" k v)) variable-table)
- (error "~@<Variable ~S not declared.~@:>" name))))
+ (error "~@<Variable ~S not declared.~@:>" name))))
(defun get-label (name)
(alexandria:if-let ((value (gethash name label-table)))
@@ -65,5 +76,5 @@ of the elements from both lists. Returns nil if the lists are not equal size."
"I-type instructions.")
(defvar j-type
- '("JMP" "JRL" "JAL" "BEQ" "BGT" "BUF" "BOF" "PUSH" "POP")
+ '("JMP" "JRL" "JAL" "BEQ" "BGT" "BUF" "BOF" "PUSH" "POP" "RET" "NOP")
"J-type instructions.")