1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 02:59:03 -08:00

simplify format_out arguments

This commit is contained in:
Connor Olding 2016-01-02 07:21:40 -08:00
parent 05b0e8aa8f
commit fabb1852e0

View file

@ -985,7 +985,7 @@ function Parser:format_in(informat)
return args return args
end end
function Parser:format_out(outformat, first, args, const, formatconst) function Parser:format_out_raw(outformat, first, args, const, formatconst)
local lookup = { local lookup = {
[1]=self.dumper.add_instruction_j, [1]=self.dumper.add_instruction_j,
[3]=self.dumper.add_instruction_i, [3]=self.dumper.add_instruction_i,
@ -1029,6 +1029,10 @@ function Parser:format_out(outformat, first, args, const, formatconst)
f(self.dumper, self.line, first, out[1], out[2], out[3], out[4], out[5]) f(self.dumper, self.line, first, out[1], out[2], out[3], out[4], out[5])
end end
function Parser:format_out(t, args)
self:format_out_raw(t[3], t[1], args, t[4], t[5])
end
local overrides = {} local overrides = {}
function overrides.LI(self, name) function overrides.LI(self, name)
@ -1050,19 +1054,19 @@ function overrides.LI(self, name)
if im[2] >= 0x10000 and im[2] <= 0xFFFF8000 then if im[2] >= 0x10000 and im[2] <= 0xFFFF8000 then
args.rs = args.rt args.rs = args.rt
args.immediate = {'UPPER', im} args.immediate = {'UPPER', im}
self:format_out(lui[3], lui[1], args, lui[4], lui[5]) self:format_out(lui, args)
if im[2] % 0x10000 ~= 0 then if im[2] % 0x10000 ~= 0 then
args.immediate = {'LOWER', im} args.immediate = {'LOWER', im}
self:format_out(ori[3], ori[1], args, ori[4], ori[5]) self:format_out(ori, args)
end end
elseif im[2] >= 0x8000 and im[2] < 0x10000 then elseif im[2] >= 0x8000 and im[2] < 0x10000 then
args.rs = 'R0' args.rs = 'R0'
args.immediate = {'LOWER', im} args.immediate = {'LOWER', im}
self:format_out(ori[3], ori[1], args, ori[4], ori[5]) self:format_out(ori, args)
else else
args.rs = 'R0' args.rs = 'R0'
args.immediate = {'LOWER', im} args.immediate = {'LOWER', im}
self:format_out(addiu[3], addiu[1], args, addiu[4], addiu[5]) self:format_out(addiu, args)
end end
end end
@ -1076,9 +1080,9 @@ function overrides.LA(self, name)
args.rs = args.rt args.rs = args.rt
args.immediate = {'UPPEROFF', im} args.immediate = {'UPPEROFF', im}
self:format_out(lui[3], lui[1], args, lui[4], lui[5]) self:format_out(lui, args)
args.immediate = {'LOWER', im} args.immediate = {'LOWER', im}
self:format_out(addiu[3], addiu[1], args, addiu[4], addiu[5]) self:format_out(addiu, args)
end end
function overrides.PUSH(self, name) function overrides.PUSH(self, name)
@ -1110,25 +1114,25 @@ function overrides.PUSH(self, name)
args.rt = 'SP' args.rt = 'SP'
args.rs = 'SP' args.rs = 'SP'
args.immediate = {'NEGATE', {'NUM', #stack*4}} args.immediate = {'NEGATE', {'NUM', #stack*4}}
self:format_out(addi[3], addi[1], args, addi[4], addi[5]) self:format_out(addi, args)
end end
args.base = 'SP' args.base = 'SP'
for i, r in ipairs(stack) do for i, r in ipairs(stack) do
args.rt = r args.rt = r
if r ~= '' then if r ~= '' then
args.offset = {'NUM', (i - 1)*4} args.offset = {'NUM', (i - 1)*4}
self:format_out(w[3], w[1], args, w[4], w[5]) self:format_out(w, args)
end end
end end
if name == 'JPOP' then if name == 'JPOP' then
args.rs = 'RA' args.rs = 'RA'
self:format_out(jr[3], jr[1], args, jr[4], jr[5]) self:format_out(jr, args)
end end
if name == 'POP' or name == 'JPOP' then if name == 'POP' or name == 'JPOP' then
args.rt = 'SP' args.rt = 'SP'
args.rs = 'SP' args.rs = 'SP'
args.immediate = {'NUM', #stack*4} args.immediate = {'NUM', #stack*4}
self:format_out(addi[3], addi[1], args, addi[4], addi[5]) self:format_out(addi, args)
end end
end end
overrides.POP = overrides.PUSH overrides.POP = overrides.PUSH
@ -1143,10 +1147,10 @@ function overrides.NAND(self, name)
args.rs = self:register() args.rs = self:register()
self:optional_comma() self:optional_comma()
args.rt = self:register() args.rt = self:register()
self:format_out(and_[3], and_[1], args, and_[4], and_[5]) self:format_out(and_, args)
args.rs = args.rd args.rs = args.rd
args.rt = 'R0' args.rt = 'R0'
self:format_out(nor[3], nor[1], args, nor[4], nor[5]) self:format_out(nor, args)
end end
function overrides.NANDI(self, name) function overrides.NANDI(self, name)
@ -1174,11 +1178,11 @@ function overrides.NORI(self, name)
args.rs = self:register() args.rs = self:register()
self:optional_comma() self:optional_comma()
args.immediate = self:const() args.immediate = self:const()
self:format_out(ori[3], ori[1], args, ori[4], ori[5]) self:format_out(ori, args)
args.rd = args.rt args.rd = args.rt
args.rs = args.rt args.rs = args.rt
args.rt = 'R0' args.rt = 'R0'
self:format_out(nor[3], nor[1], args, nor[4], nor[5]) self:format_out(nor, args)
end end
function overrides.ROL(self, name) function overrides.ROL(self, name)
@ -1198,14 +1202,14 @@ function overrides.ROL(self, name)
if args.rd == args.rt and args.rd ~= 'R0' then if args.rd == args.rt and args.rd ~= 'R0' then
self:error('registers cannot be the same') self:error('registers cannot be the same')
end end
self:format_out(sll[3], sll[1], args, sll[4], sll[5]) self:format_out(sll, args)
args.rd = 'AT' args.rd = 'AT'
args.immediate = {'NUM', 32 - args.immediate[2]} args.immediate = {'NUM', 32 - args.immediate[2]}
self:format_out(srl[3], srl[1], args, srl[4], srl[5]) self:format_out(srl, args)
args.rd = left args.rd = left
args.rs = left args.rs = left
args.rt = 'AT' args.rt = 'AT'
self:format_out(or_[3], or_[1], args, or_[4], or_[5]) self:format_out(or_, args)
end end
function overrides.ROR(self, name) function overrides.ROR(self, name)
@ -1225,14 +1229,14 @@ function overrides.ROR(self, name)
if args.rd == args.rt and args.rd ~= 'R0' then if args.rd == args.rt and args.rd ~= 'R0' then
self:error('registers cannot be the same') self:error('registers cannot be the same')
end end
self:format_out(srl[3], srl[1], args, srl[4], srl[5]) self:format_out(srl, args)
args.rd = 'AT' args.rd = 'AT'
args.immediate = {'NUM', 32 - args.immediate[2]} args.immediate = {'NUM', 32 - args.immediate[2]}
self:format_out(sll[3], sll[1], args, sll[4], sll[5]) self:format_out(sll, args)
args.rd = right args.rd = right
args.rs = right args.rs = right
args.rt = 'AT' args.rt = 'AT'
self:format_out(or_[3], or_[1], args, or_[4], or_[5]) self:format_out(or_, args)
end end
function overrides.JR(self, name) function overrides.JR(self, name)
@ -1243,7 +1247,7 @@ function overrides.JR(self, name)
else else
args.rs = self:register() args.rs = self:register()
end end
self:format_out(jr[3], jr[1], args, jr[4], jr[5]) self:format_out(jr, args)
end end
local branch_basics = { local branch_basics = {
@ -1271,10 +1275,10 @@ function overrides.BEQI(self, name)
args.rt = 'AT' args.rt = 'AT'
args.rs = 'R0' args.rs = 'R0'
self:format_out(addiu[3], addiu[1], args, addiu[4], addiu[5]) self:format_out(addiu, args)
args.rs = reg args.rs = reg
self:format_out(branch[3], branch[1], args, branch[4], branch[5]) self:format_out(branch, args)
end end
overrides.BNEI = overrides.BEQI overrides.BNEI = overrides.BEQI
@ -1293,11 +1297,11 @@ function overrides.BLTI(self, name)
end end
args.rt = 'AT' args.rt = 'AT'
self:format_out(slti[3], slti[1], args, slti[4], slti[5]) self:format_out(slti, args)
args.rs = 'AT' args.rs = 'AT'
args.rt = 'R0' args.rt = 'R0'
self:format_out(branch[3], branch[1], args, branch[4], branch[5]) self:format_out(branch, args)
end end
overrides.BGEI = overrides.BLTI overrides.BGEI = overrides.BLTI
@ -1320,7 +1324,7 @@ function overrides.BLEI(self, name)
args.rt = 'AT' args.rt = 'AT'
args.rs = 'R0' args.rs = 'R0'
self:format_out(addiu[3], addiu[1], args, addiu[4], addiu[5]) self:format_out(addiu, args)
if name == 'BLEI' then if name == 'BLEI' then
args.offset = offset args.offset = offset
@ -1328,15 +1332,15 @@ function overrides.BLEI(self, name)
args.offset = 2 -- branch to delay slot of the next branch args.offset = 2 -- branch to delay slot of the next branch
end end
args.rs = reg args.rs = reg
self:format_out(beq[3], beq[1], args, beq[4], beq[5]) self:format_out(beq, args)
args.rd = 'AT' args.rd = 'AT'
self:format_out(slt[3], slt[1], args, slt[4], slt[5]) self:format_out(slt, args)
args.rs = 'AT' args.rs = 'AT'
args.rt = 'R0' args.rt = 'R0'
args.offset = offset args.offset = offset
self:format_out(branch[3], branch[1], args, branch[4], branch[5]) self:format_out(branch, args)
end end
overrides.BGTI = overrides.BLEI overrides.BGTI = overrides.BLEI
@ -1362,7 +1366,7 @@ function Parser:instruction()
local lui_args = {} local lui_args = {}
lui_args.immediate = {'UPPEROFF', o} lui_args.immediate = {'UPPEROFF', o}
lui_args.rt = 'AT' lui_args.rt = 'AT'
self:format_out(lui[3], lui[1], lui_args, lui[4], lui[5]) self:format_out(lui, lui_args)
args.offset = {'LOWER', o} args.offset = {'LOWER', o}
args.base = 'AT' args.base = 'AT'
else else
@ -1373,10 +1377,10 @@ function Parser:instruction()
self:optional_comma() self:optional_comma()
args.base = self:deref() args.base = self:deref()
end end
self:format_out(h[3], h[1], args, h[4], h[5]) self:format_out(h, args)
elseif h[2] ~= nil then elseif h[2] ~= nil then
args = self:format_in(h[2]) args = self:format_in(h[2])
self:format_out(h[3], h[1], args, h[4], h[5]) self:format_out(h, args)
else else
self:error('unimplemented instruction') self:error('unimplemented instruction')
end end