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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef L2_VM_H
  2. #define L2_VM_H
  3. #include <stdlib.h>
  4. #include "../bytecode.h"
  5. #include "../bitset.h"
  6. struct l2_vm_value {
  7. enum l2_value_flags {
  8. L2_VAL_TYPE_NONE,
  9. L2_VAL_TYPE_INTEGER,
  10. L2_VAL_TYPE_REAL,
  11. L2_VAL_TYPE_BUFFER,
  12. L2_VAL_TYPE_ARRAY,
  13. L2_VAL_TYPE_NAMESPACE,
  14. L2_VAL_MARKED = 1 << 7,
  15. L2_VAL_CONST = 1 << 8,
  16. } flags;
  17. union {
  18. int64_t integer;
  19. double real;
  20. void *data;
  21. };
  22. };
  23. #define l2_vm_value_type(val) ((val).flags & 0x0f)
  24. struct l2_vm_buffer {
  25. size_t len;
  26. char data[];
  27. };
  28. struct l2_vm_array {
  29. size_t len;
  30. size_t size;
  31. l2_word data[];
  32. };
  33. struct l2_vm_namespace {
  34. struct l2_vm_value *parent;
  35. size_t len;
  36. size_t size;
  37. l2_word mask;
  38. l2_word data[];
  39. };
  40. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  41. l2_word l2_vm_namespace_get(struct l2_vm_value *ns, l2_word key);
  42. struct l2_vm {
  43. l2_word *ops;
  44. size_t opcount;
  45. l2_word iptr;
  46. struct l2_vm_value *values;
  47. size_t valuessize;
  48. struct l2_bitset valueset;
  49. l2_word stack[1024];
  50. l2_word sptr;
  51. l2_word nstack[1024];
  52. l2_word nsptr;
  53. };
  54. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount);
  55. void l2_vm_free(struct l2_vm *vm);
  56. void l2_vm_step(struct l2_vm *vm);
  57. void l2_vm_run(struct l2_vm *vm);
  58. size_t l2_vm_gc(struct l2_vm *vm);
  59. #endif