compiler_arm_hosted.c 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #define CODESZ 8192 // FIXME
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/mman.h>
  5. Cell* execute_jitted(void* binary) {
  6. return (Cell*)((funcptr)binary)(0);
  7. }
  8. int compile_for_platform(Cell* expr, Cell** res) {
  9. code = mmap(0, CODESZ, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  10. memset(code, 0, CODESZ);
  11. jit_init(0x400); // constant pool size
  12. register void* sp asm ("sp");
  13. Frame* empty_frame = malloc(sizeof(Frame)); // FIXME leak
  14. empty_frame->f=NULL;
  15. empty_frame->sp=0;
  16. empty_frame->locals=0;
  17. empty_frame->stack_end=sp;
  18. empty_frame->parent_frame=NULL;
  19. Cell* success = compile_expr(expr, empty_frame, prototype_any);
  20. jit_ret();
  21. if (!success) {
  22. printf("<compile_expr failed: %p>\r\n",success);
  23. } else {
  24. int mp_res = mprotect(code, CODESZ, PROT_EXEC|PROT_READ);
  25. if (!mp_res) {
  26. *res = execute_jitted(code);
  27. } else {
  28. printf("<mprotect result: %d\n>",mp_res);
  29. *res = NULL;
  30. success = 0;
  31. }
  32. }
  33. return !!success;
  34. }