Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bf.cpp File Reference
#include "xbyak/xbyak.h"
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <fstream>
Include dependency graph for bf.cpp:

Go to the source code of this file.

Classes

class  Brainfuck
 

Macros

#define XBYAK_NO_OP_NAMES
 

Functions

void dump (const Xbyak::uint8 *code, size_t size)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ XBYAK_NO_OP_NAMES

#define XBYAK_NO_OP_NAMES

Definition at line 1 of file bf.cpp.

Function Documentation

◆ dump()

void dump ( const Xbyak::uint8 * code,
size_t size )

Definition at line 153 of file bf.cpp.

154{
155 puts("#include <stdio.h>\nstatic int stack[128 * 1024];");
156#ifdef _MSC_VER
157 printf("static __declspec(align(4096)) ");
158#else
159 printf("static __attribute__((aligned(4096)))");
160#endif
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');
164 }
165 puts("\n};");
166#ifdef _MSC_VER
167 puts("#include <windows.h>");
168#else
169 puts("#include <unistd.h>");
170 puts("#include <sys/mman.h>");
171#endif
172 puts("int main()\n{");
173#ifdef _MSC_VER
174 puts("\tDWORD oldProtect;");
175 puts("\tVirtualProtect((void*)code, sizeof(code), PAGE_EXECUTE_READWRITE, &oldProtect);");
176#else
177 puts("\tlong pageSize = sysconf(_SC_PAGESIZE) - 1;");
178 puts("\tmprotect((void*)code, (sizeof(code) + pageSize) & ~pageSize, PROT_READ | PROT_EXEC);");
179#endif
180 puts(
181 "\t((void (*)(void*, void*, int *))code)((void*)putchar, (void*)getchar, stack);\n"
182 "}"
183 );
184}
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 186 of file bf.cpp.

187{
188#ifdef XBYAK32
189 fprintf(stderr, "32bit mode\n");
190#else
191 fprintf(stderr, "64bit mode\n");
192#endif
193 if (argc == 1) {
194 fprintf(stderr, "bf filename.bf [0|1]\n");
195 return 1;
196 }
197 std::ifstream ifs(argv[1]);
198 int mode = argc == 3 ? atoi(argv[2]) : 0;
199 try {
200 Brainfuck bf(ifs);
201 if (mode == 0) {
202 static int stack[128 * 1024];
203 bf.getCode<void (*)(void*, void*, int *)>()(Xbyak::CastTo<void*>(putchar), Xbyak::CastTo<void*>(getchar), stack);
204 } else {
205 dump(bf.getCode(), bf.getSize());
206 }
207 } catch (std::exception& e) {
208 printf("ERR:%s\n", e.what());
209 } catch (...) {
210 printf("unknown error\n");
211 }
212}
void dump(const Xbyak::uint8 *code, size_t size)
Definition bf.cpp:153
char ** argv
const To CastTo(From p)
Definition xbyak.h:279
Here is the call graph for this function: