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"
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void Usage (void)
69 /* Print usage information and exit */
72 "Usage: %s [options] file\n"
74 " -g\t\t\tAdd debug info to object file\n"
75 " -i\t\t\tIgnore case of symbols\n"
76 " -l\t\t\tCreate a listing if assembly was ok\n"
77 " -o name\t\tName the output file\n"
78 " -s\t\t\tEnable smart mode\n"
79 " -v\t\t\tIncrease verbosity\n"
80 " -D name[=value]\tDefine a symbol\n"
81 " -I dir\t\tSet an include directory search path\n"
82 " -U\t\t\tMark unresolved symbols as import\n"
83 " -V\t\t\tPrint the assembler version\n"
84 " -W n\t\t\tSet warning level n\n"
87 " --auto-import\t\tMark unresolved symbols as import\n"
88 " --cpu type\t\tSet cpu type\n"
89 " --debug-info\t\tAdd debug info to object file\n"
90 " --ignore-case\t\tIgnore case of symbols\n"
91 " --include-dir dir\tSet an include directory search path\n"
92 " --listing\t\tCreate a listing if assembly was ok\n"
93 " --pagelength n\tSet the page length for the listing\n"
94 " --smart\t\tEnable smart mode\n"
95 " --verbose\t\tIncrease verbosity\n"
96 " --version\t\tPrint the assembler version\n",
103 static void UnknownOption (const char* Arg)
104 /* Print an error about an unknown option. Print usage information and exit */
106 fprintf (stderr, "Unknown option: %s\n", Arg);
112 static void NeedArg (const char* Arg)
113 /* Print an error about a missing option argument and exit. */
115 fprintf (stderr, "Option requires an argument: %s\n", Arg);
121 static void InvSym (const char* Def)
122 /* Print an error about an invalid symbol definition and die */
124 fprintf (stderr, "Invalid symbol definition: `%s'\n", Def);
130 static const char* GetArg (int* ArgNum, char* argv [], unsigned Len)
131 /* Get an option argument */
133 const char* Arg = argv [*ArgNum];
134 if (Arg [Len] != '\0') {
135 /* Argument appended */
138 /* Separate argument */
139 Arg = argv [*ArgNum + 1];
141 /* End of arguments */
142 NeedArg (argv [*ArgNum]);
151 static void SetOptions (void)
152 /* Set the option for the translator */
156 /* Set the translator */
157 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
160 /* Set date and time */
161 OptDateTime ((unsigned long) time(0));
166 static void DefineSymbol (const char* Def)
167 /* Define a symbol from the command line */
172 char SymName [MAX_STR_LEN+1];
174 /* The symbol must start with a character or underline */
175 if (Def [0] != '_' && !isalpha (Def [0])) {
180 /* Copy the symbol, checking the rest */
182 while (isalnum (*P) || *P == '_') {
183 if (I <= MAX_STR_LEN) {
190 /* Do we have a value given? */
197 /* We have a value */
201 if (sscanf (P, "%lx", &Val) != 1) {
205 if (sscanf (P, "%li", &Val) != 1) {
211 /* Check if have already a symbol with this name */
212 if (SymIsDef (SymName)) {
213 fprintf (stderr, "`%s' is already defined\n", SymName);
217 /* Define the symbol */
218 SymDef (SymName, LiteralExpr (Val), 0);
223 static void OptAutoImport (const char* Opt)
224 /* Mark unresolved symbols as imported */
231 static void OptCPU (const char* Opt, const char* Arg)
232 /* Handle the --cpu option */
237 if (strcmp (Arg, "6502") == 0) {
239 } else if (strcmp (Arg, "65C02") == 0) {
241 } else if (strcmp (Arg, "65816") == 0) {
244 } else if (strcmp (Arg, "sunplus") == 0) {
245 SetCPU (CPU_SUNPLUS);
248 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
255 static void OptDebugInfo (const char* Opt)
256 /* Add debug info to the object file */
263 static void OptIgnoreCase (const char* Opt)
264 /* Ignore case on symbols */
271 static void OptIncludeDir (const char* Opt, const char* Arg)
272 /* Add an include search path */
277 AddIncludePath (Arg);
282 static void OptListing (const char* Opt)
283 /* Create a listing file */
290 static void OptPageLength (const char* Opt, const char* Arg)
291 /* Handle the --pagelength option */
298 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
299 fprintf (stderr, "Invalid page length: %d\n", Len);
307 static void OptSmart (const char* Opt)
308 /* Handle the -s/--smart options */
315 static void OptVerbose (const char* Opt)
316 /* Increase verbosity */
323 static void OptVersion (const char* Opt)
324 /* Print the assembler version */
327 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
328 VER_MAJOR, VER_MINOR, VER_PATCH);
333 static void LongOption (int* ArgNum, char* argv [])
334 /* Handle a long command line option */
336 const char* Opt = argv [*ArgNum];
337 const char* Arg = argv [*ArgNum+1];
339 if (strcmp (Opt, "--auto-import") == 0) {
341 } else if (strcmp (Opt, "--cpu") == 0) {
344 } else if (strcmp (Opt, "--debug-info") == 0) {
346 } else if (strcmp (Opt, "--ignore-case") == 0) {
348 } else if (strcmp (Opt, "--include-dir") == 0) {
349 OptIncludeDir (Opt, Arg);
351 } else if (strcmp (Opt, "--listing") == 0) {
353 } else if (strcmp (Opt, "--pagelength") == 0) {
354 OptPageLength (Opt, Arg);
356 } else if (strcmp (Opt, "--smart") == 0) {
358 } else if (strcmp (Opt, "--verbose") == 0) {
360 } else if (strcmp (Opt, "--version") == 0) {
369 static void OneLine (void)
370 /* Assemble one line */
372 char Ident [MAX_STR_LEN+1];
375 /* Initialize the listing line */
378 if (Tok == TOK_COLON) {
379 /* An unnamed label */
384 /* Assemble the line */
385 if (Tok == TOK_IDENT) {
388 if (IsMacro (SVal)) {
390 /* Yes, start a macro expansion */
396 /* No, label. Remember the identifier, then skip it */
397 int HadWS = WS; /* Did we have whitespace before the ident? */
398 strcpy (Ident, SVal);
401 /* If a colon follows, this is a label definition. If there
402 * is no colon, it's an assignment.
407 /* Define the symbol with the expression following the
410 SymDef (Ident, Expression (), 0);
411 /* Don't allow anything after a symbol definition */
415 SymDef (Ident, CurrentPC (), IsZPSeg ());
416 /* Skip the colon. If NoColonLabels is enabled, allow labels
417 * without a colon if there is no whitespace before the
420 if (Tok != TOK_COLON) {
421 if (HadWS || !NoColonLabels) {
422 Error (ERR_COLON_EXPECTED);
424 if (Tok == TOK_NAMESPACE) {
425 /* Smart :: handling */
438 if (TokIsPseudo (Tok)) {
439 /* A control command, IVal is index into table */
441 } else if (Tok == TOK_MNEMO) {
442 /* A mnemonic - assemble one instruction */
443 HandleInstruction (IVal);
444 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
445 /* A macro expansion */
450 /* Calling InitListingLine again here is part of a hack that introduces
451 * enough magic to make the PC output in the listing work.
455 /* Line separator must come here */
461 static void Assemble (void)
462 /* Start the ball rolling ... */
467 /* Assemble lines until end of file */
468 while (Tok != TOK_EOF) {
475 static void CreateObjFile (void)
476 /* Create the object file */
478 /* Open the object, write the header */
481 /* Write the object file options */
484 /* Write the list of input files */
487 /* Write the segment data to the file */
490 /* Write the import list */
493 /* Write the export list */
496 /* Write debug symbols if requested */
499 /* Write an updated header and close the file */
505 int main (int argc, char* argv [])
506 /* Assembler main program */
510 /* Set the program name */
513 /* We must have a file name */
518 /* Enter the base lexical level. We must do that here, since we may
519 * define symbols using -D.
523 /* Check the parameters */
527 /* Get the argument */
528 const char* Arg = argv [I];
530 /* Check for an option */
531 if (Arg [0] == '-') {
535 LongOption (&I, argv);
551 OutFile = GetArg (&I, argv, 2);
563 DefineSymbol (GetArg (&I, argv, 2));
567 OptIncludeDir (Arg, GetArg (&I, argv, 2));
579 WarnLevel = atoi (GetArg (&I, argv, 2));
588 /* Filename. Check if we already had one */
590 fprintf (stderr, "Don't know what to do with `%s'\n", Arg);
601 /* Do we have an input file? */
603 fprintf (stderr, "No input file\n");
607 /* Initialize the scanner, open the input file */
608 InitScanner (InFile);
610 /* Define the default options */
613 /* Assemble the input */
616 /* If we didn't have any errors, check the unnamed labels */
617 if (ErrorCount == 0) {
621 /* If we didn't have any errors, check the symbol table */
622 if (ErrorCount == 0) {
626 /* If we didn't have any errors, check and resolve the segment data */
627 if (ErrorCount == 0) {
637 /* If we didn't have any errors, create the object and listing files */
638 if (ErrorCount == 0) {
645 /* Close the input file */
648 /* Return an apropriate exit code */
649 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;