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/version.h"
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static void Usage (void)
68 /* Print usage information and exit */
71 "Usage: %s [options] file\n"
73 "\t-g\t\tAdd debug info to object file\n"
74 "\t-i\t\tIgnore case of symbols\n"
75 "\t-l\t\tCreate a listing if assembly was ok\n"
76 "\t-o name\t\tName the output file\n"
77 "\t-s\t\tEnable smart mode\n"
78 "\t-v\t\tIncrease verbosity\n"
79 "\t-D name[=value]\tDefine a symbol\n"
80 "\t-U\t\tMark unresolved symbols as import\n"
81 "\t-V\t\tPrint the assembler version\n"
82 "\t-W n\t\tSet warning level n\n"
83 "\t--cpu type\tSet cpu type\n"
84 "\t--pagelength n\tSet the page length for the listing\n"
85 "\t--smart\t\tEnable smart mode\n",
92 static void UnknownOption (const char* Arg)
93 /* Print an error about an unknown option. Print usage information and exit */
95 fprintf (stderr, "Unknown option: %s\n", Arg);
101 static void NeedArg (const char* Arg)
102 /* Print an error about a missing option argument and exit. */
104 fprintf (stderr, "Option requires an argument: %s\n", Arg);
110 static void InvSym (const char* Def)
111 /* Print an error about an invalid symbol definition and die */
113 fprintf (stderr, "Invalid symbol definition: `%s'\n", Def);
119 static const char* GetArg (int* ArgNum, char* argv [], unsigned Len)
120 /* Get an option argument */
122 const char* Arg = argv [*ArgNum];
123 if (Arg [Len] != '\0') {
124 /* Argument appended */
127 /* Separate argument */
128 Arg = argv [*ArgNum + 1];
130 /* End of arguments */
131 NeedArg (argv [*ArgNum]);
140 static void SetOptions (void)
141 /* Set the option for the translator */
145 /* Set the translator */
146 sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
149 /* Set date and time */
150 OptDateTime ((unsigned long) time(0));
155 static void DefineSymbol (const char* Def)
156 /* Define a symbol from the command line */
161 char SymName [MAX_STR_LEN+1];
163 /* The symbol must start with a character or underline */
164 if (Def [0] != '_' && !isalpha (Def [0])) {
169 /* Copy the symbol, checking the rest */
171 while (isalnum (*P) || *P == '_') {
172 if (I <= MAX_STR_LEN) {
179 /* Do we have a value given? */
186 /* We have a value */
190 if (sscanf (P, "%lx", &Val) != 1) {
194 if (sscanf (P, "%li", &Val) != 1) {
200 /* Check if have already a symbol with this name */
201 if (SymIsDef (SymName)) {
202 fprintf (stderr, "`%s' is already defined\n", SymName);
206 /* Define the symbol */
207 SymDef (SymName, LiteralExpr (Val), 0);
212 static void OptCPU (const char* Opt, const char* Arg)
213 /* Handle the --cpu option */
218 if (strcmp (Arg, "6502") == 0) {
220 } else if (strcmp (Arg, "65C02") == 0) {
222 } else if (strcmp (Arg, "65816") == 0) {
225 } else if (strcmp (Arg, "sunplus") == 0) {
226 SetCPU (CPU_SUNPLUS);
229 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
236 static void OptPageLength (const char* Opt, const char* Arg)
237 /* Handle the --pagelength option */
244 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
245 fprintf (stderr, "Invalid page length: %d\n", Len);
253 static void OptSmart (const char* Opt)
254 /* Handle the -s/--smart options */
261 static void LongOption (int* ArgNum, char* argv [])
262 /* Handle a long command line option */
264 const char* Opt = argv [*ArgNum];
265 const char* Arg = argv [*ArgNum+1];
267 if (strcmp (Opt, "--cpu") == 0) {
270 } else if (strcmp (Opt, "--pagelength") == 0) {
271 OptPageLength (Opt, Arg);
273 } else if (strcmp (Opt, "--smart") == 0) {
282 static void OneLine (void)
283 /* Assemble one line */
285 char Ident [MAX_STR_LEN+1];
288 /* Initialize the listing line */
291 if (Tok == TOK_COLON) {
292 /* An unnamed label */
297 /* Assemble the line */
298 if (Tok == TOK_IDENT) {
301 if (IsMacro (SVal)) {
303 /* Yes, start a macro expansion */
309 /* No, label. Remember the identifier, then skip it */
310 int HadWS = WS; /* Did we have whitespace before the ident? */
311 strcpy (Ident, SVal);
314 /* If a colon follows, this is a label definition. If there
315 * is no colon, it's an assignment.
320 /* Define the symbol with the expression following the
323 SymDef (Ident, Expression (), 0);
324 /* Don't allow anything after a symbol definition */
328 SymDef (Ident, CurrentPC (), IsZPSeg ());
329 /* Skip the colon. If NoColonLabels is enabled, allow labels
330 * without a colon if there is no whitespace before the
333 if (Tok != TOK_COLON) {
334 if (HadWS || !NoColonLabels) {
335 Error (ERR_COLON_EXPECTED);
337 if (Tok == TOK_NAMESPACE) {
338 /* Smart :: handling */
351 if (TokIsPseudo (Tok)) {
352 /* A control command, IVal is index into table */
354 } else if (Tok == TOK_MNEMO) {
355 /* A mnemonic - assemble one instruction */
356 HandleInstruction (IVal);
357 } else if (Tok == TOK_IDENT && IsMacro (SVal)) {
358 /* A macro expansion */
363 /* Calling InitListingLine again here is part of a hack that introduces
364 * enough magic to make the PC output in the listing work.
368 /* Line separator must come here */
374 static void Assemble (void)
375 /* Start the ball rolling ... */
380 /* Assemble lines until end of file */
381 while (Tok != TOK_EOF) {
388 static void CreateObjFile (void)
389 /* Create the object file */
391 /* Open the object, write the header */
394 /* Write the object file options */
397 /* Write the list of input files */
400 /* Write the segment data to the file */
403 /* Write the import list */
406 /* Write the export list */
409 /* Write debug symbols if requested */
412 /* Write an updated header and close the file */
418 int main (int argc, char* argv [])
419 /* Assembler main program */
423 /* Set the program name */
426 /* We must have a file name */
431 /* Enter the base lexical level. We must do that here, since we may
432 * define symbols using -D.
436 /* Check the parameters */
440 /* Get the argument */
441 const char* Arg = argv [I];
443 /* Check for an option */
444 if (Arg [0] == '-') {
448 LongOption (&I, argv);
464 OutFile = GetArg (&I, argv, 2);
476 DefineSymbol (GetArg (&I, argv, 2));
485 "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
486 VER_MAJOR, VER_MINOR, VER_PATCH);
490 WarnLevel = atoi (GetArg (&I, argv, 2));
499 /* Filename. Check if we already had one */
501 fprintf (stderr, "Don't know what to do with `%s'\n", Arg);
512 /* Do we have an input file? */
514 fprintf (stderr, "No input file\n");
518 /* Initialize the scanner, open the input file */
519 InitScanner (InFile);
521 /* Define the default options */
524 /* Assemble the input */
527 /* If we didn't have any errors, check the unnamed labels */
528 if (ErrorCount == 0) {
532 /* If we didn't have any errors, check the symbol table */
533 if (ErrorCount == 0) {
537 /* If we didn't have any errors, check and resolve the segment data */
538 if (ErrorCount == 0) {
548 /* If we didn't have any errors, create the object and listing files */
549 if (ErrorCount == 0) {
556 /* Close the input file */
559 /* Return an apropriate exit code */
560 return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;