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"
|
|
33
|
325
|
34 extern int parse_pragma_string(asmstate_t *as, char *str);
|
|
35
|
323
|
36 /* command line option handling */
|
|
37 const char *argp_program_version = "lwasm from " PACKAGE_STRING;
|
|
38 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
39 char *program_name;
|
|
40
|
|
41 static struct argp_option options[] =
|
|
42 {
|
|
43 { "output", 'o', "FILE", 0, "Output to FILE"},
|
|
44 { "debug", 'd', 0, 0, "Set debug mode"},
|
|
45 { "format", 'f', "TYPE", 0, "Select output format: decb, raw, obj, os9"},
|
|
46 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, "Generate list [to FILE]"},
|
|
47 { "decb", 'b', 0, 0, "Generate DECB .bin format output, equivalent of --format=decb"},
|
|
48 { "raw", 'r', 0, 0, "Generate raw binary format output, equivalent of --format=raw"},
|
|
49 { "obj", 0x100, 0, 0, "Generate proprietary object file format for later linking, equivalent of --format=obj" },
|
|
50 { "depend", 0x101, 0, 0, "Output a dependency list to stdout; do not do any actual output though assembly is completed as usual" },
|
|
51 { "pragma", 'p', "PRAGMA", 0, "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
|
|
52 { "6809", '9', 0, 0, "Set assembler to 6809 only mode" },
|
|
53 { "6309", '3', 0, 0, "Set assembler to 6309 mode (default)" },
|
|
54 { "includedir", 'I', "PATH", 0, "Add entry to include path" },
|
|
55 { 0 }
|
|
56 };
|
|
57
|
|
58
|
|
59 static error_t parse_opts(int key, char *arg, struct argp_state *state)
|
|
60 {
|
|
61 asmstate_t *as = state -> input;
|
|
62
|
|
63 switch (key)
|
|
64 {
|
|
65 case 'I':
|
325
|
66 lw_stringlist_addstring(as -> include_list, arg);
|
|
67 break;
|
|
68
|
323
|
69 case 'o':
|
325
|
70 if (as -> output_file)
|
|
71 lw_free(as -> output_file);
|
|
72 as -> output_file = lw_strdup(arg);
|
|
73 break;
|
|
74
|
323
|
75 case 'd':
|
|
76 as -> debug_level++;
|
|
77 break;
|
|
78
|
|
79 case 'l':
|
325
|
80 if (as -> list_file)
|
|
81 lw_free(as -> list_file);
|
|
82 if (!arg)
|
|
83 as -> list_file = NULL;
|
|
84 else
|
|
85 as -> list_file = lw_strdup(arg);
|
|
86 as -> flags |= FLAG_LIST;
|
|
87 break;
|
|
88
|
323
|
89 case 'b':
|
|
90 as -> output_format = OUTPUT_DECB;
|
|
91 break;
|
|
92
|
|
93 case 'r':
|
|
94 as -> output_format = OUTPUT_RAW;
|
|
95 break;
|
|
96
|
|
97 case 0x100:
|
|
98 as -> output_format = OUTPUT_OBJ;
|
|
99 break;
|
|
100
|
|
101 case 0x101:
|
325
|
102 as -> flags |= FLAG_DEPEND;
|
|
103 break;
|
|
104
|
323
|
105 case 'f':
|
|
106 if (!strcasecmp(arg, "decb"))
|
|
107 as -> output_format = OUTPUT_DECB;
|
|
108 else if (!strcasecmp(arg, "raw"))
|
|
109 as -> output_format = OUTPUT_RAW;
|
|
110 else if (!strcasecmp(arg, "obj"))
|
|
111 as -> output_format = OUTPUT_OBJ;
|
|
112 else if (!strcasecmp(arg, "os9"))
|
|
113 {
|
325
|
114 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
|
323
|
115 as -> output_format = OUTPUT_OS9;
|
|
116 }
|
|
117 else
|
|
118 {
|
|
119 fprintf(stderr, "Invalid output format: %s\n", arg);
|
|
120 exit(1);
|
|
121 }
|
|
122 break;
|
|
123
|
|
124 case 'p':
|
325
|
125 if (parse_pragma_string(as, arg) == 0)
|
|
126 {
|
|
127 fprintf(stderr, "Unrecognized pragma string: %s\n", arg);
|
|
128 exit(1);
|
|
129 }
|
|
130 break;
|
|
131
|
323
|
132 case '9':
|
|
133 as -> target = TARGET_6809;
|
|
134 break;
|
|
135
|
|
136 case '3':
|
|
137 as -> target = TARGET_6309;
|
|
138 break;
|
|
139
|
|
140 case ARGP_KEY_END:
|
|
141 break;
|
|
142
|
|
143 case ARGP_KEY_ARG:
|
325
|
144 lw_stringlist_addstring(as -> input_files, arg);
|
323
|
145 break;
|
|
146
|
|
147 default:
|
|
148 return ARGP_ERR_UNKNOWN;
|
|
149 }
|
|
150 return 0;
|
|
151 }
|
|
152
|
|
153 static struct argp argp =
|
|
154 {
|
|
155 options,
|
|
156 parse_opts,
|
|
157 "<input file>",
|
|
158 "LWASM, a HD6309 and MC6809 cross-assembler"
|
|
159 };
|
|
160
|
|
161 /*
|
|
162 main function; parse command line, set up assembler state, and run the
|
|
163 assembler on the first file
|
|
164 */
|
|
165 int main(int argc, char **argv)
|
|
166 {
|
|
167 /* assembler state */
|
|
168 asmstate_t asmstate = { 0 };
|
325
|
169 program_name = argv[0];
|
323
|
170
|
325
|
171 /* initialize assembler state */
|
329
|
172 asmstate.include_list = lw_stringlist_create();
|
325
|
173 asmstate.input_files = lw_stringlist_create();
|
329
|
174 asmstate.file_dir = lw_stack_create(lw_free);
|
|
175 input_init();
|
325
|
176
|
|
177 /* parse command line arguments */
|
323
|
178 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
|
|
179
|
328
|
180 if (!asmstate.output_file)
|
|
181 {
|
|
182 asmstate.output_file = lw_strdup("a.out");
|
|
183 }
|
|
184
|
323
|
185 exit(0);
|
|
186 }
|