compiler_x86.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef WIN32
  2. #include <sys/mman.h>
  3. #endif
  4. Cell* execute_jitted(void* binary) {
  5. return (Cell*)((funcptr)binary)(0);
  6. }
  7. //void memdump(void* start,uint32_t len,int raw);
  8. Cell* compile_for_platform(Cell* expr, Cell** res) {
  9. int codesz = 8192;
  10. #ifdef WIN32
  11. uint8_t* jit_binary = malloc(codesz);
  12. #else
  13. uint8_t* jit_binary = mmap(0, codesz, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  14. #endif
  15. printf("jit_binary: %p\r\n",jit_binary);
  16. memset(jit_binary, 0, codesz);
  17. jit_init(jit_binary, codesz);
  18. register void* sp asm ("sp");
  19. Frame* empty_frame = malloc(sizeof(Frame)); // FIXME leak
  20. empty_frame->f=NULL;
  21. empty_frame->sp=0;
  22. empty_frame->locals=0;
  23. empty_frame->stack_end=sp;
  24. empty_frame->parent_frame=NULL;
  25. Cell* success = compile_expr(expr, empty_frame, prototype_any);
  26. jit_ret();
  27. if (success) {
  28. printf("<assembled at: %p>\r\n",jit_binary);
  29. //memdump(code,64,0);
  30. //FILE* f = fopen("/tmp/jit.x86","w+");
  31. //fwrite(code, 1, codesz, f);
  32. //fclose(f);
  33. #ifndef WIN32
  34. int mp_res = mprotect(jit_binary, codesz, PROT_EXEC|PROT_READ);
  35. #endif
  36. *res = execute_jitted(jit_binary);
  37. }
  38. return success;
  39. }