annotate src/lwasm.c @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
1 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
2 * lwasm.c
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
3 *
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
4 * main code for lwasm
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
5 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
6
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
7 #include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
8 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
9 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
10 #include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
11 #include <string.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
12 #define __lwasm_c_seen__
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
13 #include "instab.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
14 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
15
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
16 void lwasm_read_file(asmstate_t *as, char *fname);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
17 extern int add_macro_line(asmstate_t *as, sourceline_t *cl, char *optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
18 extern void expand_macro(asmstate_t *as, sourceline_t *cl, char **optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
19
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
20 #define debug(mess, ...) do { if (as->debug) { fprintf(stderr, "DEBUG: "); fprintf(stderr, (mess), ## __VA_ARGS__); } } while (0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
21
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
22 void register_error(asmstate_t *as, sourceline_t *cl, int errcode)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 errortab_t *e;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 e = malloc(sizeof(errortab_t));
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 e -> errnum = errcode;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 e -> line = cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 e -> next = cl -> errors;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 cl -> errors = e;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33 as -> errorcount++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36 int eval_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *val);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
38 int eval_min(int v1, int v2, int v3, int v4)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40 if (v2 < v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
41 v1 = v2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42 if (v3 < v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
43 v1 = v3;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44 if (v4 < v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 v1 = v4;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 return v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 int eval_max(int v1, int v2, int v3, int v4)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 if (v2 > v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 v1 = v2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 if (v3 > v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 v1 = v3;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55 if (v4 > v1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 v1 = v4;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 return v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 int lookupreg3(const char *rlist, char **str)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 int rval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 int f = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 const char *reglist = rlist;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 while (*reglist)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 if (toupper(**str) == *reglist)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 // first char matches
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71 if (reglist[1] == ' ')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 f = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
75 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
76 if (toupper(*(*str + 1)) == reglist[1])
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 // second char matches
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79 if (reglist[2] == ' ')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
80 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
81 f = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
82 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
83 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84 if (toupper(*(*str + 2)) == reglist[2])
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 f = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91 reglist += 3;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 rval++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 if (f == 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98 reglist = rval * 3 + rlist;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99 if (reglist[1] == ' ')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 (*str) += 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 else if (reglist[2] == ' ')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102 (*str) += 2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104 (*str)+=3;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
105 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
107
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
109 int lookupreg(const char *reglist, char **str)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111 int rval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
112 while (*reglist)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
113 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
114 if (toupper(**str) == *reglist)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
115 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
116 // first char matches
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
117 if (reglist[1] == ' ' && !isalpha(*(*str + 1)))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
118 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
119 if (toupper(*(*str + 1)) == reglist[1])
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
120 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
121 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
122 reglist += 2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
123 rval++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
124 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
125 if (!*reglist)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
126 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
127 if (reglist[1] == ' ')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
128 (*str)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
129 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
130 (*str)+=2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
131 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
132 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
133
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
134 void addcodebyte(asmstate_t *as, sourceline_t *cl, int cb)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
135 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
136 cl -> len += 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
137 if (as -> passnum != 2)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
138 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
139
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
140 if (cl -> numcodebytes >= cl -> codesize)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
141 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
142 cl -> codebytes = realloc(cl -> codebytes, cl -> codesize + 32);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
143 cl -> codesize += 32;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
144 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
145 debug("EMIT: %02x\n", cb & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
146 cl -> codebytes[cl -> numcodebytes++] = cb & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
147 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
148
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
149 // parse a symble out of the line and return a pointer
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
150 // to a static pointer
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
151 // return NULL if not a symbol or a bad symbol
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
152 char *parse_symbol(asmstate_t *as, char **ptr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
153 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
154 static char *symptr = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
155 char *tptr = *ptr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
156 int sl = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
157
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
158 // symbol can start with _,a-z,A-Z
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
159
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
160 if (!strchr(SYMCHAR_START, **ptr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
161 return NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
162
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
163 while (*tptr && !isspace(*tptr) && strchr(SYMCHAR, *tptr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
164 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
165 tptr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
166 sl++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
167 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
168
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
169 symptr = realloc(symptr, sl + 1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
170 tptr = symptr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
171 while (sl)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
172 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
173 *tptr++ = *(*ptr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
174 sl--;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
175 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
176 *tptr = '\0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
177 return symptr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
178 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
179
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
180 // resolve an instruction
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
181 void resolve_insn(asmstate_t *as, sourceline_t *cl)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
182 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
183 char *optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
184 char opbuf[MAX_OP_LEN + 1];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
185 char *symbol = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
186 int c;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
187
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
188 cl -> code_symloc = as -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
189
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
190 cl -> addrset = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
191 cl -> isequ = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
192 cl -> len = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
193 cl -> undef = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
194
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
195 // only parse line on first pass
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
196 if (as -> passnum == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
197 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
198 optr = cl -> line;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
199 if (!*optr || *optr == '*' || *optr == ';')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
200 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
201 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
202 cl -> remainder = cl -> line;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
203 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
204 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
205
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
206 if (!isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
207 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
208 symbol = parse_symbol(as, &optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
209 if (*optr && !isspace(*optr) && !(as -> inmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
210 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
211 errorp1(ERR_BADSYM);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
212 while (*optr && !isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
213 optr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
214 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
215 if (symbol)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
216 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
217 cl -> symstr = strdup(symbol);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
218 cl -> hassym = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
219 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
220 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
221
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
222 while (isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
223 optr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
224
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
225 // parse opcode
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
226 if (*optr && *optr != ';')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
227 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
228 c = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
229 while (c < MAX_OP_LEN && *optr && !isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
230 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
231 opbuf[c++] = *optr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
232 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
233 opbuf[c] = '\0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
234 if (*optr && !isspace(*optr) && !(as -> inmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
235 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
236 errorp1(ERR_BADOP);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
237 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
238 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
239 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
240 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
241 cl -> opcstr = strdup(opbuf);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
242 for (c = 0; instab[c].opcode; c++)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
243 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
244 if (!strcasecmp(opbuf, instab[c].opcode))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
245 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
246 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
247 if (!instab[c].opcode && opbuf[0] == '*')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
248 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
249 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
250 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
251 else if (!instab[c].opcode && !(as -> inmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
252 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
253 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
254
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
255 // look up macro
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
256 if (as -> macros)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
257 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
258 macrotab_t *m;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
259
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
260 for (m = as -> macros; m; m = m -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
261 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
262 if (!strcmp(m -> name, opbuf))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
263 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
264 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
265 if (m)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
266 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
267 // we have a macro here
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
268 cl -> macro = m;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
269 while (*optr && isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
270 optr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
271 expand_macro(as, cl, &optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
272 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
273 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
274 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
275 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
276 errorp1(ERR_BADOP);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
277 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
278 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
279 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
280 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
281 errorp1(ERR_BADOP);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
282 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
283 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
284 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
285 cl -> opcode = c;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
286 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
287 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
288 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
289 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
290
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
291 if (as -> inmacro && cl -> opcode >= 0 && instab[cl -> opcode].specialnum != SPECIAL_ENDM)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
292 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
293 add_macro_line(as, cl, cl -> line);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
294 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
295 cl -> remainder = cl -> line;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
296 cl -> opcstr = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
297 cl -> operstr = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
298 cl -> symstr = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
299 cl -> hassym = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
300 cl -> macrodef = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
301 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
302 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
303 // parse operand
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
304 while (*optr && isspace(*optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
305 optr++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
306
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
307 cl -> operstr = optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
308 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
309 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
310 optr = cl -> operstr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
311
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
312 if (as -> skipcond)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
313 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
314 // if skipping a condition, need to skip a macro
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
315 if (cl -> opcode >= 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
316 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
317 if (instab[cl -> opcode].specialnum == SPECIAL_MACRO)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
318 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
319 as -> skipmacro = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
320 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
321 else if (instab[cl -> opcode].specialnum == SPECIAL_ENDM)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
322 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
323 as -> skipmacro = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
324 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
325 else if (instab[cl -> opcode].specialnum == SPECIAL_COND && !(as -> skipmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
326 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
327 as -> skipcount++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
328 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
329 else if (instab[cl -> opcode].specialnum == SPECIAL_ENDC && !(as -> skipmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
330 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
331 as -> skipcount--;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
332 if (as -> skipcount <= 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
333 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
334 as -> skipcond = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
335 as -> noelse = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
336 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
337 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
338 else if (instab[cl -> opcode].specialnum == SPECIAL_ELSE && !(as -> skipmacro))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
339 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
340 if (as -> skipcount == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
341 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
342 as -> skipcount = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
343 as -> skipcond = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
344 as -> noelse = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
345 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
346 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
347 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
348 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
349 if (as -> skipcond)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
350 cl -> skipped = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
351 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
352 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
353
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
354 // do the code thing
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
355 // on pass 1, no code is generated
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
356 // on pass 2, code is generated using the "emit()" macro
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
357 if (cl -> opcode >= 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
358 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
359 if (instab[cl -> opcode].opfn)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
360 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
361 (*(instab[cl -> opcode].opfn))(as, cl, &optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
362 if (as -> passnum == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
363 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
364 if (*optr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
365 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
366 char *t = optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
367 char t2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
368
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
369 t2 = *optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
370 cl -> operstr = strdup(cl -> operstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
371 *optr = t2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
372 while (*t && isspace(*t))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
373 t++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
374 cl -> remainder = strdup(t);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
375
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
376 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
377 cl -> remainder = optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
378 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
379 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
380 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
381 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
382 errorp1(ERR_BADOP);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
383 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
384 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
385 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
386 // address of the symbol may have been changed by a pseudo op
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
387 // so we couldn't register it above
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
388 // that means it may turn out to be a "forward ref" in pass 1
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
389 if (cl -> hassym)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
390 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
391 register_symbol(as, cl, cl -> symstr, cl -> code_symloc, cl -> isset ? SYMFLAG_SET : SYMFLAG_NONE);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
392 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
393
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
394 as -> addr += cl -> len;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
395 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
396
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
397 void generate_code(asmstate_t *as)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
398 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
399 sourceline_t *cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
400
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
401 as -> addr = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
402 as -> dpval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
403 as -> passnum = 2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
404 for (cl = as -> source_head; cl; cl = cl -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
405 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
406 resolve_insn(as, cl);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
407 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
408 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
409
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
410 void lwasm_read_file(asmstate_t *as, char *fname)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
411 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
412 FILE *f;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
413 int cline = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
414 sourceline_t *cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
415 size_t bufflen;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
416 char *buff = NULL;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
417 int retval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
418
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
419 as -> passnum = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
420
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
421 f = fopen(fname, "r");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
422 if (!f)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
423 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
424 fprintf(stderr, "Cannot open input file %s: %s\n", fname, strerror(errno));
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
425 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
426 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
427
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
428 while (!feof(f))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
429 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
430 retval = getline(&buff, &bufflen, f);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
431 debug(" read line (%s:%d): %s\n", fname, cline, buff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
432 if (retval < 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
433 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
434 if (feof(f))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
435 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
436 fprintf(stderr, "Error reading '%s': %s\n", fname, strerror(errno));
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
437 exit(1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
438 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
439 if (strchr(buff, '\n'))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
440 *strchr(buff, '\n') = '\0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
441 if (strchr(buff, '\r'))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
442 *strchr(buff, '\r') = '\0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
443 cl = calloc(sizeof(sourceline_t), 1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
444 if (!cl)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
445 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
446 perror("Malloc");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
447 exit(1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
448 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
449
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
450 cl -> lineno = cline++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
451 cl -> sourcefile = fname;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
452 cl -> opcode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
453 cl -> addrmode = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
454 cl -> addr = as -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
455 cl -> dpval = as -> dpval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
456 cl -> prev = as -> source_tail;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
457 if (as -> source_tail)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
458 as -> source_tail -> next = cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
459 as -> source_tail = cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
460 if (as -> source_head == NULL)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
461 as -> source_head = cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
462 cl -> line = strdup(buff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
463
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
464 resolve_insn(as, cl);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
465
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
466 if (cl -> opcode >= 0 && instab[cl -> opcode].instype == INSTYPE_PSEUDO && instab[cl -> opcode].specialnum == SPECIAL_END)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
467 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
468
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
469 *buff = '\0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
470
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
471 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
472 if (buff)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
473 free(buff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
474
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
475 fclose(f);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
476
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
477 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
478 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
479
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
480 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
481 below this point is the expression evaluation package
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
482
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
483 Supported binary operators: + - / * %
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
484 Supported unary operators: -
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
485
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
486 <infix>: + | - | * | / | %
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
487 <unary>: -
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
488 <expr>: <term> <infix> <term>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
489 <term>: <unary> <term>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
490 <term>: ( <expr> )
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
491 <term>: <symbol>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
492 <term>: ' <char>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
493 <term>: " <char> <char>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
494 <term>: *
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
495 <term>: <number>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
496
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
497 <number>: <dec>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
498 <number>: & <dec>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
499
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
500 <number>: $ <hex>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
501 <number>: <hex> H
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
502 <number>: @ <oct>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
503 <number>: <oct> O
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
504 <number>: <oct> Q
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
505
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
506 <number>: % <bin>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
507 <number>: <bin> B
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
508
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
509 <bin>: 0 | 1
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
510 <oct>: <bin> | 2 | 3 | 4 | 5 | 6 | 7
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
511 <dec>: <oct> | 8 | 9
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
512 <hex>: <dec> | A | B | C | D | E | F
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
513
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
514 NOTE: hex values which start with a non-digit will need to be prefixed
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
515 by $ or have a 0 as the leading digit; hence: $CC or 0CCH otherwise the
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
516 assembler cannot tell the difference between CCH as a symbol or CCH as
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
517 the value $CC
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
518
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
519 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
520
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
521 // will throw an error and return 0 in tval if there's a problem
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
522 // -1 is problem; cl -> undef set is undefined symbol
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
523 int eval_term(asmstate_t *as, sourceline_t *cl, char **optr, int *tval)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
524 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
525 char tc;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
526 int rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
527 int binval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
528 int octval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
529 int decval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
530 int hexval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
531 int valtype;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
532 int digval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
533 int bindone = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
534
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
535 *tval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
536
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
537 beginagain:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
538 tc = **optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
539 if (tc == '+')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
540 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
541 // unary +, ignored for symetry
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
542 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
543 goto beginagain;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
544 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
545
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
546 if (tc == '(')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
547 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
548 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
549 rval = eval_expr(as, cl, optr, tval);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
550 if (rval < 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
551 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
552 if (**optr != ')')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
553 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
554 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
555 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
556 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
557 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
558 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
559 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
560
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
561 if (tc == '-')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
562 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
563 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
564 rval = eval_term(as, cl, optr, tval);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
565 if (rval < 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
566 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
567 *tval = -*tval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
568 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
569 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
570
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
571 // current address (of current instruction, not PC)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
572 if (tc == '*')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
573 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
574 *tval = cl -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
575 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
576 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
577 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
578
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
579 if (strchr("abcdefghijklmnopqrstuvwxyz_", tolower(tc)))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
580 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
581 // evaluate a symbol
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
582 char *symbuf;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
583
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
584 symbuf = parse_symbol(as, optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
585 if (!symbuf)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
586 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
587 errorp1(ERR_BADSYM);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
588 *tval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
589 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
590 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
591
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
592 debug(" looking up symbol: %s\n", symbuf);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
593 *tval = lookup_symbol(as, symbuf);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
594
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
595 // if not found, flag forward ref
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
596 if (*tval == -1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
597 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
598 errorp2(ERR_UNDEF);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
599 cl -> undef = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
600 *tval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
601 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
602 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
603 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
604 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
605
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
606 if (tc == '%')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
607 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
608 // binary number
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
609 int v1 = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
610 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
611 while (strchr("01", **optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
612 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
613 v1 = v1 << 1 | ((*(*optr)++) - '0');
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
614 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
615 *tval = v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
616 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
617 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
618 if (tc == '$')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
619 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
620 // hex number
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
621 int v1 = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
622 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
623 debug("HEX CONST: %s\n", *optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
624 while (**optr && strchr("01234567890ABCDEF", toupper(tc = **optr)))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
625 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
626 debug("HEX 2: %02x\n", tc);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
627 if (**optr >= 'A')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
628 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
629 v1 = v1 << 4 | (toupper((*(*optr)++)) - 'A' + 10);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
630 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
631 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
632 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
633 v1 = v1 << 4 | ((*(*optr)++) - '0');
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
634 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
635 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
636 *tval = v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
637 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
638 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
639 if (tc == '@')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
640 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
641 // octal number
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
642 int v1 = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
643 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
644 while (strchr("01234567", **optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
645 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
646 v1 = v1 << 3 | ((*(*optr)++) - '0');
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
647 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
648 *tval = v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
649 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
650 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
651 if (tc == '&')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
652 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
653 // decimal number
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
654 int v1 = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
655 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
656 while (strchr("0123456789", **optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
657 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
658 v1 = v1 * 10 + ((*(*optr)++) - '0');
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
659 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
660 *tval = v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
661 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
662 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
663 if (tc == '\'')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
664 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
665 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
666 if (!**optr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
667 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
668 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
669 return -2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
670 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
671 *tval = *(*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
672 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
673 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
674 if (tc == '"')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
675 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
676 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
677 if (!**optr || !*(*optr + 1))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
678 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
679 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
680 return -2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
681 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
682 *tval = *(*optr)++ << 8 | *(*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
683 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
684 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
685 // end of string
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
686 if (tc == '\0')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
687 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
688 // error if at EOS as we are looking for a term
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
689 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
690 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
691 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
692
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
693 // we have a generic number here which may be decimal, hex, binary, or octal
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
694 // based on a suffix
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
695
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
696 // possible data types are binary (1), octal (2), decimal(4), hex (8)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
697 valtype = 15;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
698 hexval = octval = decval = binval = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
699 while (1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
700 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
701
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
702 // printf(" %c\n", **optr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
703 if (!**optr || !strchr("ABCDEFabcdefqhoQHO0123456789", **optr))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
704 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
705 // end of string, must be decimal or the end of a bin
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
706 if (bindone == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
707 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
708 *tval = binval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
709 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
710 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
711 if (valtype & 4)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
712 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
713 *tval = decval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
714 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
715 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
716 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
717 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
718 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
719 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
720 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
721 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
722 tc = toupper(*(*optr)++);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
723
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
724 if (tc == 'H')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
725 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
726 if (valtype & 8)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
727 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
728 *tval = hexval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
729 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
730 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
731 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
732 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
733 // syntax error
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
734 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
735 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
736 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
737 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
738
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
739 if (tc == 'Q' || tc == 'O')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
740 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
741 if (valtype && 2)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
742 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
743 *tval = octval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
744 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
745 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
746 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
747 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
748 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
749 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
750 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
751 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
752
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
753 digval = tc - '0';
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
754 if (digval > 9)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
755 digval -= 7;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
756
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
757 // if it's not in the range of a hex digit, error out
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
758 if (tc < '0' || (tc > '9' && tc < 'A') || tc > 'F')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
759 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
760 (*optr)--;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
761 if (valtype & 4)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
762 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
763 *tval = decval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
764 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
765 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
766 // if we're in hex/bin mode and run to the end of the number
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
767 // we must have a binary constant or an error
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
768 // if the previous character is B, then we have binary
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
769 // else we have error since hex would require a terminating H
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
770 // which would be caught above
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
771 if (valtype == 8 && toupper(*(*optr)) == 'B')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
772 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
773 *tval = binval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
774 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
775 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
776 errorp1(ERR_BADEXPR);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
777 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
778 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
779
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
780 // if we have any characters past the end of the B, it's not binary
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
781 if (bindone == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
782 bindone = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
783 if (tc == 'B')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
784 bindone = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
785 if (digval > 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
786 valtype &= 14;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
787 else if (digval > 7)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
788 valtype &= 13;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
789 else if (digval > 9)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
790 valtype &= 11;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
791
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
792 if (valtype & 8)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
793 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
794 hexval = (hexval << 4) | digval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
795 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
796 if (valtype & 4)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
797 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
798 decval = decval * 10 + digval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
799 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
800 if (valtype & 2)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
801 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
802 octval = (octval << 3) | digval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
803 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
804 if (valtype & 1 && !bindone)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
805 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
806 binval = (binval << 1) | digval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
807 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
808
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
809 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
810 // can't get here from there
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
811 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
812
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
813 // returns -1 if the expression cannot be parsed
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
814 // and returns -2 if there is an undefined symbol reference
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
815 // resulting value will be in *val; undefined symbols are parsed as
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
816 // value 0 but cl -> undef will be set.
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
817 int eval_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *val)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
818 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
819 int left;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
820 int right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
821 char oper;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
822 int rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
823
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
824 // by default, return 0 in val
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
825 *val = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
826 cl -> undef = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
827
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
828 rval = eval_term(as, cl, optr, &left);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
829 if (rval < 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
830 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
831
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
832 nextop:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
833 oper = **optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
834
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
835 // end of expr
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
836 if (isspace(oper) || oper == ',' || oper == '\0' || oper == ']' || oper == ')')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
837 goto retleft;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
838
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
839 // unrecognized chars
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
840 if (!strchr("+-*/%", oper))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
841 goto retleft;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
842
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
843 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
844
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
845 rval = eval_term(as, cl, optr, &right);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
846 // propagate error
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
847 if (rval < 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
848 return rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
849
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
850 // do the operation and put it in "left"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
851 switch (oper)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
852 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
853 case '+':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
854 left += right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
855 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
856
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
857 case '-':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
858 left -= right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
859 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
860
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
861 case '*':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
862 left *= right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
863 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
864
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
865 case '/':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
866 left /= right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
867 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
868
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
869 case '%':
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
870 left %= right;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
871 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
872 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
873
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
874 goto nextop;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
875
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
876 retleft:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
877 *val = left;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
878 return 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
879 }