1 /*****************************************************************************/
5 /* Main program for the ca65 macroassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void Usage (void)
81 /* Print usage information and exit */
84 "Usage: %s [options] file\n"
86 " -D name[=value]\tDefine a symbol\n"
87 " -I dir\t\tSet an include directory search path\n"
88 " -U\t\t\tMark unresolved symbols as import\n"
89 " -V\t\t\tPrint the assembler version\n"
90 " -W n\t\t\tSet warning level n\n"
91 " -g\t\t\tAdd debug info to object file\n"
92 " -h\t\t\tHelp (this text)\n"
93 " -i\t\t\tIgnore case of symbols\n"
94 " -l\t\t\tCreate a listing if assembly was ok\n"
95 " -o name\t\tName the output file\n"
96 " -s\t\t\tEnable smart mode\n"
97 " -t sys\t\tSet the target system\n"
98 " -v\t\t\tIncrease verbosity\n"
101 " --auto-import\t\tMark unresolved symbols as import\n"
102 " --cpu type\t\tSet cpu type\n"
103 " --debug-info\t\tAdd debug info to object file\n"
104 " --feature name\tSet an emulation feature\n"
105 " --help\t\tHelp (this text)\n"
106 " --ignore-case\t\tIgnore case of symbols\n"
107 " --include-dir dir\tSet an include directory search path\n"
108 " --listing\t\tCreate a listing if assembly was ok\n"
109 " --pagelength n\tSet the page length for the listing\n"
110 " --smart\t\tEnable smart mode\n"
111 " --target sys\t\tSet the target system\n"
112 " --verbose\t\tIncrease verbosity\n"
113 " --version\t\tPrint the assembler version\n",
119 static void SetOptions (void)
120 /* Set the option for the translator */
124 /* Set the translator */
125 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
128 /* Set date and time */
129 OptDateTime ((unsigned long) time(0));
134 static void DefineSymbol (const char* Def)
135 /* Define a symbol from the command line */
140 char SymName [MAX_STR_LEN+1];
142 /* The symbol must start with a character or underline */
143 if (Def [0] != '_' && !IsAlpha (Def [0])) {
148 /* Copy the symbol, checking the rest */
150 while (IsAlNum (*P) || *P == '_') {
151 if (I <= MAX_STR_LEN) {
158 /* Do we have a value given? */
165 /* We have a value */
169 if (sscanf (P, "%lx", &Val) != 1) {
173 if (sscanf (P, "%li", &Val) != 1) {
179 /* Check if have already a symbol with this name */
180 if (SymIsDef (SymName, SCOPE_ANY)) {
181 AbEnd ("`%s' is already defined", SymName);
184 /* Define the symbol */
185 SymDef (SymName, LiteralExpr (Val), 0, 0);
190 static void OptAutoImport (const char* Opt attribute ((unused)),
191 const char* Arg attribute ((unused)))
192 /* Mark unresolved symbols as imported */
199 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
200 /* Handle the --cpu option */
202 if (strcmp (Arg, "6502") == 0) {
204 } else if (strcmp (Arg, "65C02") == 0) {
206 } else if (strcmp (Arg, "65816") == 0) {
209 } else if (strcmp (Arg, "sunplus") == 0) {
210 SetCPU (CPU_SUNPLUS);
213 AbEnd ("Invalid CPU: `%s'", Arg);
219 static void OptDebugInfo (const char* Opt attribute ((unused)),
220 const char* Arg attribute ((unused)))
221 /* Add debug info to the object file */
228 static void OptFeature (const char* Opt attribute ((unused)), 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 attribute ((unused)),
240 const char* Arg attribute ((unused)))
241 /* Print usage information and exit */
249 static void OptIgnoreCase (const char* Opt attribute ((unused)),
250 const char* Arg attribute ((unused)))
251 /* Ignore case on symbols */
258 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
259 /* Add an include search path */
261 AddIncludePath (Arg);
266 static void OptListing (const char* Opt attribute ((unused)),
267 const char* Arg attribute ((unused)))
268 /* Create a listing file */
275 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
276 /* Handle the --pagelength option */
278 int Len = atoi (Arg);
279 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
280 AbEnd ("Invalid page length: %d", Len);
287 static void OptSmart (const char* Opt attribute ((unused)),
288 const char* Arg attribute ((unused)))
289 /* Handle the -s/--smart options */
296 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
297 /* Set the target system */
299 /* Map the target name to a target id */
300 Target = FindTarget (Arg);
301 if (Target == TGT_UNKNOWN) {
302 AbEnd ("Invalid target name: `%s'", Arg);
303 } else if (Target == TGT_MODULE) {
304 AbEnd ("Cannot use `module' as a target for the assembler");
310 static void OptVerbose (const char* Opt attribute ((unused)),
311 const char* Arg attribute ((unused)))
312 /* Increase verbosity */
319 static void OptVersion (const char* Opt attribute ((unused)),
320 const char* Arg attribute ((unused)))
321 /* Print the assembler version */
324 "ca65 V%u.%u.%u - %s\n",
325 VER_MAJOR, VER_MINOR, VER_PATCH, Copyright);
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, 0);
387 /* Don't allow anything after a symbol definition */
391 SymDef (Ident, CurrentPC(), IsZPSeg(), 1);
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 line infos if requested */
484 /* Write the string pool */
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 /* If no CPU given, use the default CPU for the target */
617 if (GetCPU () == CPU_UNKNOWN) {
618 if (Target != TGT_UNKNOWN) {
619 SetCPU (DefaultCPU[Target]);
625 /* Intialize the target translation tables */
628 /* Initialize the scanner, open the input file */
629 InitScanner (InFile);
631 /* Define the default options */
634 /* Assemble the input */
637 /* If we didn't have any errors, check the segment stack */
638 if (ErrorCount == 0) {
642 /* If we didn't have any errors, check the unnamed labels */
643 if (ErrorCount == 0) {
647 /* If we didn't have any errors, check the symbol table */
648 if (ErrorCount == 0) {
652 /* If we didn't have any errors, check and resolve the segment data */
653 if (ErrorCount == 0) {
657 /* If we didn't have an errors, index the line infos */
658 MakeLineInfoIndex ();
661 if (Verbosity >= 2) {
666 /* If we didn't have any errors, create the object and listing files */
667 if (ErrorCount == 0) {
674 /* Close the input file */
677 /* Return an apropriate exit code */
678 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;