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

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