main.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>
  8. #include "headers.h"
  9. #include "tags.h"
  10. #include "globals.h"
  11. #include "stack.h"
  12. #include "bytecode.h"
  13. #include "vm.h"
  14. // TESTS
  15. void t_hdr();
  16. void t_tag();
  17. void t_stk();
  18. // END TESTS
  19. #define BYTECODE_SIZE (1 << 18)
  20. int main(int argc, char **argv) {
  21. //t_hdr();
  22. //t_tag();
  23. //t_stk();
  24. char *filename;
  25. struct stat st;
  26. int fd;
  27. int code_length;
  28. if(argc != 2) {
  29. fprintf(stderr, "Usage: ./bytecode <filename>.byte\n");
  30. return -1;
  31. }
  32. filename = argv[1];
  33. fd = open(filename, O_RDONLY);
  34. if(fd == -1) {
  35. fprintf(stderr, "Could not open bytecode file %s\n", filename);
  36. return -1;
  37. }
  38. if(stat(filename, &st) == -1) {
  39. fprintf(stderr, "Could not stat bytecode file %s\n", filename);
  40. return -1;
  41. }
  42. code_length = st.st_size;
  43. if(code_length > BYTECODE_SIZE) {
  44. fprintf(stderr, "Not enough space for bytecode\n");
  45. return -1;
  46. }
  47. bytecode = mmap(NULL, BYTECODE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
  48. if(bytecode == MAP_FAILED) {
  49. fprintf(stderr, "Memory mapping bytecode failed\n");
  50. return -1;
  51. }
  52. execute();
  53. return 0;
  54. }