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 2.6KB

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