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

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