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