annotate lwasm/insn_bitbit.c @ 381:1624a36f12a3

Properly support 32 bit relocations
author lost@starbug
date Mon, 26 Apr 2010 19:56:10 -0600
parents be0f9f8d799f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 insn_bitbit.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
24 #include <lw_expr.h>
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
25
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include "instab.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 // these instructions cannot tolerate external references
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
30 PARSEFUNC(insn_parse_bitbit)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 int r;
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
33 lw_expr_t e;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 int v1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 int tv;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 r = toupper(*(*p)++);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 if (r == 'A')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 r = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 else if (r == 'B')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 r = 2;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 else if (r == 'C' && toupper(**p) == 'C')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 r = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
49 lwasm_register_error(as, l, "Bad register");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
52
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
53 if (*(*p)++ != ',')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
55 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
58 e = lwasm_parse_expr(as, p);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
59 if (!e)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
61 lwasm_register_error(as, l, "Bad operand");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
62 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
64 lwasm_save_expr(l, 0, e);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 if (*(*p)++ != ',')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
67 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
70
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
71 e = lwasm_parse_expr(as, p);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
72 if (!e)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
74 lwasm_register_error(as, l, "Bad operand");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
75 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
77 lwasm_save_expr(l, 1, e);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
78
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 if (*(*p)++ != ',')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
81 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
84
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 // ignore base page address modifier
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 if (**p == '<')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
89 e = lwasm_parse_expr(as, p);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
90 if (!e)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
92 lwasm_register_error(as, l, "Bad operand");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
93 return;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
94 }
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
95 lwasm_save_expr(l, 2, e);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
96
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
97 l -> lint = r;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
98 l -> len = OPLEN(instab[l -> insn].ops[0]) + 2;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
99 }
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
100
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
101 EMITFUNC(insn_emit_bitbit)
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
102 {
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
103 int v1, v2;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
104 lw_expr_t e;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
105
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
106 e = lwasm_fetch_expr(l, 0);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
107 if (!lw_expr_istype(e, lw_expr_type_int))
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
108 {
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
109 lwasm_register_error(as, l, "Bit number must be fully resolved");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
112 v1 = lw_expr_intval(e);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
113 if (v1 < 0 || v1 > 7)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
115 lwasm_register_error(as, l, "Invalid bit number");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
116 v1 = 0;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
119 e = lwasm_fetch_expr(l, 1);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
120 if (!lw_expr_istype(e, lw_expr_type_int))
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
121 {
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
122 lwasm_register_error(as, l, "Bit number must be fully resolved");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
123 return;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
124 }
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
125 v2 = lw_expr_intval(e);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
126 if (v2 < 0 || v2 > 7)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 {
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
128 lwasm_register_error(as, l, "Invalid bit number");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
129 v2 = 0;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 }
358
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
131 l -> pb = (l -> lint << 6) | (v1 << 3) | v2;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
132
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
133 e = lwasm_fetch_expr(l, 2);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
134 if (lw_expr_istype(e, lw_expr_type_int))
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
135 {
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
136 v1 = lw_expr_intval(e) & 0xFFFF;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
137 v2 = v1 - ((l -> dpval) << 8);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
138 if (v2 > 0xFF || v2 < 0)
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
139 {
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
140 lwasm_register_error(as, l, "Byte overflow");
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
141 return;
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
142 }
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
143 }
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
144 lwasm_emitop(l, instab[l -> insn].ops[0]);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
145 lwasm_emit(l, l -> pb);
be0f9f8d799f Bit transfer ops added
lost@starbug
parents: 339
diff changeset
146 lwasm_emitexpr(l, e, 1);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 }