annotate src/main.c @ 13:05d4115b4860

Started work on new expression evaluator system and major code re-work for next release
author lost
date Wed, 22 Oct 2008 04:51:16 +0000
parents 287a6905a63c
children 925105ccf22f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
1 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
20
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
21 Implements the program startup code
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
22
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 #include <argp.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 #include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 static error_t parse_opts(int key, char *arg, struct argp_state *state)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47 asmstate_t *as = state -> input;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 switch (key)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 case 'o':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 // output
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 if (as -> outfile)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 as -> outfile = arg;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59 case 'd':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 // debug
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 as -> debug++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 case 'l':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65 // list
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 if (arg)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 as -> listfile = arg;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 as -> listfile = "-";
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 case 'b':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 // decb output
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 as -> outformat = OUTPUT_DECB;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
75 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
76
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 case 'r':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 // raw binary output
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79 as -> outformat = OUTPUT_RAW;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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;
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents: 5
diff changeset
85
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 case ARGP_KEY_END:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 // done; sanity check
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 if (!as -> outfile)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 as -> outfile = "a.out";
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 case ARGP_KEY_ARG:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 // non-option arg
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 if (as -> infile)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 argp_usage(state);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 as -> infile = arg;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99 default:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 return ARGP_ERR_UNKNOWN;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
105 static struct argp_option options[] =
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
107 { "output", 'o', "FILE", 0,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108 "Output to FILE"},
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
109 { "debug", 'd', 0, 0,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 "Set debug mode"},
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
112 "Generate list [to FILE]"},
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
113 { "decb", 'b', 0, 0,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
114 "Generate DECB .bin format output"},
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
115 { "raw", 'r', 0, 0,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
116 "Generate raw binary format output"},
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
117 { "rawrel", 0, 0, 0,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
118 "Generate raw binary respecing ORG statements as offsets from the start of the file"},
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents: 5
diff changeset
119 { "obj", 0, 0, 0,
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents: 5
diff changeset
120 "Generate proprietary object file format for later linking" },
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
121 { 0 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
122 };
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
123
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
124 static struct argp argp =
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
125 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
126 options,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
127 parse_opts,
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
128 "<input file>",
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
129 "LWASM, a HD6309 and MC6809 cross-assembler"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
130 };
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
131
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
132 // main function; parse command line, set up assembler state, and run the
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
133 // assembler on the first file
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
134 int main(int argc, char **argv)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
135 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
136 // assembler state
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
137 asmstate_t asmstate = { 0 };
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
138
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
139 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
140 if (!asmstate.listfile)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
141 asmstate.listfile = "-";
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
142
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
143 /* pass 1 - collect the symbols and assign addresses where possible */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
144 /* pass 1 also resolves included files, etc. */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
145 /* 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
146 lwasm_pass1(&asmstate);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
147
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
148 // pass 2: actually generate the code; if any phasing errors appear
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
149 // 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
150 lwasm_pass2(&asmstate);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
151
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
152 // 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
153 // lwasm_list(&asmstate);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
154
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
155 // 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
156 // lwasm_output(&asmstate);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
157
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
158 if (asmstate.errorcount > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
159 exit(1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
160
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
161 exit(0);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
162 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
163