cc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /* These need to be here instead of defines
  46. * since cc_* can't handle string constants. */
  47. char* m2_major = "1";
  48. char* m2_minor = "11";
  49. char* m2_patch = "0";
  50. init_macro_env("__M2__", "42", "__INTERNAL_M2__", 0); /* Setup __M2__ */
  51. init_macro_env("__M2C__", m2_major, "__INTERNAL_M2__", 0);
  52. init_macro_env("__M2C_MINOR__", m2_minor, "__INTERNAL_M2__", 0);
  53. init_macro_env("__M2C_PATCHLEVEL__", m2_patch, "__INTERNAL_M2__", 0);
  54. /* The standard allows an implementation defined valid value
  55. * if time and date are not available. */
  56. /* Since we're modifying the tokens directly we don't need closing quotes */
  57. init_macro_env("__DATE__", "\"Jan 1 1970", "__C_STANDARD__", 0);
  58. init_macro_env("__TIME__", "\"00:00:00", "__C_STANDARD__", 0);
  59. init_macro_env("__STDC__", "1", "__C_STANDARD__", 0);
  60. init_macro_env("__STDC_HOSTED__", "1", "__C_STANDARD__", 0);
  61. /* Claim support for C89 despite us supporting some newer features.
  62. * We are not close to fully supporting any standard so claim the one with least features. */
  63. init_macro_env("__STDC_VERSION__", "199409", "__C_STANDARD__", 0);
  64. char* arch;
  65. char* name;
  66. char* hold;
  67. int env=0;
  68. char* val;
  69. int i = 1;
  70. while(i <= argc)
  71. {
  72. if(NULL == argv[i])
  73. {
  74. i = i + 1;
  75. }
  76. else if(match(argv[i], "-o") || match(argv[i], "--output"))
  77. {
  78. destination_file = fopen(argv[i + 1], "w");
  79. if(NULL == destination_file)
  80. {
  81. fputs("Unable to open for writing file: ", stderr);
  82. fputs(argv[i + 1], stderr);
  83. fputs("\n Aborting to avoid problems\n", stderr);
  84. exit(EXIT_FAILURE);
  85. }
  86. i = i + 2;
  87. }
  88. else if(match(argv[i], "-A") || match(argv[i], "--architecture"))
  89. {
  90. arch = argv[i + 1];
  91. if(match("knight-native", arch)) {
  92. Architecture = KNIGHT_NATIVE;
  93. init_macro_env("__knight__", "1", "--architecture", env);
  94. env = env + 1;
  95. }
  96. else if(match("knight-posix", arch)) {
  97. Architecture = KNIGHT_POSIX;
  98. init_macro_env("__knight_posix__", "1", "--architecture", env);
  99. env = env + 1;
  100. }
  101. else if(match("x86", arch))
  102. {
  103. Architecture = X86;
  104. init_macro_env("__i386__", "1", "--architecture", env);
  105. env = env + 1;
  106. }
  107. else if(match("amd64", arch))
  108. {
  109. Architecture = AMD64;
  110. init_macro_env("__x86_64__", "1", "--architecture", env);
  111. env = env + 1;
  112. }
  113. else if(match("armv7l", arch))
  114. {
  115. Architecture = ARMV7L;
  116. init_macro_env("__arm__", "1", "--architecture", env);
  117. env = env + 1;
  118. }
  119. else if(match("aarch64", arch))
  120. {
  121. Architecture = AARCH64;
  122. init_macro_env("__aarch64__", "1", "--architecture", env);
  123. env = env + 1;
  124. }
  125. else if(match("riscv32", arch))
  126. {
  127. Architecture = RISCV32;
  128. init_macro_env("__riscv", "1", "--architecture", env);
  129. init_macro_env("__riscv_xlen", "32", "--architecture", env + 1);
  130. env = env + 2;
  131. }
  132. else if(match("riscv64", arch))
  133. {
  134. Architecture = RISCV64;
  135. init_macro_env("__riscv", "1", "--architecture", env);
  136. init_macro_env("__riscv_xlen", "64", "--architecture", env + 1);
  137. env = env + 2;
  138. }
  139. else
  140. {
  141. fputs("Unknown architecture: ", stderr);
  142. fputs(arch, stderr);
  143. fputs(" know values are: knight-native, knight-posix, x86, amd64, armv7l, aarch64, riscv32 and riscv64\n", stderr);
  144. exit(EXIT_FAILURE);
  145. }
  146. i = i + 2;
  147. }
  148. else if(match(argv[i], "--max-string"))
  149. {
  150. hold = argv[i+1];
  151. if(NULL == hold)
  152. {
  153. fputs("--max-string requires a numeric argument\n", stderr);
  154. exit(EXIT_FAILURE);
  155. }
  156. MAX_STRING = strtoint(hold);
  157. require(0 < MAX_STRING, "Not a valid string size\nAbort and fix your --max-string\n");
  158. i = i + 2;
  159. }
  160. else if(match(argv[i], "--bootstrap-mode"))
  161. {
  162. BOOTSTRAP_MODE = TRUE;
  163. i = i + 1;
  164. }
  165. else if(match(argv[i], "-g") || match(argv[i], "--debug"))
  166. {
  167. DEBUG = TRUE;
  168. i = i + 1;
  169. }
  170. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  171. {
  172. fputs("Usage: M2-Planet [options] file...\n", stdout);
  173. fputs("Options:\n", stdout);
  174. fputs(" --file,-f input file\n", stdout);
  175. fputs(" --output,-o output file\n", stdout);
  176. fputs(" --architecture,-A ARCHITECTURE Target architecture. Call without argument to list available\n", stdout);
  177. fputs(" -D Add define\n", stdout);
  178. fputs(" -E Preprocess only\n", stdout);
  179. fputs(" --debug,-g Debug mode\n", stdout);
  180. fputs(" --bootstrap-mode Emulate less powerful cc_* compilers\n", stdout);
  181. fputs(" --max-string N Size of maximum string value (default 4096)\n", stdout);
  182. fputs(" --help,-h Display this message\n", stdout);
  183. fputs(" --version,-V Display compiler version\n", stdout);
  184. exit(EXIT_SUCCESS);
  185. }
  186. else if(match(argv[i], "-E"))
  187. {
  188. PREPROCESSOR_MODE = TRUE;
  189. i = i + 1;
  190. }
  191. else if(match(argv[i], "-D"))
  192. {
  193. val = argv[i+1];
  194. if(NULL == val)
  195. {
  196. fputs("-D requires an argument", stderr);
  197. exit(EXIT_FAILURE);
  198. }
  199. while(0 != val[0])
  200. {
  201. if('=' == val[0])
  202. {
  203. val[0] = 0;
  204. val = val + 1;
  205. break;
  206. }
  207. val = val + 1;
  208. }
  209. init_macro_env(argv[i+1], val, "__ARGV__", env);
  210. env = env + 1;
  211. i = i + 2;
  212. }
  213. else if(match(argv[i], "-V") || match(argv[i], "--version"))
  214. {
  215. fputs("M2-Planet v", stderr);
  216. fputs(m2_major, stderr);
  217. fputs(".", stderr);
  218. fputs(m2_minor, stderr);
  219. fputs(".", stderr);
  220. fputs(m2_patch, stderr);
  221. fputs("\n", stderr);
  222. exit(EXIT_SUCCESS);
  223. }
  224. else
  225. {
  226. if(match(argv[i], "-f") || match(argv[i], "--file"))
  227. {
  228. i = i + 1;
  229. }
  230. name = argv[i];
  231. if(NULL == hold_string)
  232. {
  233. hold_string = calloc(MAX_STRING + 4, sizeof(char));
  234. require(NULL != hold_string, "Impossible Exhaustion has occurred\n");
  235. }
  236. if(NULL == name)
  237. {
  238. fputs("did not receive a filename\n", stderr);
  239. exit(EXIT_FAILURE);
  240. }
  241. in = fopen(name, "r");
  242. if(NULL == in)
  243. {
  244. fputs("Unable to open for reading file: ", stderr);
  245. fputs(name, stderr);
  246. fputs("\n Aborting to avoid problems\n", stderr);
  247. exit(EXIT_FAILURE);
  248. }
  249. global_token = read_all_tokens(in, global_token, name);
  250. fclose(in);
  251. i = i + 1;
  252. }
  253. }
  254. /* Deal with special case of architecture not being set */
  255. if(0 == Architecture)
  256. {
  257. Architecture = KNIGHT_NATIVE;
  258. init_macro_env("__knight__", "1", "--architecture", env);
  259. }
  260. /* Deal with special case of wanting to read from standard input */
  261. if(stdin == in)
  262. {
  263. hold_string = calloc(MAX_STRING + 4, sizeof(char));
  264. require(NULL != hold_string, "Impossible Exhaustion has occurred\n");
  265. global_token = read_all_tokens(in, global_token, "STDIN");
  266. }
  267. if(NULL == global_token)
  268. {
  269. fputs("Either no input files were given or they were empty\n", stderr);
  270. exit(EXIT_FAILURE);
  271. }
  272. global_token = reverse_list(global_token);
  273. if (BOOTSTRAP_MODE)
  274. {
  275. global_token = remove_line_comment_tokens(global_token);
  276. global_token = remove_preprocessor_directives(global_token);
  277. }
  278. else
  279. {
  280. global_token = remove_line_comments(global_token);
  281. preprocess();
  282. }
  283. if (PREPROCESSOR_MODE)
  284. {
  285. fputs("\n/* Preprocessed source */\n", destination_file);
  286. output_tokens(global_token, destination_file);
  287. goto exit_success;
  288. }
  289. /* the main parser doesn't know how to handle newline tokens */
  290. eat_newline_tokens();
  291. initialize_types();
  292. reset_hold_string();
  293. output_list = NULL;
  294. program();
  295. /* Output the program we have compiled */
  296. fputs("\n# Core program\n", destination_file);
  297. recursive_output(output_list, destination_file);
  298. if(KNIGHT_NATIVE == Architecture) fputs("\n", destination_file);
  299. else if(DEBUG) fputs("\n:ELF_data\n", destination_file);
  300. fputs("\n# Program global variables\n", destination_file);
  301. recursive_output(globals_list, destination_file);
  302. fputs("\n# Program strings\n", destination_file);
  303. recursive_output(strings_list, destination_file);
  304. if(KNIGHT_NATIVE == Architecture) fputs("\n:STACK\n", destination_file);
  305. else if(!DEBUG) fputs("\n:ELF_end\n", destination_file);
  306. exit_success:
  307. if (destination_file != stdout)
  308. {
  309. fclose(destination_file);
  310. }
  311. return EXIT_SUCCESS;
  312. }