cc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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(void);
  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(void);
  30. void init_macro_env(char* sym, char* value, char* source, int num);
  31. void preprocess(void);
  32. void program(void);
  33. void recursive_output(struct token_list* i, FILE* out);
  34. void output_tokens(struct token_list *i, FILE* out);
  35. int strtoint(char *a);
  36. int main(int argc, char** argv)
  37. {
  38. MAX_STRING = 4096;
  39. BOOTSTRAP_MODE = FALSE;
  40. PREPROCESSOR_MODE = FALSE;
  41. int DEBUG = FALSE;
  42. FILE* in = stdin;
  43. FILE* destination_file = stdout;
  44. Architecture = 0; /* catch unset */
  45. init_macro_env("__M2__", "42", "__INTERNAL_M2__", 0); /* Setup __M2__ */
  46. char* arch;
  47. char* name;
  48. char* hold;
  49. int env=0;
  50. char* val;
  51. int i = 1;
  52. while(i <= argc)
  53. {
  54. if(NULL == argv[i])
  55. {
  56. i = i + 1;
  57. }
  58. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  59. {
  60. if(NULL == hold_string)
  61. {
  62. hold_string = calloc(MAX_STRING + 4, sizeof(char));
  63. require(NULL != hold_string, "Impossible Exhaustion has occurred\n");
  64. }
  65. name = argv[i + 1];
  66. if(NULL == name)
  67. {
  68. fputs("did not receive a file name\n", stderr);
  69. exit(EXIT_FAILURE);
  70. }
  71. in = fopen(name, "r");
  72. if(NULL == in)
  73. {
  74. fputs("Unable to open for reading file: ", stderr);
  75. fputs(name, stderr);
  76. fputs("\n Aborting to avoid problems\n", stderr);
  77. exit(EXIT_FAILURE);
  78. }
  79. global_token = read_all_tokens(in, global_token, name);
  80. fclose(in);
  81. i = i + 2;
  82. }
  83. else if(match(argv[i], "-o") || match(argv[i], "--output"))
  84. {
  85. destination_file = fopen(argv[i + 1], "w");
  86. if(NULL == destination_file)
  87. {
  88. fputs("Unable to open for writing file: ", stderr);
  89. fputs(argv[i + 1], stderr);
  90. fputs("\n Aborting to avoid problems\n", stderr);
  91. exit(EXIT_FAILURE);
  92. }
  93. i = i + 2;
  94. }
  95. else if(match(argv[i], "-A") || match(argv[i], "--architecture"))
  96. {
  97. arch = argv[i + 1];
  98. if(match("knight-native", arch)) {
  99. Architecture = KNIGHT_NATIVE;
  100. init_macro_env("__knight__", "1", "--architecture", env);
  101. env = env + 1;
  102. }
  103. else if(match("knight-posix", arch)) {
  104. Architecture = KNIGHT_POSIX;
  105. init_macro_env("__knight_posix__", "1", "--architecture", env);
  106. env = env + 1;
  107. }
  108. else if(match("x86", arch))
  109. {
  110. Architecture = X86;
  111. init_macro_env("__i386__", "1", "--architecture", env);
  112. env = env + 1;
  113. }
  114. else if(match("amd64", arch))
  115. {
  116. Architecture = AMD64;
  117. init_macro_env("__x86_64__", "1", "--architecture", env);
  118. env = env + 1;
  119. }
  120. else if(match("armv7l", arch))
  121. {
  122. Architecture = ARMV7L;
  123. init_macro_env("__arm__", "1", "--architecture", env);
  124. env = env + 1;
  125. }
  126. else if(match("aarch64", arch))
  127. {
  128. Architecture = AARCH64;
  129. init_macro_env("__aarch64__", "1", "--architecture", env);
  130. env = env + 1;
  131. }
  132. else if(match("riscv32", arch))
  133. {
  134. Architecture = RISCV32;
  135. init_macro_env("__riscv", "1", "--architecture", env);
  136. init_macro_env("__riscv_xlen", "32", "--architecture", env + 1);
  137. env = env + 2;
  138. }
  139. else if(match("riscv64", arch))
  140. {
  141. Architecture = RISCV64;
  142. init_macro_env("__riscv", "1", "--architecture", env);
  143. init_macro_env("__riscv_xlen", "64", "--architecture", env + 1);
  144. env = env + 2;
  145. }
  146. else
  147. {
  148. fputs("Unknown architecture: ", stderr);
  149. fputs(arch, stderr);
  150. fputs(" know values are: knight-native, knight-posix, x86, amd64, armv7l, aarch64, riscv32 and riscv64\n", stderr);
  151. exit(EXIT_FAILURE);
  152. }
  153. i = i + 2;
  154. }
  155. else if(match(argv[i], "--max-string"))
  156. {
  157. hold = argv[i+1];
  158. if(NULL == hold)
  159. {
  160. fputs("--max-string requires a numeric argument\n", stderr);
  161. exit(EXIT_FAILURE);
  162. }
  163. MAX_STRING = strtoint(hold);
  164. require(0 < MAX_STRING, "Not a valid string size\nAbort and fix your --max-string\n");
  165. i = i + 2;
  166. }
  167. else if(match(argv[i], "--bootstrap-mode"))
  168. {
  169. BOOTSTRAP_MODE = TRUE;
  170. i = i + 1;
  171. }
  172. else if(match(argv[i], "-g") || match(argv[i], "--debug"))
  173. {
  174. DEBUG = TRUE;
  175. i = i + 1;
  176. }
  177. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  178. {
  179. fputs(" -f input file\n -o output file\n --help for this message\n --version for file version\n", stdout);
  180. exit(EXIT_SUCCESS);
  181. }
  182. else if(match(argv[i], "-E"))
  183. {
  184. PREPROCESSOR_MODE = TRUE;
  185. i = i + 1;
  186. }
  187. else if(match(argv[i], "-D"))
  188. {
  189. val = argv[i+1];
  190. if(NULL == val)
  191. {
  192. fputs("-D requires an argument", stderr);
  193. exit(EXIT_FAILURE);
  194. }
  195. while(0 != val[0])
  196. {
  197. if('=' == val[0])
  198. {
  199. val[0] = 0;
  200. val = val + 1;
  201. break;
  202. }
  203. val = val + 1;
  204. }
  205. init_macro_env(argv[i+1], val, "__ARGV__", env);
  206. env = env + 1;
  207. i = i + 2;
  208. }
  209. else if(match(argv[i], "-V") || match(argv[i], "--version"))
  210. {
  211. fputs("M2-Planet v1.11.0\n", stderr);
  212. exit(EXIT_SUCCESS);
  213. }
  214. else
  215. {
  216. fputs("UNKNOWN ARGUMENT\n", stdout);
  217. exit(EXIT_FAILURE);
  218. }
  219. }
  220. /* Deal with special case of architecture not being set */
  221. if(0 == Architecture)
  222. {
  223. Architecture = KNIGHT_NATIVE;
  224. init_macro_env("__knight__", "1", "--architecture", env);
  225. }
  226. /* Deal with special case of wanting to read from standard input */
  227. if(stdin == in)
  228. {
  229. hold_string = calloc(MAX_STRING + 4, sizeof(char));
  230. require(NULL != hold_string, "Impossible Exhaustion has occurred\n");
  231. global_token = read_all_tokens(in, global_token, "STDIN");
  232. }
  233. if(NULL == global_token)
  234. {
  235. fputs("Either no input files were given or they were empty\n", stderr);
  236. exit(EXIT_FAILURE);
  237. }
  238. global_token = reverse_list(global_token);
  239. if (BOOTSTRAP_MODE)
  240. {
  241. global_token = remove_line_comment_tokens(global_token);
  242. global_token = remove_preprocessor_directives(global_token);
  243. }
  244. else
  245. {
  246. global_token = remove_line_comments(global_token);
  247. preprocess();
  248. }
  249. if (PREPROCESSOR_MODE)
  250. {
  251. fputs("\n/* Preprocessed source */\n", destination_file);
  252. output_tokens(global_token, destination_file);
  253. goto exit_success;
  254. }
  255. /* the main parser doesn't know how to handle newline tokens */
  256. eat_newline_tokens();
  257. initialize_types();
  258. reset_hold_string();
  259. output_list = NULL;
  260. program();
  261. /* Output the program we have compiled */
  262. fputs("\n# Core program\n", destination_file);
  263. recursive_output(output_list, destination_file);
  264. if(KNIGHT_NATIVE == Architecture) fputs("\n", destination_file);
  265. else if(DEBUG) fputs("\n:ELF_data\n", destination_file);
  266. fputs("\n# Program global variables\n", destination_file);
  267. recursive_output(globals_list, destination_file);
  268. fputs("\n# Program strings\n", destination_file);
  269. recursive_output(strings_list, destination_file);
  270. if(KNIGHT_NATIVE == Architecture) fputs("\n:STACK\n", destination_file);
  271. else if(!DEBUG) fputs("\n:ELF_end\n", destination_file);
  272. exit_success:
  273. if (destination_file != stdout)
  274. {
  275. fclose(destination_file);
  276. }
  277. return EXIT_SUCCESS;
  278. }