annotate old-trunk/lwasm/main.c @ 354:60568b123281

Added os9 opcodes and ERROR
author lost@starbug
date Tue, 30 Mar 2010 23:10:01 -0600
parents eb230fa7d28e
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 © 2009 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 extern void pseudo_pragma_real(asmstate_t *as, void *cl, char **optr, int error);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 // command line option handling
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 const char *argp_program_version = "LWASM from " PACKAGE_STRING;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 static error_t parse_opts(int key, char *arg, struct argp_state *state)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 asmstate_t *as = state -> input;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 char *p;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 switch (key)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 case 'o':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 // output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 if (as -> outfile)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 as -> outfile = arg;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 case 'd':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 // debug
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 debug_level++;
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 'l':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 // list
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 if (arg)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 as -> listfile = arg;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 as -> listfile = "-";
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 case 'b':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 // decb output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 as -> outformat = OUTPUT_DECB;
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 'r':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 // raw binary output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 as -> outformat = OUTPUT_RAW;
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 0x100:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 // proprietary object format
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 as -> outformat = OUTPUT_OBJ;
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 'f':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 // output format
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 if (!strcasecmp(arg, "decb"))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 as -> outformat = OUTPUT_DECB;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 else if (!strcasecmp(arg, "raw"))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 as -> outformat = OUTPUT_RAW;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 else if (!strcasecmp(arg, "obj"))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 as -> outformat = OUTPUT_OBJ;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 else if (!strcasecmp(arg, "os9"))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 as -> outformat = OUTPUT_OS9;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 fprintf(stderr, "Invalid output format: %s\n", arg);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 case 'p':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 // pragmas
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 p = arg;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 pseudo_pragma_real(as, NULL, &p, 2);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 if (!p)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 fprintf(stderr, "Invalid pragma string: %s\n", arg);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 case '9':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 as -> no6309 = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 case '3':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 as -> no6309 = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 case ARGP_KEY_END:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 // done; sanity check
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 if (!as -> outfile)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 as -> outfile = "a.out";
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 case ARGP_KEY_ARG:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 // non-option arg
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 if (as -> infile)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 argp_usage(state);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 as -> infile = arg;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 return ARGP_ERR_UNKNOWN;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 static struct argp_option options[] =
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 { "output", 'o', "FILE", 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 "Output to FILE"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146 { "debug", 'd', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 "Set debug mode"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 { "format", 'f', "TYPE", 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 "Select output format: decb, raw, obj, os9"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 "Generate list [to FILE]"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 { "decb", 'b', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 "Generate DECB .bin format output, equivalent of --format=decb"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 { "raw", 'r', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 "Generate raw binary format output, equivalent of --format=raw"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 { "obj", 0x100, 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 "Generate proprietary object file format for later linking, equivalent of --format=obj" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 { "pragma", 'p', "PRAGMA", 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159 "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 { "6809", '9', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 "Set assembler to 6809 only mode" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 { "6309", '3', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163 "Set assembler to 6309 mode (default)" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 { 0 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167 static struct argp argp =
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 options,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 parse_opts,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171 "<input file>",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 "LWASM, a HD6309 and MC6809 cross-assembler"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 char *program_name;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177 // main function; parse command line, set up assembler state, and run the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
178 // assembler on the first file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179 int main(int argc, char **argv)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 asmstate_t asmstate;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183 program_name = argv[0];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 if (!asmstate.infile)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 fprintf(stderr, "No input files specified.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
191 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 // lose the output file if it already exists...
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 unlink(asmstate.outfile);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 exit(0);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198