mirror of
https://github.com/notwa/lips
synced 2024-05-19 00:33:23 -07:00
output sparse bytes; dump words by default
This commit is contained in:
parent
46b1cbea5a
commit
ce23d57db9
56
lips.lua
56
lips.lua
|
@ -472,9 +472,7 @@ function Dumper:init(writer, fn, options)
|
||||||
self.options = options or {}
|
self.options = options or {}
|
||||||
self.labels = {}
|
self.labels = {}
|
||||||
self.commands = {}
|
self.commands = {}
|
||||||
self.buff = ''
|
self.pos = options.offset or 0
|
||||||
self.pos = 0
|
|
||||||
self.size = 0
|
|
||||||
self.lastcommand = nil
|
self.lastcommand = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1346,9 +1344,6 @@ end
|
||||||
|
|
||||||
function Dumper:advance(by)
|
function Dumper:advance(by)
|
||||||
self.pos = self.pos + by
|
self.pos = self.pos + by
|
||||||
if self.pos > self.size then
|
|
||||||
self.size = self.pos
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Dumper:push_instruction(t)
|
function Dumper:push_instruction(t)
|
||||||
|
@ -1450,8 +1445,7 @@ function Dumper:desym(tok)
|
||||||
if label == nil then
|
if label == nil then
|
||||||
self:error('undefined label')
|
self:error('undefined label')
|
||||||
end
|
end
|
||||||
local offset = self.options.offset or 0
|
return label
|
||||||
return label + offset
|
|
||||||
elseif tok[1] == 'LABELREL' then
|
elseif tok[1] == 'LABELREL' then
|
||||||
local label = self.labels[tok[2]]
|
local label = self.labels[tok[2]]
|
||||||
if label == nil then
|
if label == nil then
|
||||||
|
@ -1533,16 +1527,9 @@ function Dumper:valvar(tok, bits)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Dumper:write(t)
|
function Dumper:write(t)
|
||||||
-- this is gonna be really slow, but eh, optimization comes last
|
|
||||||
-- should really use a sparse table and fill in the string later
|
|
||||||
for _, b in ipairs(t) do
|
for _, b in ipairs(t) do
|
||||||
if self.pos >= self.size then
|
|
||||||
error('Internal Error: pos out of range; size too small', 1)
|
|
||||||
end
|
|
||||||
local s = ('%02X'):format(b)
|
local s = ('%02X'):format(b)
|
||||||
local left = self.buff:sub(1, self.pos*2)
|
self.writer(self.pos, s)
|
||||||
local right = self.buff:sub(self.pos*2 + 3)
|
|
||||||
self.buff = left..s..right
|
|
||||||
self.pos = self.pos + 1
|
self.pos = self.pos + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1576,12 +1563,7 @@ function Dumper:dump_instruction(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Dumper:dump()
|
function Dumper:dump()
|
||||||
self.pos = 0
|
self.pos = self.options.offset or 0
|
||||||
self.buff = ''
|
|
||||||
for i=1,self.size do
|
|
||||||
self.buff = self.buff..'00'
|
|
||||||
end
|
|
||||||
|
|
||||||
for i, t in ipairs(self.commands) do
|
for i, t in ipairs(self.commands) do
|
||||||
if t.line == nil then
|
if t.line == nil then
|
||||||
error('Internal Error: no line number available')
|
error('Internal Error: no line number available')
|
||||||
|
@ -1610,9 +1592,26 @@ function Dumper:dump()
|
||||||
error('Internal Error: unknown command', 1)
|
error('Internal Error: unknown command', 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
for i=1, self.size*2 - 1, 8 do
|
function assembler.word_writer()
|
||||||
self.writer(self.buff:sub(i, i + 7))
|
local buff = {}
|
||||||
|
local max = 0
|
||||||
|
return function(pos, b)
|
||||||
|
if pos then
|
||||||
|
buff[pos] = b
|
||||||
|
if pos > max then
|
||||||
|
max = pos
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i=0, max, 4 do
|
||||||
|
local a = buff[i+0] or '00'
|
||||||
|
local b = buff[i+1] or '00'
|
||||||
|
local c = buff[i+2] or '00'
|
||||||
|
local d = buff[i+3] or '00'
|
||||||
|
print(a..b..c..d)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1621,7 +1620,8 @@ function assembler.assemble(fn_or_asm, writer, options)
|
||||||
-- if fn_or_asm contains a newline; treat as assembly, otherwise load file.
|
-- if fn_or_asm contains a newline; treat as assembly, otherwise load file.
|
||||||
-- returns error message on error, or nil on success.
|
-- returns error message on error, or nil on success.
|
||||||
fn_or_asm = tostring(fn_or_asm)
|
fn_or_asm = tostring(fn_or_asm)
|
||||||
writer = writer or io.write
|
local default_writer = not writer
|
||||||
|
writer = writer or assembler.word_writer()
|
||||||
options = options or {}
|
options = options or {}
|
||||||
|
|
||||||
function main()
|
function main()
|
||||||
|
@ -1636,7 +1636,11 @@ function assembler.assemble(fn_or_asm, writer, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
local parser = Parser(writer, fn, options)
|
local parser = Parser(writer, fn, options)
|
||||||
return parser:parse(asm)
|
parser:parse(asm)
|
||||||
|
|
||||||
|
if default_writer then
|
||||||
|
writer()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if options.unsafe then
|
if options.unsafe then
|
||||||
|
|
Loading…
Reference in New Issue
Block a user