From 92f2b0267557b130f4c0c104c385574be1d62077 Mon Sep 17 00:00:00 2001 From: John Lorentzson Date: Sun, 6 Jul 2025 22:37:37 +0200 Subject: [PATCH] Add COMPILE-IR methods for remaining arithmetic tests --- .../backend/code-generator.lisp | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/wip-duuqnd/user-side-compiler/backend/code-generator.lisp b/wip-duuqnd/user-side-compiler/backend/code-generator.lisp index 03eff74..e5b6d79 100644 --- a/wip-duuqnd/user-side-compiler/backend/code-generator.lisp +++ b/wip-duuqnd/user-side-compiler/backend/code-generator.lisp @@ -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)