Mercurial > hg-old > index.cgi
annotate lwasm/main.c @ 261:c79b3c88adbc 2.x
Added --depend option to generate a list of dependencies
author | lost |
---|---|
date | Sat, 26 Dec 2009 08:24:35 +0000 |
parents | e27279180a73 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 main.c | |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
0 | 20 |
21 Implements the program startup code | |
22 | |
23 */ | |
24 | |
212 | 25 #include <config.h> |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
26 |
0 | 27 #include <argp.h> |
28 #include <errno.h> | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
31 #include <unistd.h> |
0 | 32 |
33 #include "lwasm.h" | |
257 | 34 #include "util.h" |
0 | 35 |
36 // external declarations | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
37 extern void lwasm_pass1(asmstate_t *as); |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
38 extern void lwasm_pass2(asmstate_t *as); |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
19
diff
changeset
|
39 extern void lwasm_list(asmstate_t *as); |
46 | 40 extern void lwasm_output(asmstate_t *as); |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
41 extern void pseudo_pragma_real(asmstate_t *as, lwasm_line_t *cl, char **optr, int error); |
0 | 42 |
43 // command line option handling | |
142 | 44 const char *argp_program_version = "LWASM from " PACKAGE_STRING; |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
45 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
0 | 46 |
47 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
48 { | |
49 asmstate_t *as = state -> input; | |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
50 char *p; |
0 | 51 |
52 switch (key) | |
53 { | |
257 | 54 case 'I': |
55 as -> includedirs = lwasm_realloc(as -> includedirs, (as -> nincludedirs + 1) * sizeof(char *)); | |
56 as -> includedirs[as -> nincludedirs++] = arg; | |
57 break; | |
58 | |
0 | 59 case 'o': |
60 // output | |
61 if (as -> outfile) | |
62 { | |
63 } | |
64 as -> outfile = arg; | |
65 break; | |
66 | |
67 case 'd': | |
68 // debug | |
38 | 69 debug_level++; |
0 | 70 break; |
71 | |
72 case 'l': | |
73 // list | |
74 if (arg) | |
75 as -> listfile = arg; | |
76 else | |
77 as -> listfile = "-"; | |
78 break; | |
79 | |
80 case 'b': | |
81 // decb output | |
82 as -> outformat = OUTPUT_DECB; | |
83 break; | |
84 | |
85 case 'r': | |
86 // raw binary output | |
87 as -> outformat = OUTPUT_RAW; | |
88 break; | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
89 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
90 case 0x100: |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
91 // proprietary object format |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
92 as -> outformat = OUTPUT_OBJ; |
19 | 93 break; |
261
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
94 |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
95 case 0x101: |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
96 // dependency tracking |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
97 as -> deptrack = 1; |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
98 break; |
19 | 99 |
100 case 'f': | |
101 // output format | |
102 if (!strcasecmp(arg, "decb")) | |
103 as -> outformat = OUTPUT_DECB; | |
104 else if (!strcasecmp(arg, "raw")) | |
105 as -> outformat = OUTPUT_RAW; | |
106 else if (!strcasecmp(arg, "obj")) | |
107 as -> outformat = OUTPUT_OBJ; | |
236
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
108 else if (!strcasecmp(arg, "os9")) |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
109 { |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
110 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
111 as -> outformat = OUTPUT_OS9; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
112 } |
19 | 113 else |
114 { | |
115 fprintf(stderr, "Invalid output format: %s\n", arg); | |
116 exit(1); | |
117 } | |
118 break; | |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
119 |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
120 case 'p': |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
121 // pragmas |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
122 p = arg; |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
123 pseudo_pragma_real(as, NULL, &p, 2); |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
124 if (!p) |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
125 { |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
126 fprintf(stderr, "Invalid pragma string: %s\n", arg); |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
127 exit(1); |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
128 } |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
129 break; |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
130 |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
131 case '9': |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
132 as -> no6309 = 1; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
133 break; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
134 |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
135 case '3': |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
136 as -> no6309 = 0; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
137 break; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
138 |
0 | 139 case ARGP_KEY_END: |
140 // done; sanity check | |
141 if (!as -> outfile) | |
142 as -> outfile = "a.out"; | |
143 break; | |
144 | |
145 case ARGP_KEY_ARG: | |
146 // non-option arg | |
147 if (as -> infile) | |
148 argp_usage(state); | |
149 as -> infile = arg; | |
150 break; | |
151 | |
152 default: | |
153 return ARGP_ERR_UNKNOWN; | |
154 } | |
155 return 0; | |
156 } | |
157 | |
158 static struct argp_option options[] = | |
159 { | |
160 { "output", 'o', "FILE", 0, | |
161 "Output to FILE"}, | |
162 { "debug", 'd', 0, 0, | |
163 "Set debug mode"}, | |
19 | 164 { "format", 'f', "TYPE", 0, |
236
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
230
diff
changeset
|
165 "Select output format: decb, raw, obj, os9"}, |
0 | 166 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, |
167 "Generate list [to FILE]"}, | |
168 { "decb", 'b', 0, 0, | |
19 | 169 "Generate DECB .bin format output, equivalent of --format=decb"}, |
0 | 170 { "raw", 'r', 0, 0, |
19 | 171 "Generate raw binary format output, equivalent of --format=raw"}, |
74 | 172 { "obj", 0x100, 0, 0, |
19 | 173 "Generate proprietary object file format for later linking, equivalent of --format=obj" }, |
261
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
174 { "depend", 0x101, 0, 0, |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
175 "Output a dependency list to stdout; do not do any actual output though assembly is completed as usual" }, |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
176 { "pragma", 'p', "PRAGMA", 0, |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
142
diff
changeset
|
177 "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"}, |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
178 { "6809", '9', 0, 0, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
179 "Set assembler to 6809 only mode" }, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
180 { "6309", '3', 0, 0, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
181 "Set assembler to 6309 mode (default)" }, |
257 | 182 { "includedir", 'I', "PATH", 0, |
183 "Add entry to include path" }, | |
0 | 184 { 0 } |
185 }; | |
186 | |
187 static struct argp argp = | |
188 { | |
189 options, | |
190 parse_opts, | |
191 "<input file>", | |
192 "LWASM, a HD6309 and MC6809 cross-assembler" | |
193 }; | |
194 | |
212 | 195 char *program_name; |
196 | |
0 | 197 // main function; parse command line, set up assembler state, and run the |
198 // assembler on the first file | |
199 int main(int argc, char **argv) | |
200 { | |
212 | 201 |
202 program_name = argv[0]; | |
203 | |
0 | 204 // assembler state |
205 asmstate_t asmstate = { 0 }; | |
206 | |
207 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
208 | |
19 | 209 if (!asmstate.infile) |
210 { | |
211 fprintf(stderr, "No input files specified.\n"); | |
212 exit(1); | |
213 } | |
214 | |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
215 // lose the output file if it already exists... |
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
216 unlink(asmstate.outfile); |
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
217 |
0 | 218 /* pass 1 - collect the symbols and assign addresses where possible */ |
219 /* pass 1 also resolves included files, etc. */ | |
220 /* that means files are read exactly once unless included multiple times */ | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
221 lwasm_pass1(&asmstate); |
0 | 222 |
223 // pass 2: actually generate the code; if any phasing errors appear | |
224 // at this stage, we have a bug | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
225 lwasm_pass2(&asmstate); |
0 | 226 |
227 // now make a pretty listing | |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
19
diff
changeset
|
228 lwasm_list(&asmstate); |
0 | 229 |
230 // now write the code out to the output file | |
261
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
231 if (asmstate.deptrack) |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
232 { |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
233 int i; |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
234 // list the files read (on includes) |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
235 for (i = 0; i < asmstate.nincfiles; i++) |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
236 { |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
237 printf("%s\n", asmstate.incfiles[i]); |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
238 } |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
239 } |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
240 else |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
241 { |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
242 lwasm_output(&asmstate); |
c79b3c88adbc
Added --depend option to generate a list of dependencies
lost
parents:
257
diff
changeset
|
243 } |
0 | 244 |
245 if (asmstate.errorcount > 0) | |
246 exit(1); | |
247 | |
248 exit(0); | |
249 } | |
250 |