Mercurial > hg-old > index.cgi
annotate src/main.c @ 4:34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
author | lost |
---|---|
date | Sat, 04 Oct 2008 03:14:20 +0000 |
parents | 57495da01900 |
children | 287a6905a63c |
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 | |
25 #include <argp.h> | |
26 #include <errno.h> | |
27 #include <stdio.h> | |
28 #include <stdlib.h> | |
29 | |
30 #include "lwasm.h" | |
31 | |
32 // external declarations | |
33 extern void resolve_phasing(asmstate_t *as); | |
34 extern void generate_code(asmstate_t *as); | |
35 extern void list_code(asmstate_t *as); | |
36 extern void write_code(asmstate_t *as); | |
37 | |
38 | |
39 // command line option handling | |
40 const char *argp_program_version = "LWASM Version 0.0"; | |
41 const char *argp_program_bug_address = "lost@l-w.ca"; | |
42 | |
43 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
44 { | |
45 asmstate_t *as = state -> input; | |
46 | |
47 switch (key) | |
48 { | |
49 case 'o': | |
50 // output | |
51 if (as -> outfile) | |
52 { | |
53 } | |
54 as -> outfile = arg; | |
55 break; | |
56 | |
57 case 'd': | |
58 // debug | |
59 as -> debug++; | |
60 break; | |
61 | |
62 case 'l': | |
63 // list | |
64 if (arg) | |
65 as -> listfile = arg; | |
66 else | |
67 as -> listfile = "-"; | |
68 break; | |
69 | |
70 case 'b': | |
71 // decb output | |
72 as -> outformat = OUTPUT_DECB; | |
73 break; | |
74 | |
75 case 'r': | |
76 // raw binary output | |
77 as -> outformat = OUTPUT_RAW; | |
78 break; | |
79 | |
80 case ARGP_KEY_END: | |
81 // done; sanity check | |
82 if (!as -> outfile) | |
83 as -> outfile = "a.out"; | |
84 break; | |
85 | |
86 case ARGP_KEY_ARG: | |
87 // non-option arg | |
88 if (as -> infile) | |
89 argp_usage(state); | |
90 as -> infile = arg; | |
91 break; | |
92 | |
93 default: | |
94 return ARGP_ERR_UNKNOWN; | |
95 } | |
96 return 0; | |
97 } | |
98 | |
99 static struct argp_option options[] = | |
100 { | |
101 { "output", 'o', "FILE", 0, | |
102 "Output to FILE"}, | |
103 { "debug", 'd', 0, 0, | |
104 "Set debug mode"}, | |
105 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, | |
106 "Generate list [to FILE]"}, | |
107 { "decb", 'b', 0, 0, | |
108 "Generate DECB .bin format output"}, | |
109 { "raw", 'r', 0, 0, | |
110 "Generate raw binary format output"}, | |
111 { "rawrel", 0, 0, 0, | |
112 "Generate raw binary respecing ORG statements as offsets from the start of the file"}, | |
113 { 0 } | |
114 }; | |
115 | |
116 static struct argp argp = | |
117 { | |
118 options, | |
119 parse_opts, | |
120 "<input file>", | |
121 "LWASM, a HD6309 and MC6809 cross-assembler" | |
122 }; | |
123 | |
124 // main function; parse command line, set up assembler state, and run the | |
125 // assembler on the first file | |
126 int main(int argc, char **argv) | |
127 { | |
128 // assembler state | |
129 asmstate_t asmstate = { 0 }; | |
130 | |
131 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
132 if (!asmstate.listfile) | |
133 asmstate.listfile = "-"; | |
134 | |
135 // printf("Assembling %s to %s; list to %s\n", asmstate.infile, asmstate.outfile, asmstate.listfile); | |
136 | |
137 /* pass 1 - collect the symbols and assign addresses where possible */ | |
138 /* pass 1 also resolves included files, etc. */ | |
139 /* that means files are read exactly once unless included multiple times */ | |
140 lwasm_read_file(&asmstate, (char *)(asmstate.infile)); | |
141 | |
142 // pass 2: actually generate the code; if any phasing errors appear | |
143 // at this stage, we have a bug | |
144 generate_code(&asmstate); | |
145 | |
146 // now make a pretty listing | |
147 list_code(&asmstate); | |
148 | |
149 // now write the code out to the output file | |
150 write_code(&asmstate); | |
151 | |
152 if (asmstate.errorcount > 0) | |
153 exit(1); | |
154 | |
155 exit(0); | |
156 } | |
157 |