Conversation
Notices
-
Embed this notice
ロミンちゃん (romin@shitposter.club)'s status on Wednesday, 11-Oct-2023 06:50:23 JST ロミンちゃん @iska @hayley @moonchild @lispi314 the best code is written at 3am. -
Embed this notice
Iska (iska@catposter.club)'s status on Wednesday, 11-Oct-2023 06:50:25 JST Iska @hayley@social.applied-langua.ge @moonchild@social.applied-langua.ge @lispi314@mastodon.top
== was a typo, it's supposed to be (= ,i1 ,i2)
defj and goto have no second operand, you'll have to add some garbage there because my parser assumes a fixed layout of opcode + 2 operands. Maybe I can add a tag there?
next step is to let the vm generate code at runtime. If possible, I want to make it a tracing jit somehow.
but that's all for tomorrow, it's 2 AM now. -
Embed this notice
Hayley (hayley@social.applied-langua.ge)'s status on Wednesday, 11-Oct-2023 06:50:26 JST Hayley @iska @moonchild @lispi314 Reminds me of the Netfarm VM, which is also compiled together https://gitlab.com/cal-coop/netfarm/netfarm/-/blob/master/Code/Scripts/Script-machine/dispatch-function.lisp?ref_type=heads
Dunno what == and the second argument to GO are though. -
Embed this notice
Iska (iska@catposter.club)'s status on Wednesday, 11-Oct-2023 06:50:28 JST Iska cc @hayley@social.applied-langua.ge @moonchild@social.applied-langua.ge @lispi314@mastodon.top
just a cool proof of concept/excercise. (still better than uxn)In conversation permalink -
Embed this notice
Iska (iska@catposter.club)'s status on Wednesday, 11-Oct-2023 06:50:30 JST Iska I've just made my simple vm compiler, under 70 lines of Common Lisp.
Not even putting it into a file upload, here's the entire thing:(defvar *asm-alist* '()) (defmacro define-instruction (name &body form) `(progn (push (cons ',name ',name) *asm-alist*) (defmacro ,name (i1 i2) ,@form))) (defmacro define-easy-instruction (name function) ;;FIXME: doesn't work yet (list 'define-instruction name (list 'setf '(unquote i1) `(,function '(unquote i1) '(unquote i2))))) (defun make-instruction-list (string) (let ((seqnum 0)) (loop until (>= seqnum (length string)) collect (loop for n from 1 to 3 collect (let ((a (multiple-value-list (read-from-string (subseq string seqnum))))) (incf seqnum (cadr a)) (car a)))))) (defun compile-instructions (instruction-list) (let ((instructions (loop for i in instruction-list collect (list (cdr (assoc (car i) *asm-alist*)) (cadr i) (caddr i))))) (eval (list 'lambda '(mem) (append `(let ((r0 0) (r1 0) (r2 0) (r3 0) (r4 0) (r5 0) (r6 0) (r7 0) (sp 0) (cr 0))) (list (append '(tagbody (go main) main) instructions))))))) (defun real-call (reg call mem) (declare (ignore mem)) ;;for now (case call (0 (setf reg (read-byte *standard-input*))) (1 (write-byte reg *standard-output*)) (2 (print reg)))) ;;FIXME commented until define-easy-instruction is fixed. ;(define-easy-instruction +imm +) ;(define-easy-instruction +reg +) ;(define-easy-instruction -imm -) ;(define-easy-instruction -reg -) ;(define-easy-instruction *reg *) ;(define-easy-instruction /reg /) ;(define-easy-instruction bxor logxor) ;(define-easy-instruction band logand) ;(define-easy-instruction b1or logor) ;(define-easy-instruction bnot lognot) (define-instruction +imm `(incf ,i1 ,i2)) (define-instruction +reg `(incf ,i1 ,i2)) (define-instruction -imm `(decf ,i1 ,i2)) (define-instruction -reg `(decf ,i1 ,i2)) (define-instruction *reg `(setf ,i1 (* ,i1 ,i2))) (define-instruction /reg `(setf ,i1 (/ ,i1 ,i2))) (define-instruction bxor `(setf ,i1 (logxor ,i1 ,i2))) (define-instruction band `(setf ,i1 (logand ,i1 ,i2))) (define-instruction b1or `(setf ,i1 (logor ,i1 ,i2))) (define-instruction bnot `(setf ,i1 (lognot ,i1 ,i2))) (define-instruction rcop `(setf ,i1 ,i2)) (define-instruction ladr `(setf ,i1 (aref mem ,i2))) (define-instruction stre `(setf (aref mem ,i2) ,i1)) (define-instruction call `(real-call ,i1 ,i2 mem)) (define-instruction cmpr `(setf cr (cond (((> ,i1 ,i2) 1) ((< ,i1 ,i2) 2) ((== ,i1 ,i2) 3))))) (define-instruction defj i1) (define-instruction goto `(go ,i1)) (define-instruction breq `(when (= ,i1 0) (go ,i2 'meow))) (define-instruction bneq `(unless (= ,i1 0) (go ,i2 'meow)))In conversation permalink
-
Embed this notice