feat: add assembler test file
This commit is contained in:
parent
99f6e167ea
commit
dcf9b5e89d
@ -1 +1,44 @@
|
|||||||
(local opcodes (require :host.assembler.opcodes))
|
(local
|
||||||
|
{: r
|
||||||
|
: move
|
||||||
|
: branch
|
||||||
|
: branch-link}
|
||||||
|
(require :host.assembler.opcodes))
|
||||||
|
(local {: any : slice : push} (require :deps.lume))
|
||||||
|
|
||||||
|
(fn label [name] {:label name})
|
||||||
|
(fn label? [value]
|
||||||
|
(if (and (= (type value) :table) (. value :label)) true false))
|
||||||
|
|
||||||
|
(fn insert-label [table label index]
|
||||||
|
(if (. table :label)
|
||||||
|
(:error (.. "Duplicate label " label))
|
||||||
|
(tset table :labels label index)))
|
||||||
|
|
||||||
|
(fn assemble [forms offset]
|
||||||
|
(let [state {:labels {} :instructions [] :reverse-labels []}]
|
||||||
|
(each [_ form (ipairs forms)]
|
||||||
|
(if (= (type form) :string)
|
||||||
|
(insert-label state form (+ offset (* 4 (length (. state :instructions)))))
|
||||||
|
(push (. state :instructions)
|
||||||
|
(if (any (slice form 2) label?)
|
||||||
|
(do
|
||||||
|
(push (. state :reverse-labels) (+ 1 (length (. state :instructions))))
|
||||||
|
form)
|
||||||
|
((. form 1) (table.unpack (slice form 2)))))))
|
||||||
|
state))
|
||||||
|
|
||||||
|
(fn link [{: labels : instructions : reverse-labels}]
|
||||||
|
())
|
||||||
|
|
||||||
|
(macro x [...]
|
||||||
|
`[,(unpack (icollect [_ form (ipairs ...)] '[,(. form 1)]))])
|
||||||
|
|
||||||
|
(macrodebug (x (move (r 9) (r 8))))
|
||||||
|
|
||||||
|
(assemble
|
||||||
|
[[move (r 8) (r 2)]
|
||||||
|
:label-1
|
||||||
|
[move (r 9) (r 3)]
|
||||||
|
[branch (label :label-1)]]
|
||||||
|
0x8000)
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
(local util (require :host.util))
|
(local util (require :host.util))
|
||||||
|
|
||||||
(macro construct-code [& forms]
|
(macro construct-opcode [& forms]
|
||||||
`(bor
|
`(bor ,(unpack
|
||||||
,(unpack
|
(icollect [i n (ipairs forms)]
|
||||||
(icollect [i n (ipairs forms)] `(lshift ,(. n 1) ,(. n 2))))))
|
`(lshift ,(. n 1) ,(. n 2))))))
|
||||||
|
|
||||||
(local conditions {:equal 0x0
|
(local conditions {:equal 0x0
|
||||||
:not-equal 0x1
|
:not-equal 0x1
|
||||||
|
|||||||
2
test/assembler/opcodes.fnl
Normal file
2
test/assembler/opcodes.fnl
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
(local t (require :deps.faith))
|
||||||
|
(local opcodes (require :host.assembler.opcodes))
|
||||||
Loading…
Reference in New Issue
Block a user