compiler_arm_hosted.c 1.0 KB

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