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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. } extra;
  45. // Byte 4: 1 byte, 3 bytes padding
  46. uint8_t flags;
  47. // Byte 8: 8 bytes
  48. union {
  49. l2_word atom;
  50. double real;
  51. struct l2_vm_buffer *buffer;
  52. struct l2_vm_array *array;
  53. struct l2_vm_namespace *ns;
  54. struct {
  55. l2_word pos;
  56. l2_word ns;
  57. } func;
  58. l2_vm_cfunction cfunc;
  59. struct l2_vm_contcontext *cont;
  60. char *error;
  61. };
  62. };
  63. #define l2_vm_value_type(val) ((enum l2_value_type)((val)->flags & 0x0f))
  64. struct l2_vm_buffer {
  65. size_t len;
  66. char data[];
  67. };
  68. struct l2_vm_array {
  69. size_t len;
  70. size_t size;
  71. l2_word data[];
  72. };
  73. struct l2_vm_namespace {
  74. size_t len;
  75. size_t size;
  76. l2_word mask;
  77. l2_word data[];
  78. };
  79. struct l2_vm_stack_frame {
  80. l2_word ns;
  81. l2_word sptr;
  82. l2_word retptr;
  83. };
  84. l2_word l2_vm_namespace_get(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key);
  85. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  86. int l2_vm_namespace_replace(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key, l2_word val);
  87. struct l2_vm {
  88. int halted;
  89. int gc_scheduled;
  90. unsigned char *ops;
  91. size_t opslen;
  92. l2_word iptr;
  93. struct l2_io_writer *std_output;
  94. struct l2_io_writer *std_error;
  95. struct l2_vm_value *values;
  96. size_t valuessize;
  97. struct l2_bitset valueset;
  98. l2_word stack[1024];
  99. l2_word sptr;
  100. struct l2_vm_stack_frame fstack[1024];
  101. l2_word fsptr;
  102. l2_word knone, ktrue, kfalse;
  103. l2_word gc_start;
  104. };
  105. void l2_vm_init(struct l2_vm *vm, unsigned char *ops, size_t opslen);
  106. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags);
  107. l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...);
  108. l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val);
  109. void l2_vm_free(struct l2_vm *vm);
  110. void l2_vm_step(struct l2_vm *vm);
  111. void l2_vm_run(struct l2_vm *vm);
  112. size_t l2_vm_gc(struct l2_vm *vm);
  113. #endif