Add functions for handling bools during code generation

This commit is contained in:
John Lorentzson 2025-07-03 16:54:22 +02:00
parent f54e064fda
commit d88107a209

View file

@ -102,6 +102,22 @@
(emit-sta :address (data-reference data)) (emit-sta :address (data-reference data))
(setf *last-instruction* (list :store data))))) (setf *last-instruction* (list :store data)))))
(defun emit-store-bool (data)
"Stores the inverse of the zeroflag to DATA. Inverse so that non-0 is TRUE."
;; The "DATA is stored"-case
(if (or (null (allocation-details data))
(member (strategy (allocation-details data))
'(:constant :accumulator)))
(setf *last-instruction* '(:useless))
(progn
(emit-asm-instruction :opcode :php :byte-length 1)
(emit-asm-instruction :opcode :pla :byte-length 1)
(emit-asm-instruction :opcode :and :operand #b00000010 :byte-length 2)
(emit-asm-instruction :opcode :lsr-a :byte-length 1)
(emit-asm-instruction :opcode :not :operand #b00000001 :byte-length 2)
(emit-sta :address (data-reference data))
(setf *last-instruction* '(:store-zero-flag data)))))
(defun emit-load-data (data) (defun emit-load-data (data)
(if (or (member (strategy (allocation-details data)) (if (or (member (strategy (allocation-details data))
'(:accumulator :direct-to-argvec)) '(:accumulator :direct-to-argvec))
@ -114,6 +130,24 @@
(emit-lda :address (data-reference data))) (emit-lda :address (data-reference data)))
(setf *last-instruction* (list :load data))))) (setf *last-instruction* (list :load data)))))
(defun emit-load-bool (data)
(if (or (member (strategy (allocation-details data))
'(:accumulator))
(equal *last-instruction* (list :store-zero-flag data))
(equal *last-instruction* (list :load-zero-flag data)))
(setf *last-instruction* '(:useless))
(progn
(if (eql (strategy (allocation-details data)) :constant)
(progn
(emit-lda :immediate (ir-constant-value data))
(emit-asm-instruction :opcode :and :operand 1 :byte-length 1)
(emit-cmp :immediate 1))
(progn
(emit-lda :address (data-reference data))
(emit-asm-instruction :opcode :and :operand 1 :byte-length 1)
(emit-cmp :immediate 1)))
(setf *last-instruction* (list :load-zero-flag data)))))
(defmethod compile-ir ((inst ir-inst)) (defmethod compile-ir ((inst ir-inst))
(warn "Skipped compiling ~A; no COMPILE-IR method" inst)) (warn "Skipped compiling ~A; no COMPILE-IR method" inst))