2020-10-12 15:48:14 +00:00
|
|
|
(local lume (require "lib.lume"))
|
2021-08-10 17:23:09 +00:00
|
|
|
(local {: int8-to-bytes : int16-to-bytes : int24-to-bytes : int32-to-bytes} (require "lib.util"))
|
2020-09-17 02:34:36 +00:00
|
|
|
|
2020-09-20 17:55:06 +00:00
|
|
|
(fn make-env [block parent]
|
|
|
|
{:parent parent
|
2021-07-31 01:57:38 +00:00
|
|
|
:prg (or parent.prg parent)
|
|
|
|
:root-block (or parent.root-block block)
|
2020-09-20 17:55:06 +00:00
|
|
|
:block block
|
2021-07-31 01:57:38 +00:00
|
|
|
:lookup-org
|
|
|
|
(fn [self name]
|
|
|
|
(if (or (. self.block.symbols name) (. self.block.globals name))
|
|
|
|
self.root-block.org
|
|
|
|
(self.parent:lookup-org name)))
|
2020-09-20 17:55:06 +00:00
|
|
|
:lookup-addr
|
2020-09-17 02:34:36 +00:00
|
|
|
(fn [self name]
|
2020-09-20 17:55:06 +00:00
|
|
|
(local ipdat (. self.block.symbols name))
|
2020-10-15 23:02:32 +00:00
|
|
|
(local ipdat-global (. self.block.globals name))
|
2020-09-27 18:53:16 +00:00
|
|
|
; (print "looking up" name "in" self)
|
2020-09-17 02:34:36 +00:00
|
|
|
(if
|
2021-01-10 19:52:05 +00:00
|
|
|
(and ipdat (= (type ipdat) :number) (> ipdat (length self.block.pdats)))
|
2020-09-20 17:55:06 +00:00
|
|
|
(+ self.block.addr self.block.size)
|
|
|
|
|
2021-01-10 19:52:05 +00:00
|
|
|
(and ipdat (= (type ipdat) :function)) (ipdat self)
|
|
|
|
|
2020-09-20 17:55:06 +00:00
|
|
|
ipdat (. self.block.pdats ipdat :addr)
|
|
|
|
|
2020-10-15 23:02:32 +00:00
|
|
|
ipdat-global (: (make-env (. self.block.pdats ipdat-global) block) :lookup-addr name)
|
|
|
|
|
2020-09-20 17:55:06 +00:00
|
|
|
(self.parent:lookup-addr name)))})
|
2020-09-17 02:34:36 +00:00
|
|
|
|
2021-07-31 01:57:38 +00:00
|
|
|
(fn program [prg-base ?processor]
|
|
|
|
(local {: opcodes : op-pdat : parse-mode-arg : addr-parser} (require (.. :asm. (or ?processor :6502))))
|
2020-10-06 03:47:25 +00:00
|
|
|
; dat - anything that takes up space in the assembled output (op, dw, db, etc)
|
|
|
|
; takes the form [:op args]
|
|
|
|
; pdat - a parsed dat; takes the form {:type type :addr addr ...}
|
|
|
|
(local dat-parser {})
|
2021-05-15 15:50:53 +00:00
|
|
|
(fn new-block [last-symbol] {:type :block :pdats [] :preserved {} :symbols {} :globals {} : last-symbol})
|
2020-10-06 03:47:25 +00:00
|
|
|
|
|
|
|
(fn parse-dats [block dats]
|
|
|
|
(each [_ dat (ipairs dats)]
|
|
|
|
(if (= (type dat) "string")
|
2021-04-18 02:50:09 +00:00
|
|
|
(do (set block.last-symbol dat)
|
|
|
|
(tset block.symbols dat (+ (length block.pdats) 1))
|
2021-01-17 02:40:04 +00:00
|
|
|
(when (= (dat:sub 1 2) "G-")
|
|
|
|
(tset block.globals dat true)))
|
|
|
|
|
|
|
|
(not= (type dat) :table)
|
|
|
|
(error (.. "Invalid operation " dat))
|
2021-01-24 15:14:32 +00:00
|
|
|
|
2020-10-06 03:47:25 +00:00
|
|
|
(let [opcode (. dat 1)
|
|
|
|
parser (. dat-parser opcode)
|
|
|
|
pdat
|
2020-10-29 02:59:47 +00:00
|
|
|
(if parser (parser dat block)
|
|
|
|
(. opcodes opcode) (dat-parser.op dat)
|
|
|
|
(error (.. "Unrecognized opcode " (fv opcode))))]
|
2021-04-18 02:50:09 +00:00
|
|
|
(when pdat
|
|
|
|
(set pdat.nearest-symbol block.last-symbol)
|
|
|
|
(table.insert block.pdats pdat)
|
|
|
|
(when pdat.globals
|
|
|
|
(each [name _ (pairs pdat.globals)]
|
2021-05-15 15:50:53 +00:00
|
|
|
(tset block.globals name (length block.pdats))))
|
|
|
|
(when pdat.preserved
|
|
|
|
(each [name pdat-preserved (pairs pdat.preserved)]
|
|
|
|
(tset block.preserved name pdat-preserved)))))))
|
2020-10-06 03:47:25 +00:00
|
|
|
block)
|
|
|
|
|
|
|
|
(fn dat-parser.op [op]
|
|
|
|
(let [[mode arg] (parse-mode-arg op)]
|
|
|
|
{:type :op :opcode (. op 1) : mode : arg}))
|
|
|
|
|
|
|
|
(fn dat-parser.block [block]
|
|
|
|
(let [dats (lume.clone block)]
|
|
|
|
(table.remove dats 1)
|
2021-04-18 02:50:09 +00:00
|
|
|
(parse-dats (new-block block.last-symbol) dats)))
|
2020-10-06 03:47:25 +00:00
|
|
|
|
|
|
|
(fn dat-parser.db [db] {:type :var :init (. db 2) :size 1})
|
|
|
|
(fn dat-parser.dw [dw] {:type :var :init (. dw 2) :size 2})
|
2021-08-10 17:23:09 +00:00
|
|
|
(fn dat-parser.dl [dl] {:type :var :init (. dl 4) :size 4})
|
2020-10-06 03:47:25 +00:00
|
|
|
(fn dat-parser.bytes [bytes] {:type :raw :bytes (. bytes 2)})
|
|
|
|
(fn dat-parser.ref [ref] {:type :ref :target (. ref 2)})
|
|
|
|
(fn dat-parser.flatten [flat block]
|
|
|
|
(parse-dats block (lume.slice flat 2))
|
|
|
|
nil)
|
2020-11-29 05:44:23 +00:00
|
|
|
(fn dat-parser.export [label block]
|
|
|
|
(tset block.globals (. label 2) true)
|
|
|
|
nil)
|
2021-01-10 19:52:05 +00:00
|
|
|
(fn dat-parser.computed [label block]
|
|
|
|
(tset block.symbols (. label 2) (. label 3))
|
|
|
|
nil)
|
|
|
|
|
2021-01-03 19:01:48 +00:00
|
|
|
(fn dat-parser.align [pad] {:type :pad :align (. pad 2)})
|
2021-05-15 15:50:53 +00:00
|
|
|
(fn dat-parser.hot-preserve [[_ label & dats] block]
|
|
|
|
(let [preserve-block (new-block)]
|
|
|
|
(tset block.preserved label preserve-block)
|
|
|
|
(tset preserve-block.globals label true)
|
|
|
|
(parse-dats preserve-block [label])
|
|
|
|
(parse-dats preserve-block dats)
|
|
|
|
preserve-block))
|
2020-10-06 03:47:25 +00:00
|
|
|
|
|
|
|
(local pdat-processor {
|
2021-07-31 01:57:38 +00:00
|
|
|
:op op-pdat
|
2020-10-06 03:47:25 +00:00
|
|
|
:var {}
|
|
|
|
:ref {}
|
|
|
|
:raw {}
|
|
|
|
:block {}
|
2020-12-24 03:17:33 +00:00
|
|
|
:pad {}
|
2020-10-06 03:47:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(fn process-pdat [pdat process default ...]
|
2021-04-18 02:50:09 +00:00
|
|
|
(fn complain [ok ...]
|
|
|
|
(if ok (values ...)
|
2021-04-25 03:39:50 +00:00
|
|
|
(do (error (.. process " failed in " pdat.type " near " (or pdat.nearest-symbol "<start of block>") " @" (or pdat.addr "<no address>") " - " ...)))))
|
2020-10-06 03:47:25 +00:00
|
|
|
(local processor (. pdat-processor pdat.type process))
|
2021-07-31 23:50:47 +00:00
|
|
|
(if processor (complain (pcall #(processor pdat $...) ...)) default))
|
2020-10-06 03:47:25 +00:00
|
|
|
|
|
|
|
(fn pdat-processor.raw.size [raw] (length raw.bytes))
|
|
|
|
(fn pdat-processor.var.size [d] d.size)
|
|
|
|
(fn pdat-processor.ref.size [r] 2)
|
2021-01-03 19:01:48 +00:00
|
|
|
(fn pdat-processor.pad.size [pad]
|
|
|
|
(let [misalignment (% pad.addr pad.align)]
|
|
|
|
(if (= misalignment 0) 0
|
|
|
|
(- pad.align misalignment))))
|
2020-10-06 03:47:25 +00:00
|
|
|
(fn pdat-processor.var.bytes [d env]
|
2021-01-08 01:14:51 +00:00
|
|
|
(local init (match (type d.init)
|
|
|
|
:number d.init
|
|
|
|
:nil 0
|
|
|
|
:function (d.init env)))
|
2020-10-06 03:47:25 +00:00
|
|
|
(match d.size
|
2021-01-08 01:14:51 +00:00
|
|
|
1 (int8-to-bytes init)
|
|
|
|
2 (int16-to-bytes init)
|
2021-08-10 17:23:09 +00:00
|
|
|
3 (int24-to-bytes init)
|
|
|
|
4 (int32-to-bytes init)
|
2020-10-06 03:47:25 +00:00
|
|
|
n (string.rep "\0" n)))
|
|
|
|
(fn pdat-processor.ref.bytes [ref env]
|
|
|
|
(int16-to-bytes (env:lookup-addr ref.target)))
|
|
|
|
|
2020-12-24 03:17:33 +00:00
|
|
|
(fn pdat-processor.pad.bytes [pad] (string.rep "\0" pad.size))
|
2020-10-06 03:47:25 +00:00
|
|
|
(fn pdat-processor.block.symbols [block]
|
2021-01-24 15:14:32 +00:00
|
|
|
(lume.concat (lume.keys block.symbols) (lume.keys block.globals)))
|
2020-10-06 03:47:25 +00:00
|
|
|
|
|
|
|
(fn pdat-processor.block.patch [block env]
|
|
|
|
(local block-env (make-env block env))
|
|
|
|
(each [_ pdat (ipairs block.pdats)]
|
|
|
|
(process-pdat pdat :patch nil block-env)))
|
|
|
|
|
2020-10-15 03:40:01 +00:00
|
|
|
(fn pdat-processor.block.allocate [block addr base-org]
|
2020-10-06 03:47:25 +00:00
|
|
|
(var size 0)
|
2020-10-15 03:40:01 +00:00
|
|
|
(local baseaddr (if base-org (+ base-org.addr base-org.size) addr))
|
|
|
|
(set block.addr baseaddr)
|
2020-10-06 03:47:25 +00:00
|
|
|
(each [_ pdat (ipairs block.pdats)]
|
2020-10-15 03:40:01 +00:00
|
|
|
(set pdat.addr (+ baseaddr size))
|
2020-10-06 03:47:25 +00:00
|
|
|
(process-pdat pdat :allocate nil pdat.addr)
|
|
|
|
(local pdatsize (process-pdat pdat :size pdat.size))
|
|
|
|
(set pdat.size pdatsize)
|
2020-10-15 03:40:01 +00:00
|
|
|
(set pdat.addr (+ baseaddr size))
|
2020-10-06 03:47:25 +00:00
|
|
|
(set size (+ size pdatsize)))
|
|
|
|
(set block.size size))
|
|
|
|
|
|
|
|
(fn pdat-processor.block.generate [block env]
|
|
|
|
(local block-env (make-env block env))
|
|
|
|
(var bytes "")
|
|
|
|
(each [_ pdat (ipairs block.pdats)]
|
|
|
|
(process-pdat pdat :generate nil block-env)
|
|
|
|
(local pdatbytes (process-pdat pdat :bytes pdat.bytes block-env))
|
|
|
|
(assert (= (type pdatbytes) :string) (.. "failed to generate bytes: " (fv pdat)))
|
|
|
|
(set pdat.bytes pdatbytes)
|
|
|
|
(set bytes (.. bytes pdatbytes)))
|
|
|
|
(set block.bytes bytes))
|
|
|
|
|
2020-09-20 17:55:06 +00:00
|
|
|
{:type :program
|
|
|
|
:org-to-block {}
|
|
|
|
:symbol-to-org {}
|
2020-09-17 02:34:36 +00:00
|
|
|
:start-symbol :main
|
2020-10-06 03:47:25 +00:00
|
|
|
: dat-parser
|
|
|
|
: pdat-processor
|
|
|
|
: new-block
|
2020-10-15 03:40:01 +00:00
|
|
|
: prg-base
|
2020-10-06 03:47:25 +00:00
|
|
|
:parse-dats (fn [self block dats] (parse-dats block dats))
|
2020-10-04 19:10:56 +00:00
|
|
|
:dbg
|
|
|
|
(fn [self ...]
|
|
|
|
(when self.dbgfile
|
|
|
|
(for [i 1 (select :# ...)]
|
|
|
|
(when (not= i 1) (self.dbgfile:write " "))
|
|
|
|
(self.dbgfile:write (fv (select i ...))))
|
|
|
|
(self.dbgfile:write "\n")))
|
|
|
|
:debug-to
|
|
|
|
(fn [self filename]
|
|
|
|
(set self.dbgfile (io.open filename :w)))
|
2020-09-20 17:55:06 +00:00
|
|
|
:org
|
|
|
|
(fn [self org]
|
|
|
|
(var block (. self.org-to-block org))
|
|
|
|
(when (not block)
|
2020-09-21 01:39:17 +00:00
|
|
|
(set block (new-block))
|
2021-07-31 01:57:38 +00:00
|
|
|
(tset self.org-to-block org block)
|
|
|
|
(set block.org org))
|
2020-09-20 17:55:06 +00:00
|
|
|
{: block
|
2020-10-04 19:10:56 +00:00
|
|
|
: org
|
|
|
|
:prg self
|
|
|
|
:ptr (fn [self] (tostring self.org))
|
|
|
|
:append (fn [self ...] (self.prg:dbg self.org ...) (parse-dats self.block [...]) self)})
|
|
|
|
:parse-addr
|
|
|
|
(fn [self name]
|
2021-07-31 01:57:38 +00:00
|
|
|
(local addr ((or addr-parser tonumber) name))
|
2020-10-04 19:10:56 +00:00
|
|
|
(if addr addr (error (.. "Symbol '" name "' not found"))))
|
|
|
|
:env-lookup
|
|
|
|
(fn [self name lookup ...]
|
|
|
|
(local org (. self.symbol-to-org name))
|
2020-10-15 03:40:01 +00:00
|
|
|
(if org (: (make-env (. self.org-to-block org) self) lookup name ...)
|
|
|
|
self.prg-base (self.prg-base:env-lookup name lookup ...)
|
|
|
|
nil))
|
2020-10-04 19:10:56 +00:00
|
|
|
; :lookup-symbol (fn [self addr]) TODO
|
2020-09-20 17:55:06 +00:00
|
|
|
:lookup-addr
|
|
|
|
(fn [self name]
|
2020-09-27 18:53:16 +00:00
|
|
|
; (print "looking up" name "in" self)
|
2020-10-04 19:10:56 +00:00
|
|
|
(or (self:env-lookup name :lookup-addr) (self:parse-addr name)))
|
2021-07-31 01:57:38 +00:00
|
|
|
:lookup-org
|
|
|
|
(fn [self name]
|
|
|
|
(or (self:env-lookup name :lookup-org) (self:parse-addr name)))
|
2020-09-20 17:55:06 +00:00
|
|
|
:pass
|
|
|
|
(fn [self passname]
|
2021-04-18 02:50:09 +00:00
|
|
|
(print passname)
|
2020-09-20 17:55:06 +00:00
|
|
|
(each [org block (pairs self.org-to-block)]
|
2020-10-15 03:40:01 +00:00
|
|
|
(: self passname org block (if self.prg-base (. self.prg-base.org-to-block org) nil))))
|
2020-09-20 17:55:06 +00:00
|
|
|
:gather-symbols
|
|
|
|
(fn [self org block]
|
|
|
|
(each [_ name (ipairs (process-pdat block :symbols []))]
|
|
|
|
(tset self.symbol-to-org name org)))
|
|
|
|
:patch (fn [self org block] (process-pdat block :patch nil self))
|
2020-10-15 03:40:01 +00:00
|
|
|
:allocate (fn [self org block base-org] (process-pdat block :allocate nil org base-org))
|
2020-09-20 17:55:06 +00:00
|
|
|
:generate (fn [self org block] (process-pdat block :generate nil self))
|
2020-10-04 19:10:56 +00:00
|
|
|
:debug-pass (fn [self org block] (self:dbg org block))
|
2020-09-20 17:55:06 +00:00
|
|
|
:assemble
|
2020-09-17 02:34:36 +00:00
|
|
|
(fn [self]
|
2020-09-20 17:55:06 +00:00
|
|
|
(self:pass :gather-symbols)
|
|
|
|
(self:pass :patch)
|
|
|
|
(self:pass :allocate)
|
2020-10-04 19:10:56 +00:00
|
|
|
(self:pass :debug-pass)
|
|
|
|
(self:pass :generate)
|
|
|
|
(when self.dbgfile
|
|
|
|
(self.dbgfile:close)
|
2020-10-06 03:47:25 +00:00
|
|
|
(set self.dbgfile nil))
|
|
|
|
self)
|
2021-05-15 15:50:53 +00:00
|
|
|
:read-hotswap
|
|
|
|
(fn [self machine]
|
2021-05-16 00:52:06 +00:00
|
|
|
(let [addr-to-label {}
|
|
|
|
addr-to-size {}]
|
2021-05-15 15:50:53 +00:00
|
|
|
(each [_ block (pairs self.org-to-block)]
|
|
|
|
(each [label pdat (pairs block.preserved)]
|
2021-05-16 00:52:06 +00:00
|
|
|
(tset addr-to-label pdat.addr label)
|
|
|
|
(tset addr-to-size pdat.addr pdat.size)))
|
|
|
|
(collect [addr bytes (pairs (machine:read-batch addr-to-size))]
|
|
|
|
(values (. addr-to-label addr) bytes))))
|
2021-05-15 15:50:53 +00:00
|
|
|
:write-hotswap
|
|
|
|
(fn [self machine hotswap]
|
2021-05-16 00:52:06 +00:00
|
|
|
(machine:write-batch
|
|
|
|
(collect [label bytes (pairs hotswap)]
|
|
|
|
(values (self:lookup-addr label) bytes))))
|
2020-09-20 17:55:06 +00:00
|
|
|
:upload
|
2020-11-02 00:39:31 +00:00
|
|
|
(fn [self machine]
|
|
|
|
(if machine.upload (machine:upload self)
|
|
|
|
(each [org block (pairs self.org-to-block)]
|
|
|
|
(machine:write block.addr block.bytes))))
|
2020-09-20 17:55:06 +00:00
|
|
|
})
|
|
|
|
|
2020-10-06 03:47:25 +00:00
|
|
|
{:new program}
|