test_opcodes.c 675 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include "fvm.h"
  4. char code[4096];
  5. char stack[4096];
  6. int code_cursor = 0;
  7. struct fvm vm;
  8. static void reset() {
  9. vm.pc = (int64_t)code;
  10. vm.sp = (int64_t)stack + 4095;
  11. vm.bp = (int64_t)stack + 4095;
  12. code_cursor = 0;
  13. }
  14. static void emit_op(enum fvm_op op) {
  15. code[code_cursor] = (char)op;
  16. code_cursor++;
  17. }
  18. static void emit_num(int64_t x) {
  19. *(int64_t*)(code + code_cursor) = x;
  20. code_cursor += 8;
  21. }
  22. void test_imm() {
  23. printf("[TEST] imm\n");
  24. reset();
  25. emit_op(FVM_OP_IMM);
  26. emit_num(3);
  27. emit_op(FVM_OP_EXIT);
  28. assert(fvm_execute(&vm) == 3);
  29. printf("[PASS] imm\n");
  30. }
  31. int main()
  32. {
  33. test_imm();
  34. return 0;
  35. }