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 /*****************************************************************************/
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static void Usage (void)
77 /* Print usage information and exit */
80 "Usage: %s [options] file\n"
82 " -g\t\t\tAdd debug info to object file\n"
83 " -h\t\t\tHelp (this text)\n"
84 " -i\t\t\tIgnore case of symbols\n"
85 " -l\t\t\tCreate a listing if assembly was ok\n"
86 " -o name\t\tName the output file\n"
87 " -s\t\t\tEnable smart mode\n"
88 " -t sys\t\tSet the target system\n"
89 " -v\t\t\tIncrease verbosity\n"
90 " -D name[=value]\tDefine a symbol\n"
91 " -I dir\t\tSet an include directory search path\n"
92 " -U\t\t\tMark unresolved symbols as import\n"
93 " -V\t\t\tPrint the assembler version\n"
94 " -W n\t\t\tSet warning level n\n"
97 " --auto-import\t\tMark unresolved symbols as import\n"
98 " --cpu type\t\tSet cpu type\n"
99 " --debug-info\t\tAdd debug info to object file\n"
100 " --feature name\tSet an emulation feature\n"
101 " --help\t\tHelp (this text)\n"
102 " --ignore-case\t\tIgnore case of symbols\n"
103 " --include-dir dir\tSet an include directory search path\n"
104 " --listing\t\tCreate a listing if assembly was ok\n"
105 " --pagelength n\tSet the page length for the listing\n"
106 " --smart\t\tEnable smart mode\n"
107 " --target sys\t\tSet the target system\n"
108 " --verbose\t\tIncrease verbosity\n"
109 " --version\t\tPrint the assembler version\n",
115 static void SetOptions (void)
116 /* Set the option for the translator */
120 /* Set the translator */
121 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
124 /* Set date and time */
125 OptDateTime ((unsigned long) time(0));
130 static void DefineSymbol (const char* Def)
131 /* Define a symbol from the command line */
136 char SymName [MAX_STR_LEN+1];
138 /* The symbol must start with a character or underline */
139 if (Def [0] != '_' && !isalpha (Def [0])) {
144 /* Copy the symbol, checking the rest */
146 while (isalnum (*P) || *P == '_') {
147 if (I <= MAX_STR_LEN) {
154 /* Do we have a value given? */
161 /* We have a value */
165 if (sscanf (P, "%lx", &Val) != 1) {
169 if (sscanf (P, "%li", &Val) != 1) {
175 /* Check if have already a symbol with this name */
176 if (SymIsDef (SymName)) {
177 AbEnd ("`%s' is already defined", SymName);
180 /* Define the symbol */
181 SymDef (SymName, LiteralExpr (Val), 0);
186 static void OptAutoImport (const char* Opt, const char* Arg)
187 /* Mark unresolved symbols as imported */
194 static void OptCPU (const char* Opt, const char* Arg)
195 /* Handle the --cpu option */
200 if (strcmp (Arg, "6502") == 0) {
202 } else if (strcmp (Arg, "65C02") == 0) {
204 } else if (strcmp (Arg, "65816") == 0) {
207 } else if (strcmp (Arg, "sunplus") == 0) {
208 SetCPU (CPU_SUNPLUS);
211 AbEnd ("Invalid CPU: `%s'", Arg);
217 static void OptDebugInfo (const char* Opt, const char* Arg)
218 /* Add debug info to the object file */
225 static void OptFeature (const char* Opt, const char* Arg)
226 /* Set an emulation feature */
228 /* Set the feature, check for errors */
229 if (SetFeature (Arg) == FEAT_UNKNOWN) {
230 AbEnd ("Illegal emulation feature: `%s'", Arg);
236 static void OptHelp (const char* Opt, const char* Arg)
237 /* Print usage information and exit */
245 static void OptIgnoreCase (const char* Opt, const char* Arg)
246 /* Ignore case on symbols */
253 static void OptIncludeDir (const char* Opt, const char* Arg)
254 /* Add an include search path */
259 AddIncludePath (Arg);
264 static void OptListing (const char* Opt, const char* Arg)
265 /* Create a listing file */
272 static void OptPageLength (const char* Opt, const char* Arg)
273 /* Handle the --pagelength option */
280 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
281 AbEnd ("Invalid page length: %d", Len);
288 static void OptSmart (const char* Opt, const char* Arg)
289 /* Handle the -s/--smart options */
296 static void OptTarget (const char* Opt, const char* Arg)
297 /* Set the target system */
303 /* Map the target name to a target id */
304 Target = FindTarget (Arg);
305 if (Target == TGT_UNKNOWN) {
306 AbEnd ("Invalid target name: `%s'", Arg);
312 static void OptVerbose (const char* Opt, const char* Arg)
313 /* Increase verbosity */
320 static void OptVersion (const char* Opt, const char* Arg)
321 /* Print the assembler version */
324 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
325 VER_MAJOR, VER_MINOR, VER_PATCH);
330 static void DoPCAssign (void)
331 /* Start absolute code */
333 long PC = ConstExpression ();
334 if (PC < 0 || PC > 0xFFFFFF) {
343 static void OneLine (void)
344 /* Assemble one line */
346 char Ident [MAX_STR_LEN+1];
349 /* Initialize the new listing line if we are actually reading from file
350 * and not from internally pushed input.
352 if (!HavePushedInput ()) {
356 if (Tok == TOK_COLON) {
357 /* An unnamed label */
362 /* Assemble the line */
363 if (Tok == TOK_IDENT) {
366 if (IsMacro (SVal)) {
368 /* Yes, start a macro expansion */
374 /* No, label. Remember the identifier, then skip it */
375 int HadWS = WS; /* Did we have whitespace before the ident? */
376 strcpy (Ident, SVal);
379 /* If a colon follows, this is a label definition. If there
380 * is no colon, it's an assignment.
385 /* Define the symbol with the expression following the '=' */
386 SymDef (Ident, Expression (), 0);
387 /* Don't allow anything after a symbol definition */
391 SymDef (Ident, CurrentPC (), IsZPSeg ());
392 /* Skip the colon. If NoColonLabels is enabled, allow labels
393 * without a colon if there is no whitespace before the
396 if (Tok != TOK_COLON) {
397 if (HadWS || !NoColonLabels) {
398 Error (ERR_COLON_EXPECTED);
400 if (Tok == TOK_NAMESPACE) {
401 /* Smart :: handling */
414 if (TokIsPseudo (Tok)) {
415 /* A control command, IVal is index into table */
417 } else if (Tok == TOK_MNEMO) {
418 /* A mnemonic - assemble one instruction */
419 HandleInstruction (IVal);
420 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
421 /* A macro expansion */
423 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
426 Error (ERR_EQ_EXPECTED);
429 /* Skip the equal sign */
431 /* Enter absolute mode */
437 /* Line separator must come here */
443 static void Assemble (void)
444 /* Start the ball rolling ... */
449 /* Assemble lines until end of file */
450 while (Tok != TOK_EOF) {
457 static void CreateObjFile (void)
458 /* Create the object file */
460 /* Open the object, write the header */
463 /* Write the object file options */
466 /* Write the list of input files */
469 /* Write the segment data to the file */
472 /* Write the import list */
475 /* Write the export list */
478 /* Write debug symbols if requested */
481 /* Write an updated header and close the file */
487 int main (int argc, char* argv [])
488 /* Assembler main program */
490 /* Program long options */
491 static const LongOpt OptTab[] = {
492 { "--auto-import", 0, OptAutoImport },
493 { "--cpu", 1, OptCPU },
494 { "--debug-info", 0, OptDebugInfo },
495 { "--feature", 1, OptFeature },
496 { "--help", 0, OptHelp },
497 { "--ignore-case", 0, OptIgnoreCase },
498 { "--include-dir", 1, OptIncludeDir },
499 { "--listing", 0, OptListing },
500 { "--pagelength", 1, OptPageLength },
501 { "--smart", 0, OptSmart },
502 { "--target", 1, OptTarget },
503 { "--verbose", 0, OptVerbose },
504 { "--version", 0, OptVersion },
509 /* Initialize the cmdline module */
510 InitCmdLine (argc, argv, "ca65");
512 /* Enter the base lexical level. We must do that here, since we may
513 * define symbols using -D.
517 /* Check the parameters */
521 /* Get the argument */
522 const char* Arg = argv [I];
524 /* Check for an option */
525 if (Arg [0] == '-') {
529 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
533 OptDebugInfo (Arg, 0);
541 OptIgnoreCase (Arg, 0);
549 OutFile = GetArg (&I, 2);
557 OptTarget (Arg, GetArg (&I, 2));
565 DefineSymbol (GetArg (&I, 2));
569 OptIncludeDir (Arg, GetArg (&I, 2));
573 OptAutoImport (Arg, 0);
581 WarnLevel = atoi (GetArg (&I, 2));
590 /* Filename. Check if we already had one */
592 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
604 /* Do we have an input file? */
606 fprintf (stderr, "%s: No input files\n", ProgName);
610 /* Initialize the scanner, open the input file */
611 InitScanner (InFile);
613 /* Define the default options */
616 /* Assemble the input */
619 /* If we didn't have any errors, check the unnamed labels */
620 if (ErrorCount == 0) {
624 /* If we didn't have any errors, check the symbol table */
625 if (ErrorCount == 0) {
629 /* If we didn't have any errors, check and resolve the segment data */
630 if (ErrorCount == 0) {
640 /* If we didn't have any errors, create the object and listing files */
641 if (ErrorCount == 0) {
648 /* Close the input file */
651 /* Return an apropriate exit code */
652 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;