compiler_arm_hosted.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #define CODESZ 8192
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/mman.h>
  5. #define DEBUG
  6. int compile_for_platform(Cell* expr, Cell** res) {
  7. code = mmap(0, CODESZ, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  8. memset(code, 0, CODESZ);
  9. jit_init(0x200); // FIXME
  10. //cpool_idx = CODESZ/2; // 128 ops gap
  11. //code_idx = 0;
  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. int tag = compile_expr(expr, empty_frame, prototype_any);
  20. jit_ret();
  21. FILE* f = fopen("/tmp/test","w");
  22. fwrite(code, CODESZ, 1, f);
  23. fclose(f);
  24. // disassemble
  25. #ifdef DEBUG
  26. system("objdump -D -b binary -m aarch64 /tmp/test >/tmp/disasm");
  27. int fd = open("/tmp/disasm",O_RDONLY);
  28. char buf[1024];
  29. int buflen;
  30. while((buflen = read(fd, buf, 1024)) > 0)
  31. {
  32. write(1, buf, buflen);
  33. }
  34. close(fd);
  35. #endif
  36. int mp_res = mprotect(code, CODESZ, PROT_EXEC|PROT_READ);
  37. funcptr fn = (funcptr)code;
  38. *res = (Cell*)fn();
  39. //printf("pointer result: %p\n",*res);
  40. //printf("pointer value: %p\n",((Cell*)*res)->value);
  41. return 1;
  42. }