Mercurial > hg-old > index.cgi
comparison old-trunk/lwasm/old/main.c @ 339:eb230fa7d28e
Prepare for migration to hg
author | lost |
---|---|
date | Fri, 19 Mar 2010 02:54:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
338:e7885b3ee266 | 339:eb230fa7d28e |
---|---|
1 /* | |
2 main.c | |
3 Copyright © 2008 William Astle | |
4 | |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 Implements the program startup code | |
22 | |
23 */ | |
24 | |
25 #include <config.h> | |
26 | |
27 #include <argp.h> | |
28 #include <errno.h> | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 #include <unistd.h> | |
32 | |
33 #include "lwasm.h" | |
34 | |
35 // external declarations | |
36 extern void lwasm_pass1(asmstate_t *as); | |
37 extern void lwasm_pass2(asmstate_t *as); | |
38 extern void lwasm_list(asmstate_t *as); | |
39 extern void lwasm_output(asmstate_t *as); | |
40 extern void pseudo_pragma_real(asmstate_t *as, lwasm_line_t *cl, char **optr, int error); | |
41 | |
42 // command line option handling | |
43 const char *argp_program_version = "LWASM from " PACKAGE_STRING; | |
44 const char *argp_program_bug_address = PACKAGE_BUGREPORT; | |
45 | |
46 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
47 { | |
48 asmstate_t *as = state -> input; | |
49 char *p; | |
50 | |
51 switch (key) | |
52 { | |
53 case 'o': | |
54 // output | |
55 if (as -> outfile) | |
56 { | |
57 } | |
58 as -> outfile = arg; | |
59 break; | |
60 | |
61 case 'd': | |
62 // debug | |
63 debug_level++; | |
64 break; | |
65 | |
66 case 'l': | |
67 // list | |
68 if (arg) | |
69 as -> listfile = arg; | |
70 else | |
71 as -> listfile = "-"; | |
72 break; | |
73 | |
74 case 'b': | |
75 // decb output | |
76 as -> outformat = OUTPUT_DECB; | |
77 break; | |
78 | |
79 case 'r': | |
80 // raw binary output | |
81 as -> outformat = OUTPUT_RAW; | |
82 break; | |
83 | |
84 case 0x100: | |
85 // proprietary object format | |
86 as -> outformat = OUTPUT_OBJ; | |
87 break; | |
88 | |
89 case 'f': | |
90 // output format | |
91 if (!strcasecmp(arg, "decb")) | |
92 as -> outformat = OUTPUT_DECB; | |
93 else if (!strcasecmp(arg, "raw")) | |
94 as -> outformat = OUTPUT_RAW; | |
95 else if (!strcasecmp(arg, "obj")) | |
96 as -> outformat = OUTPUT_OBJ; | |
97 else if (!strcasecmp(arg, "os9")) | |
98 { | |
99 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL; | |
100 as -> outformat = OUTPUT_OS9; | |
101 } | |
102 else | |
103 { | |
104 fprintf(stderr, "Invalid output format: %s\n", arg); | |
105 exit(1); | |
106 } | |
107 break; | |
108 | |
109 case 'p': | |
110 // pragmas | |
111 p = arg; | |
112 pseudo_pragma_real(as, NULL, &p, 2); | |
113 if (!p) | |
114 { | |
115 fprintf(stderr, "Invalid pragma string: %s\n", arg); | |
116 exit(1); | |
117 } | |
118 break; | |
119 | |
120 case '9': | |
121 as -> no6309 = 1; | |
122 break; | |
123 | |
124 case '3': | |
125 as -> no6309 = 0; | |
126 break; | |
127 | |
128 case ARGP_KEY_END: | |
129 // done; sanity check | |
130 if (!as -> outfile) | |
131 as -> outfile = "a.out"; | |
132 break; | |
133 | |
134 case ARGP_KEY_ARG: | |
135 // non-option arg | |
136 if (as -> infile) | |
137 argp_usage(state); | |
138 as -> infile = arg; | |
139 break; | |
140 | |
141 default: | |
142 return ARGP_ERR_UNKNOWN; | |
143 } | |
144 return 0; | |
145 } | |
146 | |
147 static struct argp_option options[] = | |
148 { | |
149 { "output", 'o', "FILE", 0, | |
150 "Output to FILE"}, | |
151 { "debug", 'd', 0, 0, | |
152 "Set debug mode"}, | |
153 { "format", 'f', "TYPE", 0, | |
154 "Select output format: decb, raw, obj, os9"}, | |
155 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, | |
156 "Generate list [to FILE]"}, | |
157 { "decb", 'b', 0, 0, | |
158 "Generate DECB .bin format output, equivalent of --format=decb"}, | |
159 { "raw", 'r', 0, 0, | |
160 "Generate raw binary format output, equivalent of --format=raw"}, | |
161 { "obj", 0x100, 0, 0, | |
162 "Generate proprietary object file format for later linking, equivalent of --format=obj" }, | |
163 { "pragma", 'p', "PRAGMA", 0, | |
164 "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"}, | |
165 { "6809", '9', 0, 0, | |
166 "Set assembler to 6809 only mode" }, | |
167 { "6309", '3', 0, 0, | |
168 "Set assembler to 6309 mode (default)" }, | |
169 { 0 } | |
170 }; | |
171 | |
172 static struct argp argp = | |
173 { | |
174 options, | |
175 parse_opts, | |
176 "<input file>", | |
177 "LWASM, a HD6309 and MC6809 cross-assembler" | |
178 }; | |
179 | |
180 char *program_name; | |
181 | |
182 // main function; parse command line, set up assembler state, and run the | |
183 // assembler on the first file | |
184 int main(int argc, char **argv) | |
185 { | |
186 | |
187 program_name = argv[0]; | |
188 | |
189 // assembler state | |
190 asmstate_t asmstate = { 0 }; | |
191 | |
192 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
193 | |
194 if (!asmstate.infile) | |
195 { | |
196 fprintf(stderr, "No input files specified.\n"); | |
197 exit(1); | |
198 } | |
199 | |
200 // lose the output file if it already exists... | |
201 unlink(asmstate.outfile); | |
202 | |
203 /* pass 1 - collect the symbols and assign addresses where possible */ | |
204 /* pass 1 also resolves included files, etc. */ | |
205 /* that means files are read exactly once unless included multiple times */ | |
206 lwasm_pass1(&asmstate); | |
207 | |
208 // pass 2: actually generate the code; if any phasing errors appear | |
209 // at this stage, we have a bug | |
210 lwasm_pass2(&asmstate); | |
211 | |
212 // now make a pretty listing | |
213 lwasm_list(&asmstate); | |
214 | |
215 // now write the code out to the output file | |
216 lwasm_output(&asmstate); | |
217 | |
218 if (asmstate.errorcount > 0) | |
219 exit(1); | |
220 | |
221 exit(0); | |
222 } | |
223 |