339
|
1 /*
|
|
2 insn_gen.c, Copyright © 2009 William Astle
|
|
3
|
|
4 This file is part of LWASM.
|
|
5
|
|
6 LWASM is free software: you can redistribute it and/or modify it under the
|
|
7 terms of the GNU General Public License as published by the Free Software
|
|
8 Foundation, either version 3 of the License, or (at your option) any later
|
|
9 version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
14 more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License along with
|
|
17 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
|
19 Contains code for parsing general addressing modes (IMM+DIR+EXT+IND)
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <ctype.h>
|
|
25 #include <stdlib.h>
|
|
26
|
|
27 #include "lwasm.h"
|
|
28 #include "instab.h"
|
|
29 #include "expr.h"
|
|
30
|
|
31 extern void insn_indexed_aux(asmstate_t *as, lwasm_line_t *l, const char **p, int *b1, int *b2, int *b3);
|
|
32
|
|
33 // "extra" is required due to the way OIM, EIM, TIM, and AIM work
|
|
34 void insn_gen_aux(asmstate_t *as, lwasm_line_t *l, char **optr, int opnum, int extra)
|
|
35 {
|
|
36 int b1 = -1, b2 = -1, b3 = -1;
|
|
37
|
|
38 const char *optr2;
|
|
39 int v1, tv, rval;
|
|
40 lwasm_expr_stack_t *s;
|
|
41 int f8 = 0;
|
|
42 int f16 = 0;
|
|
43 int isdp = 0;
|
|
44
|
|
45 optr2 = *optr;
|
|
46 while (*optr2 && !isspace(*optr2) && *optr2 != ',') optr2++
|
|
47 /* do nothing */ ;
|
|
48
|
|
49 if (*optr2 != ',' && **optr != '[')
|
|
50 {
|
|
51 // not indexed
|
|
52 if (l -> fsize == 1)
|
|
53 f8 = 1;
|
|
54 else if (l -> fsize == 2)
|
|
55 f16 = 1;
|
|
56
|
|
57 if (**optr == '<')
|
|
58 {
|
|
59 (*optr)++;
|
|
60 f8 = 1;
|
|
61 }
|
|
62 // for compatibility with asxxxx
|
|
63 // * followed by a digit, alpha, or _, or ., or ?, or another * is "f8"
|
|
64 else if (**optr == '*')
|
|
65 {
|
|
66 tv = *(*optr + 1);
|
|
67 if (isdigit(tv) || isalpha(tv) || tv == '_' || tv == '.' || tv == '?' || tv == '@' || tv == '*' || tv == '+' || tv == '-')
|
|
68 {
|
|
69 f8 = 1;
|
|
70 (*optr)++;
|
|
71 }
|
|
72 }
|
|
73 else if (**optr == '>')
|
|
74 {
|
|
75 (*optr)++;
|
|
76 f16 = 1;
|
|
77 }
|
|
78 rval = lwasm_expr_result2(as, l, optr, 0, &v1, 0);
|
|
79 if (rval != 0)
|
|
80 {
|
|
81 f16 = 1;
|
|
82 v1 = 0;
|
|
83 l -> fsize = 2;
|
|
84 }
|
|
85
|
|
86 if (((v1 >> 8) & 0xff) == (as -> dpval & 0xff))
|
|
87 isdp = 1;
|
|
88
|
|
89 // disallow non-explicit DP in obj target
|
|
90 if (as -> outformat == OUTPUT_OBJ && !f8)
|
|
91 f16 = 1;
|
|
92
|
|
93 if (f8 || (!f16 && isdp))
|
|
94 {
|
|
95 v1 = v1 & 0xffff;
|
|
96 tv = v1 - ((as -> dpval) << 8);
|
|
97 if (tv < 0 || tv > 0xff)
|
|
98 {
|
|
99 register_error(as, l, 2, "Byte overflow");
|
|
100 }
|
|
101 v1 = v1 & 0xff;
|
|
102 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
103 if (extra != -1)
|
|
104 lwasm_emit(as, l, extra);
|
|
105 l -> relocoff = as -> addr - l -> codeaddr;
|
|
106 l -> reloc8bit = 1;
|
|
107 lwasm_emit(as, l, v1 & 0xff);
|
|
108 return;
|
|
109 }
|
|
110 else
|
|
111 {
|
|
112 // everything else is 16 bit....
|
|
113 lwasm_emitop(as, l, instab[opnum].ops[2]);
|
|
114 if (extra != -1)
|
|
115 lwasm_emit(as, l, extra);
|
|
116 l -> relocoff = as -> addr - l -> codeaddr;
|
|
117 lwasm_emit(as, l, v1 >> 8);
|
|
118 lwasm_emit(as, l, v1 & 0xff);
|
|
119 return;
|
|
120 }
|
|
121 }
|
|
122
|
|
123 lwasm_emitop(as, l, instab[opnum].ops[1]);
|
|
124 if (extra != -1)
|
|
125 lwasm_emit(as, l, extra);
|
|
126 insn_indexed_aux(as, l, (const char **)optr, &b1, &b2, &b3);
|
|
127 if (b1 != -1)
|
|
128 lwasm_emit(as, l, b1);
|
|
129 if (b2 != -1)
|
|
130 lwasm_emit(as, l, b2);
|
|
131 if (b3 != -1)
|
|
132 lwasm_emit(as, l, b3);
|
|
133 return;
|
|
134 }
|
|
135
|
|
136 // the various insn_gen? functions have an immediate mode of ? bits
|
|
137 OPFUNC(insn_gen0)
|
|
138 {
|
|
139 if (**p == '#')
|
|
140 {
|
|
141 register_error(as, l, 1, "Immediate mode not allowed");
|
|
142 return;
|
|
143 }
|
|
144
|
|
145 // handle non-immediate
|
|
146 insn_gen_aux(as, l, p, opnum, -1);
|
|
147 }
|
|
148
|
|
149 OPFUNC(insn_gen8)
|
|
150 {
|
|
151 int rval;
|
|
152 int r;
|
|
153
|
|
154 if (**p == '#')
|
|
155 {
|
|
156 lwasm_emitop(as, l, instab[opnum].ops[3]);
|
|
157 (*p)++;
|
|
158 r = lwasm_expr_result2(as, l, p, 0, &rval, 0);
|
|
159 if (r != 0)
|
|
160 rval = 0;
|
|
161 if (r == 1 && as -> passnum == 2)
|
|
162 {
|
|
163 l -> relocoff = as -> addr - l -> codeaddr;
|
|
164 l -> reloc8bit = 1;
|
|
165 }
|
|
166 // register_error(as, l, 2, "Illegal external or intersegment reference");
|
|
167 lwasm_emit(as, l, rval & 0xff);
|
|
168 return;
|
|
169 }
|
|
170
|
|
171 insn_gen_aux(as, l, p, opnum, -1);
|
|
172 }
|
|
173
|
|
174 OPFUNC(insn_gen16)
|
|
175 {
|
|
176 int rval, r;
|
|
177
|
|
178 if (**p == '#')
|
|
179 {
|
|
180 lwasm_emitop(as, l, instab[opnum].ops[3]);
|
|
181 (*p)++;
|
|
182
|
|
183 r = lwasm_expr_result2(as, l, p, 0, &rval, 0);
|
|
184 if (r != 0)
|
|
185 rval = 0;
|
|
186 if (r == 1 && as -> passnum == 2)
|
|
187 {
|
|
188 l -> relocoff = as -> addr - l -> codeaddr;
|
|
189 }
|
|
190 lwasm_emit(as, l, (rval >> 8) & 0xff);
|
|
191 lwasm_emit(as, l, rval & 0xff);
|
|
192 return;
|
|
193 }
|
|
194
|
|
195 insn_gen_aux(as, l, p, opnum, -1);
|
|
196 }
|
|
197
|
|
198 OPFUNC(insn_gen32)
|
|
199 {
|
|
200 int r, rval;
|
|
201
|
|
202 if (**p == '#')
|
|
203 {
|
|
204 lwasm_emitop(as, l, instab[opnum].ops[3]);
|
|
205 (*p)++;
|
|
206
|
|
207 r = lwasm_expr_result2(as, l, p, 0, &rval, 0);
|
|
208 if (r != 0)
|
|
209 rval = 0;
|
|
210 if (r == 1 && as -> passnum == 2)
|
|
211 {
|
|
212 register_error(as, l, 2, "Illegal external or intersegment reference");
|
|
213 }
|
|
214
|
|
215 lwasm_emit(as, l, (rval >> 24) & 0xff);
|
|
216 lwasm_emit(as, l, (rval >> 16) & 0xff);
|
|
217 lwasm_emit(as, l, (rval >> 8) & 0xff);
|
|
218 lwasm_emit(as, l, rval & 0xff);
|
|
219 return;
|
|
220 }
|
|
221
|
|
222 insn_gen_aux(as, l, p, opnum, -1);
|
|
223 }
|
|
224
|
|
225 OPFUNC(insn_imm8)
|
|
226 {
|
|
227 int r, rval;
|
|
228
|
|
229 if (**p == '#')
|
|
230 {
|
|
231 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
232 (*p)++;
|
|
233
|
|
234 r = lwasm_expr_result2(as, l, p, 0, &rval, 0);
|
|
235 if (r != 0)
|
|
236 rval = 0;
|
|
237 if (r == 1 && as -> passnum == 2)
|
|
238 register_error(as, l, 2, "Illegal external or intersegment reference");
|
|
239
|
|
240 if (rval < -128 || rval > 255)
|
|
241 register_error(as, l, 2, "Byte overflow");
|
|
242 lwasm_emit(as, l, rval & 0xff);
|
|
243 return;
|
|
244 }
|
|
245
|
|
246 register_error(as, l, 1, "Bad operand");
|
|
247 }
|