comparison lwasm/insn_rel.c @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children 7317fbe024af
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
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 <stdlib.h>
26
27 #include <lw_expr.h>
28
29 #include "lwasm.h"
30 #include "instab.h"
31
32 PARSEFUNC(insn_parse_rel8)
33 {
34 int v;
35 lw_expr_t t, e1, e2;
36 int r;
37
38 // sometimes there is a "#", ignore if there
39 if (**p == '#')
40 (*p)++;
41
42 t = lwasm_parse_expr(as, p);
43 if (!t)
44 {
45 lwasm_register_error(as, l, "Bad operand");
46 return;
47 }
48 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
49
50 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
51 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
52 lw_expr_destroy(e1);
53 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
54 lw_expr_destroy(e2);
55 lwasm_save_expr(l, 0, e1);
56 }
57
58 EMITFUNC(insn_emit_rel8)
59 {
60 lw_expr_t e;
61 int offs;
62
63 e = lwasm_fetch_expr(l, 0);
64 if (!lw_expr_istype(e, lw_expr_type_int))
65 {
66 lwasm_register_error(as, l, "Illegal non-constant expression");
67 return;
68 }
69
70 offs = lw_expr_intval(e);
71 if (offs < -128 || offs > 127)
72 {
73 lwasm_register_error(as, l, "Byte overflow");
74 return;
75 }
76
77 lwasm_emitop(l, instab[l -> insn].ops[0]);
78 lwasm_emit(l, offs);
79 }
80
81 PARSEFUNC(insn_parse_rel16)
82 {
83 int v;
84 lw_expr_t t, e1, e2;
85 int r;
86
87 // sometimes there is a "#", ignore if there
88 if (**p == '#')
89 (*p)++;
90
91 t = lwasm_parse_expr(as, p);
92 if (!t)
93 {
94 lwasm_register_error(as, l, "Bad operand");
95 return;
96 }
97 l -> len = OPLEN(instab[l -> insn].ops[0]) + 2;
98
99 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
100 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
101 lw_expr_destroy(e1);
102 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
103 lw_expr_destroy(e2);
104 lwasm_save_expr(l, 0, e1);
105 }
106
107 EMITFUNC(insn_emit_rel16)
108 {
109 lw_expr_t e;
110 int offs;
111
112 e = lwasm_fetch_expr(l, 0);
113
114 lwasm_emitop(l, instab[l -> insn].ops[0]);
115 lwasm_emitexpr(l, e, 2);
116 }