compile_vector.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. jit_word_t vec_get(Cell* vec, jit_word_t position) {
  2. if (!vec || (vec->tag!=TAG_STR && vec->tag!=TAG_BYTES)) return 0;
  3. if (position>=vec->size || position<0) return 0;
  4. return ((uint8_t*)vec->addr)[position];
  5. }
  6. jit_word_t vec_put(Cell* vec, jit_word_t position, jit_word_t value) {
  7. if (!vec || (vec->tag!=TAG_STR && vec->tag!=TAG_BYTES)) return 0;
  8. if (position>=vec->size || position<0) return 0;
  9. uint8_t v = (uint8_t)value;
  10. ((uint8_t*)vec->addr)[position] = v;
  11. return v;
  12. }
  13. // vectors/strings
  14. int compile_get(int retreg, Cell* args, int requires) {
  15. if (!car(args) || !car(cdr(args))) return argnum_error("(get bytes-or-string index)");
  16. Cell* arg = car(args);
  17. // TODO: tag + bounds check
  18. compile_arg(JIT_R0, arg, TAG_ANY);
  19. stack_push(JIT_R0, &stack_ptr);
  20. compile_arg(JIT_R1, car(cdr(args)), TAG_PURE_INT);
  21. stack_pop(JIT_R0, &stack_ptr);
  22. jit_prepare();
  23. jit_pushargr(JIT_R0);
  24. jit_pushargr(JIT_R1);
  25. jit_finishi(vec_get);
  26. jit_retval(retreg);
  27. // fetch size
  28. // jit_ldxi(JIT_R2, JIT_R0, sizeof(jit_word_t));
  29. // jit_node_t* jump = jit_bltr(JIT_R2, JIT_R1);
  30. //jit_ldr(JIT_R0, JIT_R0); // car r0 = r0->addr
  31. //jit_ldxr_uc(JIT_R0, JIT_R0, JIT_R1); // *(r0 + r1) -> r0
  32. return box_int(retreg, requires);
  33. }
  34. int compile_put(int retreg, Cell* args, int requires) {
  35. if (!car(args) || !car(cdr(args))) return argnum_error("(put bytes-or-string index value)");
  36. Cell* arg = car(args);
  37. // TODO: tag + bounds check
  38. // TODO: optimize
  39. compile_arg(JIT_R0, car(cdr(cdr(args))), TAG_PURE_INT);
  40. stack_push(JIT_R0, &stack_ptr);
  41. compile_arg(JIT_R0, car(cdr(args)), TAG_PURE_INT);
  42. stack_push(JIT_R0, &stack_ptr);
  43. compile_arg(JIT_R0, arg, TAG_ANY);
  44. jit_prepare();
  45. jit_pushargr(JIT_R0);
  46. stack_pop(JIT_R0, &stack_ptr);
  47. jit_pushargr(JIT_R0);
  48. stack_pop(JIT_R0, &stack_ptr);
  49. jit_pushargr(JIT_R0);
  50. jit_finishi(vec_put);
  51. jit_retval(retreg);
  52. /*jit_ldr(JIT_R0, JIT_R0); // car r0 = r0->addr
  53. jit_addr(JIT_R0, JIT_R0, JIT_R1);
  54. compile_arg(JIT_R1, car(cdr(cdr(args))), 1, 1);
  55. jit_str_c(JIT_R0, JIT_R1); // *(r0 + r1) -> r0 */
  56. return box_int(retreg, requires);
  57. }
  58. int compile_size(int retreg, Cell* args, int requires) {
  59. if (!car(args)) return argnum_error("(size bytes/string)");
  60. Cell* arg = car(args);
  61. printf("++ potential crash: unsafe (size)\n");
  62. // FIXME: will crash with NULL
  63. compile_arg(JIT_R0, arg, TAG_ANY);
  64. jit_ldxi(retreg, JIT_R0, sizeof(jit_word_t)); // cdr r0 = r0 + one word = r0->next
  65. return box_int(retreg, requires);
  66. }
  67. // utf8 char get
  68. int compile_uget(int retreg, Cell* args, int requires) {
  69. if (!car(args) || !car(cdr(args))) return argnum_error("(uget string index)");
  70. Cell* arg = car(args);
  71. compile_arg(JIT_R0, arg, TAG_ANY);
  72. stack_push(JIT_R0, &stack_ptr);
  73. compile_arg(JIT_R0, car(cdr(args)), TAG_ANY);
  74. stack_push(JIT_R0, &stack_ptr);
  75. jit_prepare();
  76. stack_pop(JIT_R1, &stack_ptr);
  77. stack_pop(JIT_R0, &stack_ptr);
  78. jit_pushargr(JIT_R0);
  79. jit_pushargr(JIT_R1);
  80. jit_finishi(utf8_rune_at_cell);
  81. jit_retval(retreg);
  82. return box_int(retreg, requires);
  83. }
  84. // utf8 char put
  85. int compile_uput(int retreg, Cell* args, int requires) {
  86. if (!car(args) || !car(cdr(args))) return argnum_error("(uput string index rune)");
  87. Cell* arg = car(args);
  88. compile_arg(JIT_R0, car(cdr(cdr(args))), TAG_ANY);
  89. stack_push(JIT_R0, &stack_ptr);
  90. compile_arg(JIT_R0, car(cdr(args)), TAG_ANY);
  91. stack_push(JIT_R0, &stack_ptr);
  92. compile_arg(JIT_R0, arg, TAG_ANY);
  93. jit_prepare();
  94. jit_pushargr(JIT_R0);
  95. stack_pop(JIT_R0, &stack_ptr);
  96. jit_pushargr(JIT_R0);
  97. stack_pop(JIT_R0, &stack_ptr);
  98. jit_pushargr(JIT_R0);
  99. jit_finishi(utf8_put_rune_at); // checks tag + bounds
  100. jit_retval(retreg);
  101. return box_int(retreg, requires);
  102. }
  103. // utf8 string length
  104. int compile_usize(int retreg, Cell* args, int requires) {
  105. if (!car(args)) return argnum_error("(usize string)");
  106. Cell* arg = car(args);
  107. compile_arg(JIT_R0, arg, TAG_ANY);
  108. jit_prepare();
  109. jit_pushargr(JIT_R0);
  110. jit_finishi(utf8_strlen_cell); // this checks the tag
  111. jit_retval(retreg);
  112. return box_int(retreg, requires);
  113. }