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