diff options
author | Mike Pall <mike> | 2012-07-08 16:25:38 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2012-07-08 16:27:18 +0200 |
commit | c00ffcb870cd51849ee2a0f1bf1862ac965026b7 (patch) | |
tree | e9af978c9883e709b1b5e5a845ce8ef5b6d0c490 /dynasm/dasm_ppc.lua | |
parent | e3dec0438d50dbdf0184078d4e0233e399725787 (diff) | |
download | luajit-c00ffcb870cd51849ee2a0f1bf1862ac965026b7.tar.gz luajit-c00ffcb870cd51849ee2a0f1bf1862ac965026b7.tar.bz2 luajit-c00ffcb870cd51849ee2a0f1bf1862ac965026b7.zip |
Change DynASM bit operations to use Lua BitOp.
Diffstat (limited to 'dynasm/dasm_ppc.lua')
-rw-r--r-- | dynasm/dasm_ppc.lua | 66 |
1 files changed, 28 insertions, 38 deletions
diff --git a/dynasm/dasm_ppc.lua b/dynasm/dasm_ppc.lua index 7d64d81c..020ef0d9 100644 --- a/dynasm/dasm_ppc.lua +++ b/dynasm/dasm_ppc.lua | |||
@@ -26,6 +26,9 @@ local _s = string | |||
26 | local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char | 26 | local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char |
27 | local match, gmatch = _s.match, _s.gmatch | 27 | local match, gmatch = _s.match, _s.gmatch |
28 | local concat, sort = table.concat, table.sort | 28 | local concat, sort = table.concat, table.sort |
29 | local bit = bit or require("bit") | ||
30 | local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift | ||
31 | local tohex = bit.tohex | ||
29 | 32 | ||
30 | -- Inherited tables and callbacks. | 33 | -- Inherited tables and callbacks. |
31 | local g_opt, g_arch | 34 | local g_opt, g_arch |
@@ -60,11 +63,6 @@ local secpos = 1 | |||
60 | 63 | ||
61 | ------------------------------------------------------------------------------ | 64 | ------------------------------------------------------------------------------ |
62 | 65 | ||
63 | -- Return 8 digit hex number. | ||
64 | local function tohex(x) | ||
65 | return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. | ||
66 | end | ||
67 | |||
68 | -- Dump action names and numbers. | 66 | -- Dump action names and numbers. |
69 | local function dumpactions(out) | 67 | local function dumpactions(out) |
70 | out:write("DynASM encoding engine action codes:\n") | 68 | out:write("DynASM encoding engine action codes:\n") |
@@ -837,7 +835,7 @@ end | |||
837 | -- Add more branch mnemonics. | 835 | -- Add more branch mnemonics. |
838 | for cond,c in pairs(map_cond) do | 836 | for cond,c in pairs(map_cond) do |
839 | local b1 = "b"..cond | 837 | local b1 = "b"..cond |
840 | local c1 = (c%4)*0x00010000 + (c < 4 and 0x01000000 or 0) | 838 | local c1 = shl(band(c, 3), 16) + (c < 4 and 0x01000000 or 0) |
841 | -- bX[l] | 839 | -- bX[l] |
842 | map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" | 840 | map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" |
843 | map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" | 841 | map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" |
@@ -905,16 +903,14 @@ end | |||
905 | local function parse_imm(imm, bits, shift, scale, signed) | 903 | local function parse_imm(imm, bits, shift, scale, signed) |
906 | local n = tonumber(imm) | 904 | local n = tonumber(imm) |
907 | if n then | 905 | if n then |
908 | if n % 2^scale == 0 then | 906 | local m = sar(n, scale) |
909 | n = n / 2^scale | 907 | if shl(m, scale) == n then |
910 | if signed then | 908 | if signed then |
911 | if n >= 0 then | 909 | local s = sar(m, bits-1) |
912 | if n < 2^(bits-1) then return n*2^shift end | 910 | if s == 0 then return shl(m, shift) |
913 | else | 911 | elseif s == -1 then return shl(m + shl(1, bits), shift) end |
914 | if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end | ||
915 | end | ||
916 | else | 912 | else |
917 | if n >= 0 and n <= 2^bits-1 then return n*2^shift end | 913 | if sar(m, bits) == 0 then return shl(m, shift) end |
918 | end | 914 | end |
919 | end | 915 | end |
920 | werror("out of range immediate `"..imm.."'") | 916 | werror("out of range immediate `"..imm.."'") |
@@ -930,10 +926,10 @@ end | |||
930 | local function parse_shiftmask(imm, isshift) | 926 | local function parse_shiftmask(imm, isshift) |
931 | local n = tonumber(imm) | 927 | local n = tonumber(imm) |
932 | if n then | 928 | if n then |
933 | if n % 1 == 0 and n >= 0 and n <= 63 then | 929 | if shr(n, 6) == 0 then |
934 | local lsb = imm % 32 | 930 | local lsb = band(imm, 31) |
935 | local msb = imm - lsb | 931 | local msb = imm - lsb |
936 | return isshift and (lsb*2048+msb/16) or (lsb*64+msb) | 932 | return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb) |
937 | end | 933 | end |
938 | werror("out of range immediate `"..imm.."'") | 934 | werror("out of range immediate `"..imm.."'") |
939 | elseif match(imm, "^r([1-3]?[0-9])$") or | 935 | elseif match(imm, "^r([1-3]?[0-9])$") or |
@@ -949,7 +945,7 @@ local function parse_disp(disp) | |||
949 | if imm then | 945 | if imm then |
950 | local r = parse_gpr(reg) | 946 | local r = parse_gpr(reg) |
951 | if r == 0 then werror("cannot use r0 in displacement") end | 947 | if r == 0 then werror("cannot use r0 in displacement") end |
952 | return r*65536 + parse_imm(imm, 16, 0, 0, true) | 948 | return shl(r, 16) + parse_imm(imm, 16, 0, 0, true) |
953 | end | 949 | end |
954 | local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") | 950 | local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") |
955 | if reg and tailr ~= "" then | 951 | if reg and tailr ~= "" then |
@@ -957,7 +953,7 @@ local function parse_disp(disp) | |||
957 | if r == 0 then werror("cannot use r0 in displacement") end | 953 | if r == 0 then werror("cannot use r0 in displacement") end |
958 | if tp then | 954 | if tp then |
959 | waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) | 955 | waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) |
960 | return r*65536 | 956 | return shl(r, 16) |
961 | end | 957 | end |
962 | end | 958 | end |
963 | werror("bad displacement `"..disp.."'") | 959 | werror("bad displacement `"..disp.."'") |
@@ -968,7 +964,7 @@ local function parse_u5disp(disp, scale) | |||
968 | if imm then | 964 | if imm then |
969 | local r = parse_gpr(reg) | 965 | local r = parse_gpr(reg) |
970 | if r == 0 then werror("cannot use r0 in displacement") end | 966 | if r == 0 then werror("cannot use r0 in displacement") end |
971 | return r*65536 + parse_imm(imm, 5, 11, scale, false) | 967 | return shl(r, 16) + parse_imm(imm, 5, 11, scale, false) |
972 | end | 968 | end |
973 | local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") | 969 | local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") |
974 | if reg and tailr ~= "" then | 970 | if reg and tailr ~= "" then |
@@ -976,7 +972,7 @@ local function parse_u5disp(disp, scale) | |||
976 | if r == 0 then werror("cannot use r0 in displacement") end | 972 | if r == 0 then werror("cannot use r0 in displacement") end |
977 | if tp then | 973 | if tp then |
978 | waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) | 974 | waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) |
979 | return r*65536 | 975 | return shl(r, 16) |
980 | end | 976 | end |
981 | end | 977 | end |
982 | werror("bad displacement `"..disp.."'") | 978 | werror("bad displacement `"..disp.."'") |
@@ -1028,9 +1024,9 @@ map_op[".template__"] = function(params, template, nparams) | |||
1028 | -- Process each character. | 1024 | -- Process each character. |
1029 | for p in gmatch(sub(template, 9), ".") do | 1025 | for p in gmatch(sub(template, 9), ".") do |
1030 | if p == "R" then | 1026 | if p == "R" then |
1031 | rs = rs - 5; op = op + parse_gpr(params[n]) * 2^rs; n = n + 1 | 1027 | rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1 |
1032 | elseif p == "F" then | 1028 | elseif p == "F" then |
1033 | rs = rs - 5; op = op + parse_fpr(params[n]) * 2^rs; n = n + 1 | 1029 | rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1 |
1034 | elseif p == "A" then | 1030 | elseif p == "A" then |
1035 | rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 | 1031 | rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 |
1036 | elseif p == "S" then | 1032 | elseif p == "S" then |
@@ -1048,9 +1044,9 @@ map_op[".template__"] = function(params, template, nparams) | |||
1048 | elseif p == "8" then | 1044 | elseif p == "8" then |
1049 | op = op + parse_u5disp(params[n], 3); n = n + 1 | 1045 | op = op + parse_u5disp(params[n], 3); n = n + 1 |
1050 | elseif p == "C" then | 1046 | elseif p == "C" then |
1051 | rs = rs - 5; op = op + parse_cond(params[n]) * 2^rs; n = n + 1 | 1047 | rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1 |
1052 | elseif p == "X" then | 1048 | elseif p == "X" then |
1053 | rs = rs - 5; op = op + parse_cr(params[n]) * 2^(rs+2); n = n + 1 | 1049 | rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1 |
1054 | elseif p == "W" then | 1050 | elseif p == "W" then |
1055 | op = op + parse_cr(params[n]); n = n + 1 | 1051 | op = op + parse_cr(params[n]); n = n + 1 |
1056 | elseif p == "G" then | 1052 | elseif p == "G" then |
@@ -1065,22 +1061,16 @@ map_op[".template__"] = function(params, template, nparams) | |||
1065 | waction("REL_"..mode, n, s, 1) | 1061 | waction("REL_"..mode, n, s, 1) |
1066 | n = n + 1 | 1062 | n = n + 1 |
1067 | elseif p == "0" then | 1063 | elseif p == "0" then |
1068 | local mm = 2^rs | 1064 | if band(shr(op, rs), 31) == 0 then werror("cannot use r0") end |
1069 | local t = op % mm | ||
1070 | if ((op - t) / mm) % 32 == 0 then werror("cannot use r0") end | ||
1071 | elseif p == "=" or p == "%" then | 1065 | elseif p == "=" or p == "%" then |
1072 | local mm = 2^(rs + (p == "%" and 5 or 0)) | 1066 | local t = band(shr(op, p == "%" and rs+5 or rs), 31) |
1073 | local t = ((op - op % mm) / mm) % 32 | ||
1074 | rs = rs - 5 | 1067 | rs = rs - 5 |
1075 | op = op + t * 2^rs | 1068 | op = op + shl(t, rs) |
1076 | elseif p == "~" then | 1069 | elseif p == "~" then |
1077 | local mm = 2^rs | 1070 | local mm = shl(31, rs) |
1078 | local t1l = op % mm | 1071 | local lo = band(op, mm) |
1079 | local t1h = (op - t1l) / mm | 1072 | local hi = band(op, shl(mm, 5)) |
1080 | local t2l = t1h % 32 | 1073 | op = op - lo - hi + shl(lo, 5) + shr(hi, 5) |
1081 | local t2h = (t1h - t2l) / 32 | ||
1082 | local t3l = t2h % 32 | ||
1083 | op = ((t2h - t3l + t2l)*32 + t3l)*mm + t1l | ||
1084 | elseif p == "-" then | 1074 | elseif p == "-" then |
1085 | rs = rs - 5 | 1075 | rs = rs - 5 |
1086 | elseif p == "." then | 1076 | elseif p == "." then |