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.

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