Mercurial > hg-old > index.cgi
annotate src/main.c @ 109:f21a5593a661
Updated docs
author | lost |
---|---|
date | Wed, 28 Jan 2009 05:38:21 +0000 |
parents | dbf1b926c2f1 |
children | 36ca3fa755e0 |
rev | line source |
---|---|
0 | 1 /* |
2 main.c | |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
0 | 20 |
21 Implements the program startup code | |
22 | |
23 */ | |
24 | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
25 #ifdef HAVE_CONFIG_H |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
26 #include "config.h" |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
27 #endif |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
28 |
0 | 29 #include <argp.h> |
30 #include <errno.h> | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 | |
34 #include "lwasm.h" | |
35 | |
36 // external declarations | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
37 extern void lwasm_pass1(asmstate_t *as); |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
38 extern void lwasm_pass2(asmstate_t *as); |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
19
diff
changeset
|
39 extern void lwasm_list(asmstate_t *as); |
46 | 40 extern void lwasm_output(asmstate_t *as); |
0 | 41 |
42 // command line option handling | |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
43 const char *argp_program_version = PACKAGE_STRING; |
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
44 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
0 | 45 |
46 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
47 { | |
48 asmstate_t *as = state -> input; | |
49 | |
50 switch (key) | |
51 { | |
52 case 'o': | |
53 // output | |
54 if (as -> outfile) | |
55 { | |
56 } | |
57 as -> outfile = arg; | |
58 break; | |
59 | |
60 case 'd': | |
61 // debug | |
38 | 62 debug_level++; |
0 | 63 break; |
64 | |
65 case 'l': | |
66 // list | |
67 if (arg) | |
68 as -> listfile = arg; | |
69 else | |
70 as -> listfile = "-"; | |
71 break; | |
72 | |
73 case 'b': | |
74 // decb output | |
75 as -> outformat = OUTPUT_DECB; | |
76 break; | |
77 | |
78 case 'r': | |
79 // raw binary output | |
80 as -> outformat = OUTPUT_RAW; | |
81 break; | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
82 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
83 case 0x100: |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
84 // proprietary object format |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
85 as -> outformat = OUTPUT_OBJ; |
19 | 86 break; |
87 | |
88 case 'f': | |
89 // output format | |
90 if (!strcasecmp(arg, "decb")) | |
91 as -> outformat = OUTPUT_DECB; | |
92 else if (!strcasecmp(arg, "raw")) | |
93 as -> outformat = OUTPUT_RAW; | |
94 else if (!strcasecmp(arg, "obj")) | |
95 as -> outformat = OUTPUT_OBJ; | |
96 else | |
97 { | |
98 fprintf(stderr, "Invalid output format: %s\n", arg); | |
99 exit(1); | |
100 } | |
101 break; | |
0 | 102 case ARGP_KEY_END: |
103 // done; sanity check | |
104 if (!as -> outfile) | |
105 as -> outfile = "a.out"; | |
106 break; | |
107 | |
108 case ARGP_KEY_ARG: | |
109 // non-option arg | |
110 if (as -> infile) | |
111 argp_usage(state); | |
112 as -> infile = arg; | |
113 break; | |
114 | |
115 default: | |
116 return ARGP_ERR_UNKNOWN; | |
117 } | |
118 return 0; | |
119 } | |
120 | |
121 static struct argp_option options[] = | |
122 { | |
123 { "output", 'o', "FILE", 0, | |
124 "Output to FILE"}, | |
125 { "debug", 'd', 0, 0, | |
126 "Set debug mode"}, | |
19 | 127 { "format", 'f', "TYPE", 0, |
128 "Select output format: decb, raw, obj"}, | |
0 | 129 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, |
130 "Generate list [to FILE]"}, | |
131 { "decb", 'b', 0, 0, | |
19 | 132 "Generate DECB .bin format output, equivalent of --format=decb"}, |
0 | 133 { "raw", 'r', 0, 0, |
19 | 134 "Generate raw binary format output, equivalent of --format=raw"}, |
74 | 135 { "obj", 0x100, 0, 0, |
19 | 136 "Generate proprietary object file format for later linking, equivalent of --format=obj" }, |
0 | 137 { 0 } |
138 }; | |
139 | |
140 static struct argp argp = | |
141 { | |
142 options, | |
143 parse_opts, | |
144 "<input file>", | |
145 "LWASM, a HD6309 and MC6809 cross-assembler" | |
146 }; | |
147 | |
148 // main function; parse command line, set up assembler state, and run the | |
149 // assembler on the first file | |
150 int main(int argc, char **argv) | |
151 { | |
152 // assembler state | |
153 asmstate_t asmstate = { 0 }; | |
154 | |
155 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
156 | |
19 | 157 if (!asmstate.infile) |
158 { | |
159 fprintf(stderr, "No input files specified.\n"); | |
160 exit(1); | |
161 } | |
162 | |
0 | 163 /* pass 1 - collect the symbols and assign addresses where possible */ |
164 /* pass 1 also resolves included files, etc. */ | |
165 /* that means files are read exactly once unless included multiple times */ | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
166 lwasm_pass1(&asmstate); |
0 | 167 |
168 // pass 2: actually generate the code; if any phasing errors appear | |
169 // at this stage, we have a bug | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
170 lwasm_pass2(&asmstate); |
0 | 171 |
172 // now make a pretty listing | |
35
39d750ee8d34
Added error display and fixed infinite loop in lwasm_parse_line()
lost
parents:
19
diff
changeset
|
173 lwasm_list(&asmstate); |
0 | 174 |
175 // now write the code out to the output file | |
46 | 176 lwasm_output(&asmstate); |
0 | 177 |
178 if (asmstate.errorcount > 0) | |
179 exit(1); | |
180 | |
181 exit(0); | |
182 } | |
183 |