diff --git a/lips/Dumper.lua b/lips/Dumper.lua index 5a84302..6d90537 100644 --- a/lips/Dumper.lua +++ b/lips/Dumper.lua @@ -33,16 +33,16 @@ function Dumper:push_instruction(t) self:advance(4) end -function Dumper:add_instruction_j(line, o, T) - self:push_instruction{line=line, o, T} +function Dumper:add_instruction_j(fn, line, o, T) + self:push_instruction{fn=fn, line=line, o, T} end -function Dumper:add_instruction_i(line, o, s, t, i) - self:push_instruction{line=line, o, s, t, i} +function Dumper:add_instruction_i(fn, line, o, s, t, i) + self:push_instruction{fn=fn, line=line, o, s, t, i} end -function Dumper:add_instruction_r(line, o, s, t, d, f, c) - self:push_instruction{line=line, o, s, t, d, f, c} +function Dumper:add_instruction_r(fn, line, o, s, t, d, f, c) + self:push_instruction{fn=fn, line=line, o, s, t, d, f, c} end function Dumper:add_label(name) @@ -58,6 +58,8 @@ function Dumper:add_bytes(line, ...) t = {} t.kind = 'bytes' t.size = 0 + t.fn = self.fn + t.line = self.line end t.line = line for _, b in ipairs{...} do @@ -70,9 +72,12 @@ function Dumper:add_bytes(line, ...) self:advance(t.size) end -function Dumper:add_directive(line, name, a, b) +function Dumper:add_directive(fn, line, name, a, b) + self.fn = fn + self.line = line local t = {} - t.line = line + t.fn = self.fn + t.line = self.line if name == 'BYTE' then self:add_bytes(line, a % 0x100) elseif name == 'HALFWORD' then @@ -124,7 +129,6 @@ function Dumper:add_directive(line, name, a, b) end function Dumper:desym(tok) - -- FIXME: errors can give wrong filename if type(tok[2]) == 'number' then return tok[2] elseif tok[1] == 'LABELSYM' then @@ -254,9 +258,9 @@ end function Dumper:dump() self.pos = self.options.offset or 0 for i, t in ipairs(self.commands) do - if t.line == nil then - error('Internal Error: no line number available') - end + assert(t.fn, 'Internal Error: no file name available') + assert(t.line, 'Internal Error: no line number available') + self.fn = t.fn self.line = t.line if t.kind == 'instruction' then local uw, lw = self:dump_instruction(t) diff --git a/lips/Parser.lua b/lips/Parser.lua index 64eaea6..03d1e04 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -69,36 +69,38 @@ end function Parser:directive() local name = self.tok self:advance() - local line = self.line + local function add(...) + self.dumper:add_directive(self.fn, self.line, ...) + end if name == 'ORG' then - self.dumper:add_directive(line, name, self:number()) + add(name, self:number()) elseif name == 'ALIGN' or name == 'SKIP' then if self:is_EOL() and name == 'ALIGN' then - self.dumper:add_directive(line, name, 0) + add(name, 0) else local size = self:number() if self:is_EOL() then - self.dumper:add_directive(line, name, size) + add(name, size) else self:optional_comma() - self.dumper:add_directive(line, name, size, self:number()) + add(name, size, self:number()) end self:expect_EOL() end elseif name == 'BYTE' or name == 'HALFWORD' then - self.dumper:add_directive(line, name, self:number()) + add(name, self:number()) while not self:is_EOL() do self:advance() self:optional_comma() - self.dumper:add_directive(line, name, self:number()) + add(name, self:number()) end self:expect_EOL() elseif name == 'WORD' then -- allow labels in word directives - self.dumper:add_directive(line, name, self:const()[2]) + add(name, self:const()[2]) while not self:is_EOL() do self:advance() self:optional_comma() - self.dumper:add_directive(line, name, self:const()[2]) + add(name, self:const()[2]) end self:expect_EOL() elseif name == 'INC' then @@ -106,10 +108,10 @@ function Parser:directive() elseif name == 'ASCII' or name == 'ASCIIZ' then local bytes = self:string() for i, number in ipairs(bytes) do - self.dumper:add_directive(line, 'BYTE', number) + add('BYTE', number) end if name == 'ASCIIZ' then - self.dumper:add_directive(line, 'BYTE', 0) + add('BYTE', 0) end self:expect_EOL() elseif name == 'INCBIN' then @@ -246,7 +248,7 @@ function Parser:format_out_raw(outformat, first, args, const, formatconst) if f == nil then error('Internal Error: invalid output formatting string', 1) end - f(self.dumper, self.line, first, out[1], out[2], out[3], out[4], out[5]) + f(self.dumper, self.fn, self.line, first, out[1], out[2], out[3], out[4], out[5]) end function Parser:format_out(t, args) @@ -311,12 +313,12 @@ function Parser:tokenize(asm) end) local function lex() - local t = {} local ok, a, b, c, d = coroutine.resume(routine) if not ok then a = a or 'Internal Error: lexer coroutine has stopped' error(a) end + local t = {} t.tt = a t.tok = b t.fn = c