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 " -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\tPrint 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",
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 */
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
337 SymDef (Ident, Expression (), 0);
338 /* Don't allow anything after a symbol definition */
342 SymDef (Ident, CurrentPC (), IsZPSeg ());
343 /* Skip the colon. If NoColonLabels is enabled, allow labels
344 * without a colon if there is no whitespace before the
347 if (Tok != TOK_COLON) {
348 if (HadWS || !NoColonLabels) {
349 Error (ERR_COLON_EXPECTED);
351 if (Tok == TOK_NAMESPACE) {
352 /* Smart :: handling */
365 if (TokIsPseudo (Tok)) {
366 /* A control command, IVal is index into table */
368 } else if (Tok == TOK_MNEMO) {
369 /* A mnemonic - assemble one instruction */
370 HandleInstruction (IVal);
371 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
372 /* A macro expansion */
377 /* Calling InitListingLine again here is part of a hack that introduces
378 * enough magic to make the PC output in the listing work.
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);
455 /* Set the program name */
458 /* We must have a file name */
463 /* Enter the base lexical level. We must do that here, since we may
464 * define symbols using -D.
468 /* Check the parameters */
472 /* Get the argument */
473 const char* Arg = argv [I];
475 /* Check for an option */
476 if (Arg [0] == '-') {
480 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
484 OptDebugInfo (Arg, 0);
488 OptIgnoreCase (Arg, 0);
496 OutFile = GetArg (&I, 2);
508 DefineSymbol (GetArg (&I, 2));
512 OptIncludeDir (Arg, GetArg (&I, 2));
516 OptAutoImport (Arg, 0);
524 WarnLevel = atoi (GetArg (&I, 2));
533 /* Filename. Check if we already had one */
535 fprintf (stderr, "Don't know what to do with `%s'\n", Arg);
546 /* Do we have an input file? */
548 fprintf (stderr, "No input file\n");
552 /* Initialize the scanner, open the input file */
553 InitScanner (InFile);
555 /* Define the default options */
558 /* Assemble the input */
561 /* If we didn't have any errors, check the unnamed labels */
562 if (ErrorCount == 0) {
566 /* If we didn't have any errors, check the symbol table */
567 if (ErrorCount == 0) {
571 /* If we didn't have any errors, check and resolve the segment data */
572 if (ErrorCount == 0) {
582 /* If we didn't have any errors, create the object and listing files */
583 if (ErrorCount == 0) {
590 /* Close the input file */
593 /* Return an apropriate exit code */
594 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;