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 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void Usage (void)
74 /* Print usage information and exit */
77 "Usage: %s [options] file\n"
79 " -g\t\t\tAdd debug info to object file\n"
80 " -h\t\t\tHelp (this text)\n"
81 " -i\t\t\tIgnore case of symbols\n"
82 " -l\t\t\tCreate a listing if assembly was ok\n"
83 " -o name\t\tName the output file\n"
84 " -s\t\t\tEnable smart mode\n"
85 " -v\t\t\tIncrease verbosity\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"
93 " --auto-import\t\tMark unresolved symbols as import\n"
94 " --cpu type\t\tSet cpu type\n"
95 " --debug-info\t\tAdd debug info to object file\n"
96 " --help\t\tHelp (this text)\n"
97 " --ignore-case\t\tIgnore case of symbols\n"
98 " --include-dir dir\tSet an include directory search path\n"
99 " --listing\t\tCreate a listing if assembly was ok\n"
100 " --pagelength n\tSet the page length for the listing\n"
101 " --smart\t\tEnable smart mode\n"
102 " --verbose\t\tIncrease verbosity\n"
103 " --version\t\tPrint the assembler version\n",
109 static void SetOptions (void)
110 /* Set the option for the translator */
114 /* Set the translator */
115 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
118 /* Set date and time */
119 OptDateTime ((unsigned long) time(0));
124 static void DefineSymbol (const char* Def)
125 /* Define a symbol from the command line */
130 char SymName [MAX_STR_LEN+1];
132 /* The symbol must start with a character or underline */
133 if (Def [0] != '_' && !isalpha (Def [0])) {
138 /* Copy the symbol, checking the rest */
140 while (isalnum (*P) || *P == '_') {
141 if (I <= MAX_STR_LEN) {
148 /* Do we have a value given? */
155 /* We have a value */
159 if (sscanf (P, "%lx", &Val) != 1) {
163 if (sscanf (P, "%li", &Val) != 1) {
169 /* Check if have already a symbol with this name */
170 if (SymIsDef (SymName)) {
171 fprintf (stderr, "`%s' is already defined\n", SymName);
175 /* Define the symbol */
176 SymDef (SymName, LiteralExpr (Val), 0);
181 static void OptAutoImport (const char* Opt, const char* Arg)
182 /* Mark unresolved symbols as imported */
189 static void OptCPU (const char* Opt, const char* Arg)
190 /* Handle the --cpu option */
195 if (strcmp (Arg, "6502") == 0) {
197 } else if (strcmp (Arg, "65C02") == 0) {
199 } else if (strcmp (Arg, "65816") == 0) {
202 } else if (strcmp (Arg, "sunplus") == 0) {
203 SetCPU (CPU_SUNPLUS);
206 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
213 static void OptDebugInfo (const char* Opt, const char* Arg)
214 /* Add debug info to the object file */
221 static void OptHelp (const char* Opt, const char* Arg)
222 /* Print usage information and exit */
230 static void OptIgnoreCase (const char* Opt, const char* Arg)
231 /* Ignore case on symbols */
238 static void OptIncludeDir (const char* Opt, const char* Arg)
239 /* Add an include search path */
244 AddIncludePath (Arg);
249 static void OptListing (const char* Opt, const char* Arg)
250 /* Create a listing file */
257 static void OptPageLength (const char* Opt, const char* Arg)
258 /* Handle the --pagelength option */
265 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
266 fprintf (stderr, "Invalid page length: %d\n", Len);
274 static void OptSmart (const char* Opt, const char* Arg)
275 /* Handle the -s/--smart options */
282 static void OptVerbose (const char* Opt, const char* Arg)
283 /* Increase verbosity */
290 static void OptVersion (const char* Opt, const char* Arg)
291 /* Print the assembler version */
294 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
295 VER_MAJOR, VER_MINOR, VER_PATCH);
300 static void OneLine (void)
301 /* Assemble one line */
303 char Ident [MAX_STR_LEN+1];
306 /* Initialize the new listing line if we are actually reading from file
307 * and not from internally pushed input.
309 if (!HavePushedInput ()) {
313 if (Tok == TOK_COLON) {
314 /* An unnamed label */
319 /* Assemble the line */
320 if (Tok == TOK_IDENT) {
323 if (IsMacro (SVal)) {
325 /* Yes, start a macro expansion */
331 /* No, label. Remember the identifier, then skip it */
332 int HadWS = WS; /* Did we have whitespace before the ident? */
333 strcpy (Ident, SVal);
336 /* If a colon follows, this is a label definition. If there
337 * is no colon, it's an assignment.
342 /* Define the symbol with the expression following the '=' */
343 SymDef (Ident, Expression (), 0);
344 /* Don't allow anything after a symbol definition */
348 SymDef (Ident, CurrentPC (), IsZPSeg ());
349 /* Skip the colon. If NoColonLabels is enabled, allow labels
350 * without a colon if there is no whitespace before the
353 if (Tok != TOK_COLON) {
354 if (HadWS || !NoColonLabels) {
355 Error (ERR_COLON_EXPECTED);
357 if (Tok == TOK_NAMESPACE) {
358 /* Smart :: handling */
371 if (TokIsPseudo (Tok)) {
372 /* A control command, IVal is index into table */
374 } else if (Tok == TOK_MNEMO) {
375 /* A mnemonic - assemble one instruction */
376 HandleInstruction (IVal);
377 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
378 /* A macro expansion */
383 /* Line separator must come here */
389 static void Assemble (void)
390 /* Start the ball rolling ... */
395 /* Assemble lines until end of file */
396 while (Tok != TOK_EOF) {
403 static void CreateObjFile (void)
404 /* Create the object file */
406 /* Open the object, write the header */
409 /* Write the object file options */
412 /* Write the list of input files */
415 /* Write the segment data to the file */
418 /* Write the import list */
421 /* Write the export list */
424 /* Write debug symbols if requested */
427 /* Write an updated header and close the file */
433 int main (int argc, char* argv [])
434 /* Assembler main program */
436 /* Program long options */
437 static const LongOpt OptTab[] = {
438 { "--auto-import", 0, OptAutoImport },
439 { "--cpu", 1, OptCPU },
440 { "--debug-info", 0, OptDebugInfo },
441 { "--help", 0, OptHelp },
442 { "--ignore-case", 0, OptIgnoreCase },
443 { "--include-dir", 1, OptIncludeDir },
444 { "--listing", 0, OptListing },
445 { "--pagelength", 1, OptPageLength },
446 { "--smart", 0, OptSmart },
447 { "--verbose", 0, OptVerbose },
448 { "--version", 0, OptVersion },
453 /* Initialize the cmdline module */
454 InitCmdLine (argc, argv, "ca65");
456 /* Enter the base lexical level. We must do that here, since we may
457 * define symbols using -D.
461 /* Check the parameters */
465 /* Get the argument */
466 const char* Arg = argv [I];
468 /* Check for an option */
469 if (Arg [0] == '-') {
473 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
477 OptDebugInfo (Arg, 0);
485 OptIgnoreCase (Arg, 0);
493 OutFile = GetArg (&I, 2);
505 DefineSymbol (GetArg (&I, 2));
509 OptIncludeDir (Arg, GetArg (&I, 2));
513 OptAutoImport (Arg, 0);
521 WarnLevel = atoi (GetArg (&I, 2));
530 /* Filename. Check if we already had one */
532 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
544 /* Do we have an input file? */
546 fprintf (stderr, "%s: No input files\n", ProgName);
550 /* Initialize the scanner, open the input file */
551 InitScanner (InFile);
553 /* Define the default options */
556 /* Assemble the input */
559 /* If we didn't have any errors, check the unnamed labels */
560 if (ErrorCount == 0) {
564 /* If we didn't have any errors, check the symbol table */
565 if (ErrorCount == 0) {
569 /* If we didn't have any errors, check and resolve the segment data */
570 if (ErrorCount == 0) {
580 /* If we didn't have any errors, create the object and listing files */
581 if (ErrorCount == 0) {
588 /* Close the input file */
591 /* Return an apropriate exit code */
592 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;