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 */
291 /* Map the target name to a target id */
294 AbEnd ("Invalid target name: `%s'", Arg);
296 Target = (target_t) T;
301 static void OptVerbose (const char* Opt, const char* Arg)
302 /* Increase verbosity */
309 static void OptVersion (const char* Opt, const char* Arg)
310 /* Print the assembler version */
313 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
314 VER_MAJOR, VER_MINOR, VER_PATCH);
319 static void OneLine (void)
320 /* Assemble one line */
322 char Ident [MAX_STR_LEN+1];
325 /* Initialize the new listing line if we are actually reading from file
326 * and not from internally pushed input.
328 if (!HavePushedInput ()) {
332 if (Tok == TOK_COLON) {
333 /* An unnamed label */
338 /* Assemble the line */
339 if (Tok == TOK_IDENT) {
342 if (IsMacro (SVal)) {
344 /* Yes, start a macro expansion */
350 /* No, label. Remember the identifier, then skip it */
351 int HadWS = WS; /* Did we have whitespace before the ident? */
352 strcpy (Ident, SVal);
355 /* If a colon follows, this is a label definition. If there
356 * is no colon, it's an assignment.
361 /* Define the symbol with the expression following the '=' */
362 SymDef (Ident, Expression (), 0);
363 /* Don't allow anything after a symbol definition */
367 SymDef (Ident, CurrentPC (), IsZPSeg ());
368 /* Skip the colon. If NoColonLabels is enabled, allow labels
369 * without a colon if there is no whitespace before the
372 if (Tok != TOK_COLON) {
373 if (HadWS || !NoColonLabels) {
374 Error (ERR_COLON_EXPECTED);
376 if (Tok == TOK_NAMESPACE) {
377 /* Smart :: handling */
390 if (TokIsPseudo (Tok)) {
391 /* A control command, IVal is index into table */
393 } else if (Tok == TOK_MNEMO) {
394 /* A mnemonic - assemble one instruction */
395 HandleInstruction (IVal);
396 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
397 /* A macro expansion */
402 /* Line separator must come here */
408 static void Assemble (void)
409 /* Start the ball rolling ... */
414 /* Assemble lines until end of file */
415 while (Tok != TOK_EOF) {
422 static void CreateObjFile (void)
423 /* Create the object file */
425 /* Open the object, write the header */
428 /* Write the object file options */
431 /* Write the list of input files */
434 /* Write the segment data to the file */
437 /* Write the import list */
440 /* Write the export list */
443 /* Write debug symbols if requested */
446 /* Write an updated header and close the file */
452 int main (int argc, char* argv [])
453 /* Assembler main program */
455 /* Program long options */
456 static const LongOpt OptTab[] = {
457 { "--auto-import", 0, OptAutoImport },
458 { "--cpu", 1, OptCPU },
459 { "--debug-info", 0, OptDebugInfo },
460 { "--help", 0, OptHelp },
461 { "--ignore-case", 0, OptIgnoreCase },
462 { "--include-dir", 1, OptIncludeDir },
463 { "--listing", 0, OptListing },
464 { "--pagelength", 1, OptPageLength },
465 { "--smart", 0, OptSmart },
466 { "--target", 1, OptTarget },
467 { "--verbose", 0, OptVerbose },
468 { "--version", 0, OptVersion },
473 /* Initialize the cmdline module */
474 InitCmdLine (argc, argv, "ca65");
476 /* Enter the base lexical level. We must do that here, since we may
477 * define symbols using -D.
481 /* Check the parameters */
485 /* Get the argument */
486 const char* Arg = argv [I];
488 /* Check for an option */
489 if (Arg [0] == '-') {
493 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
497 OptDebugInfo (Arg, 0);
505 OptIgnoreCase (Arg, 0);
513 OutFile = GetArg (&I, 2);
521 OptTarget (Arg, GetArg (&I, 2));
529 DefineSymbol (GetArg (&I, 2));
533 OptIncludeDir (Arg, GetArg (&I, 2));
537 OptAutoImport (Arg, 0);
545 WarnLevel = atoi (GetArg (&I, 2));
554 /* Filename. Check if we already had one */
556 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
568 /* Do we have an input file? */
570 fprintf (stderr, "%s: No input files\n", ProgName);
574 /* Initialize the scanner, open the input file */
575 InitScanner (InFile);
577 /* Define the default options */
580 /* Assemble the input */
583 /* If we didn't have any errors, check the unnamed labels */
584 if (ErrorCount == 0) {
588 /* If we didn't have any errors, check the symbol table */
589 if (ErrorCount == 0) {
593 /* If we didn't have any errors, check and resolve the segment data */
594 if (ErrorCount == 0) {
604 /* If we didn't have any errors, create the object and listing files */
605 if (ErrorCount == 0) {
612 /* Close the input file */
615 /* Return an apropriate exit code */
616 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;