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 /*****************************************************************************/
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static void Usage (void)
80 /* Print usage information and exit */
83 "Usage: %s [options] file\n"
85 " -g\t\t\tAdd debug info to object file\n"
86 " -h\t\t\tHelp (this text)\n"
87 " -i\t\t\tIgnore case of symbols\n"
88 " -l\t\t\tCreate a listing if assembly was ok\n"
89 " -o name\t\tName the output file\n"
90 " -s\t\t\tEnable smart mode\n"
91 " -t sys\t\tSet the target system\n"
92 " -v\t\t\tIncrease verbosity\n"
93 " -D name[=value]\tDefine a symbol\n"
94 " -I dir\t\tSet an include directory search path\n"
95 " -U\t\t\tMark unresolved symbols as import\n"
96 " -V\t\t\tPrint the assembler version\n"
97 " -W n\t\t\tSet warning level n\n"
100 " --auto-import\t\tMark unresolved symbols as import\n"
101 " --cpu type\t\tSet cpu type\n"
102 " --debug-info\t\tAdd debug info to object file\n"
103 " --feature name\tSet an emulation feature\n"
104 " --help\t\tHelp (this text)\n"
105 " --ignore-case\t\tIgnore case of symbols\n"
106 " --include-dir dir\tSet an include directory search path\n"
107 " --listing\t\tCreate a listing if assembly was ok\n"
108 " --pagelength n\tSet the page length for the listing\n"
109 " --smart\t\tEnable smart mode\n"
110 " --target sys\t\tSet the target system\n"
111 " --verbose\t\tIncrease verbosity\n"
112 " --version\t\tPrint the assembler version\n",
118 static void SetOptions (void)
119 /* Set the option for the translator */
123 /* Set the translator */
124 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
127 /* Set date and time */
128 OptDateTime ((unsigned long) time(0));
133 static void DefineSymbol (const char* Def)
134 /* Define a symbol from the command line */
139 char SymName [MAX_STR_LEN+1];
141 /* The symbol must start with a character or underline */
142 if (Def [0] != '_' && !IsAlpha (Def [0])) {
147 /* Copy the symbol, checking the rest */
149 while (IsAlNum (*P) || *P == '_') {
150 if (I <= MAX_STR_LEN) {
157 /* Do we have a value given? */
164 /* We have a value */
168 if (sscanf (P, "%lx", &Val) != 1) {
172 if (sscanf (P, "%li", &Val) != 1) {
178 /* Check if have already a symbol with this name */
179 if (SymIsDef (SymName)) {
180 AbEnd ("`%s' is already defined", SymName);
183 /* Define the symbol */
184 SymDef (SymName, LiteralExpr (Val), 0, 0);
189 static void OptAutoImport (const char* Opt, const char* Arg)
190 /* Mark unresolved symbols as imported */
197 static void OptCPU (const char* Opt, const char* Arg)
198 /* Handle the --cpu option */
203 if (strcmp (Arg, "6502") == 0) {
205 } else if (strcmp (Arg, "65C02") == 0) {
207 } else if (strcmp (Arg, "65816") == 0) {
210 } else if (strcmp (Arg, "sunplus") == 0) {
211 SetCPU (CPU_SUNPLUS);
214 AbEnd ("Invalid CPU: `%s'", Arg);
220 static void OptDebugInfo (const char* Opt, const char* Arg)
221 /* Add debug info to the object file */
228 static void OptFeature (const char* Opt, const char* Arg)
229 /* Set an emulation feature */
231 /* Set the feature, check for errors */
232 if (SetFeature (Arg) == FEAT_UNKNOWN) {
233 AbEnd ("Illegal emulation feature: `%s'", Arg);
239 static void OptHelp (const char* Opt, const char* Arg)
240 /* Print usage information and exit */
248 static void OptIgnoreCase (const char* Opt, const char* Arg)
249 /* Ignore case on symbols */
256 static void OptIncludeDir (const char* Opt, const char* Arg)
257 /* Add an include search path */
262 AddIncludePath (Arg);
267 static void OptListing (const char* Opt, const char* Arg)
268 /* Create a listing file */
275 static void OptPageLength (const char* Opt, const char* Arg)
276 /* Handle the --pagelength option */
283 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
284 AbEnd ("Invalid page length: %d", Len);
291 static void OptSmart (const char* Opt, const char* Arg)
292 /* Handle the -s/--smart options */
299 static void OptTarget (const char* Opt, const char* Arg)
300 /* Set the target system */
306 /* Map the target name to a target id */
307 Target = FindTarget (Arg);
308 if (Target == TGT_UNKNOWN) {
309 AbEnd ("Invalid target name: `%s'", Arg);
315 static void OptVerbose (const char* Opt, const char* Arg)
316 /* Increase verbosity */
323 static void OptVersion (const char* Opt, const char* Arg)
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 DoPCAssign (void)
334 /* Start absolute code */
336 long PC = ConstExpression ();
337 if (PC < 0 || PC > 0xFFFFFF) {
346 static void OneLine (void)
347 /* Assemble one line */
349 char Ident [MAX_STR_LEN+1];
352 /* Initialize the new listing line if we are actually reading from file
353 * and not from internally pushed input.
355 if (!HavePushedInput ()) {
359 if (Tok == TOK_COLON) {
360 /* An unnamed label */
365 /* Assemble the line */
366 if (Tok == TOK_IDENT) {
369 if (IsMacro (SVal)) {
371 /* Yes, start a macro expansion */
377 /* No, label. Remember the identifier, then skip it */
378 int HadWS = WS; /* Did we have whitespace before the ident? */
379 strcpy (Ident, SVal);
382 /* If a colon follows, this is a label definition. If there
383 * is no colon, it's an assignment.
388 /* Define the symbol with the expression following the '=' */
389 SymDef (Ident, Expression(), 0, 0);
390 /* Don't allow anything after a symbol definition */
394 SymDef (Ident, CurrentPC(), IsZPSeg(), 1);
395 /* Skip the colon. If NoColonLabels is enabled, allow labels
396 * without a colon if there is no whitespace before the
399 if (Tok != TOK_COLON) {
400 if (HadWS || !NoColonLabels) {
401 Error (ERR_COLON_EXPECTED);
403 if (Tok == TOK_NAMESPACE) {
404 /* Smart :: handling */
417 if (TokIsPseudo (Tok)) {
418 /* A control command, IVal is index into table */
420 } else if (Tok == TOK_MNEMO) {
421 /* A mnemonic - assemble one instruction */
422 HandleInstruction (IVal);
423 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
424 /* A macro expansion */
426 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
429 Error (ERR_EQ_EXPECTED);
432 /* Skip the equal sign */
434 /* Enter absolute mode */
440 /* Line separator must come here */
446 static void Assemble (void)
447 /* Start the ball rolling ... */
452 /* Assemble lines until end of file */
453 while (Tok != TOK_EOF) {
460 static void CreateObjFile (void)
461 /* Create the object file */
463 /* Open the object, write the header */
466 /* Write the object file options */
469 /* Write the list of input files */
472 /* Write the segment data to the file */
475 /* Write the import list */
478 /* Write the export list */
481 /* Write debug symbols if requested */
484 /* Write line infos if requested */
487 /* Write an updated header and close the file */
493 int main (int argc, char* argv [])
494 /* Assembler main program */
496 /* Program long options */
497 static const LongOpt OptTab[] = {
498 { "--auto-import", 0, OptAutoImport },
499 { "--cpu", 1, OptCPU },
500 { "--debug-info", 0, OptDebugInfo },
501 { "--feature", 1, OptFeature },
502 { "--help", 0, OptHelp },
503 { "--ignore-case", 0, OptIgnoreCase },
504 { "--include-dir", 1, OptIncludeDir },
505 { "--listing", 0, OptListing },
506 { "--pagelength", 1, OptPageLength },
507 { "--smart", 0, OptSmart },
508 { "--target", 1, OptTarget },
509 { "--verbose", 0, OptVerbose },
510 { "--version", 0, OptVersion },
515 /* Initialize the cmdline module */
516 InitCmdLine (&argc, &argv, "ca65");
518 /* Enter the base lexical level. We must do that here, since we may
519 * define symbols using -D.
523 /* Check the parameters */
525 while (I < ArgCount) {
527 /* Get the argument */
528 const char* Arg = ArgVec [I];
530 /* Check for an option */
531 if (Arg [0] == '-') {
535 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
539 OptDebugInfo (Arg, 0);
547 OptIgnoreCase (Arg, 0);
555 OutFile = GetArg (&I, 2);
563 OptTarget (Arg, GetArg (&I, 2));
571 DefineSymbol (GetArg (&I, 2));
575 OptIncludeDir (Arg, GetArg (&I, 2));
579 OptAutoImport (Arg, 0);
587 WarnLevel = atoi (GetArg (&I, 2));
596 /* Filename. Check if we already had one */
598 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
610 /* Do we have an input file? */
612 fprintf (stderr, "%s: No input files\n", ProgName);
616 /* Intialize the target translation tables */
619 /* Initialize the scanner, open the input file */
620 InitScanner (InFile);
622 /* Define the default options */
625 /* Assemble the input */
628 /* If we didn't have any errors, check the unnamed labels */
629 if (ErrorCount == 0) {
633 /* If we didn't have any errors, check the symbol table */
634 if (ErrorCount == 0) {
638 /* If we didn't have any errors, check and resolve the segment data */
639 if (ErrorCount == 0) {
643 /* If we didn't have an errors, index the line infos */
644 MakeLineInfoIndex ();
647 if (Verbosity >= 2) {
652 /* If we didn't have any errors, create the object and listing files */
653 if (ErrorCount == 0) {
660 /* Close the input file */
663 /* Return an apropriate exit code */
664 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;