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

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