diff options
Diffstat (limited to 'src/util.lisp')
-rw-r--r-- | src/util.lisp | 71 |
1 files changed, 58 insertions, 13 deletions
diff --git a/src/util.lisp b/src/util.lisp index 93db659..d8bd227 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -4,7 +4,10 @@ "Returns t if FILE is extended with .asm, nil otherwise." (string= (pathname-type file) "asm")) -;; TODO this won't work for negative numbers of odd sizes quite yet. +(defun fits-in-X-bits (n) + "Returns the number of bits required to represent N" + (ceiling (/ (log (ceiling n (log 2))) (log 2)))) + (defun format-as-binary (num len) "Formats NUM as a binary number, and pads to LEN with zeros." (declare (type number num)) @@ -18,26 +21,68 @@ "Generates a number sequence from 0 to N." (when (> n 0) (do ((i 0 (1+ i)) - (item 0 (1+ item)) - (result nil (push item result))) - ((= i n) (nreverse result))))) + (item 0 (1+ item)) + (result nil (push item result))) + ((= i n) (nreverse result))))) (defun riffle (lst1 lst2) - "Given LST1 and LST2, returns a new list which is the an alternative sequence + "Given LST1 and LST2, returns a new list which is the an alternating sequence of the elements from both lists. Returns nil if the lists are not equal size." (when (eq (length lst1) (length lst2)) (loop for l1 in lst1 - for l2 in lst2 - append (list l1 l2)))) + for l2 in lst2 + append (list l1 l2)))) + +(defvar variable-table (make-hash-table :test #'equal)) +(defvar label-table (make-hash-table :test #'equal)) + +(defun add-variable (name value) + (if (gethash name variable-table) + (error "~@<Variable declared twice: ~S.~@:>" name) + (setf (gethash name variable-table) value))) + +(defun add-label (name value) + (if (gethash name label-table) + (error "~@<Label declared twice: ~S.~@:>" name) + (setf (gethash name label-table) value))) -(defparameter type-r - '(ADD SUB MUL QUOT REM SFTR SFTL AND OR NOT XOR ADDV SUBV MULV DIVV CMP CEV) +(defun get-variable (name) + (alexandria:if-let ((value (gethash name variable-table))) + value + (error "~@<Variable ~S not declared.~@:>" name))) + +(defun get-label (name) + (alexandria:if-let ((value (gethash name label-table))) + value + (error "~@<Label ~S not found.~@:>" name))) + +(defmacro generate-type-map (ops) + "Generates an alist where the key corresponds to an element in +OPS, while the value is the index of that key (padded to the minimum +number of bits required to represent all +concatenated with TYPE." + `(let ((i 0) + (opsize (fits-in-X-bits (length ,ops)))) + (mapcar (lambda (x) + (incf i) + (cons x (format-as-binary i opsize))) + ,ops))) + +(defvar r-type + '("ADD" "SUB" "MUL" "QUOT" "REM" "SFTR" "SFTL" "AND" "OR" + "NOT" "XOR" "ADDV" "SUBV" "MULV" "DIVV" "CMP" "CEV") "R-type instructions.") -(defparameter type-i - '(LOAD LOADV ADDI SUBI SFTRI SFTLI ANDI ORI XORI STORE STOREV MOV) +(defvar i-type + '("LOAD" "LOADV" "ADDI" "SUBI" "SFTRI" "SFTLI" "ANDI" "ORI" "XORI" "STORE" "STOREV") "I-type instructions.") -(defparameter type-j - '(JMP JRL JAL BEQ BGT BUF BOF PUSH POP) +(defvar j-type + '("JMP" "JRL" "JAL" "BEQ" "BGT" "BUF" "BOF" "PUSH" "POP") "J-type instructions.") + +(defvar mnemonic-loc + `(,@(generate-type-map r-type) + ,@(generate-type-map i-type) + ,@(generate-type-map j-type)) + "An alist mapping known mnemonics to their binary representation.") |