Mercurial > hg-old > index.cgi
annotate src/lwasm.c @ 60:309810f39ab7
Implemented the * and . special symbols
author | lost |
---|---|
date | Mon, 05 Jan 2009 00:44:38 +0000 |
parents | b1d81800bc91 |
children | c8c772ef5df9 |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 lwasm.c |
55 | 3 Copyright © 2009 William Astle |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
26 | 20 |
21 Contains random functions used by the assembler | |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
22 */ |
0 | 23 |
26 | 24 #define __lwasm_c_seen__ |
25 | |
26 #include <stdarg.h> | |
0 | 27 #include <stdlib.h> |
26 | 28 #include <stdio.h> |
29 | |
0 | 30 #include "lwasm.h" |
26 | 31 #include "util.h" |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
32 #include "expr.h" |
0 | 33 |
38 | 34 int debug_level = 0; |
35 | |
26 | 36 int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...) |
0 | 37 { |
26 | 38 lwasm_error_t *e; |
39 va_list args; | |
40 char errbuff[1024]; | |
41 int r; | |
0 | 42 |
26 | 43 if (as -> passnum != pass) |
44 return; | |
45 | |
46 va_start(args, fmt); | |
0 | 47 |
26 | 48 e = lwasm_alloc(sizeof(lwasm_error_t)); |
49 | |
50 e -> next = l -> err; | |
51 l -> err = e; | |
0 | 52 |
53 as -> errorcount++; | |
54 | |
26 | 55 r = vsnprintf(errbuff, 1024, fmt, args); |
56 e -> mess = lwasm_strdup(errbuff); | |
0 | 57 |
26 | 58 va_end(args); |
0 | 59 |
26 | 60 return r; |
0 | 61 } |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
62 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
63 void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
64 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
65 as -> addr += 1; |
58 | 66 as -> addr &= 0xffff; |
67 | |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
68 if (as -> passnum == 1) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
69 return; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
70 |
42 | 71 |
72 if (l -> codelen >= l -> codesize) | |
73 { | |
74 l -> bytes = realloc(l -> bytes, l -> codesize + 16); | |
75 l -> codesize += 16; | |
76 } | |
77 l -> bytes[l -> codelen] = b & 0xff; | |
78 l -> codelen += 1; | |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
79 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
80 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
81 void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
82 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
83 if (o >= 0x100) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
84 lwasm_emit(as, l, o >> 8); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
85 lwasm_emit(as, l, o & 0xff); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
86 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
87 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
88 int lwasm_lookupreg2(const char *reglist, char **str) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
89 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
90 int rval = 0; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
91 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
92 while (*reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
93 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
94 if (toupper(**str) == *reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
95 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
96 // first char matches |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
97 if (reglist[1] == ' ' && !isalpha(*(*str + 1))) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
98 break; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
99 if (toupper(*(*str + 1)) == reglist[1]) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
100 break; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
101 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
102 reglist += 2; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
103 rval++; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
104 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
105 if (!*reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
106 return -1; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
107 if (reglist[1] == ' ') |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
108 (*str)++; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
109 else |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
110 (*str) += 2; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
111 return rval; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
112 } |
32 | 113 |
114 int lwasm_lookupreg3(const char *rlist, const char **str) | |
115 { | |
116 int rval = 0; | |
117 int f = 0; | |
118 const char *reglist = rlist; | |
119 | |
120 while (*reglist) | |
121 { | |
122 if (toupper(**str) == *reglist) | |
123 { | |
124 // first char matches | |
125 if (reglist[1] == ' ') | |
126 { | |
127 f = 1; | |
128 break; | |
129 } | |
130 if (toupper(*(*str + 1)) == reglist[1]) | |
131 { | |
132 // second char matches | |
133 if (reglist[2] == ' ') | |
134 { | |
135 f = 1; | |
136 break; | |
137 } | |
138 if (toupper(*(*str + 2)) == reglist[2]) | |
139 { | |
140 f = 1; | |
141 break; | |
142 } | |
143 } | |
144 } | |
145 reglist += 3; | |
146 rval++; | |
147 } | |
148 if (f == 0) | |
149 return -1; | |
150 | |
151 | |
152 reglist = rval * 3 + rlist; | |
153 if (reglist[1] == ' ') | |
154 (*str) += 1; | |
155 else if (reglist[2] == ' ') | |
156 (*str) += 2; | |
157 else | |
158 (*str)+=3; | |
159 return rval; | |
160 } | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
161 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
162 struct symstateinfo |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
163 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
164 asmstate_t *as; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
165 lwasm_line_t *l; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
166 }; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
167 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
168 int lwasm_expr_lookup_symbol(char *sym, void *state, int *val) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
169 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
170 lwasm_symbol_ent_t *se; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
171 struct symstateinfo *st; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
172 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
173 st = state; |
52 | 174 debug_message(3, "lwasm_expr_lookup_symbol(): find '%s' (context=%d)", sym, st -> as -> context); |
175 | |
60 | 176 // check for special symbols first... |
177 if (sym[1] == '\0') | |
178 { | |
179 switch (sym[0]) | |
180 { | |
181 // current line address | |
182 case '*': | |
183 case '.': | |
184 *val = st -> l -> codeaddr; | |
185 return 0; | |
186 | |
187 case '<': | |
188 // previous branch point | |
189 // not implemented | |
190 break; | |
191 case '>': | |
192 // next branch point | |
193 // not implemented | |
194 break; | |
195 } | |
196 } | |
197 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
198 // look for local symbol first then global symbol |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
199 se = lwasm_find_symbol(st -> as, sym, st -> as -> context); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
200 if (!se) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
201 se = lwasm_find_symbol(st -> as, sym, -1); |
52 | 202 debug_message(3, "lwasm_expr_lookup_symbol(): got '%p'", se); |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
203 if (!se) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
204 return -1; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
205 *val = se -> value; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
206 return 0; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
207 } |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
208 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
209 lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
210 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
211 struct symstateinfo st; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
212 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
213 st.as = as; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
214 st.l = l; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
215 |
38 | 216 debug_message(2, "Evaluate expression: %s", inp); |
217 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
218 return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st)); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
219 } |
38 | 220 |
55 | 221 int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val) |
222 { | |
223 lwasm_expr_stack_t *s; | |
58 | 224 const char *ep; |
55 | 225 int rval; |
226 | |
227 s = lwasm_evaluate_expr(as, l, *inp, &ep); | |
228 if (!s) | |
229 { | |
230 register_error(as, l, 1, "Bad expression"); | |
231 *val = 0; | |
232 return -1; | |
233 } | |
58 | 234 *inp = (char *)ep; |
55 | 235 |
236 if (flag & EXPR_PASS1CONST && as -> passnum == 1 && !lwasm_expr_is_constant(s)) | |
237 { | |
238 register_error(as, l, 1, "Illegal incomplete reference (pass 1)"); | |
239 *val = 0; | |
240 lwasm_expr_stack_free(s); | |
241 return -1; | |
242 } | |
243 if (flag & EXPR_PASS2CONST && as -> passnum == 2 && !lwasm_expr_is_constant(s)) | |
244 { | |
245 register_error(as, l, 2, "Incomplete reference (pass 2)"); | |
246 *val = 0; | |
247 lwasm_expr_stack_free(s); | |
248 return -1; | |
249 } | |
250 *val = lwasm_expr_get_value(s); | |
251 lwasm_expr_stack_free(s); | |
252 | |
253 if (flag & EXPR_BYTE && as -> passnum == 2 && (*val < -128 || *val > 255)) | |
254 { | |
255 register_error(as, l, 2, "Byte overflow"); | |
256 *val &= 0xff; | |
257 return -1; | |
258 } | |
259 if (flag & EXPR_BYTE) | |
260 { | |
261 *val &= 0xff; | |
262 } | |
263 | |
264 return 0; | |
265 } | |
266 | |
38 | 267 void debug_message(int level, const char *fmt, ...) |
268 { | |
269 va_list args; | |
270 | |
271 va_start(args, fmt); | |
272 if (debug_level >= level) | |
273 { | |
39 | 274 if (level > 0) |
275 fprintf(stderr, "DEBUG %d: ", level); | |
38 | 276 vfprintf(stderr, fmt, args); |
277 fputc('\n', stderr); | |
278 } | |
279 va_end(args); | |
280 } | |
57 | 281 |
282 int lwasm_next_context(asmstate_t *as) | |
283 { | |
58 | 284 int r; |
285 r = as -> nextcontext; | |
286 as -> nextcontext += 1; | |
287 debug_message(3, "lwasm_next_context(): %d (%d) pass %d", r, as -> nextcontext, as -> passnum); | |
288 return r; | |
57 | 289 } |
290 |