comparison lwasm/pass5.c @ 367:34dfc9747f23

Reduction passes complete
author lost@starbug
date Thu, 15 Apr 2010 21:56:06 -0600
parents
children
comparison
equal deleted inserted replaced
366:84dc6d2ec6ba 367:34dfc9747f23
1 /*
2 pass5.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 AssignAddresses Pass
35
36 Force resolution of all line addresses
37
38 */
39
40 static int exprok_aux(lw_expr_t e, void *priv)
41 {
42 asmstate_t *as = priv;
43
44 if (lw_expr_istype(e, lw_expr_type_int))
45 return 0;
46 if (lw_expr_istype(e, lw_expr_type_oper))
47 return 0;
48 if (lw_expr_istype(e, lw_expr_type_special) && as -> output_format == OUTPUT_OBJ)
49 {
50 int t;
51 t = lw_expr_specint(e);
52 if (t == lwasm_expr_secbase)
53 return 0;
54 }
55
56 return 1;
57 }
58
59 static int exprok(asmstate_t *as, lw_expr_t e)
60 {
61 if (lw_expr_testterms(e, exprok_aux, as))
62 return 0;
63 return 1;
64 }
65
66 void do_pass5(asmstate_t *as)
67 {
68 int rc;
69 int cnt;
70 int ocnt;
71 line_t *cl, *sl;
72 struct line_expr_s *le;
73
74 // first, count the number of non-constant addresses; do
75 // a reduction first on each one
76 for (cnt = 0, cl = as -> line_head; cl; cl = cl -> next)
77 {
78 as -> cl = cl;
79 lwasm_reduce_expr(as, cl -> addr);
80 if (!exprok(as, cl -> addr))
81 cnt++;
82 }
83
84 sl = as -> line_head;
85 while (cnt > 0)
86 {
87 ocnt = cnt;
88
89 // find an unresolved address
90 for ( ; sl && exprok(as, sl -> addr); sl = sl -> next)
91 /* do nothing */ ;
92
93 // simplify address
94 for (cl = sl; cl; cl = cl -> next)
95 {
96 as -> cl = sl;
97 lwasm_reduce_expr(as, sl -> addr);
98
99 if (exprok(as, cl -> addr))
100 {
101 if (0 == --cnt);
102 return;
103 }
104 }
105
106 if (cnt == ocnt)
107 break;
108 }
109
110 if (cnt)
111 {
112 // we have non-resolved line addresses here
113 for (cl = sl; cl; cl = cl -> next)
114 {
115 if (!exprok(as, cl -> addr))
116 {
117 lwasm_register_error(as, cl, "Cannot resolve line address");
118 }
119 }
120 }
121 }