annotate lwasm/main.c @ 332:67224d8d1024

Basic input layer works
author lost
date Tue, 02 Mar 2010 00:10:32 +0000
parents 81c005b82775
children 9f58e3bca6e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
1 /*
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
2 main.c
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
3
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
5
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
7
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
11 version.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
12
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
16 more details.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
17
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
20 */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
21
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
22 #include <config.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
23
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
24 #include <argp.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
25 #include <stdio.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
26 #include <stdlib.h>
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
27
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
28 #include <lw_alloc.h>
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
29 #include <lw_string.h>
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
30 #include <lw_stringlist.h>
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
31
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
32 #include "lwasm.h"
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
33 #include "input.h"
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
34
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
35 extern int parse_pragma_string(asmstate_t *as, char *str);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
36
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
37 /* command line option handling */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
38 const char *argp_program_version = "lwasm from " PACKAGE_STRING;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
40 char *program_name;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
41
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
42 static struct argp_option options[] =
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
43 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
44 { "output", 'o', "FILE", 0, "Output to FILE"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
45 { "debug", 'd', 0, 0, "Set debug mode"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
46 { "format", 'f', "TYPE", 0, "Select output format: decb, raw, obj, os9"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
47 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, "Generate list [to FILE]"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
48 { "decb", 'b', 0, 0, "Generate DECB .bin format output, equivalent of --format=decb"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
49 { "raw", 'r', 0, 0, "Generate raw binary format output, equivalent of --format=raw"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
50 { "obj", 0x100, 0, 0, "Generate proprietary object file format for later linking, equivalent of --format=obj" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
51 { "depend", 0x101, 0, 0, "Output a dependency list to stdout; do not do any actual output though assembly is completed as usual" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
52 { "pragma", 'p', "PRAGMA", 0, "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
53 { "6809", '9', 0, 0, "Set assembler to 6809 only mode" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
54 { "6309", '3', 0, 0, "Set assembler to 6309 mode (default)" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
55 { "includedir", 'I', "PATH", 0, "Add entry to include path" },
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
56 { 0 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
57 };
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
58
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
59
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
60 static error_t parse_opts(int key, char *arg, struct argp_state *state)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
61 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
62 asmstate_t *as = state -> input;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
63
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
64 switch (key)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
65 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
66 case 'I':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
67 lw_stringlist_addstring(as -> include_list, arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
68 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
69
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
70 case 'o':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
71 if (as -> output_file)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
72 lw_free(as -> output_file);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
73 as -> output_file = lw_strdup(arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
74 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
75
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
76 case 'd':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
77 as -> debug_level++;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
78 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
79
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
80 case 'l':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
81 if (as -> list_file)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
82 lw_free(as -> list_file);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
83 if (!arg)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
84 as -> list_file = NULL;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
85 else
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
86 as -> list_file = lw_strdup(arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
87 as -> flags |= FLAG_LIST;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
88 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
89
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
90 case 'b':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
91 as -> output_format = OUTPUT_DECB;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
92 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
93
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
94 case 'r':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
95 as -> output_format = OUTPUT_RAW;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
96 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
97
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
98 case 0x100:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
99 as -> output_format = OUTPUT_OBJ;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
100 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
101
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
102 case 0x101:
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
103 as -> flags |= FLAG_DEPEND;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
104 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
105
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
106 case 'f':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
107 if (!strcasecmp(arg, "decb"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
108 as -> output_format = OUTPUT_DECB;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
109 else if (!strcasecmp(arg, "raw"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
110 as -> output_format = OUTPUT_RAW;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
111 else if (!strcasecmp(arg, "obj"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
112 as -> output_format = OUTPUT_OBJ;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
113 else if (!strcasecmp(arg, "os9"))
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
114 {
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
115 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
116 as -> output_format = OUTPUT_OS9;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
117 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
118 else
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
119 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
120 fprintf(stderr, "Invalid output format: %s\n", arg);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
121 exit(1);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
122 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
123 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
124
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
125 case 'p':
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
126 if (parse_pragma_string(as, arg) == 0)
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
127 {
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
128 fprintf(stderr, "Unrecognized pragma string: %s\n", arg);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
129 exit(1);
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
130 }
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
131 break;
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
132
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
133 case '9':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
134 as -> target = TARGET_6809;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
135 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
136
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
137 case '3':
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
138 as -> target = TARGET_6309;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
139 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
140
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
141 case ARGP_KEY_END:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
142 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
143
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
144 case ARGP_KEY_ARG:
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
145 lw_stringlist_addstring(as -> input_files, arg);
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
146 break;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
147
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
148 default:
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
149 return ARGP_ERR_UNKNOWN;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
150 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
151 return 0;
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
152 }
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
153
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
154 static struct argp argp =
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
155 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
156 options,
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
157 parse_opts,
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
158 "<input file>",
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
159 "LWASM, a HD6309 and MC6809 cross-assembler"
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
160 };
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
161
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
162 /*
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
163 main function; parse command line, set up assembler state, and run the
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
164 assembler on the first file
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
165 */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
166 extern void do_pass1(asmstate_t *as);
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
167
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
168 int main(int argc, char **argv)
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
169 {
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
170 /* assembler state */
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
171 asmstate_t asmstate = { 0 };
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
172 program_name = argv[0];
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
173
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
174 /* initialize assembler state */
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
175 asmstate.include_list = lw_stringlist_create();
325
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
176 asmstate.input_files = lw_stringlist_create();
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
177
619fd6ad4ab9 Completed command line parsing
lost
parents: 323
diff changeset
178 /* parse command line arguments */
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
179 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
180
328
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
181 if (!asmstate.output_file)
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
182 {
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
183 asmstate.output_file = lw_strdup("a.out");
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
184 }
591d01b343b9 checkpoint - input layer
lost
parents: 325
diff changeset
185
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
186 input_init(&asmstate);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
187
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
188 do_pass1(&asmstate);
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
189
323
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
190 exit(0);
473ed9b353eb Started framework for lwasm binary
lost
parents:
diff changeset
191 }