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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Usage (void)
73 /* Print usage information and exit */
76 "Usage: %s [options] file\n"
78 " -g\t\t\tAdd debug info to object file\n"
79 " -h\t\t\tHelp (this text)\n"
80 " -i\t\t\tIgnore case of symbols\n"
81 " -l\t\t\tCreate a listing if assembly was ok\n"
82 " -o name\t\tName the output file\n"
83 " -s\t\t\tEnable smart mode\n"
84 " -v\t\t\tIncrease verbosity\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"
92 " --auto-import\t\tMark unresolved symbols as import\n"
93 " --cpu type\t\tSet cpu type\n"
94 " --debug-info\t\tAdd debug info to object file\n"
95 " --help\t\tHelp (this text)\n"
96 " --ignore-case\t\tIgnore case of symbols\n"
97 " --include-dir dir\tSet an include directory search path\n"
98 " --listing\t\tCreate a listing if assembly was ok\n"
99 " --pagelength n\tSet the page length for the listing\n"
100 " --smart\t\tEnable smart mode\n"
101 " --verbose\t\tIncrease verbosity\n"
102 " --version\t\tPrint the assembler version\n",
108 static void SetOptions (void)
109 /* Set the option for the translator */
113 /* Set the translator */
114 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
117 /* Set date and time */
118 OptDateTime ((unsigned long) time(0));
123 static void DefineSymbol (const char* Def)
124 /* Define a symbol from the command line */
129 char SymName [MAX_STR_LEN+1];
131 /* The symbol must start with a character or underline */
132 if (Def [0] != '_' && !isalpha (Def [0])) {
137 /* Copy the symbol, checking the rest */
139 while (isalnum (*P) || *P == '_') {
140 if (I <= MAX_STR_LEN) {
147 /* Do we have a value given? */
154 /* We have a value */
158 if (sscanf (P, "%lx", &Val) != 1) {
162 if (sscanf (P, "%li", &Val) != 1) {
168 /* Check if have already a symbol with this name */
169 if (SymIsDef (SymName)) {
170 fprintf (stderr, "`%s' is already defined\n", SymName);
174 /* Define the symbol */
175 SymDef (SymName, LiteralExpr (Val), 0);
180 static void OptAutoImport (const char* Opt, const char* Arg)
181 /* Mark unresolved symbols as imported */
188 static void OptCPU (const char* Opt, const char* Arg)
189 /* Handle the --cpu option */
194 if (strcmp (Arg, "6502") == 0) {
196 } else if (strcmp (Arg, "65C02") == 0) {
198 } else if (strcmp (Arg, "65816") == 0) {
201 } else if (strcmp (Arg, "sunplus") == 0) {
202 SetCPU (CPU_SUNPLUS);
205 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
212 static void OptDebugInfo (const char* Opt, const char* Arg)
213 /* Add debug info to the object file */
220 static void OptHelp (const char* Opt, const char* Arg)
221 /* Print usage information and exit */
229 static void OptIgnoreCase (const char* Opt, const char* Arg)
230 /* Ignore case on symbols */
237 static void OptIncludeDir (const char* Opt, const char* Arg)
238 /* Add an include search path */
243 AddIncludePath (Arg);
248 static void OptListing (const char* Opt, const char* Arg)
249 /* Create a listing file */
256 static void OptPageLength (const char* Opt, const char* Arg)
257 /* Handle the --pagelength option */
264 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
265 fprintf (stderr, "Invalid page length: %d\n", Len);
273 static void OptSmart (const char* Opt, const char* Arg)
274 /* Handle the -s/--smart options */
281 static void OptVerbose (const char* Opt, const char* Arg)
282 /* Increase verbosity */
289 static void OptVersion (const char* Opt, const char* Arg)
290 /* Print the assembler version */
293 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
294 VER_MAJOR, VER_MINOR, VER_PATCH);
299 static void OneLine (void)
300 /* Assemble one line */
302 char Ident [MAX_STR_LEN+1];
305 /* Initialize the new listing line if we are actually reading from file
306 * and not from internally pushed input.
308 if (!HavePushedInput ()) {
312 if (Tok == TOK_COLON) {
313 /* An unnamed label */
318 /* Assemble the line */
319 if (Tok == TOK_IDENT) {
322 if (IsMacro (SVal)) {
324 /* Yes, start a macro expansion */
330 /* No, label. Remember the identifier, then skip it */
331 int HadWS = WS; /* Did we have whitespace before the ident? */
332 strcpy (Ident, SVal);
335 /* If a colon follows, this is a label definition. If there
336 * is no colon, it's an assignment.
341 /* Define the symbol with the expression following the '=' */
342 SymDef (Ident, Expression (), 0);
343 /* Don't allow anything after a symbol definition */
347 SymDef (Ident, CurrentPC (), IsZPSeg ());
348 /* Skip the colon. If NoColonLabels is enabled, allow labels
349 * without a colon if there is no whitespace before the
352 if (Tok != TOK_COLON) {
353 if (HadWS || !NoColonLabels) {
354 Error (ERR_COLON_EXPECTED);
356 if (Tok == TOK_NAMESPACE) {
357 /* Smart :: handling */
370 if (TokIsPseudo (Tok)) {
371 /* A control command, IVal is index into table */
373 } else if (Tok == TOK_MNEMO) {
374 /* A mnemonic - assemble one instruction */
375 HandleInstruction (IVal);
376 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
377 /* A macro expansion */
382 /* Line separator must come here */
388 static void Assemble (void)
389 /* Start the ball rolling ... */
394 /* Assemble lines until end of file */
395 while (Tok != TOK_EOF) {
402 static void CreateObjFile (void)
403 /* Create the object file */
405 /* Open the object, write the header */
408 /* Write the object file options */
411 /* Write the list of input files */
414 /* Write the segment data to the file */
417 /* Write the import list */
420 /* Write the export list */
423 /* Write debug symbols if requested */
426 /* Write an updated header and close the file */
432 int main (int argc, char* argv [])
433 /* Assembler main program */
435 /* Program long options */
436 static const LongOpt OptTab[] = {
437 { "--auto-import", 0, OptAutoImport },
438 { "--cpu", 1, OptCPU },
439 { "--debug-info", 0, OptDebugInfo },
440 { "--help", 0, OptHelp },
441 { "--ignore-case", 0, OptIgnoreCase },
442 { "--include-dir", 1, OptIncludeDir },
443 { "--listing", 0, OptListing },
444 { "--pagelength", 1, OptPageLength },
445 { "--smart", 0, OptSmart },
446 { "--verbose", 0, OptVerbose },
447 { "--version", 0, OptVersion },
452 /* Initialize the cmdline module */
453 InitCmdLine (argc, argv, "ca65");
455 /* Enter the base lexical level. We must do that here, since we may
456 * define symbols using -D.
460 /* Check the parameters */
464 /* Get the argument */
465 const char* Arg = argv [I];
467 /* Check for an option */
468 if (Arg [0] == '-') {
472 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
476 OptDebugInfo (Arg, 0);
484 OptIgnoreCase (Arg, 0);
492 OutFile = GetArg (&I, 2);
504 DefineSymbol (GetArg (&I, 2));
508 OptIncludeDir (Arg, GetArg (&I, 2));
512 OptAutoImport (Arg, 0);
520 WarnLevel = atoi (GetArg (&I, 2));
529 /* Filename. Check if we already had one */
531 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
543 /* Do we have an input file? */
545 fprintf (stderr, "%s: No input files\n", ProgName);
549 /* Initialize the scanner, open the input file */
550 InitScanner (InFile);
552 /* Define the default options */
555 /* Assemble the input */
558 /* If we didn't have any errors, check the unnamed labels */
559 if (ErrorCount == 0) {
563 /* If we didn't have any errors, check the symbol table */
564 if (ErrorCount == 0) {
568 /* If we didn't have any errors, check and resolve the segment data */
569 if (ErrorCount == 0) {
579 /* If we didn't have any errors, create the object and listing files */
580 if (ErrorCount == 0) {
587 /* Close the input file */
590 /* Return an apropriate exit code */
591 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;