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 /*****************************************************************************/
75 /*****************************************************************************/
77 /*****************************************************************************/
81 static void Usage (void)
82 /* Print usage information and exit */
85 "Usage: %s [options] file\n"
87 " -D name[=value]\tDefine a symbol\n"
88 " -I dir\t\tSet an include directory search path\n"
89 " -U\t\t\tMark unresolved symbols as import\n"
90 " -V\t\t\tPrint the assembler version\n"
91 " -W n\t\t\tSet warning level n\n"
92 " -g\t\t\tAdd debug info to object file\n"
93 " -h\t\t\tHelp (this text)\n"
94 " -i\t\t\tIgnore case of symbols\n"
95 " -l\t\t\tCreate a listing if assembly was ok\n"
96 " -o name\t\tName the output file\n"
97 " -s\t\t\tEnable smart mode\n"
98 " -t sys\t\tSet the target system\n"
99 " -v\t\t\tIncrease verbosity\n"
102 " --auto-import\t\tMark unresolved symbols as import\n"
103 " --cpu type\t\tSet cpu type\n"
104 " --debug-info\t\tAdd debug info to object file\n"
105 " --feature name\tSet an emulation feature\n"
106 " --help\t\tHelp (this text)\n"
107 " --ignore-case\t\tIgnore case of symbols\n"
108 " --include-dir dir\tSet an include directory search path\n"
109 " --listing\t\tCreate a listing if assembly was ok\n"
110 " --pagelength n\tSet the page length for the listing\n"
111 " --smart\t\tEnable smart mode\n"
112 " --target sys\t\tSet the target system\n"
113 " --verbose\t\tIncrease verbosity\n"
114 " --version\t\tPrint the assembler version\n",
120 static void SetOptions (void)
121 /* Set the option for the translator */
125 /* Set the translator */
126 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
129 /* Set date and time */
130 OptDateTime ((unsigned long) time(0));
135 static void DefineSymbol (const char* Def)
136 /* Define a symbol from the command line */
141 char SymName [MAX_STR_LEN+1];
143 /* The symbol must start with a character or underline */
144 if (Def [0] != '_' && !IsAlpha (Def [0])) {
149 /* Copy the symbol, checking the rest */
151 while (IsAlNum (*P) || *P == '_') {
152 if (I <= MAX_STR_LEN) {
159 /* Do we have a value given? */
166 /* We have a value */
170 if (sscanf (P, "%lx", &Val) != 1) {
174 if (sscanf (P, "%li", &Val) != 1) {
180 /* Check if have already a symbol with this name */
181 if (SymIsDef (SymName, SCOPE_ANY)) {
182 AbEnd ("`%s' is already defined", SymName);
185 /* Define the symbol */
186 SymDef (SymName, LiteralExpr (Val), 0, 0);
191 static void OptAutoImport (const char* Opt attribute ((unused)),
192 const char* Arg attribute ((unused)))
193 /* Mark unresolved symbols as imported */
200 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
201 /* 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 attribute ((unused)),
221 const char* Arg attribute ((unused)))
222 /* Add debug info to the object file */
229 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
230 /* Set an emulation feature */
232 /* Set the feature, check for errors */
233 if (SetFeature (Arg) == FEAT_UNKNOWN) {
234 AbEnd ("Illegal emulation feature: `%s'", Arg);
240 static void OptHelp (const char* Opt attribute ((unused)),
241 const char* Arg attribute ((unused)))
242 /* Print usage information and exit */
250 static void OptIgnoreCase (const char* Opt attribute ((unused)),
251 const char* Arg attribute ((unused)))
252 /* Ignore case on symbols */
259 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
260 /* Add an include search path */
262 AddIncludePath (Arg);
267 static void OptListing (const char* Opt attribute ((unused)),
268 const char* Arg attribute ((unused)))
269 /* Create a listing file */
276 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
277 /* Handle the --pagelength option */
279 int Len = atoi (Arg);
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 attribute ((unused)),
289 const char* Arg attribute ((unused)))
290 /* Handle the -s/--smart options */
297 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
298 /* Set the target system */
300 /* Map the target name to a target id */
301 Target = FindTarget (Arg);
302 if (Target == TGT_UNKNOWN) {
303 AbEnd ("Invalid target name: `%s'", Arg);
304 } else if (Target == TGT_MODULE) {
305 AbEnd ("Cannot use `module' as a target for the assembler");
311 static void OptVerbose (const char* Opt attribute ((unused)),
312 const char* Arg attribute ((unused)))
313 /* Increase verbosity */
320 static void OptVersion (const char* Opt attribute ((unused)),
321 const char* Arg attribute ((unused)))
322 /* Print the assembler version */
325 "ca65 V%u.%u.%u - %s\n",
326 VER_MAJOR, VER_MINOR, VER_PATCH, Copyright);
331 static void DoPCAssign (void)
332 /* Start absolute code */
334 long PC = ConstExpression ();
335 if (PC < 0 || PC > 0xFFFFFF) {
344 static void OneLine (void)
345 /* Assemble one line */
347 char Ident [MAX_STR_LEN+1];
350 /* Initialize the new listing line if we are actually reading from file
351 * and not from internally pushed input.
353 if (!HavePushedInput ()) {
357 if (Tok == TOK_COLON) {
358 /* An unnamed label */
363 /* Assemble the line */
364 if (Tok == TOK_IDENT) {
367 if (IsMacro (SVal)) {
369 /* Yes, start a macro expansion */
375 /* No, label. Remember the identifier, then skip it */
376 int HadWS = WS; /* Did we have whitespace before the ident? */
377 strcpy (Ident, SVal);
380 /* If a colon follows, this is a label definition. If there
381 * is no colon, it's an assignment.
386 /* Define the symbol with the expression following the '=' */
387 SymDef (Ident, Expression(), 0, 0);
388 /* Don't allow anything after a symbol definition */
392 SymDef (Ident, CurrentPC(), IsZPSeg(), 1);
393 /* Skip the colon. If NoColonLabels is enabled, allow labels
394 * without a colon if there is no whitespace before the
397 if (Tok != TOK_COLON) {
398 if (HadWS || !NoColonLabels) {
399 Error (ERR_COLON_EXPECTED);
401 if (Tok == TOK_NAMESPACE) {
402 /* Smart :: handling */
415 if (TokIsPseudo (Tok)) {
416 /* A control command, IVal is index into table */
418 } else if (Tok == TOK_MNEMO) {
419 /* A mnemonic - assemble one instruction */
420 HandleInstruction (IVal);
421 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
422 /* A macro expansion */
424 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
427 Error (ERR_EQ_EXPECTED);
430 /* Skip the equal sign */
432 /* Enter absolute mode */
438 /* Line separator must come here */
444 static void Assemble (void)
445 /* Start the ball rolling ... */
450 /* Assemble lines until end of file */
451 while (Tok != TOK_EOF) {
458 static void CreateObjFile (void)
459 /* Create the object file */
461 /* Open the object, write the header */
464 /* Write the object file options */
467 /* Write the list of input files */
470 /* Write the segment data to the file */
473 /* Write the import list */
476 /* Write the export list */
479 /* Write debug symbols if requested */
482 /* Write line infos if requested */
485 /* Write the string pool */
488 /* Write the assertions */
491 /* Write an updated header and close the file */
497 int main (int argc, char* argv [])
498 /* Assembler main program */
500 /* Program long options */
501 static const LongOpt OptTab[] = {
502 { "--auto-import", 0, OptAutoImport },
503 { "--cpu", 1, OptCPU },
504 { "--debug-info", 0, OptDebugInfo },
505 { "--feature", 1, OptFeature },
506 { "--help", 0, OptHelp },
507 { "--ignore-case", 0, OptIgnoreCase },
508 { "--include-dir", 1, OptIncludeDir },
509 { "--listing", 0, OptListing },
510 { "--pagelength", 1, OptPageLength },
511 { "--smart", 0, OptSmart },
512 { "--target", 1, OptTarget },
513 { "--verbose", 0, OptVerbose },
514 { "--version", 0, OptVersion },
519 /* Initialize the cmdline module */
520 InitCmdLine (&argc, &argv, "ca65");
522 /* Enter the base lexical level. We must do that here, since we may
523 * define symbols using -D.
527 /* Check the parameters */
529 while (I < ArgCount) {
531 /* Get the argument */
532 const char* Arg = ArgVec [I];
534 /* Check for an option */
535 if (Arg [0] == '-') {
539 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
543 OptDebugInfo (Arg, 0);
551 OptIgnoreCase (Arg, 0);
559 OutFile = GetArg (&I, 2);
567 OptTarget (Arg, GetArg (&I, 2));
575 DefineSymbol (GetArg (&I, 2));
579 OptIncludeDir (Arg, GetArg (&I, 2));
583 OptAutoImport (Arg, 0);
591 WarnLevel = atoi (GetArg (&I, 2));
600 /* Filename. Check if we already had one */
602 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
614 /* Do we have an input file? */
616 fprintf (stderr, "%s: No input files\n", ProgName);
620 /* If no CPU given, use the default CPU for the target */
621 if (GetCPU () == CPU_UNKNOWN) {
622 if (Target != TGT_UNKNOWN) {
623 SetCPU (DefaultCPU[Target]);
629 /* Intialize the target translation tables */
632 /* Initialize the scanner, open the input file */
633 InitScanner (InFile);
635 /* Define the default options */
638 /* Assemble the input */
641 /* If we didn't have any errors, check the segment stack */
642 if (ErrorCount == 0) {
646 /* If we didn't have any errors, check the unnamed labels */
647 if (ErrorCount == 0) {
651 /* If we didn't have any errors, check the symbol table */
652 if (ErrorCount == 0) {
656 /* If we didn't have any errors, check and resolve the segment data */
657 if (ErrorCount == 0) {
661 /* If we didn't have an errors, index the line infos */
662 MakeLineInfoIndex ();
665 if (Verbosity >= 2) {
670 /* If we didn't have any errors, create the object and listing files */
671 if (ErrorCount == 0) {
678 /* Close the input file */
681 /* Return an apropriate exit code */
682 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;