diff --git a/Lua/lib/lips/Dumper.lua b/Lua/lib/lips/Dumper.lua index 2448e8b..d908b95 100644 --- a/Lua/lib/lips/Dumper.lua +++ b/Lua/lib/lips/Dumper.lua @@ -100,7 +100,7 @@ function Dumper:add_directive(fn, line, name, a, b) t.kind = 'goto' t.addr = a insert(self.commands, t) - self.pos = a % 0x80000000 + self.pos = a self:advance(0) elseif name == 'ALIGN' then t.kind = 'ahead' @@ -129,7 +129,15 @@ function Dumper:add_directive(fn, line, name, a, b) end function Dumper:desym(t) - if type(t.tok) == 'number' then + if t.tt == 'REL' then + local target = t.tok % 0x80000000 + local pos = self.pos % 0x80000000 + local rel = floor(target/4) - 1 - floor(pos/4) + if rel > 0x8000 or rel <= -0x8000 then + self:error('branch too far') + end + return rel % 0x10000 + elseif type(t.tok) == 'number' then return t.tok elseif t.tt == 'REG' then assert(data.all_registers[t.tok], 'Internal Error: unknown register') diff --git a/Lua/lib/lips/Lexer.lua b/Lua/lib/lips/Lexer.lua index 33f6ebd..d97883f 100644 --- a/Lua/lib/lips/Lexer.lua +++ b/Lua/lib/lips/Lexer.lua @@ -28,6 +28,7 @@ function Lexer:init(asm, fn, options) self.pos = 1 self.line = 1 self.EOF = -1 + self.was_EOL = false self:nextc() end @@ -59,6 +60,7 @@ function Lexer:nextc() end self.ord = 10 end + self.was_EOL = self.ord == 10 self.chr = char(self.ord) if self.pos <= #self.asm then @@ -281,6 +283,9 @@ function Lexer:lex(_yield) yield('EOL', '\n') self:nextc() elseif self.ord == self.EOF then + if not self.was_EOL then + yield('EOL', '\n') + end yield('EOF', self.EOF) break elseif self.chr == ';' then diff --git a/Lua/lib/lips/Muncher.lua b/Lua/lib/lips/Muncher.lua index db54263..c0ab378 100644 --- a/Lua/lib/lips/Muncher.lua +++ b/Lua/lib/lips/Muncher.lua @@ -116,8 +116,12 @@ function Muncher:const(relative, no_label) self:error('labels are not allowed here') end local t = self:token(self.t) - if relative and self.tt == 'LABELSYM' then - t.tt = 'LABELREL' + if relative then + if self.tt == 'LABELSYM' then + t.tt = 'LABELREL' + else + t.tt = 'REL' + end end self:advance() return t