1 /*****************************************************************************/
5 /* Main program for the ca65 macroassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
75 /*****************************************************************************/
77 /*****************************************************************************/
81 static void Usage (void)
82 /* Print usage information and exit */
85 "Usage: %s [options] file\n"
87 " -D name[=value]\tDefine a symbol\n"
88 " -I dir\t\tSet an include directory search path\n"
89 " -U\t\t\tMark unresolved symbols as import\n"
90 " -V\t\t\tPrint the assembler version\n"
91 " -W n\t\t\tSet warning level n\n"
92 " -g\t\t\tAdd debug info to object file\n"
93 " -h\t\t\tHelp (this text)\n"
94 " -i\t\t\tIgnore case of symbols\n"
95 " -l\t\t\tCreate a listing if assembly was ok\n"
96 " -o name\t\tName the output file\n"
97 " -s\t\t\tEnable smart mode\n"
98 " -t sys\t\tSet the target system\n"
99 " -v\t\t\tIncrease verbosity\n"
102 " --auto-import\t\tMark unresolved symbols as import\n"
103 " --cpu type\t\tSet cpu type\n"
104 " --debug-info\t\tAdd debug info to object file\n"
105 " --feature name\tSet an emulation feature\n"
106 " --help\t\tHelp (this text)\n"
107 " --ignore-case\t\tIgnore case of symbols\n"
108 " --include-dir dir\tSet an include directory search path\n"
109 " --listing\t\tCreate a listing if assembly was ok\n"
110 " --pagelength n\tSet the page length for the listing\n"
111 " --smart\t\tEnable smart mode\n"
112 " --target sys\t\tSet the target system\n"
113 " --verbose\t\tIncrease verbosity\n"
114 " --version\t\tPrint the assembler version\n",
120 static void SetOptions (void)
121 /* Set the option for the translator */
125 /* Set the translator */
126 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
129 /* Set date and time */
130 OptDateTime ((unsigned long) time(0));
135 static void DefineSymbol (const char* Def)
136 /* Define a symbol from the command line */
141 char SymName [MAX_STR_LEN+1];
145 /* The symbol must start with a character or underline */
146 if (Def [0] != '_' && !IsAlpha (Def [0])) {
151 /* Copy the symbol, checking the rest */
153 while (IsAlNum (*P) || *P == '_') {
154 if (I <= MAX_STR_LEN) {
161 /* Do we have a value given? */
168 /* We have a value */
172 if (sscanf (P, "%lx", &Val) != 1) {
176 if (sscanf (P, "%li", &Val) != 1) {
182 /* Search for the symbol, allocate a new one if it doesn't exist */
183 Sym = SymFind (CurrentScope, SymName, SYM_ALLOC_NEW);
185 /* Check if have already a symbol with this name */
186 if (SymIsDef (Sym)) {
187 AbEnd ("`%s' is already defined", SymName);
190 /* Mark the symbol as defined */
191 SymDef (Sym, GenLiteralExpr (Val), SYM_DEFAULT);
196 static void OptAutoImport (const char* Opt attribute ((unused)),
197 const char* Arg attribute ((unused)))
198 /* Mark unresolved symbols as imported */
205 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
206 /* Handle the --cpu option */
208 cpu_t CPU = FindCPU (Arg);
209 if (CPU == CPU_UNKNOWN) {
210 AbEnd ("Invalid CPU: `%s'", Arg);
218 static void OptDebugInfo (const char* Opt attribute ((unused)),
219 const char* Arg attribute ((unused)))
220 /* Add debug info to the object file */
227 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
228 /* Set an emulation feature */
230 /* Set the feature, check for errors */
231 if (SetFeature (Arg) == FEAT_UNKNOWN) {
232 AbEnd ("Illegal emulation feature: `%s'", Arg);
238 static void OptHelp (const char* Opt attribute ((unused)),
239 const char* Arg attribute ((unused)))
240 /* Print usage information and exit */
248 static void OptIgnoreCase (const char* Opt attribute ((unused)),
249 const char* Arg attribute ((unused)))
250 /* Ignore case on symbols */
257 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
258 /* Add an include search path */
260 AddIncludePath (Arg);
265 static void OptListing (const char* Opt attribute ((unused)),
266 const char* Arg attribute ((unused)))
267 /* Create a listing file */
274 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
275 /* Handle the --pagelength option */
277 int Len = atoi (Arg);
278 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
279 AbEnd ("Invalid page length: %d", Len);
286 static void OptSmart (const char* Opt attribute ((unused)),
287 const char* Arg attribute ((unused)))
288 /* Handle the -s/--smart options */
295 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
296 /* Set the target system */
298 /* Map the target name to a target id */
299 Target = FindTarget (Arg);
300 if (Target == TGT_UNKNOWN) {
301 AbEnd ("Invalid target name: `%s'", Arg);
302 } else if (Target == TGT_MODULE) {
303 AbEnd ("Cannot use `module' as a target for the assembler");
309 static void OptVerbose (const char* Opt attribute ((unused)),
310 const char* Arg attribute ((unused)))
311 /* Increase verbosity */
318 static void OptVersion (const char* Opt attribute ((unused)),
319 const char* Arg attribute ((unused)))
320 /* Print the assembler version */
323 "ca65 V%u.%u.%u - %s\n",
324 VER_MAJOR, VER_MINOR, VER_PATCH, Copyright);
329 static void DoPCAssign (void)
330 /* Start absolute code */
332 long PC = ConstExpression ();
333 if (PC < 0 || PC > 0xFFFFFF) {
342 static void OneLine (void)
343 /* Assemble one line */
347 /* Initialize the new listing line if we are actually reading from file
348 * and not from internally pushed input.
350 if (!HavePushedInput ()) {
354 if (Tok == TOK_COLON) {
355 /* An unnamed label */
360 /* Assemble the line */
361 if (Tok == TOK_IDENT) {
364 if (IsMacro (SVal)) {
366 /* Yes, start a macro expansion */
372 /* No, label. Remember the identifier, then skip it */
373 int HadWS = WS; /* Did we have whitespace before the ident? */
375 /* Generate the symbol table entry, then skip the name */
376 SymEntry* Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
379 /* If a colon follows, this is a label definition. If there
380 * is no colon, it's an assignment.
382 if (Tok == TOK_EQ || Tok == TOK_ASSIGN) {
383 /* If it's an assign token, we have a label */
384 unsigned Flags = (Tok == TOK_ASSIGN)? SYM_LABEL : SYM_DEFAULT;
387 /* Define the symbol with the expression following the '=' */
388 SymDef (Sym, Expression(), Flags);
389 /* Don't allow anything after a symbol definition */
392 /* Define the symbol flags */
393 unsigned Flags = IsZPSeg ()? SYM_ZP | SYM_LABEL : SYM_LABEL;
395 SymDef (Sym, GenCurrentPC (), Flags);
396 /* Skip the colon. If NoColonLabels is enabled, allow labels
397 * without a colon if there is no whitespace before the
400 if (Tok != TOK_COLON) {
401 if (HadWS || !NoColonLabels) {
402 Error (ERR_COLON_EXPECTED);
404 if (Tok == TOK_NAMESPACE) {
405 /* Smart :: handling */
418 if (TokIsPseudo (Tok)) {
419 /* A control command, IVal is index into table */
421 } else if (Tok == TOK_MNEMO) {
422 /* A mnemonic - assemble one instruction */
423 HandleInstruction (IVal);
424 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
425 /* A macro expansion */
427 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
430 Error (ERR_EQ_EXPECTED);
433 /* Skip the equal sign */
435 /* Enter absolute mode */
441 /* Line separator must come here */
447 static void Assemble (void)
448 /* Start the ball rolling ... */
453 /* Assemble lines until end of file */
454 while (Tok != TOK_EOF) {
461 static void CreateObjFile (void)
462 /* Create the object file */
464 /* Open the object, write the header */
467 /* Write the object file options */
470 /* Write the list of input files */
473 /* Write the segment data to the file */
476 /* Write the import list */
479 /* Write the export list */
482 /* Write debug symbols if requested */
485 /* Write line infos if requested */
488 /* Write the string pool */
491 /* Write the assertions */
494 /* Write an updated header and close the file */
500 int main (int argc, char* argv [])
501 /* Assembler main program */
503 /* Program long options */
504 static const LongOpt OptTab[] = {
505 { "--auto-import", 0, OptAutoImport },
506 { "--cpu", 1, OptCPU },
507 { "--debug-info", 0, OptDebugInfo },
508 { "--feature", 1, OptFeature },
509 { "--help", 0, OptHelp },
510 { "--ignore-case", 0, OptIgnoreCase },
511 { "--include-dir", 1, OptIncludeDir },
512 { "--listing", 0, OptListing },
513 { "--pagelength", 1, OptPageLength },
514 { "--smart", 0, OptSmart },
515 { "--target", 1, OptTarget },
516 { "--verbose", 0, OptVerbose },
517 { "--version", 0, OptVersion },
522 /* Initialize the cmdline module */
523 InitCmdLine (&argc, &argv, "ca65");
525 /* Enter the base lexical level. We must do that here, since we may
526 * define symbols using -D.
530 /* Check the parameters */
532 while (I < ArgCount) {
534 /* Get the argument */
535 const char* Arg = ArgVec [I];
537 /* Check for an option */
538 if (Arg [0] == '-') {
542 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
546 OptDebugInfo (Arg, 0);
554 OptIgnoreCase (Arg, 0);
562 OutFile = GetArg (&I, 2);
570 OptTarget (Arg, GetArg (&I, 2));
578 DefineSymbol (GetArg (&I, 2));
582 OptIncludeDir (Arg, GetArg (&I, 2));
586 OptAutoImport (Arg, 0);
594 WarnLevel = atoi (GetArg (&I, 2));
603 /* Filename. Check if we already had one */
605 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
617 /* Do we have an input file? */
619 fprintf (stderr, "%s: No input files\n", ProgName);
623 /* If no CPU given, use the default CPU for the target */
624 if (GetCPU () == CPU_UNKNOWN) {
625 if (Target != TGT_UNKNOWN) {
626 SetCPU (DefaultCPU[Target]);
632 /* Intialize the target translation tables */
635 /* Initialize the scanner, open the input file */
636 InitScanner (InFile);
638 /* Define the default options */
641 /* Assemble the input */
644 /* If we didn't have any errors, check the segment stack */
645 if (ErrorCount == 0) {
649 /* If we didn't have any errors, check the unnamed labels */
650 if (ErrorCount == 0) {
654 /* If we didn't have any errors, check the symbol table */
655 if (ErrorCount == 0) {
659 /* If we didn't have any errors, check and resolve the segment data */
660 if (ErrorCount == 0) {
664 /* If we didn't have an errors, index the line infos */
665 MakeLineInfoIndex ();
668 if (Verbosity >= 2) {
673 /* If we didn't have any errors, create the object and listing files */
674 if (ErrorCount == 0) {
681 /* Close the input file */
684 /* Return an apropriate exit code */
685 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;