compiler_arm_hosted.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #define CODESZ 8192
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/mman.h>
  5. int compile_for_platform(Cell* expr, Cell** res) {
  6. code = mmap(0, CODESZ, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  7. memset(code, 0, CODESZ);
  8. jit_init(0x400);
  9. //cpool_idx = CODESZ/2; // 128 ops gap
  10. //code_idx = 0;
  11. register void* sp asm ("sp"); // FIXME maybe unportable
  12. Frame empty_frame = {NULL, 0, 0, sp};
  13. int tag = compile_expr(expr, &empty_frame, TAG_ANY);
  14. jit_ret();
  15. FILE* f = fopen("/tmp/test","w");
  16. fwrite(code, CODESZ, 1, f);
  17. fclose(f);
  18. // disassemble
  19. #ifdef DEBUG
  20. system("arm-linux-gnueabihf-objdump -D -b binary -marmv5 /tmp/test >/tmp/disasm");
  21. int fd = open("/tmp/disasm",O_RDONLY);
  22. char buf[1024];
  23. int buflen;
  24. while((buflen = read(fd, buf, 1024)) > 0)
  25. {
  26. write(1, buf, buflen);
  27. }
  28. close(fd);
  29. #endif
  30. int mp_res = mprotect(code, CODESZ, PROT_EXEC|PROT_READ);
  31. funcptr fn = (funcptr)code;
  32. *res = (Cell*)fn();
  33. //printf("pointer result: %p\n",*res);
  34. //printf("pointer value: %p\n",((Cell*)*res)->value);
  35. return 1;
  36. }