main_opt.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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-2007 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. " -output-data-var <file> Write data variable (if any) to <file>.???\n"
  85. " -n --varname <[folder\\]name>\n"
  86. " Name output variable <folder\\name>\n"
  87. #ifdef DATA_VAR_SUPPORT
  88. " -d --data-var <[folder\\]name>\n"
  89. " Use data variable named <folder\\name>\n"
  90. " --data-var-copy={never|archived|always}\n"
  91. " Work on original data variable or copy\n"
  92. #endif /* DATA_VAR_SUPPORT */
  93. #ifdef PUCRUNCH_SUPPORT
  94. " --pack Compress main file with pucrunch\n"
  95. #endif /* PUCRUNCH_SUPPORT */
  96. "<file> can be an archive or an object file.\n"
  97. "Options marked with `(!)' are likely to cause errors in binary code.\n");
  98. goto Cleanup;
  99. }
  100. else
  101. #endif /* ENABLE_HELP */
  102. #ifdef ENABLE_STATS
  103. if ((ArgMatches ("verbose")) || (ArgMatches ("v")))
  104. DisplayStats = TRUE;
  105. else
  106. #endif /* ENABLE_STATS */
  107. #ifdef ENABLE_DUMP
  108. if (!(strncmp (Arg, "dump", sizeof ("dump") - 1)))
  109. {
  110. Arg += sizeof ("dump") - 1;
  111. if (*Arg)
  112. {
  113. int DumpIdx = atoi (Arg);
  114. if ((DumpIdx >= 0) && (DumpIdx < DUMP_COUNT))
  115. Dump [DumpIdx] = TRUE;
  116. }
  117. else
  118. memset (Dump, -1, sizeof (Dump));
  119. }
  120. else
  121. #endif /* ENABLE_DUMP */
  122. if (ArgMatches ("native"))
  123. Program.Type = PT_NATIVE;
  124. else
  125. #ifdef FLASH_OS_SUPPORT
  126. if (ArgMatches ("flash-os"))
  127. {
  128. Program.Type = PT_FLASH_OS;
  129. Warning (NULL, "Flash OS support in TIGCC is experimental.");
  130. }
  131. else
  132. #endif /* FLASH_OS_SUPPORT */
  133. #ifdef FARGO_SUPPORT
  134. if (ArgMatches ("fargo"))
  135. {
  136. Program.Type = PT_FARGO;
  137. Program.Calcs |= CALC_TI92;
  138. Warning (NULL, "Fargo support in TIGCC is experimental.");
  139. }
  140. else
  141. #endif /* FARGO_SUPPORT */
  142. if (ArgMatches ("remove-unused"))
  143. OptInfo->RemoveUnused = TRUE;
  144. else if (ArgMatches ("optimize-relocs"))
  145. OptInfo->OptimizeRelocs = TRUE;
  146. else if (ArgMatches ("optimize-code"))
  147. {
  148. OptInfo->OptimizeNOPs = TRUE;
  149. OptInfo->OptimizeReturns = TRUE;
  150. OptInfo->OptimizeBranches = TRUE;
  151. OptInfo->OptimizeMoves = TRUE;
  152. OptInfo->OptimizeTests = TRUE;
  153. OptInfo->OptimizeCalcs = TRUE;
  154. }
  155. else if (ArgMatches ("optimize-nops"))
  156. OptInfo->OptimizeNOPs = TRUE;
  157. else if (ArgMatches ("optimize-returns"))
  158. OptInfo->OptimizeReturns = TRUE;
  159. else if (ArgMatches ("optimize-branches"))
  160. OptInfo->OptimizeBranches = TRUE;
  161. else if (ArgMatches ("optimize-moves"))
  162. OptInfo->OptimizeMoves = TRUE;
  163. else if (ArgMatches ("optimize-tests"))
  164. OptInfo->OptimizeTests = TRUE;
  165. else if (ArgMatches ("optimize-calcs"))
  166. OptInfo->OptimizeCalcs = TRUE;
  167. else if (ArgMatches ("cut-ranges"))
  168. OptInfo->CutRanges = TRUE;
  169. else if (ArgMatches ("reorder-sections"))
  170. OptInfo->ReorderSections = TRUE;
  171. else if (ArgMatches ("merge-constants"))
  172. OptInfo->MergeConstants = TRUE;
  173. else if (ArgMatches ("omit-bss-init"))
  174. OmitBSSInitialization = TRUE;
  175. else if (ArgMatches ("outputbin"))
  176. OutputBin = TRUE;
  177. else if (ArgMatches ("outputbin-main-only"))
  178. OutputBinMainOnly = TRUE;
  179. else if ((ArgMatches ("output")) || (ArgMatches ("o")))
  180. {
  181. if ((++CurArg) < ArgCount)
  182. {
  183. DestFile = Args [CurArg];
  184. DestFileSize = strlen (DestFile);
  185. // Adjust the program's name if it isn't set yet.
  186. if (!(*ProgramName))
  187. {
  188. // Start at the last separator.
  189. const char *Name = strrchr (DestFile, '/');
  190. if (!Name)
  191. Name = strrchr (DestFile, '\\');
  192. if (Name)
  193. Name++;
  194. else
  195. Name = DestFile;
  196. // Copy into name field.
  197. strncpy (ProgramName, Name, MAX_NAME_LEN);
  198. {
  199. // Cut at first dot.
  200. char *S = strchr (ProgramName, '.');
  201. if (S)
  202. *S = 0;
  203. // Cut at first space.
  204. S = strchr (ProgramName, ' ');
  205. if (S)
  206. *S = 0;
  207. // Convert to lowercase.
  208. for (S = ProgramName; *S; S++)
  209. *S = CalcTolower (*S);
  210. }
  211. }
  212. }
  213. else
  214. Error (NULL, "`--output' option must be followed by a name.");
  215. }
  216. else if (ArgMatches ("output-data-var"))
  217. {
  218. if ((++CurArg) < ArgCount)
  219. {
  220. DestDataFile = Args [CurArg];
  221. DestDataFileSize = strlen (DestDataFile);
  222. }
  223. else
  224. Error (NULL, "`--output-data-var' option must be followed by a name.");
  225. }
  226. else if ((ArgMatches ("varname")) || (ArgMatches ("n")))
  227. {
  228. if ((++CurArg) < ArgCount)
  229. {
  230. // Extract variable name.
  231. const char *Name = strchr (Args [CurArg], '/');
  232. if (!Name)
  233. Name = strchr (Args [CurArg], '\\');
  234. if (Name)
  235. {
  236. Name++;
  237. // Extract folder name.
  238. DecodeOnCalcName (ProgramFolder, Args [CurArg]);
  239. {
  240. char *S;
  241. for (S = ProgramFolder; *S; S++)
  242. {
  243. if (*S == '/' || *S == '\\')
  244. {
  245. *S = 0;
  246. break;
  247. }
  248. else
  249. *S = CalcTolower (*S);
  250. }
  251. }
  252. }
  253. else
  254. Name = Args [CurArg];
  255. DecodeOnCalcName (ProgramName, Name);
  256. {
  257. char *S;
  258. for (S = ProgramName; *S; S++)
  259. *S = CalcTolower (*S);
  260. }
  261. }
  262. else
  263. Error (NULL, "`--varname' option must be followed by a name.");
  264. }
  265. #ifdef DATA_VAR_SUPPORT
  266. else if (ArgMatches ("data-var") || ArgMatches ("d"))
  267. {
  268. if ((++CurArg) < ArgCount)
  269. {
  270. // Extract variable name.
  271. const char *Name = strchr (Args [CurArg], '/');
  272. if (!Name)
  273. Name = strchr (Args [CurArg], '\\');
  274. if (Name)
  275. {
  276. Name++;
  277. // Extract folder name.
  278. DecodeOnCalcName (DataFolder, Args [CurArg]);
  279. {
  280. char *S;
  281. for (S = DataFolder; *S; S++)
  282. {
  283. if (*S == '/' || *S == '\\')
  284. {
  285. *S = 0;
  286. break;
  287. }
  288. else
  289. *S = CalcTolower (*S);
  290. }
  291. }
  292. sprintf (DataVarString, "%s\\", DataFolder);
  293. }
  294. else
  295. {
  296. Name = Args [CurArg];
  297. *DataVarString = 0;
  298. }
  299. DecodeOnCalcName (DataName, Name);
  300. {
  301. char *S;
  302. for (S = DataName; *S; S++)
  303. *S = CalcTolower (*S);
  304. }
  305. strcat (DataVarString, DataName);
  306. DatVarInfo->Name = DataVarString;
  307. DatVarInfo->CreateCopy = TRUE;
  308. DatVarInfo->CopyOnlyIfArchived = TRUE;
  309. }
  310. else
  311. Error (NULL, "`--data-var' option must be followed by a name.");
  312. }
  313. else if (!(strncmp (Arg, "data-var-copy=", sizeof ("data-var-copy=") - 1)))
  314. {
  315. Arg += sizeof ("data-var-copy=") - 1;
  316. if (ArgMatches ("always"))
  317. {
  318. DatVarInfo->CreateCopy = TRUE;
  319. DatVarInfo->CopyOnlyIfArchived = FALSE;
  320. }
  321. else if (ArgMatches ("archived"))
  322. {
  323. DatVarInfo->CreateCopy = TRUE;
  324. DatVarInfo->CopyOnlyIfArchived = TRUE;
  325. }
  326. else if (ArgMatches ("never"))
  327. {
  328. DatVarInfo->CreateCopy = FALSE;
  329. DatVarInfo->CopyOnlyIfArchived = FALSE;
  330. }
  331. else
  332. Error (NULL, "Unrecognized copying condition `%s'.", Arg);
  333. }
  334. #endif /* DATA_VAR_SUPPORT */
  335. #ifdef PUCRUNCH_SUPPORT
  336. else if (ArgMatches ("pack"))
  337. Pack = TRUE;
  338. #endif /* PUCRUNCH_SUPPORT */
  339. else
  340. Error (NULL, "Unrecognized option `%s'.", Args [CurArg]);
  341. }
  342. else
  343. {
  344. // Treat it as a file name.
  345. // Open the file and load it into memory.
  346. {
  347. FILE *File = fopen (Arg, "rb");
  348. if (File)
  349. {
  350. SIZE Size;
  351. fseek (File, 0, SEEK_END);
  352. Size = ftell (File);
  353. rewind (File);
  354. {
  355. I1 *Data = malloc (Size);
  356. if (Data)
  357. {
  358. BOOLEAN IsArchive = FALSE;
  359. if (fread (Data, Size, 1, File) == 1)
  360. {
  361. // Act differently depending on the
  362. // file type.
  363. IsArchive = IsArchiveFile (Data, Size);
  364. if (IsArchive)
  365. {
  366. // Add the file to the list of
  367. // available archives.
  368. AddArchiveFile (&Program, Data, Size, Arg);
  369. }
  370. else
  371. {
  372. // Adjust the destination file
  373. // name if it isn't set yet.
  374. if (!DestFile)
  375. {
  376. const char *DotPos, *SlashPos;
  377. // Assign the destination file name.
  378. DestFile = Arg;
  379. DestFileSize = strlen (Arg);
  380. // Cut it at the last dot.
  381. DotPos = strrchr (Arg, '.');
  382. if (DotPos)
  383. {
  384. SlashPos = strrchr (Arg, '/');
  385. if (!SlashPos)
  386. SlashPos = strrchr (Arg, '\\');
  387. if ((!SlashPos) || (DotPos > SlashPos))
  388. DestFileSize = DotPos - Arg;
  389. }
  390. }
  391. // Import the file.
  392. ImportObjectFile (&Program, Data, Size, Arg);
  393. }
  394. }
  395. else
  396. Error (Arg, "Unable to read file.");
  397. // Archives are not imported; they keep
  398. // their data until they are freed.
  399. if (!IsArchive)
  400. free (Data);
  401. }
  402. else
  403. Error (Arg, "Not enough memory to load file.");
  404. }
  405. fclose (File);
  406. }
  407. else
  408. Error (Arg, "Unable to open file.");
  409. }
  410. }
  411. }
  412. }
  413. // Adjust the program's name if it isn't set yet.
  414. if (DestFile && (!(*ProgramName)))
  415. {
  416. // Start at the last separator.
  417. const char *Name = strrchr (DestFile, '/');
  418. if (!Name)
  419. Name = strrchr (DestFile, '\\');
  420. if (Name)
  421. Name++;
  422. else
  423. Name = DestFile;
  424. // Copy into name field.
  425. strncpy (ProgramName, Name, MAX_NAME_LEN);
  426. {
  427. // Cut at first dot.
  428. char *S = strchr (ProgramName, '.');
  429. if (S)
  430. *S = 0;
  431. // Cut at first space.
  432. S = strchr (ProgramName, ' ');
  433. if (S)
  434. *S = 0;
  435. // Convert to lowercase.
  436. for (S = ProgramName; *S; S++)
  437. *S = CalcTolower (*S);
  438. }
  439. }
  440. }