annotate lwasm/pass4.c @ 416:1d69ed28f175

Fixed offset problem with emission of incomplete references at start of insn
author lost@l-w.ca
date Tue, 10 Aug 2010 22:00:21 -0600
parents a741d2e4869f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
1 /*
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
2 pass4.c
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
3
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
4 Copyright © 2010 William Astle
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
5
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
6 This file is part of LWTOOLS.
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
7
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
11 version.
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
12
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
16 more details.
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
17
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
20 */
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
21
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
22 #include <config.h>
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
23
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
24 #include <stdio.h>
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
25 #include <string.h>
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
26
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
27 #include <lw_alloc.h>
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
28 #include <lw_string.h>
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
29
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
30 #include "lwasm.h"
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
31 #include "instab.h"
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
32
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
33 /*
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
34 Resolve2 Pass
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
35
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
36 Force resolution of instruction sizes.
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
37
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
38 */
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
39 void do_pass4_aux(asmstate_t *as, int force)
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
40 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
41 int rc;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
42 int cnt;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
43 line_t *cl, *sl;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
44 struct line_expr_s *le;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
45
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
46 // first, count the number of unresolved instructions
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
47 for (cnt = 0, cl = as -> line_head; cl; cl = cl -> next)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
48 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
49 if (cl -> len == -1)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
50 cnt++;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
51 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
52
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
53 sl = as -> line_head;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
54 while (cnt > 0)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
55 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
56 // find an unresolved instruction
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
57 for ( ; sl && sl -> len != -1; sl = sl -> next)
366
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
58 {
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
59 as -> cl = sl;
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
60 lwasm_reduce_expr(as, sl -> addr);
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
61
366
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
62 // simplify each expression
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
63 for (le = sl -> exprs; le; le = le -> next)
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
64 lwasm_reduce_expr(as, le -> expr);
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
65 }
84dc6d2ec6ba Added some more expression reductions
lost@starbug
parents: 365
diff changeset
66
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
67 // simplify address
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
68 as -> cl = sl;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
69 lwasm_reduce_expr(as, sl -> addr);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
70
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
71 // simplify each expression
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
72 for (le = sl -> exprs; le; le = le -> next)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
73 lwasm_reduce_expr(as, le -> expr);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
74
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
75
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 366
diff changeset
76 if (sl -> len == -1 && sl -> insn >= 0 && instab[sl -> insn].resolve)
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
77 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 366
diff changeset
78 (instab[sl -> insn].resolve)(as, sl, 1);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
79 if (force && sl -> len == -1)
365
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
80 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
81 lwasm_register_error(as, sl, "Instruction failed to resolve.");
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
82 return;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
83 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
84 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
85 cnt--;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
86 if (cnt == 0)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
87 return;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
88
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
89 do
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
90 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
91 rc = 0;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
92 for (cl = sl; cl; cl = cl -> next)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
93 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
94 as -> cl = cl;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
95
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
96 // simplify address
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
97 lwasm_reduce_expr(as, cl -> addr);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
98
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
99 // simplify each expression
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
100 for (le = cl -> exprs; le; le = le -> next)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
101 lwasm_reduce_expr(as, le -> expr);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
102
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
103 if (cl -> len == -1)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
104 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
105 // try resolving the instruction length
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
106 // but don't force resolution
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
107 if (cl -> insn >= 0 && instab[cl -> insn].resolve)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
108 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
109 (instab[cl -> insn].resolve)(as, cl, 0);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
110 if (cl -> len != -1)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
111 {
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
112 rc++;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
113 cnt--;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
114 if (cnt == 0)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
115 return;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
116 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
117 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
118 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
119 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
120 if (as -> errorcount > 0)
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
121 return;
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
122 } while (rc > 0);
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
123 }
6a98cc90c14f Added resolve passes
lost@starbug
parents:
diff changeset
124 }
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
125
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
126 void do_pass4(asmstate_t *as)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
127 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
128 do_pass4_aux(as, 1);
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 371
diff changeset
129 }