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