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