Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d5bcd5d12 |
@ -7,6 +7,7 @@
|
|||||||
(require :host.assembler.opcodes))
|
(require :host.assembler.opcodes))
|
||||||
(local {: any : slice : push : map :match find-pred} (require :deps.lume))
|
(local {: any : slice : push : map :match find-pred} (require :deps.lume))
|
||||||
(local {: word->byte : hex} (require :host.util))
|
(local {: word->byte : hex} (require :host.util))
|
||||||
|
(local f (require :fennel))
|
||||||
|
|
||||||
(fn label [name] {:label name})
|
(fn label [name] {:label name})
|
||||||
(fn label? [value]
|
(fn label? [value]
|
||||||
@ -24,17 +25,28 @@
|
|||||||
form)
|
form)
|
||||||
|
|
||||||
(fn assemble [forms offset]
|
(fn assemble [forms offset]
|
||||||
(let [state {:labels {} :instructions [] :reverse-labels []}]
|
(let [state {:labels {} :instructions [] :reverse-labels []}]
|
||||||
(each [_ form (ipairs forms)]
|
(each [_ form (ipairs forms)]
|
||||||
(if (= (type form) :string)
|
(if (= (type form) :string)
|
||||||
(insert-label state form (+ offset (* 4 (length (. state :instructions)))))
|
(insert-label
|
||||||
(push (. state :instructions)
|
state
|
||||||
(if (any (slice form 2) label?)
|
form
|
||||||
(let [{: type :offset addend} (. relocatable-instructions (. form 1))]
|
(+ offset (* 4 (length (. state :instructions)))))
|
||||||
(push (. state :reverse-labels) {: type :label (. (find-pred form label?) :label) :offset (+ offset (* 4 (length (. state :instructions))))})
|
(push (. state :instructions)
|
||||||
((. form 1) (table.unpack (replace-label! (slice form 2) addend))))
|
(if (any (slice form 2) label?)
|
||||||
((. form 1) (table.unpack (slice form 2)))))))
|
(let [opcode (. form 1)
|
||||||
state))
|
args (slice form 2)
|
||||||
|
{: type :offset addend}
|
||||||
|
(. relocatable-instructions opcode)]
|
||||||
|
(push
|
||||||
|
(. state :reverse-labels)
|
||||||
|
{: type
|
||||||
|
:label (. (find-pred form label?) :label)
|
||||||
|
:offset (+ offset (* 4 (length (. state :instructions))))})
|
||||||
|
(opcode
|
||||||
|
(table.unpack (replace-label! args addend))))
|
||||||
|
((. form 1) (table.unpack (slice form 2)))))))
|
||||||
|
state))
|
||||||
|
|
||||||
(fn link [{: labels : instructions : reverse-labels} base]
|
(fn link [{: labels : instructions : reverse-labels} base]
|
||||||
(each [_ {: type :label target : offset} (ipairs reverse-labels)]
|
(each [_ {: type :label target : offset} (ipairs reverse-labels)]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user