aicasm.c revision 1.4 1 1.4 fvdl /* $NetBSD: aicasm.c,v 1.4 2003/04/19 19:26:10 fvdl Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Aic7xxx SCSI host adapter firmware asssembler
5 1.1 fvdl *
6 1.4 fvdl * Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs.
7 1.4 fvdl * Copyright (c) 2001, 2002 Adaptec Inc.
8 1.1 fvdl * All rights reserved.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1 fvdl * modification, are permitted provided that the following conditions
12 1.1 fvdl * are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions, and the following disclaimer,
15 1.4 fvdl * without modification.
16 1.4 fvdl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 1.4 fvdl * substantially similar to the "NO WARRANTY" disclaimer below
18 1.4 fvdl * ("Disclaimer") and any redistribution must be conditioned upon
19 1.4 fvdl * including a substantially similar Disclaimer requirement for further
20 1.4 fvdl * binary redistribution.
21 1.4 fvdl * 3. Neither the names of the above-listed copyright holders nor the names
22 1.4 fvdl * of any contributors may be used to endorse or promote products derived
23 1.4 fvdl * from this software without specific prior written permission.
24 1.1 fvdl *
25 1.4 fvdl * Alternatively, this software may be distributed under the terms of the
26 1.4 fvdl * GNU General Public License ("GPL") version 2 as published by the Free
27 1.4 fvdl * Software Foundation.
28 1.4 fvdl *
29 1.4 fvdl * NO WARRANTY
30 1.4 fvdl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 1.4 fvdl * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 1.4 fvdl * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 1.4 fvdl * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 1.4 fvdl * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.4 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.4 fvdl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 1.4 fvdl * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.4 fvdl * POSSIBILITY OF SUCH DAMAGES.
41 1.1 fvdl *
42 1.4 fvdl * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm.c,v 1.35 2002/08/31 06:39:40 gibbs Exp $
43 1.1 fvdl */
44 1.1 fvdl #include <sys/types.h>
45 1.1 fvdl #include <sys/mman.h>
46 1.1 fvdl
47 1.1 fvdl #include <ctype.h>
48 1.4 fvdl #include <inttypes.h>
49 1.4 fvdl #include <regex.h>
50 1.1 fvdl #include <stdio.h>
51 1.1 fvdl #include <stdlib.h>
52 1.1 fvdl #include <string.h>
53 1.1 fvdl #include <sysexits.h>
54 1.1 fvdl #include <unistd.h>
55 1.1 fvdl
56 1.4 fvdl #if linux
57 1.4 fvdl #include <endian.h>
58 1.4 fvdl #else
59 1.4 fvdl #include <machine/endian.h>
60 1.4 fvdl #endif
61 1.4 fvdl
62 1.1 fvdl #include "aicasm.h"
63 1.1 fvdl #include "aicasm_symbol.h"
64 1.4 fvdl #include "aicasm_insformat.h"
65 1.1 fvdl
66 1.1 fvdl typedef struct patch {
67 1.1 fvdl STAILQ_ENTRY(patch) links;
68 1.1 fvdl int patch_func;
69 1.1 fvdl u_int begin;
70 1.1 fvdl u_int skip_instr;
71 1.1 fvdl u_int skip_patch;
72 1.1 fvdl } patch_t;
73 1.1 fvdl
74 1.1 fvdl STAILQ_HEAD(patch_list, patch) patches;
75 1.1 fvdl
76 1.1 fvdl static void usage(void);
77 1.1 fvdl static void back_patch(void);
78 1.4 fvdl static void output_code(void);
79 1.4 fvdl static void output_listing(char *ifilename);
80 1.1 fvdl static void dump_scope(scope_t *scope);
81 1.1 fvdl static void emit_patch(scope_t *scope, int patch);
82 1.1 fvdl static int check_patch(patch_t **start_patch, int start_instr,
83 1.1 fvdl int *skip_addr, int *func_vals);
84 1.1 fvdl
85 1.1 fvdl struct path_list search_path;
86 1.1 fvdl int includes_search_curdir;
87 1.1 fvdl char *appname;
88 1.4 fvdl char *stock_include_file;
89 1.1 fvdl FILE *ofile;
90 1.1 fvdl char *ofilename;
91 1.1 fvdl char *regfilename;
92 1.1 fvdl FILE *regfile;
93 1.1 fvdl char *listfilename;
94 1.1 fvdl FILE *listfile;
95 1.4 fvdl char *regdiagfilename;
96 1.4 fvdl FILE *regdiagfile;
97 1.4 fvdl int src_mode;
98 1.4 fvdl int dst_mode;
99 1.1 fvdl
100 1.1 fvdl static STAILQ_HEAD(,instruction) seq_program;
101 1.4 fvdl struct cs_tailq cs_tailq;
102 1.1 fvdl struct scope_list scope_stack;
103 1.1 fvdl symlist_t patch_functions;
104 1.1 fvdl
105 1.1 fvdl #if DEBUG
106 1.1 fvdl extern int yy_flex_debug;
107 1.4 fvdl extern int mm_flex_debug;
108 1.1 fvdl extern int yydebug;
109 1.4 fvdl extern int mmdebug;
110 1.1 fvdl #endif
111 1.1 fvdl extern FILE *yyin;
112 1.4 fvdl extern int yyparse(void);
113 1.4 fvdl
114 1.4 fvdl int main(int argc, char *argv[]);
115 1.1 fvdl
116 1.1 fvdl int
117 1.4 fvdl main(int argc, char *argv[])
118 1.1 fvdl {
119 1.1 fvdl extern char *optarg;
120 1.1 fvdl extern int optind;
121 1.1 fvdl int ch;
122 1.1 fvdl int retval;
123 1.1 fvdl char *inputfilename;
124 1.1 fvdl scope_t *sentinal;
125 1.1 fvdl
126 1.1 fvdl STAILQ_INIT(&patches);
127 1.1 fvdl SLIST_INIT(&search_path);
128 1.1 fvdl STAILQ_INIT(&seq_program);
129 1.4 fvdl TAILQ_INIT(&cs_tailq);
130 1.1 fvdl SLIST_INIT(&scope_stack);
131 1.1 fvdl
132 1.1 fvdl /* Set Sentinal scope node */
133 1.1 fvdl sentinal = scope_alloc();
134 1.1 fvdl sentinal->type = SCOPE_ROOT;
135 1.1 fvdl
136 1.1 fvdl includes_search_curdir = 1;
137 1.1 fvdl appname = *argv;
138 1.1 fvdl regfile = NULL;
139 1.1 fvdl listfile = NULL;
140 1.1 fvdl #if DEBUG
141 1.1 fvdl yy_flex_debug = 0;
142 1.4 fvdl mm_flex_debug = 0;
143 1.1 fvdl yydebug = 0;
144 1.4 fvdl mmdebug = 0;
145 1.1 fvdl #endif
146 1.4 fvdl while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:")) != -1) {
147 1.1 fvdl switch(ch) {
148 1.1 fvdl case 'd':
149 1.1 fvdl #if DEBUG
150 1.1 fvdl if (strcmp(optarg, "s") == 0) {
151 1.1 fvdl yy_flex_debug = 1;
152 1.4 fvdl mm_flex_debug = 1;
153 1.1 fvdl } else if (strcmp(optarg, "p") == 0) {
154 1.1 fvdl yydebug = 1;
155 1.4 fvdl mmdebug = 1;
156 1.1 fvdl } else {
157 1.1 fvdl fprintf(stderr, "%s: -d Requires either an "
158 1.1 fvdl "'s' or 'p' argument\n", appname);
159 1.1 fvdl usage();
160 1.1 fvdl }
161 1.1 fvdl #else
162 1.1 fvdl stop("-d: Assembler not built with debugging "
163 1.1 fvdl "information", EX_SOFTWARE);
164 1.1 fvdl #endif
165 1.1 fvdl break;
166 1.4 fvdl case 'i':
167 1.4 fvdl stock_include_file = optarg;
168 1.4 fvdl break;
169 1.1 fvdl case 'l':
170 1.1 fvdl /* Create a program listing */
171 1.1 fvdl if ((listfile = fopen(optarg, "w")) == NULL) {
172 1.1 fvdl perror(optarg);
173 1.1 fvdl stop(NULL, EX_CANTCREAT);
174 1.1 fvdl }
175 1.1 fvdl listfilename = optarg;
176 1.1 fvdl break;
177 1.1 fvdl case 'n':
178 1.1 fvdl /* Don't complain about the -nostdinc directrive */
179 1.1 fvdl if (strcmp(optarg, "ostdinc")) {
180 1.1 fvdl fprintf(stderr, "%s: Unknown option -%c%s\n",
181 1.1 fvdl appname, ch, optarg);
182 1.1 fvdl usage();
183 1.1 fvdl /* NOTREACHED */
184 1.1 fvdl }
185 1.1 fvdl break;
186 1.1 fvdl case 'o':
187 1.1 fvdl if ((ofile = fopen(optarg, "w")) == NULL) {
188 1.1 fvdl perror(optarg);
189 1.1 fvdl stop(NULL, EX_CANTCREAT);
190 1.1 fvdl }
191 1.1 fvdl ofilename = optarg;
192 1.1 fvdl break;
193 1.4 fvdl case 'p':
194 1.4 fvdl /* Create Register Diagnostic "printing" Functions */
195 1.4 fvdl if ((regdiagfile = fopen(optarg, "w")) == NULL) {
196 1.4 fvdl perror(optarg);
197 1.4 fvdl stop(NULL, EX_CANTCREAT);
198 1.4 fvdl }
199 1.4 fvdl regdiagfilename = optarg;
200 1.4 fvdl break;
201 1.1 fvdl case 'r':
202 1.1 fvdl if ((regfile = fopen(optarg, "w")) == NULL) {
203 1.1 fvdl perror(optarg);
204 1.1 fvdl stop(NULL, EX_CANTCREAT);
205 1.1 fvdl }
206 1.1 fvdl regfilename = optarg;
207 1.1 fvdl break;
208 1.1 fvdl case 'I':
209 1.1 fvdl {
210 1.1 fvdl path_entry_t include_dir;
211 1.1 fvdl
212 1.1 fvdl if (strcmp(optarg, "-") == 0) {
213 1.1 fvdl if (includes_search_curdir == 0) {
214 1.1 fvdl fprintf(stderr, "%s: Warning - '-I-' "
215 1.1 fvdl "specified multiple "
216 1.1 fvdl "times\n", appname);
217 1.1 fvdl }
218 1.1 fvdl includes_search_curdir = 0;
219 1.1 fvdl for (include_dir = SLIST_FIRST(&search_path);
220 1.1 fvdl include_dir != NULL;
221 1.4 fvdl include_dir = SLIST_NEXT(include_dir,
222 1.4 fvdl links))
223 1.1 fvdl /*
224 1.1 fvdl * All entries before a '-I-' only
225 1.1 fvdl * apply to includes specified with
226 1.1 fvdl * quotes instead of "<>".
227 1.1 fvdl */
228 1.1 fvdl include_dir->quoted_includes_only = 1;
229 1.1 fvdl } else {
230 1.1 fvdl include_dir =
231 1.1 fvdl (path_entry_t)malloc(sizeof(*include_dir));
232 1.1 fvdl if (include_dir == NULL) {
233 1.1 fvdl perror(optarg);
234 1.1 fvdl stop(NULL, EX_OSERR);
235 1.1 fvdl }
236 1.1 fvdl include_dir->directory = strdup(optarg);
237 1.1 fvdl if (include_dir->directory == NULL) {
238 1.1 fvdl perror(optarg);
239 1.1 fvdl stop(NULL, EX_OSERR);
240 1.1 fvdl }
241 1.1 fvdl include_dir->quoted_includes_only = 0;
242 1.1 fvdl SLIST_INSERT_HEAD(&search_path, include_dir,
243 1.1 fvdl links);
244 1.1 fvdl }
245 1.1 fvdl break;
246 1.1 fvdl }
247 1.1 fvdl case '?':
248 1.1 fvdl default:
249 1.1 fvdl usage();
250 1.1 fvdl /* NOTREACHED */
251 1.1 fvdl }
252 1.1 fvdl }
253 1.1 fvdl argc -= optind;
254 1.1 fvdl argv += optind;
255 1.1 fvdl
256 1.1 fvdl if (argc != 1) {
257 1.1 fvdl fprintf(stderr, "%s: No input file specifiled\n", appname);
258 1.1 fvdl usage();
259 1.1 fvdl /* NOTREACHED */
260 1.1 fvdl }
261 1.1 fvdl
262 1.4 fvdl if (regdiagfile != NULL
263 1.4 fvdl && (regfile == NULL || stock_include_file == NULL)) {
264 1.4 fvdl fprintf(stderr,
265 1.4 fvdl "%s: The -p option requires the -r and -i options.\n",
266 1.4 fvdl appname);
267 1.4 fvdl usage();
268 1.4 fvdl /* NOTREACHED */
269 1.4 fvdl }
270 1.1 fvdl symtable_open();
271 1.1 fvdl inputfilename = *argv;
272 1.1 fvdl include_file(*argv, SOURCE_FILE);
273 1.1 fvdl retval = yyparse();
274 1.1 fvdl if (retval == 0) {
275 1.1 fvdl if (SLIST_FIRST(&scope_stack) == NULL
276 1.1 fvdl || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
277 1.4 fvdl stop("Unterminated conditional expression", EX_DATAERR);
278 1.1 fvdl /* NOTREACHED */
279 1.1 fvdl }
280 1.1 fvdl
281 1.1 fvdl /* Process outmost scope */
282 1.1 fvdl process_scope(SLIST_FIRST(&scope_stack));
283 1.1 fvdl /*
284 1.1 fvdl * Decend the tree of scopes and insert/emit
285 1.1 fvdl * patches as appropriate. We perform a depth first
286 1.1 fvdl * tranversal, recursively handling each scope.
287 1.1 fvdl */
288 1.1 fvdl /* start at the root scope */
289 1.1 fvdl dump_scope(SLIST_FIRST(&scope_stack));
290 1.1 fvdl
291 1.1 fvdl /* Patch up forward jump addresses */
292 1.1 fvdl back_patch();
293 1.1 fvdl
294 1.1 fvdl if (ofile != NULL)
295 1.4 fvdl output_code();
296 1.4 fvdl if (regfile != NULL)
297 1.4 fvdl symtable_dump(regfile, regdiagfile);
298 1.1 fvdl if (listfile != NULL)
299 1.4 fvdl output_listing(inputfilename);
300 1.1 fvdl }
301 1.1 fvdl
302 1.1 fvdl stop(NULL, 0);
303 1.1 fvdl /* NOTREACHED */
304 1.1 fvdl return (0);
305 1.1 fvdl }
306 1.1 fvdl
307 1.1 fvdl static void
308 1.1 fvdl usage()
309 1.1 fvdl {
310 1.1 fvdl
311 1.1 fvdl (void)fprintf(stderr,
312 1.4 fvdl "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
313 1.4 fvdl " [-r register_output_file [-p register_diag_file -i includefile]]\n"
314 1.4 fvdl " [-l program_list_file]\n"
315 1.4 fvdl " input_file\n", appname);
316 1.1 fvdl exit(EX_USAGE);
317 1.1 fvdl }
318 1.1 fvdl
319 1.1 fvdl static void
320 1.1 fvdl back_patch()
321 1.1 fvdl {
322 1.1 fvdl struct instruction *cur_instr;
323 1.1 fvdl
324 1.4 fvdl for (cur_instr = STAILQ_FIRST(&seq_program);
325 1.4 fvdl cur_instr != NULL;
326 1.4 fvdl cur_instr = STAILQ_NEXT(cur_instr, links)) {
327 1.1 fvdl if (cur_instr->patch_label != NULL) {
328 1.1 fvdl struct ins_format3 *f3_instr;
329 1.1 fvdl u_int address;
330 1.1 fvdl
331 1.1 fvdl if (cur_instr->patch_label->type != LABEL) {
332 1.1 fvdl char buf[255];
333 1.1 fvdl
334 1.1 fvdl snprintf(buf, sizeof(buf),
335 1.1 fvdl "Undefined label %s",
336 1.1 fvdl cur_instr->patch_label->name);
337 1.1 fvdl stop(buf, EX_DATAERR);
338 1.1 fvdl /* NOTREACHED */
339 1.1 fvdl }
340 1.1 fvdl f3_instr = &cur_instr->format.format3;
341 1.1 fvdl address = f3_instr->address;
342 1.1 fvdl address += cur_instr->patch_label->info.linfo->address;
343 1.1 fvdl f3_instr->address = address;
344 1.1 fvdl }
345 1.1 fvdl }
346 1.1 fvdl }
347 1.1 fvdl
348 1.1 fvdl static void
349 1.4 fvdl output_code()
350 1.1 fvdl {
351 1.1 fvdl struct instruction *cur_instr;
352 1.1 fvdl patch_t *cur_patch;
353 1.4 fvdl critical_section_t *cs;
354 1.1 fvdl symbol_node_t *cur_node;
355 1.1 fvdl int instrcount;
356 1.1 fvdl
357 1.1 fvdl instrcount = 0;
358 1.1 fvdl fprintf(ofile,
359 1.4 fvdl "/*\n"
360 1.4 fvdl " * DO NOT EDIT - This file is automatically generated\n"
361 1.4 fvdl " * from the following source files:\n"
362 1.4 fvdl " *\n"
363 1.4 fvdl "%s */\n", versions);
364 1.4 fvdl
365 1.4 fvdl fprintf(ofile, "static uint8_t seqprog[] = {\n");
366 1.4 fvdl for (cur_instr = STAILQ_FIRST(&seq_program);
367 1.4 fvdl cur_instr != NULL;
368 1.4 fvdl cur_instr = STAILQ_NEXT(cur_instr, links)) {
369 1.4 fvdl
370 1.4 fvdl fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
371 1.4 fvdl cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
372 1.4 fvdl #if BYTE_ORDER == LITTLE_ENDIAN
373 1.4 fvdl cur_instr->format.bytes[0],
374 1.4 fvdl cur_instr->format.bytes[1],
375 1.4 fvdl cur_instr->format.bytes[2],
376 1.4 fvdl cur_instr->format.bytes[3]);
377 1.4 fvdl #else
378 1.1 fvdl cur_instr->format.bytes[3],
379 1.1 fvdl cur_instr->format.bytes[2],
380 1.1 fvdl cur_instr->format.bytes[1],
381 1.1 fvdl cur_instr->format.bytes[0]);
382 1.1 fvdl #endif
383 1.1 fvdl instrcount++;
384 1.1 fvdl }
385 1.4 fvdl fprintf(ofile, "\n};\n\n");
386 1.4 fvdl
387 1.4 fvdl if (patch_arg_list == NULL)
388 1.4 fvdl stop("Patch argument list not defined",
389 1.4 fvdl EX_DATAERR);
390 1.1 fvdl
391 1.1 fvdl /*
392 1.1 fvdl * Output patch information. Patch functions first.
393 1.1 fvdl */
394 1.4 fvdl fprintf(ofile,
395 1.4 fvdl "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
396 1.4 fvdl
397 1.4 fvdl for (cur_node = SLIST_FIRST(&patch_functions);
398 1.4 fvdl cur_node != NULL;
399 1.4 fvdl cur_node = SLIST_NEXT(cur_node,links)) {
400 1.1 fvdl fprintf(ofile,
401 1.4 fvdl "static %spatch_func_t %spatch%d_func;\n"
402 1.4 fvdl "\n"
403 1.4 fvdl "static int\n"
404 1.4 fvdl "%spatch%d_func(%s)\n"
405 1.4 fvdl "{\n"
406 1.4 fvdl " return (%s);\n"
407 1.4 fvdl "}\n\n",
408 1.4 fvdl prefix,
409 1.4 fvdl prefix,
410 1.1 fvdl cur_node->symbol->info.condinfo->func_num,
411 1.4 fvdl prefix,
412 1.1 fvdl cur_node->symbol->info.condinfo->func_num,
413 1.4 fvdl patch_arg_list,
414 1.1 fvdl cur_node->symbol->name);
415 1.1 fvdl }
416 1.1 fvdl
417 1.1 fvdl fprintf(ofile,
418 1.4 fvdl "static struct patch {\n"
419 1.4 fvdl " %spatch_func_t *patch_func;\n"
420 1.4 fvdl " uint32_t begin :10,\n"
421 1.4 fvdl " skip_instr :10,\n"
422 1.4 fvdl " skip_patch :12;\n"
423 1.4 fvdl "} patches[] = {\n", prefix);
424 1.4 fvdl
425 1.4 fvdl for (cur_patch = STAILQ_FIRST(&patches);
426 1.4 fvdl cur_patch != NULL;
427 1.4 fvdl cur_patch = STAILQ_NEXT(cur_patch,links)) {
428 1.4 fvdl fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
429 1.4 fvdl cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
430 1.4 fvdl prefix,
431 1.1 fvdl cur_patch->patch_func, cur_patch->begin,
432 1.1 fvdl cur_patch->skip_instr, cur_patch->skip_patch);
433 1.1 fvdl }
434 1.1 fvdl
435 1.4 fvdl fprintf(ofile, "\n};\n\n");
436 1.4 fvdl
437 1.4 fvdl fprintf(ofile,
438 1.4 fvdl "static struct cs {\n"
439 1.4 fvdl " uint16_t begin;\n"
440 1.4 fvdl " uint16_t end;\n"
441 1.4 fvdl "} critical_sections[] = {\n");
442 1.4 fvdl
443 1.4 fvdl for (cs = TAILQ_FIRST(&cs_tailq);
444 1.4 fvdl cs != NULL;
445 1.4 fvdl cs = TAILQ_NEXT(cs, links)) {
446 1.4 fvdl fprintf(ofile, "%s\t{ %d, %d }",
447 1.4 fvdl cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
448 1.4 fvdl cs->begin_addr, cs->end_addr);
449 1.4 fvdl }
450 1.4 fvdl
451 1.4 fvdl fprintf(ofile, "\n};\n\n");
452 1.4 fvdl
453 1.4 fvdl fprintf(ofile,
454 1.4 fvdl "static const int num_critical_sections = sizeof(critical_sections)\n"
455 1.4 fvdl " / sizeof(*critical_sections);\n");
456 1.1 fvdl
457 1.1 fvdl fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
458 1.1 fvdl }
459 1.1 fvdl
460 1.1 fvdl static void
461 1.1 fvdl dump_scope(scope_t *scope)
462 1.1 fvdl {
463 1.1 fvdl scope_t *cur_scope;
464 1.1 fvdl
465 1.1 fvdl /*
466 1.1 fvdl * Emit the first patch for this scope
467 1.1 fvdl */
468 1.1 fvdl emit_patch(scope, 0);
469 1.1 fvdl
470 1.1 fvdl /*
471 1.1 fvdl * Dump each scope within this one.
472 1.1 fvdl */
473 1.1 fvdl cur_scope = TAILQ_FIRST(&scope->inner_scope);
474 1.1 fvdl
475 1.1 fvdl while (cur_scope != NULL) {
476 1.1 fvdl
477 1.1 fvdl dump_scope(cur_scope);
478 1.1 fvdl
479 1.1 fvdl cur_scope = TAILQ_NEXT(cur_scope, scope_links);
480 1.1 fvdl }
481 1.1 fvdl
482 1.1 fvdl /*
483 1.1 fvdl * Emit the second, closing, patch for this scope
484 1.1 fvdl */
485 1.1 fvdl emit_patch(scope, 1);
486 1.1 fvdl }
487 1.1 fvdl
488 1.1 fvdl void
489 1.1 fvdl emit_patch(scope_t *scope, int patch)
490 1.1 fvdl {
491 1.1 fvdl patch_info_t *pinfo;
492 1.1 fvdl patch_t *new_patch;
493 1.1 fvdl
494 1.1 fvdl pinfo = &scope->patches[patch];
495 1.1 fvdl
496 1.1 fvdl if (pinfo->skip_instr == 0)
497 1.1 fvdl /* No-Op patch */
498 1.1 fvdl return;
499 1.1 fvdl
500 1.1 fvdl new_patch = (patch_t *)malloc(sizeof(*new_patch));
501 1.1 fvdl
502 1.1 fvdl if (new_patch == NULL)
503 1.1 fvdl stop("Could not malloc patch structure", EX_OSERR);
504 1.1 fvdl
505 1.1 fvdl memset(new_patch, 0, sizeof(*new_patch));
506 1.1 fvdl
507 1.1 fvdl if (patch == 0) {
508 1.1 fvdl new_patch->patch_func = scope->func_num;
509 1.1 fvdl new_patch->begin = scope->begin_addr;
510 1.1 fvdl } else {
511 1.1 fvdl new_patch->patch_func = 0;
512 1.1 fvdl new_patch->begin = scope->end_addr;
513 1.1 fvdl }
514 1.1 fvdl new_patch->skip_instr = pinfo->skip_instr;
515 1.1 fvdl new_patch->skip_patch = pinfo->skip_patch;
516 1.1 fvdl STAILQ_INSERT_TAIL(&patches, new_patch, links);
517 1.1 fvdl }
518 1.1 fvdl
519 1.1 fvdl void
520 1.4 fvdl output_listing(char *ifilename)
521 1.1 fvdl {
522 1.1 fvdl char buf[1024];
523 1.1 fvdl FILE *ifile;
524 1.1 fvdl struct instruction *cur_instr;
525 1.1 fvdl patch_t *cur_patch;
526 1.1 fvdl symbol_node_t *cur_func;
527 1.1 fvdl int *func_values;
528 1.1 fvdl int instrcount;
529 1.1 fvdl int instrptr;
530 1.1 fvdl int line;
531 1.1 fvdl int func_count;
532 1.1 fvdl int skip_addr;
533 1.1 fvdl
534 1.1 fvdl instrcount = 0;
535 1.1 fvdl instrptr = 0;
536 1.1 fvdl line = 1;
537 1.1 fvdl skip_addr = 0;
538 1.1 fvdl if ((ifile = fopen(ifilename, "r")) == NULL) {
539 1.1 fvdl perror(ifilename);
540 1.1 fvdl stop(NULL, EX_DATAERR);
541 1.1 fvdl }
542 1.1 fvdl
543 1.1 fvdl /*
544 1.1 fvdl * Determine which options to apply to this listing.
545 1.1 fvdl */
546 1.1 fvdl for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
547 1.1 fvdl cur_func != NULL;
548 1.1 fvdl cur_func = SLIST_NEXT(cur_func, links))
549 1.1 fvdl func_count++;
550 1.1 fvdl
551 1.4 fvdl func_values = NULL;
552 1.1 fvdl if (func_count != 0) {
553 1.1 fvdl func_values = (int *)malloc(func_count * sizeof(int));
554 1.1 fvdl
555 1.1 fvdl if (func_values == NULL)
556 1.1 fvdl stop("Could not malloc", EX_OSERR);
557 1.1 fvdl
558 1.1 fvdl func_values[0] = 0; /* FALSE func */
559 1.1 fvdl func_count--;
560 1.1 fvdl
561 1.1 fvdl /*
562 1.1 fvdl * Ask the user to fill in the return values for
563 1.1 fvdl * the rest of the functions.
564 1.1 fvdl */
565 1.1 fvdl
566 1.1 fvdl
567 1.1 fvdl for (cur_func = SLIST_FIRST(&patch_functions);
568 1.1 fvdl cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
569 1.1 fvdl cur_func = SLIST_NEXT(cur_func, links), func_count--) {
570 1.1 fvdl int input;
571 1.1 fvdl
572 1.1 fvdl fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
573 1.1 fvdl fprintf(stdout,
574 1.1 fvdl "Enter the return value for "
575 1.1 fvdl "this expression[T/F]:");
576 1.1 fvdl
577 1.1 fvdl while (1) {
578 1.1 fvdl
579 1.1 fvdl input = getchar();
580 1.1 fvdl input = toupper(input);
581 1.1 fvdl
582 1.1 fvdl if (input == 'T') {
583 1.1 fvdl func_values[func_count] = 1;
584 1.1 fvdl break;
585 1.1 fvdl } else if (input == 'F') {
586 1.1 fvdl func_values[func_count] = 0;
587 1.1 fvdl break;
588 1.1 fvdl }
589 1.1 fvdl }
590 1.1 fvdl if (isatty(fileno(stdin)) == 0)
591 1.1 fvdl putchar(input);
592 1.1 fvdl }
593 1.1 fvdl fprintf(stdout, "\nThanks!\n");
594 1.1 fvdl }
595 1.1 fvdl
596 1.1 fvdl /* Now output the listing */
597 1.1 fvdl cur_patch = STAILQ_FIRST(&patches);
598 1.4 fvdl for (cur_instr = STAILQ_FIRST(&seq_program);
599 1.4 fvdl cur_instr != NULL;
600 1.4 fvdl cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
601 1.1 fvdl
602 1.1 fvdl if (check_patch(&cur_patch, instrcount,
603 1.1 fvdl &skip_addr, func_values) == 0) {
604 1.1 fvdl /* Don't count this instruction as it is in a patch
605 1.1 fvdl * that was removed.
606 1.1 fvdl */
607 1.1 fvdl continue;
608 1.1 fvdl }
609 1.1 fvdl
610 1.1 fvdl while (line < cur_instr->srcline) {
611 1.1 fvdl fgets(buf, sizeof(buf), ifile);
612 1.1 fvdl fprintf(listfile, "\t\t%s", buf);
613 1.1 fvdl line++;
614 1.1 fvdl }
615 1.1 fvdl fprintf(listfile, "%03x %02x%02x%02x%02x", instrptr,
616 1.4 fvdl #if BYTE_ORDER == LITTLE_ENDIAN
617 1.1 fvdl cur_instr->format.bytes[0],
618 1.1 fvdl cur_instr->format.bytes[1],
619 1.1 fvdl cur_instr->format.bytes[2],
620 1.1 fvdl cur_instr->format.bytes[3]);
621 1.4 fvdl #else
622 1.4 fvdl cur_instr->format.bytes[3],
623 1.4 fvdl cur_instr->format.bytes[2],
624 1.4 fvdl cur_instr->format.bytes[1],
625 1.4 fvdl cur_instr->format.bytes[0]);
626 1.4 fvdl #endif
627 1.1 fvdl fgets(buf, sizeof(buf), ifile);
628 1.1 fvdl fprintf(listfile, "\t%s", buf);
629 1.1 fvdl line++;
630 1.1 fvdl instrptr++;
631 1.1 fvdl }
632 1.1 fvdl /* Dump the remainder of the file */
633 1.1 fvdl while(fgets(buf, sizeof(buf), ifile) != NULL)
634 1.1 fvdl fprintf(listfile, "\t\t%s", buf);
635 1.1 fvdl
636 1.1 fvdl fclose(ifile);
637 1.1 fvdl }
638 1.1 fvdl
639 1.1 fvdl static int
640 1.1 fvdl check_patch(patch_t **start_patch, int start_instr,
641 1.1 fvdl int *skip_addr, int *func_vals)
642 1.1 fvdl {
643 1.1 fvdl patch_t *cur_patch;
644 1.1 fvdl
645 1.1 fvdl cur_patch = *start_patch;
646 1.1 fvdl
647 1.1 fvdl while (cur_patch != NULL && start_instr == cur_patch->begin) {
648 1.1 fvdl if (func_vals[cur_patch->patch_func] == 0) {
649 1.1 fvdl int skip;
650 1.1 fvdl
651 1.1 fvdl /* Start rejecting code */
652 1.1 fvdl *skip_addr = start_instr + cur_patch->skip_instr;
653 1.1 fvdl for (skip = cur_patch->skip_patch;
654 1.1 fvdl skip > 0 && cur_patch != NULL;
655 1.1 fvdl skip--)
656 1.1 fvdl cur_patch = STAILQ_NEXT(cur_patch, links);
657 1.1 fvdl } else {
658 1.1 fvdl /* Accepted this patch. Advance to the next
659 1.1 fvdl * one and wait for our intruction pointer to
660 1.1 fvdl * hit this point.
661 1.1 fvdl */
662 1.1 fvdl cur_patch = STAILQ_NEXT(cur_patch, links);
663 1.1 fvdl }
664 1.1 fvdl }
665 1.1 fvdl
666 1.1 fvdl *start_patch = cur_patch;
667 1.1 fvdl if (start_instr < *skip_addr)
668 1.1 fvdl /* Still skipping */
669 1.1 fvdl return (0);
670 1.1 fvdl
671 1.1 fvdl return (1);
672 1.1 fvdl }
673 1.1 fvdl
674 1.1 fvdl /*
675 1.1 fvdl * Print out error information if appropriate, and clean up before
676 1.1 fvdl * terminating the program.
677 1.1 fvdl */
678 1.1 fvdl void
679 1.4 fvdl stop(const char *string, int err_code)
680 1.1 fvdl {
681 1.1 fvdl if (string != NULL) {
682 1.1 fvdl fprintf(stderr, "%s: ", appname);
683 1.1 fvdl if (yyfilename != NULL) {
684 1.1 fvdl fprintf(stderr, "Stopped at file %s, line %d - ",
685 1.1 fvdl yyfilename, yylineno);
686 1.1 fvdl }
687 1.1 fvdl fprintf(stderr, "%s\n", string);
688 1.1 fvdl }
689 1.1 fvdl
690 1.1 fvdl if (ofile != NULL) {
691 1.1 fvdl fclose(ofile);
692 1.1 fvdl if (err_code != 0) {
693 1.1 fvdl fprintf(stderr, "%s: Removing %s due to error\n",
694 1.1 fvdl appname, ofilename);
695 1.1 fvdl unlink(ofilename);
696 1.1 fvdl }
697 1.1 fvdl }
698 1.1 fvdl
699 1.1 fvdl if (regfile != NULL) {
700 1.1 fvdl fclose(regfile);
701 1.1 fvdl if (err_code != 0) {
702 1.1 fvdl fprintf(stderr, "%s: Removing %s due to error\n",
703 1.1 fvdl appname, regfilename);
704 1.1 fvdl unlink(regfilename);
705 1.1 fvdl }
706 1.1 fvdl }
707 1.1 fvdl
708 1.1 fvdl if (listfile != NULL) {
709 1.1 fvdl fclose(listfile);
710 1.1 fvdl if (err_code != 0) {
711 1.1 fvdl fprintf(stderr, "%s: Removing %s due to error\n",
712 1.1 fvdl appname, listfilename);
713 1.1 fvdl unlink(listfilename);
714 1.1 fvdl }
715 1.1 fvdl }
716 1.1 fvdl
717 1.1 fvdl symlist_free(&patch_functions);
718 1.1 fvdl symtable_close();
719 1.1 fvdl
720 1.1 fvdl exit(err_code);
721 1.1 fvdl }
722 1.1 fvdl
723 1.1 fvdl struct instruction *
724 1.1 fvdl seq_alloc()
725 1.1 fvdl {
726 1.1 fvdl struct instruction *new_instr;
727 1.1 fvdl
728 1.1 fvdl new_instr = (struct instruction *)malloc(sizeof(struct instruction));
729 1.1 fvdl if (new_instr == NULL)
730 1.1 fvdl stop("Unable to malloc instruction object", EX_SOFTWARE);
731 1.1 fvdl memset(new_instr, 0, sizeof(*new_instr));
732 1.1 fvdl STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
733 1.1 fvdl new_instr->srcline = yylineno;
734 1.1 fvdl return new_instr;
735 1.4 fvdl }
736 1.4 fvdl
737 1.4 fvdl critical_section_t *
738 1.4 fvdl cs_alloc()
739 1.4 fvdl {
740 1.4 fvdl critical_section_t *new_cs;
741 1.4 fvdl
742 1.4 fvdl new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
743 1.4 fvdl if (new_cs == NULL)
744 1.4 fvdl stop("Unable to malloc critical_section object", EX_SOFTWARE);
745 1.4 fvdl memset(new_cs, 0, sizeof(*new_cs));
746 1.4 fvdl
747 1.4 fvdl TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
748 1.4 fvdl return new_cs;
749 1.1 fvdl }
750 1.1 fvdl
751 1.1 fvdl scope_t *
752 1.1 fvdl scope_alloc()
753 1.1 fvdl {
754 1.1 fvdl scope_t *new_scope;
755 1.1 fvdl
756 1.1 fvdl new_scope = (scope_t *)malloc(sizeof(scope_t));
757 1.1 fvdl if (new_scope == NULL)
758 1.1 fvdl stop("Unable to malloc scope object", EX_SOFTWARE);
759 1.1 fvdl memset(new_scope, 0, sizeof(*new_scope));
760 1.1 fvdl TAILQ_INIT(&new_scope->inner_scope);
761 1.1 fvdl
762 1.1 fvdl if (SLIST_FIRST(&scope_stack) != NULL) {
763 1.1 fvdl TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
764 1.1 fvdl new_scope, scope_links);
765 1.1 fvdl }
766 1.1 fvdl /* This patch is now the current scope */
767 1.1 fvdl SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
768 1.1 fvdl return new_scope;
769 1.1 fvdl }
770 1.1 fvdl
771 1.1 fvdl void
772 1.1 fvdl process_scope(scope_t *scope)
773 1.1 fvdl {
774 1.1 fvdl /*
775 1.1 fvdl * We are "leaving" this scope. We should now have
776 1.1 fvdl * enough information to process the lists of scopes
777 1.1 fvdl * we encapsulate.
778 1.1 fvdl */
779 1.1 fvdl scope_t *cur_scope;
780 1.1 fvdl u_int skip_patch_count;
781 1.1 fvdl u_int skip_instr_count;
782 1.1 fvdl
783 1.1 fvdl cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
784 1.1 fvdl skip_patch_count = 0;
785 1.1 fvdl skip_instr_count = 0;
786 1.1 fvdl while (cur_scope != NULL) {
787 1.1 fvdl u_int patch0_patch_skip;
788 1.1 fvdl
789 1.1 fvdl patch0_patch_skip = 0;
790 1.1 fvdl switch (cur_scope->type) {
791 1.1 fvdl case SCOPE_IF:
792 1.1 fvdl case SCOPE_ELSE_IF:
793 1.1 fvdl if (skip_instr_count != 0) {
794 1.1 fvdl /* Create a tail patch */
795 1.1 fvdl patch0_patch_skip++;
796 1.1 fvdl cur_scope->patches[1].skip_patch =
797 1.1 fvdl skip_patch_count + 1;
798 1.1 fvdl cur_scope->patches[1].skip_instr =
799 1.1 fvdl skip_instr_count;
800 1.1 fvdl }
801 1.1 fvdl
802 1.1 fvdl /* Count Head patch */
803 1.1 fvdl patch0_patch_skip++;
804 1.1 fvdl
805 1.1 fvdl /* Count any patches contained in our inner scope */
806 1.1 fvdl patch0_patch_skip += cur_scope->inner_scope_patches;
807 1.1 fvdl
808 1.1 fvdl cur_scope->patches[0].skip_patch = patch0_patch_skip;
809 1.1 fvdl cur_scope->patches[0].skip_instr =
810 1.1 fvdl cur_scope->end_addr - cur_scope->begin_addr;
811 1.1 fvdl
812 1.1 fvdl skip_instr_count += cur_scope->patches[0].skip_instr;
813 1.1 fvdl
814 1.1 fvdl skip_patch_count += patch0_patch_skip;
815 1.1 fvdl if (cur_scope->type == SCOPE_IF) {
816 1.1 fvdl scope->inner_scope_patches += skip_patch_count;
817 1.1 fvdl skip_patch_count = 0;
818 1.1 fvdl skip_instr_count = 0;
819 1.1 fvdl }
820 1.1 fvdl break;
821 1.1 fvdl case SCOPE_ELSE:
822 1.1 fvdl /* Count any patches contained in our innter scope */
823 1.1 fvdl skip_patch_count += cur_scope->inner_scope_patches;
824 1.1 fvdl
825 1.1 fvdl skip_instr_count += cur_scope->end_addr
826 1.1 fvdl - cur_scope->begin_addr;
827 1.1 fvdl break;
828 1.1 fvdl case SCOPE_ROOT:
829 1.1 fvdl stop("Unexpected scope type encountered", EX_SOFTWARE);
830 1.1 fvdl /* NOTREACHED */
831 1.1 fvdl }
832 1.1 fvdl
833 1.1 fvdl cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);
834 1.1 fvdl }
835 1.1 fvdl }
836