1 /*****************************************************************************/
5 /* Main program for the ca65 macroassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
42 #include "../common/version.h"
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Usage (void)
70 /* Print usage information and exit */
73 "Usage: %s [options] file\n"
75 " -g\t\t\tAdd debug info to object file\n"
76 " -i\t\t\tIgnore case of symbols\n"
77 " -l\t\t\tCreate a listing if assembly was ok\n"
78 " -o name\t\tName the output file\n"
79 " -s\t\t\tEnable smart mode\n"
80 " -v\t\t\tIncrease verbosity\n"
81 " -D name[=value]\tDefine a symbol\n"
82 " -I dir\t\tSet an include directory search path\n"
83 " -U\t\t\tMark unresolved symbols as import\n"
84 " -V\t\t\tPrint the assembler version\n"
85 " -W n\t\t\tSet warning level n\n"
88 " --auto-import\t\tMark unresolved symbols as import\n"
89 " --cpu type\t\tSet cpu type\n"
90 " --debug-info\t\tAdd debug info to object file\n"
91 " --ignore-case\t\tIgnore case of symbols\n"
92 " --include-dir dir\tSet an include directory search path\n"
93 " --listing\t\tCreate a listing if assembly was ok\n"
94 " --pagelength n\tSet the page length for the listing\n"
95 " --smart\t\tEnable smart mode\n"
96 " --verbose\t\tIncrease verbosity\n"
97 " --version\t\tPrint the assembler version\n",
104 static void UnknownOption (const char* Arg)
105 /* Print an error about an unknown option. Print usage information and exit */
107 fprintf (stderr, "Unknown option: %s\n", Arg);
113 static void NeedArg (const char* Arg)
114 /* Print an error about a missing option argument and exit. */
116 fprintf (stderr, "Option requires an argument: %s\n", Arg);
122 static void InvSym (const char* Def)
123 /* Print an error about an invalid symbol definition and die */
125 fprintf (stderr, "Invalid symbol definition: `%s'\n", Def);
131 static const char* GetArg (int* ArgNum, char* argv [], unsigned Len)
132 /* Get an option argument */
134 const char* Arg = argv [*ArgNum];
135 if (Arg [Len] != '\0') {
136 /* Argument appended */
139 /* Separate argument */
140 Arg = argv [*ArgNum + 1];
142 /* End of arguments */
143 NeedArg (argv [*ArgNum]);
152 static void SetOptions (void)
153 /* Set the option for the translator */
157 /* Set the translator */
158 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
161 /* Set date and time */
162 OptDateTime ((unsigned long) time(0));
167 static void DefineSymbol (const char* Def)
168 /* Define a symbol from the command line */
173 char SymName [MAX_STR_LEN+1];
175 /* The symbol must start with a character or underline */
176 if (Def [0] != '_' && !isalpha (Def [0])) {
181 /* Copy the symbol, checking the rest */
183 while (isalnum (*P) || *P == '_') {
184 if (I <= MAX_STR_LEN) {
191 /* Do we have a value given? */
198 /* We have a value */
202 if (sscanf (P, "%lx", &Val) != 1) {
206 if (sscanf (P, "%li", &Val) != 1) {
212 /* Check if have already a symbol with this name */
213 if (SymIsDef (SymName)) {
214 fprintf (stderr, "`%s' is already defined\n", SymName);
218 /* Define the symbol */
219 SymDef (SymName, LiteralExpr (Val), 0);
224 static void OptAutoImport (const char* Opt)
225 /* Mark unresolved symbols as imported */
232 static void OptCPU (const char* Opt, const char* Arg)
233 /* Handle the --cpu option */
238 if (strcmp (Arg, "6502") == 0) {
240 } else if (strcmp (Arg, "65C02") == 0) {
242 } else if (strcmp (Arg, "65816") == 0) {
245 } else if (strcmp (Arg, "sunplus") == 0) {
246 SetCPU (CPU_SUNPLUS);
249 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
256 static void OptDebugInfo (const char* Opt)
257 /* Add debug info to the object file */
264 static void OptIgnoreCase (const char* Opt)
265 /* Ignore case on symbols */
272 static void OptIncludeDir (const char* Opt, const char* Arg)
273 /* Add an include search path */
278 AddIncludePath (Arg);
283 static void OptListing (const char* Opt)
284 /* Create a listing file */
291 static void OptPageLength (const char* Opt, const char* Arg)
292 /* Handle the --pagelength option */
299 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
300 fprintf (stderr, "Invalid page length: %d\n", Len);
308 static void OptSmart (const char* Opt)
309 /* Handle the -s/--smart options */
316 static void OptVerbose (const char* Opt)
317 /* Increase verbosity */
324 static void OptVersion (const char* Opt)
325 /* Print the assembler version */
328 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
329 VER_MAJOR, VER_MINOR, VER_PATCH);
334 static void LongOption (int* ArgNum, char* argv [])
335 /* Handle a long command line option */
337 const char* Opt = argv [*ArgNum];
338 const char* Arg = argv [*ArgNum+1];
340 if (strcmp (Opt, "--auto-import") == 0) {
342 } else if (strcmp (Opt, "--cpu") == 0) {
345 } else if (strcmp (Opt, "--debug-info") == 0) {
347 } else if (strcmp (Opt, "--ignore-case") == 0) {
349 } else if (strcmp (Opt, "--include-dir") == 0) {
350 OptIncludeDir (Opt, Arg);
352 } else if (strcmp (Opt, "--listing") == 0) {
354 } else if (strcmp (Opt, "--pagelength") == 0) {
355 OptPageLength (Opt, Arg);
357 } else if (strcmp (Opt, "--smart") == 0) {
359 } else if (strcmp (Opt, "--verbose") == 0) {
361 } else if (strcmp (Opt, "--version") == 0) {
370 static void OneLine (void)
371 /* Assemble one line */
373 char Ident [MAX_STR_LEN+1];
376 /* Initialize the listing line */
379 if (Tok == TOK_COLON) {
380 /* An unnamed label */
385 /* Assemble the line */
386 if (Tok == TOK_IDENT) {
389 if (IsMacro (SVal)) {
391 /* Yes, start a macro expansion */
397 /* No, label. Remember the identifier, then skip it */
398 int HadWS = WS; /* Did we have whitespace before the ident? */
399 strcpy (Ident, SVal);
402 /* If a colon follows, this is a label definition. If there
403 * is no colon, it's an assignment.
408 /* Define the symbol with the expression following the
411 SymDef (Ident, Expression (), 0);
412 /* Don't allow anything after a symbol definition */
416 SymDef (Ident, CurrentPC (), IsZPSeg ());
417 /* Skip the colon. If NoColonLabels is enabled, allow labels
418 * without a colon if there is no whitespace before the
421 if (Tok != TOK_COLON) {
422 if (HadWS || !NoColonLabels) {
423 Error (ERR_COLON_EXPECTED);
425 if (Tok == TOK_NAMESPACE) {
426 /* Smart :: handling */
439 if (TokIsPseudo (Tok)) {
440 /* A control command, IVal is index into table */
442 } else if (Tok == TOK_MNEMO) {
443 /* A mnemonic - assemble one instruction */
444 HandleInstruction (IVal);
445 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
446 /* A macro expansion */
451 /* Calling InitListingLine again here is part of a hack that introduces
452 * enough magic to make the PC output in the listing work.
456 /* Line separator must come here */
462 static void Assemble (void)
463 /* Start the ball rolling ... */
468 /* Assemble lines until end of file */
469 while (Tok != TOK_EOF) {
476 static void CreateObjFile (void)
477 /* Create the object file */
479 /* Open the object, write the header */
482 /* Write the object file options */
485 /* Write the list of input files */
488 /* Write the segment data to the file */
491 /* Write the import list */
494 /* Write the export list */
497 /* Write debug symbols if requested */
500 /* Write an updated header and close the file */
506 int main (int argc, char* argv [])
507 /* Assembler main program */
511 /* Set the program name */
514 /* We must have a file name */
519 /* Enter the base lexical level. We must do that here, since we may
520 * define symbols using -D.
524 /* Check the parameters */
528 /* Get the argument */
529 const char* Arg = argv [I];
531 /* Check for an option */
532 if (Arg [0] == '-') {
536 LongOption (&I, argv);
552 OutFile = GetArg (&I, argv, 2);
564 DefineSymbol (GetArg (&I, argv, 2));
568 OptIncludeDir (Arg, GetArg (&I, argv, 2));
580 WarnLevel = atoi (GetArg (&I, argv, 2));
589 /* Filename. Check if we already had one */
591 fprintf (stderr, "Don't know what to do with `%s'\n", Arg);
602 /* Do we have an input file? */
604 fprintf (stderr, "No input file\n");
608 /* Initialize the scanner, open the input file */
609 InitScanner (InFile);
611 /* Define the default options */
614 /* Assemble the input */
617 /* If we didn't have any errors, check the unnamed labels */
618 if (ErrorCount == 0) {
622 /* If we didn't have any errors, check the symbol table */
623 if (ErrorCount == 0) {
627 /* If we didn't have any errors, check and resolve the segment data */
628 if (ErrorCount == 0) {
638 /* If we didn't have any errors, create the object and listing files */
639 if (ErrorCount == 0) {
646 /* Close the input file */
649 /* Return an apropriate exit code */
650 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;