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 /*****************************************************************************/
69 /*****************************************************************************/
71 /*****************************************************************************/
75 static void Usage (void)
76 /* Print usage information and exit */
79 "Usage: %s [options] file\n"
81 " -g\t\t\tAdd debug info to object file\n"
82 " -h\t\t\tHelp (this text)\n"
83 " -i\t\t\tIgnore case of symbols\n"
84 " -l\t\t\tCreate a listing if assembly was ok\n"
85 " -o name\t\tName the output file\n"
86 " -s\t\t\tEnable smart mode\n"
87 " -t sys\t\tSet the target system\n"
88 " -v\t\t\tIncrease verbosity\n"
89 " -D name[=value]\tDefine a symbol\n"
90 " -I dir\t\tSet an include directory search path\n"
91 " -U\t\t\tMark unresolved symbols as import\n"
92 " -V\t\t\tPrint the assembler version\n"
93 " -W n\t\t\tSet warning level n\n"
96 " --auto-import\t\tMark unresolved symbols as import\n"
97 " --cpu type\t\tSet cpu type\n"
98 " --debug-info\t\tAdd debug info to object file\n"
99 " --help\t\tHelp (this text)\n"
100 " --ignore-case\t\tIgnore case of symbols\n"
101 " --include-dir dir\tSet an include directory search path\n"
102 " --listing\t\tCreate a listing if assembly was ok\n"
103 " --pagelength n\tSet the page length for the listing\n"
104 " --smart\t\tEnable smart mode\n"
105 " --target sys\t\tSet the target system\n"
106 " --verbose\t\tIncrease verbosity\n"
107 " --version\t\tPrint the assembler version\n",
113 static void SetOptions (void)
114 /* Set the option for the translator */
118 /* Set the translator */
119 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
122 /* Set date and time */
123 OptDateTime ((unsigned long) time(0));
128 static void DefineSymbol (const char* Def)
129 /* Define a symbol from the command line */
134 char SymName [MAX_STR_LEN+1];
136 /* The symbol must start with a character or underline */
137 if (Def [0] != '_' && !isalpha (Def [0])) {
142 /* Copy the symbol, checking the rest */
144 while (isalnum (*P) || *P == '_') {
145 if (I <= MAX_STR_LEN) {
152 /* Do we have a value given? */
159 /* We have a value */
163 if (sscanf (P, "%lx", &Val) != 1) {
167 if (sscanf (P, "%li", &Val) != 1) {
173 /* Check if have already a symbol with this name */
174 if (SymIsDef (SymName)) {
175 AbEnd ("`%s' is already defined", SymName);
178 /* Define the symbol */
179 SymDef (SymName, LiteralExpr (Val), 0);
184 static void OptAutoImport (const char* Opt, const char* Arg)
185 /* Mark unresolved symbols as imported */
192 static void OptCPU (const char* Opt, const char* Arg)
193 /* Handle the --cpu option */
198 if (strcmp (Arg, "6502") == 0) {
200 } else if (strcmp (Arg, "65C02") == 0) {
202 } else if (strcmp (Arg, "65816") == 0) {
205 } else if (strcmp (Arg, "sunplus") == 0) {
206 SetCPU (CPU_SUNPLUS);
209 AbEnd ("Invalid CPU: `%s'", Arg);
215 static void OptDebugInfo (const char* Opt, const char* Arg)
216 /* Add debug info to the object file */
223 static void OptHelp (const char* Opt, const char* Arg)
224 /* Print usage information and exit */
232 static void OptIgnoreCase (const char* Opt, const char* Arg)
233 /* Ignore case on symbols */
240 static void OptIncludeDir (const char* Opt, const char* Arg)
241 /* Add an include search path */
246 AddIncludePath (Arg);
251 static void OptListing (const char* Opt, const char* Arg)
252 /* Create a listing file */
259 static void OptPageLength (const char* Opt, const char* Arg)
260 /* Handle the --pagelength option */
267 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
268 AbEnd ("Invalid page length: %d", Len);
275 static void OptSmart (const char* Opt, const char* Arg)
276 /* Handle the -s/--smart options */
283 static void OptTarget (const char* Opt, const char* Arg)
284 /* Set the target system */
290 /* Map the target name to a target id */
291 Target = FindTarget (Arg);
292 if (Target == TGT_UNKNOWN) {
293 AbEnd ("Invalid target name: `%s'", Arg);
299 static void OptVerbose (const char* Opt, const char* Arg)
300 /* Increase verbosity */
307 static void OptVersion (const char* Opt, const char* Arg)
308 /* Print the assembler version */
311 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
312 VER_MAJOR, VER_MINOR, VER_PATCH);
317 static void OneLine (void)
318 /* Assemble one line */
320 char Ident [MAX_STR_LEN+1];
323 /* Initialize the new listing line if we are actually reading from file
324 * and not from internally pushed input.
326 if (!HavePushedInput ()) {
330 if (Tok == TOK_COLON) {
331 /* An unnamed label */
336 /* Assemble the line */
337 if (Tok == TOK_IDENT) {
340 if (IsMacro (SVal)) {
342 /* Yes, start a macro expansion */
348 /* No, label. Remember the identifier, then skip it */
349 int HadWS = WS; /* Did we have whitespace before the ident? */
350 strcpy (Ident, SVal);
353 /* If a colon follows, this is a label definition. If there
354 * is no colon, it's an assignment.
359 /* Define the symbol with the expression following the '=' */
360 SymDef (Ident, Expression (), 0);
361 /* Don't allow anything after a symbol definition */
365 SymDef (Ident, CurrentPC (), IsZPSeg ());
366 /* Skip the colon. If NoColonLabels is enabled, allow labels
367 * without a colon if there is no whitespace before the
370 if (Tok != TOK_COLON) {
371 if (HadWS || !NoColonLabels) {
372 Error (ERR_COLON_EXPECTED);
374 if (Tok == TOK_NAMESPACE) {
375 /* Smart :: handling */
388 if (TokIsPseudo (Tok)) {
389 /* A control command, IVal is index into table */
391 } else if (Tok == TOK_MNEMO) {
392 /* A mnemonic - assemble one instruction */
393 HandleInstruction (IVal);
394 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
395 /* A macro expansion */
400 /* Line separator must come here */
406 static void Assemble (void)
407 /* Start the ball rolling ... */
412 /* Assemble lines until end of file */
413 while (Tok != TOK_EOF) {
420 static void CreateObjFile (void)
421 /* Create the object file */
423 /* Open the object, write the header */
426 /* Write the object file options */
429 /* Write the list of input files */
432 /* Write the segment data to the file */
435 /* Write the import list */
438 /* Write the export list */
441 /* Write debug symbols if requested */
444 /* Write an updated header and close the file */
450 int main (int argc, char* argv [])
451 /* Assembler main program */
453 /* Program long options */
454 static const LongOpt OptTab[] = {
455 { "--auto-import", 0, OptAutoImport },
456 { "--cpu", 1, OptCPU },
457 { "--debug-info", 0, OptDebugInfo },
458 { "--help", 0, OptHelp },
459 { "--ignore-case", 0, OptIgnoreCase },
460 { "--include-dir", 1, OptIncludeDir },
461 { "--listing", 0, OptListing },
462 { "--pagelength", 1, OptPageLength },
463 { "--smart", 0, OptSmart },
464 { "--target", 1, OptTarget },
465 { "--verbose", 0, OptVerbose },
466 { "--version", 0, OptVersion },
471 /* Initialize the cmdline module */
472 InitCmdLine (argc, argv, "ca65");
474 /* Enter the base lexical level. We must do that here, since we may
475 * define symbols using -D.
479 /* Check the parameters */
483 /* Get the argument */
484 const char* Arg = argv [I];
486 /* Check for an option */
487 if (Arg [0] == '-') {
491 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
495 OptDebugInfo (Arg, 0);
503 OptIgnoreCase (Arg, 0);
511 OutFile = GetArg (&I, 2);
519 OptTarget (Arg, GetArg (&I, 2));
527 DefineSymbol (GetArg (&I, 2));
531 OptIncludeDir (Arg, GetArg (&I, 2));
535 OptAutoImport (Arg, 0);
543 WarnLevel = atoi (GetArg (&I, 2));
552 /* Filename. Check if we already had one */
554 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
566 /* Do we have an input file? */
568 fprintf (stderr, "%s: No input files\n", ProgName);
572 /* Initialize the scanner, open the input file */
573 InitScanner (InFile);
575 /* Define the default options */
578 /* Assemble the input */
581 /* If we didn't have any errors, check the unnamed labels */
582 if (ErrorCount == 0) {
586 /* If we didn't have any errors, check the symbol table */
587 if (ErrorCount == 0) {
591 /* If we didn't have any errors, check and resolve the segment data */
592 if (ErrorCount == 0) {
602 /* If we didn't have any errors, create the object and listing files */
603 if (ErrorCount == 0) {
610 /* Close the input file */
613 /* Return an apropriate exit code */
614 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;