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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. enum l2_value_type {
  10. L2_VAL_TYPE_NONE,
  11. L2_VAL_TYPE_INTEGER,
  12. L2_VAL_TYPE_REAL,
  13. L2_VAL_TYPE_BUFFER,
  14. L2_VAL_TYPE_ARRAY,
  15. L2_VAL_TYPE_NAMESPACE,
  16. L2_VAL_TYPE_FUNCTION,
  17. L2_VAL_TYPE_CFUNCTION,
  18. };
  19. enum l2_value_flags {
  20. L2_VAL_MARKED = 1 << 6,
  21. L2_VAL_CONST = 1 << 7,
  22. };
  23. // The smallest size an l2_vm_value can be is 16 bytes on common platforms.
  24. // Pointers are 8 bytes, and since we need to store _at least_ 1 pointer +
  25. // 1 byte for flags, it's going to be padded up to 16 bytes anyways.
  26. // Might as well store some useful extra info in here.
  27. struct l2_vm_value {
  28. // Byte 0: 4 bytes
  29. union {
  30. l2_word ns_parent;
  31. } extra;
  32. // Byte 4: 1 byte, 3 bytes padding
  33. uint8_t flags;
  34. // Byte 8: 8 bytes
  35. union {
  36. int64_t integer;
  37. double real;
  38. struct l2_vm_buffer *buffer;
  39. struct l2_vm_array *array;
  40. struct l2_vm_namespace *ns;
  41. struct {
  42. l2_word pos;
  43. l2_word namespace;
  44. } func;
  45. l2_vm_cfunction cfunc;
  46. };
  47. };
  48. #define l2_vm_value_type(val) ((enum l2_value_type)((val)->flags & 0x0f))
  49. struct l2_vm_buffer {
  50. size_t len;
  51. char data[];
  52. };
  53. struct l2_vm_array {
  54. size_t len;
  55. size_t size;
  56. l2_word data[];
  57. };
  58. struct l2_vm_namespace {
  59. size_t len;
  60. size_t size;
  61. l2_word mask;
  62. l2_word data[];
  63. };
  64. void l2_vm_namespace_set(struct l2_vm_value *ns, l2_word key, l2_word val);
  65. l2_word l2_vm_namespace_get(struct l2_vm *vm, struct l2_vm_value *ns, l2_word key);
  66. struct l2_vm {
  67. l2_word *ops;
  68. size_t opcount;
  69. l2_word iptr;
  70. struct l2_vm_value *values;
  71. size_t valuessize;
  72. struct l2_bitset valueset;
  73. l2_word stack[1024];
  74. l2_word sptr;
  75. l2_word nstack[1024];
  76. l2_word nsptr;
  77. };
  78. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount);
  79. void l2_vm_free(struct l2_vm *vm);
  80. void l2_vm_step(struct l2_vm *vm);
  81. void l2_vm_run(struct l2_vm *vm);
  82. size_t l2_vm_gc(struct l2_vm *vm);
  83. #endif