From 901d7ceac58e69fba5bf6e6124d73b3117561489 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 21 Apr 2016 02:12:49 -0700 Subject: [PATCH] fix .align directive and data measurement --- lips/Collector.lua | 2 +- lips/Dumper.lua | 20 +++++++------------- lips/util.lua | 10 ++++++---- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lips/Collector.lua b/lips/Collector.lua index af3136b..d090e5f 100644 --- a/lips/Collector.lua +++ b/lips/Collector.lua @@ -88,7 +88,7 @@ function Collector:directive() add(name, self:const(nil, 'no labels')) elseif name == 'ALIGN' or name == 'SKIP' then if self:is_EOL() and name == 'ALIGN' then - add(name, self:token('NUM', 0)) + add(name) else local size = self:number() if self:is_EOL() then diff --git a/lips/Dumper.lua b/lips/Dumper.lua index 9578480..0881cdb 100644 --- a/lips/Dumper.lua +++ b/lips/Dumper.lua @@ -35,10 +35,6 @@ function Dumper:export_labels(t) return t end -function Dumper:advance(by) - self.pos = self.pos + by -end - function Dumper:desym(t) if t.tt == 'REL' then local target = t.tok % 0x80000000 @@ -313,7 +309,7 @@ end function Dumper:load(statements) self.labels = {} - local pos = 0 + local pos = self.options.offset or 0 local new_statements = {} for i=1, #statements do local s = statements[i] @@ -345,14 +341,13 @@ function Dumper:load(statements) content = s[2] and s[2].tok or nil end + pos = pos + length if content == nil then - pos = pos + length local new = Statement(self.fn, self.line, '!ORG', pos) insert(new_statements, new) elseif length > 0 then insert(new_statements, self:fill(length, content)) elseif length < 0 then - pos = pos + length local new = Statement(self.fn, self.line, '!ORG', pos) insert(new_statements, new) insert(new_statements, self:fill(length, content)) @@ -372,16 +367,15 @@ function Dumper:load(statements) statements = new_statements new_statements = {} - -- TODO: keep track of lengths here? - self.pos = 0 + self.pos = self.options.offset or 0 for i=1, #statements do local s = statements[i] self.fn = s.fn self.line = s.line if s.type:sub(1, 1) ~= '!' then local new = self:assemble(s) - insert(new_statements, new) self.pos = self.pos + 4 + insert(new_statements, new) elseif s.type == '!DATA' then for i, t in ipairs(s) do if t.tt == 'LABEL' then @@ -390,14 +384,14 @@ function Dumper:load(statements) self:error('undefined label', t.tok) end t.tt = 'WORDS' - t.tok = {self.labels[t.tok]} + t.tok = {label} end end - insert(new_statements, s) self.pos = self.pos + util.measure_data(s) - elseif s.type == '!ORG' then insert(new_statements, s) + elseif s.type == '!ORG' then self.pos = s[1].tok + insert(new_statements, s) elseif s.type == '!LABEL' then -- noop else diff --git a/lips/util.lua b/lips/util.lua index 9b0b56a..4b80607 100644 --- a/lips/util.lua +++ b/lips/util.lua @@ -43,14 +43,16 @@ local function measure_data(s) assert(s and s.type == '!DATA', 'Internal Error: expected !DATA statement') local n = 0 for i, t in ipairs(s) do - if t.type == 'LABEL' then + if t.tt == 'LABEL' then n = n + 4 - elseif t.type == 'WORDS' then + elseif t.tt == 'WORDS' then n = n + #t.tok * 4 - elseif t.type == 'HALFWORDS' then + elseif t.tt == 'HALFWORDS' then n = n + #t.tok * 2 - elseif t.type == 'BYTES' then + elseif t.tt == 'BYTES' then n = n + #t.tok * 1 + else + error('Internal Error: unknown data type in !DATA') end end return n