local lex = require("lex") local log = require("log") ASM_REG8 = 0 ASM_REG16 = 1 ASM_REG32 = 2 ASM_REG64 = 3 ASM_MEM8 = 4 ASM_MEM16 = 5 ASM_MEM32 = 6 ASM_MEM64 = 7 ASM_IMM8 = 8 ASM_IMM16 = 9 ASM_IMM32 = 10 ASM_IMM64 = 11 ASM_SREG = 12 ASM_REL32 = 15 ASM_IMM16_IMM16 = 16 -- For absolute jumps and calls -- SREG: ES = 0, CS = 1, SS = 2, DS = 3, FS = 4, GS = 5 local map = { ------------------------------------------------------------------------------------ -- MOV ------------------------------------------------------------------------------------ { mnemonic = "mov", operands = { ASM_REG8, ASM_REG8 }, opcode = { "0h88", "/r"} }, { mnemonic = "mov", operands = { ASM_REG8, ASM_IMM8 }, opcode = {"+", "0hB0", "rb", "ib"} }, { mnemonic = "mov", operands = { ASM_REG32, ASM_REG32 }, opcode = {"0h89", "/r"} }, { mnemonic = "mov", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"+", "0hB8", "rd", "id"} }, { mnemonic = "mov", operands = { ASM_REG64, ASM_REG64 }, opcode = "REX.W + 0h89 /r" }, { mnemonic = "mov", operands = { ASM_REG64, ASM_IMM64 }, opcode = {"REX.W", "+", "0hB8", "rd", "io"} }, { mnemonic = "mov", operands = { ASM_CR0, ASM_REG64 }, opcode = "0h0F 0h22 /r" }, { mnemonic = "mov", operands = { ASM_REG64, ASM_CR0 }, opcode = "0h0F 0h20 /r" }, { mnemonic = "mov", operands = { ASM_MEM8, ASM_IMM8 }, opcode = {"0hC6", "/0", "iw"} }, { mnemonic = "mov", operands = { ASM_MEM16, ASM_IMM16 }, opcode = {"0h66", "0hC7", "/0", "iw"} }, { mnemonic = "mov", operands = { ASM_MEM32, ASM_IMM32 }, opcode = {"0hC7", "/0", "id"} }, { mnemonic = "mov", operands = { ASM_MEM64, ASM_IMM32 }, opcode = {"REX.W", "0hC7", "/0", "id"} }, { mnemonic = "mov", operands = { ASM_MEM8, ASM_REG8 }, opcode = { "0h88", "/r"} }, { mnemonic = "mov", operands = { ASM_MEM64, ASM_REG64 }, opcode = { "REX.W", "0h89", "/r"} }, ------------------------------------------------------------------------------------ -- CMP ------------------------------------------------------------------------------------ { mnemonic = "cmp", operands = { ASM_REG8, ASM_REG8 }, opcode = {"0h38", "/r"} }, { mnemonic = "cmp", operands = { ASM_REG8, ASM_IMM8 }, opcode = {"0h80", "/7", "ib"} }, { mnemonic = "cmp", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"0h81", "/7", "id"} }, ------------------------------------------------------------------------------------ -- LEA ------------------------------------------------------------------------------------ { mnemonic = "lea", operands = { ASM_REG64, ASM_MEM32 }, opcode = {"0h48", "0h8D", "/r"} }, ------------------------------------------------------------------------------------ -- IN ------------------------------------------------------------------------------------ { mnemonic = "in", operands = { ASM_REG8, ASM_IMM8 }, opcode = {"0hE4", "ib" } }, ------------------------------------------------------------------------------------ -- ADD ------------------------------------------------------------------------------------ { mnemonic = "add", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"0h81", "/0", "id"} }, ------------------------------------------------------------------------------------ -- AND ------------------------------------------------------------------------------------ { mnemonic = "and", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"0h81", "/4", "id"} }, ------------------------------------------------------------------------------------ -- XOR ------------------------------------------------------------------------------------ { mnemonic = "xor", operands = { ASM_REG32, ASM_REG32 }, opcode = {"0h31", "/r"} }, { mnemonic = "xor", operands = { ASM_REG64, ASM_REG64 }, opcode = {"REX.W", "0h31", "/r"} }, ------------------------------------------------------------------------------------ -- OR ------------------------------------------------------------------------------------ { mnemonic = "or", operands = { ASM_REG8, ASM_IMM8 }, opcode = {"0h80", "/1", "ib"} }, { mnemonic = "or", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"0h81", "/1", "id"} }, ------------------------------------------------------------------------------------ -- LGDT ------------------------------------------------------------------------------------ { mnemonic = "lgdt", operands = { ASM_MEM64 }, opcode = {"0h0F", "0h01", "/2"} }, ------------------------------------------------------------------------------------ -- TEST ------------------------------------------------------------------------------------ { mnemonic = "test", operands = { ASM_REG8, ASM_REG8 }, opcode = {"0h84", "/r"} }, { mnemonic = "test", operands = { ASM_REG32, ASM_IMM32 }, opcode = {"0hF7", "/0", "id"} }, ------------------------------------------------------------------------------------ -- INC ------------------------------------------------------------------------------------ { mnemonic = "inc", operands = { ASM_REG8 }, opcode = {"0hFE", "/0"} }, { mnemonic = "inc", operands = { ASM_REG32 }, opcode = {"0hFF", "/0"} }, { mnemonic = "inc", operands = { ASM_REG64 }, opcode = {"REX.W", "0hFF", "/0"} }, ------------------------------------------------------------------------------------ -- DEC ------------------------------------------------------------------------------------ { mnemonic = "dec", operands = { ASM_REG32 }, opcode = {"0hFF", "/1"} }, ------------------------------------------------------------------------------------ -- JMP ------------------------------------------------------------------------------------ { mnemonic = "jmp", operands = { ASM_REL32 }, opcode = {"0hE9", "cd"} }, { mnemonic = "jmp", operands = { ASM_REG64 }, opcode = {"0hFF", "/4"} }, ------------------------------------------------------------------------------------ -- JNZ ------------------------------------------------------------------------------------ { mnemonic = "jnz", operands = { ASM_REL32 }, opcode = {"0h0F", "0h85", "cd"} }, ------------------------------------------------------------------------------------ -- JZ ------------------------------------------------------------------------------------ { mnemonic = "jz", operands = { ASM_REL32 }, opcode = {"0h0F", "0h84", "cd"} }, ------------------------------------------------------------------------------------ -- JC ------------------------------------------------------------------------------------ { mnemonic = "jc", operands = { ASM_REL32 }, opcode = {"0h0F", "0h82", "cd"} }, ------------------------------------------------------------------------------------ -- JNC ------------------------------------------------------------------------------------ { mnemonic = "jnc", operands = { ASM_REL32 }, opcode = {"0h0F", "0h83", "cd"} }, ------------------------------------------------------------------------------------ -- CALL ------------------------------------------------------------------------------------ { mnemonic = "call", operands = { ASM_REL32 }, opcode = "0hE8 cd" }, ------------------------------------------------------------------------------------ -- HLT ------------------------------------------------------------------------------------ { mnemonic = "hlt", operands = { }, opcode = { "0hF4" } }, ------------------------------------------------------------------------------------ -- CLI ------------------------------------------------------------------------------------ { mnemonic = "cli", operands = { }, opcode = { "0hFA" } }, ------------------------------------------------------------------------------------ -- RET ------------------------------------------------------------------------------------ { mnemonic = "ret", operands = { }, opcode = { "0hC3" } }, ------------------------------------------------------------------------------------ -- STI ------------------------------------------------------------------------------------ { mnemonic = "sti", operands = { }, opcode = { "0hFB" } }, ------------------------------------------------------------------------------------ -- CLD ------------------------------------------------------------------------------------ { mnemonic = "cld", operands = { }, opcode = { "0hFC" } }, ------------------------------------------------------------------------------------ -- PUSH ------------------------------------------------------------------------------------ { mnemonic = "push", operands = { ASM_IMM8 }, opcode = "0h50 + rd" }, ------------------------------------------------------------------------------------ -- INT ------------------------------------------------------------------------------------ { mnemonic = "int", operands = { ASM_IMM8 }, opcode = {"0hCD", "ib"} }, ------------------------------------------------------------------------------------ -- REP MOVSD ------------------------------------------------------------------------------------ { mnemonic = "rep_movsd", operands = { }, opcode = {"0hF3", "0hA5"} }, ------------------------------------------------------------------------------------ -- REP STOSD ------------------------------------------------------------------------------------ { mnemonic = "rep_stosd", operands = { }, opcode = {"0hF3", "0hAB"} }, ------------------------------------------------------------------------------------ -- CPUID ------------------------------------------------------------------------------------ { mnemonic = "cpuid", operands = { }, opcode = {"0h0F", "0hA2"} }, ------------------------------------------------------------------------------------ -- RDMSR ------------------------------------------------------------------------------------ { mnemonic = "rdmsr", operands = { }, opcode = {"0h0F", "0h32"} }, ------------------------------------------------------------------------------------ -- WRMSR ------------------------------------------------------------------------------------ { mnemonic = "wrmsr", operands = { }, opcode = {"0h0F", "0h30"} }, } local function mapfind(scaninfo, expr) for i = 1, #map do if scaninfo.mnemonic == map[i].mnemonic then if scaninfo.opcount == #map[i].operands then if scaninfo.opcount == 0 then return map[i] end if scaninfo.opcount == 1 then if expr[2].type == lex.tkns.integer and (map[i].operands[1] == ASM_IMM8 or map[i].operands[1] == ASM_IMM16 or map[i].operands[1] == ASM_IMM32 or map[i].operands[1] == ASM_IMM64) then return map[i] elseif lex.isreg8(expr[2]) and map[i].operands[1] == ASM_REG8 then return map[i] elseif lex.isreg32(expr[2]) and map[i].operands[1] == ASM_REG32 then return map[i] elseif lex.isreg64(expr[2]) and map[i].operands[1] == ASM_REG64 then return map[i] elseif expr[2].type == lex.tkns["("] then if expr[3].literal == "u8" then if map[i].operands[1] == ASM_MEM8 then return map[i] end elseif expr[3].literal == "u16" then if map[i].operands[1] == ASM_MEM16 then return map[i] end elseif expr[3].literal == "u32" then if map[i].operands[1] == ASM_MEM32 then return map[i] end elseif expr[3].literal == "u64" then if map[i].operands[1] == ASM_MEM64 then return map[i] end end else if string.sub(expr[2].literal, 1, 1) == ":" then if map[i].operands[1] == ASM_REL32 then return map[i] end end end end if scaninfo.opcount == 2 then if expr[2].type == lex.tkns.integer then log.error("illegal first operand cannot be imm at line " .. lex.line) end local opinfo = { } if lex.isreg8(expr[2]) and map[i].operands[1] == ASM_REG8 then table.insert(opinfo, ASM_REG8) elseif lex.isreg32(expr[2]) and map[i].operands[1] == ASM_REG32 then table.insert(opinfo, ASM_REG32) elseif lex.isreg64(expr[2]) and map[i].operands[1] == ASM_REG64 then table.insert(opinfo, ASM_REG64) elseif expr[2].type == lex.tkns["("] then if expr[3].literal == "u8" then table.insert(opinfo, ASM_MEM8) elseif expr[3].literal == "u16" then table.insert(opinfo, ASM_MEM16) elseif expr[3].literal == "u32" then table.insert(opinfo, ASM_MEM32) elseif expr[3].literal == "u64" then table.insert(opinfo, ASM_MEM64) end elseif expr[2].literal:sub(1, 1) == ":" then local vname = expr[2].literal:sub(2) local sym = symbols[vname] if sym == nil then log.error("unknown symbol at line " .. lex.line) end if sym.type == "lvar" then local reg = expr[3] if lex.isreg8(reg) then table.insert(opinfo, ASM_MEM8) elseif lex.isreg32(reg) then table.insert(opinfo, ASM_MEM32) elseif lex.isreg64(reg) then table.insert(opinfo, ASM_MEM64) else log.error("destination can only be a u8/u16/u32 at line " .. lex.line) end end end if scaninfo.blocks[2] ~= "table" then local ftkn = { literal = scaninfo.blocks[2] } if lex.isreg8(ftkn) and map[i].operands[2] == ASM_REG8 then table.insert(opinfo, ASM_REG8) elseif lex.isreg32(ftkn) and map[i].operands[2] == ASM_REG32 then table.insert(opinfo, ASM_REG32) elseif lex.isreg64(ftkn) and map[i].operands[2] == ASM_REG64 then table.insert(opinfo, ASM_REG64) elseif tonumber(scaninfo.blocks[2]) then if opinfo[1] == ASM_REG8 or opinfo[1] == ASM_MEM8 then table.insert(opinfo, ASM_IMM8) elseif opinfo[1] == ASM_MEM16 then table.insert(opinfo, ASM_IMM16) elseif opinfo[1] == ASM_REG32 or opinfo[1] == ASM_MEM32 or opinfo[1] == ASM_MEM64 then table.insert(opinfo, ASM_IMM32) elseif opinfo[1] == ASM_REG64 then table.insert(opinfo, ASM_IMM64) end elseif ftkn.literal == "u8" then table.insert(opinfo, ASM_MEM8) elseif ftkn.literal == "u16" then table.insert(opinfo, ASM_MEM16) elseif ftkn.literal == "u32" then table.insert(opinfo, ASM_MEM32) elseif ftkn.literal == "u64" then table.insert(opinfo, ASM_MEM64) elseif ftkn.literal:sub(1, 1) == ":" then if opinfo[1] == ASM_REG8 then table.insert(opinfo, ASM_MEM8) elseif opinfo[1] == ASM_REG64 then table.insert(opinfo, ASM_MEM32) end else if opinfo[1] == ASM_REG32 then table.insert(opinfo, ASM_IMM32) end end end if opinfo[1] == map[i].operands[1] and opinfo[2] == map[i].operands[2] then return map[i] end end end end end return nil end map.scan = function(expr) -- Count operands local scaninfo = { mnemonic = expr[1].literal, blocks = { }, opcount = 0, map = nil, } if #expr > 1 then local depth = 0 local buf = { } for i = 2, #expr do if expr[i].type == lex.tkns["("] then if depth == 0 then scaninfo.opcount = scaninfo.opcount + 1 end depth = depth + 1 elseif expr[i].type == lex.tkns[")"] then depth = depth - 1 if depth == 0 then table.insert(scaninfo.blocks, buf) buf = { } end else if depth == 0 then scaninfo.opcount = scaninfo.opcount + 1 table.insert(scaninfo.blocks, expr[i].literal) else table.insert(buf, expr[i].literal) end end end end if scaninfo.opcount > 2 then log.error("only 2 operands maximum are allowed for all instructions. Line " .. lex.line) end -- print(expr[1].literal .. " has " .. scaninfo.opcount .. " operands", #scaninfo.blocks) scaninfo.map = mapfind(scaninfo, expr) return scaninfo end return map