29
|
1 /*
|
|
2 insn_rel.c
|
|
3 Copyright © 2008 William Astle
|
|
4
|
|
5 This file is part of LWASM.
|
|
6
|
|
7 LWASM is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License along with
|
|
18 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 for handling relative mode instructions
|
|
23 */
|
|
24
|
|
25 #define __insn_inh_c_seen__
|
|
26
|
|
27 #include <stdlib.h>
|
|
28
|
|
29 #include "expr.h"
|
|
30 #include "lwasm.h"
|
|
31 #include "instab.h"
|
|
32
|
|
33 OPFUNC(insn_rel8)
|
|
34 {
|
|
35 lwasm_expr_stack_t *s;
|
|
36 int v;
|
|
37
|
|
38 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
39
|
|
40 s = lwasm_expr_eval(*p, NULL);
|
|
41 if (!s)
|
|
42 {
|
|
43 register_error(as, l, 1, "Bad operand");
|
|
44 lwasm_emitop(as, l, 0);
|
|
45 return;
|
|
46 }
|
|
47 if (lwasm_expr_is_constant(s))
|
|
48 register_error(as, l, 2, "Incomplete reference");
|
|
49 v = lwasm_expr_get_value(s);
|
|
50 v -= as -> addr + 1;
|
|
51 if (v < -128 || v > 127)
|
|
52 register_error(as, l, 2, "Byte overflow");
|
|
53 lwasm_emit(as, l, v & 0xff);
|
|
54
|
|
55 lwasm_expr_stack_free(s);
|
|
56 }
|
|
57
|
|
58 OPFUNC(insn_rel16)
|
|
59 {
|
|
60 lwasm_expr_stack_t *s;
|
|
61 int v;
|
|
62
|
|
63 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
64
|
|
65 s = lwasm_expr_eval(*p, NULL);
|
|
66 if (!s)
|
|
67 {
|
|
68 register_error(as, l, 1, "Bad operand");
|
|
69 lwasm_emitop(as, l, 0);
|
|
70 return;
|
|
71 }
|
|
72 if (lwasm_expr_is_constant(s))
|
|
73 register_error(as, l, 2, "Incomplete reference");
|
|
74 v = lwasm_expr_get_value(s);
|
|
75 v -= as -> addr + 2;
|
|
76 lwasm_emit(as, l, (v >> 8) & 0xff);
|
|
77 lwasm_emit(as, l, v & 0xff);
|
|
78
|
|
79 lwasm_expr_stack_free(s);
|
|
80 }
|