new-s
(binformat d new-s))))
+(defun ez-list-to-string-hex (l)
+ (concat "("
+ (when l
+ (concat
+ (format "%x" (car l))
+ (apply 'concat (mapcar (lambda (n) (format " %x" n)) (cdr l)))))
+ ")"))
+
;; Memory
(defvar ez-memory nil
(defun ez-get-global-var (gvar)
(if (> gvar 239)
(error "Invalid global variable %d" gvar))
- (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr)))
+ (let ((val
+ (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr))))
+ (message "\tRetrieved %x from global variable %x" val gvar)
+ val))
(defun ez-set-global-var (gvar val)
- (ez-mem-set-word (+ (* 2 gvar) ez-globalvartab-addr) val))
+ (ez-mem-set-word (+ (* 2 gvar) ez-globalvartab-addr) val)
+ (message "\tSet global variabl %x to %x" gvar val))
;; Object tree
(defun ez-routine-stack-push (val)
(let ((frame (car ez-call-stack)))
+ (message "\tPushed %x to stack" val)
(setf (elt frame 1) (cons val (elt frame 1)))))
(defun ez-routine-stack-pop ()
(let* ((frame (car ez-call-stack))
(rs-head (car (elt frame 1))))
(setf (elt frame 1) (cdr (elt frame 1)))
+ (message "\tPopped %x from stack" rs-head)
rs-head))
(defun ez-get-local-var (lvar)
- (let ((frame (car ez-call-stack)))
- (aref (elt frame 2) (- lvar 1))))
+ (let* ((frame (car ez-call-stack))
+ (val (aref (elt frame 2) (- lvar 1))))
+ (message "\tRetrieved value %x from local variable %x" val lvar)
+ val))
(defun ez-set-local-var (lvar val)
(let ((frame (car ez-call-stack)))
+ (message "\tSet local variable %x to %x" lvar val)
(aset (elt frame 2) (- lvar 1) val)))
(defun ez-get-pc ()
(let ((table-row (assoc (list optype opcode) ez-op-table)))
(unless table-row
(error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
- instr-pc optype opcode operands operand-types))
+ instr-pc optype opcode (ez-list-to-string-hex operands) operand-types))
(let ((mnemonic (elt table-row 1)))
(message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
- instr-pc optype opcode mnemonic operands operand-types))
+ instr-pc optype opcode mnemonic
+ (ez-list-to-string-hex operands) operand-types))
(funcall (elt table-row 2) operands operand-types))))
(defun ez-read-var-operands-and-inc ()
(if (= type1 #b11)
nil
(cons type1
- (let ((type2 (lsh (logand #b110000 type-byte) -4)))
+ (let ((type2 (mod (lsh type-byte -4) 4)))
(if (= type2 #b11)
nil
(cons type2
- (let ((type3 (lsh (logand #b1100 type-byte) -2)))
+ (let ((type3 (mod (lsh type-byte -2) 4)))
(if (= type3 #b11)
nil
(cons type3
- (let ((type4 (logand #b11)))
+ (let ((type4 (mod type-byte 4)))
(if (= type4 #b11)
nil
(list type4))))))))))))))
((1op #x05) inc ez-op-inc)
((1op #x06) dec ez-op-dec)
((1op #x0B) ret ez-op-ret)
+ ((0op #x08) ret_pulled ez-op-ret-pulled)
((1op #x0C) jump ez-op-jump)
((2op #x05) inc_jg ez-op-inc-jg)
((2op #x04) dec_jg ez-op-dec-jg)
((2op #x10) loadb ez-op-loadb)
((2op #x01) je ez-op-je)
((2op #x02) jl ez-op-jl)
+ ((2op #x03) jg ez-op-jg)
((2op #x06) jin ez-op-jin)
+ ((2op #x07) test ez-op-test)
((2op #x08) or ez-op-or)
((2op #x09) and ez-op-and)
((2op #x14) add ez-op-add)
((2op #x16) mul ez-op-mul)
((2op #x17) div ez-op-div)
((2op #x18) mod ez-op-mod)
- ((var #x00) call_fv ez-op-callf)
+ ((var #x00) call_fv ez-op-callfv)
((1op #x01) get_sibling ez-op-get-sibling)
((1op #x02) get_child ez-op-get-child)
((1op #x03) get_parent ez-op-get-parent)
(defun ez-op-ret (operands &optional operand-types)
(let ((retval (car operands)))
+ (message "\tReturning value %x" retval)
(ez-pop-call-stack-frame)
(ez-set-var (ez-read-pc-byte-and-inc) retval))
t)
+(defun ez-op-ret-pulled (operands operand-types)
+ (let ((retval (ez-routine-stack-pop)))
+ (message "\tReturning value %x" retval)
+ (ez-op-ret (list retval)))
+ t)
+
(defun ez-op-rtrue (&optional operands operand-types)
(ez-op-ret (list 1))
t)
(= n (ez-get-obj-parent obj)))))
t)
+(defun ez-op-test (operands operand-types)
+ (let ((a (car operands))
+ (b (cadr operands)))
+ (ez-do-branch (= (logand a b) b)))
+ t)
+
(defun ez-op-jump (operands operand-types)
(let ((offset (if (eq (car operand-types) 'b)
(ez-decode-signed-byte (car operands))
(defun ez-op-mod (a b)
(error "Not implemented"))
-(defun ez-op-callf (operands operand-types)
+(defun ez-op-callfv (operands operand-types)
(let* ((raddr (car operands))
(call-operands (cdr operands))
(r (* 2 raddr))
(L (ez-mem-ref-byte r))
(n (length call-operands))
(new-pc (+ r 1 (* L 2))))
- (ez-add-call-stack-frame new-pc)
- (dotimes (i L)
- (if (< i n)
- (ez-set-local-var (+ i 1) (elt call-operands i))
- (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
+ (if (= raddr 0)
+ (ez-set-var (ez-read-pc-byte-and-inc) 0) ; Simply return 0
+ (ez-add-call-stack-frame new-pc)
+ (dotimes (i L)
+ (if (< i n)
+ (ez-set-local-var (+ i 1) (elt call-operands i))
+ (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i))))))))
t)
(defun ez-op-test-attr (operands operand-types)
;;; Buffer and I/O
;;
-(defun ez-render-prompt ()
- (with-current-buffer "*ez*"
- (let ((update-point (= ez-input-marker (point)))
- (update-window-points (mapcar (lambda (w)
- (list (= (window-point w) ez-input-marker)
- w))
- (get-buffer-window-list nil nil t))))
- (save-excursion
- (set-marker-insertion-type ez-prompt-marker nil)
- (set-marker-insertion-type ez-input-marker t)
- (let ((inhibit-read-only t))
- (delete-region ez-prompt-marker ez-input-marker)
- (goto-char ez-prompt-marker)
- (insert
- ">"
- (propertize " " ; Need this to be separate to mark it as rear-nonsticky
- 'read-only t
- 'rear-nonsticky t)))
- (set-marker-insertion-type ez-input-marker nil))
- (goto-char ez-input-marker))))
-
-(defvar ez-prompt-marker nil
- "Marker for prompt position in buffer.")
-
(defvar ez-input-marker nil
- "Marker for prompt position in buffer.")
+ "Marker for input position in buffer.")
(defun ez-setup-buffer ()
(with-current-buffer (get-buffer-create "*ez*")
(let ((inhibit-read-only t))
(delete-region (point-min) (point-max)))
(setq-local scroll-conservatively 1)
- (if (markerp ez-prompt-marker)
- (set-marker ez-prompt-marker (point-max))
- (setq ez-prompt-marker (point-max-marker)))
(if (markerp ez-input-marker)
(set-marker ez-input-marker (point-max))
(setq ez-input-marker (point-max-marker)))
- (goto-char (point-max))
- (ez-render-prompt)))
+ (goto-char (point-max))))
(defun ez-print (string)
(with-current-buffer "*ez*"
(save-excursion
- (goto-char ez-prompt-marker)
+ (goto-char ez-input-marker)
(insert-before-markers string))))
;; Mode