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"
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Usage (void)
70 /* Print usage information and exit */
73 "Usage: %s [options] file\n"
75 " -g\t\t\tAdd debug info to object file\n"
76 " -h\t\t\tHelp (this text)\n"
77 " -i\t\t\tIgnore case of symbols\n"
78 " -l\t\t\tCreate a listing if assembly was ok\n"
79 " -o name\t\tName the output file\n"
80 " -s\t\t\tEnable smart mode\n"
81 " -v\t\t\tIncrease verbosity\n"
82 " -D name[=value]\tDefine a symbol\n"
83 " -I dir\t\tSet an include directory search path\n"
84 " -U\t\t\tMark unresolved symbols as import\n"
85 " -V\t\t\tPrint the assembler version\n"
86 " -W n\t\t\tSet warning level n\n"
89 " --auto-import\t\tMark unresolved symbols as import\n"
90 " --cpu type\t\tSet cpu type\n"
91 " --debug-info\t\tAdd debug info to object file\n"
92 " --help\t\tHelp (this text)\n"
93 " --ignore-case\t\tIgnore case of symbols\n"
94 " --include-dir dir\tSet an include directory search path\n"
95 " --listing\t\tCreate a listing if assembly was ok\n"
96 " --pagelength n\tSet the page length for the listing\n"
97 " --smart\t\tEnable smart mode\n"
98 " --verbose\t\tIncrease verbosity\n"
99 " --version\t\tPrint the assembler version\n",
105 static void SetOptions (void)
106 /* Set the option for the translator */
110 /* Set the translator */
111 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
114 /* Set date and time */
115 OptDateTime ((unsigned long) time(0));
120 static void DefineSymbol (const char* Def)
121 /* Define a symbol from the command line */
126 char SymName [MAX_STR_LEN+1];
128 /* The symbol must start with a character or underline */
129 if (Def [0] != '_' && !isalpha (Def [0])) {
134 /* Copy the symbol, checking the rest */
136 while (isalnum (*P) || *P == '_') {
137 if (I <= MAX_STR_LEN) {
144 /* Do we have a value given? */
151 /* We have a value */
155 if (sscanf (P, "%lx", &Val) != 1) {
159 if (sscanf (P, "%li", &Val) != 1) {
165 /* Check if have already a symbol with this name */
166 if (SymIsDef (SymName)) {
167 fprintf (stderr, "`%s' is already defined\n", SymName);
171 /* Define the symbol */
172 SymDef (SymName, LiteralExpr (Val), 0);
177 static void OptAutoImport (const char* Opt, const char* Arg)
178 /* Mark unresolved symbols as imported */
185 static void OptCPU (const char* Opt, const char* Arg)
186 /* Handle the --cpu option */
191 if (strcmp (Arg, "6502") == 0) {
193 } else if (strcmp (Arg, "65C02") == 0) {
195 } else if (strcmp (Arg, "65816") == 0) {
198 } else if (strcmp (Arg, "sunplus") == 0) {
199 SetCPU (CPU_SUNPLUS);
202 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
209 static void OptDebugInfo (const char* Opt, const char* Arg)
210 /* Add debug info to the object file */
217 static void OptHelp (const char* Opt, const char* Arg)
218 /* Print usage information and exit */
226 static void OptIgnoreCase (const char* Opt, const char* Arg)
227 /* Ignore case on symbols */
234 static void OptIncludeDir (const char* Opt, const char* Arg)
235 /* Add an include search path */
240 AddIncludePath (Arg);
245 static void OptListing (const char* Opt, const char* Arg)
246 /* Create a listing file */
253 static void OptPageLength (const char* Opt, const char* Arg)
254 /* Handle the --pagelength option */
261 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
262 fprintf (stderr, "Invalid page length: %d\n", Len);
270 static void OptSmart (const char* Opt, const char* Arg)
271 /* Handle the -s/--smart options */
278 static void OptVerbose (const char* Opt, const char* Arg)
279 /* Increase verbosity */
286 static void OptVersion (const char* Opt, const char* Arg)
287 /* Print the assembler version */
290 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
291 VER_MAJOR, VER_MINOR, VER_PATCH);
296 static void OneLine (void)
297 /* Assemble one line */
299 char Ident [MAX_STR_LEN+1];
302 /* Initialize the listing line */
305 if (Tok == TOK_COLON) {
306 /* An unnamed label */
311 /* Assemble the line */
312 if (Tok == TOK_IDENT) {
315 if (IsMacro (SVal)) {
317 /* Yes, start a macro expansion */
323 /* No, label. Remember the identifier, then skip it */
324 int HadWS = WS; /* Did we have whitespace before the ident? */
325 strcpy (Ident, SVal);
328 /* If a colon follows, this is a label definition. If there
329 * is no colon, it's an assignment.
334 /* Define the symbol with the expression following the '=' */
335 SymDef (Ident, Expression (), 0);
336 /* Don't allow anything after a symbol definition */
340 SymDef (Ident, CurrentPC (), IsZPSeg ());
341 /* Skip the colon. If NoColonLabels is enabled, allow labels
342 * without a colon if there is no whitespace before the
345 if (Tok != TOK_COLON) {
346 if (HadWS || !NoColonLabels) {
347 Error (ERR_COLON_EXPECTED);
349 if (Tok == TOK_NAMESPACE) {
350 /* Smart :: handling */
363 if (TokIsPseudo (Tok)) {
364 /* A control command, IVal is index into table */
366 } else if (Tok == TOK_MNEMO) {
367 /* A mnemonic - assemble one instruction */
368 HandleInstruction (IVal);
369 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
370 /* A macro expansion */
375 /* Calling InitListingLine again here is part of a hack that introduces
376 * enough magic to make the PC output in the listing work.
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;