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];
143 /* The symbol must start with a character or underline */
144 if (Def [0] != '_' && !IsAlpha (Def [0])) {
149 /* Copy the symbol, checking the rest */
151 while (IsAlNum (*P) || *P == '_') {
152 if (I <= MAX_STR_LEN) {
159 /* Do we have a value given? */
166 /* We have a value */
170 if (sscanf (P, "%lx", &Val) != 1) {
174 if (sscanf (P, "%li", &Val) != 1) {
180 /* Check if have already a symbol with this name */
181 if (SymIsDef (SymName, SCOPE_ANY)) {
182 AbEnd ("`%s' is already defined", SymName);
185 /* Define the symbol */
186 SymDef (SymName, GenLiteralExpr (Val), SYM_DEFAULT);
191 static void OptAutoImport (const char* Opt attribute ((unused)),
192 const char* Arg attribute ((unused)))
193 /* Mark unresolved symbols as imported */
200 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
201 /* Handle the --cpu option */
203 cpu_t CPU = FindCPU (Arg);
204 if (CPU == CPU_UNKNOWN) {
205 AbEnd ("Invalid CPU: `%s'", Arg);
213 static void OptDebugInfo (const char* Opt attribute ((unused)),
214 const char* Arg attribute ((unused)))
215 /* Add debug info to the object file */
222 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
223 /* Set an emulation feature */
225 /* Set the feature, check for errors */
226 if (SetFeature (Arg) == FEAT_UNKNOWN) {
227 AbEnd ("Illegal emulation feature: `%s'", Arg);
233 static void OptHelp (const char* Opt attribute ((unused)),
234 const char* Arg attribute ((unused)))
235 /* Print usage information and exit */
243 static void OptIgnoreCase (const char* Opt attribute ((unused)),
244 const char* Arg attribute ((unused)))
245 /* Ignore case on symbols */
252 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
253 /* Add an include search path */
255 AddIncludePath (Arg);
260 static void OptListing (const char* Opt attribute ((unused)),
261 const char* Arg attribute ((unused)))
262 /* Create a listing file */
269 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
270 /* Handle the --pagelength option */
272 int Len = atoi (Arg);
273 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
274 AbEnd ("Invalid page length: %d", Len);
281 static void OptSmart (const char* Opt attribute ((unused)),
282 const char* Arg attribute ((unused)))
283 /* Handle the -s/--smart options */
290 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
291 /* Set the target system */
293 /* Map the target name to a target id */
294 Target = FindTarget (Arg);
295 if (Target == TGT_UNKNOWN) {
296 AbEnd ("Invalid target name: `%s'", Arg);
297 } else if (Target == TGT_MODULE) {
298 AbEnd ("Cannot use `module' as a target for the assembler");
304 static void OptVerbose (const char* Opt attribute ((unused)),
305 const char* Arg attribute ((unused)))
306 /* Increase verbosity */
313 static void OptVersion (const char* Opt attribute ((unused)),
314 const char* Arg attribute ((unused)))
315 /* Print the assembler version */
318 "ca65 V%u.%u.%u - %s\n",
319 VER_MAJOR, VER_MINOR, VER_PATCH, Copyright);
324 static void DoPCAssign (void)
325 /* Start absolute code */
327 long PC = ConstExpression ();
328 if (PC < 0 || PC > 0xFFFFFF) {
337 static void OneLine (void)
338 /* Assemble one line */
340 char Ident [MAX_STR_LEN+1];
343 /* Initialize the new listing line if we are actually reading from file
344 * and not from internally pushed input.
346 if (!HavePushedInput ()) {
350 if (Tok == TOK_COLON) {
351 /* An unnamed label */
356 /* Assemble the line */
357 if (Tok == TOK_IDENT) {
360 if (IsMacro (SVal)) {
362 /* Yes, start a macro expansion */
368 /* No, label. Remember the identifier, then skip it */
369 int HadWS = WS; /* Did we have whitespace before the ident? */
370 strcpy (Ident, SVal);
373 /* If a colon follows, this is a label definition. If there
374 * is no colon, it's an assignment.
376 if (Tok == TOK_EQ || Tok == TOK_ASSIGN) {
377 /* If it's an assign token, we have a label */
378 unsigned Flags = (Tok == TOK_ASSIGN)? SYM_LABEL : SYM_DEFAULT;
381 /* Define the symbol with the expression following the '=' */
382 SymDef (Ident, Expression(), Flags);
383 /* Don't allow anything after a symbol definition */
386 /* Define the symbol flags */
387 unsigned Flags = IsZPSeg ()? SYM_ZP | SYM_LABEL : SYM_LABEL;
389 SymDef (Ident, GenCurrentPC (), Flags);
390 /* Skip the colon. If NoColonLabels is enabled, allow labels
391 * without a colon if there is no whitespace before the
394 if (Tok != TOK_COLON) {
395 if (HadWS || !NoColonLabels) {
396 Error (ERR_COLON_EXPECTED);
398 if (Tok == TOK_NAMESPACE) {
399 /* Smart :: handling */
412 if (TokIsPseudo (Tok)) {
413 /* A control command, IVal is index into table */
415 } else if (Tok == TOK_MNEMO) {
416 /* A mnemonic - assemble one instruction */
417 HandleInstruction (IVal);
418 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
419 /* A macro expansion */
421 } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
424 Error (ERR_EQ_EXPECTED);
427 /* Skip the equal sign */
429 /* Enter absolute mode */
435 /* Line separator must come here */
441 static void Assemble (void)
442 /* Start the ball rolling ... */
447 /* Assemble lines until end of file */
448 while (Tok != TOK_EOF) {
455 static void CreateObjFile (void)
456 /* Create the object file */
458 /* Open the object, write the header */
461 /* Write the object file options */
464 /* Write the list of input files */
467 /* Write the segment data to the file */
470 /* Write the import list */
473 /* Write the export list */
476 /* Write debug symbols if requested */
479 /* Write line infos if requested */
482 /* Write the string pool */
485 /* Write the assertions */
488 /* Write an updated header and close the file */
494 int main (int argc, char* argv [])
495 /* Assembler main program */
497 /* Program long options */
498 static const LongOpt OptTab[] = {
499 { "--auto-import", 0, OptAutoImport },
500 { "--cpu", 1, OptCPU },
501 { "--debug-info", 0, OptDebugInfo },
502 { "--feature", 1, OptFeature },
503 { "--help", 0, OptHelp },
504 { "--ignore-case", 0, OptIgnoreCase },
505 { "--include-dir", 1, OptIncludeDir },
506 { "--listing", 0, OptListing },
507 { "--pagelength", 1, OptPageLength },
508 { "--smart", 0, OptSmart },
509 { "--target", 1, OptTarget },
510 { "--verbose", 0, OptVerbose },
511 { "--version", 0, OptVersion },
516 /* Initialize the cmdline module */
517 InitCmdLine (&argc, &argv, "ca65");
519 /* Enter the base lexical level. We must do that here, since we may
520 * define symbols using -D.
524 /* Check the parameters */
526 while (I < ArgCount) {
528 /* Get the argument */
529 const char* Arg = ArgVec [I];
531 /* Check for an option */
532 if (Arg [0] == '-') {
536 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
540 OptDebugInfo (Arg, 0);
548 OptIgnoreCase (Arg, 0);
556 OutFile = GetArg (&I, 2);
564 OptTarget (Arg, GetArg (&I, 2));
572 DefineSymbol (GetArg (&I, 2));
576 OptIncludeDir (Arg, GetArg (&I, 2));
580 OptAutoImport (Arg, 0);
588 WarnLevel = atoi (GetArg (&I, 2));
597 /* Filename. Check if we already had one */
599 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
611 /* Do we have an input file? */
613 fprintf (stderr, "%s: No input files\n", ProgName);
617 /* If no CPU given, use the default CPU for the target */
618 if (GetCPU () == CPU_UNKNOWN) {
619 if (Target != TGT_UNKNOWN) {
620 SetCPU (DefaultCPU[Target]);
626 /* Intialize the target translation tables */
629 /* Initialize the scanner, open the input file */
630 InitScanner (InFile);
632 /* Define the default options */
635 /* Assemble the input */
638 /* If we didn't have any errors, check the segment stack */
639 if (ErrorCount == 0) {
643 /* If we didn't have any errors, check the unnamed labels */
644 if (ErrorCount == 0) {
648 /* If we didn't have any errors, check the symbol table */
649 if (ErrorCount == 0) {
653 /* If we didn't have any errors, check and resolve the segment data */
654 if (ErrorCount == 0) {
658 /* If we didn't have an errors, index the line infos */
659 MakeLineInfoIndex ();
662 if (Verbosity >= 2) {
667 /* If we didn't have any errors, create the object and listing files */
668 if (ErrorCount == 0) {
675 /* Close the input file */
678 /* Return an apropriate exit code */
679 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;