comparison lwasm/pass4.c @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children 697bc543368c
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
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 <stdio.h>
23 #include <string.h>
24
25 #include <lw_alloc.h>
26 #include <lw_string.h>
27
28 #include "lwasm.h"
29 #include "instab.h"
30
31 /*
32 Resolve2 Pass
33
34 Force resolution of instruction sizes.
35
36 */
37 void do_pass4_aux(asmstate_t *as, int force)
38 {
39 int rc;
40 int cnt;
41 line_t *cl, *sl;
42 struct line_expr_s *le;
43
44 // first, count the number of unresolved instructions
45 for (cnt = 0, cl = as -> line_head; cl; cl = cl -> next)
46 {
47 if (cl -> len == -1)
48 cnt++;
49 }
50
51 sl = as -> line_head;
52 while (cnt > 0)
53 {
54 // find an unresolved instruction
55 for ( ; sl && sl -> len != -1; sl = sl -> next)
56 {
57 as -> cl = sl;
58 lwasm_reduce_expr(as, sl -> addr);
59
60 // simplify each expression
61 for (le = sl -> exprs; le; le = le -> next)
62 lwasm_reduce_expr(as, le -> expr);
63 }
64
65 // simplify address
66 as -> cl = sl;
67 lwasm_reduce_expr(as, sl -> addr);
68
69 // simplify each expression
70 for (le = sl -> exprs; le; le = le -> next)
71 lwasm_reduce_expr(as, le -> expr);
72
73
74 if (sl -> len == -1 && sl -> insn >= 0 && instab[sl -> insn].resolve)
75 {
76 (instab[sl -> insn].resolve)(as, sl, 1);
77 if (force && sl -> len == -1)
78 {
79 lwasm_register_error(as, sl, "Instruction failed to resolve.");
80 return;
81 }
82 }
83 cnt--;
84 if (cnt == 0)
85 return;
86
87 do
88 {
89 rc = 0;
90 for (cl = sl; cl; cl = cl -> next)
91 {
92 as -> cl = cl;
93
94 // simplify address
95 lwasm_reduce_expr(as, cl -> addr);
96
97 // simplify each expression
98 for (le = cl -> exprs; le; le = le -> next)
99 lwasm_reduce_expr(as, le -> expr);
100
101 if (cl -> len == -1)
102 {
103 // try resolving the instruction length
104 // but don't force resolution
105 if (cl -> insn >= 0 && instab[cl -> insn].resolve)
106 {
107 (instab[cl -> insn].resolve)(as, cl, 0);
108 if (cl -> len != -1)
109 {
110 rc++;
111 cnt--;
112 if (cnt == 0)
113 return;
114 }
115 }
116 }
117 }
118 if (as -> errorcount > 0)
119 return;
120 } while (rc > 0);
121 }
122 }
123
124 void do_pass4(asmstate_t *as)
125 {
126 do_pass4_aux(as, 1);
127 }