main_opt.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* main_opt.inc: Option parser for ld-tigcc command-line program
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2004 Billy Charvet
  4. Copyright (C) 2004-2006 Kevin Kofler
  5. This program 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 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. // This file is included by main.c.
  17. {
  18. const char *Arg;
  19. BOOLEAN ArgMatches (const char *Name)
  20. {
  21. return (!(strcmp (Arg, Name)));
  22. }
  23. // For each argument...
  24. for (CurArg = 1; CurArg < ArgCount; CurArg++)
  25. {
  26. // Get pointer to arg.
  27. Arg = Args [CurArg];
  28. // Check whether it is empty.
  29. if (Arg && *Arg)
  30. {
  31. // Check whether it is an option.
  32. if (Arg [0] == '-')
  33. {
  34. // Skip this dash and possibly a second one.
  35. Arg++;
  36. if (Arg [0] == '-')
  37. Arg++;
  38. // Handle the option.
  39. #ifdef ENABLE_HELP
  40. if (ArgMatches ("version"))
  41. {
  42. printf ("ld-tigcc Version " PROGRAM_VERSION_STRING "\n"
  43. COPYRIGHT_NOTICE_STRING "\n"
  44. "ld-tigcc is free software; see the source code for details.\n");
  45. goto Cleanup;
  46. }
  47. else if ((ArgMatches ("help")) || (ArgMatches ("h")))
  48. {
  49. printf ("Usage: ld-tigcc [options] <file> [<file> ...]\n"
  50. "Options:\n"
  51. " -h --help Display this message\n"
  52. " --version Display version number\n"
  53. #ifdef ENABLE_STATS
  54. " -v --verbose Display program statistics\n"
  55. #endif /* ENABLE_STATS */
  56. #ifdef ENABLE_DUMP
  57. " --dump[n] Display [n-th] dump of the program contents\n"
  58. #endif /* ENABLE_DUMP */
  59. " --native Link in TIGCC native mode\n"
  60. #ifdef FLASH_OS_SUPPORT
  61. " --flash-os Create (unsigned) Flash OS\n"
  62. #endif /* FLASH_OS_SUPPORT */
  63. #ifdef FARGO_SUPPORT
  64. " --fargo Create Fargo II program\n"
  65. #endif /* FARGO_SUPPORT */
  66. " --remove-unused Remove unused sections\n"
  67. " --optimize-relocs Update relocs to point to the closest symbol\n"
  68. " --optimize-code Perform all MC68000 code optimizations (!)\n"
  69. " --optimize-nops Remove format-specific NOP instructions\n"
  70. " --optimize-returns Optimize function return sequences (!)\n"
  71. " --optimize-branches Optimize branch instructions\n"
  72. " --optimize-moves Optimize move/load/push instructions (!)\n"
  73. " --optimize-tests Optimize compare/test instructions (!)\n"
  74. " --optimize-calcs Optimize calculation instructions (!)\n"
  75. " --cut-ranges Cut unneeded section ranges when optimizing (!)\n"
  76. " (ignored for files not assembled in all-relocs mode)\n"
  77. " --reorder-sections Reorder sections to make references shorter\n"
  78. " --merge-constants Merge constants and strings to avoid duplication\n"
  79. " --omit-bss-init Do not initialize BSS data to zero\n"
  80. " --outputbin Create program image without PC header\n"
  81. " --outputbin-main-only\n"
  82. " Create main file w/o PC header (but data file with)\n"
  83. " -o --output <file> Write output to <file>.???\n"
  84. " -n --varname <[folder\\]name>\n"
  85. " Name output variable <folder\\name>\n"
  86. #ifdef DATA_VAR_SUPPORT
  87. " -d --data-var <[folder\\]name>\n"
  88. " Use data variable named <folder\\name>\n"
  89. " --data-var-copy={never|archived|always}\n"
  90. " Work on original data variable or copy\n"
  91. #endif /* DATA_VAR_SUPPORT */
  92. #ifdef PUCRUNCH_SUPPORT
  93. " --pack Compress main file with pucrunch\n"
  94. #endif /* PUCRUNCH_SUPPORT */
  95. "<file> can be an archive or an object file.\n"
  96. "Options marked with `(!)' are likely to cause errors in binary code.\n");
  97. goto Cleanup;
  98. }
  99. else
  100. #endif /* ENABLE_HELP */
  101. #ifdef ENABLE_STATS
  102. if ((ArgMatches ("verbose")) || (ArgMatches ("v")))
  103. DisplayStats = TRUE;
  104. else
  105. #endif /* ENABLE_STATS */
  106. #ifdef ENABLE_DUMP
  107. if (!(strncmp (Arg, "dump", sizeof ("dump") - 1)))
  108. {
  109. Arg += sizeof ("dump") - 1;
  110. if (*Arg)
  111. {
  112. int DumpIdx = atoi (Arg);
  113. if ((DumpIdx >= 0) && (DumpIdx < DUMP_COUNT))
  114. Dump [DumpIdx] = TRUE;
  115. }
  116. else
  117. memset (Dump, -1, sizeof (Dump));
  118. }
  119. else
  120. #endif /* ENABLE_DUMP */
  121. if (ArgMatches ("native"))
  122. Program.Type = PT_NATIVE;
  123. else
  124. #ifdef FLASH_OS_SUPPORT
  125. if (ArgMatches ("flash-os"))
  126. {
  127. Program.Type = PT_FLASH_OS;
  128. Warning (NULL, "Flash OS support in TIGCC is experimental.");
  129. }
  130. else
  131. #endif /* FLASH_OS_SUPPORT */
  132. #ifdef FARGO_SUPPORT
  133. if (ArgMatches ("fargo"))
  134. {
  135. Program.Type = PT_FARGO;
  136. Program.Calcs |= CALC_TI92;
  137. Warning (NULL, "Fargo support in TIGCC is experimental.");
  138. }
  139. else
  140. #endif /* FARGO_SUPPORT */
  141. if (ArgMatches ("remove-unused"))
  142. OptInfo->RemoveUnused = TRUE;
  143. else if (ArgMatches ("optimize-relocs"))
  144. OptInfo->OptimizeRelocs = TRUE;
  145. else if (ArgMatches ("optimize-code"))
  146. {
  147. OptInfo->OptimizeNOPs = TRUE;
  148. OptInfo->OptimizeReturns = TRUE;
  149. OptInfo->OptimizeBranches = TRUE;
  150. OptInfo->OptimizeMoves = TRUE;
  151. OptInfo->OptimizeTests = TRUE;
  152. OptInfo->OptimizeCalcs = TRUE;
  153. }
  154. else if (ArgMatches ("optimize-nops"))
  155. OptInfo->OptimizeNOPs = TRUE;
  156. else if (ArgMatches ("optimize-returns"))
  157. OptInfo->OptimizeReturns = TRUE;
  158. else if (ArgMatches ("optimize-branches"))
  159. OptInfo->OptimizeBranches = TRUE;
  160. else if (ArgMatches ("optimize-moves"))
  161. OptInfo->OptimizeMoves = TRUE;
  162. else if (ArgMatches ("optimize-tests"))
  163. OptInfo->OptimizeTests = TRUE;
  164. else if (ArgMatches ("optimize-calcs"))
  165. OptInfo->OptimizeCalcs = TRUE;
  166. else if (ArgMatches ("cut-ranges"))
  167. OptInfo->CutRanges = TRUE;
  168. else if (ArgMatches ("reorder-sections"))
  169. OptInfo->ReorderSections = TRUE;
  170. else if (ArgMatches ("merge-constants"))
  171. OptInfo->MergeConstants = TRUE;
  172. else if (ArgMatches ("omit-bss-init"))
  173. OmitBSSInitialization = TRUE;
  174. else if (ArgMatches ("outputbin"))
  175. OutputBin = TRUE;
  176. else if (ArgMatches ("outputbin-main-only"))
  177. OutputBinMainOnly = TRUE;
  178. else if ((ArgMatches ("output")) || (ArgMatches ("o")))
  179. {
  180. if ((++CurArg) < ArgCount)
  181. {
  182. DestFile = Args [CurArg];
  183. DestFileSize = strlen (DestFile);
  184. // Adjust the program's name if it isn't set yet.
  185. if (!(*ProgramName))
  186. {
  187. // Start at the last separator.
  188. const char *Name = strrchr (DestFile, '/');
  189. if (!Name)
  190. Name = strrchr (DestFile, '\\');
  191. if (Name)
  192. Name++;
  193. else
  194. Name = DestFile;
  195. // Copy into name field.
  196. strncpy (ProgramName, Name, MAX_NAME_LEN);
  197. {
  198. // Cut at first dot.
  199. char *S = strchr (ProgramName, '.');
  200. if (S)
  201. *S = 0;
  202. // Cut at first space.
  203. S = strchr (ProgramName, ' ');
  204. if (S)
  205. *S = 0;
  206. // Convert to lowercase.
  207. for (S = ProgramName; *S; S++)
  208. *S = tolower (*S);
  209. }
  210. }
  211. }
  212. else
  213. Error (NULL, "`--output' option must be followed by a name.");
  214. }
  215. else if (ArgMatches ("output-data-var"))
  216. {
  217. if ((++CurArg) < ArgCount)
  218. {
  219. DestDataFile = Args [CurArg];
  220. DestDataFileSize = strlen (DestDataFile);
  221. }
  222. else
  223. Error (NULL, "`--output-data-var' option must be followed by a name.");
  224. }
  225. else if ((ArgMatches ("varname")) || (ArgMatches ("n")))
  226. {
  227. if ((++CurArg) < ArgCount)
  228. {
  229. // Extract variable name.
  230. const char *Name = strchr (Args [CurArg], '/');
  231. if (!Name)
  232. Name = strchr (Args [CurArg], '\\');
  233. if (Name)
  234. {
  235. Name++;
  236. // Extract folder name.
  237. strncpy (ProgramFolder, Args [CurArg], MAX_NAME_LEN);
  238. {
  239. char *S;
  240. for (S = ProgramFolder; *S; S++)
  241. {
  242. if (*S == '/' || *S == '\\')
  243. {
  244. *S = 0;
  245. break;
  246. }
  247. else
  248. *S = tolower (*S);
  249. }
  250. }
  251. }
  252. else
  253. Name = Args [CurArg];
  254. strncpy (ProgramName, Name, MAX_NAME_LEN);
  255. {
  256. char *S;
  257. for (S = ProgramName; *S; S++)
  258. *S = tolower (*S);
  259. }
  260. }
  261. else
  262. Error (NULL, "`--varname' option must be followed by a name.");
  263. }
  264. #ifdef DATA_VAR_SUPPORT
  265. else if (ArgMatches ("data-var") || ArgMatches ("d"))
  266. {
  267. if ((++CurArg) < ArgCount)
  268. {
  269. // Extract variable name.
  270. const char *Name = strchr (Args [CurArg], '/');
  271. if (!Name)
  272. Name = strchr (Args [CurArg], '\\');
  273. if (Name)
  274. {
  275. Name++;
  276. // Extract folder name.
  277. strncpy (DataFolder, Args [CurArg], MAX_NAME_LEN);
  278. {
  279. char *S;
  280. for (S = DataFolder; *S; S++)
  281. {
  282. if (*S == '/' || *S == '\\')
  283. {
  284. *S = 0;
  285. break;
  286. }
  287. else
  288. *S = tolower (*S);
  289. }
  290. }
  291. sprintf (DataVarString, "%s\\", DataFolder);
  292. }
  293. else
  294. {
  295. Name = Args [CurArg];
  296. *DataVarString = 0;
  297. }
  298. strncpy (DataName, Name, MAX_NAME_LEN);
  299. {
  300. char *S;
  301. for (S = DataName; *S; S++)
  302. *S = tolower (*S);
  303. }
  304. strcat (DataVarString, DataName);
  305. DatVarInfo->Name = DataVarString;
  306. DatVarInfo->CreateCopy = TRUE;
  307. DatVarInfo->CopyOnlyIfArchived = TRUE;
  308. }
  309. else
  310. Error (NULL, "`--data-var' option must be followed by a name.");
  311. }
  312. else if (!(strncmp (Arg, "data-var-copy=", sizeof ("data-var-copy=") - 1)))
  313. {
  314. Arg += sizeof ("data-var-copy=") - 1;
  315. if (ArgMatches ("always"))
  316. {
  317. DatVarInfo->CreateCopy = TRUE;
  318. DatVarInfo->CopyOnlyIfArchived = FALSE;
  319. }
  320. else if (ArgMatches ("archived"))
  321. {
  322. DatVarInfo->CreateCopy = TRUE;
  323. DatVarInfo->CopyOnlyIfArchived = TRUE;
  324. }
  325. else if (ArgMatches ("never"))
  326. {
  327. DatVarInfo->CreateCopy = FALSE;
  328. DatVarInfo->CopyOnlyIfArchived = FALSE;
  329. }
  330. else
  331. Error (NULL, "Unrecognized copying condition `%s'.", Arg);
  332. }
  333. #endif /* DATA_VAR_SUPPORT */
  334. #ifdef PUCRUNCH_SUPPORT
  335. else if (ArgMatches ("pack"))
  336. Pack = TRUE;
  337. #endif /* PUCRUNCH_SUPPORT */
  338. else
  339. Error (NULL, "Unrecognized option `%s'.", Args [CurArg]);
  340. }
  341. else
  342. {
  343. // Treat it as a file name.
  344. // Open the file and load it into memory.
  345. {
  346. FILE *File = fopen (Arg, "rb");
  347. if (File)
  348. {
  349. SIZE Size;
  350. fseek (File, 0, SEEK_END);
  351. Size = ftell (File);
  352. rewind (File);
  353. {
  354. I1 *Data = malloc (Size);
  355. if (Data)
  356. {
  357. BOOLEAN IsArchive = FALSE;
  358. if (fread (Data, Size, 1, File) == 1)
  359. {
  360. // Act differently depending on the
  361. // file type.
  362. IsArchive = IsArchiveFile (Data, Size);
  363. if (IsArchive)
  364. {
  365. // Add the file to the list of
  366. // available archives.
  367. AddArchiveFile (&Program, Data, Size, Arg);
  368. }
  369. else
  370. {
  371. // Adjust the destination file
  372. // name if it isn't set yet.
  373. if (!DestFile)
  374. {
  375. const char *DotPos, *SlashPos;
  376. // Assign the destination file name.
  377. DestFile = Arg;
  378. DestFileSize = strlen (Arg);
  379. // Cut it at the last dot.
  380. DotPos = strrchr (Arg, '.');
  381. if (DotPos)
  382. {
  383. SlashPos = strrchr (Arg, '/');
  384. if (!SlashPos)
  385. SlashPos = strrchr (Arg, '\\');
  386. if ((!SlashPos) || (DotPos > SlashPos))
  387. DestFileSize = DotPos - Arg;
  388. }
  389. }
  390. // Import the file.
  391. ImportObjectFile (&Program, Data, Size, Arg);
  392. }
  393. }
  394. else
  395. Error (Arg, "Unable to read file.");
  396. // Archives are not imported; they keep
  397. // their data until they are freed.
  398. if (!IsArchive)
  399. free (Data);
  400. }
  401. else
  402. Error (Arg, "Not enough memory to load file.");
  403. }
  404. fclose (File);
  405. }
  406. else
  407. Error (Arg, "Unable to open file.");
  408. }
  409. }
  410. }
  411. }
  412. // Adjust the program's name if it isn't set yet.
  413. if (DestFile && (!(*ProgramName)))
  414. {
  415. // Start at the last separator.
  416. const char *Name = strrchr (DestFile, '/');
  417. if (!Name)
  418. Name = strrchr (DestFile, '\\');
  419. if (Name)
  420. Name++;
  421. else
  422. Name = DestFile;
  423. // Copy into name field.
  424. strncpy (ProgramName, Name, MAX_NAME_LEN);
  425. {
  426. // Cut at first dot.
  427. char *S = strchr (ProgramName, '.');
  428. if (S)
  429. *S = 0;
  430. // Cut at first space.
  431. S = strchr (ProgramName, ' ');
  432. if (S)
  433. *S = 0;
  434. // Convert to lowercase.
  435. for (S = ProgramName; *S; S++)
  436. *S = tolower (*S);
  437. }
  438. }
  439. }