080b4b3aff
4a6fac8a update version to 5.77 801cf3fd cosmetic change of getNumCores d397e824 fix number of cores that share LLC cache a669e092 support non-intel-cpu visual studio af5f422e Merge branch 'fenghaitao-guard_x86' into develop 9b98dc17 Guard x86 specific codes with "#if defined(__i386__) || defined(__x86_64__)" dd4173e1 move some member variables input private f72646a7 update version 4612528f format change 4b95e862 Merge branch 'shelleygoel-master' 4c262fa6 add functionality to get num of cores using x2APIC ID bc70e7e1 recover Xbyak::CastTo d09a230f unlink Label when LabelManager is destroyed 973e8597 update version afdb9fe9 Xbyak::CastTo is removed b011aca4 add RegRip +/- int acae93cd increase max temp regs for StackFrame ea4e3562 util::StackFrame uses push/pop instead of mov 42462ef9 use evex encoding for vpslld/vpslldq/vpsraw/...(reg, mem, imm); da9117a9 update version of readme.md d35f4fb7 fix the encoding of vinsertps for disp8N 1de435ed bf uses Label class 613922bd add Label L() for convenience 43e15583 fix typo 93579ee6 add protect-re.cpp 60004b5c fix url of protect-re.cpp 348b2709 fix typo of doc f34f6ed5 update manual 232110be update test 82b78bf0 add setProtectMode dd8b290f put warning message if pageSize != 4096 64775ca2 a little refactoring 7c3e7b85 fix wrong VSIB encoding with idx >= 16 git-subtree-dir: externals/xbyak git-subtree-split: 4a6fac8ade404f667b94170f713367fe7da2a852
211 lines
4.4 KiB
C++
211 lines
4.4 KiB
C++
#define XBYAK_NO_OP_NAMES
|
|
#include "xbyak/xbyak.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stack>
|
|
#include <fstream>
|
|
#ifdef _MSC_VER
|
|
#pragma warning(disable : 4996) // scanf
|
|
#define snprintf _snprintf_s
|
|
#endif
|
|
|
|
class Brainfuck : public Xbyak::CodeGenerator {
|
|
public:
|
|
int getContinuousChar(std::istream& is, char c)
|
|
{
|
|
int count = 1;
|
|
char p;
|
|
while (is >> p) {
|
|
if (p != c) break;
|
|
count++;
|
|
}
|
|
is.unget();
|
|
return count;
|
|
}
|
|
Brainfuck(std::istream& is) : CodeGenerator(100000)
|
|
{
|
|
// void (*)(void* putchar, void* getchar, int *stack)
|
|
using namespace Xbyak;
|
|
#ifdef XBYAK32
|
|
const Reg32& pPutchar(esi);
|
|
const Reg32& pGetchar(edi);
|
|
const Reg32& stack(ebp);
|
|
const Address cur = dword [stack];
|
|
push(ebp); // stack
|
|
push(esi);
|
|
push(edi);
|
|
const int P_ = 4 * 3;
|
|
mov(pPutchar, ptr[esp + P_ + 4]); // putchar
|
|
mov(pGetchar, ptr[esp + P_ + 8]); // getchar
|
|
mov(stack, ptr[esp + P_ + 12]); // stack
|
|
#elif defined(XBYAK64_WIN)
|
|
const Reg64& pPutchar(rsi);
|
|
const Reg64& pGetchar(rdi);
|
|
const Reg64& stack(rbp); // stack
|
|
const Address cur = dword [stack];
|
|
push(rsi);
|
|
push(rdi);
|
|
push(rbp);
|
|
mov(pPutchar, rcx); // putchar
|
|
mov(pGetchar, rdx); // getchar
|
|
mov(stack, r8); // stack
|
|
#else
|
|
const Reg64& pPutchar(rbx);
|
|
const Reg64& pGetchar(rbp);
|
|
const Reg64& stack(r12); // stack
|
|
const Address cur = dword [stack];
|
|
push(rbx);
|
|
push(rbp);
|
|
push(r12);
|
|
mov(pPutchar, rdi); // putchar
|
|
mov(pGetchar, rsi); // getchar
|
|
mov(stack, rdx); // stack
|
|
#endif
|
|
std::stack<Label> labelF, labelB;
|
|
char c;
|
|
while (is >> c) {
|
|
switch (c) {
|
|
case '+':
|
|
case '-':
|
|
{
|
|
int count = getContinuousChar(is, c);
|
|
if (count == 1) {
|
|
c == '+' ? inc(cur) : dec(cur);
|
|
} else {
|
|
add(cur, (c == '+' ? count : -count));
|
|
}
|
|
}
|
|
break;
|
|
case '>':
|
|
case '<':
|
|
{
|
|
int count = getContinuousChar(is, c);
|
|
add(stack, 4 * (c == '>' ? count : -count));
|
|
}
|
|
break;
|
|
case '.':
|
|
#ifdef XBYAK32
|
|
push(cur);
|
|
call(pPutchar);
|
|
pop(eax);
|
|
#elif defined(XBYAK64_WIN)
|
|
mov(ecx, cur);
|
|
sub(rsp, 32);
|
|
call(pPutchar);
|
|
add(rsp, 32);
|
|
#else
|
|
mov(edi, cur);
|
|
call(pPutchar);
|
|
#endif
|
|
break;
|
|
case ',':
|
|
#if defined(XBYAK32) || defined(XBYAK64_GCC)
|
|
call(pGetchar);
|
|
#elif defined(XBYAK64_WIN)
|
|
sub(rsp, 32);
|
|
call(pGetchar);
|
|
add(rsp, 32);
|
|
#endif
|
|
mov(cur, eax);
|
|
break;
|
|
case '[':
|
|
{
|
|
Label B = L();
|
|
labelB.push(B);
|
|
mov(eax, cur);
|
|
test(eax, eax);
|
|
Label F;
|
|
jz(F, T_NEAR);
|
|
labelF.push(F);
|
|
}
|
|
break;
|
|
case ']':
|
|
{
|
|
Label B = labelB.top(); labelB.pop();
|
|
jmp(B);
|
|
Label F = labelF.top(); labelF.pop();
|
|
L(F);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
#ifdef XBYAK32
|
|
pop(edi);
|
|
pop(esi);
|
|
pop(ebp);
|
|
#elif defined(XBYAK64_WIN)
|
|
pop(rbp);
|
|
pop(rdi);
|
|
pop(rsi);
|
|
#else
|
|
pop(r12);
|
|
pop(rbp);
|
|
pop(rbx);
|
|
#endif
|
|
ret();
|
|
}
|
|
};
|
|
|
|
void dump(const Xbyak::uint8 *code, size_t size)
|
|
{
|
|
puts("#include <stdio.h>\nstatic int stack[128 * 1024];");
|
|
#ifdef _MSC_VER
|
|
printf("static __declspec(align(4096)) ");
|
|
#else
|
|
printf("static __attribute__((aligned(4096)))");
|
|
#endif
|
|
puts("const unsigned char code[] = {");
|
|
for (size_t i = 0; i < size; i++) {
|
|
printf("0x%02x,", code[i]); if ((i % 16) == 15) putchar('\n');
|
|
}
|
|
puts("\n};");
|
|
#ifdef _MSC_VER
|
|
puts("#include <windows.h>");
|
|
#else
|
|
puts("#include <unistd.h>");
|
|
puts("#include <sys/mman.h>");
|
|
#endif
|
|
puts("int main()\n{");
|
|
#ifdef _MSC_VER
|
|
puts("\tDWORD oldProtect;");
|
|
puts("\tVirtualProtect((void*)code, sizeof(code), PAGE_EXECUTE_READWRITE, &oldProtect);");
|
|
#else
|
|
puts("\tlong pageSize = sysconf(_SC_PAGESIZE) - 1;");
|
|
puts("\tmprotect((void*)code, (sizeof(code) + pageSize) & ~pageSize, PROT_READ | PROT_EXEC);");
|
|
#endif
|
|
puts(
|
|
"\t((void (*)(void*, void*, int *))code)((void*)putchar, (void*)getchar, stack);\n"
|
|
"}"
|
|
);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
#ifdef XBYAK32
|
|
fprintf(stderr, "32bit mode\n");
|
|
#else
|
|
fprintf(stderr, "64bit mode\n");
|
|
#endif
|
|
if (argc == 1) {
|
|
fprintf(stderr, "bf filename.bf [0|1]\n");
|
|
return 1;
|
|
}
|
|
std::ifstream ifs(argv[1]);
|
|
int mode = argc == 3 ? atoi(argv[2]) : 0;
|
|
try {
|
|
Brainfuck bf(ifs);
|
|
if (mode == 0) {
|
|
static int stack[128 * 1024];
|
|
bf.getCode<void (*)(const void*, const void*, int *)>()(reinterpret_cast<const void*>(putchar), reinterpret_cast<const void*>(getchar), stack);
|
|
} else {
|
|
dump(bf.getCode(), bf.getSize());
|
|
}
|
|
} catch (std::exception& e) {
|
|
printf("ERR:%s\n", e.what());
|
|
} catch (...) {
|
|
printf("unknown error\n");
|
|
}
|
|
}
|
|
|