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.

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