compile_eval.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. jit_word_t compile_compile(Cell* expr) {
  2. if (!expr) return (jit_word_t)alloc_nil();
  3. if (expr->tag!=TAG_STR && expr->tag!=TAG_BYTES) return (jit_word_t)alloc_error(ERR_INVALID_PARAM_TYPE);
  4. Cell* read_expr = read_string(expr->addr);
  5. if (!read_expr) return (jit_word_t)alloc_error(ERR_APPLY_NIL);
  6. int done = 0;
  7. int step_by_step = 0;
  8. Cell* compile_expr = read_expr;
  9. if (read_expr->tag == TAG_CONS && car(read_expr)->tag == TAG_CONS) {
  10. printf("-- eval top-level list, compiling step-by-step\r\n");
  11. /*char* buf = malloc(20000);
  12. memset(buf,0,20000);
  13. lisp_write(compile_expr, buf, 19999);
  14. printf("~~ %s ~~\r\n",buf);*/
  15. step_by_step = 1;
  16. }
  17. Cell* exec_res = NULL;
  18. while (!done) {
  19. if (step_by_step) {
  20. compile_expr = car(read_expr);
  21. /*static char buf[1024];
  22. memset(buf,0,1024);
  23. lisp_write(compile_expr, buf, 1023);
  24. printf("~~ s-b-s compiling %s\r\n",buf);*/
  25. }
  26. if (compile_expr) {
  27. Cell* res = alloc_lambda(alloc_nil());
  28. push_jit_state();
  29. _jit = jit_new_state();
  30. jit_node_t* fn_label = jit_label();
  31. jit_prolog();
  32. //stack_ptr = stack_base = jit_allocai(32 * sizeof(jit_word_t));
  33. jit_node_t* fn_body_label = jit_label();
  34. int success = compile_arg(JIT_R0, compile_expr, TAG_ANY);
  35. if (success) {
  36. jit_retr(JIT_R0);
  37. jit_epilog();
  38. res->next = jit_emit();
  39. }
  40. jit_clear_state();
  41. //jit_destroy_state();
  42. pop_jit_state();
  43. if (success) {
  44. funcptr fn = (funcptr)res->next;
  45. if (fn) {
  46. //printf("would jump to fn %p\r\n",fn);
  47. exec_res = (Cell*)fn();
  48. }
  49. } else {
  50. return 0;
  51. }
  52. } else {
  53. //done = 1;
  54. }
  55. if (step_by_step) {
  56. read_expr = cdr(read_expr);
  57. if (!read_expr) done = 1;
  58. } else {
  59. done = 1;
  60. }
  61. }
  62. return (jit_word_t)exec_res;
  63. }
  64. typedef jit_word_t (*funcptr)();
  65. // eval
  66. int compile_eval(int retreg, Cell* args, tag_t requires) {
  67. Cell* arg = car(args);
  68. if (!arg) return argnum_error("(eval string)");
  69. compile_arg(JIT_R0, arg, TAG_ANY);
  70. // compile s-exp
  71. jit_prepare();
  72. jit_pushargr(JIT_R0);
  73. jit_finishi(compile_compile);
  74. jit_retval(retreg);
  75. return 1;
  76. }