comparison lwasm/pass4.c @ 365:6a98cc90c14f

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