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. l2_word args;
  36. };
  37. // The smallest size an l2_vm_value can be is 16 bytes on common platforms.
  38. // Pointers are 8 bytes, and since we need to store _at least_ 1 pointer +
  39. // 1 byte for flags, it's going to be padded up to 16 bytes anyways.
  40. // Might as well store some useful extra info in here.
  41. struct l2_vm_value {
  42. // Byte 0: 4 bytes
  43. union {
  44. l2_word ns_parent;
  45. l2_word cont_call;
  46. l2_word buf_length;
  47. l2_word arr_length;
  48. } extra;
  49. // Byte 4: 1 byte, 3 bytes padding
  50. uint8_t flags;
  51. // Byte 8: 8 bytes
  52. union {
  53. l2_word atom;
  54. double real;
  55. char *buffer;
  56. struct l2_vm_array *array;
  57. l2_word shortarray[2];
  58. struct l2_vm_namespace *ns;
  59. struct {
  60. l2_word pos;
  61. l2_word ns;
  62. } func;
  63. l2_vm_cfunction cfunc;
  64. struct l2_vm_contcontext *cont;
  65. char *error;
  66. };
  67. };
  68. #define l2_value_get_type(val) ((enum l2_value_type)((val)->flags & 0x0f))
  69. l2_word l2_value_arr_get(struct l2_vm *vm, struct l2_vm_value *val, l2_word k);
  70. l2_word l2_value_arr_set(struct l2_vm *vm, struct l2_vm_value *val, l2_word k, l2_word v);
  71. struct l2_vm_array {
  72. size_t size;
  73. l2_word data[];
  74. };
  75. struct l2_vm_namespace {
  76. size_t len;
  77. size_t size;
  78. l2_word mask;
  79. l2_word data[];
  80. };
  81. struct l2_vm_stack_frame {
  82. l2_word ns;
  83. l2_word sptr;
  84. l2_word retptr;
  85. };
  86. l2_word l2_vm_namespace_get(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key);
  87. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  88. int l2_vm_namespace_replace(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key, l2_word val);
  89. struct l2_vm {
  90. int halted;
  91. int gc_scheduled;
  92. unsigned char *ops;
  93. size_t opslen;
  94. l2_word iptr;
  95. struct l2_io_writer *std_output;
  96. struct l2_io_writer *std_error;
  97. struct l2_vm_value *values;
  98. size_t valuessize;
  99. struct l2_bitset valueset;
  100. l2_word stack[1024];
  101. l2_word sptr;
  102. struct l2_vm_stack_frame fstack[1024];
  103. l2_word fsptr;
  104. l2_word knone, ktrue, kfalse, kstop;
  105. l2_word gc_start;
  106. };
  107. void l2_vm_init(struct l2_vm *vm, unsigned char *ops, size_t opslen);
  108. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags);
  109. l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...);
  110. l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val);
  111. void l2_vm_free(struct l2_vm *vm);
  112. void l2_vm_step(struct l2_vm *vm);
  113. void l2_vm_run(struct l2_vm *vm);
  114. size_t l2_vm_gc(struct l2_vm *vm);
  115. int l2_vm_val_is_true(struct l2_vm *vm, struct l2_vm_value *val);
  116. #endif