annotate src/insn_indexed.c @ 68:cef25b0088e6

Fixed some problems detected by valgrind and testing
author lost
date Mon, 05 Jan 2009 06:14:41 +0000
parents 538e15927776
children b6b1e79cc277
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
1 /*
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
2 insn_indexed.c
33
74a3fef7c8d0 Added general addressing modes (immediate, base page, extended, indexed)
lost
parents: 32
diff changeset
3 Copyright © 2009 William Astle
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
4
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
5 This file is part of LWASM.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
6
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
10 version.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
11
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
15 more details.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
16
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
19 */
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
20
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
21 /*
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
22 for handling indexed mode instructions
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
23 */
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
24
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
25 #define __insn_indexed_c_seen__
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
26
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
27 #include <ctype.h>
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
28 #include <string.h>
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
29
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
30 #include "lwasm.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
31 #include "instab.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
32 #include "expr.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
33
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
34 void insn_indexed_aux(asmstate_t *as, lwasm_line_t *l, const char **p, int *b1, int *b2, int *b3)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
35 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
36 static const char *regs = "X Y U S W PCRPC ";
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
37 static const struct { char *opstr; int pb; } simpleindex[] =
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
38 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
39 {",x", 0x84}, {",y", 0xa4}, {",u", 0xc4}, {",s", 0xe4},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
40 {",x+", 0x80}, {",y+", 0xa0}, {",u+", 0xc0}, {",s+", 0xe0},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
41 {",x++", 0x81}, {",y++", 0xa1}, {",u++", 0xc1}, {",s++", 0xe1},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
42 {",-x", 0x82}, {",-y", 0xa2}, {",-u", 0xc2}, {",-s", 0xe2},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
43 {",--x", 0x83}, {",--y", 0xa3}, {",--u", 0xc3}, {",--s", 0xe3},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
44 {"a,x", 0x86}, {"a,y", 0xa6}, {"a,u", 0xc6}, {"a,s", 0xe6},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
45 {"b,x", 0x85}, {"b,y", 0xa5}, {"b,u", 0xc5}, {"b,s", 0xe5},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
46 {"e,x", 0x87}, {"e,y", 0xa7}, {"e,u", 0xc7}, {"e,s", 0xe7},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
47 {"f,x", 0x8a}, {"f,y", 0xaa}, {"f,u", 0xca}, {"f,s", 0xea},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
48 {"d,x", 0x8b}, {"d,y", 0xab}, {"d,u", 0xcb}, {"d,s", 0xed},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
49 {"w,x", 0x8e}, {"w,y", 0xae}, {"w,u", 0xce}, {"w,s", 0xee},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
50 {",w", 0x8f}, {",w++", 0xcf}, {",--w", 0xef},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
51
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
52 {"[,x]", 0x94}, {"[,y]", 0xb4}, {"[,u", 0xd4}, {"[,s]", 0xf4},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
53 {"[,x++]", 0x91}, {"[,y++]", 0xb1}, {"[,u++]", 0xd1}, {"[,s++]", 0xf1},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
54 {"[,--x]", 0x93}, {"[,--y]", 0xb3}, {"[,--u]", 0xd3}, {"[,--s]", 0xf3},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
55 {"[a,x]", 0x96}, {"[a,y]", 0xb6}, {"[a,u]", 0xd6}, {"[a,s]", 0xf6},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
56 {"[b,x]", 0x95}, {"[b,y]", 0xb5}, {"[b,u]", 0xd5}, {"[b,s]", 0xf5},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
57 {"[e,x]", 0x97}, {"[e,y]", 0xb7}, {"[e,u]", 0xd7}, {"[e,s]", 0xf7},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
58 {"[f,x]", 0x9a}, {"[f,y]", 0xba}, {"[f,u]", 0xda}, {"[f,s]", 0xfa},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
59 {"[d,x]", 0x9b}, {"[d,y]", 0xbb}, {"[d,u]", 0xdb}, {"[d,s]", 0xfd},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
60 {"[w,x]", 0x9e}, {"[w,y]", 0xbe}, {"[w,u]", 0xde}, {"[w,s]", 0xfe},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
61 {"[,w]", 0x90}, {"[,w++]", 0xd0}, {"[,--w]", 0xf0},
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
62
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
63 { "", -1 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
64 };
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
65 char stbuf[25];
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
66 int i;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
67 int f8 = 0, f16 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
68 int rn;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
69 int indir = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
70 int rval;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
71 int f0 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
72 const char *p2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
73 lwasm_expr_stack_t *s;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
74
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
75 *b1 = *b2 = *b3 = -1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
76
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
77 for (i = 0; i < 24; i++)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
78 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
79 if (*((*p) + i) && !isspace(*((*p) + i)))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
80 stbuf[i] = *((*p) + i);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
81 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
82 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
83 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
84 stbuf[i] = '\0';
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
85 if (!*((*p) + i) || isspace(*((*p) + i)))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
86 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
87 int j;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
88 // do simple lookup
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
89 for (j = 0; simpleindex[j].opstr[0]; j++)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
90 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
91 if (!strcasecmp(stbuf, simpleindex[j].opstr))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
92 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
93 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
94 if (simpleindex[j].opstr[0])
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
95 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
96 *b1 = simpleindex[j].pb;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
97 (*p) += i;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
98 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
99 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
100 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
101
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
102 // now we have the hard ones to work out here
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
103
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
104 // if we've previously forced the sizes, make a note of it
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
105 if (l -> fsize == 1)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
106 f8 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
107 else if (l -> fsize == 2)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
108 f16 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
109
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
110 if (**p == '[')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
111 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
112 indir = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
113 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
114 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
115
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
116 rn = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
117 for (i = 0; (*p)[i] && !isspace((*p)[i]); i++)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
118 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
119 if ((*p)[i] == ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
120 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
121 rn = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
122 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
123 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
124 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
125
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
126 if (!rn && indir)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
127 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
128 // extended indir
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
129 *b1 = 0x9f;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
130 *b2 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
131 *b3 = 0;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 33
diff changeset
132 s = lwasm_evaluate_expr(as, l, *p, &p2);
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
133 if (!s)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
134 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
135 register_error(as, l, 1, "Bad expression");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
136 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
137 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
138 *p = p2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
139 if (**p != ']')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
140 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
141 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
142 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
143 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
144 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
145 if (!lwasm_expr_is_constant(s))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
146 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
147 register_error(as, l, 2, "Incomplete reference");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
148 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
149 rn = lwasm_expr_get_value(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
150 lwasm_expr_stack_free(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
151 *b2 = (rn >> 8) & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
152 *b3 = rn & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
153 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
154 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
155
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
156 if (**p == '<')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
157 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
158 f8 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
159 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
160 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
161 else if (**p == '>')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
162 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
163 f16 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
164 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
165 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
166
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
167 if (**p == '0' && *(*p+1) == ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
168 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
169 f0 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
170 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
171
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
172 // now we have to evaluate the expression
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 33
diff changeset
173 s = lwasm_evaluate_expr(as, l, *p, &p2);
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
174 *p = p2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
175 if (!s)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
176 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
177 register_error(as, l, 1, "Bad expression");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
178 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
179 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
180 if (!lwasm_expr_is_constant(s))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
181 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
182 register_error(as, l, 2, "Incomplete reference");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
183 if (!f8 && !f0)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
184 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
185 f16 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
186 l -> fsize = 2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
187 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
188 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
189 rval = lwasm_expr_get_value(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
190 lwasm_expr_stack_free(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
191
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
192 // now look for a comma; if not present, explode
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
193 if (*(*p)++ != ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
194 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
195 // syntax error; force 0 bit
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
196 *b1 = 00;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
197 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
198 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
199
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
200 // now get the register
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
201 rn = lwasm_lookupreg3(regs, p);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
202 if (rn < 0)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
203 goto reterr;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
204
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
205 if (indir)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
206 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
207 if (**p != ']')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
208 goto reterr;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
209 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
210 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
211 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
212
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
213 // nnnn,W is only 16 bit
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
214 if (rn == 4)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
215 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
216 if (f8)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
217 goto reterr;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
218 if (!f16 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE) && rval == 0)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
219 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
220 if (indir)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
221 *b1 = 0x90;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
222 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
223 *b1 = 0x8f;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
224 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
225 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
226 if (indir)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
227 *b1 = 0xb0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
228 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
229 *b1 = 0xcf;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
230 *b2 = (rval >> 8) & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
231 *b3 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
232 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
233 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
234
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
235 if (indir) indir = 0x10;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
236
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
237 // PCR? redo v1, v2 relative to current address
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
238 if (rn == 5)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
239 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
240 rval -= as -> addr;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
241
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
242 // we have a slight problem here
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
243 // PCR based on current insn loc is really
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
244 // -125 <= offset <= +130 (8 bit offset)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
245 // NOTE: when we are called, we already have the opcode emitted
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
246 // so we only need to worry about the size of the operand
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
247 // hence the 2 and 3 magic numbers below instead of 3 and 4
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
248 // (and that also avoids errors with two byte opcodes, etc)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
249 if (f8 || (!f16 && rval >= -125 && rval <= 130))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
250 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
251 *b1 = indir | 0x8C;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
252 rval -= 2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
253 if (rval < -128 || rval > 127)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
254 register_error(as, l, 2, "Byte overflow");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
255 *b2 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
256 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
257 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
258
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
259 // anything else is 16 bit offset
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
260 // need 16 bit
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
261 *b1 = indir | 0x8D;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
262 rval -= 3;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
263 *b2 = (rval >> 8) & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
264 *b3 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
265 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
266 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
267
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
268 // constant offset from PC
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
269 if (rn == 6)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
270 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
271 if (f8 || (!f16 && rval >= -128 && rval <= 127))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
272 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
273 *b1 = indir | 0x8C;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
274 if (rval < -128 || rval > 127)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
275 register_error(as, l, 2, "Byte overflow");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
276 *b2 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
277 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
278 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
279
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
280 // everything else must be 16 bit
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
281 // need 16 bit
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
282 *b1 = indir | 0x8D;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
283 *b2 = (rval >> 8) & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
284 *b3 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
285 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
286 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
287
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
288 // we only have to deal with x,y,u,s here
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
289 if (!f8 && !f16 && rval >= -16 && rval <= 15)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
290 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
291 if (rval == 0 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
292 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
293 *b1 = rn << 5 | indir | 0x80 | 0x04;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
294 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
295 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
296 // we pick the smallest value here
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
297 if (indir)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
298 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
299 f8 = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
300 goto no5bit;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
301 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
302 *b1 = rn << 5 | (rval & 0x1F);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
303 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
304 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
305 no5bit:
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
306 if (f16 || (!f8 && (rval < -128 || rval > 127)))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
307 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
308 // must be a 16 bit offset here
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
309 *b1 = rn << 5 | indir | 0x80 | 0x09;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
310 *b2 = (rval >> 8) & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
311 *b3 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
312 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
313 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
314
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
315 // if we're here, we have an 8 bit offset
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
316 *b1 = rn << 5 | indir | 0x80 | 0x08;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
317 if (rval < -128 || rval > 127)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
318 register_error(as, l, 2, "Byte overflow");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
319 *b2 = rval & 0xff;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
320 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
321 reterr:
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
322 *b1 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
323 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
324 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
325
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
326 OPFUNC(insn_indexed)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
327 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
328 int b1, b2, b3;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
329
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
330 lwasm_emitop(as, l, instab[opnum].ops[0]);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
331
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
332 insn_indexed_aux(as, l, (const char **)p, &b1, &b2, &b3);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
333 if (b1 != -1)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
334 lwasm_emit(as, l, b1);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
335 if (b2 != -1)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
336 lwasm_emit(as, l, b2);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
337 if (b3 != -1)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
338 lwasm_emit(as, l, b3);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
339 }