tigcc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* TIGCC - A front-end for the compiler, assembler, linker and some other
  2. * stuffs.
  3. * Copyright (C) 2001 John David Ratliff
  4. * Copyright (C) 2001-2002 Romain Liévin
  5. * Copyright (C) 2002-2006 Kevin Kofler
  6. * Modified by Nils Gesbert, 2003
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/wait.h>
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif
  30. #include "tigcc.h"
  31. /* The name the program was run with, stripped of any leading path. */
  32. char *program_name;
  33. void print_version(void)
  34. {
  35. fprintf(stderr, "tigcc version %s built for TIGCC/*nix version %s\n", VERSION, TIGCC_VERSION);
  36. exit(0);
  37. }
  38. /* Display helping informations */
  39. static void
  40. usage (int status)
  41. {
  42. printf ("%s - front-end for TIGCC\n", program_name);
  43. printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
  44. printf ("Options:\n"
  45. " -h, --help display this help and exit\n"
  46. " -V, --version output version information and exit\n"
  47. " -v, --verbose set verbosity on: show what the program is doing\n"
  48. " -q, --quiet display nothing\n"
  49. " -E, pre-process but don't compile or assemble\n"
  50. " -S, compile but don't assemble or link\n"
  51. " -c, compile/assemble, but don't link\n"
  52. " -g, --debug turns on debugging information (DWARF 2 format)\n"
  53. " -bsr ignored for backwards compatibility\n"
  54. " -o [outfile] output to file outfile\n"
  55. " -pack [varname] compress the program using ExePack technology\n"
  56. " -outputbin produce a pure binary file (as with ttstrip)\n"
  57. " -standalone does not link against the tigcc.a archive\n"
  58. " -r, --redirect redirect stdout to the tigcc.log file\n"
  59. " -Wa/WA,options pass options to as/a68k\n"
  60. " -ar create static library (function archive)\n"
  61. " -keep keep object files\n"
  62. " --all-relocs assemble files in all-relocs mode (for range cutting)\n"
  63. "Linker options are automatically passed on to ld-tigcc. See ld-tigcc --help for\n"
  64. "details.\n"
  65. "\n");
  66. exit (status);
  67. }
  68. #define print_help() usage(0)
  69. /* Process cmdline args */
  70. short int process_arg(short int arg, char *argv[], int argc)
  71. {
  72. char *cur_arg = argv[arg];
  73. if (!strcmp("-h", cur_arg) || !strcmp("--help", cur_arg)) {
  74. print_help();
  75. } else if (!strcmp("-V", cur_arg) || !strcmp("--version", cur_arg)) {
  76. print_version();
  77. } else if (!strcmp("-v", cur_arg) || !strcmp("--verbose", cur_arg)) {
  78. ld_argv[ld_argc++] = cur_arg;
  79. gcc_argv[gcc_argc++] = cur_arg;
  80. verbose = TRUE;
  81. printcommands = TRUE;
  82. quiet = FALSE;
  83. } else if (!strcmp("-v0", cur_arg)) {
  84. printcommands = TRUE;
  85. } else if (!strcmp("-q", cur_arg) || !strcmp("--quiet", cur_arg)) {
  86. quiet = TRUE;
  87. verbose = FALSE;
  88. } else if(!strcmp("-E", cur_arg)) {
  89. delete = FALSE;
  90. patch = FALSE;
  91. do_compile = FALSE;
  92. do_link = FALSE;
  93. do_assemble = FALSE;
  94. } else if (!strcmp("-S", cur_arg)) {
  95. delete = FALSE;
  96. do_link = FALSE;
  97. do_assemble = FALSE;
  98. } else if (!strcmp("-c", cur_arg)) {
  99. do_link = FALSE;
  100. } else if (!strcmp("-g", cur_arg)) {
  101. debug = TRUE;
  102. } else if (!strcmp("--debug", cur_arg)) {
  103. debug = TRUE;
  104. } else if (!strcmp("-pack", cur_arg)) {
  105. char *next_arg;
  106. if(arg >= argc-1) {
  107. fprintf(stderr, "Error: you didn't specify a pack file\n");
  108. exit(-1);
  109. } else {
  110. next_arg = argv[++arg];
  111. if(next_arg[0] == '-') {
  112. fprintf(stderr, "Error: invalid pack filename\n");
  113. exit(-1);
  114. }
  115. packfile[0] = 0;
  116. strncpy(packfile, next_arg, 8);
  117. do_pack = TRUE;
  118. }
  119. } else if(!strcmp("-bsr", cur_arg)) {
  120. /* Empty. Ignored for backwards compatibility. */
  121. } else if (!strcmp("-outputbin", cur_arg) || !strcmp("--outputbin", cur_arg)) {
  122. outputbin = TRUE;
  123. } else if (!strcmp("-standalone", cur_arg) || !strcmp("--standalone", cur_arg)) {
  124. nostdlib = TRUE;
  125. } else if (!strcmp("-ar", cur_arg)) {
  126. staticlib = TRUE;
  127. nostdlib = TRUE;
  128. allrelocs = TRUE;
  129. optreturns = TRUE;
  130. } else if (!strcmp("-quill", cur_arg)) {
  131. char *quill_drv = malloc (strlen(tigcc_base) + 25);
  132. if (!quill_drv) {
  133. fprintf(stderr, "Fatal error: not enough free memory\n");
  134. exit(-1);
  135. }
  136. sprintf(quill_drv, "%s/bin/quill.drv", tigcc_base);
  137. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  138. sprintf(quill_drv, "%s/bin/Quill.drv", tigcc_base);
  139. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  140. sprintf(quill_drv, "%s/include/c/quill.drv", tigcc_base);
  141. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  142. sprintf(quill_drv, "%s/include/c/Quill.drv", tigcc_base);
  143. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  144. sprintf(quill_drv, "%s/include/quill/quill.drv", tigcc_base);
  145. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  146. sprintf(quill_drv, "%s/include/quill/Quill.drv", tigcc_base);
  147. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  148. sprintf(quill_drv, "%s/include/Quill/quill.drv", tigcc_base);
  149. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  150. sprintf(quill_drv, "%s/include/Quill/Quill.drv", tigcc_base);
  151. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  152. sprintf(quill_drv, "%s/lib/quill.drv", tigcc_base);
  153. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  154. sprintf(quill_drv, "%s/lib/Quill.drv", tigcc_base);
  155. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  156. fprintf(stderr, "Quill installation problem: quill.drv file is missing.\n");
  157. exit(-1);
  158. quill_drv_found:
  159. gcc_argv[gcc_argc++] = "-include";
  160. gcc_argv[gcc_argc++] = quill_drv;
  161. gcc_argv[gcc_argc++] = "-x";
  162. gcc_argv[gcc_argc++] = "c";
  163. } else if (!strcmp("-o", cur_arg) || !strcmp("--output", cur_arg)) {
  164. if (++arg > argc) {
  165. fprintf(stderr, "Error: you didn't specify an output file\n");
  166. } else {
  167. outfile = argv[arg];
  168. }
  169. } else if (!strcmp("-r", cur_arg) || !strcmp("--redirect", cur_arg)) {
  170. freopen (REDIRECT, "wt", stdout);
  171. dup2 (STDOUT_FILENO, STDERR_FILENO);
  172. } else if (!strncmp("-Wa,", cur_arg, 4)) {
  173. if (as_args) {
  174. as_args = realloc(as_args, strlen(as_args)+strlen(cur_arg)-2);
  175. if (!as_args) {
  176. fputs("Fatal error: not enough free memory\n", stderr);
  177. exit(-1);
  178. }
  179. strcat(as_args, ",");
  180. strcat(as_args, cur_arg+4);
  181. } else {
  182. as_args = strdup(cur_arg);
  183. if (!as_args) {
  184. fputs("Fatal error: not enough free memory\n", stderr);
  185. exit(-1);
  186. }
  187. }
  188. } else if (!strncmp("-WA,", cur_arg, 4)) {
  189. if (a68k_args) {
  190. a68k_args = realloc(a68k_args, strlen(a68k_args)+strlen(cur_arg)-2);
  191. if (!a68k_args) {
  192. fputs("Fatal error: not enough free memory\n", stderr);
  193. exit(-1);
  194. }
  195. strcat(a68k_args, ",");
  196. strcat(a68k_args, cur_arg+4);
  197. } else {
  198. a68k_args = strdup(cur_arg);
  199. if (!a68k_args) {
  200. fputs("Fatal error: not enough free memory\n", stderr);
  201. exit(-1);
  202. }
  203. }
  204. } else if (!strcmp("-np", cur_arg)) {
  205. printf("Developer option: no patching enabled !\n");
  206. patch = FALSE;
  207. } else if (!strcmp("-keep", cur_arg) || !strcmp("--keep", cur_arg)) {
  208. keepobj = TRUE;
  209. } else if (!strcmp("-save-temps", cur_arg) || !strcmp("--save-temps", cur_arg)) {
  210. savetemps = keepobj = TRUE; delete = FALSE;
  211. gcc_argv[gcc_argc++] = cur_arg;
  212. } else if (!strcmp("--fargo", cur_arg)) {
  213. fargo = TRUE;
  214. ld_argv[ld_argc++] = cur_arg;
  215. } else if (!strcmp("--flash-os", cur_arg)) {
  216. flashos = TRUE;
  217. ld_argv[ld_argc++] = cur_arg;
  218. } else if (!strcmp("--cut-ranges", cur_arg)) {
  219. allrelocs = TRUE;
  220. ld_argv[ld_argc++] = cur_arg;
  221. } else if (!strcmp("--all-relocs", cur_arg)) {
  222. allrelocs = TRUE;
  223. } else if (!strcmp("--optimize-code", cur_arg) || !strcmp("--optimize-returns", cur_arg)) {
  224. optreturns = TRUE;
  225. ld_argv[ld_argc++] = cur_arg;
  226. } else if (!strcmp("--native", cur_arg) || !strcmp("--remove-unused", cur_arg)
  227. || !strcmp("--optimize-relocs", cur_arg) || !strcmp("--optimize-nops", cur_arg)
  228. || !strcmp("--optimize-branches", cur_arg) || !strcmp("--optimize-moves", cur_arg)
  229. || !strcmp("--optimize-tests", cur_arg) || !strcmp("--optimize-calcs", cur_arg)
  230. || !strcmp("--reorder-sections", cur_arg) || !strcmp("--merge-constants", cur_arg)
  231. || !strcmp("--omit-bss-init", cur_arg) || !strcmp("--data-var-copy=never", cur_arg)
  232. || !strcmp("--data-var-copy=always", cur_arg)
  233. || !strcmp("--data-var-copy=archived", cur_arg) || !strcmp("--dump", cur_arg)
  234. || !strcmp("--dump0", cur_arg) || !strcmp("--dump1", cur_arg) || !strcmp("--dump2", cur_arg)
  235. || !strcmp("--dump3", cur_arg) || !strcmp("--dump4", cur_arg) || !strcmp("--dump5", cur_arg)
  236. || !strcmp("--dump6", cur_arg) || !strcmp("--dump7", cur_arg) || !strcmp("--dump8", cur_arg)
  237. || !strcmp("--dump9", cur_arg)) {
  238. ld_argv[ld_argc++] = cur_arg;
  239. } else if (!strcmp("-n", cur_arg) || !strcmp("--varname", cur_arg)) {
  240. if (++arg > argc) {
  241. fprintf(stderr, "Error: you didn't specify a variable name\n");
  242. } else {
  243. ld_argv[ld_argc++] = cur_arg;
  244. ld_argv[ld_argc++] = argv[arg];
  245. }
  246. } else if (!strcmp("-d", cur_arg) || !strcmp("--data-var", cur_arg)) {
  247. if (++arg > argc) {
  248. fprintf(stderr, "Error: you didn't specify a data variable name\n");
  249. } else {
  250. ld_argv[ld_argc++] = cur_arg;
  251. ld_argv[ld_argc++] = argv[arg];
  252. nomergesections = TRUE;
  253. }
  254. }
  255. return arg;
  256. }
  257. short int is_tigcc_arg(char *arg)
  258. {
  259. short int loop;
  260. for (loop = 0; tigcc_args[loop] != NULL; loop++) {
  261. if (strncmp(arg, tigcc_args[loop], 4) == 0) {
  262. return TRUE;
  263. }
  264. }
  265. return FALSE;
  266. }
  267. /* Return file type */
  268. short int is_source_file(char *file)
  269. {
  270. char *ext = strrchr(file, '.');
  271. short int filetype = BADFILE;
  272. if (ext == NULL) {
  273. return filetype;
  274. }
  275. if (strcasecmp(ext, ".c") == 0) {
  276. filetype = CFILE;
  277. } else if (strcasecmp(ext, ".asm") == 0) {
  278. filetype = ASMFILE;
  279. } else if (strcasecmp(ext, ".s") == 0) {
  280. filetype = SFILE;
  281. } else if (strcasecmp(ext, ".o") == 0) {
  282. filetype = OFILE;
  283. } else if (strcasecmp(ext, ".a") == 0) {
  284. filetype = AFILE;
  285. } else if (strcasecmp(ext, ".qll") == 0) {
  286. filetype = QLLFILE;
  287. }
  288. return filetype;
  289. }
  290. #define add_to_file_array(file,array) do {array->files[array->count++] = file;} while (0)
  291. void free_file_array(array_of_files *array)
  292. {
  293. /* (NG) Well, it doesn't work even if *all* the strings in the array are malloc-ated.
  294. I don't understand why... :(
  295. Anyway, it won't work if a68k() is called as it is now (see there). */
  296. #if 0
  297. short int loop;
  298. for (loop = 0; loop < array->count; loop++) {
  299. free(array->files[loop]);
  300. }
  301. #endif
  302. free(array);
  303. }
  304. /* Change filename extension and keep long filenames */
  305. char *change_extension(char *file, char *newext)
  306. {
  307. char *start = (char *)strrchr(file, '.');
  308. if(start == NULL) {
  309. start = file + strlen(file);
  310. }
  311. sprintf(start, "%s", newext);
  312. /* (NG) So to be safe file must be strlen(file) + strlen (newext) + 1 long... */
  313. return file;
  314. }
  315. void parse_args(int *argc, char *argv[])
  316. {
  317. static int firstsourcefile = 0;
  318. short int loop, filetype;
  319. array_of_files *array = NULL;
  320. for (loop = 1; loop < (*argc); loop++) {
  321. if ((!strcmp(argv[loop],"-include")||!strcmp(argv[loop],"-x")||!strcmp(argv[loop],"--param")||!strcmp(argv[loop],"-isystem")) && loop+1<*argc) {
  322. gcc_argv[gcc_argc++] = argv[loop++];
  323. gcc_argv[gcc_argc++] = argv[loop];
  324. } else if (is_tigcc_arg(argv[loop])) {
  325. loop = process_arg(loop, argv, *argc);
  326. } else if (argv[loop][0] == '-') {
  327. // if it's not one of our args, pass it to gcc
  328. gcc_argv[gcc_argc++] = argv[loop];
  329. } else {
  330. filetype = is_source_file(argv[loop]);
  331. switch (filetype) {
  332. case CFILE:
  333. array = src_files;
  334. handle_outfile:
  335. if (!firstsourcefile) firstsourcefile = loop;
  336. break;
  337. case ASMFILE:
  338. array = a68k_files;
  339. goto handle_outfile;
  340. case SFILE:
  341. array = asm_files;
  342. goto handle_outfile;
  343. case OFILE:
  344. array = obj_files;
  345. goto handle_outfile;
  346. case AFILE:
  347. array = ar_files;
  348. break;
  349. case QLLFILE: /* Quill files are just C files which need a special
  350. -include switch to include the quill.drv header */
  351. array = src_files;
  352. goto handle_outfile;
  353. default: fprintf(stderr, "Invalid option %s\n", argv[loop]); break;
  354. }
  355. if (array != NULL) {
  356. #if 0 /* Necessary if we want free_file_array not to crash, but it's a waste of memory... */
  357. char *s = strdup(argv[loop]);
  358. if (!s) {
  359. fputs ("Fatal error: not enough free memory.\n", stderr);
  360. exit (1);
  361. }
  362. add_to_file_array(s, array);
  363. #else
  364. add_to_file_array(argv[loop], array);
  365. #endif
  366. }
  367. }
  368. }
  369. // If no output filename, first arg used as default name.
  370. // However, don't do this with -S or -c. Instead, let compile() or
  371. // assemble() guess an appropriate file name for each individual file.
  372. if (firstsourcefile && do_link && !outfile) {
  373. outfile = malloc(strlen(argv[firstsourcefile])+3);
  374. strcpy(outfile,argv[firstsourcefile]);
  375. change_extension(outfile,".o");
  376. }
  377. }
  378. static inline void escape_arg(char *escaped_arg, const char *unescaped_arg)
  379. {
  380. char *p;
  381. strcpy(escaped_arg,unescaped_arg);
  382. for (p=escaped_arg; *p; p++) {
  383. switch(*p) {
  384. /* The following characters are shell characters and need to be escaped! */
  385. case ' ':
  386. case '\\':
  387. case '\"':
  388. case '\'':
  389. case '$':
  390. case '!':
  391. case '^':
  392. case '&':
  393. case '*':
  394. case '(':
  395. case ')':
  396. case '~':
  397. case '[':
  398. case ']':
  399. case '|':
  400. case '{':
  401. case '}':
  402. case ';':
  403. case '<':
  404. case '>':
  405. case '?':
  406. memmove(p+1,p,strlen(p)+1);
  407. *(p++)='\\';
  408. default:
  409. break;
  410. }
  411. }
  412. }
  413. /* Execute a program */
  414. void execute(const char *program, char **argv)
  415. {
  416. pid_t pid;
  417. int status;
  418. if (printcommands) {
  419. char **ptr;
  420. fprintf(stderr, "tigcc: %s", program);
  421. for (ptr = argv + 1; *ptr; ptr++)
  422. {
  423. char escapedarg[((strlen(*ptr))<<1)+1];
  424. escape_arg(escapedarg,*ptr);
  425. fprintf (stderr, " %s", escapedarg);
  426. }
  427. putc ('\n', stderr);
  428. }
  429. fflush (stdout);
  430. pid = fork();
  431. if (pid == 0) {
  432. execv (program, argv);
  433. // execv only returns in case of an error
  434. fprintf (stderr, "Error executing %s\n", program);
  435. exit (1);
  436. }
  437. else if (pid < 0 || (waitpid (pid, &status, 0) != pid) || !WIFEXITED(status)) {
  438. fprintf (stderr, "Error executing %s\n", program);
  439. exit (1);
  440. }
  441. /* Fail silently if another program already showed an error message. */
  442. if (WEXITSTATUS(status)) exit(1);
  443. }
  444. /* Execute GNU cc */
  445. void compile(char *file)
  446. {
  447. unsigned short local_argc = gcc_argc;
  448. char gcc_name[9 + strlen (tigcc_base)];
  449. char bindir[6 + strlen (tigcc_base)];
  450. char includedir[13 + strlen (tigcc_base)];
  451. sprintf (gcc_name, "%s/bin/gcc", tigcc_base);
  452. sprintf (bindir, "-B%s/bin/", tigcc_base);
  453. sprintf (includedir, "-I%s/include/c", tigcc_base);
  454. gcc_argv[0] = gcc_name;
  455. gcc_argv[local_argc++] = bindir;
  456. gcc_argv[local_argc++] = includedir;
  457. gcc_argv[local_argc++] = file;
  458. if (!do_compile) { // preprocess only
  459. gcc_argv[local_argc++] = "-E";
  460. if (outfile) {
  461. gcc_argv[local_argc++] = "-o";
  462. gcc_argv[local_argc++] = outfile;
  463. }
  464. gcc_argv[local_argc] = NULL;
  465. execute(gcc_name, gcc_argv);
  466. }
  467. else {
  468. char *tmpfile;
  469. if (!do_assemble && outfile) tmpfile = outfile;
  470. else {
  471. tmpfile = malloc (strlen (file) + 3);
  472. if (!tmpfile) {
  473. fprintf(stderr, "Fatal error: not enough free memory\n");
  474. exit(-1);
  475. }
  476. strcpy(tmpfile, file);
  477. change_extension(tmpfile, ".s");
  478. }
  479. if (debug) {
  480. gcc_argv[local_argc++] = "-gdwarf-2";
  481. gcc_argv[local_argc++] = "-g3";
  482. gcc_argv[local_argc++] = "-fasynchronous-unwind-tables";
  483. }
  484. if (nomergesections) {
  485. gcc_argv[local_argc++] = "-mno-merge-sections";
  486. }
  487. gcc_argv[local_argc++] = "-S";
  488. if (fargo) gcc_argv[local_argc++] = "-DFARGO";
  489. if (flashos) gcc_argv[local_argc++] = "-DFLASH_OS";
  490. gcc_argv[local_argc++] = "-o";
  491. gcc_argv[local_argc++] = tmpfile;
  492. add_to_file_array(tmpfile, asm_files);
  493. gcc_argv[local_argc] = NULL;
  494. execute(gcc_name, gcc_argv);
  495. // patch assembly file
  496. if(patch) {
  497. char patcher_name[strlen(tigcc_base) + 13];
  498. char *argv[] = {patcher_name, tmpfile, "-o", tmpfile, NULL};
  499. sprintf (patcher_name, "%s/bin/patcher", tigcc_base);
  500. execute (patcher_name, argv);
  501. }
  502. }
  503. }
  504. /* Execute GNU as */
  505. void assemble(char *file)
  506. {
  507. char as_name[strlen(tigcc_base) + 8];
  508. char includedir[strlen(tigcc_base) + 13];
  509. char *tmpfile;
  510. char *argv[7 + (as_args ? strlen (as_args) / 2 : 0) + allrelocs + optreturns + debug];
  511. char asargtokens[as_args?(strlen(as_args)+1):0];
  512. int i = 6;
  513. argv[0] = as_name;
  514. argv[1] = "-mc68000";
  515. argv[2] = includedir;
  516. argv[3] = file;
  517. argv[4] = "-o";
  518. if (!do_link && outfile) tmpfile = outfile;
  519. else {
  520. tmpfile = malloc (strlen (file) + 3);
  521. if (!tmpfile) {
  522. fprintf(stderr, "Fatal error: not enough free memory\n");
  523. exit(-1);
  524. }
  525. strcpy(tmpfile, file);
  526. change_extension(tmpfile, ".o");
  527. }
  528. argv[5] = tmpfile;
  529. sprintf (as_name, "%s/bin/as", tigcc_base);
  530. sprintf (includedir, "-I%s/include/s", tigcc_base);
  531. // add the file to the list
  532. add_to_file_array(tmpfile, obj_files);
  533. if (allrelocs) argv[i++] = "--all-relocs";
  534. if (optreturns) argv[i++] = "--keep-locals";
  535. if (debug) argv[i++] = "--gdwarf2";
  536. if (as_args) {
  537. strcpy (asargtokens, as_args);
  538. strtok (asargtokens, ",");
  539. while ((argv[i++] = strtok (NULL, ",")));
  540. }
  541. else argv[i] = NULL;
  542. execute(as_name, argv);
  543. }
  544. /* Execute ld-tigcc */
  545. void ld(void)
  546. {
  547. short int loop;
  548. unsigned short local_argc = ld_argc;
  549. char ld_name[strlen(tigcc_base) + 14];
  550. char tmpfile[strlen(outfile) + 1];
  551. sprintf (ld_name, "%s/bin/ld-tigcc", tigcc_base);
  552. ld_argv[0] = ld_name;
  553. for (loop = 0; loop < obj_files->count; loop++)
  554. ld_argv[local_argc++] = obj_files->files[loop];
  555. for (loop = 0; loop < ar_files->count; loop++)
  556. ld_argv[local_argc++] = ar_files->files[loop];
  557. ld_argv[local_argc++] = "-o";
  558. ld_argv[local_argc++] = tmpfile;
  559. strcpy(tmpfile, outfile);
  560. change_extension(tmpfile, "");
  561. if (do_pack)
  562. ld_argv[local_argc++] = "--outputbin-main-only";
  563. else if (outputbin)
  564. ld_argv[local_argc++] = "--outputbin";
  565. ld_argv[local_argc] = NULL;
  566. execute(ld_name, ld_argv);
  567. }
  568. /* Execute A68K */
  569. void a68k(char *file)
  570. {
  571. char *tmpfile;
  572. if (!do_link && outfile) {
  573. tmpfile = malloc (strlen (outfile) + 3);
  574. if (!tmpfile) {
  575. fprintf(stderr, "Fatal error: not enough free memory\n");
  576. exit(-1);
  577. }
  578. sprintf(tmpfile, "-o%s", outfile);
  579. }
  580. else {
  581. tmpfile = malloc (strlen (file) + 5);
  582. if (!tmpfile) {
  583. fprintf(stderr, "Fatal error: not enough free memory\n");
  584. exit(-1);
  585. }
  586. sprintf(tmpfile, "-o%s", file);
  587. change_extension(tmpfile, ".o");
  588. }
  589. tmpfile += 2; /* Warning: this prevents free_file_array from working */
  590. /* First, assemble file with A68k which produces AmigaOS files */
  591. {
  592. char a68k_name[strlen(tigcc_base) + 10];
  593. char includedir[strlen(tigcc_base) + 16];
  594. char *argv[7 + (a68k_args ? strlen (a68k_args) / 2 : 0) + allrelocs + optreturns + quiet];
  595. char a68kargtokens[a68k_args?(strlen(a68k_args)+1):0];
  596. int i = 6;
  597. argv[0] = a68k_name;
  598. argv[1] = includedir;
  599. argv[2] = "-g";
  600. argv[3] = "-t";
  601. argv[4] = file;
  602. argv[5] = tmpfile;
  603. sprintf (a68k_name, "%s/bin/a68k", tigcc_base);
  604. sprintf (includedir, "-i%s/include/asm/", tigcc_base);
  605. // add the file to the list
  606. add_to_file_array(tmpfile, obj_files);
  607. if (allrelocs) argv[i++] = "-a";
  608. if (optreturns) argv[i++] = "-d";
  609. if (quiet) argv[i++] = "-q";
  610. if (a68k_args) {
  611. strcpy (a68kargtokens, a68k_args);
  612. strtok (a68kargtokens, ",");
  613. while ((argv[i++] = strtok (NULL, ",")));
  614. }
  615. else argv[i] = NULL;
  616. execute(a68k_name, argv);
  617. }
  618. }
  619. /* Parse pstarter file and change internal varname */
  620. static short int parse_pstarter(const char *input, const char *output,
  621. const char *packvar)
  622. {
  623. FILE *f;
  624. char array[65536];
  625. char *token = "tempprog";
  626. unsigned int i, n;
  627. unsigned int j, k;
  628. // open file in reading
  629. f = fopen(input, "rb");
  630. if(f == NULL) {
  631. fprintf(stderr, "Unable to open pstarter.o\n");
  632. exit(-1);
  633. }
  634. n = fread(array, sizeof(char), 65536*sizeof(char), f);
  635. fclose(f);
  636. // change varname
  637. for(i=0; i<n-strlen(token); i++) {
  638. for(j=0, k=0; j<strlen(token); j++) {
  639. if(array[i+j] == token[j]) {
  640. k++;
  641. }
  642. }
  643. if(k==strlen(token)) {
  644. //printf("offset = %i = 0x%04x\n", i, i);
  645. break;
  646. }
  647. }
  648. sprintf(array+i, "%s", packvar);
  649. // open file in writing
  650. f = fopen(output, "wb");
  651. if(f == NULL) {
  652. fprintf(stderr, "Unable to open pstarter.o\n");
  653. exit(-1);
  654. }
  655. fwrite(array, sizeof(char), n*sizeof(char), f);
  656. fclose(f);
  657. return 0;
  658. }
  659. /* Execute ar-tigcc */
  660. void ar(void)
  661. {
  662. int i, loop;
  663. char buffer[strlen(outfile) + 3];
  664. char ar_name[14 + strlen(tigcc_base)];
  665. char *argv[5 + obj_files->count + ar_files->count];
  666. argv[0] = ar_name;
  667. argv[1] = "-o";
  668. argv[2] = buffer;
  669. argv[3] = "--no-names";
  670. strcpy(buffer, outfile);
  671. change_extension(buffer, ".a");
  672. unlink (buffer);
  673. sprintf(ar_name, "%s/bin/ar-tigcc", tigcc_base);
  674. i = 4;
  675. for (loop = 0; loop < obj_files->count; loop++)
  676. argv[i++] = obj_files->files[loop];
  677. for (loop = 0; loop < ar_files->count; loop++)
  678. argv[i++] = ar_files->files[loop];
  679. argv[i] = NULL;
  680. execute(ar_name, argv);
  681. }
  682. /* Compress TI executable */
  683. void pack(void)
  684. {
  685. char pstarter_file[25 + strlen(tigcc_base)];
  686. char tmpfile[strlen(outfile) + 14];
  687. int ti89_targeted=0;
  688. // check for decompressor program
  689. sprintf(pstarter_file, "%s/lib/pstarter.o", tigcc_base);
  690. if(access(pstarter_file, F_OK) == -1) {
  691. fprintf(stderr, "TIGCC installation problem: pstarter.o file is missing.\n");
  692. return;
  693. }
  694. strcpy(tmpfile, outfile);
  695. change_extension(tmpfile, ".z89");
  696. if(access(tmpfile, F_OK) != -1) {
  697. ti89_targeted=1;
  698. // compress on calc variable
  699. {
  700. char ttpack_name[strlen (tigcc_base) + 12];
  701. char *argv[5];
  702. int i=1;
  703. *argv = ttpack_name;
  704. if (verbose)
  705. argv[i++] = "-v";
  706. else if (quiet)
  707. argv[i++] = "-quiet";
  708. argv[i++] = tmpfile;
  709. argv[i++] = TMP_PCK;
  710. argv[i] = NULL;
  711. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  712. execute(ttpack_name, argv);
  713. }
  714. // encapsulate in var (.89y)
  715. {
  716. char ttbin2oth_name[strlen(tigcc_base) + 15];
  717. char *argv[7];
  718. int i=1;
  719. *argv = ttbin2oth_name;
  720. if (quiet)
  721. argv[i++] = "-quiet";
  722. argv[i++] = "-89";
  723. argv[i++] = "ppg";
  724. argv[i++] = TMP_PCK;
  725. argv[i++] = packfile;
  726. argv[i] = NULL;
  727. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  728. execute(ttbin2oth_name, argv);
  729. }
  730. unlink(tmpfile);
  731. unlink(TMP_PCK);
  732. }
  733. change_extension(tmpfile, ".zv2");
  734. if(access(tmpfile, F_OK) != -1) {
  735. // compress on calc variable
  736. {
  737. char ttpack_name[strlen (tigcc_base) + 12];
  738. char *argv[5];
  739. int i=1;
  740. *argv = ttpack_name;
  741. if (verbose)
  742. argv[i++] = "-v";
  743. else if (quiet)
  744. argv[i++] = "-quiet";
  745. argv[i++] = tmpfile;
  746. argv[i++] = TMP_PCK;
  747. argv[i] = NULL;
  748. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  749. execute(ttpack_name, argv);
  750. }
  751. // encapsulate in var (.v2y)
  752. {
  753. char ttbin2oth_name[strlen(tigcc_base) + 15];
  754. char filename9x[strlen(packfile)+5];
  755. char filenamev2[strlen(packfile)+5];
  756. char *argv[7];
  757. int i=1;
  758. *argv = ttbin2oth_name;
  759. if (quiet)
  760. argv[i++] = "-quiet";
  761. argv[i++] = "-92";
  762. argv[i++] = "ppg";
  763. argv[i++] = TMP_PCK;
  764. argv[i++] = packfile;
  765. argv[i] = NULL;
  766. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  767. execute(ttbin2oth_name, argv);
  768. strcpy(filename9x,packfile);
  769. strcat(filename9x,".9xy");
  770. strcpy(filenamev2,packfile);
  771. strcat(filenamev2,".v2y");
  772. rename(filename9x,filenamev2);
  773. }
  774. unlink(tmpfile);
  775. unlink(TMP_PCK);
  776. }
  777. change_extension(tmpfile, ".z9x");
  778. if(access(tmpfile, F_OK) != -1) {
  779. // compress on calc variable
  780. {
  781. char ttpack_name[strlen (tigcc_base) + 12];
  782. char *argv[5];
  783. int i=1;
  784. *argv = ttpack_name;
  785. if (verbose)
  786. argv[i++] = "-v";
  787. else if (quiet)
  788. argv[i++] = "-quiet";
  789. argv[i++] = tmpfile;
  790. argv[i++] = TMP_PCK;
  791. argv[i] = NULL;
  792. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  793. execute(ttpack_name, argv);
  794. }
  795. // encapsulate in var (.9xy)
  796. {
  797. char ttbin2oth_name[strlen(tigcc_base) + 15];
  798. char *argv[7];
  799. int i=1;
  800. *argv = ttbin2oth_name;
  801. if (quiet)
  802. argv[i++] = "-quiet";
  803. argv[i++] = "-92";
  804. argv[i++] = "ppg";
  805. argv[i++] = TMP_PCK;
  806. argv[i++] = packfile;
  807. argv[i] = NULL;
  808. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  809. execute(ttbin2oth_name, argv);
  810. }
  811. unlink(tmpfile);
  812. unlink(TMP_PCK);
  813. }
  814. // parse pstarter.o and change varname
  815. parse_pstarter(pstarter_file, "pstarter.o", packfile);
  816. // create decompressor (.??z)
  817. {
  818. char ld_tigcc_name[strlen(tigcc_base) + 14];
  819. char *argv[] = {ld_tigcc_name, "pstarter.o", "-o", tmpfile, NULL};
  820. sprintf (ld_tigcc_name, "%s/bin/ld-tigcc", tigcc_base);
  821. change_extension(tmpfile, "");
  822. execute(ld_tigcc_name, argv);
  823. }
  824. unlink("pstarter.o");
  825. if (!ti89_targeted) return; // no TI-89 -> no Titanium
  826. #if 1
  827. // zap any leftover Titanium launchers
  828. strcat(tmpfile, "-titanium.89z");
  829. unlink(tmpfile);
  830. strcpy(tmpfile, outfile);
  831. strcat(tmpfile, "-Titanium.89z");
  832. unlink(tmpfile);
  833. #else
  834. // check for Titanium decompressor program
  835. sprintf(pstarter_file, "%s/lib/pstarter-titanium.o", tigcc_base);
  836. if(access(pstarter_file, F_OK) == -1) {
  837. fprintf(stderr, "TIGCC installation problem: pstarter-titanium.o file is missing.\n");
  838. return;
  839. }
  840. // parse pstarter.o and change varname
  841. parse_pstarter(pstarter_file, "pstarter.o", packfile);
  842. // create decompressor (*-titanium.89z)
  843. {
  844. char ld_tigcc_name[strlen(tigcc_base) + 14];
  845. char *argv[] = {ld_tigcc_name, "pstarter.o", "-o", tmpfile, NULL};
  846. sprintf (ld_tigcc_name, "%s/bin/ld-tigcc", tigcc_base);
  847. strcat(tmpfile, "-titanium");
  848. execute(ld_tigcc_name, argv);
  849. // zap non-TI-89 Titanium launchers
  850. change_extension(tmpfile, ".9xz");
  851. unlink(tmpfile);
  852. change_extension(tmpfile, ".v2z");
  853. unlink(tmpfile);
  854. }
  855. unlink("pstarter.o");
  856. #endif
  857. }
  858. /* Destroy allocated resources */
  859. void safe_exit(void)
  860. {
  861. free(gcc_argv);
  862. free(ld_argv);
  863. free_file_array(src_files);
  864. free_file_array(obj_files);
  865. free_file_array(asm_files);
  866. free_file_array(a68k_files);
  867. free_file_array(ar_files);
  868. free(as_args); free(a68k_args);
  869. }
  870. int main(int argc, char *argv[])
  871. {
  872. short int loop;
  873. program_name = argv[0];
  874. if (argc < 2) {
  875. fprintf(stderr, "tigcc: no input files\n");
  876. exit(0);
  877. }
  878. if ((tigcc_base = getenv("TIGCC")) == NULL) {
  879. fprintf(stderr, "Fatal error: TIGCC is not defined in the environment. TIGCC must be defined before tigcc can run.\nFor (ba)sh, try: export TIGCC=/path/to/tigcc\nFor (t)csh, try: setenv TIGCC /path/to/tigcc\n");
  880. exit(-1);
  881. }
  882. if (atexit(safe_exit) != 0) {
  883. fprintf(stderr, "Fatal error: unable to register safe exit callback\n");
  884. exit(-1);
  885. }
  886. /* worst case (every arg is a gcc arg): argc + -Bprefix/bin/ + -Idir + -gdwarf-2 + -g3 +
  887. + -fasynchronous-unwind-tables + -mno-merge-sections + -S
  888. + -DFARGO/-DFLASH_OS + -include + quill.drv + -x + c + -o + filename */
  889. gcc_argv = malloc((argc + 15) * sizeof (char*));
  890. /* worst case: each arg is either a file to link or a linker switch; add
  891. tigcc.a/fargo.a/flashos.a, -o outfile, --outputbin, NULL */
  892. ld_argv = malloc((argc + 5) * sizeof (char*));
  893. src_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  894. obj_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  895. asm_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  896. a68k_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  897. ar_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  898. if ((gcc_argv == NULL) || (ld_argv == NULL) || (src_files == NULL) || (obj_files == NULL) ||
  899. (asm_files == NULL) || (a68k_files == NULL) || (ar_files == NULL)) {
  900. fprintf(stderr, "Fatal error: not enough free memory\n");
  901. exit(-1);
  902. }
  903. src_files -> count = 0;
  904. obj_files -> count = 0;
  905. asm_files -> count = 0;
  906. a68k_files -> count = 0;
  907. ar_files -> count = 0;
  908. // parse the arguments
  909. parse_args(&argc, argv);
  910. // check there is at least one file to process
  911. if(!src_files->count && !asm_files->count && !a68k_files->count && !obj_files->count)
  912. {
  913. fprintf(stderr, "tigcc: no input files\n");
  914. exit(0);
  915. }
  916. // set the archive file
  917. if(!nostdlib) {
  918. char *buffer = malloc(strlen(tigcc_base)+13);
  919. sprintf(buffer, flashos?"%s/lib/flashos.a":(fargo?"%s/lib/fargo.a":"%s/lib/tigcc.a"), tigcc_base);
  920. add_to_file_array(buffer, ar_files);
  921. }
  922. if(1) {
  923. for (loop = 0; loop < src_files->count; loop++) {
  924. compile(src_files->files[loop]);
  925. }
  926. }
  927. if (do_assemble) {
  928. for (loop = 0; loop < asm_files->count; loop++) {
  929. assemble(asm_files->files[loop]);
  930. }
  931. for (loop = 0; loop < a68k_files->count; loop++) {
  932. a68k(a68k_files->files[loop]);
  933. }
  934. }
  935. if (staticlib) ar(); else {
  936. if (do_link && obj_files->count > 0) {
  937. ld();
  938. }
  939. }
  940. if(delete) {
  941. for (loop = 0; loop < src_files->count; loop++) {
  942. char tmpfile[strlen(src_files->files[loop]) + 3];
  943. strcpy(tmpfile, src_files->files[loop]);
  944. change_extension(tmpfile, ".s");
  945. unlink(tmpfile);
  946. }
  947. }
  948. if(do_link && !keepobj) {
  949. for (loop = 0; loop < src_files->count; loop++) {
  950. char tmpfile[strlen(src_files->files[loop]) + 3];
  951. strcpy(tmpfile, src_files->files[loop]);
  952. change_extension(tmpfile, ".o");
  953. unlink(tmpfile);
  954. }
  955. for (loop = 0; loop < asm_files->count; loop++) {
  956. char tmpfile[strlen(asm_files->files[loop]) + 3];
  957. strcpy(tmpfile, asm_files->files[loop]);
  958. change_extension(tmpfile, ".o");
  959. unlink(tmpfile);
  960. }
  961. for (loop = 0; loop < a68k_files->count; loop++) {
  962. char tmpfile[strlen(a68k_files->files[loop]) + 3];
  963. strcpy(tmpfile, a68k_files->files[loop]);
  964. change_extension(tmpfile, ".o");
  965. unlink(tmpfile);
  966. }
  967. }
  968. if (!staticlib && do_pack && do_link) {
  969. pack();
  970. }
  971. return 0;
  972. }