Mercurial > hg-old > index.cgi
annotate src/main.c @ 25:3b818f05dc2a
imported instruction table from older version to development version
author | lost |
---|---|
date | Fri, 02 Jan 2009 02:03:48 +0000 |
parents | 925105ccf22f |
children | 39d750ee8d34 |
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); |
0 | 39 |
40 | |
41 // command line option handling | |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
42 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
|
43 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
0 | 44 |
45 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
46 { | |
47 asmstate_t *as = state -> input; | |
48 | |
49 switch (key) | |
50 { | |
51 case 'o': | |
52 // output | |
53 if (as -> outfile) | |
54 { | |
55 } | |
56 as -> outfile = arg; | |
57 break; | |
58 | |
59 case 'd': | |
60 // debug | |
61 as -> debug++; | |
62 break; | |
63 | |
64 case 'l': | |
65 // list | |
66 if (arg) | |
67 as -> listfile = arg; | |
68 else | |
69 as -> listfile = "-"; | |
70 break; | |
71 | |
72 case 'b': | |
73 // decb output | |
74 as -> outformat = OUTPUT_DECB; | |
75 break; | |
76 | |
77 case 'r': | |
78 // raw binary output | |
79 as -> outformat = OUTPUT_RAW; | |
80 break; | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
81 |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
82 case 0x100: |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
83 // proprietary object format |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
84 as -> outformat = OUTPUT_OBJ; |
19 | 85 break; |
86 | |
87 case 'f': | |
88 // output format | |
89 if (!strcasecmp(arg, "decb")) | |
90 as -> outformat = OUTPUT_DECB; | |
91 else if (!strcasecmp(arg, "raw")) | |
92 as -> outformat = OUTPUT_RAW; | |
93 else if (!strcasecmp(arg, "obj")) | |
94 as -> outformat = OUTPUT_OBJ; | |
95 else | |
96 { | |
97 fprintf(stderr, "Invalid output format: %s\n", arg); | |
98 exit(1); | |
99 } | |
100 break; | |
0 | 101 case ARGP_KEY_END: |
102 // done; sanity check | |
103 if (!as -> outfile) | |
104 as -> outfile = "a.out"; | |
105 break; | |
106 | |
107 case ARGP_KEY_ARG: | |
108 // non-option arg | |
109 if (as -> infile) | |
110 argp_usage(state); | |
111 as -> infile = arg; | |
112 break; | |
113 | |
114 default: | |
115 return ARGP_ERR_UNKNOWN; | |
116 } | |
117 return 0; | |
118 } | |
119 | |
120 static struct argp_option options[] = | |
121 { | |
122 { "output", 'o', "FILE", 0, | |
123 "Output to FILE"}, | |
124 { "debug", 'd', 0, 0, | |
125 "Set debug mode"}, | |
19 | 126 { "format", 'f', "TYPE", 0, |
127 "Select output format: decb, raw, obj"}, | |
0 | 128 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, |
129 "Generate list [to FILE]"}, | |
130 { "decb", 'b', 0, 0, | |
19 | 131 "Generate DECB .bin format output, equivalent of --format=decb"}, |
0 | 132 { "raw", 'r', 0, 0, |
19 | 133 "Generate raw binary format output, equivalent of --format=raw"}, |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
134 { "obj", 0, 0, 0, |
19 | 135 "Generate proprietary object file format for later linking, equivalent of --format=obj" }, |
0 | 136 { 0 } |
137 }; | |
138 | |
139 static struct argp argp = | |
140 { | |
141 options, | |
142 parse_opts, | |
143 "<input file>", | |
144 "LWASM, a HD6309 and MC6809 cross-assembler" | |
145 }; | |
146 | |
147 // main function; parse command line, set up assembler state, and run the | |
148 // assembler on the first file | |
149 int main(int argc, char **argv) | |
150 { | |
151 // assembler state | |
152 asmstate_t asmstate = { 0 }; | |
153 | |
154 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
155 if (!asmstate.listfile) | |
156 asmstate.listfile = "-"; | |
157 | |
19 | 158 if (!asmstate.infile) |
159 { | |
160 fprintf(stderr, "No input files specified.\n"); | |
161 exit(1); | |
162 } | |
163 | |
0 | 164 /* pass 1 - collect the symbols and assign addresses where possible */ |
165 /* pass 1 also resolves included files, etc. */ | |
166 /* 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
|
167 lwasm_pass1(&asmstate); |
0 | 168 |
169 // pass 2: actually generate the code; if any phasing errors appear | |
170 // 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
|
171 lwasm_pass2(&asmstate); |
0 | 172 |
173 // now make a pretty listing | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
174 // lwasm_list(&asmstate); |
0 | 175 |
176 // now write the code out to the output file | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
5
diff
changeset
|
177 // lwasm_output(&asmstate); |
0 | 178 |
179 if (asmstate.errorcount > 0) | |
180 exit(1); | |
181 | |
182 exit(0); | |
183 } | |
184 |