Skip to content

Commit

Permalink
Sorted files/namespaces so now the API is much different. also finish…
Browse files Browse the repository at this point in the history
…ed the 5.1 Verifier
  • Loading branch information
efrederickson committed Dec 10, 2012
1 parent 6b55e58 commit e3e2e18
Show file tree
Hide file tree
Showing 43 changed files with 500 additions and 365 deletions.
4 changes: 2 additions & 2 deletions Decompiler.lua → Decompiler51.lua
Expand Up @@ -3,8 +3,8 @@ require'LAT'
if #arg == 0 then
error("No input file specified!")
end
file = Disassemble(io.open(arg[1], "rb"):read"*a")
file = LAT.Lua51.Disassemble(io.open(arg[1], "rb"):read"*a")
print("; Decompiled to lasm by LASM Decompiler v1.0")
print("; Decompiler Copyright (C) 2012 LoDC")
print""
print(Decompile.LASM(file))
print(LAT.Lua51.Decompile.LASM(file))
4 changes: 2 additions & 2 deletions LasmCompiler.lua → LasmCompiler51.lua
Expand Up @@ -32,7 +32,7 @@ local inFile = io.open(options.File, "r")
if not inFile then error("Unable to open input file") end
local source = inFile:read"*a"
inFile:close()
local p = Parser:new()
local p = LAT.Lua51.Parser:new()
local ok, file = pcall(p.Parse, p, source, options.File)
--local ok
--local file = p:Parse(source, options.File)
Expand All @@ -57,5 +57,5 @@ if options.Nocompile == false then
f:close()
end
if options.Dump then
Dump(file)
LAT.Lua51.Dump(file)
end
2 changes: 1 addition & 1 deletion LasmInterpreter.lua → LasmInterpreter51.lua
Expand Up @@ -23,7 +23,7 @@ while true do
end
io.write">> "
end
local p = Parser:new()
local p = LAT.Lua51.Parser:new()
local ok, ret = pcall(p.Parse, p, line, "LASM Interactive Chunk")
if not ok then
print("Syntax Error: " .. ret)
Expand Down
7 changes: 0 additions & 7 deletions lua52/init.lua

This file was deleted.

4 changes: 2 additions & 2 deletions samples/consteval.bat
@@ -1,8 +1,8 @@
@del consteval_test.luac
@cd ..
@LasmCompiler.lua -o samples\consteval_test.luac samples\consteval_test.lasm
@LasmCompiler51.lua -o samples\consteval_test.luac samples\consteval_test.lasm
@samples\consteval.lua samples\consteval_test.luac
@Decompiler samples\consteval_test.luac
@Decompiler51 samples\consteval_test.luac
@cd samples
@echo Output:
@consteval_test.luac
6 changes: 3 additions & 3 deletions samples/consteval.lua
Expand Up @@ -9,7 +9,7 @@ if not fn then print"No input file!" return end
local f = io.open(fn, "rb")
if not f then print("Unable to open input file '" .. fn .. "'") return end

local chunk = Disassemble(f:read"*a")
local chunk = LAT.Lua51.Disassemble(f:read"*a")
f:close()

local m = chunk.Main
Expand All @@ -22,11 +22,11 @@ for i = 0, m.Instructions.Count - 1 do
if i2.Opcode == "LOADK" and i3.Opcode == "LOADK" then
if m.Constants[i2.Bx].Type == "Number" and m.Constants[i3.Bx].Type == "Number" then
local x = m.Constants[i2.Bx].Value + m.Constants[i3.Bx].Value
local idx = m.Constants:Add(Constant:new("Number", x))
local idx = m.Constants:Add(LAT.Lua51.Constant:new("Number", x))
m.Instructions:Remove(i1)
m.Instructions:Remove(i2)
m.Instructions:Remove(i3)
local instr = Instruction:new"LOADK"
local instr = LAT.Lua51.Instruction:new"LOADK"
instr.A = i1.A
instr.Bx = idx
m.Instructions:Add(instr, i - 2) -- first LOADK index
Expand Down
2 changes: 0 additions & 2 deletions src/LasmParser/init.lua

This file was deleted.

10 changes: 6 additions & 4 deletions src/Chunk.lua → src/Lua51/Chunk.lua
@@ -1,8 +1,8 @@
require"bin"
require"PlatformConfig"
local verifier = require"Verifier"
local DumpBinary = LAT.Lua51.DumpBinary
local GetNumberType = LAT.Lua51.GetNumberType
local verifier = LAT.Lua51.Verifier

Chunk = {
local Chunk = {
new = function(self)
local function toList(t) -- little hack using meta tables i put together in like 30 seconds. Might have some issues.
t.Add = function(self, obj, index)
Expand Down Expand Up @@ -197,3 +197,5 @@ print(i, i-1, self.Instructions[i-1].LineNumber)
end
end,
}
return Chunk
3 changes: 1 addition & 2 deletions src/Decompiler/LASM.lua → src/Lua51/Decompiler/LASM.lua
@@ -1,6 +1,5 @@
-- This is a full lua decompiler. In 4 kilobytes.
Decompile = Decompile or { }
Decompile.LASM = function(file)
return function(file)
local s = ""
local indent = 0

Expand Down
17 changes: 9 additions & 8 deletions src/Disassembler.lua → src/Lua51/Disassembler.lua
@@ -1,16 +1,15 @@
require"Instruction"
require"bin"
require"Chunk"
require"LuaFile"
require"PlatformConfig"
local bit = LAT.Lua51.bit
local Chunk, Local, Constant, Upvalue, Instruction = LAT.Lua51.Chunk, LAT.Lua51.Local, LAT.Lua51.Constant, LAT.Lua51.Upvalue, LAT.Lua51.Instruction
local Luafile = LAT.Lua51.LuaFile
local GetNumberType = LAT.Lua51.GetNumberType

function Disassemble(chunk)
local function Disassemble(chunk)
if chunk == nil then
error("File is nil!")
end
local index = 1
local big = false;
local file = LuaFile:new()
local file = LAT.Lua51.LuaFile:new()
local loadNumber = nil

local function Read(len)
Expand Down Expand Up @@ -147,7 +146,7 @@ function Disassemble(chunk)
--c.Upvalues.Count = ReadInt32()
count = ReadInt32()
for i = 1, count do
c.Upvalues[i - 1] = { Name = ReadString() }
c.Upvalues[i - 1] = Upvalue:new(ReadString())
end

return c
Expand Down Expand Up @@ -178,3 +177,5 @@ function Disassemble(chunk)
file.Main = ReadFunction()
return file
end

return Disassemble
4 changes: 3 additions & 1 deletion src/Dumper.lua → src/Lua51/Dumper.lua
@@ -1,6 +1,6 @@
-- Mainly all LuaDbg code =P

function Dump(file)
local function Dump(file)
local _print = print
local indent = 1
local last = true
Expand Down Expand Up @@ -148,3 +148,5 @@ function Dump(file)
print("[Main function]")
dumpFunc(file.Main)
end

return Dump
10 changes: 6 additions & 4 deletions src/Instruction.lua → src/Lua51/Instruction.lua
Expand Up @@ -138,7 +138,7 @@ local LuaOp = {
VARARG = 37
}

Instruction = {
local Instruction = {
new = function(self, opcode, num)
opcode = (type(opcode) == "number" and opcode) or (opcode and LuaOp[opcode:upper()] and LuaOp[opcode:upper()] + 1) or error("Unknown opcode '" .. (opcode == nil and "<nil>" or opcode) .. "'!")
return setmetatable({
Expand All @@ -158,20 +158,22 @@ Instruction = {
end,
}

Local = {
local Local = {
new = function(self, name, spc, epc)
return setmetatable({ Name = name, StartPC = spc, EndPC = epc }, { __index = self })
end,
}

Constant = {
local Constant = {
new = function(self, type, val)
return setmetatable({ Type = type, Value = val, Number = 0 }, { __index = self })
end,
}

Upvalue = {
local Upvalue = {
new = function(self, name)
return setmetatable({ Name = name }, { __index = self })
end,
}

return { Instruction, Local, Constant, Upvalue }
File renamed without changes.
4 changes: 3 additions & 1 deletion src/LasmParser/Lexer.lua → src/Lua51/LasmParser/Lexer.lua
Expand Up @@ -27,7 +27,7 @@ local Keywords = lookupify{
'true', 'false', 'nil', 'null',
};

Lexer = {
local Lexer = {
new = function(self, str, name)
return setmetatable({ _str = str, _name = name }, { __index = self })
end,
Expand Down Expand Up @@ -453,3 +453,5 @@ Lexer = {
return tok
end
}

return Lexer
36 changes: 19 additions & 17 deletions src/LasmParser/Parser.lua → src/Lua51/LasmParser/Parser.lua
@@ -1,7 +1,7 @@
Parser = {
local Parser = {
new = function(self, source, name)
return setmetatable({
lexed = source and (type(source) == "table" and source) or (type(source) == "string" and Lexer:new():Lex(source, "<unknown>")) or nil,
lexed = source and (type(source) == "table" and source) or (type(source) == "string" and LAT.Lua51.Lexer:new():Lex(source, "<unknown>")) or nil,
name = name
}, { __index = self })
end,
Expand All @@ -15,13 +15,13 @@ Parser = {
local funcConstTables = { } -- for functions, constTable and constNil go in here
local funcStack = { } -- functions... for nesting functions and such
local func = nil -- the current function
local file = LuaFile:new()
local file = LAT.Lua51.LuaFile:new()
local wasStacksizeSet = false
file.Main.Vararg = 2 -- main function is always vararg
file.Main.Name = self.name or name or "LASM Chunk"
func = file.Main

local tok = self.lexed or (type(source) == "table" and source) or (type(source) == "string" and Lexer:new():Lex(source, name or "<unknown>"))
local tok = self.lexed or (type(source) == "table" and source) or (type(source) == "string" and LAT.Lua51.Lexer:new():Lex(source, name or "<unknown>"))
if not tok then error'No parser input!' end

local function parseError(msg)
Expand Down Expand Up @@ -83,7 +83,7 @@ Parser = {
end

local function isOpcode(d)
local x,y = pcall(function() Instruction:new(d) end)
local x,y = pcall(function() LAT.Lua51.Instruction:new(d) end)
--print(x, y)
return x
end
Expand All @@ -110,7 +110,7 @@ Parser = {
end

local function addConst(alwaysDefine)
alwaysDefine = alwaysDefine or true
alwaysDefine = alwaysDefine == nil and true or alwaysDefine
local value = nil

if tok:Is'String' then value = evalString(tok:Get().Data)
Expand All @@ -123,13 +123,13 @@ Parser = {

if alwaysDefine or constTable[value] == nil then
if value == true or value == false then
func.Constants:Add(Constant:new("Bool", value))
func.Constants:Add(LAT.Lua51.Constant:new("Bool", value))
elseif value == nil then
func.Constants:Add(Constant:new("Nil", nil))
func.Constants:Add(LAT.Lua51.Constant:new("Nil", nil))
elseif type(value) == "number" then
func.Constants:Add(Constant:new("Number", value))
func.Constants:Add(LAT.Lua51.Constant:new("Number", value))
elseif type(value) == "string" then
func.Constants:Add(Constant:new("String", value))
func.Constants:Add(LAT.Lua51.Constant:new("String", value))
end

if value ~= nil then
Expand Down Expand Up @@ -180,22 +180,22 @@ Parser = {
elseif tok:Is'Ident' then
name = tok:Get().Data
end
func.Locals:Add(Local:new(name, 0, 0))
func.Locals:Add(LAT.Lua51.Local:new(name, 0, 0))
elseif tok:ConsumeKeyword".upval" or tok:ConsumeKeyword".upvalue" then
local name
if tok:Is'String' then
name = readString()
elseif tok:Is'Ident' then
name = tok:Get().Data
end
func.Upvalues:Add(Upvalue:new(name))
func.Upvalues:Add(LAT.Lua51.Upvalue:new(name))
elseif tok:ConsumeKeyword".stacksize" or tok:ConsumeKeyword".maxstacksize" then
func.MaxStackSize = readNum()
wasStacksizeSet = true
elseif tok:ConsumeKeyword".vararg" then
func.Vararg = readNum()
elseif tok:ConsumeKeyword".func" or tok:ConsumeKeyword".function" then
local n = Chunk:new()
local n = LAT.Lua51.Chunk:new()
n.FirstLine = tok:Peek(-1).Line
n.Name = tok:Is'String' and readString() or "LASM Chunk"
func.Protos:Add(n)
Expand All @@ -208,7 +208,7 @@ Parser = {
local f = table.remove(funcStack)
func.LastLine = tok:Peek(-1).Line
local instr1 = func.Instructions[func.Instructions.Count - 1]
local instr2 = Instruction:new("RETURN")
local instr2 = LAT.Lua51.Instruction:new("RETURN")
instr2.A = 0
instr2.B = 1
instr2.C = 0
Expand Down Expand Up @@ -303,7 +303,7 @@ Parser = {

if not tok:Is'Ident' then parseError'Opcode expected' end
local op = tok:ConsumeIdent()
local instr = Instruction:new(op, 0)
local instr = LAT.Lua51.Instruction:new(op, 0)
if not instr.Opcode then
parseError("Unknown opcode: " .. op)
end
Expand Down Expand Up @@ -356,7 +356,6 @@ Parser = {
end
end


if instr.OpcodeParams[1] == 3 then
if not wasStacksizeSet and instr.A <= 255 then
if func.MaxStackSize < instr.A + 1 then
Expand All @@ -370,6 +369,7 @@ Parser = {
end
end
end

return instr
end

Expand All @@ -394,7 +394,7 @@ Parser = {
local instr1 = func.Instructions[func.Instructions.Count - 1]
if instr1 then
if instr1.Opcode ~= "RETURN" then
local instr2 = Instruction:new("RETURN")
local instr2 = LAT.Lua51.Instruction:new("RETURN")
instr2.A = 0
instr2.B = 1
instr2.C = 0
Expand All @@ -407,3 +407,5 @@ Parser = {
return file
end,
}

return Parser
8 changes: 5 additions & 3 deletions src/LuaFile.lua → src/Lua51/LuaFile.lua
@@ -1,7 +1,7 @@
require"bin"
require"Chunk"
local Chunk = LAT.Lua51.Chunk
local DumpBinary = LAT.Lua51.DumpBinary

LuaFile = {
local LuaFile = {
-- Default to x86 standard
new = function(self)
return setmetatable({
Expand Down Expand Up @@ -46,3 +46,5 @@ LuaFile = {
self.Main:Verify()
end,
}

return LuaFile
4 changes: 3 additions & 1 deletion lua52/PlatformConfig.lua → src/Lua51/PlatformConfig.lua
Expand Up @@ -161,10 +161,12 @@ end
-- because long longs exceeds the precision of doubles.
ConvertTo["long long"] = ConvertTo["int"]

function GetNumberType(file)
local function GetNumberType(file)
local nt = LuaNumberID[file.NumberSize .. (file.IsFloatingPoint and 0 or 1)]
if not nt then
error("Unable to determine Number type")
end
return ConvertFrom[nt], ConvertTo[nt]
end

return GetNumberType

0 comments on commit e3e2e18

Please sign in to comment.