cc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2020 deesix <deesix@tuta.io>
  3. * This file is part of M2-Planet.
  4. *
  5. * M2-Planet is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * M2-Planet is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include<stdlib.h>
  19. #include<stdio.h>
  20. #include<string.h>
  21. #include"cc.h"
  22. /* The core functions */
  23. void initialize_types();
  24. struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename);
  25. struct token_list* reverse_list(struct token_list* head);
  26. void program();
  27. void recursive_output(struct token_list* i, FILE* out);
  28. int numerate_string(char *a);
  29. int main(int argc, char** argv)
  30. {
  31. MAX_STRING = 4096;
  32. BOOTSTRAP_MODE = FALSE;
  33. int DEBUG = FALSE;
  34. FILE* in = stdin;
  35. FILE* destination_file = stdout;
  36. Architecture = KNIGHT_NATIVE; /* Assume Knight-native */
  37. char* arch;
  38. char* name;
  39. int i = 1;
  40. while(i <= argc)
  41. {
  42. if(NULL == argv[i])
  43. {
  44. i = i + 1;
  45. }
  46. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  47. {
  48. if(NULL == hold_string)
  49. {
  50. hold_string = calloc(MAX_STRING, sizeof(char));
  51. require(NULL != hold_string, "Impossible Exhustion has occured\n");
  52. }
  53. name = argv[i + 1];
  54. in = fopen(name, "r");
  55. if(NULL == in)
  56. {
  57. file_print("Unable to open for reading file: ", stderr);
  58. file_print(name, stderr);
  59. file_print("\n Aborting to avoid problems\n", stderr);
  60. exit(EXIT_FAILURE);
  61. }
  62. global_token = read_all_tokens(in, global_token, name);
  63. fclose(in);
  64. i = i + 2;
  65. }
  66. else if(match(argv[i], "-o") || match(argv[i], "--output"))
  67. {
  68. destination_file = fopen(argv[i + 1], "w");
  69. if(NULL == destination_file)
  70. {
  71. file_print("Unable to open for writing file: ", stderr);
  72. file_print(argv[i + 1], stderr);
  73. file_print("\n Aborting to avoid problems\n", stderr);
  74. exit(EXIT_FAILURE);
  75. }
  76. i = i + 2;
  77. }
  78. else if(match(argv[i], "-A") || match(argv[i], "--architecture"))
  79. {
  80. arch = argv[i + 1];
  81. if(match("knight-native", arch)) Architecture = KNIGHT_NATIVE;
  82. else if(match("knight-posix", arch)) Architecture = KNIGHT_POSIX;
  83. else if(match("x86", arch)) Architecture = X86;
  84. else if(match("amd64", arch)) Architecture = AMD64;
  85. else if(match("armv7l", arch)) Architecture = ARMV7L;
  86. else if(match("aarch64", arch)) Architecture = AARCH64;
  87. else
  88. {
  89. file_print("Unknown architecture: ", stderr);
  90. file_print(arch, stderr);
  91. file_print(" know values are: knight-native, knight-posix, x86, amd64, armv7l and aarch64", stderr);
  92. exit(EXIT_FAILURE);
  93. }
  94. i = i + 2;
  95. }
  96. else if(match(argv[i], "--max-string"))
  97. {
  98. MAX_STRING = numerate_string(argv[i+1]);
  99. require(0 < MAX_STRING, "Not a valid string size\nAbort and fix your --max-string\n");
  100. i = i + 2;
  101. }
  102. else if(match(argv[i], "--bootstrap-mode"))
  103. {
  104. BOOTSTRAP_MODE = TRUE;
  105. i = i + 1;
  106. }
  107. else if(match(argv[i], "-g") || match(argv[i], "--debug"))
  108. {
  109. DEBUG = TRUE;
  110. i = i + 1;
  111. }
  112. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  113. {
  114. file_print(" -f input file\n -o output file\n --help for this message\n --version for file version\n", stdout);
  115. exit(EXIT_SUCCESS);
  116. }
  117. else if(match(argv[i], "-V") || match(argv[i], "--version"))
  118. {
  119. file_print("M2-Planet v1.7.0\n", stderr);
  120. exit(EXIT_SUCCESS);
  121. }
  122. else
  123. {
  124. file_print("UNKNOWN ARGUMENT\n", stdout);
  125. exit(EXIT_FAILURE);
  126. }
  127. }
  128. /* Deal with special case of wanting to read from standard input */
  129. if(stdin == in)
  130. {
  131. hold_string = calloc(MAX_STRING, sizeof(char));
  132. require(NULL != hold_string, "Impossible Exhustion has occured\n");
  133. global_token = read_all_tokens(in, global_token, "STDIN");
  134. }
  135. if(NULL == global_token)
  136. {
  137. file_print("Either no input files were given or they were empty\n", stderr);
  138. exit(EXIT_FAILURE);
  139. }
  140. global_token = reverse_list(global_token);
  141. initialize_types();
  142. reset_hold_string();
  143. output_list = NULL;
  144. program();
  145. /* Output the program we have compiled */
  146. file_print("\n# Core program\n", destination_file);
  147. recursive_output(output_list, destination_file);
  148. if(KNIGHT_NATIVE == Architecture) file_print("\n", destination_file);
  149. else if(DEBUG) file_print("\n:ELF_data\n", destination_file);
  150. file_print("\n# Program global variables\n", destination_file);
  151. recursive_output(globals_list, destination_file);
  152. file_print("\n# Program strings\n", destination_file);
  153. recursive_output(strings_list, destination_file);
  154. if(KNIGHT_NATIVE == Architecture) file_print("\n:STACK\n", destination_file);
  155. else if(!DEBUG) file_print("\n:ELF_end\n", destination_file);
  156. if (destination_file != stdout)
  157. {
  158. fclose(destination_file);
  159. }
  160. return EXIT_SUCCESS;
  161. }