Pluggable instruction sets

This commit is contained in:
2026-02-10 10:58:48 -06:00
parent e4fe2c6bff
commit 4280841719
6 changed files with 226 additions and 188 deletions

View File

@@ -3,7 +3,7 @@
#:use-module (scmvm assembler)
#:use-module (srfi srfi-1)
#:use-module ((scheme base) #:select (write-bytevector))
#:export (assemble assemble-instructions))
#:export (assemble assemble-instructions!))
(define *aliases*
'((if . branch)))
@@ -11,10 +11,10 @@
(define (or-alias inst)
(or (assq-ref *aliases* inst) inst))
(define (lookup-instruction inst)
(define inst-obj (assq (or-alias inst) *instruction-set*))
(define (lookup-instruction isa inst)
(define inst-obj (instruction-lookup isa (or-alias inst)))
(or inst-obj
(error (format #f "could not find instruction ~a" inst))))
(error (format #f "could not find instruction ~a in ISA ~a" inst isa))))
(define label? (negate pair?))
(define (variable? x)
@@ -32,28 +32,29 @@
(emit-literal asm v)
(emit-reference asm v)))
(define (assemble-instructions asm inst-seq)
(define (assemble-instructions! asm inst-seq)
(when (pair? inst-seq)
(define next-inst (car inst-seq))
(cond
[(label? (car inst-seq))
(emit-label asm (car inst-seq))]
[(variable? (car inst-seq))
(emit-label asm (second (car inst-seq)))
(emit-literal asm (third (car inst-seq)))]
[(ref? (car inst-seq))
(emit-push asm (second (car inst-seq)))
[(label? next-inst)
(emit-label asm next-inst)]
[(variable? next-inst)
(emit-label asm (second next-inst))
(emit-literal asm (third next-inst))]
[(ref? next-inst)
(emit-push asm (second next-inst))
(emit-instruction asm '@)]
[(set!? (car inst-seq))
(emit-push asm (second (car inst-seq)))
[(set!? next-inst)
(emit-push asm (second next-inst))
(emit-instruction asm '!)]
[(push? (car inst-seq))
(emit-push asm (second (car inst-seq)))]
[(push? next-inst)
(emit-push asm (second next-inst))]
[else
(emit-instruction asm (instruction-name (lookup-instruction (first (car inst-seq)))))])
(assemble-instructions asm (cdr inst-seq))))
(emit-instruction asm (instruction-name (lookup-instruction (assembler-instruction-set asm) (first next-inst))))])
(assemble-instructions! asm (cdr inst-seq))))
(define (assemble instructions port)
(define asm (make-assembler))
(assemble-instructions asm instructions)
(finalize-references asm)
(define (assemble instructions instruction-set port)
(define asm (make-assembler instruction-set))
(assemble-instructions! asm instructions)
(assembler-backpatch! asm)
(assembler-dump-program asm port))