local lex = require("lex") local log = require("log") local bit = require("bit") local mrm = { } mrm.getreg = function(mod) local regmap = { ["al"] = "000", ["ah"] = "100", ["bl"] = "011", ["bh"] = "111", ["cl"] = "001", ["ch"] = "101", ["dl"] = "010", ["dh"] = "110", ["ax"] = "000", ["si"] = "110", ["bx"] = "011", ["sp"] = "100", ["eax"] = "000", ["ecx"] = "001", ["edx"] = "010", ["ebx"] = "011", ["esi"] = "110", ["edi"] = "111", ["rax"] = "000", ["rcx"] = "001", ["rdx"] = "010", ["rbx"] = "011", ["rsp"] = "100", ["rdi"] = "111", ["rbp"] = "101", ["es"] = "000", ["cs"] = "001", ["ss"] = "010", ["ds"] = "011", } return regmap[mod] end mrm.getrm = function(mod, reg) if mod == "00" then local map = { ["rax"] = "000", ["rcx"] = "001", ["rdx"] = "010", ["rbx"] = "011", ["rsp"] = "100", ["rdi"] = "111", ["rbp"] = "101", } return map[reg] elseif mod == "10" then local map = { ["rax"] = "000", ["rcx"] = "001", ["rdx"] = "010", ["rbp"] = "101", ["rsi"] = "110", ["rdi"] = "111", ["rbp"] = "101", } return map[reg] end return nil end mrm.getbase = function(reg) local map = { ["rax"] = "000", ["rcx"] = "001", ["rdx"] = "010", ["rbp"] = "101", ["rsi"] = "110", ["rdi"] = "111", ["rbp"] = "101", } return map[reg] end mrm.getindex = function(reg) local map = { ["rax"] = "000", ["rcx"] = "001", ["rdx"] = "010", ["rbp"] = "101", ["rsi"] = "110", ["rdi"] = "111", ["rbp"] = "101", } return map[reg] end mrm.fmtmem = function(block) local fmt = { base = nil, index = nil, scale = "00", disp = 0, } -- This is an ID if type(block) ~= "table" then if block:sub(1, 1) ~= ":" then log.error("assembly instruction ID must start with `:` at line " .. lex.line) end local vname = block:sub(2) local sym = symbols[vname] if sym.type == "lvar" then fmt.base = "rbp" fmt.disp = -sym.spos return fmt else log.error("only local variables can be given in assembly at line " .. lex.line) end end if #block == 2 then if lex.isreg64(block[2]) then fmt.base = block[2] elseif tonumber(block[2]) then fmt.disp = tonumber(block[2]) end return fmt elseif #block == 4 then if lex.isreg64(block[2]) and block[3] == "+" then fmt.base = block[2] elseif lex.isreg64(block[2]) and block[3] == "*" then fmt.index = block[2] else log.error("asm memory access must be (base + index), (base + disp32) or (index * scale) at line " .. lex.line) end if block[3] ~= "+" and block[3] ~= "*" then log.error("asm memory access must be (base + index), (base + disp32) or (index * scale) at line " .. lex.line) end if lex.isreg64(block[4]) and block[3] == "+" then fmt.index = block[4] elseif tonumber(block[4]) and block[3] == "+" then fmt.disp = tonumber(block[4]) elseif tonumber(block[4]) and block[3] == "*" then local s = tonumber(block[4]) if s == 1 then fmt.scale = "00" elseif s == 2 then fmt.scale = "01" elseif s == 4 then fmt.scale = "10" elseif s == 8 then fmt.scale = "11" else log.error("illegal index multiplication. Must be 1, 2, 4 or 8 at line " .. lex.line) end else log.error("asm memory access must be (base + index), (base + disp32) or (index * scale) at line " .. lex.line) end return fmt elseif #block == 6 then if lex.isreg64(block[2]) then fmt.base = block[2] else log.error("asm memory access must be (base + index + disp32) or (base + index * scale) at line " .. lex.line) end if block[3] ~= "+" then log.error("asm memory access must be (base + index + disp32) or (base + index * scale) at line " .. lex.line) end if lex.isreg64(block[4]) then fmt.index = block[4] else log.error("asm memory access must be (base + index + disp32) or (base + index * scale) at line " .. lex.line) end if tonumber(block[6]) and block[5] == "+" then fmt.disp = tonumber(block[6]) elseif tonumber(block[6]) and block[5] == "*" then local s = tonumber(block[6]) if s == 1 then fmt.scale = "00" elseif s == 2 then fmt.scale = "01" elseif s == 4 then fmt.scale = "10" elseif s == 8 then fmt.scale = "11" else log.error("illegal index multiplication. Must be 1, 2, 4 or 8 at line " .. lex.line) end else log.error("asm memory access must be (base + index + disp32) or (base + index * scale) at line " .. lex.line) end return fmt elseif #block == 8 then if lex.isreg64(block[2]) then fmt.base = block[2] else log.error("asm memory access must be (base + index * scale + disp32) at line " .. lex.line) end if block[3] ~= "+" then log.error("asm memory access must be (base + index * scale + disp32) at line " .. lex.line) end if lex.isreg64(block[4]) then fmt.index = block[4] else log.error("asm memory access must be (base + index * scale + disp32) at line " .. lex.line) end if block[5] ~= "*" then log.error("asm memory access must be (base + index * scale + disp32) at line " .. lex.line) end if tonumber(block[6]) ~= nil then local s = tonumber(block[6]) if s == 1 then fmt.scale = "00" elseif s == 2 then fmt.scale = "01" elseif s == 4 then fmt.scale = "10" elseif s == 8 then fmt.scale = "11" else log.error("illegal index multiplication. Must be 1, 2, 4 or 8 at line " .. lex.line) end end if block[7] == "+" and tonumber(block[8]) then fmt.disp = tonumber(block[8]) else log.error("asm memory access must be (base + index * scale + disp32) at line " .. lex.line) end return fmt else log.error("asm memory access only accept a maximum of 7 entries") end end return mrm