move operator-to-string conversion to unit.c
[xorcyst.git] / xasm.c
bloba1daf8e97255de3c9c02a6d28200941d72398a84
1 /*
2 * $Id: xasm.c,v 1.22 2007/11/11 22:35:22 khansen Exp $
3 * $Log: xasm.c,v $
4 * Revision 1.22 2007/11/11 22:35:22 khansen
5 * compile on mac
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
20 * version
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
29 * xorcyst 1.4.5
31 * Revision 1.13 2005/01/05 09:37:32 kenth
32 * xorcyst 1.4.4
34 * Revision 1.12 2005/01/05 01:52:13 kenth
35 * xorcyst 1.4.3
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
41 * xorcyst 1.4.2
43 * Revision 1.9 2004/12/25 02:23:19 kenth
44 * xorcyst 1.4.1
46 * Revision 1.8 2004/12/19 19:58:46 kenth
47 * xorcyst 1.4.0
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
53 * xorcyst 1.3.5
55 * Revision 1.5 2004/12/14 01:50:12 kenth
56 * xorcyst 1.3.0
58 * Revision 1.4 2004/12/11 02:06:27 kenth
59 * xorcyst 1.2.0
61 * Revision 1.3 2004/12/06 04:53:02 kenth
62 * xorcyst 1.1.0
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
68 * Initial revision
72 /**
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
90 /**
91 * The main program.
94 #include <stdlib.h>
95 #include <stdio.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include "getopt.h"
99 #include "astnode.h"
100 #include "astproc.h"
101 #include "symtab.h"
102 #include "codegen.h"
103 #include "xasm.h"
105 /*---------------------------------------------------------------------------*/
107 /* Parser stuff we need. */
108 int yyparse(void);
109 extern int yydebug;
110 extern int yynerrs;
112 /* Scanner stuff we need. */
113 int yybegin(const char *, int, int);
115 /* Other. */
116 astnode *root_node;
117 static symtab *symbol_table;
118 char *xasm_path;
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 },
144 { 0 }
147 /* Prints usage message and exits. */
148 static void usage()
150 printf("\
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\
157 FILE\n\
159 exit(0);
162 /* Prints help message and exits. */
163 static void help()
165 printf("\
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\
188 exit(0);
191 /* Prints version and exits. */
192 static void version()
194 printf("%s\n", program_version);
195 exit(0);
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)
221 int i;
222 char c;
223 c = id[0];
224 if ( !__isalpha(c) && (c != '_') ) {
225 return 0;
227 for (i=1; i<strlen(id); i++) {
228 c = id[i];
229 if ( !__isalnum(c) && (c != '_') ) {
230 return 0;
233 return 1; /* OK */
236 /* Parses program arguments. */
237 static void
238 parse_arguments (int argc, char **argv)
240 int key;
241 /* getopt_long stores the option index here. */
242 int index = 0;
244 /* Set default values. */
245 xasm_args.debug = 0;
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;
256 /* Parse options. */
257 while ((key = getopt_long(argc, argv, "D:I:o:qsvV", long_options, &index)) != -1) {
258 switch (key) {
259 case 'g':
260 xasm_args.debug = 1;
261 break;
263 case 'q': case 's':
264 xasm_args.silent = 1;
265 break;
267 case 'v':
268 xasm_args.verbose++;
269 break;
271 case 'o':
272 xasm_args.output_file = optarg;
273 break;
275 case 'D': {
276 char *id;
277 char *str;
278 astnode *val;
279 static location loc = { 0, 0, 0, 0, NULL };
280 if (strchr(optarg, '=') != NULL) {
281 /* IDENT=VALUE */
282 id = strtok(optarg, "=");
283 str = strtok(NULL, "\0");
284 if (str) {
285 /* Parse the value */
286 if (str[0] == '\"') {
287 /* Assume string */
288 str = strtok(&str[1], "\"");
289 val = astnode_create_string(str, loc);
290 } else {
291 /* Assume integer */
292 val = astnode_create_integer(strtol(str, NULL, 0), loc);
294 } else {
295 /* No value given -- use empty string */
296 val = astnode_create_string("", loc);
298 } else {
299 id = optarg;
300 val = astnode_create_integer(0, loc);
302 if (validate_ident(id)) {
303 symtab_entry *e;
304 e = symtab_lookup(id);
305 if (e == NULL) {
306 symtab_enter(id, CONSTANT_SYMBOL, val, 0);
307 } else {
308 /* Error, redefinition */
309 fprintf(stderr, "--ident: `%s' already defined\n", id);
311 } else {
312 /* Error, bad identifier */
313 fprintf(stderr, "--ident: `%s' is not a valid identifier\n", id);
316 break;
318 case 'I': {
319 char *p;
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);
324 strcpy(p, optarg);
325 xasm_args.include_paths[count-1] = p;
326 xasm_args.include_path_count = count;
328 break;
330 case 0:
331 /* Use index to differentiate between options */
332 if (strcmp(long_options[index].name, "usage") == 0) {
333 usage();
334 } else if (strcmp(long_options[index].name, "help") == 0) {
335 help();
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;
345 break;
347 case 'V':
348 version();
349 break;
351 case '?':
352 /* Error message has been printed by getopt_long */
353 exit(1);
354 break;
356 default:
357 /* Forgot to handle a short option, most likely */
358 fprintf(stderr, "internal error: unhandled option `%c'\n", key);
359 exit(1);
360 break;
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");
367 exit(1);
369 else {
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)
384 char *p;
385 /* Find the last dot. */
386 p = strrchr(infile, '.');
387 if (p == NULL) {
388 /* There is no dot, simply concatenate extension. */
389 sprintf(outfile, "%s.%s", infile, ext);
391 else {
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) {
408 printf("%s\n", s);
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[]) {
424 char *default_outfile = 0;
426 /* Working directory is needed for include statements */
427 xasm_path = getcwd(NULL, 0);
429 /* Create global symbol table (auto-pushed on stack) */
430 symbol_table = symtab_create();
432 /* Parse our arguments. */
433 parse_arguments (argc, argv);
435 /* Open input for scanning */
436 if (!yybegin(xasm_args.input_file,
437 xasm_args.swap_parens,
438 xasm_args.case_insensitive)) {
439 printf("error: could not open `%s' for reading\n", xasm_args.input_file);
440 symtab_finalize(symbol_table);
441 return(1);
444 /* Parse it into a syntax tree */
445 //yydebug = -1;
446 verbose("Parsing input...");
447 yyparse();
449 if (root_node == NULL) {
450 symtab_finalize(symbol_table);
451 return(0);
454 /* First pass does a lot of stuff. */
455 verbose("First pass...");
456 astproc_first_pass(root_node);
458 /* Second pass does more stuff. */
459 verbose("Second pass...");
460 astproc_second_pass(root_node);
462 /* Third pass is fun. */
463 verbose("Third pass...");
464 astproc_third_pass(root_node);
466 if (xasm_args.pure_binary) {
467 /* Do another pass to prepare for writing pure 6502 */
468 verbose("Fourth pass...");
469 astproc_fourth_pass(root_node);
472 /* Print the final AST (debugging) */
473 // astnode_print(root_node, 0);
475 /* If no errors, proceed with code generation. */
476 if (total_errors() == 0) {
477 if (xasm_args.output_file == NULL) {
478 /* Create default name of output */
479 const char *default_ext = "o";
480 int default_outfile_len = strlen(xasm_args.input_file)
481 + /*dot*/1 + strlen(default_ext) + 1;
482 default_outfile = (char *)malloc(default_outfile_len);
483 change_extension(xasm_args.input_file, default_ext, default_outfile);
484 xasm_args.output_file = default_outfile;
486 /* Write it! */
487 verbose("Generating final output...");
488 if (xasm_args.pure_binary) {
489 astproc_fifth_pass(root_node);
490 } else {
491 codegen_write(root_node, xasm_args.output_file);
495 /* Cleanup */
496 verbose("cleaning up...");
497 symtab_pop();
498 symtab_finalize(symbol_table);
499 astnode_finalize(root_node);
501 if (default_outfile)
502 free(default_outfile);
504 if (xasm_args.include_path_count > 0) {
505 int i;
506 for (i = 0; i < xasm_args.include_path_count; ++i)
507 free(xasm_args.include_paths[i]);
508 free(xasm_args.include_paths);
511 free(xasm_path);
513 return (total_errors() == 0) ? 0 : 1;