339
|
1 /*
|
|
2 insn_rel.c
|
|
3 Copyright © 2009 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 #include <config.h>
|
|
26 #include <stdlib.h>
|
|
27
|
357
|
28 #include <lw_expr.h>
|
|
29
|
339
|
30 #include "lwasm.h"
|
|
31 #include "instab.h"
|
|
32
|
357
|
33 PARSEFUNC(insn_parse_rel8)
|
339
|
34 {
|
|
35 int v;
|
357
|
36 lw_expr_t t, e1, e2;
|
339
|
37 int r;
|
|
38
|
357
|
39 // sometimes there is a "#", ignore if there
|
339
|
40 if (**p == '#')
|
|
41 (*p)++;
|
|
42
|
357
|
43 t = lwasm_parse_expr(as, p);
|
|
44 if (!t)
|
339
|
45 {
|
357
|
46 lwasm_register_error(as, l, "Bad operand");
|
|
47 return;
|
339
|
48 }
|
357
|
49 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
50
|
|
51 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
|
|
52 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
|
|
53 lw_expr_destroy(e1);
|
|
54 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
|
|
55 lw_expr_destroy(e2);
|
371
|
56 lwasm_save_expr(l, 0, e1);
|
339
|
57 }
|
|
58
|
357
|
59 EMITFUNC(insn_emit_rel8)
|
|
60 {
|
|
61 lw_expr_t e;
|
|
62 int offs;
|
|
63
|
|
64 e = lwasm_fetch_expr(l, 0);
|
|
65 if (!lw_expr_istype(e, lw_expr_type_int))
|
|
66 {
|
|
67 lwasm_register_error(as, l, "Illegal non-constant expression");
|
|
68 return;
|
|
69 }
|
|
70
|
|
71 offs = lw_expr_intval(e);
|
|
72 if (offs < -128 || offs > 127)
|
|
73 {
|
|
74 lwasm_register_error(as, l, "Byte overflow");
|
|
75 return;
|
|
76 }
|
|
77
|
|
78 lwasm_emitop(l, instab[l -> insn].ops[0]);
|
|
79 lwasm_emit(l, offs);
|
|
80 }
|
|
81
|
|
82 PARSEFUNC(insn_parse_rel16)
|
339
|
83 {
|
|
84 int v;
|
357
|
85 lw_expr_t t, e1, e2;
|
339
|
86 int r;
|
357
|
87
|
|
88 // sometimes there is a "#", ignore if there
|
339
|
89 if (**p == '#')
|
|
90 (*p)++;
|
|
91
|
357
|
92 t = lwasm_parse_expr(as, p);
|
|
93 if (!t)
|
339
|
94 {
|
357
|
95 lwasm_register_error(as, l, "Bad operand");
|
|
96 return;
|
339
|
97 }
|
357
|
98 l -> len = OPLEN(instab[l -> insn].ops[0]) + 2;
|
|
99
|
|
100 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
|
|
101 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
|
|
102 lw_expr_destroy(e1);
|
|
103 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
|
|
104 lw_expr_destroy(e2);
|
371
|
105 lwasm_save_expr(l, 0, e1);
|
357
|
106 }
|
339
|
107
|
357
|
108 EMITFUNC(insn_emit_rel16)
|
|
109 {
|
|
110 lw_expr_t e;
|
|
111 int offs;
|
|
112
|
|
113 e = lwasm_fetch_expr(l, 0);
|
|
114
|
|
115 lwasm_emitop(l, instab[l -> insn].ops[0]);
|
|
116 lwasm_emitexpr(l, e, 2);
|
339
|
117 }
|