You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vm.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef L2_VM_H
  2. #define L2_VM_H
  3. #include <stdlib.h>
  4. #include "../bytecode.h"
  5. #include "../bitset.h"
  6. #include "../io.h"
  7. struct l2_vm;
  8. struct l2_vm_array;
  9. typedef l2_word (*l2_vm_cfunction)(struct l2_vm *vm, l2_word argc, l2_word *argv);
  10. typedef l2_word (*l2_vm_contcallback)(struct l2_vm *vm, l2_word retval, l2_word cont);
  11. typedef void (*l2_vm_gcmarker)(
  12. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id));
  13. enum l2_value_type {
  14. L2_VAL_TYPE_NONE,
  15. L2_VAL_TYPE_ATOM,
  16. L2_VAL_TYPE_REAL,
  17. L2_VAL_TYPE_BUFFER,
  18. L2_VAL_TYPE_ARRAY,
  19. L2_VAL_TYPE_NAMESPACE,
  20. L2_VAL_TYPE_FUNCTION,
  21. L2_VAL_TYPE_CFUNCTION,
  22. L2_VAL_TYPE_CONTINUATION,
  23. L2_VAL_TYPE_ERROR,
  24. };
  25. const char *l2_value_type_name(enum l2_value_type typ);
  26. enum l2_value_flags {
  27. L2_VAL_MARKED = 1 << 7,
  28. L2_VAL_CONST = 1 << 6,
  29. L2_VAL_CONT_CALLBACK = 1 << 6, // Re-use the const bit
  30. L2_VAL_SBO = 1 << 5,
  31. };
  32. struct l2_vm_contcontext {
  33. l2_vm_contcallback callback;
  34. l2_vm_gcmarker marker;
  35. };
  36. // The smallest size an l2_vm_value can be is 16 bytes on common platforms.
  37. // Pointers are 8 bytes, and since we need to store _at least_ 1 pointer +
  38. // 1 byte for flags, it's going to be padded up to 16 bytes anyways.
  39. // Might as well store some useful extra info in here.
  40. struct l2_vm_value {
  41. // Byte 0: 4 bytes
  42. union {
  43. l2_word ns_parent;
  44. l2_word cont_call;
  45. l2_word buf_length;
  46. l2_word arr_length;
  47. } extra;
  48. // Byte 4: 1 byte, 3 bytes padding
  49. uint8_t flags;
  50. // Byte 8: 8 bytes
  51. union {
  52. l2_word atom;
  53. double real;
  54. char *buffer;
  55. struct l2_vm_array *array;
  56. l2_word shortarray[2];
  57. struct l2_vm_namespace *ns;
  58. struct {
  59. l2_word pos;
  60. l2_word ns;
  61. } func;
  62. l2_vm_cfunction cfunc;
  63. struct l2_vm_contcontext *cont;
  64. char *error;
  65. };
  66. };
  67. #define l2_value_get_type(val) ((enum l2_value_type)((val)->flags & 0x0f))
  68. l2_word l2_value_arr_get(struct l2_vm *vm, struct l2_vm_value *val, l2_word k);
  69. l2_word l2_value_arr_set(struct l2_vm *vm, struct l2_vm_value *val, l2_word k, l2_word v);
  70. struct l2_vm_array {
  71. size_t size;
  72. l2_word data[];
  73. };
  74. struct l2_vm_namespace {
  75. size_t len;
  76. size_t size;
  77. l2_word mask;
  78. l2_word data[];
  79. };
  80. struct l2_vm_stack_frame {
  81. l2_word ns;
  82. l2_word sptr;
  83. l2_word retptr;
  84. };
  85. l2_word l2_vm_namespace_get(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key);
  86. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  87. int l2_vm_namespace_replace(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key, l2_word val);
  88. struct l2_vm {
  89. int halted;
  90. int gc_scheduled;
  91. unsigned char *ops;
  92. size_t opslen;
  93. l2_word iptr;
  94. struct l2_io_writer *std_output;
  95. struct l2_io_writer *std_error;
  96. struct l2_vm_value *values;
  97. size_t valuessize;
  98. struct l2_bitset valueset;
  99. l2_word stack[1024];
  100. l2_word sptr;
  101. struct l2_vm_stack_frame fstack[1024];
  102. l2_word fsptr;
  103. l2_word knone, ktrue, kfalse;
  104. l2_word gc_start;
  105. };
  106. void l2_vm_init(struct l2_vm *vm, unsigned char *ops, size_t opslen);
  107. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags);
  108. l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...);
  109. l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val);
  110. void l2_vm_free(struct l2_vm *vm);
  111. void l2_vm_step(struct l2_vm *vm);
  112. void l2_vm_run(struct l2_vm *vm);
  113. size_t l2_vm_gc(struct l2_vm *vm);
  114. int l2_vm_val_is_true(struct l2_vm *vm, l2_word id);
  115. #endif