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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_TYPE_FUNCTION,
  15. L2_VAL_MARKED = 1 << 7,
  16. L2_VAL_CONST = 1 << 8,
  17. } flags;
  18. union {
  19. int64_t integer;
  20. double real;
  21. struct {
  22. l2_word pos;
  23. l2_word namespace;
  24. } func;
  25. void *data;
  26. };
  27. };
  28. #define l2_vm_value_type(val) ((val)->flags & 0x0f)
  29. struct l2_vm_buffer {
  30. size_t len;
  31. char data[];
  32. };
  33. struct l2_vm_array {
  34. size_t len;
  35. size_t size;
  36. l2_word data[];
  37. };
  38. struct l2_vm_namespace {
  39. struct l2_vm_value *parent;
  40. size_t len;
  41. size_t size;
  42. l2_word mask;
  43. l2_word data[];
  44. };
  45. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  46. l2_word l2_vm_namespace_get(struct l2_vm_value *ns, l2_word key);
  47. struct l2_vm {
  48. l2_word *ops;
  49. size_t opcount;
  50. l2_word iptr;
  51. struct l2_vm_value *values;
  52. size_t valuessize;
  53. struct l2_bitset valueset;
  54. l2_word stack[1024];
  55. l2_word sptr;
  56. l2_word nstack[1024];
  57. l2_word nsptr;
  58. };
  59. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount);
  60. void l2_vm_free(struct l2_vm *vm);
  61. void l2_vm_step(struct l2_vm *vm);
  62. void l2_vm_run(struct l2_vm *vm);
  63. size_t l2_vm_gc(struct l2_vm *vm);
  64. #endif