-- 6c - amd64 static lisp compiler progname = "6c" local oargs = require("oargs") local stdio = require("stdio") local log = require("log") local mybit = require("mybit") local tcm = require("tcm") local lex = require("lex") local args = oargs.compile(arg, { "-o", "-org", "-entry" }) if #args.none == 0 then print("usage: 6c [-o output] file ...") os.exit(1) end local output = (args["-o"] == nil) and "6.out" or args["-o"] local dst = io.open(output, "wb") local function add_vsymbol(name, extra) local symbol = add_symbol(name) if extra == nil then extra = { } end for k,v in pairs(extra) do symbol[k] = v end end if dst == nil then log.error("failed to open " .. output) end if args["-org"] then org = mybit.hexval(args["-org"]) else org = 0 end local function cond_push(status) local entry = { depth = depth, branches = { }, } table.insert(status.cond_stack, entry) end local function cond_add_branch(status, entry) local branch = { entry = entry, exit = nil, } table.insert(status.cond_stack[#status.cond_stack].branches, branch) end local function loop_push(status) local entry = { entry = binloc, depth = depth, breaks = { }, } table.insert(status.loop_stack, entry) end local function loop_add_break(status, entry) local brk = { entry = entry } if #status.loop_stack == 0 then log.error("cannot break outside of loop at line " .. lex.line) end table.insert(status.loop_stack[#status.loop_stack].breaks, brk) end local function solveval(expr) local sexp = expr[1] local cval = nil if sexp == "+" or sexp == "-" or sexp == "/" or sexp == "*" or sexp == ">" or sexp == "<" or sexp == ">=" or sexp == "<=" or sexp == "&" or sexp == "=" or sexp == "!=" or sexp == "|" then i = 2 while true do if expr[i] == nil or i > #expr then break end -- Nested. Will return through RAX if expr[i] == "(" then local sub_expr = { } local depth = 1 local subend = i -- We can't see the future so save the variable to -- avoid override if cval == "var" then -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 end for j = i + 1, #expr do if expr[j] == "(" then depth = depth + 1 table.insert(sub_expr, expr[j]) elseif expr[j] == ")" then depth = depth - 1 if depth == 0 then subend = j break end table.insert(sub_expr, expr[j]) else table.insert(sub_expr, expr[j]) end end local res = solveval(sub_expr) i = subend if tonumber(res) then if cval == "var" then -- MOV RCX, CVAL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, tonumber(res)) binloc = binloc + 10 -- POP RAX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 2 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "/" then -- CQO stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h99")) binloc = binloc + 2 -- DIV RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hF7")) stdio.writeu8(dst, mybit.hexval("0hF9")) binloc = binloc + 3 elseif sexp == "*" then -- IMUL RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hAF")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "&" then -- AND RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h21")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "|" then -- OR RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h09")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h94")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "!=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETL AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9C")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETLE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9E")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == ">" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETG AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == ">=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETGE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9D")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 end elseif tonumber(cval) then if sexp == "+" then cval = cval + tonumber(res) elseif sexp == "-" then cval = cval - tonumber(res) end else cval = tonumber(res) end else -- If not solveval already stores the result in RAX if cval == "var" then -- POP RCX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 2 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "*" then -- IMUL RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hAF")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "/" then -- MOV RDX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hCA")) binloc = binloc + 3 -- MOV RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RDX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hD0")) binloc = binloc + 3 -- CQO stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h99")) binloc = binloc + 2 -- DIV RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hF7")) stdio.writeu8(dst, mybit.hexval("0hF9")) binloc = binloc + 3 elseif sexp == "=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h94")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "!=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "&" then -- AND RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h21")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "|" then -- OR RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h09")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETL CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9C")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "<=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETLE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9E")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETG CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9F")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETGE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9D")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 end elseif tonumber(cval) then -- MOV RCX, CVAL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, tonumber(cval)) binloc = binloc + 10 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 end end cval = "var" end -- This is a symbol elseif expr[i]:sub(1, 1) == ":" then local sym = symbols[expr[i]:sub(2)] if sym == nil then log.error("unknown symbol `" .. expr[i]:sub(2) .. "` at line " .. lex.line) end if cval == "var" then -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 end if sym.type == "lvar" then if sym.vtype == "u8" then -- MOVZX RAX, BYTE [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u16" then -- MOVZX RAX, WORD [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u32" then -- MOV EAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 6 elseif sym.vtype == "ptr" or sym.vtype == "u64" then -- MOV RAX, [RBP - DELTA] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 end elseif sym.type == "gvar" then local offset = 0ULL offset = sym.gpos - (binloc + 17) -- MOV RCX, OFFSET stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, offset) binloc = binloc + 10 -- LEA RAX, [RIP] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0h05")) stdio.writes32(dst, 0) binloc = binloc + 7 -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 if sym.vtype == "u8" then -- MOVZX EAX, BYTE [RAX] stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 3 elseif sym.vtype == "u16" then -- MOVZX EAX, WORD [RAX] stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 3 elseif sym.vtype == "u32" then -- MOV EAX, DWORD [RAX] stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 2 elseif sym.vtype == "u64" then -- MOV RAX, QWORD [RAX] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 3 end end if cval == "var" then -- POP RCX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 2 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "/" then -- MOV RDX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hCA")) binloc = binloc + 3 -- MOV RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RDX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hD0")) binloc = binloc + 3 -- CQO stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h99")) binloc = binloc + 2 -- DIV RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hF7")) stdio.writeu8(dst, mybit.hexval("0hF9")) binloc = binloc + 3 elseif sexp == "*" then -- IMUL RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hAF")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "&" then -- AND RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h21")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "|" then -- OR RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h09")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h94")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "!=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETL CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9C")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "<=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETLE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9E")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETG CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9F")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETGE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9D")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 end elseif tonumber(cval) then -- MOV RCX, CVAL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, cval) binloc = binloc + 10 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOV RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "*" then -- IMUL RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hAF")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "/" then -- CQO stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h99")) binloc = binloc + 2 -- DIV RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hF7")) stdio.writeu8(dst, mybit.hexval("0hF9")) binloc = binloc + 3 elseif sexp == "&" then -- AND RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h21")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "|" then -- OR RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h09")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h94")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "!=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETL CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9C")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "<=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETLE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9E")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETG CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9F")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == ">=" then -- CMP RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- SETGE CL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9D")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 3 -- MOVZX RAX, CL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 end end cval = "var" elseif tonumber(expr[i]) then if cval == nil then cval = tonumber(expr[i]) elseif tonumber(cval) then if sexp == "+" then cval = tonumber(expr[i]) + cval elseif sexp == "-" then cval = cval - tonumber(expr[i]) elseif sexp == "/" then cval = cval / tonumber(expr[i]) elseif sexp == "*" then cval = cval * tonumber(expr[i]) elseif sexp == "&" then cval = bit.band(expr[i], cval) elseif sexp == "|" then cval = bit.bor(expr[i], cval) elseif sexp == "=" then if cval == tonumber(expr[i]) then cval = 1 else cval = 0 end elseif sexp == "!=" then if cval ~= tonumber(expr[i]) then cval = 1 else cval = 0 end elseif sexp == "<" then if cval < tonumber(expr[i]) then cval = 1 else cval = 0 end elseif sexp == "<=" then if cval <= tonumber(expr[i]) then cval = 1 else cval = 0 end elseif sexp == ">" then if cval > tonumber(expr[i]) then cval = 1 else cval = 0 end elseif sexp == ">=" then if cval >= tonumber(expr[i]) then cval = 1 else cval = 0 end end else -- MOV RCX, CVAL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, tonumber(expr[i])) binloc = binloc + 10 if sexp == "+" then -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "-" then -- SUB RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h29")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 elseif sexp == "/" then -- CQO stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h99")) binloc = binloc + 2 -- DIV RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hF7")) stdio.writeu8(dst, mybit.hexval("0hF9")) binloc = binloc + 3 elseif sexp == "*" then -- IMUL RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hAF")) stdio.writeu8(dst, mybit.hexval("0hC1")) binloc = binloc + 4 elseif sexp == "&" then -- AND RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h21")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "|" then -- OR RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h09")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNZ AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h94")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "!=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETNE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h95")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETL AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9C")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == "<=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETLE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9E")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == ">" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETG AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 elseif sexp == ">=" then -- CMP RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h39")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- SETGE AL stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h9D")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- MOVZX RAX, AL stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 4 end end end i = i + 1 end elseif sexp:sub(1, 1) == ":" then local sym = symbols[sexp:sub(2)] if sym == nil then log.error("could not find symbol `" .. tkn.literal:sub(2) .. "` at line " .. lex.line) end -- SUB RSP, RET_FRAME if sym.ret_frame > 0 then stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h81")) stdio.writeu8(dst, mybit.hexval("0hEC")) stdio.writes32(dst, sym.ret_frame) binloc = binloc + 7 end table.remove(expr, 1) local argcount = 0 local fdepth = 1 local i = 1 while true do local tk = expr[i] if tk == nil then break end if tk == "(" then local equation = { } local edepth = 1 for j = i + 1, #expr do if expr[j] == "(" then edepth = edepth + 1 table.insert(equation, expr[j]) elseif expr[j] == ")" then edepth = edepth - 1 if edepth == 0 then i = j break end table.insert(equation, expr[j]) else table.insert(equation, expr[j]) end end argcount = argcount + 1 local res = solveval(equation) if tonumber(res) then stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, tonumber(res)) binloc = binloc + 10 end -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 elseif tk:sub(1, 1) == ":" then local sym = symbols[tk:sub(2)] argcount = argcount + 1 if sym == nil then log.error("unknown symbol `" .. tk:sub(2) .. "` at line " .. lex.line) end if sym.vtype == "u8" then -- MOVZX RAX, BYTE [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u16" then -- MOVZX RAX, WORD [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u32" then -- MOV EAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 6 elseif sym.vtype == "u64" or sym.vtype == "ptr" then -- MOV RAX, [RBP - DELTA] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 end -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 else argcount = argcount + 1 if tonumber(tk) then -- MOV RAX, IMM64 stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, tonumber(tk)) binloc = binloc + 10 -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 end end i = i + 1 end if argcount ~= sym.arg_count then log.error("function `" .. sexp:sub(2) .. "` at line " .. lex.line .. " takes " .. sym.arg_count .. " argument(s).") end -- THIS IS TEMPORARY. WILL BE SOLVEVAL LATER -- CALL REL32 stdio.writeu8(dst, mybit.hexval("0hE8")) stdio.writes32(dst, sym.loc - binloc - 5) binloc = binloc + 5 -- Reset the stack frames if sym.arg_count > 0 then -- ADD RSP, STACK_FRAME stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h81")) stdio.writeu8(dst, mybit.hexval("0hC4")) stdio.writes32(dst, sym.arg_count * 8) binloc = binloc + 7 end if sym.ret_frame > 0 then -- POP RAX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 2 end return "var" elseif sexp == "@" then local var = expr[2] if var:sub(1, 1) ~= ":" then log.error("@ requires a variable starting with `:` at line " .. lex.line) end local vname = var:sub(2) local sym = symbols[vname] if sym == nil then log.error("unknow variable `" .. vname .. "` at line " .. lex.line) end if sym.type == "lvar" then -- LEA RAX, [RBP - SPISE] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 elseif sym.type == "gvar" then local offset = 0ULL offset = offset + sym.gpos offset = offset - (binloc + 17) -- MOV RCX, OFFSET stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, offset) binloc = binloc + 10 -- LEA RAX, [RIP] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0h05")) stdio.writes32(dst, 0) binloc = binloc + 7 -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 else log.error("@ can only take local variable at line " .. lex.line) end return "var" elseif sexp == "get" then local type = expr[2] local name = expr[3] if name:sub(1, 1) ~= ":" then log.error("get can only work on variables at line " .. lex.line) end local sym = symbols[name:sub(2)] if sym == nil then log.error("unknown symbol `" .. name:sub(2) .. "` at line " .. lex.line) end if sym.type == "lvar" then if type == "u8" then -- MOV RAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 -- MOVZX RAX, BYTE [RAX] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 4 elseif type == "u16" then -- MOV RAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 -- MOVZX RAX, WORD [RAX] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 4 end end elseif sexp == "sizeof" then local vname = expr[2] if vname:sub(1, 1) ~= ":" then log.error("variable name must start with `:` at line " .. lex.line) end vname = vname:sub(2) local sym = symbols[vname] if sym == nil then log.error("unknow variable at `" .. vname .. "` at line " .. lex.line) end if sym.type == "lvar" then if sym.vtype == "u8" then if sym.arrsize then return sym.arrsize end return 1 end elseif sym.type == "gvar" then if sym.vtype == "u8" then if sym.arrsize then return sym.arrsize end return 1 end end log.error("unknown sizeof type at line " .. lex.line) else log.error("wrong s-expression for equation at line " .. lex.line) end return cval end local function readfunc(src, status) -- For now we assume the function has no types local name = lex.read(src) local ret_frame = 0 if name.literal == "u8" then ret_frame = 8 name = lex.read(src) elseif name.literal == "u16" then ret_frame = 8 name = lex.read(src) end if string.sub(name.literal, 1, 1) ~= ":" then log.error("function name must begin with semicolon at line " .. lex.line) end local s = add_symbol(string.sub(name.literal, 2)) s.type = "func" local fargs = { } -- Reading arguments (not really lol) while true do local tkn = lex.read(src) if tkn == nil then break end if tkn.type == lex.tkns["("] then depth = depth + 1 elseif tkn.type == lex.tkns[")"] then depth = depth - 1 end table.insert(fargs, tkn.literal) -- A function is always at depth 1 if depth == 1 then break end end local argread = 0 table.remove(fargs, #fargs) table.remove(fargs, 1) s.arg_count = #fargs / 2 -- This is mega hack if #fargs > 0 then local i = 1 while true do local type = fargs[i] local name = fargs[i + 1] if name:sub(1, 1) ~= ":" then log.error("argument name must start with `:` at line " .. lex.line) end name = name:sub(2) add_vsymbol(name, { type = "lvar", vtype=type, spos = -(8 + (s.arg_count - argread) * 8), arrsize = arrsize }) argread = argread + 1 if type == "u8" then elseif type == "u16" then elseif type == "u32" then elseif type == "u64" then end i = i + 2 if i >= #fargs then break end end end s.ret_frame = ret_frame -- This is where we will be storing our local variables stdio.writeu8(dst, mybit.hexval("0h55")) -- PUSH RBP (1 byte) stdio.writeu8(dst, mybit.hexval("0h48")) -- MOV RBP, RSP (3 bytes) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hE5")) stdio.writeu8(dst, mybit.hexval("0h48")) -- SUB RSP, 0 (7 bytes) stdio.writeu8(dst, mybit.hexval("0h81")) stdio.writeu8(dst, mybit.hexval("0hEC")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu8(dst, mybit.hexval("0h00")) binloc = binloc + 11 status.spaddr = binloc - 4 status.infunc = true status.curr_func = s end local function readvar(src, status) local arrsize = nil local vtype = "lvar" local dtype = lex.read(src).literal -- [u64 n] if dtype == "[" then dtype = lex.read(src).literal local cb = lex.read(src) if tonumber(cb.literal) then arrsize = tonumber(cb.literal) cb = lex.read(src) else arrsize = 1 end if cb.type ~= lex.tkns["]"] then log.error("Bad array format at line " .. lex.line) end end if status.infunc == false then vtype = "gvar" end local name = lex.read(src) if string.sub(name.literal, 1, 1) ~= ":" then log.error("function name must begin with semicolon at line " .. lex.line) end local vtkn = lex.read(src) local value = vtkn.literal local eval = nil local gpos = 0 if value == "(" then local depth = 1 local expr = { } while true do local tk = lex.read(src) if tk == nil then log.error("s-expression assignment badly formatted at line " .. lex.line) end tk = tk.literal if tk == "(" then depth = depth + 1 table.insert(expr, tk) elseif tk == ")" then depth = depth - 1 if depth == 0 then break end table.insert(expr, tk) else table.insert(expr, tk) end end eval = solveval(expr) elseif value:sub(1, 1) == ":" then local name = value:sub(2) local sym = symbols[name] if sym == nil then log.error("unknown variable at line " .. lex.line) end if sym.type == "lvar" then if sym.vtype == "u8" then -- MOVZX RAX, [RBP - DELTA] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u16" then -- MOVZX RAX, WORD [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u32" then -- MOV EAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 6 elseif sym.vtype == "u64" or sym.vtype == "ptr" then -- MOV RAX, [RBP - DELTA] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 end else log.error("value being assigned can only be local variable at line " .. lex.line) end value = "(" eval = "var" elseif tonumber(value) then eval = tonumber(value) end if dtype == "u8" then if arrsize == nil then if vtype == "lvar" then status.spsize = status.spsize + 1 if value == "(" then -- MOV [RBP - SPOS], AL stdio.writeu8(dst, mybit.hexval("0h88")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) binloc = binloc + 6 elseif value ~= ")" then -- MOV [RBP - 2], VALUE stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) stdio.writes8(dst, tonumber(value)) binloc = binloc + 7 else src:seek("cur", -1) end elseif vtype == "gvar" then if value == "(" then if tonumber(eval) == nil then log.error("cannot initialize global variable with non constant line " .. lex.line) end stdio.writeu8(dst, tonumber(eval)) gpos = binloc binloc = binloc + 1 elseif value ~= ")" then stdio.writeu8(dst, tonumber(value)) gpos = binloc binloc = binloc + 1 else src:seek("cur", -1) end end else if vtype == "lvar" then if vtkn.type == lex.tkns.string then local vlen = value:len() local rem = vlen local cval = 0ULL arrsize = vlen status.spsize = status.spsize + vlen -- JMP REL32 stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, vlen) binloc = binloc + 5 for i = 1, vlen do stdio.writeu8(dst, value:byte(i)) binloc = binloc + 1 end -- LEA RSI, [REL 32] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0h35")) stdio.writes32(dst, -(vlen + 7)) binloc = binloc + 7 -- LEA RDI, [RBP - base] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0hBD")) stdio.writes32(dst, -status.spsize) binloc = binloc + 7 -- MOV ECX, vlen stdio.writeu8(dst, 0xB9) stdio.writeu32(dst, vlen) binloc = binloc + 5 -- REP MOVSB stdio.writeu8(dst, 0xF3) stdio.writeu8(dst, 0xA4) binloc = binloc + 2 elseif vtkn.type == lex.tkns[")"] then status.spsize = status.spsize + arrsize src:seek("cur", -1) end elseif vtype == "gvar" then if vtkn.type == lex.tkns.string then local vlen = value:len() arrsize = vlen gpos = binloc for i = 1, vlen do stdio.writeu8(dst, value:byte(i)) binloc = binloc + 1 end elseif vtkn.type == lex.tkns[")"] then src:seek("cur", -1) end end end elseif dtype == "u16" then status.spsize = status.spsize + 2 if value == "(" then -- MOV [RBP - 8], RAX stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) binloc = binloc + 7 elseif value ~= ")" then if arrsize == nil then -- MOV [RBP - 2], VALUE stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) stdio.writes16(dst, tonumber(value)) binloc = binloc + 9 else log.error("Array initialization is illegal at line " .. lex.line) end else if arrsize ~= nil then status.spsize = status.spsize + 2 * arrsize else status.spsize = status.spsize + 2 end src:seek("cur", -1) end elseif dtype == "u32" then status.spsize = status.spsize + 4 if value == "(" then elseif value ~= ")" then -- MOV [RBP - 4], VALUE stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) stdio.writes32(dst, tonumber(value)) binloc = binloc + 10 else src:seek("cur", -1) end elseif dtype == "u64" or dtype == "ptr" then status.spsize = status.spsize + 8 if value == "(" then if not tonumber(eval) then -- MOV [RBP - 8], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) binloc = binloc + 7 else -- MOV [RBP - 8], VALUE stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) stdio.writes32(dst, eval) binloc = binloc + 11 end elseif value ~= ")" then -- MOV [RBP - 8], VALUE stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -status.spsize) stdio.writes32(dst, tonumber(value)) binloc = binloc + 11 else src:seek("cur", -1) end end name = string.sub(name.literal, 2) if vtype == "lvar" then add_vsymbol(name, { type = "lvar", vtype=dtype, spos = status.spsize, arrsize = arrsize }) else add_vsymbol(name, { type = "gvar", vtype=dtype, gpos = gpos, arrsize = arrsize }) end end local function setvar(src, status) local ftk = lex.read(src) local svar = nil local mem = nil if ftk.type == lex.tkns["["] then mem = lex.read(src).literal local nxt = lex.read(src).literal -- (set [uxx ID] ...) if nxt:sub(1, 1) == ":" then local sym = symbols[nxt:sub(2)] if sym == nil then log.error("unknown symbol `" .. nxt:sub(2) .. "` at line " .. lex.line) end if sym.type == "lvar" then if sym.vtype == "ptr" then -- MOV RAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 end end elseif nxt == "(" then local depth = 1 local sexpr = { } while true do local tk = lex.read(src) if tk == nil then break end tk = tk.literal if tk == "(" then depth = depth + 1 table.insert(sexpr, tk) elseif tk == ")" then depth = depth - 1 if depth == 0 then break end table.insert(sexpr, tk) else table.insert(sexpr, tk) end end local res = solveval(sexpr) elseif tonumber(nxt) then -- MOV RAX, NUMBER stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, tonumber(nxt)) binloc = binloc + 10 end if lex.read(src).type ~= lex.tkns["]"] then log.error("memory access must be closed with an ']' at line " .. lex.line) end elseif ftk.literal:sub(1, 1) == ":" then local sym = symbols[ftk.literal:sub(2)] if sym == nil then log.error("unknown symbol `" .. ftk.literal .. "` at line " .. lex.line) end if sym.type == "lvar" or sym.type == "gvar" then svar = sym end else log.error("badly formatted set at line " .. lex.line .. " must be variable or memory") end local rval = lex.read(src) if tonumber(rval.literal) then if mem ~= nil then if mem == "u8" then stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes8(dst, tonumber(rval.literal)) binloc = binloc + 3 elseif mem == "u16" then -- MOV WORD [RAX], RVAL.LITERAL stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes16(dst, tonumber(rval.literal)) binloc = binloc + 5 elseif mem == "u32" then stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes32(dst, tonumber(rval.literal)) binloc = binloc + 6 elseif mem == "u64" then -- MOV RCX, NUMBER stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, tonumber(rval.literal)) binloc = binloc + 10 -- MOV [RAX], RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 end elseif svar ~= nil then if svar.type == "lvar" then if svar.vtype == "u8" then -- MOV BYTE [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) stdio.writes8(dst, tonumber(rval.literal)) binloc = binloc + 7 elseif svar.vtype == "u16" then -- MOV WORD [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) Stdio.writes32(dst, -svar.spos) stdio.writes16(dst, tonumber(rval.literal)) binloc = binloc + 9 elseif svar.vtype == "u32" then -- MOV DWORD [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) stdio.writes32(dst, tonumber(rval.literal)) binloc = binloc + 10 elseif svar.vtype == "ptr" or svar.vtype == "u64" then -- MOV RAX, NUMBER stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, tonumber(rval.literal)) binloc = binloc + 10 -- MOV [RBP - SPOS], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 7 end elseif svar.type == "gvar" then local offset = 0ULL offset = offset + svar.gpos offset = offset - (binloc + 17) -- MOV RCX, OFFSET stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, offset) binloc = binloc + 10 -- LEA RAX, [RIP] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writeu8(dst, mybit.hexval("0h05")) stdio.writes32(dst, 0) binloc = binloc + 7 -- ADD RAX, RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h01")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 if svar.vtype == "u8" then -- MOV BYTE [RAX], VALUE stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu8(dst, tonumber(rval.literal)) binloc = binloc + 3 elseif svar.vtype == "u16" then -- MOV WORD [RAX], VALUE stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu16(dst, tonumber(rval.literal)) binloc = binloc + 5 elseif svar.vtype == "u32" then -- MOV DWORD [RAX], VALUE stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writeu32(dst, tonumber(rval.literal)) binloc = binloc + 6 elseif svar.vtype == "u64" or svar.vtype == "ptr" then -- MOV RCX, IMM64 stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writeu64(dst, tonumber(rval.literal)) binloc = binloc + 10 -- MOV [RAX], RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 end end end if lex.peek(src).type ~= lex.tkns[")"] then log.error("Badly formatted set at line " .. lex.line) end elseif rval.type == lex.tkns["("] then local sexp = { } local depth = 1 while true do local tk = lex.read(src) if tk == nil then log.error("badly formatted value for set at line " .. lex.line) end local tk = tk.literal if tk == "(" then depth = depth + 1 table.insert(sexp, tk) elseif tk == ")" then depth = depth - 1 if depth == 0 then break end table.insert(sexp, tk) else table.insert(sexp, tk) end end if svar == nil then -- PUSH RAX stdio.writeu8(dst, mybit.hexval("0hFF")) stdio.writeu8(dst, mybit.hexval("0hF0")) binloc = binloc + 2 end local cval = solveval(sexp) if tonumber(cval) then if svar == nil then -- POP RAX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 2 end if mem ~= nil then if mem == "u8" then stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes8(dst, cval) binloc = binloc + 3 elseif mem == "u16" then -- MOV WORD [RAX], RVAL.LITERAL stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes16(dst, cval) binloc = binloc + 5 elseif mem == "u32" then stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h00")) stdio.writes32(dst, cval) binloc = binloc + 6 elseif mem == "u64" then -- MOV RCX, NUMBER stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB9")) stdio.writes64(dst, cval) binloc = binloc + 10 -- MOV [RAX], RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 end elseif svar ~= nil then if svar.type == "lvar" then if svar.vtype == "u8" then -- MOV BYTE [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0hC6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) stdio.writes8(dst, cval) binloc = binloc + 7 elseif svar.vtype == "u16" then -- MOV WORD [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) stdio.writes16(dst, cval) binloc = binloc + 9 elseif svar.vtype == "u32" then -- MOV DWORD [RBP - SPOS], VALUE stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) stdio.writes32(dst, cval) binloc = binloc + 10 elseif svar.vtype == "ptr" or svar.vtype == "u64" then -- MOV RAX, NUMBER stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, cval) binloc = binloc + 10 -- MOV [RBP - SPOS], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 7 end end end else if svar == nil then -- MOV RCX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0hC8")) binloc = binloc + 3 -- POP RAX stdio.writeu8(dst, mybit.hexval("0h8F")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 2 end if mem ~= nil then if mem == "u8" then -- MOV BYTE [RAX], CL stdio.writeu8(dst, mybit.hexval("0h88")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 2 elseif mem == "u16" then -- MOV WORD [RAX], CX stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 elseif mem == "u32" then -- MOV DWORD [RAX], ECX stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 2 elseif mem == "ptr" or mem == "u64" then -- MOV [RAX], RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 end elseif svar ~= nil then if svar.vtype == "u8" then -- MOV BYTE [RBP - SPOS], CL stdio.writeu8(dst, mybit.hexval("0h88")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 6 elseif svar.vtype == "u16" then -- MOV WORD [RBP - SPOS], CX stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 7 elseif svar.vtype == "u32" then -- MOV DWORD [RBP - SPOS], ECX stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 6 elseif svar.vtype == "ptr" or svar.vtype == "u64" then -- MOV [RBP - SPOS], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -svar.spos) binloc = binloc + 7 end end end elseif rval.literal:sub(1, 1) == ":" then local sym = symbols[rval.literal:sub(2)] if sym == nil then log.error("symbol `" .. rval.literal:sub(2) .. "` does not exist at line " .. lex.line) end if sym.vtype == "u8" then -- MOVZX RCX, BYTE [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u16" then -- MOVZX RCX, WORD [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u32" then -- MOV ECX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writes32(dst, -sym.spos) binloc = binloc + 6 elseif sym.vtype == "u64" or sym.vtype == "ptr" then -- MOV RCX, [RBP - DELTA] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h8D")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 end if mem ~= nil then if mem == "u8" then -- MOV BYTE [RAX], CL stdio.writeu8(dst, mybit.hexval("0h88")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 2 elseif mem == "u16" then -- MOV WORD [RAX], CX stdio.writeu8(dst, mybit.hexval("0h66")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 elseif mem == "u32" then -- MOV DWORD [RAX], ECX stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 2 elseif mem == "u64" then -- MOV [RAX], RCX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h08")) binloc = binloc + 3 end else log.error("can't assign variables with variables yet at line " .. lex.line) end else log.error("badly formatted set at line " .. lex.line) end end local function compas(src, tkn, status) if tkn.type == lex.tkns["("] then local edepth = depth local expr = { tkn } depth = depth + 1 while true do local tkn = lex.read(src) if tkn == nil then log.error("malformed assembly instruction/label at line " .. lex.line) end if tkn.type == lex.tkns["("] then depth = depth + 1 elseif tkn.type == lex.tkns[")"] then depth = depth - 1 end table.insert(expr, tkn) if depth == edepth then break end end local bytes = tcm.asm(expr) -- If its not a label if bytes ~= nil then for i = 1, #bytes do stdio.writeu8(dst, bytes[i]) end binloc = binloc + #bytes end elseif tkn.type == lex.tkns[")"] then depth = depth - 1 status.mode = "sl" end end local function compss(src, tkn, status) if tkn.type == lex.tkns["("] then if #status.cond_stack > 0 then if status.cond_stack[#status.cond_stack].depth == depth then if lex.peek(src).type == lex.tkns["("] then lex.read(src) -- Skip the initial bracket local cstack = status.cond_stack[#status.cond_stack] if #cstack.branches > 0 then local lbranch = cstack.branches[#cstack.branches] -- JMP REL32 stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, -5) binloc = binloc + 5 lbranch.exit = binloc - 4 -- Backpatch entry local dsave = dst:seek("cur") dst:seek("set", lbranch.entry) stdio.writes32(dst, (binloc - lbranch.entry - 4)) dst:seek("set", dsave) end local sexpr = { } local edepth = 1 while true do local tk = lex.read(src) if tk == nil then log.error("badly formatted expression for cond at line " .. lex.line) end tk = tk.literal if tk == "(" then edepth = edepth + 1 table.insert(sexpr, tk) elseif tk == ")" then edepth = edepth - 1 if edepth == 0 then break end table.insert(sexpr, tk) else table.insert(sexpr, tk) end end local result = solveval(sexpr) -- If the result is a number MOV RAX, RESULT if tonumber(result) then -- MOV RAX, RESULT stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, result) binloc = binloc + 10 end -- TEST RAX, RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writeu8(dst, mybit.hexval("0hC0")) binloc = binloc + 3 -- JZ to the end of condition stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0h84")) stdio.writes32(dst, -6) binloc = binloc + 6 cond_add_branch(status, binloc - 4) elseif lex.peek(src).type == lex.tkns["else"] then lex.read(src) local cstack = status.cond_stack[#status.cond_stack] if #cstack.branches > 0 then local lbranch = cstack.branches[#cstack.branches] -- JMP REL32 stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, -5) binloc = binloc + 5 lbranch.exit = binloc - 4 -- Backpatch entry local dsave = dst:seek("cur") dst:seek("set", lbranch.entry) stdio.writes32(dst, (binloc - lbranch.entry - 4)) dst:seek("set", dsave) else log.error("else cannot be without branches at line " .. lex.line) end else log.error("cond must have test at line " .. lex.line) end end end depth = depth + 1 elseif tkn.type == lex.tkns[")"] then climp_scope() depth = depth - 1 if depth == 0 then if status.infunc == true then if status.curr_func.ret_frame > 0 then -- MOV [RBP + ARG_COUNT + RET_FRAME], 0 -- This puts 0 on the return frame at the end stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hC7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, 8 + status.curr_func.ret_frame + 8 * status.curr_func.arg_count) stdio.writeu32(dst, 0) binloc = binloc + 11 end -- Restoring the stack pointer stdio.writeu8(dst, mybit.hexval("0h48")) -- MOV RSP, RBP (3 bytes) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hEC")) stdio.writeu8(dst, mybit.hexval("0h5D")) -- POP RBP (1 byte) stdio.writeu8(dst, mybit.hexval("0hC3")) -- RET (1 byte) binloc = binloc + 5 local dsave = dst:seek("cur") status.spsize = mybit.align16bytes(status.spsize) dst:seek("set", status.spaddr) local spbytes = mybit.nbytes(status.spsize, 4) for i = 1, #spbytes do stdio.writeu8(dst, spbytes[i]) end dst:seek("set", dsave) status.infunc = false for key, value in pairs(symbols) do local s = symbols[key] if s.type == "lvar" then symbols[key] = nil end end end end -- Backpatching `COND` if #status.cond_stack > 0 and status.cond_stack[#status.cond_stack].depth - 1 == depth then local cstack = table.remove(status.cond_stack, #status.cond_stack) for i = 1, #cstack.branches do local branch = cstack.branches[i] if branch.entry and branch.exit == nil then local dsave = dst:seek("cur") dst:seek("set", branch.entry) stdio.writes32(dst, (binloc - branch.entry - 4)) dst:seek("set", dsave) else local dsave = dst:seek("cur") dst:seek("set", branch.exit) stdio.writes32(dst, (binloc - branch.exit - 4)) dst:seek("set", dsave) end end end -- Backpatching `LOOP` if #status.loop_stack > 0 and status.loop_stack[#status.loop_stack].depth - 1 == depth then local lstack = table.remove(status.loop_stack, #status.loop_stack) -- JMP LOOP_ENTRY stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, lstack.entry - binloc - 5) binloc = binloc + 5 for i = 1, #lstack.breaks do local b = lstack.breaks[i] local dsave = dst:seek("cur") dst:seek("set", b.entry) stdio.writes32(dst, binloc - b.entry - 4) dst:seek("set", dsave) end end elseif tkn.type == lex.tkns["func"] then if depth ~= 1 then log.error("functions must be at depth level 1 not " .. depth .. " at line " .. lex.line) end readfunc(src, status) elseif tkn.type == lex.tkns["var"] then readvar(src, status) elseif tkn.type == lex.tkns["set"] then setvar(src, status) elseif tkn.type == lex.tkns["loop"] then local lp = lex.peek(src) if lp.type ~= lex.tkns[")"] then loop_push(status) else -- Small optimization -- JMP $ stdio.writeu8(dst, mybit.hexval("0hEB")) stdio.writeu8(dst, mybit.hexval("0hFE")) binloc = binloc + 2 end elseif tkn.type == lex.tkns["cond"] then cond_push(status) elseif tkn.type == lex.tkns["break"] then -- JMP REL32 stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, -5) binloc = binloc + 5 loop_add_break(status, binloc - 4) -- This is either a function or a macro elseif tkn.literal:sub(1, 1) == ":" then local sym = symbols[tkn.literal:sub(2)] if sym == nil then log.error("could not find symbol `" .. tkn.literal:sub(2) .. "` at line " .. lex.line) end if sym.type == "func" then local expr = { tkn.literal } local fdepth = 1 while true do local tk = lex.read(src) if tk == nil then log.error("badly formatted function at line " .. lex.line) end tk = tk.literal if tk == "(" then table.insert(expr, tk) fdepth = fdepth + 1 elseif tk == ")" then fdepth = fdepth - 1 if fdepth == 0 then src:seek("cur", -1) break end table.insert(expr, tk) else table.insert(expr, tk) end end solveval(expr) else log.error("can only call functions in s-expression. line " .. lex.line) end elseif tkn.type == lex.tkns["return"] then local val = lex.read(src).literal if status.infunc == false then log.error("return can only be in function at line " .. lex.line) end if status.curr_func.ret_frame == 0 then log.error("cannot return from void function at line " .. lex.line) end if tonumber(val) then -- MOV RAX, VALUE stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0hB8")) stdio.writes64(dst, tonumber(val)) binloc = binloc + 10 -- MOV [RBP + DELTA], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, 8 + status.curr_func.ret_frame + 8 * status.curr_func.arg_count) binloc = binloc + 7 elseif val:sub(1, 1) == ":" then local vname = val:sub(2) local sym = symbols[vname] if sym == nil then log.error("unknown symbol `" .. vname .. "` at line " .. lex.line) end if sym.type == "lvar" then if sym.vtype == "u8" then -- MOVZX RAX, BYTE [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB6")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u16" then -- MOVZX RAX, [RBP - SPOS] stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h0F")) stdio.writeu8(dst, mybit.hexval("0hB7")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 8 elseif sym.vtype == "u64" or sym.vtype == "ptr" then -- MOV RAX, [RBP - SPOS] print(44) stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h8B")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, -sym.spos) binloc = binloc + 7 else log.error("return type `" .. sym.vtype .. "` not implemented line " .. lex.line) end -- MOV [RBP + DELTA], RAX stdio.writeu8(dst, mybit.hexval("0h48")) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0h85")) stdio.writes32(dst, 8 + status.curr_func.ret_frame + 8 * status.curr_func.arg_count) binloc = binloc + 7 end end -- Restoring the stack pointer stdio.writeu8(dst, mybit.hexval("0h48")) -- MOV RSP, RBP (3 bytes) stdio.writeu8(dst, mybit.hexval("0h89")) stdio.writeu8(dst, mybit.hexval("0hEC")) stdio.writeu8(dst, mybit.hexval("0h5D")) -- POP RBP (1 byte) stdio.writeu8(dst, mybit.hexval("0hC3")) -- RET (1 byte) binloc = binloc + 5 elseif tkn.type == lex.tkns["asm"] then status.mode = "as" end end local function compile(fname) local src = io.open(fname, "rb") local status = { infunc = false, spaddr = 0, spsize = 0, mode = "sl", curr_func = nil, cond_stack = { }, loop_stack = { }, } if src == nil then log.error("can't open `"..fname.."`") end while true do local tkn = lex.read(src) -- Skip comments if tkn and tkn.type == lex.tkns[";"] then local cur = tkn local line = lex.line while true do cur = lex.read(src) -- If the next line starts with ; if cur ~= nil and cur.type == lex.tkns[";"] then line = lex.line elseif cur == nil or lex.line ~= line then tkn = cur break end end end if tkn == nil then break end if status.mode == "sl" then compss(src, tkn, status) else compas(src, tkn, status) end end if depth ~= 0 then log.error("depth level is not 0 at line " .. lex.line .. " depth " .. depth) end print("6c: finished reading " .. fname) src:close() end if args["-entry"] ~= nil then stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writeu32(dst, 0) binloc = binloc + 5 end for i = 1, #args.none do log.fname = args.none[i] lex.line = 1 compile(args.none[i]) end if args["-entry"] ~= nil then if symbols[args["-entry"]] == nil then log.error("Unable to find jmp entry `" .. args["-entry"] .. "`") end dst:seek("set", 0) stdio.writeu8(dst, mybit.hexval("0hE9")) stdio.writes32(dst, symbols[args["-entry"]].loc - 5) end tcm.backpatch(dst) dst:close()