annotate lwasm/main.c @ 448:5cccf90bf838 3.0 tip

Fixed bug with complex external references generating invalid relocations in the object file
author lost@l-w.ca
date Fri, 05 Nov 2010 22:27:00 -0600
parents 027d7fbcdcfc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
1 /*
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
2 main.c
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
3
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
5
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
7
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
11 version.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
12
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
16 more details.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
17
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
20 */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
21
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
22 #include <config.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
23
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
24 #include <argp.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
25 #include <stdio.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
26 #include <stdlib.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
27
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
28 #include <lw_alloc.h>
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
29 #include <lw_string.h>
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
30 #include <lw_stringlist.h>
335
9f58e3bca6e3 checkpoint
lost
parents: 332
diff changeset
31 #include <lw_expr.h>
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
32
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
33 #include "lwasm.h"
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
34 #include "input.h"
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
35
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 346
diff changeset
36 extern int parse_pragma_string(asmstate_t *as, char *str, int ignoreerr);
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
37
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
38 /* command line option handling */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
39 const char *argp_program_version = "lwasm from " PACKAGE_STRING;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
40 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
41 char *program_name;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
42
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
43 static struct argp_option options[] =
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
44 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
45 { "output", 'o', "FILE", 0, "Output to FILE"},
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
46 { "debug", 'd', "LEVEL", OPTION_ARG_OPTIONAL, "Set debug mode"},
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
47 { "format", 'f', "TYPE", 0, "Select output format: decb, raw, obj, os9"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
48 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, "Generate list [to FILE]"},
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 387
diff changeset
49 { "symbols", 's', 0, OPTION_ARG_OPTIONAL, "Generate symbol list in listing, no effect without --list"},
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
50 { "decb", 'b', 0, 0, "Generate DECB .bin format output, equivalent of --format=decb"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
51 { "raw", 'r', 0, 0, "Generate raw binary format output, equivalent of --format=raw"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
52 { "obj", 0x100, 0, 0, "Generate proprietary object file format for later linking, equivalent of --format=obj" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
53 { "depend", 0x101, 0, 0, "Output a dependency list to stdout; do not do any actual output though assembly is completed as usual" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
54 { "pragma", 'p', "PRAGMA", 0, "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
55 { "6809", '9', 0, 0, "Set assembler to 6809 only mode" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
56 { "6309", '3', 0, 0, "Set assembler to 6309 mode (default)" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
57 { "includedir", 'I', "PATH", 0, "Add entry to include path" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
58 { 0 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
59 };
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
60
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
61
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
62 static error_t parse_opts(int key, char *arg, struct argp_state *state)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
63 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
64 asmstate_t *as = state -> input;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
65
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
66 switch (key)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
67 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
68 case 'I':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
69 lw_stringlist_addstring(as -> include_list, arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
70 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
71
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
72 case 'o':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
73 if (as -> output_file)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
74 lw_free(as -> output_file);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
75 as -> output_file = lw_strdup(arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
76 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
77
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
78 case 'd':
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
79 if (!arg)
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
80 as -> debug_level = 50;
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
81 else
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
82 as -> debug_level = atoi(arg);
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
83 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
84
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
85 case 'l':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
86 if (as -> list_file)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
87 lw_free(as -> list_file);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
88 if (!arg)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
89 as -> list_file = NULL;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
90 else
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
91 as -> list_file = lw_strdup(arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
92 as -> flags |= FLAG_LIST;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
93 break;
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 387
diff changeset
94
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 387
diff changeset
95 case 's':
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 387
diff changeset
96 as -> flags |= FLAG_SYMBOLS;
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 387
diff changeset
97 break;
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
98
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
99 case 'b':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
100 as -> output_format = OUTPUT_DECB;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
101 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
102
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
103 case 'r':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
104 as -> output_format = OUTPUT_RAW;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
105 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
106
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
107 case 0x100:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
108 as -> output_format = OUTPUT_OBJ;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
109 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
110
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
111 case 0x101:
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
112 as -> flags |= FLAG_DEPEND;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
113 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
114
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
115 case 'f':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
116 if (!strcasecmp(arg, "decb"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
117 as -> output_format = OUTPUT_DECB;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
118 else if (!strcasecmp(arg, "raw"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
119 as -> output_format = OUTPUT_RAW;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
120 else if (!strcasecmp(arg, "obj"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
121 as -> output_format = OUTPUT_OBJ;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
122 else if (!strcasecmp(arg, "os9"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
123 {
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
124 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
125 as -> output_format = OUTPUT_OS9;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
126 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
127 else
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
128 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
129 fprintf(stderr, "Invalid output format: %s\n", arg);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
130 exit(1);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
131 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
132 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
133
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
134 case 'p':
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 346
diff changeset
135 if (parse_pragma_string(as, arg, 0) == 0)
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
136 {
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
137 fprintf(stderr, "Unrecognized pragma string: %s\n", arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
138 exit(1);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
139 }
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
140 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
141
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
142 case '9':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
143 as -> target = TARGET_6809;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
144 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
145
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
146 case '3':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
147 as -> target = TARGET_6309;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
148 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
149
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
150 case ARGP_KEY_END:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
151 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
152
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
153 case ARGP_KEY_ARG:
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
154 lw_stringlist_addstring(as -> input_files, arg);
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
155 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
156
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
157 default:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
158 return ARGP_ERR_UNKNOWN;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
159 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
160 return 0;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
161 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
162
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
163 static struct argp argp =
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
164 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
165 options,
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
166 parse_opts,
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
167 "<input file>",
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
168 "LWASM, a HD6309 and MC6809 cross-assembler"
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
169 };
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
170
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
171 /*
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
172 main function; parse command line, set up assembler state, and run the
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
173 assembler on the first file
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
174 */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
175 extern void do_pass1(asmstate_t *as);
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 352
diff changeset
176 extern void do_pass2(asmstate_t *as);
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
177 extern void do_pass3(asmstate_t *as);
365
6a98cc90c14f Added resolve passes
lost@starbug
parents: 364
diff changeset
178 extern void do_pass4(asmstate_t *as);
367
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 365
diff changeset
179 extern void do_pass5(asmstate_t *as);
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 365
diff changeset
180 extern void do_pass6(asmstate_t *as);
368
656630007668 Emit pass
lost@starbug
parents: 367
diff changeset
181 extern void do_pass7(asmstate_t *as);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 372
diff changeset
182 extern void do_output(asmstate_t *as);
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 374
diff changeset
183 extern void do_list(asmstate_t *as);
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
184 extern lw_expr_t lwasm_evaluate_special(int t, void *ptr, void *priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
185 extern lw_expr_t lwasm_evaluate_var(char *var, void *priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
186 extern lw_expr_t lwasm_parse_term(char **p, void *priv);
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
187
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
188 struct passlist_s
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
189 {
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
190 char *passname;
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
191 void (*fn)(asmstate_t *as);
385
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 384
diff changeset
192 int fordep;
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
193 } passlist[] = {
385
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 384
diff changeset
194 { "parse", do_pass1, 1 },
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
195 { "symcheck", do_pass2 },
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
196 { "resolve1", do_pass3 },
365
6a98cc90c14f Added resolve passes
lost@starbug
parents: 364
diff changeset
197 { "resolve2", do_pass4 },
367
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 365
diff changeset
198 { "addressresolve", do_pass5 },
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 365
diff changeset
199 { "finalize", do_pass6 },
368
656630007668 Emit pass
lost@starbug
parents: 367
diff changeset
200 { "emit", do_pass7 },
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
201 { NULL, NULL }
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
202 };
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
203
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 368
diff changeset
204
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
205 int main(int argc, char **argv)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
206 {
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
207 int passnum;
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
208
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
209 /* assembler state */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
210 asmstate_t asmstate = { 0 };
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
211 program_name = argv[0];
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
212
337
04c80c51b16a Checkpoint development
lost
parents: 335
diff changeset
213 lw_expr_set_special_handler(lwasm_evaluate_special);
04c80c51b16a Checkpoint development
lost
parents: 335
diff changeset
214 lw_expr_set_var_handler(lwasm_evaluate_var);
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 368
diff changeset
215 lw_expr_set_term_parser(lwasm_parse_term);
337
04c80c51b16a Checkpoint development
lost
parents: 335
diff changeset
216
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
217 /* initialize assembler state */
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
218 asmstate.include_list = lw_stringlist_create();
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
219 asmstate.input_files = lw_stringlist_create();
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 342
diff changeset
220 asmstate.nextcontext = 1;
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
221
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
222 /* parse command line arguments */
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
223 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
224
328
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
225 if (!asmstate.output_file)
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
226 {
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
227 asmstate.output_file = lw_strdup("a.out");
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
228 }
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
229
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
230 input_init(&asmstate);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
231
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
232 for (passnum = 0; passlist[passnum].fn; passnum++)
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 352
diff changeset
233 {
385
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 384
diff changeset
234 if ((asmstate.flags & FLAG_DEPEND) && passlist[passnum].fordep == 0)
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 384
diff changeset
235 continue;
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
236 debug_message(&asmstate, 50, "Doing pass %d (%s)\n", passnum, passlist[passnum].passname);
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
237 (passlist[passnum].fn)(&asmstate);
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
238 debug_message(&asmstate, 50, "After pass %d (%s)\n", passnum, passlist[passnum].passname);
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
239 dump_state(&asmstate);
364
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
240
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
241 if (asmstate.errorcount > 0)
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
242 {
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
243 lwasm_show_errors(&asmstate);
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
244 exit(1);
0b5a26bedbe1 Pass framework simplified; skeleton pass 3
lost@starbug
parents: 363
diff changeset
245 }
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 352
diff changeset
246 }
384
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
247
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
248 if (asmstate.flags & FLAG_DEPEND)
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
249 {
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
250 // output dependencies
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
251 char *n;
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
252
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
253 while (n = lw_stack_pop(asmstate.includelist))
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
254 {
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
255 fprintf(stdout, "%s\n", n);
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
256 lw_free(n);
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
257 }
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
258 }
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
259 else
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
260 {
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
261 debug_message(&asmstate, 50, "Doing output");
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
262 do_output(&asmstate);
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 383
diff changeset
263 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 372
diff changeset
264
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 372
diff changeset
265 debug_message(&asmstate, 50, "Done assembly");
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 374
diff changeset
266
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 374
diff changeset
267 do_list(&asmstate);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 385
diff changeset
268
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 385
diff changeset
269 exit(0);
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
270 }