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.c 655B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "vm/vm.h"
  2. void l2_vm_init(struct l2_vm *vm, struct l2_op *ops, size_t opcount) {
  3. vm->ops = ops;
  4. vm->opcount = opcount;
  5. vm->iptr = 0;
  6. vm->sptr = 0;
  7. }
  8. static l2_word alloc(struct l2_vm *vm, size_t size) {
  9. l2_word id = vm->allocslen++;
  10. vm->allocs[id] = malloc(size);
  11. return id | 1 << 31;
  12. }
  13. void l2_vm_step(struct l2_vm *vm) {
  14. struct l2_op op = vm->ops[vm->iptr++];
  15. switch (op.code) {
  16. case L2_OP_PUSH:
  17. vm->stack[vm->sptr++] = op.val;
  18. break;
  19. case L2_OP_ADD:
  20. vm->sptr -= 1;
  21. vm->stack[vm->sptr - 1] += vm->stack[vm->sptr];
  22. break;
  23. case L2_OP_JUMP:
  24. vm->iptr = vm->stack[--vm->sptr];
  25. break;
  26. case L2_OP_HALT:
  27. break;
  28. }
  29. }