323
|
1 /*
|
|
2 main.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <argp.h>
|
|
25 #include <stdio.h>
|
|
26 #include <stdlib.h>
|
|
27
|
329
|
28 #include <lw_alloc.h>
|
325
|
29 #include <lw_string.h>
|
|
30 #include <lw_stringlist.h>
|
|
31
|
323
|
32 #include "lwasm.h"
|
330
|
33 #include "input.h"
|
323
|
34
|
325
|
35 extern int parse_pragma_string(asmstate_t *as, char *str);
|
|
36
|
323
|
37 /* command line option handling */
|
|
38 const char *argp_program_version = "lwasm from " PACKAGE_STRING;
|
|
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
40 char *program_name;
|
|
41
|
|
42 static struct argp_option options[] =
|
|
43 {
|
|
44 { "output", 'o', "FILE", 0, "Output to FILE"},
|
|
45 { "debug", 'd', 0, 0, "Set debug mode"},
|
|
46 { "format", 'f', "TYPE", 0, "Select output format: decb, raw, obj, os9"},
|
|
47 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, "Generate list [to FILE]"},
|
|
48 { "decb", 'b', 0, 0, "Generate DECB .bin format output, equivalent of --format=decb"},
|
|
49 { "raw", 'r', 0, 0, "Generate raw binary format output, equivalent of --format=raw"},
|
|
50 { "obj", 0x100, 0, 0, "Generate proprietary object file format for later linking, equivalent of --format=obj" },
|
|
51 { "depend", 0x101, 0, 0, "Output a dependency list to stdout; do not do any actual output though assembly is completed as usual" },
|
|
52 { "pragma", 'p', "PRAGMA", 0, "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
|
|
53 { "6809", '9', 0, 0, "Set assembler to 6809 only mode" },
|
|
54 { "6309", '3', 0, 0, "Set assembler to 6309 mode (default)" },
|
|
55 { "includedir", 'I', "PATH", 0, "Add entry to include path" },
|
|
56 { 0 }
|
|
57 };
|
|
58
|
|
59
|
|
60 static error_t parse_opts(int key, char *arg, struct argp_state *state)
|
|
61 {
|
|
62 asmstate_t *as = state -> input;
|
|
63
|
|
64 switch (key)
|
|
65 {
|
|
66 case 'I':
|
325
|
67 lw_stringlist_addstring(as -> include_list, arg);
|
|
68 break;
|
|
69
|
323
|
70 case 'o':
|
325
|
71 if (as -> output_file)
|
|
72 lw_free(as -> output_file);
|
|
73 as -> output_file = lw_strdup(arg);
|
|
74 break;
|
|
75
|
323
|
76 case 'd':
|
|
77 as -> debug_level++;
|
|
78 break;
|
|
79
|
|
80 case 'l':
|
325
|
81 if (as -> list_file)
|
|
82 lw_free(as -> list_file);
|
|
83 if (!arg)
|
|
84 as -> list_file = NULL;
|
|
85 else
|
|
86 as -> list_file = lw_strdup(arg);
|
|
87 as -> flags |= FLAG_LIST;
|
|
88 break;
|
|
89
|
323
|
90 case 'b':
|
|
91 as -> output_format = OUTPUT_DECB;
|
|
92 break;
|
|
93
|
|
94 case 'r':
|
|
95 as -> output_format = OUTPUT_RAW;
|
|
96 break;
|
|
97
|
|
98 case 0x100:
|
|
99 as -> output_format = OUTPUT_OBJ;
|
|
100 break;
|
|
101
|
|
102 case 0x101:
|
325
|
103 as -> flags |= FLAG_DEPEND;
|
|
104 break;
|
|
105
|
323
|
106 case 'f':
|
|
107 if (!strcasecmp(arg, "decb"))
|
|
108 as -> output_format = OUTPUT_DECB;
|
|
109 else if (!strcasecmp(arg, "raw"))
|
|
110 as -> output_format = OUTPUT_RAW;
|
|
111 else if (!strcasecmp(arg, "obj"))
|
|
112 as -> output_format = OUTPUT_OBJ;
|
|
113 else if (!strcasecmp(arg, "os9"))
|
|
114 {
|
325
|
115 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
|
323
|
116 as -> output_format = OUTPUT_OS9;
|
|
117 }
|
|
118 else
|
|
119 {
|
|
120 fprintf(stderr, "Invalid output format: %s\n", arg);
|
|
121 exit(1);
|
|
122 }
|
|
123 break;
|
|
124
|
|
125 case 'p':
|
325
|
126 if (parse_pragma_string(as, arg) == 0)
|
|
127 {
|
|
128 fprintf(stderr, "Unrecognized pragma string: %s\n", arg);
|
|
129 exit(1);
|
|
130 }
|
|
131 break;
|
|
132
|
323
|
133 case '9':
|
|
134 as -> target = TARGET_6809;
|
|
135 break;
|
|
136
|
|
137 case '3':
|
|
138 as -> target = TARGET_6309;
|
|
139 break;
|
|
140
|
|
141 case ARGP_KEY_END:
|
|
142 break;
|
|
143
|
|
144 case ARGP_KEY_ARG:
|
325
|
145 lw_stringlist_addstring(as -> input_files, arg);
|
323
|
146 break;
|
|
147
|
|
148 default:
|
|
149 return ARGP_ERR_UNKNOWN;
|
|
150 }
|
|
151 return 0;
|
|
152 }
|
|
153
|
|
154 static struct argp argp =
|
|
155 {
|
|
156 options,
|
|
157 parse_opts,
|
|
158 "<input file>",
|
|
159 "LWASM, a HD6309 and MC6809 cross-assembler"
|
|
160 };
|
|
161
|
|
162 /*
|
|
163 main function; parse command line, set up assembler state, and run the
|
|
164 assembler on the first file
|
|
165 */
|
332
|
166 extern void do_pass1(asmstate_t *as);
|
|
167
|
323
|
168 int main(int argc, char **argv)
|
|
169 {
|
|
170 /* assembler state */
|
|
171 asmstate_t asmstate = { 0 };
|
325
|
172 program_name = argv[0];
|
323
|
173
|
325
|
174 /* initialize assembler state */
|
329
|
175 asmstate.include_list = lw_stringlist_create();
|
325
|
176 asmstate.input_files = lw_stringlist_create();
|
|
177
|
|
178 /* parse command line arguments */
|
323
|
179 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
|
|
180
|
328
|
181 if (!asmstate.output_file)
|
|
182 {
|
|
183 asmstate.output_file = lw_strdup("a.out");
|
|
184 }
|
|
185
|
330
|
186 input_init(&asmstate);
|
|
187
|
332
|
188 do_pass1(&asmstate);
|
|
189
|
323
|
190 exit(0);
|
|
191 }
|