tigcc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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-2005 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. as_args = strdup (cur_arg);
  174. if (!as_args) {
  175. fputs ("Fatal error: not enough free memory\n", stderr);
  176. exit (-1);
  177. }
  178. } else if (!strncmp("-WA,", cur_arg, 4)) {
  179. a68k_args = strdup (cur_arg);
  180. if (!a68k_args) {
  181. fputs ("Fatal error: not enough free memory\n", stderr);
  182. exit (-1);
  183. }
  184. } else if (!strcmp("-np", cur_arg)) {
  185. printf("Developer option: no patching enabled !\n");
  186. patch = FALSE;
  187. } else if (!strcmp("-keep", cur_arg) || !strcmp("--keep", cur_arg)) {
  188. keepobj = TRUE;
  189. } else if (!strcmp("-save-temps", cur_arg) || !strcmp("--save-temps", cur_arg)) {
  190. savetemps = keepobj = TRUE; delete = FALSE;
  191. gcc_argv[gcc_argc++] = cur_arg;
  192. } else if (!strcmp("--fargo", cur_arg)) {
  193. fargo = TRUE;
  194. ld_argv[ld_argc++] = cur_arg;
  195. } else if (!strcmp("--flash-os", cur_arg)) {
  196. flashos = TRUE;
  197. ld_argv[ld_argc++] = cur_arg;
  198. } else if (!strcmp("--cut-ranges", cur_arg)) {
  199. allrelocs = TRUE;
  200. ld_argv[ld_argc++] = cur_arg;
  201. } else if (!strcmp("--all-relocs", cur_arg)) {
  202. allrelocs = TRUE;
  203. } else if (!strcmp("--optimize-code", cur_arg) || !strcmp("--optimize-returns", cur_arg)) {
  204. optreturns = TRUE;
  205. ld_argv[ld_argc++] = cur_arg;
  206. } else if (!strcmp("--native", cur_arg) || !strcmp("--remove-unused", cur_arg)
  207. || !strcmp("--optimize-relocs", cur_arg) || !strcmp("--optimize-nops", cur_arg)
  208. || !strcmp("--optimize-branches", cur_arg) || !strcmp("--optimize-moves", cur_arg)
  209. || !strcmp("--optimize-tests", cur_arg) || !strcmp("--optimize-calcs", cur_arg)
  210. || !strcmp("--reorder-sections", cur_arg) || !strcmp("--merge-constants", cur_arg)
  211. || !strcmp("--omit-bss-init", cur_arg) || !strcmp("--data-var-copy=never", cur_arg)
  212. || !strcmp("--data-var-copy=always", cur_arg)
  213. || !strcmp("--data-var-copy=archived", cur_arg) || !strcmp("--dump", cur_arg)
  214. || !strcmp("--dump0", cur_arg) || !strcmp("--dump1", cur_arg) || !strcmp("--dump2", cur_arg)
  215. || !strcmp("--dump3", cur_arg) || !strcmp("--dump4", cur_arg) || !strcmp("--dump5", cur_arg)
  216. || !strcmp("--dump6", cur_arg) || !strcmp("--dump7", cur_arg) || !strcmp("--dump8", cur_arg)
  217. || !strcmp("--dump9", cur_arg)) {
  218. ld_argv[ld_argc++] = cur_arg;
  219. } else if (!strcmp("-n", cur_arg) || !strcmp("--varname", cur_arg)) {
  220. if (++arg > argc) {
  221. fprintf(stderr, "Error: you didn't specify a variable name\n");
  222. } else {
  223. ld_argv[ld_argc++] = cur_arg;
  224. ld_argv[ld_argc++] = argv[arg];
  225. }
  226. } else if (!strcmp("-d", cur_arg) || !strcmp("--data-var", cur_arg)) {
  227. if (++arg > argc) {
  228. fprintf(stderr, "Error: you didn't specify a data variable name\n");
  229. } else {
  230. ld_argv[ld_argc++] = cur_arg;
  231. ld_argv[ld_argc++] = argv[arg];
  232. nomergesections = TRUE;
  233. }
  234. }
  235. return arg;
  236. }
  237. short int is_tigcc_arg(char *arg)
  238. {
  239. short int loop;
  240. for (loop = 0; tigcc_args[loop] != NULL; loop++) {
  241. if (strncmp(arg, tigcc_args[loop], 4) == 0) {
  242. return TRUE;
  243. }
  244. }
  245. return FALSE;
  246. }
  247. /* Return file type */
  248. short int is_source_file(char *file)
  249. {
  250. char *ext = strrchr(file, '.');
  251. short int filetype = BADFILE;
  252. if (ext == NULL) {
  253. return filetype;
  254. }
  255. if (strcasecmp(ext, ".c") == 0) {
  256. filetype = CFILE;
  257. } else if (strcasecmp(ext, ".asm") == 0) {
  258. filetype = ASMFILE;
  259. } else if (strcasecmp(ext, ".s") == 0) {
  260. filetype = SFILE;
  261. } else if (strcasecmp(ext, ".o") == 0) {
  262. filetype = OFILE;
  263. } else if (strcasecmp(ext, ".a") == 0) {
  264. filetype = AFILE;
  265. } else if (strcasecmp(ext, ".qll") == 0) {
  266. filetype = QLLFILE;
  267. }
  268. return filetype;
  269. }
  270. #define add_to_file_array(file,array) do {array->files[array->count++] = file;} while (0)
  271. void free_file_array(array_of_files *array)
  272. {
  273. /* (NG) Well, it doesn't work even if *all* the strings in the array are malloc-ated.
  274. I don't understand why... :(
  275. Anyway, it won't work if a68k() is called as it is now (see there). */
  276. #if 0
  277. short int loop;
  278. for (loop = 0; loop < array->count; loop++) {
  279. free(array->files[loop]);
  280. }
  281. #endif
  282. free(array);
  283. }
  284. /* Change filename extension and keep long filenames */
  285. char *change_extension(char *file, char *newext)
  286. {
  287. char *start = (char *)strrchr(file, '.');
  288. if(start == NULL) {
  289. start = file + strlen(file);
  290. }
  291. sprintf(start, "%s", newext);
  292. /* (NG) So to be safe file must be strlen(file) + strlen (newext) + 1 long... */
  293. return file;
  294. }
  295. void parse_args(int *argc, char *argv[])
  296. {
  297. static int firstsourcefile = 0;
  298. short int loop, filetype;
  299. array_of_files *array = NULL;
  300. for (loop = 1; loop < (*argc); loop++) {
  301. if ((!strcmp(argv[loop],"-include")||!strcmp(argv[loop],"-x")||!strcmp(argv[loop],"--param")||!strcmp(argv[loop],"-isystem")) && loop+1<*argc) {
  302. gcc_argv[gcc_argc++] = argv[loop++];
  303. gcc_argv[gcc_argc++] = argv[loop];
  304. } else if (is_tigcc_arg(argv[loop])) {
  305. loop = process_arg(loop, argv, *argc);
  306. } else if (argv[loop][0] == '-') {
  307. // if it's not one of our args, pass it to gcc
  308. gcc_argv[gcc_argc++] = argv[loop];
  309. } else {
  310. filetype = is_source_file(argv[loop]);
  311. switch (filetype) {
  312. case CFILE:
  313. array = src_files;
  314. handle_outfile:
  315. if (!firstsourcefile) firstsourcefile = loop;
  316. break;
  317. case ASMFILE:
  318. array = a68k_files;
  319. goto handle_outfile;
  320. case SFILE:
  321. array = asm_files;
  322. goto handle_outfile;
  323. case OFILE:
  324. array = obj_files;
  325. goto handle_outfile;
  326. case AFILE:
  327. array = ar_files;
  328. break;
  329. case QLLFILE: /* Quill files are just C files which need a special
  330. -include switch to include the quill.drv header */
  331. array = src_files;
  332. goto handle_outfile;
  333. default: fprintf(stderr, "Invalid option %s\n", argv[loop]); break;
  334. }
  335. if (array != NULL) {
  336. #if 0 /* Necessary if we want free_file_array not to crash, but it's a waste of memory... */
  337. char *s = strdup(argv[loop]);
  338. if (!s) {
  339. fputs ("Fatal error: not enough free memory.\n", stderr);
  340. exit (1);
  341. }
  342. add_to_file_array(s, array);
  343. #else
  344. add_to_file_array(argv[loop], array);
  345. #endif
  346. }
  347. }
  348. }
  349. // If no output filename, first arg used as default name.
  350. // However, don't do this with -S or -c. Instead, let compile() or
  351. // assemble() guess an appropriate file name for each individual file.
  352. if (firstsourcefile && do_link && !outfile) {
  353. outfile = malloc(strlen(argv[firstsourcefile])+3);
  354. strcpy(outfile,argv[firstsourcefile]);
  355. change_extension(outfile,".o");
  356. }
  357. }
  358. static inline void escape_arg(char *escaped_arg, const char *unescaped_arg)
  359. {
  360. char *p;
  361. strcpy(escaped_arg,unescaped_arg);
  362. for (p=escaped_arg; *p; p++) {
  363. switch(*p) {
  364. /* The following characters are shell characters and need to be escaped! */
  365. case ' ':
  366. case '\\':
  367. case '\"':
  368. case '\'':
  369. case '$':
  370. case '!':
  371. case '^':
  372. case '&':
  373. case '*':
  374. case '(':
  375. case ')':
  376. case '~':
  377. case '[':
  378. case ']':
  379. case '|':
  380. case '{':
  381. case '}':
  382. case ';':
  383. case '<':
  384. case '>':
  385. case '?':
  386. memmove(p+1,p,strlen(p)+1);
  387. *(p++)='\\';
  388. default:
  389. break;
  390. }
  391. }
  392. }
  393. /* Execute a program */
  394. void execute(const char *program, char **argv)
  395. {
  396. pid_t pid;
  397. int status;
  398. if (printcommands) {
  399. char **ptr;
  400. fprintf(stderr, "tigcc: %s", program);
  401. for (ptr = argv + 1; *ptr; ptr++)
  402. {
  403. char escapedarg[((strlen(*ptr))<<1)+1];
  404. escape_arg(escapedarg,*ptr);
  405. fprintf (stderr, " %s", escapedarg);
  406. }
  407. putc ('\n', stderr);
  408. }
  409. fflush (stdout);
  410. pid = fork();
  411. if (pid == 0) {
  412. execv (program, argv);
  413. // execv only returns in case of an error
  414. fprintf (stderr, "Error executing %s\n", program);
  415. exit (1);
  416. }
  417. else if (pid < 0 || (waitpid (pid, &status, 0) != pid) || !WIFEXITED(status)) {
  418. fprintf (stderr, "Error executing %s\n", program);
  419. exit (1);
  420. }
  421. /* Fail silently if another program already showed an error message. */
  422. if (WEXITSTATUS(status)) exit(1);
  423. }
  424. /* Execute GNU cc */
  425. void compile(char *file)
  426. {
  427. unsigned short local_argc = gcc_argc;
  428. char gcc_name[9 + strlen (tigcc_base)];
  429. char bindir[6 + strlen (tigcc_base)];
  430. char includedir[13 + strlen (tigcc_base)];
  431. sprintf (gcc_name, "%s/bin/gcc", tigcc_base);
  432. sprintf (bindir, "-B%s/bin/", tigcc_base);
  433. sprintf (includedir, "-I%s/include/c", tigcc_base);
  434. gcc_argv[0] = gcc_name;
  435. gcc_argv[local_argc++] = bindir;
  436. gcc_argv[local_argc++] = includedir;
  437. gcc_argv[local_argc++] = file;
  438. if (!do_compile) { // preprocess only
  439. gcc_argv[local_argc++] = "-E";
  440. if (outfile) {
  441. gcc_argv[local_argc++] = "-o";
  442. gcc_argv[local_argc++] = outfile;
  443. }
  444. gcc_argv[local_argc] = NULL;
  445. execute(gcc_name, gcc_argv);
  446. }
  447. else {
  448. char *tmpfile;
  449. if (!do_assemble && outfile) tmpfile = outfile;
  450. else {
  451. tmpfile = malloc (strlen (file) + 3);
  452. if (!tmpfile) {
  453. fprintf(stderr, "Fatal error: not enough free memory\n");
  454. exit(-1);
  455. }
  456. strcpy(tmpfile, file);
  457. change_extension(tmpfile, ".s");
  458. }
  459. if (debug) {
  460. gcc_argv[local_argc++] = "-gdwarf-2";
  461. gcc_argv[local_argc++] = "-g3";
  462. gcc_argv[local_argc++] = "-fasynchronous-unwind-tables";
  463. }
  464. if (nomergesections) {
  465. gcc_argv[local_argc++] = "-mno-merge-sections";
  466. }
  467. gcc_argv[local_argc++] = "-S";
  468. if (fargo) gcc_argv[local_argc++] = "-DFARGO";
  469. if (flashos) gcc_argv[local_argc++] = "-DFLASH_OS";
  470. gcc_argv[local_argc++] = "-o";
  471. gcc_argv[local_argc++] = tmpfile;
  472. add_to_file_array(tmpfile, asm_files);
  473. gcc_argv[local_argc] = NULL;
  474. execute(gcc_name, gcc_argv);
  475. // patch assembly file
  476. if(patch) {
  477. char patcher_name[strlen(tigcc_base) + 13];
  478. char *argv[] = {patcher_name, tmpfile, "-o", tmpfile, NULL};
  479. sprintf (patcher_name, "%s/bin/patcher", tigcc_base);
  480. execute (patcher_name, argv);
  481. }
  482. }
  483. }
  484. /* Execute GNU as */
  485. void assemble(char *file)
  486. {
  487. char as_name[strlen(tigcc_base) + 8];
  488. char includedir[strlen(tigcc_base) + 13];
  489. char *tmpfile;
  490. char *argv[7 + (as_args ? strlen (as_args) / 2 : 0) + allrelocs + optreturns + debug];
  491. char asargtokens[as_args?(strlen(as_args)+1):0];
  492. int i = 6;
  493. argv[0] = as_name;
  494. argv[1] = "-mc68000";
  495. argv[2] = includedir;
  496. argv[3] = file;
  497. argv[4] = "-o";
  498. if (!do_link && outfile) tmpfile = outfile;
  499. else {
  500. tmpfile = malloc (strlen (file) + 3);
  501. if (!tmpfile) {
  502. fprintf(stderr, "Fatal error: not enough free memory\n");
  503. exit(-1);
  504. }
  505. strcpy(tmpfile, file);
  506. change_extension(tmpfile, ".o");
  507. }
  508. argv[5] = tmpfile;
  509. sprintf (as_name, "%s/bin/as", tigcc_base);
  510. sprintf (includedir, "-I%s/include/s", tigcc_base);
  511. // add the file to the list
  512. add_to_file_array(tmpfile, obj_files);
  513. if (allrelocs) argv[i++] = "--all-relocs";
  514. if (optreturns) argv[i++] = "--keep-locals";
  515. if (debug) argv[i++] = "--gdwarf2";
  516. if (as_args) {
  517. strcpy (asargtokens, as_args);
  518. strtok (asargtokens, ",");
  519. while ((argv[i++] = strtok (NULL, ",")));
  520. }
  521. else argv[i] = NULL;
  522. execute(as_name, argv);
  523. }
  524. /* Execute ld-tigcc */
  525. void ld(void)
  526. {
  527. short int loop;
  528. unsigned short local_argc = ld_argc;
  529. char ld_name[strlen(tigcc_base) + 14];
  530. char tmpfile[strlen(outfile) + 1];
  531. sprintf (ld_name, "%s/bin/ld-tigcc", tigcc_base);
  532. ld_argv[0] = ld_name;
  533. for (loop = 0; loop < obj_files->count; loop++)
  534. ld_argv[local_argc++] = obj_files->files[loop];
  535. for (loop = 0; loop < ar_files->count; loop++)
  536. ld_argv[local_argc++] = ar_files->files[loop];
  537. ld_argv[local_argc++] = "-o";
  538. ld_argv[local_argc++] = tmpfile;
  539. strcpy(tmpfile, outfile);
  540. change_extension(tmpfile, "");
  541. if(outputbin || do_pack)
  542. ld_argv[local_argc++] = "--outputbin";
  543. ld_argv[local_argc] = NULL;
  544. execute(ld_name, ld_argv);
  545. }
  546. /* Execute A68K */
  547. void a68k(char *file)
  548. {
  549. char *tmpfile;
  550. if (!do_link && outfile) {
  551. tmpfile = malloc (strlen (outfile) + 3);
  552. if (!tmpfile) {
  553. fprintf(stderr, "Fatal error: not enough free memory\n");
  554. exit(-1);
  555. }
  556. sprintf(tmpfile, "-o%s", outfile);
  557. }
  558. else {
  559. tmpfile = malloc (strlen (file) + 5);
  560. if (!tmpfile) {
  561. fprintf(stderr, "Fatal error: not enough free memory\n");
  562. exit(-1);
  563. }
  564. sprintf(tmpfile, "-o%s", file);
  565. change_extension(tmpfile, ".o");
  566. }
  567. tmpfile += 2; /* Warning: this prevents free_file_array from working */
  568. /* First, assemble file with A68k which produces AmigaOS files */
  569. {
  570. char a68k_name[strlen(tigcc_base) + 10];
  571. char includedir[strlen(tigcc_base) + 16];
  572. char *argv[7 + (a68k_args ? strlen (a68k_args) / 2 : 0) + allrelocs + optreturns + quiet];
  573. char a68kargtokens[a68k_args?(strlen(a68k_args)+1):0];
  574. int i = 6;
  575. argv[0] = a68k_name;
  576. argv[1] = includedir;
  577. argv[2] = "-g";
  578. argv[3] = "-t";
  579. argv[4] = file;
  580. argv[5] = tmpfile;
  581. sprintf (a68k_name, "%s/bin/a68k", tigcc_base);
  582. sprintf (includedir, "-i%s/include/asm/", tigcc_base);
  583. // add the file to the list
  584. add_to_file_array(tmpfile, obj_files);
  585. if (allrelocs) argv[i++] = "-a";
  586. if (optreturns) argv[i++] = "-d";
  587. if (quiet) argv[i++] = "-q";
  588. if (a68k_args) {
  589. strcpy (a68kargtokens, a68k_args);
  590. strtok (a68kargtokens, ",");
  591. while ((argv[i++] = strtok (NULL, ",")));
  592. }
  593. else argv[i] = NULL;
  594. execute(a68k_name, argv);
  595. }
  596. }
  597. /* Parse pstarter file and change internal varname */
  598. static short int parse_pstarter(const char *input, const char *output,
  599. const char *packvar)
  600. {
  601. FILE *f;
  602. char array[65536];
  603. char *token = "tempprog";
  604. unsigned int i, n;
  605. unsigned int j, k;
  606. // open file in reading
  607. f = fopen(input, "rb");
  608. if(f == NULL) {
  609. fprintf(stderr, "Unable to open pstarter.o\n");
  610. exit(-1);
  611. }
  612. n = fread(array, sizeof(char), 65536*sizeof(char), f);
  613. fclose(f);
  614. // change varname
  615. for(i=0; i<n-strlen(token); i++) {
  616. for(j=0, k=0; j<strlen(token); j++) {
  617. if(array[i+j] == token[j]) {
  618. k++;
  619. }
  620. }
  621. if(k==strlen(token)) {
  622. //printf("offset = %i = 0x%04x\n", i, i);
  623. break;
  624. }
  625. }
  626. sprintf(array+i, "%s", packvar);
  627. // open file in writing
  628. f = fopen(output, "wb");
  629. if(f == NULL) {
  630. fprintf(stderr, "Unable to open pstarter.o\n");
  631. exit(-1);
  632. }
  633. fwrite(array, sizeof(char), n*sizeof(char), f);
  634. fclose(f);
  635. return 0;
  636. }
  637. /* Execute ar-tigcc */
  638. void ar(void)
  639. {
  640. int i, loop;
  641. char buffer[strlen(outfile) + 3];
  642. char ar_name[14 + strlen(tigcc_base)];
  643. char *argv[4 + obj_files->count + ar_files->count];
  644. argv[0] = ar_name;
  645. argv[1] = "-o";
  646. argv[2] = buffer;
  647. strcpy(buffer, outfile);
  648. change_extension(buffer, ".a");
  649. unlink (buffer);
  650. sprintf(ar_name, "%s/bin/ar-tigcc", tigcc_base);
  651. i = 3;
  652. for (loop = 0; loop < obj_files->count; loop++)
  653. argv[i++] = obj_files->files[loop];
  654. for (loop = 0; loop < ar_files->count; loop++)
  655. argv[i++] = ar_files->files[loop];
  656. argv[i] = NULL;
  657. execute(ar_name, argv);
  658. }
  659. /* Compress TI executable */
  660. void pack(void)
  661. {
  662. char pstarter_file[25 + strlen(tigcc_base)];
  663. char tmpfile[strlen(outfile) + 14];
  664. int ti89_targeted=0;
  665. // check for decompressor program
  666. sprintf(pstarter_file, "%s/lib/pstarter.o", tigcc_base);
  667. if(access(pstarter_file, F_OK) == -1) {
  668. fprintf(stderr, "TIGCC installation problem: pstarter.o file is missing.\n");
  669. return;
  670. }
  671. strcpy(tmpfile, outfile);
  672. change_extension(tmpfile, ".z89");
  673. if(access(tmpfile, F_OK) != -1) {
  674. ti89_targeted=1;
  675. // compress on calc variable
  676. {
  677. char ttpack_name[strlen (tigcc_base) + 12];
  678. char *argv[5];
  679. int i=1;
  680. *argv = ttpack_name;
  681. if (verbose)
  682. argv[i++] = "-v";
  683. else if (quiet)
  684. argv[i++] = "-quiet";
  685. argv[i++] = tmpfile;
  686. argv[i++] = TMP_PCK;
  687. argv[i] = NULL;
  688. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  689. execute(ttpack_name, argv);
  690. }
  691. // encapsulate in var (.89y)
  692. {
  693. char ttbin2oth_name[strlen(tigcc_base) + 15];
  694. char *argv[7];
  695. int i=1;
  696. *argv = ttbin2oth_name;
  697. if (quiet)
  698. argv[i++] = "-quiet";
  699. argv[i++] = "-89";
  700. argv[i++] = "ppg";
  701. argv[i++] = TMP_PCK;
  702. argv[i++] = packfile;
  703. argv[i] = NULL;
  704. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  705. execute(ttbin2oth_name, argv);
  706. }
  707. unlink(tmpfile);
  708. unlink(TMP_PCK);
  709. }
  710. change_extension(tmpfile, ".zv2");
  711. if(access(tmpfile, F_OK) != -1) {
  712. // compress on calc variable
  713. {
  714. char ttpack_name[strlen (tigcc_base) + 12];
  715. char *argv[5];
  716. int i=1;
  717. *argv = ttpack_name;
  718. if (verbose)
  719. argv[i++] = "-v";
  720. else if (quiet)
  721. argv[i++] = "-quiet";
  722. argv[i++] = tmpfile;
  723. argv[i++] = TMP_PCK;
  724. argv[i] = NULL;
  725. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  726. execute(ttpack_name, argv);
  727. }
  728. // encapsulate in var (.v2y)
  729. {
  730. char ttbin2oth_name[strlen(tigcc_base) + 15];
  731. char filename9x[strlen(packfile)+5];
  732. char filenamev2[strlen(packfile)+5];
  733. char *argv[7];
  734. int i=1;
  735. *argv = ttbin2oth_name;
  736. if (quiet)
  737. argv[i++] = "-quiet";
  738. argv[i++] = "-92";
  739. argv[i++] = "ppg";
  740. argv[i++] = TMP_PCK;
  741. argv[i++] = packfile;
  742. argv[i] = NULL;
  743. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  744. execute(ttbin2oth_name, argv);
  745. strcpy(filename9x,packfile);
  746. strcat(filename9x,".9xy");
  747. strcpy(filenamev2,packfile);
  748. strcat(filenamev2,".v2y");
  749. rename(filename9x,filenamev2);
  750. }
  751. unlink(tmpfile);
  752. unlink(TMP_PCK);
  753. }
  754. change_extension(tmpfile, ".z9x");
  755. if(access(tmpfile, F_OK) != -1) {
  756. // compress on calc variable
  757. {
  758. char ttpack_name[strlen (tigcc_base) + 12];
  759. char *argv[5];
  760. int i=1;
  761. *argv = ttpack_name;
  762. if (verbose)
  763. argv[i++] = "-v";
  764. else if (quiet)
  765. argv[i++] = "-quiet";
  766. argv[i++] = tmpfile;
  767. argv[i++] = TMP_PCK;
  768. argv[i] = NULL;
  769. sprintf (ttpack_name, "%s/bin/ttpack", tigcc_base);
  770. execute(ttpack_name, argv);
  771. }
  772. // encapsulate in var (.9xy)
  773. {
  774. char ttbin2oth_name[strlen(tigcc_base) + 15];
  775. char *argv[7];
  776. int i=1;
  777. *argv = ttbin2oth_name;
  778. if (quiet)
  779. argv[i++] = "-quiet";
  780. argv[i++] = "-92";
  781. argv[i++] = "ppg";
  782. argv[i++] = TMP_PCK;
  783. argv[i++] = packfile;
  784. argv[i] = NULL;
  785. sprintf(ttbin2oth_name, "%s/bin/ttbin2oth", tigcc_base);
  786. execute(ttbin2oth_name, argv);
  787. }
  788. unlink(tmpfile);
  789. unlink(TMP_PCK);
  790. }
  791. // parse pstarter.o and change varname
  792. parse_pstarter(pstarter_file, "pstarter.o", packfile);
  793. // create decompressor (.??z)
  794. {
  795. char ld_tigcc_name[strlen(tigcc_base) + 14];
  796. char *argv[] = {ld_tigcc_name, "pstarter.o", "-o", tmpfile, NULL};
  797. sprintf (ld_tigcc_name, "%s/bin/ld-tigcc", tigcc_base);
  798. change_extension(tmpfile, "");
  799. execute(ld_tigcc_name, argv);
  800. }
  801. unlink("pstarter.o");
  802. if (!ti89_targeted) return; // no TI-89 -> no Titanium
  803. #if 1
  804. // zap any leftover Titanium launchers
  805. strcat(tmpfile, "-titanium.89z");
  806. unlink(tmpfile);
  807. strcpy(tmpfile, outfile);
  808. strcat(tmpfile, "-Titanium.89z");
  809. unlink(tmpfile);
  810. #else
  811. // check for Titanium decompressor program
  812. sprintf(pstarter_file, "%s/lib/pstarter-titanium.o", tigcc_base);
  813. if(access(pstarter_file, F_OK) == -1) {
  814. fprintf(stderr, "TIGCC installation problem: pstarter-titanium.o file is missing.\n");
  815. return;
  816. }
  817. // parse pstarter.o and change varname
  818. parse_pstarter(pstarter_file, "pstarter.o", packfile);
  819. // create decompressor (*-titanium.89z)
  820. {
  821. char ld_tigcc_name[strlen(tigcc_base) + 14];
  822. char *argv[] = {ld_tigcc_name, "pstarter.o", "-o", tmpfile, NULL};
  823. sprintf (ld_tigcc_name, "%s/bin/ld-tigcc", tigcc_base);
  824. strcat(tmpfile, "-titanium");
  825. execute(ld_tigcc_name, argv);
  826. // zap non-TI-89 Titanium launchers
  827. change_extension(tmpfile, ".9xz");
  828. unlink(tmpfile);
  829. change_extension(tmpfile, ".v2z");
  830. unlink(tmpfile);
  831. }
  832. unlink("pstarter.o");
  833. #endif
  834. }
  835. /* Destroy allocated resources */
  836. void safe_exit(void)
  837. {
  838. free(gcc_argv);
  839. free(ld_argv);
  840. free_file_array(src_files);
  841. free_file_array(obj_files);
  842. free_file_array(asm_files);
  843. free_file_array(a68k_files);
  844. free_file_array(ar_files);
  845. free(as_args); free(a68k_args);
  846. }
  847. int main(int argc, char *argv[])
  848. {
  849. short int loop;
  850. program_name = argv[0];
  851. if (argc < 2) {
  852. fprintf(stderr, "tigcc: no input files\n");
  853. exit(0);
  854. }
  855. if ((tigcc_base = getenv("TIGCC")) == NULL) {
  856. 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");
  857. exit(-1);
  858. }
  859. if (atexit(safe_exit) != 0) {
  860. fprintf(stderr, "Fatal error: unable to register safe exit callback\n");
  861. exit(-1);
  862. }
  863. /* worst case (every arg is a gcc arg): argc + -Bprefix/bin/ + -Idir + -gdwarf-2 + -g3 +
  864. + -fasynchronous-unwind-tables + -mno-merge-sections + -S
  865. + -DFARGO/-DFLASH_OS + -include + quill.drv + -x + c + -o + filename */
  866. gcc_argv = malloc((argc + 15) * sizeof (char*));
  867. /* worst case: each arg is either a file to link or a linker switch; add
  868. tigcc.a/fargo.a/flashos.a, -o outfile, --outputbin, NULL */
  869. ld_argv = malloc((argc + 5) * sizeof (char*));
  870. src_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  871. obj_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  872. asm_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  873. a68k_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  874. ar_files = malloc(sizeof (array_of_files) + MAX_ARRAY_FILES * sizeof (char*));
  875. if ((gcc_argv == NULL) || (ld_argv == NULL) || (src_files == NULL) || (obj_files == NULL) ||
  876. (asm_files == NULL) || (a68k_files == NULL) || (ar_files == NULL)) {
  877. fprintf(stderr, "Fatal error: not enough free memory\n");
  878. exit(-1);
  879. }
  880. src_files -> count = 0;
  881. obj_files -> count = 0;
  882. asm_files -> count = 0;
  883. a68k_files -> count = 0;
  884. ar_files -> count = 0;
  885. // parse the arguments
  886. parse_args(&argc, argv);
  887. // check there is at least one file to process
  888. if(!src_files->count && !asm_files->count && !a68k_files->count && !obj_files->count)
  889. {
  890. fprintf(stderr, "tigcc: no input files\n");
  891. exit(0);
  892. }
  893. // set the archive file
  894. if(!nostdlib) {
  895. char *buffer = malloc(strlen(tigcc_base)+13);
  896. sprintf(buffer, flashos?"%s/lib/flashos.a":(fargo?"%s/lib/fargo.a":"%s/lib/tigcc.a"), tigcc_base);
  897. add_to_file_array(buffer, ar_files);
  898. }
  899. if(1) {
  900. for (loop = 0; loop < src_files->count; loop++) {
  901. compile(src_files->files[loop]);
  902. }
  903. }
  904. if (do_assemble) {
  905. for (loop = 0; loop < asm_files->count; loop++) {
  906. assemble(asm_files->files[loop]);
  907. }
  908. for (loop = 0; loop < a68k_files->count; loop++) {
  909. a68k(a68k_files->files[loop]);
  910. }
  911. }
  912. if (staticlib) ar(); else {
  913. if (do_link && obj_files->count > 0) {
  914. ld();
  915. }
  916. }
  917. if(delete) {
  918. for (loop = 0; loop < src_files->count; loop++) {
  919. char tmpfile[strlen(src_files->files[loop]) + 3];
  920. strcpy(tmpfile, src_files->files[loop]);
  921. change_extension(tmpfile, ".s");
  922. unlink(tmpfile);
  923. }
  924. }
  925. if(do_link && !keepobj) {
  926. for (loop = 0; loop < src_files->count; loop++) {
  927. char tmpfile[strlen(src_files->files[loop]) + 3];
  928. strcpy(tmpfile, src_files->files[loop]);
  929. change_extension(tmpfile, ".o");
  930. unlink(tmpfile);
  931. }
  932. for (loop = 0; loop < asm_files->count; loop++) {
  933. char tmpfile[strlen(asm_files->files[loop]) + 3];
  934. strcpy(tmpfile, asm_files->files[loop]);
  935. change_extension(tmpfile, ".o");
  936. unlink(tmpfile);
  937. }
  938. for (loop = 0; loop < a68k_files->count; loop++) {
  939. char tmpfile[strlen(a68k_files->files[loop]) + 3];
  940. strcpy(tmpfile, a68k_files->files[loop]);
  941. change_extension(tmpfile, ".o");
  942. unlink(tmpfile);
  943. }
  944. }
  945. if (!staticlib && do_pack && do_link) {
  946. pack();
  947. }
  948. return 0;
  949. }