From: plugd Date: Fri, 12 Nov 2021 15:14:16 +0000 (+0100) Subject: Fixed bug in branch handling. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=d6715a8988d1e6907c26be18fc866fe03a92a252;p=ez.git Fixed bug in branch handling. --- diff --git a/ez.el b/ez.el index 4ec4346..d33f857 100644 --- a/ez.el +++ b/ez.el @@ -175,7 +175,7 @@ (defun ez-get-obj-prop (obj prop) (let* ((prop-addr (ez-get-obj-prop-addr obj prop)) (size-byte (ez-mem-ref-byte prop-addr))) - (if (= prop-addr 0) + (if (= size-byte 0) (ez-get-prop-default prop) (let ((prop-len (ez-get-prop-len size-byte)) (data-addr (+ prop-addr 1))) @@ -380,7 +380,7 @@ (setq optype '0op opcode (- opbyte #xb0))) ((<= #xC0 opbyte #xDF) - (error "Unsupported op" opbyte)) + (error "Unsupported op %x" opbyte)) ((<= #xE0 opbyte #xFF) (setq optype 'var opcode (- opbyte #xe0)) @@ -389,7 +389,8 @@ operand-types (cdr operands-and-types))))) (let ((table-row (assoc (list optype opcode) ez-op-table))) (unless table-row - (error "Unsupported op" instr-pc optype opcode operands operand-types)) + (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s" + instr-pc optype opcode operands operand-types)) (let ((mnemonic (elt table-row 1))) (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s" instr-pc optype opcode mnemonic operands)) @@ -434,7 +435,7 @@ (defun ez-do-branch (branch) (let* ((branch-byte (ez-read-pc-byte-and-inc)) (invert (= 0 (logand branch-byte #b10000000))) - (single-byte (= 1 (logand branch-byte #b01000000))) + (single-byte (> (logand branch-byte #b01000000) 0)) (offset (if single-byte (logand branch-byte #b00111111) @@ -444,13 +445,15 @@ (if pos val (- val 8192)))))) - (cond - ((= offset 0) - (ez-op-rfalse)) - ((= offset 1) - (ez-op-rtrue)) - (t - (ez-set-pc (+ (ez-get-pc) offset -2)))))) + (if (or (and branch (not invert)) + (and (not branch) invert)) + (cond + ((= offset 0) + (ez-op-rfalse)) + ((= offset 1) + (ez-op-rtrue)) + (t + (ez-set-pc (+ (ez-get-pc) offset -2))))))) ;; Operations @@ -475,6 +478,7 @@ ((1op #x01) get_sibling ez-op-get-sibling) ((1op #x02) get_child ez-op-get-child) ((1op #x03) get_parent ez-op-get-parent) + ((2op #x0A) test_attr ez-op-test-attr) ((var #x03) put_prop ez-op-put-prop))) (defun ez-op-ret (operands &optional operand-types) @@ -484,10 +488,12 @@ t) (defun ez-op-rtrue (&optional operands operand-types) - (ez-op-ret (list 1))) + (ez-op-ret (list 1)) + t) (defun ez-op-rfalse (&optional operands operand-types) - (ez-op-ret (list 0))) + (ez-op-ret (list 0)) + t) (defun ez-op-jz (operands operand-types) (ez-do-branch (= (car operands) 0)) @@ -572,26 +578,36 @@ (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i))))))) t) +(defun ez-op-test-attr (operands operand-types) + (let ((obj (car operands)) + (attr (cadr operands))) + (ez-do-branch (= 1 (ez-get-obj-attr obj attr))) + t)) + (defun ez-op-get-sibling (operands operand-types) (let ((sib (ez-get-obj-sibling (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) sib) - (ez-do-branch (> sib 0)))) + (ez-do-branch (> sib 0)) + t)) (defun ez-op-get-child (operands operand-types) (let ((child (ez-get-obj-child (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) child) - (ez-do-branch (> child 0)))) + (ez-do-branch (> child 0)) + t)) (defun ez-op-get-parent (operands operand-types) (let ((parent (ez-get-obj-parent (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) parent) - (ez-do-branch (> parent 0)))) + (ez-do-branch (> parent 0)) + t)) (defun ez-op-put-prop (operands operand-types) (let* ((obj (car operands)) (prop (cadr operands)) (a (caddr operands))) - (ez-set-obj-prop obj prop a))) + (ez-set-obj-prop obj prop a) + t)) ;; Main