annotate lwasm/pass5.c @ 384:38b50ce6967a

Made --list and --depend work
author lost@starbug
date Sat, 15 May 2010 20:46:04 -0600
parents 34dfc9747f23
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
367
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
1 /*
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
2 pass5.c
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
3
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
4 Copyright © 2010 William Astle
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
5
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
6 This file is part of LWTOOLS.
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
7
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
11 version.
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
12
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
16 more details.
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
17
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
20 */
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
21
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
22 #include <config.h>
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
23
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
24 #include <stdio.h>
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
25 #include <string.h>
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
26
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
27 #include <lw_alloc.h>
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
28 #include <lw_string.h>
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
29
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
30 #include "lwasm.h"
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
31 #include "instab.h"
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
32
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
33 /*
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
34 AssignAddresses Pass
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
35
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
36 Force resolution of all line addresses
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
37
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
38 */
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
39
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
40 static int exprok_aux(lw_expr_t e, void *priv)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
41 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
42 asmstate_t *as = priv;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
43
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
44 if (lw_expr_istype(e, lw_expr_type_int))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
45 return 0;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
46 if (lw_expr_istype(e, lw_expr_type_oper))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
47 return 0;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
48 if (lw_expr_istype(e, lw_expr_type_special) && as -> output_format == OUTPUT_OBJ)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
49 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
50 int t;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
51 t = lw_expr_specint(e);
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
52 if (t == lwasm_expr_secbase)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
53 return 0;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
54 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
55
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
56 return 1;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
57 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
58
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
59 static int exprok(asmstate_t *as, lw_expr_t e)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
60 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
61 if (lw_expr_testterms(e, exprok_aux, as))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
62 return 0;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
63 return 1;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
64 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
65
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
66 void do_pass5(asmstate_t *as)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
67 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
68 int rc;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
69 int cnt;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
70 int ocnt;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
71 line_t *cl, *sl;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
72 struct line_expr_s *le;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
73
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
74 // first, count the number of non-constant addresses; do
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
75 // a reduction first on each one
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
76 for (cnt = 0, cl = as -> line_head; cl; cl = cl -> next)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
77 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
78 as -> cl = cl;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
79 lwasm_reduce_expr(as, cl -> addr);
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
80 if (!exprok(as, cl -> addr))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
81 cnt++;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
82 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
83
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
84 sl = as -> line_head;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
85 while (cnt > 0)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
86 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
87 ocnt = cnt;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
88
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
89 // find an unresolved address
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
90 for ( ; sl && exprok(as, sl -> addr); sl = sl -> next)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
91 /* do nothing */ ;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
92
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
93 // simplify address
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
94 for (cl = sl; cl; cl = cl -> next)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
95 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
96 as -> cl = sl;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
97 lwasm_reduce_expr(as, sl -> addr);
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
98
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
99 if (exprok(as, cl -> addr))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
100 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
101 if (0 == --cnt);
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
102 return;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
103 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
104 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
105
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
106 if (cnt == ocnt)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
107 break;
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
108 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
109
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
110 if (cnt)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
111 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
112 // we have non-resolved line addresses here
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
113 for (cl = sl; cl; cl = cl -> next)
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
114 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
115 if (!exprok(as, cl -> addr))
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
116 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
117 lwasm_register_error(as, cl, "Cannot resolve line address");
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
118 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
119 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
120 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents:
diff changeset
121 }