2 * $Id: xasm.c,v 1.22 2007/11/11 22:35:22 khansen Exp $
4 * Revision 1.22 2007/11/11 22:35:22 khansen
7 * Revision 1.21 2007/08/19 11:18:56 khansen
8 * --case-insensitive option
10 * Revision 1.20 2007/08/12 18:58:12 khansen
11 * ability to generate pure 6502 binary (--pure-binary switch)
13 * Revision 1.19 2007/08/11 01:24:36 khansen
14 * includepaths support (-I option)
16 * Revision 1.18 2007/08/10 20:21:02 khansen
17 * *** empty log message ***
19 * Revision 1.17 2007/08/07 22:42:53 khansen
22 * Revision 1.16 2007/07/22 14:49:40 khansen
23 * don't crash in change_extension()
25 * Revision 1.15 2007/07/22 13:33:26 khansen
26 * convert tabs to whitespaces
28 * Revision 1.14 2005/01/09 11:19:23 kenth
31 * Revision 1.13 2005/01/05 09:37:32 kenth
34 * Revision 1.12 2005/01/05 01:52:13 kenth
37 * Revision 1.11 2005/01/04 21:35:10 kenth
38 * return error code from main() when error count > 0
40 * Revision 1.10 2004/12/29 21:43:50 kenth
43 * Revision 1.9 2004/12/25 02:23:19 kenth
46 * Revision 1.8 2004/12/19 19:58:46 kenth
49 * Revision 1.7 2004/12/18 17:01:21 kenth
50 * --debug switch, multiple verbose levels
52 * Revision 1.6 2004/12/16 13:20:35 kenth
55 * Revision 1.5 2004/12/14 01:50:12 kenth
58 * Revision 1.4 2004/12/11 02:06:27 kenth
61 * Revision 1.3 2004/12/06 04:53:02 kenth
64 * Revision 1.2 2004/06/30 23:37:54 kenth
65 * replaced argp with something else
67 * Revision 1.1 2004/06/30 07:56:02 kenth
73 * (C) 2004 Kent Hansen
75 * The XORcyst is free software; you can redistribute it and/or modify
76 * it under the terms of the GNU General Public License as published by
77 * the Free Software Foundation; either version 2 of the License, or
78 * (at your option) any later version.
80 * The XORcyst is distributed in the hope that it will be useful,
81 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83 * GNU General Public License for more details.
85 * You should have received a copy of the GNU General Public License
86 * along with The XORcyst; if not, write to the Free Software
87 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
105 /*---------------------------------------------------------------------------*/
107 /* Parser stuff we need. */
112 /* Scanner stuff we need. */
113 int yybegin(const char *, int, int);
117 static symtab
*symbol_table
;
120 /*---------------------------------------------------------------------------*/
121 /* Argument parsing stuff. */
123 static char program_version
[] = "xasm 1.5.2";
125 /* Argument variables set by arg parser. */
126 xasm_arguments xasm_args
;
128 /* Long options for getopt_long(). */
129 static struct option long_options
[] = {
130 { "define", required_argument
, 0, 'D' },
131 { "include-path", required_argument
, 0, 'I' },
132 { "output", required_argument
, 0, 'o' },
133 { "quiet", no_argument
, 0, 'q' },
134 { "silent", no_argument
, 0, 's' },
135 { "verbose", no_argument
, 0, 'v' },
136 { "debug", no_argument
, 0, 'g' },
137 { "help", no_argument
, 0, 0 },
138 { "usage", no_argument
, 0, 0 },
139 { "version", no_argument
, 0, 'V' },
140 { "swap-parens", no_argument
, 0, 0 },
141 { "pure-binary", no_argument
, 0, 0 },
142 { "case-insensitive", no_argument
, 0, 0 },
143 { "no-warn", no_argument
, 0, 0 },
147 /* Prints usage message and exits. */
151 Usage: xasm [-gqsvV] [-D IDENT[=VALUE]] [--define=IDENT]\n\
152 [-o FILE] [--output=FILE] [--pure-binary]\n\
153 [--include-path=DIR] [-I DIR] [--swap-parens]\n\
154 [--case-insensitive]\n\
155 [--no-warn] [--verbose] [--quiet] [--silent] \n\
156 [--debug] [--help] [--usage] [--version]\n\
162 /* Prints help message and exits. */
166 Usage: xasm [OPTION...] FILE\n\
167 The XORcyst Assembler -- it kicks the 6502's ass\n\
169 -D, --define=IDENT[=VALUE] Define IDENT\n\
170 -I, --include-path=DIR Specify a search path for include files\n\
171 -o, --output=FILE Output to FILE instead of standard output\n\
172 --pure-binary Output pure 6502 binary\n\
173 --swap-parens Use ( ) instead of [ ] for indirection\n\
174 --case-insensitive Case-insensitive identifiers\n\
175 --no-warn Suppress warnings\n\
176 -q, -s, --quiet, --silent Don't produce any output\n\
177 -v, --verbose Produce verbose output\n\
178 -g, --debug Retain file locations\n\
179 --help Give this help list\n\
180 --usage Give a short usage message\n\
181 -V, --version Print program version\n\
183 Mandatory or optional arguments to long options are also mandatory or optional\n\
184 for any corresponding short options.\n\
186 Report bugs to <kentmhan@gmail.com>.\n\
191 /* Prints version and exits. */
192 static void version()
194 printf("%s\n", program_version
);
199 * Checks if a character is alpha (a-z, A-Z).
201 static int __isalpha(char c
)
203 return ( ((c
>= 'a') && (c
<= 'z')) || ((c
>= 'A') && (c
<= 'Z')) );
207 * Checks if a character is alpha (a-z, A-Z) or numeric (0-9).
209 static int __isalnum(char c
)
211 return ( __isalpha(c
) || ((c
>= '0') && (c
<= '9')) );
215 * Checks that an identifier matches the regexp [a-zA-Z_][a-zA-Z0-9_]*
216 * @param id Identifier to validate
217 * @return 1 if OK, 0 otherwise
219 static int validate_ident(char *id
)
224 if ( !__isalpha(c
) && (c
!= '_') ) {
227 for (i
=1; i
<strlen(id
); i
++) {
229 if ( !__isalnum(c
) && (c
!= '_') ) {
236 /* Parses program arguments. */
238 parse_arguments (int argc
, char **argv
)
241 /* getopt_long stores the option index here. */
244 /* Set default values. */
246 xasm_args
.silent
= 0;
247 xasm_args
.verbose
= 0;
248 xasm_args
.swap_parens
= 0;
249 xasm_args
.pure_binary
= 0;
250 xasm_args
.case_insensitive
= 0;
251 xasm_args
.input_file
= NULL
;
252 xasm_args
.output_file
= NULL
;
253 xasm_args
.include_paths
= NULL
;
254 xasm_args
.include_path_count
= 0;
257 while ((key
= getopt_long(argc
, argv
, "D:I:o:qsvV", long_options
, &index
)) != -1) {
264 xasm_args
.silent
= 1;
272 xasm_args
.output_file
= optarg
;
279 static location loc
= { 0, 0, 0, 0, NULL
};
280 if (strchr(optarg
, '=') != NULL
) {
282 id
= strtok(optarg
, "=");
283 str
= strtok(NULL
, "\0");
285 /* Parse the value */
286 if (str
[0] == '\"') {
288 str
= strtok(&str
[1], "\"");
289 val
= astnode_create_string(str
, loc
);
292 val
= astnode_create_integer(strtol(str
, NULL
, 0), loc
);
295 /* No value given -- use empty string */
296 val
= astnode_create_string("", loc
);
300 val
= astnode_create_integer(0, loc
);
302 if (validate_ident(id
)) {
304 e
= symtab_lookup(id
);
306 symtab_enter(id
, CONSTANT_SYMBOL
, val
, 0);
308 /* Error, redefinition */
309 fprintf(stderr
, "--ident: `%s' already defined\n", id
);
312 /* Error, bad identifier */
313 fprintf(stderr
, "--ident: `%s' is not a valid identifier\n", id
);
320 int count
= xasm_args
.include_path_count
+ 1;
321 xasm_args
.include_paths
= (char **)realloc(
322 xasm_args
.include_paths
, sizeof(const char *) * count
);
323 p
= (char *)malloc(strlen(optarg
) + 1);
325 xasm_args
.include_paths
[count
-1] = p
;
326 xasm_args
.include_path_count
= count
;
331 /* Use index to differentiate between options */
332 if (strcmp(long_options
[index
].name
, "usage") == 0) {
334 } else if (strcmp(long_options
[index
].name
, "help") == 0) {
336 } else if (strcmp(long_options
[index
].name
, "swap-parens") == 0) {
337 xasm_args
.swap_parens
= 1;
338 } else if (strcmp(long_options
[index
].name
, "pure-binary") == 0) {
339 xasm_args
.pure_binary
= 1;
340 } else if (strcmp(long_options
[index
].name
, "case-insensitive") == 0) {
341 xasm_args
.case_insensitive
= 1;
342 } else if (strcmp(long_options
[index
].name
, "no-warn") == 0) {
343 xasm_args
.no_warn
= 1;
352 /* Error message has been printed by getopt_long */
357 /* Forgot to handle a short option, most likely */
358 fprintf(stderr
, "internal error: unhandled option `%c'\n", key
);
364 /* Must be one additional argument, which is the input file. */
365 if (argc
-1 != optind
) {
366 printf("Usage: xasm [OPTION...] FILE\nTry `xasm --help' or `xasm --usage' for more information.\n");
370 xasm_args
.input_file
= argv
[optind
];
374 /*---------------------------------------------------------------------------*/
377 * Changes the extension of a filename.
378 * @param infile Filename whose extension to change
379 * @param ext New extension
380 * @param outfile Destination filename
382 static void change_extension(const char *infile
, const char *ext
, char *outfile
)
385 /* Find the last dot. */
386 p
= strrchr(infile
, '.');
388 /* There is no dot, simply concatenate extension. */
389 sprintf(outfile
, "%s.%s", infile
, ext
);
392 /* Copy the name up to and including the last dot */
393 strncpy(outfile
, infile
, p
- infile
+ 1);
394 outfile
[p
- infile
+ 1] = '\0';
395 /* Then concatenate the extension. */
396 strcat(outfile
, ext
);
400 /*---------------------------------------------------------------------------*/
403 * Prints message only if --verbose option was given to assembler.
405 static void verbose(const char *s
)
407 if (xasm_args
.verbose
) {
413 * Gets total number of errors (parsing + semantics).
415 static int total_errors()
417 return yynerrs
+ astproc_err_count();
421 * Program entrypoint.
423 int main(int argc
, char *argv
[]) {
425 char *default_outfile
= 0;
427 /* Working directory is needed for include statements */
428 xasm_path
= getcwd(NULL
, 0);
430 /* Create global symbol table (auto-pushed on stack) */
431 symbol_table
= symtab_create();
433 /* Parse our arguments. */
434 parse_arguments (argc
, argv
);
436 /* Open input for scanning */
437 if (!yybegin(xasm_args
.input_file
,
438 xasm_args
.swap_parens
,
439 xasm_args
.case_insensitive
)) {
440 fprintf(stderr
, "error: could not open `%s' for reading\n", xasm_args
.input_file
);
441 symtab_finalize(symbol_table
);
445 /* Parse it into a syntax tree */
447 verbose("Parsing input...");
450 if (root_node
== NULL
) {
451 symtab_finalize(symbol_table
);
455 /* First pass does a lot of stuff. */
456 verbose("First pass...");
457 astproc_first_pass(root_node
);
459 /* Second pass does more stuff. */
460 verbose("Second pass...");
461 astproc_second_pass(root_node
);
463 /* Third pass is fun. */
464 verbose("Third pass...");
465 astproc_third_pass(root_node
);
467 if (xasm_args
.pure_binary
) {
468 /* Do another pass to prepare for writing pure 6502 */
469 verbose("Fourth pass...");
470 astproc_fourth_pass(root_node
);
473 /* Print the final AST (debugging) */
474 // astnode_print(root_node, 0);
476 /* If no errors, proceed with code generation. */
477 if (total_errors() == 0) {
478 if (xasm_args
.output_file
== NULL
) {
479 /* Create default name of output */
480 const char *default_ext
= "o";
481 int default_outfile_len
= strlen(xasm_args
.input_file
)
482 + /*dot*/1 + strlen(default_ext
) + 1;
483 default_outfile
= (char *)malloc(default_outfile_len
);
484 change_extension(xasm_args
.input_file
, default_ext
, default_outfile
);
485 xasm_args
.output_file
= default_outfile
;
487 /* Attempt to open file for writing */
488 output_fp
= fopen(xasm_args
.output_file
, "wb");
489 if (output_fp
== NULL
) {
490 fprintf(stderr
, "error: could not open `%s' for writing\n", xasm_args
.output_file
);
492 verbose("Generating final output...");
493 if (xasm_args
.pure_binary
) {
494 astproc_fifth_pass(root_node
, output_fp
);
496 codegen_write(root_node
, output_fp
);
503 verbose("cleaning up...");
505 symtab_finalize(symbol_table
);
506 astnode_finalize(root_node
);
509 free(default_outfile
);
511 if (xasm_args
.include_path_count
> 0) {
513 for (i
= 0; i
< xasm_args
.include_path_count
; ++i
)
514 free(xasm_args
.include_paths
[i
]);
515 free(xasm_args
.include_paths
);
520 return (total_errors() == 0) ? 0 : 1;