annotate lwasm/pass2.c @ 370:6b33faa21a0a

Debugging output and bugfixing pass 0
author lost@starbug
date Tue, 20 Apr 2010 21:59:58 -0600
parents d96c30e60ddf
children d99322ef6f21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
1 /*
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
2 pass2.c
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
3
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
4 Copyright © 2010 William Astle
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
5
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
6 This file is part of LWTOOLS.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
7
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
11 version.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
12
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
16 more details.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
17
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
20 */
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
21
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
22 #include <config.h>
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
23
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
24 #include <stdio.h>
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
25 #include <string.h>
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
26
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
27 #include <lw_alloc.h>
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
28 #include <lw_string.h>
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
29
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
30 #include "lwasm.h"
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
31 #include "instab.h"
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
32
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
33 /*
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
34 pass 2: deal with undefined symbols and do a simplification pass
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
35 on all the expressions. Handle PRAGMA_IMPORTUNDEFEXPORT
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
36
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
37 */
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
38 void do_pass2(asmstate_t *as)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
39 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
40 line_t *cl;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
41 exportlist_t *ex;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
42 struct symtabe *s;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
43 importlist_t *im;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
44 struct line_expr_s *le;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
45
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
46 // verify the export list
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
47 if (as -> output_format == OUTPUT_OBJ)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
48 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
49 for (ex = as -> exportlist; ex; ex = ex -> next)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
50 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
51 s = lookup_symbol(as, NULL, ex -> symbol);
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
52 if (!s)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
53 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
54 if (CURPRAGMA(ex -> line, PRAGMA_IMPORTUNDEFEXPORT))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
55 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
56 for (im = as -> importlist; im; im = im -> next)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
57 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
58 if (!strcmp(ex -> symbol, im -> symbol))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
59 break;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
60 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
61 if (!im)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
62 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
63 im = lw_alloc(sizeof(importlist_t));
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
64 im -> symbol = lw_strdup(ex -> symbol);
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
65 im -> next = as -> importlist;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
66 as -> importlist = im;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
67 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
68 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
69 else
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
70 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
71 // undefined export - register error
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
72 lwasm_register_error(as, ex -> line, "Undefined exported symbol");
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
73 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
74 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
75 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
76 if (as -> errorcount > 0)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
77 return;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
78 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
79
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
80 // we want to throw errors on undefined symbols here
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
81 as -> badsymerr = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
82
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
83 // now do some reductions on expressions
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
84 for (cl = as -> line_head; cl; cl = cl -> next)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
85 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
86 as -> cl = cl;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
87
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
88 // simplify address
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
89 lwasm_reduce_expr(as, cl -> addr);
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
90
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
91 // simplify each expression
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
92 for (le = cl -> exprs; le; le = le -> next)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
93 lwasm_reduce_expr(as, le -> expr);
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
94 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
diff changeset
95 }