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 /*****************************************************************************/
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static void Usage (void)
78 /* Print usage information and exit */
81 "Usage: %s [options] file\n"
83 " -g\t\t\tAdd debug info to object file\n"
84 " -h\t\t\tHelp (this text)\n"
85 " -i\t\t\tIgnore case of symbols\n"
86 " -l\t\t\tCreate a listing if assembly was ok\n"
87 " -o name\t\tName the output file\n"
88 " -s\t\t\tEnable smart mode\n"
89 " -t sys\t\tSet the target system\n"
90 " -v\t\t\tIncrease verbosity\n"
91 " -D name[=value]\tDefine a symbol\n"
92 " -I dir\t\tSet an include directory search path\n"
93 " -U\t\t\tMark unresolved symbols as import\n"
94 " -V\t\t\tPrint the assembler version\n"
95 " -W n\t\t\tSet warning level n\n"
98 " --auto-import\t\tMark unresolved symbols as import\n"
99 " --cpu type\t\tSet cpu type\n"
100 " --debug-info\t\tAdd debug info to object file\n"
101 " --feature name\tSet an emulation feature\n"
102 " --help\t\tHelp (this text)\n"
103 " --ignore-case\t\tIgnore case of symbols\n"
104 " --include-dir dir\tSet an include directory search path\n"
105 " --listing\t\tCreate a listing if assembly was ok\n"
106 " --pagelength n\tSet the page length for the listing\n"
107 " --smart\t\tEnable smart mode\n"
108 " --target sys\t\tSet the target system\n"
109 " --verbose\t\tIncrease verbosity\n"
110 " --version\t\tPrint the assembler version\n",
116 static void SetOptions (void)
117 /* Set the option for the translator */
121 /* Set the translator */
122 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
125 /* Set date and time */
126 OptDateTime ((unsigned long) time(0));
131 static void DefineSymbol (const char* Def)
132 /* Define a symbol from the command line */
137 char SymName [MAX_STR_LEN+1];
139 /* The symbol must start with a character or underline */
140 if (Def [0] != '_' && !isalpha (Def [0])) {
145 /* Copy the symbol, checking the rest */
147 while (isalnum (*P) || *P == '_') {
148 if (I <= MAX_STR_LEN) {
155 /* Do we have a value given? */
162 /* We have a value */
166 if (sscanf (P, "%lx", &Val) != 1) {
170 if (sscanf (P, "%li", &Val) != 1) {
176 /* Check if have already a symbol with this name */
177 if (SymIsDef (SymName)) {
178 AbEnd ("`%s' is already defined", SymName);
181 /* Define the symbol */
182 SymDef (SymName, LiteralExpr (Val), 0);
187 static void OptAutoImport (const char* Opt, const char* Arg)
188 /* Mark unresolved symbols as imported */
195 static void OptCPU (const char* Opt, const char* Arg)
196 /* Handle the --cpu option */
201 if (strcmp (Arg, "6502") == 0) {
203 } else if (strcmp (Arg, "65C02") == 0) {
205 } else if (strcmp (Arg, "65816") == 0) {
208 } else if (strcmp (Arg, "sunplus") == 0) {
209 SetCPU (CPU_SUNPLUS);
212 AbEnd ("Invalid CPU: `%s'", Arg);
218 static void OptDebugInfo (const char* Opt, const char* Arg)
219 /* Add debug info to the object file */
226 static void OptFeature (const char* Opt, const char* Arg)
227 /* Set an emulation feature */
229 /* Set the feature, check for errors */
230 if (SetFeature (Arg) == FEAT_UNKNOWN) {
231 AbEnd ("Illegal emulation feature: `%s'", Arg);
237 static void OptHelp (const char* Opt, const char* Arg)
238 /* Print usage information and exit */
246 static void OptIgnoreCase (const char* Opt, const char* Arg)
247 /* Ignore case on symbols */
254 static void OptIncludeDir (const char* Opt, const char* Arg)
255 /* Add an include search path */
260 AddIncludePath (Arg);
265 static void OptListing (const char* Opt, const char* Arg)
266 /* Create a listing file */
273 static void OptPageLength (const char* Opt, const char* Arg)
274 /* Handle the --pagelength option */
281 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
282 AbEnd ("Invalid page length: %d", Len);
289 static void OptSmart (const char* Opt, const char* Arg)
290 /* Handle the -s/--smart options */
297 static void OptTarget (const char* Opt, const char* Arg)
298 /* Set the target system */
304 /* Map the target name to a target id */
305 Target = FindTarget (Arg);
306 if (Target == TGT_UNKNOWN) {
307 AbEnd ("Invalid target name: `%s'", Arg);
313 static void OptVerbose (const char* Opt, const char* Arg)
314 /* Increase verbosity */
321 static void OptVersion (const char* Opt, const char* Arg)
322 /* Print the assembler version */
325 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
326 VER_MAJOR, VER_MINOR, VER_PATCH);
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);
388 /* Don't allow anything after a symbol definition */
392 SymDef (Ident, CurrentPC (), IsZPSeg ());
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 an updated header and close the file */
488 int main (int argc, char* argv [])
489 /* Assembler main program */
491 /* Program long options */
492 static const LongOpt OptTab[] = {
493 { "--auto-import", 0, OptAutoImport },
494 { "--cpu", 1, OptCPU },
495 { "--debug-info", 0, OptDebugInfo },
496 { "--feature", 1, OptFeature },
497 { "--help", 0, OptHelp },
498 { "--ignore-case", 0, OptIgnoreCase },
499 { "--include-dir", 1, OptIncludeDir },
500 { "--listing", 0, OptListing },
501 { "--pagelength", 1, OptPageLength },
502 { "--smart", 0, OptSmart },
503 { "--target", 1, OptTarget },
504 { "--verbose", 0, OptVerbose },
505 { "--version", 0, OptVersion },
510 /* Initialize the cmdline module */
511 InitCmdLine (argc, argv, "ca65");
513 /* Enter the base lexical level. We must do that here, since we may
514 * define symbols using -D.
518 /* Check the parameters */
522 /* Get the argument */
523 const char* Arg = argv [I];
525 /* Check for an option */
526 if (Arg [0] == '-') {
530 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
534 OptDebugInfo (Arg, 0);
542 OptIgnoreCase (Arg, 0);
550 OutFile = GetArg (&I, 2);
558 OptTarget (Arg, GetArg (&I, 2));
566 DefineSymbol (GetArg (&I, 2));
570 OptIncludeDir (Arg, GetArg (&I, 2));
574 OptAutoImport (Arg, 0);
582 WarnLevel = atoi (GetArg (&I, 2));
591 /* Filename. Check if we already had one */
593 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
605 /* Do we have an input file? */
607 fprintf (stderr, "%s: No input files\n", ProgName);
611 /* Intialize the target translation tables */
614 /* Initialize the scanner, open the input file */
615 InitScanner (InFile);
617 /* Define the default options */
620 /* Assemble the input */
623 /* If we didn't have any errors, check the unnamed labels */
624 if (ErrorCount == 0) {
628 /* If we didn't have any errors, check the symbol table */
629 if (ErrorCount == 0) {
633 /* If we didn't have any errors, check and resolve the segment data */
634 if (ErrorCount == 0) {
644 /* If we didn't have any errors, create the object and listing files */
645 if (ErrorCount == 0) {
652 /* Close the input file */
655 /* Return an apropriate exit code */
656 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;