1 /*****************************************************************************/
5 /* Main program for the ca65 macroassembler */
9 /* (C) 1998-2001 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 " -D name[=value]\tDefine a symbol\n"
86 " -I dir\t\tSet an include directory search path\n"
87 " -U\t\t\tMark unresolved symbols as import\n"
88 " -V\t\t\tPrint the assembler version\n"
89 " -W n\t\t\tSet warning level n\n"
90 " -g\t\t\tAdd debug info to object file\n"
91 " -h\t\t\tHelp (this text)\n"
92 " -i\t\t\tIgnore case of symbols\n"
93 " -l\t\t\tCreate a listing if assembly was ok\n"
94 " -o name\t\tName the output file\n"
95 " -s\t\t\tEnable smart mode\n"
96 " -t sys\t\tSet the target system\n"
97 " -v\t\t\tIncrease verbosity\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 attribute ((unused)),
190 const char* Arg attribute ((unused)))
191 /* Mark unresolved symbols as imported */
198 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
199 /* 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 attribute ((unused)),
219 const char* Arg attribute ((unused)))
220 /* Add debug info to the object file */
227 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
228 /* Set an emulation feature */
230 /* Set the feature, check for errors */
231 if (SetFeature (Arg) == FEAT_UNKNOWN) {
232 AbEnd ("Illegal emulation feature: `%s'", Arg);
238 static void OptHelp (const char* Opt attribute ((unused)),
239 const char* Arg attribute ((unused)))
240 /* Print usage information and exit */
248 static void OptIgnoreCase (const char* Opt attribute ((unused)),
249 const char* Arg attribute ((unused)))
250 /* Ignore case on symbols */
257 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
258 /* Add an include search path */
260 AddIncludePath (Arg);
265 static void OptListing (const char* Opt attribute ((unused)),
266 const char* Arg attribute ((unused)))
267 /* Create a listing file */
274 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
275 /* Handle the --pagelength option */
277 int Len = atoi (Arg);
278 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
279 AbEnd ("Invalid page length: %d", Len);
286 static void OptSmart (const char* Opt attribute ((unused)),
287 const char* Arg attribute ((unused)))
288 /* Handle the -s/--smart options */
295 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
296 /* Set the target system */
298 /* Map the target name to a target id */
299 Target = FindTarget (Arg);
300 if (Target == TGT_UNKNOWN) {
301 AbEnd ("Invalid target name: `%s'", Arg);
307 static void OptVerbose (const char* Opt attribute ((unused)),
308 const char* Arg attribute ((unused)))
309 /* Increase verbosity */
316 static void OptVersion (const char* Opt attribute ((unused)),
317 const char* Arg attribute ((unused)))
318 /* Print the assembler version */
321 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
322 VER_MAJOR, VER_MINOR, VER_PATCH);
327 static void DoPCAssign (void)
328 /* Start absolute code */
330 long PC = ConstExpression ();
331 if (PC < 0 || PC > 0xFFFFFF) {
340 static void OneLine (void)
341 /* Assemble one line */
343 char Ident [MAX_STR_LEN+1];
346 /* Initialize the new listing line if we are actually reading from file
347 * and not from internally pushed input.
349 if (!HavePushedInput ()) {
353 if (Tok == TOK_COLON) {
354 /* An unnamed label */
359 /* Assemble the line */
360 if (Tok == TOK_IDENT) {
363 if (IsMacro (SVal)) {
365 /* Yes, start a macro expansion */
371 /* No, label. Remember the identifier, then skip it */
372 int HadWS = WS; /* Did we have whitespace before the ident? */
373 strcpy (Ident, SVal);
376 /* If a colon follows, this is a label definition. If there
377 * is no colon, it's an assignment.
382 /* Define the symbol with the expression following the '=' */
383 SymDef (Ident, Expression(), 0, 0);
384 /* Don't allow anything after a symbol definition */
388 SymDef (Ident, CurrentPC(), IsZPSeg(), 1);
389 /* Skip the colon. If NoColonLabels is enabled, allow labels
390 * without a colon if there is no whitespace before the
393 if (Tok != TOK_COLON) {
394 if (HadWS || !NoColonLabels) {
395 Error (ERR_COLON_EXPECTED);
397 if (Tok == TOK_NAMESPACE) {
398 /* Smart :: handling */
411 if (TokIsPseudo (Tok)) {
412 /* A control command, IVal is index into table */
414 } else if (Tok == TOK_MNEMO) {
415 /* A mnemonic - assemble one instruction */
416 HandleInstruction (IVal);
417 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
418 /* A macro expansion */
420 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
423 Error (ERR_EQ_EXPECTED);
426 /* Skip the equal sign */
428 /* Enter absolute mode */
434 /* Line separator must come here */
440 static void Assemble (void)
441 /* Start the ball rolling ... */
446 /* Assemble lines until end of file */
447 while (Tok != TOK_EOF) {
454 static void CreateObjFile (void)
455 /* Create the object file */
457 /* Open the object, write the header */
460 /* Write the object file options */
463 /* Write the list of input files */
466 /* Write the segment data to the file */
469 /* Write the import list */
472 /* Write the export list */
475 /* Write debug symbols if requested */
478 /* Write line infos if requested */
481 /* Write an updated header and close the file */
487 int main (int argc, char* argv [])
488 /* Assembler main program */
490 /* Program long options */
491 static const LongOpt OptTab[] = {
492 { "--auto-import", 0, OptAutoImport },
493 { "--cpu", 1, OptCPU },
494 { "--debug-info", 0, OptDebugInfo },
495 { "--feature", 1, OptFeature },
496 { "--help", 0, OptHelp },
497 { "--ignore-case", 0, OptIgnoreCase },
498 { "--include-dir", 1, OptIncludeDir },
499 { "--listing", 0, OptListing },
500 { "--pagelength", 1, OptPageLength },
501 { "--smart", 0, OptSmart },
502 { "--target", 1, OptTarget },
503 { "--verbose", 0, OptVerbose },
504 { "--version", 0, OptVersion },
509 /* Initialize the cmdline module */
510 InitCmdLine (&argc, &argv, "ca65");
512 /* Enter the base lexical level. We must do that here, since we may
513 * define symbols using -D.
517 /* Check the parameters */
519 while (I < ArgCount) {
521 /* Get the argument */
522 const char* Arg = ArgVec [I];
524 /* Check for an option */
525 if (Arg [0] == '-') {
529 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
533 OptDebugInfo (Arg, 0);
541 OptIgnoreCase (Arg, 0);
549 OutFile = GetArg (&I, 2);
557 OptTarget (Arg, GetArg (&I, 2));
565 DefineSymbol (GetArg (&I, 2));
569 OptIncludeDir (Arg, GetArg (&I, 2));
573 OptAutoImport (Arg, 0);
581 WarnLevel = atoi (GetArg (&I, 2));
590 /* Filename. Check if we already had one */
592 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
604 /* Do we have an input file? */
606 fprintf (stderr, "%s: No input files\n", ProgName);
610 /* Intialize the target translation tables */
613 /* Initialize the scanner, open the input file */
614 InitScanner (InFile);
616 /* Define the default options */
619 /* Assemble the input */
622 /* If we didn't have any errors, check the unnamed labels */
623 if (ErrorCount == 0) {
627 /* If we didn't have any errors, check the symbol table */
628 if (ErrorCount == 0) {
632 /* If we didn't have any errors, check and resolve the segment data */
633 if (ErrorCount == 0) {
637 /* If we didn't have an errors, index the line infos */
638 MakeLineInfoIndex ();
641 if (Verbosity >= 2) {
646 /* If we didn't have any errors, create the object and listing files */
647 if (ErrorCount == 0) {
654 /* Close the input file */
657 /* Return an apropriate exit code */
658 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;