Add COMPILE-IR methods for remaining arithmetic tests

This commit is contained in:
John Lorentzson 2025-07-06 22:37:37 +02:00
parent ccd06bc8cb
commit 92f2b02675

View file

@ -261,15 +261,20 @@ is the responsibility of the pre-assembly compilation step."
:byte-length 3))))
(setf *last-instruction* :branch)))
(defun emit-branch-test-code (inputs output branch-opcode)
(defun emit-branch-test-code (inputs output branch-opcodes)
(let ((branchp (eql (strategy (allocation-details output)) :branch)))
(emit-load-data (first inputs))
(unless branchp ; If we're *NOT* branching, we're storing a test result.
;; LDX #1. This value will go into A if the test succeeds.
(emit-asm-instruction :opcode #xA2 :operand 1 :byte-length 2))
(emit-cmp (data-reference (second inputs)))
;; The actual branch instruction for our test.
(emit-asm-instruction :opcode branch-opcode :operand 3 :byte-length 2)
;; The actual branch instructions for our test.
(loop :for (opcode . offset) :in branch-opcodes
:do (emit-asm-instruction :opcode opcode
:operand (if (null offset)
3
offset)
:byte-length 2))
(unless branchp
;; In the event of no branch -- we're storing the result -- we skip over
;; an LDX #0 instruction if it succeeded, run if if the test failed.
@ -284,7 +289,32 @@ is the responsibility of the pre-assembly compilation step."
(emit-store-data output)))
(defmethod compile-ir ((inst ir-test-equal))
(emit-branch-test-code (inputs inst) (output inst) #xF0))
;; BEQ to true path
(emit-branch-test-code (inputs inst) (output inst) '((#xF0))))
(defmethod compile-ir ((inst ir-test-not-equal))
;; BNE to true path
(emit-branch-test-code (inputs inst) (output inst) '((#xD0))))
(defmethod compile-ir ((inst ir-test-less))
;; BCC to true path
(emit-branch-test-code (inputs inst) (output inst) '((#x90))))
(defmethod compile-ir ((inst ir-test-less-or-equal))
;; BCC, BEQ, both to true path
(emit-branch-test-code (inputs inst) (output inst)
'((#x90 . 5)
(#xF0 . 3))))
(defmethod compile-ir ((inst ir-test-greater))
;; BEQ to false path, BCS to true
(emit-branch-test-code (inputs inst) (output inst)
'((#xF0 . 2)
(#xB0 . 3))))
(defmethod compile-ir ((inst ir-test-greater-or-equal))
;; BCS to true path
(emit-branch-test-code (inputs inst) (output inst) '((#xB0))))
(defmacro do-asm-objects ((asm-obj start-asm-obj) &body body)
`(loop :for ,asm-obj := ,start-asm-obj :then (next ,asm-obj)