Mercurial > hg-old > index.cgi
annotate lwasm/main.c @ 182:833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
author | lost |
---|---|
date | Fri, 06 Mar 2009 01:35:40 +0000 |
parents | 427e268e876b |
children | bae1e3ecdce1 |
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 | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
25 #ifdef HAVE_CONFIG_H |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
26 #include "config.h" |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
27 #endif |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
28 |
0 | 29 #include <argp.h> |
30 #include <errno.h> | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
33 #include <unistd.h> |
0 | 34 |
35 #include "lwasm.h" | |
36 | |
37 // external declarations | |
13
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_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
|
39 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
|
40 extern void lwasm_list(asmstate_t *as); |
46 | 41 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
|
42 extern void pseudo_pragma_real(asmstate_t *as, lwasm_line_t *cl, char **optr, int error); |
0 | 43 |
44 // command line option handling | |
142 | 45 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
|
46 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
0 | 47 |
48 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
49 { | |
50 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
|
51 char *p; |
0 | 52 |
53 switch (key) | |
54 { | |
55 case 'o': | |
56 // output | |
57 if (as -> outfile) | |
58 { | |
59 } | |
60 as -> outfile = arg; | |
61 break; | |
62 | |
63 case 'd': | |
64 // debug | |
38 | 65 debug_level++; |
0 | 66 break; |
67 | |
68 case 'l': | |
69 // list | |
70 if (arg) | |
71 as -> listfile = arg; | |
72 else | |
73 as -> listfile = "-"; | |
74 break; | |
75 | |
76 case 'b': | |
77 // decb output | |
78 as -> outformat = OUTPUT_DECB; | |
79 break; | |
80 | |
81 case 'r': | |
82 // raw binary output | |
83 as -> outformat = OUTPUT_RAW; | |
84 break; | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
85 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
86 case 0x100: |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
87 // proprietary object format |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
88 as -> outformat = OUTPUT_OBJ; |
19 | 89 break; |
90 | |
91 case 'f': | |
92 // output format | |
93 if (!strcasecmp(arg, "decb")) | |
94 as -> outformat = OUTPUT_DECB; | |
95 else if (!strcasecmp(arg, "raw")) | |
96 as -> outformat = OUTPUT_RAW; | |
97 else if (!strcasecmp(arg, "obj")) | |
98 as -> outformat = OUTPUT_OBJ; | |
99 else | |
100 { | |
101 fprintf(stderr, "Invalid output format: %s\n", arg); | |
102 exit(1); | |
103 } | |
104 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
|
105 |
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
|
106 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
|
107 // 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
|
108 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
|
109 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
|
110 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
|
111 { |
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
|
112 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
|
113 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
|
114 } |
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
|
115 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
|
116 |
0 | 117 case ARGP_KEY_END: |
118 // done; sanity check | |
119 if (!as -> outfile) | |
120 as -> outfile = "a.out"; | |
121 break; | |
122 | |
123 case ARGP_KEY_ARG: | |
124 // non-option arg | |
125 if (as -> infile) | |
126 argp_usage(state); | |
127 as -> infile = arg; | |
128 break; | |
129 | |
130 default: | |
131 return ARGP_ERR_UNKNOWN; | |
132 } | |
133 return 0; | |
134 } | |
135 | |
136 static struct argp_option options[] = | |
137 { | |
138 { "output", 'o', "FILE", 0, | |
139 "Output to FILE"}, | |
140 { "debug", 'd', 0, 0, | |
141 "Set debug mode"}, | |
19 | 142 { "format", 'f', "TYPE", 0, |
143 "Select output format: decb, raw, obj"}, | |
0 | 144 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, |
145 "Generate list [to FILE]"}, | |
146 { "decb", 'b', 0, 0, | |
19 | 147 "Generate DECB .bin format output, equivalent of --format=decb"}, |
0 | 148 { "raw", 'r', 0, 0, |
19 | 149 "Generate raw binary format output, equivalent of --format=raw"}, |
74 | 150 { "obj", 0x100, 0, 0, |
19 | 151 "Generate proprietary object file format for later linking, equivalent of --format=obj" }, |
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
|
152 { "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
|
153 "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"}, |
0 | 154 { 0 } |
155 }; | |
156 | |
157 static struct argp argp = | |
158 { | |
159 options, | |
160 parse_opts, | |
161 "<input file>", | |
162 "LWASM, a HD6309 and MC6809 cross-assembler" | |
163 }; | |
164 | |
165 // main function; parse command line, set up assembler state, and run the | |
166 // assembler on the first file | |
167 int main(int argc, char **argv) | |
168 { | |
169 // assembler state | |
170 asmstate_t asmstate = { 0 }; | |
171 | |
172 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
173 | |
19 | 174 if (!asmstate.infile) |
175 { | |
176 fprintf(stderr, "No input files specified.\n"); | |
177 exit(1); | |
178 } | |
179 | |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
180 // 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
|
181 unlink(asmstate.outfile); |
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
151
diff
changeset
|
182 |
0 | 183 /* pass 1 - collect the symbols and assign addresses where possible */ |
184 /* pass 1 also resolves included files, etc. */ | |
185 /* 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
|
186 lwasm_pass1(&asmstate); |
0 | 187 |
188 // pass 2: actually generate the code; if any phasing errors appear | |
189 // 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
|
190 lwasm_pass2(&asmstate); |
0 | 191 |
192 // now make a pretty listing | |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
19
diff
changeset
|
193 lwasm_list(&asmstate); |
0 | 194 |
195 // now write the code out to the output file | |
46 | 196 lwasm_output(&asmstate); |
0 | 197 |
198 if (asmstate.errorcount > 0) | |
199 exit(1); | |
200 | |
201 exit(0); | |
202 } | |
203 |