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 /*****************************************************************************/
42 #include "../common/cmdline.h"
43 #include "../common/version.h"
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void Usage (void)
71 /* Print usage information and exit */
74 "Usage: %s [options] file\n"
76 " -g\t\t\tAdd debug info to object file\n"
77 " -h\t\t\tHelp (this text)\n"
78 " -i\t\t\tIgnore case of symbols\n"
79 " -l\t\t\tCreate a listing if assembly was ok\n"
80 " -o name\t\tName the output file\n"
81 " -s\t\t\tEnable smart mode\n"
82 " -v\t\t\tIncrease verbosity\n"
83 " -D name[=value]\tDefine a symbol\n"
84 " -I dir\t\tSet an include directory search path\n"
85 " -U\t\t\tMark unresolved symbols as import\n"
86 " -V\t\t\tPrint the assembler version\n"
87 " -W n\t\t\tSet warning level n\n"
90 " --auto-import\t\tMark unresolved symbols as import\n"
91 " --cpu type\t\tSet cpu type\n"
92 " --debug-info\t\tAdd debug info to object file\n"
93 " --help\t\tHelp (this text)\n"
94 " --ignore-case\t\tIgnore case of symbols\n"
95 " --include-dir dir\tSet an include directory search path\n"
96 " --listing\t\tCreate a listing if assembly was ok\n"
97 " --pagelength n\tSet the page length for the listing\n"
98 " --smart\t\tEnable smart mode\n"
99 " --verbose\t\tIncrease verbosity\n"
100 " --version\t\tPrint the assembler version\n",
106 static void SetOptions (void)
107 /* Set the option for the translator */
111 /* Set the translator */
112 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
115 /* Set date and time */
116 OptDateTime ((unsigned long) time(0));
121 static void DefineSymbol (const char* Def)
122 /* Define a symbol from the command line */
127 char SymName [MAX_STR_LEN+1];
129 /* The symbol must start with a character or underline */
130 if (Def [0] != '_' && !isalpha (Def [0])) {
135 /* Copy the symbol, checking the rest */
137 while (isalnum (*P) || *P == '_') {
138 if (I <= MAX_STR_LEN) {
145 /* Do we have a value given? */
152 /* We have a value */
156 if (sscanf (P, "%lx", &Val) != 1) {
160 if (sscanf (P, "%li", &Val) != 1) {
166 /* Check if have already a symbol with this name */
167 if (SymIsDef (SymName)) {
168 fprintf (stderr, "`%s' is already defined\n", SymName);
172 /* Define the symbol */
173 SymDef (SymName, LiteralExpr (Val), 0);
178 static void OptAutoImport (const char* Opt, const char* Arg)
179 /* Mark unresolved symbols as imported */
186 static void OptCPU (const char* Opt, const char* Arg)
187 /* Handle the --cpu option */
192 if (strcmp (Arg, "6502") == 0) {
194 } else if (strcmp (Arg, "65C02") == 0) {
196 } else if (strcmp (Arg, "65816") == 0) {
199 } else if (strcmp (Arg, "sunplus") == 0) {
200 SetCPU (CPU_SUNPLUS);
203 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
210 static void OptDebugInfo (const char* Opt, const char* Arg)
211 /* Add debug info to the object file */
218 static void OptHelp (const char* Opt, const char* Arg)
219 /* Print usage information and exit */
227 static void OptIgnoreCase (const char* Opt, const char* Arg)
228 /* Ignore case on symbols */
235 static void OptIncludeDir (const char* Opt, const char* Arg)
236 /* Add an include search path */
241 AddIncludePath (Arg);
246 static void OptListing (const char* Opt, const char* Arg)
247 /* Create a listing file */
254 static void OptPageLength (const char* Opt, const char* Arg)
255 /* Handle the --pagelength option */
262 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
263 fprintf (stderr, "Invalid page length: %d\n", Len);
271 static void OptSmart (const char* Opt, const char* Arg)
272 /* Handle the -s/--smart options */
279 static void OptVerbose (const char* Opt, const char* Arg)
280 /* Increase verbosity */
287 static void OptVersion (const char* Opt, const char* Arg)
288 /* Print the assembler version */
291 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
292 VER_MAJOR, VER_MINOR, VER_PATCH);
297 static void OneLine (void)
298 /* Assemble one line */
300 char Ident [MAX_STR_LEN+1];
303 /* Initialize the new listing line if we are actually reading from file
304 * and not from internally pushed input.
306 if (!HavePushedInput ()) {
310 if (Tok == TOK_COLON) {
311 /* An unnamed label */
316 /* Assemble the line */
317 if (Tok == TOK_IDENT) {
320 if (IsMacro (SVal)) {
322 /* Yes, start a macro expansion */
328 /* No, label. Remember the identifier, then skip it */
329 int HadWS = WS; /* Did we have whitespace before the ident? */
330 strcpy (Ident, SVal);
333 /* If a colon follows, this is a label definition. If there
334 * is no colon, it's an assignment.
339 /* Define the symbol with the expression following the '=' */
340 SymDef (Ident, Expression (), 0);
341 /* Don't allow anything after a symbol definition */
345 SymDef (Ident, CurrentPC (), IsZPSeg ());
346 /* Skip the colon. If NoColonLabels is enabled, allow labels
347 * without a colon if there is no whitespace before the
350 if (Tok != TOK_COLON) {
351 if (HadWS || !NoColonLabels) {
352 Error (ERR_COLON_EXPECTED);
354 if (Tok == TOK_NAMESPACE) {
355 /* Smart :: handling */
368 if (TokIsPseudo (Tok)) {
369 /* A control command, IVal is index into table */
371 } else if (Tok == TOK_MNEMO) {
372 /* A mnemonic - assemble one instruction */
373 HandleInstruction (IVal);
374 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
375 /* A macro expansion */
380 /* Line separator must come here */
386 static void Assemble (void)
387 /* Start the ball rolling ... */
392 /* Assemble lines until end of file */
393 while (Tok != TOK_EOF) {
400 static void CreateObjFile (void)
401 /* Create the object file */
403 /* Open the object, write the header */
406 /* Write the object file options */
409 /* Write the list of input files */
412 /* Write the segment data to the file */
415 /* Write the import list */
418 /* Write the export list */
421 /* Write debug symbols if requested */
424 /* Write an updated header and close the file */
430 int main (int argc, char* argv [])
431 /* Assembler main program */
433 /* Program long options */
434 static const LongOpt OptTab[] = {
435 { "--auto-import", 0, OptAutoImport },
436 { "--cpu", 1, OptCPU },
437 { "--debug-info", 0, OptDebugInfo },
438 { "--help", 0, OptHelp },
439 { "--ignore-case", 0, OptIgnoreCase },
440 { "--include-dir", 1, OptIncludeDir },
441 { "--listing", 0, OptListing },
442 { "--pagelength", 1, OptPageLength },
443 { "--smart", 0, OptSmart },
444 { "--verbose", 0, OptVerbose },
445 { "--version", 0, OptVersion },
450 /* Initialize the cmdline module */
451 InitCmdLine (argc, argv, "ca65");
453 /* Enter the base lexical level. We must do that here, since we may
454 * define symbols using -D.
458 /* Check the parameters */
462 /* Get the argument */
463 const char* Arg = argv [I];
465 /* Check for an option */
466 if (Arg [0] == '-') {
470 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
474 OptDebugInfo (Arg, 0);
482 OptIgnoreCase (Arg, 0);
490 OutFile = GetArg (&I, 2);
502 DefineSymbol (GetArg (&I, 2));
506 OptIncludeDir (Arg, GetArg (&I, 2));
510 OptAutoImport (Arg, 0);
518 WarnLevel = atoi (GetArg (&I, 2));
527 /* Filename. Check if we already had one */
529 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
541 /* Do we have an input file? */
543 fprintf (stderr, "%s: No input files\n", ProgName);
547 /* Initialize the scanner, open the input file */
548 InitScanner (InFile);
550 /* Define the default options */
553 /* Assemble the input */
556 /* If we didn't have any errors, check the unnamed labels */
557 if (ErrorCount == 0) {
561 /* If we didn't have any errors, check the symbol table */
562 if (ErrorCount == 0) {
566 /* If we didn't have any errors, check and resolve the segment data */
567 if (ErrorCount == 0) {
577 /* If we didn't have any errors, create the object and listing files */
578 if (ErrorCount == 0) {
585 /* Close the input file */
588 /* Return an apropriate exit code */
589 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;