1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 06:49:03 -08:00
mm/Lua/lib/lips/init.lua

90 lines
2.5 KiB
Lua
Raw Normal View History

2016-01-13 11:20:28 -08:00
local lips = {
2016-01-13 07:56:18 -08:00
_DESCRIPTION = 'Assembles MIPS assembly files for the R4300i CPU.',
_URL = 'https://github.com/notwa/lips/',
_LICENSE = [[
Copyright (C) 2015,2016 Connor Olding
This program is licensed under the terms of the MIT License, and
is distributed without any warranty. You should have received a
copy of the license along with this program; see the file LICENSE.
]],
}
2016-04-21 13:04:49 -07:00
local path = string.gsub(..., "%.init$", "").."."
local util = require(path.."util")
local Parser = require(path.."Parser")
2016-01-13 07:56:18 -08:00
2016-01-13 11:20:28 -08:00
function lips.word_writer()
2016-01-13 07:56:18 -08:00
local buff = {}
local max = -1
return function(pos, b)
if pos then
2016-04-10 08:56:42 -07:00
buff[pos] = ("%02X"):format(b)
2016-01-13 07:56:18 -08:00
if pos > max then
max = pos
end
elseif max >= 0 then
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
2016-01-13 11:20:28 -08:00
function lips.assemble(fn_or_asm, writer, options)
2016-01-13 07:56:18 -08:00
-- assemble MIPS R4300i assembly code.
-- if fn_or_asm contains a newline; treat as assembly, otherwise load file.
-- returns error message on error, or nil on success.
fn_or_asm = tostring(fn_or_asm)
local default_writer = not writer
2016-01-13 11:20:28 -08:00
writer = writer or lips.word_writer()
2016-01-13 07:56:18 -08:00
options = options or {}
local function main()
2016-04-21 13:04:49 -07:00
if options.offset then
if options.origin or options.base then
error('offset and origin/base options are mutually exclusive')
end
io.stderr:write('Warning: options.offset is deprecated.\n')
options.origin = options.offset
options.base = 0
else
options.base = options.base or 0x80000000
end
2016-01-13 07:56:18 -08:00
local fn = nil
local asm
if fn_or_asm:find('[\r\n]') then
asm = fn_or_asm
else
fn = fn_or_asm
2016-01-13 11:20:28 -08:00
asm = util.readfile(fn)
2016-01-13 07:56:18 -08:00
options.path = fn:match(".*/")
end
local parser = Parser(writer, fn, options)
parser:parse(asm)
if default_writer then
writer()
end
end
if options.unsafe then
return main()
else
local ok, err = pcall(main)
return err
end
end
2016-01-13 11:20:28 -08:00
return setmetatable(lips, {
2016-01-13 07:56:18 -08:00
__call = function(self, ...)
return self.assemble(...)
end,
})