1#define XBYAK_NO_OP_NAMES
8 #pragma warning(disable : 4996)
9 #define snprintf _snprintf_s
14 enum Direction {
B, F };
15 std::string toStr(
int labelNo, Direction dir)
34 using namespace Xbyak;
47#elif defined(XBYAK64_WIN)
48 const Reg64& pPutchar(rsi);
49 const Reg64& pGetchar(rdi);
50 const Reg64& stack(rbp);
59 const Reg64& pPutchar(rbx);
60 const Reg64& pGetchar(rbp);
61 const Reg64& stack(r12);
71 std::stack<int> keepLabelNo;
80 c ==
'+' ?
inc(cur) :
dec(cur);
98#elif defined(XBYAK64_WIN)
109#if defined(XBYAK32) || defined(XBYAK64_GCC)
111#elif defined(XBYAK64_WIN)
119 L(toStr(labelNo,
B));
123 keepLabelNo.push(labelNo++);
127 int no = keepLabelNo.top(); keepLabelNo.pop();
140#elif defined(XBYAK64_WIN)
153void dump(
const Xbyak::uint8 *code,
size_t size)
155 puts(
"#include <stdio.h>\nstatic int stack[128 * 1024];");
157 printf(
"static __declspec(align(4096)) ");
159 printf(
"static __attribute__((aligned(4096)))");
161 puts(
"const unsigned char code[] = {");
162 for (
size_t i = 0; i < size; i++) {
163 printf(
"0x%02x,", code[i]);
if ((i % 16) == 15) putchar(
'\n');
167 puts(
"#include <windows.h>");
169 puts(
"#include <unistd.h>");
170 puts(
"#include <sys/mman.h>");
172 puts(
"int main()\n{");
174 puts(
"\tDWORD oldProtect;");
175 puts(
"\tVirtualProtect((void*)code, sizeof(code), PAGE_EXECUTE_READWRITE, &oldProtect);");
177 puts(
"\tlong pageSize = sysconf(_SC_PAGESIZE) - 1;");
178 puts(
"\tmprotect((void*)code, (sizeof(code) + pageSize) & ~pageSize, PROT_READ | PROT_EXEC);");
181 "\t((void (*)(void*, void*, int *))code)((void*)putchar, (void*)getchar, stack);\n"
189 fprintf(stderr,
"32bit mode\n");
191 fprintf(stderr,
"64bit mode\n");
194 fprintf(stderr,
"bf filename.bf [0|1]\n");
197 std::ifstream ifs(
argv[1]);
198 int mode = argc == 3 ? atoi(
argv[2]) : 0;
202 static int stack[128 * 1024];
207 }
catch (std::exception& e) {
208 printf(
"ERR:%s\n", e.what());
210 printf(
"unknown error\n");
void dump(const Xbyak::uint8 *code, size_t size)
Brainfuck(std::istream &is)
int getContinuousChar(std::istream &is, char c)
const uint8 * getCode() const
void call(const Operand &op)
void jmp(const Operand &op)
void inc(const Operand &op)
void test(const Operand &op, const Reg ®)
void add(const Operand &op, uint32 imm)
void sub(const Operand &op, uint32 imm)
void jz(const Label &label, LabelType type=T_AUTO)
void mov(const Operand ®1, const Operand ®2)
void pop(const Operand &op)
void L(const std::string &label)
void push(const Operand &op)
void dec(const Operand &op)
static std::string toStr(int num)
Xbyak ; JIT assembler for x86(IA32)/x64 by C++.