cc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. struct token_list* remove_line_comments(struct token_list* head);
  27. struct token_list* remove_line_comment_tokens(struct token_list* head);
  28. struct token_list* remove_preprocessor_directives(struct token_list* head);
  29. void eat_newline_tokens();
  30. void preprocess();
  31. void program();
  32. void recursive_output(struct token_list* i, FILE* out);
  33. void output_tokens(struct token_list *i, FILE* out);
  34. int numerate_string(char *a);
  35. int main(int argc, char** argv)
  36. {
  37. MAX_STRING = 4096;
  38. BOOTSTRAP_MODE = FALSE;
  39. PREPROCESSOR_MODE = FALSE;
  40. int DEBUG = FALSE;
  41. FILE* in = stdin;
  42. FILE* destination_file = stdout;
  43. Architecture = KNIGHT_NATIVE; /* Assume Knight-native */
  44. char* arch;
  45. char* name;
  46. int i = 1;
  47. while(i <= argc)
  48. {
  49. if(NULL == argv[i])
  50. {
  51. i = i + 1;
  52. }
  53. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  54. {
  55. if(NULL == hold_string)
  56. {
  57. hold_string = calloc(MAX_STRING, sizeof(char));
  58. require(NULL != hold_string, "Impossible Exhustion has occured\n");
  59. }
  60. name = argv[i + 1];
  61. in = fopen(name, "r");
  62. if(NULL == in)
  63. {
  64. file_print("Unable to open for reading file: ", stderr);
  65. file_print(name, stderr);
  66. file_print("\n Aborting to avoid problems\n", stderr);
  67. exit(EXIT_FAILURE);
  68. }
  69. global_token = read_all_tokens(in, global_token, name);
  70. fclose(in);
  71. i = i + 2;
  72. }
  73. else if(match(argv[i], "-o") || match(argv[i], "--output"))
  74. {
  75. destination_file = fopen(argv[i + 1], "w");
  76. if(NULL == destination_file)
  77. {
  78. file_print("Unable to open for writing file: ", stderr);
  79. file_print(argv[i + 1], stderr);
  80. file_print("\n Aborting to avoid problems\n", stderr);
  81. exit(EXIT_FAILURE);
  82. }
  83. i = i + 2;
  84. }
  85. else if(match(argv[i], "-A") || match(argv[i], "--architecture"))
  86. {
  87. arch = argv[i + 1];
  88. if(match("knight-native", arch)) Architecture = KNIGHT_NATIVE;
  89. else if(match("knight-posix", arch)) Architecture = KNIGHT_POSIX;
  90. else if(match("x86", arch)) Architecture = X86;
  91. else if(match("amd64", arch)) Architecture = AMD64;
  92. else if(match("armv7l", arch)) Architecture = ARMV7L;
  93. else if(match("aarch64", arch)) Architecture = AARCH64;
  94. else
  95. {
  96. file_print("Unknown architecture: ", stderr);
  97. file_print(arch, stderr);
  98. file_print(" know values are: knight-native, knight-posix, x86, amd64, armv7l and aarch64", stderr);
  99. exit(EXIT_FAILURE);
  100. }
  101. i = i + 2;
  102. }
  103. else if(match(argv[i], "--max-string"))
  104. {
  105. MAX_STRING = numerate_string(argv[i+1]);
  106. require(0 < MAX_STRING, "Not a valid string size\nAbort and fix your --max-string\n");
  107. i = i + 2;
  108. }
  109. else if(match(argv[i], "--bootstrap-mode"))
  110. {
  111. BOOTSTRAP_MODE = TRUE;
  112. i = i + 1;
  113. }
  114. else if(match(argv[i], "-g") || match(argv[i], "--debug"))
  115. {
  116. DEBUG = TRUE;
  117. i = i + 1;
  118. }
  119. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  120. {
  121. file_print(" -f input file\n -o output file\n --help for this message\n --version for file version\n", stdout);
  122. exit(EXIT_SUCCESS);
  123. }
  124. else if(match(argv[i], "-E"))
  125. {
  126. PREPROCESSOR_MODE = TRUE;
  127. i = i + 1;
  128. }
  129. else if(match(argv[i], "-V") || match(argv[i], "--version"))
  130. {
  131. file_print("M2-Planet v1.7.0\n", stderr);
  132. exit(EXIT_SUCCESS);
  133. }
  134. else
  135. {
  136. file_print("UNKNOWN ARGUMENT\n", stdout);
  137. exit(EXIT_FAILURE);
  138. }
  139. }
  140. /* Deal with special case of wanting to read from standard input */
  141. if(stdin == in)
  142. {
  143. hold_string = calloc(MAX_STRING, sizeof(char));
  144. require(NULL != hold_string, "Impossible Exhustion has occured\n");
  145. global_token = read_all_tokens(in, global_token, "STDIN");
  146. }
  147. if(NULL == global_token)
  148. {
  149. file_print("Either no input files were given or they were empty\n", stderr);
  150. exit(EXIT_FAILURE);
  151. }
  152. global_token = reverse_list(global_token);
  153. if (BOOTSTRAP_MODE)
  154. {
  155. global_token = remove_line_comment_tokens(global_token);
  156. global_token = remove_preprocessor_directives(global_token);
  157. }
  158. else
  159. {
  160. global_token = remove_line_comments(global_token);
  161. preprocess();
  162. }
  163. if (PREPROCESSOR_MODE)
  164. {
  165. file_print("\n/* Preprocessed source */\n", destination_file);
  166. output_tokens(global_token, destination_file);
  167. goto exit_success;
  168. }
  169. /* the main parser doesn't know how to handle newline tokens */
  170. eat_newline_tokens();
  171. initialize_types();
  172. reset_hold_string();
  173. output_list = NULL;
  174. program();
  175. /* Output the program we have compiled */
  176. file_print("\n# Core program\n", destination_file);
  177. recursive_output(output_list, destination_file);
  178. if(KNIGHT_NATIVE == Architecture) file_print("\n", destination_file);
  179. else if(DEBUG) file_print("\n:ELF_data\n", destination_file);
  180. file_print("\n# Program global variables\n", destination_file);
  181. recursive_output(globals_list, destination_file);
  182. file_print("\n# Program strings\n", destination_file);
  183. recursive_output(strings_list, destination_file);
  184. if(KNIGHT_NATIVE == Architecture) file_print("\n:STACK\n", destination_file);
  185. else if(!DEBUG) file_print("\n:ELF_end\n", destination_file);
  186. exit_success:
  187. if (destination_file != stdout)
  188. {
  189. fclose(destination_file);
  190. }
  191. return EXIT_SUCCESS;
  192. }