annotate src/insn_rel.c @ 37:538e15927776

Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
author lost
date Sat, 03 Jan 2009 04:20:49 +0000
parents 74a3fef7c8d0
children 89657cb3fdf8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
1 /*
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
2 insn_rel.c
33
74a3fef7c8d0 Added general addressing modes (immediate, base page, extended, indexed)
lost
parents: 32
diff changeset
3 Copyright © 2009 William Astle
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
4
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
5 This file is part of LWASM.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
6
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
10 version.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
11
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
15 more details.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
16
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
19 */
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
20
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
21 /*
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
22 for handling relative mode instructions
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
23 */
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
24
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 29
diff changeset
25 #define __insn_rel_c_seen__
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
26
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
27 #include <stdlib.h>
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
28
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
29 #include "expr.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
30 #include "lwasm.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
31 #include "instab.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
32
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
33 OPFUNC(insn_rel8)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
34 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
35 lwasm_expr_stack_t *s;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
36 int v;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
37
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
38 lwasm_emitop(as, l, instab[opnum].ops[0]);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
39
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 33
diff changeset
40 s = lwasm_evaluate_expr(as, l, *p, NULL);
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
41 if (!s)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
42 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
43 register_error(as, l, 1, "Bad operand");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
44 lwasm_emitop(as, l, 0);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
45 return;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
46 }
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
47 if (lwasm_expr_is_constant(s))
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
48 register_error(as, l, 2, "Incomplete reference");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
49 v = lwasm_expr_get_value(s);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
50 v -= as -> addr + 1;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
51 if (v < -128 || v > 127)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
52 register_error(as, l, 2, "Byte overflow");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
53 lwasm_emit(as, l, v & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
54
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
55 lwasm_expr_stack_free(s);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
56 }
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
57
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
58 OPFUNC(insn_rel16)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
59 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
60 lwasm_expr_stack_t *s;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
61 int v;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
62
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
63 lwasm_emitop(as, l, instab[opnum].ops[0]);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
64
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 33
diff changeset
65 s = lwasm_evaluate_expr(as, l, *p, NULL);
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
66 if (!s)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
67 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
68 register_error(as, l, 1, "Bad operand");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
69 lwasm_emitop(as, l, 0);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
70 return;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
71 }
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
72 if (lwasm_expr_is_constant(s))
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
73 register_error(as, l, 2, "Incomplete reference");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
74 v = lwasm_expr_get_value(s);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
75 v -= as -> addr + 2;
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
76 lwasm_emit(as, l, (v >> 8) & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
77 lwasm_emit(as, l, v & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
78
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
79 lwasm_expr_stack_free(s);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
80 }