Skip to content

Commit

Permalink
DynASM update from upstream capr/LuaJIT:v2.1-dynasm-luamode
Browse files Browse the repository at this point in the history
  • Loading branch information
capr authored and eugeneia committed Jan 20, 2016
1 parent 8dd093b commit 20329bf
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/dasm.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

--binding to the DynASM encoding engine.
--Binding to the DynASM encoding engine.
--Written by Cosmin Apreutesei. Public Domain.

local ffi = require'ffi'
local bit = require'bit'
local arch = ffi.arch
if arch == 'x64' then arch = 'x86' end --same linker for x64
local C = ffi.C
local C = ffi.load('dasm_'..arch)
local M = {C = C}

M._VERSION = 10400
Expand Down
3 changes: 2 additions & 1 deletion src/dasm_x64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
------------------------------------------------------------------------------

--unload dasm_x86 if it's already loaded.
if not package then package = {loaded = {}} end --for compat. with minilua
local dasm_x86 = package.loaded.dasm_x86
package.loaded.dasm_x86 = nil

rawset(_G, 'x64', true) -- Using a global is an ugly, but effective solution.
x64 = true -- Using a global is an ugly, but effective solution.
local dasm_x64 = require("dasm_x86")

package.loaded.dasm_x86 = dasm_x86 --put it back
Expand Down
8 changes: 7 additions & 1 deletion src/dasm_x86.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#define DASM_CHECKS
/*
Encoding engine to use with dasm.lua.
Compile with:
gcc dasm_x86.c -DDASM_CHECKS -shared -s -o dasm_x86.so
*/

#include "dasm_extern.h"
#include "dasm_proto.h"
Expand Down
34 changes: 28 additions & 6 deletions src/dasm_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void dasm_put(Dst_DECL, int start, ...)
dasm_State *D = Dst_REF;
dasm_ActList p = D->actionlist + start;
dasm_Section *sec = D->section;
int pos = sec->pos, ofs = sec->ofs, mrm = 4;
int pos = sec->pos, ofs = sec->ofs, mrm = -1;
int *b;

if (pos >= sec->epos) {
Expand All @@ -193,7 +193,7 @@ void dasm_put(Dst_DECL, int start, ...)
b[pos++] = n;
switch (action) {
case DASM_DISP:
if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; }
if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob;
case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
case DASM_IMM_D: ofs += 4; break;
Expand All @@ -203,10 +203,17 @@ void dasm_put(Dst_DECL, int start, ...)
case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
case DASM_SPACE: p++; ofs += n; break;
case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */
case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG);
if (*p++ == 1 && *p == DASM_DISP) mrm = n; continue;
case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
if (*p < 0x20 && (n&7) == 4) ofs++;
switch ((*p++ >> 3) & 3) {
case 3: n |= b[pos-3];
case 2: n |= b[pos-2];
case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
}
continue;
}
mrm = 4;
mrm = -1;
} else {
int *pl, n;
switch (action) {
Expand Down Expand Up @@ -393,7 +400,22 @@ int dasm_encode(Dst_DECL, void *buffer)
case DASM_IMM_W: dasmw(n); break;
case DASM_VREG: {
int t = *p++;
if (t >= 5) n <<= 4; else if (t >= 2) n <<= 3;
unsigned char *ex = cp - (t&7);
if ((n & 8) && t < 0xa0) {
if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
n &= 7;
} else if (n & 0x10) {
if (*ex & 0x80) {
*ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
}
while (++ex < cp) ex[-1] = *ex;
if (mark) mark--;
cp--;
n &= 7;
}
if (t >= 0xc0) n <<= 4;
else if (t >= 0x40) n <<= 3;
else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
cp[-1] ^= n;
break;
}
Expand Down
Loading

0 comments on commit 20329bf

Please sign in to comment.