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 #define __insn_rel_c_seen__
|
|
26 #include <config.h>
|
|
27 #include <stdlib.h>
|
|
28
|
|
29 #include "expr.h"
|
|
30 #include "lwasm.h"
|
|
31 #include "instab.h"
|
|
32
|
|
33 OPFUNC(insn_rel8)
|
|
34 {
|
|
35 int v;
|
|
36 lwasm_expr_term_t *t;
|
|
37 int r;
|
|
38
|
|
39 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
40
|
|
41 if (**p == '#')
|
|
42 (*p)++;
|
|
43
|
|
44 if ((r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0)) < 0)
|
|
45 v = 0;
|
|
46 else
|
|
47 {
|
|
48 if (as -> passnum == 1)
|
|
49 {
|
|
50 // need to adjust the expression
|
|
51 if (l -> exprs[0])
|
|
52 {
|
|
53 t = lwasm_expr_term_create_int(as -> addr + 1);
|
|
54 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
55 lwasm_expr_term_free(t);
|
|
56 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
|
|
57 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
58 lwasm_expr_term_free(t);
|
|
59 }
|
|
60 else
|
|
61 {
|
|
62 l -> exprvals[0] -= as -> addr + 1;
|
|
63 }
|
|
64 }
|
|
65 }
|
|
66 if (r == 1 && as -> passnum == 2)
|
|
67 {
|
|
68 register_error(as, l, 2, "Illegal external or intersegment reference");
|
|
69 }
|
|
70 if (v < -128 || v > 127)
|
|
71 register_error(as, l, 2, "Byte overflow");
|
|
72 lwasm_emit(as, l, v & 0xff);
|
|
73 }
|
|
74
|
|
75 /*
|
|
76 External and intersegment references are adjusted for the relative addressing mode
|
|
77 by adjusting the expression on pass 1 and then treated as absolute references later
|
|
78 */
|
|
79 OPFUNC(insn_rel16)
|
|
80 {
|
|
81 int v;
|
|
82 int r;
|
|
83 lwasm_expr_term_t *t;
|
|
84
|
|
85 lwasm_emitop(as, l, instab[opnum].ops[0]);
|
|
86
|
|
87 if (**p == '#')
|
|
88 (*p)++;
|
|
89
|
|
90 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0);
|
|
91 if (r < 0)
|
|
92 v = 0;
|
|
93 else
|
|
94 {
|
|
95 if (as -> passnum == 1)
|
|
96 {
|
|
97 // need to adjust the expression
|
|
98 if (l -> exprs[0])
|
|
99 {
|
|
100 t = lwasm_expr_term_create_int(as -> addr + 2);
|
|
101 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
102 lwasm_expr_term_free(t);
|
|
103 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
|
|
104 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
105 lwasm_expr_term_free(t);
|
|
106 }
|
|
107 else
|
|
108 {
|
|
109 l -> exprvals[0] -= as -> addr + 2;
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 if (as -> passnum == 2 && r == 1)
|
|
114 {
|
|
115 // since we have a reference outside this section, add
|
|
116 // a subtract of the section base to get the right value
|
|
117 // upon linking
|
|
118 t = lwasm_expr_term_create_secbase();
|
|
119 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
120 lwasm_expr_term_free(t);
|
|
121 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
|
|
122 lwasm_expr_stack_push(l -> exprs[0], t);
|
|
123 lwasm_expr_term_free(t);
|
|
124
|
|
125 l -> relocoff = as -> addr - l -> codeaddr;
|
|
126 }
|
|
127 lwasm_emit(as, l, (v >> 8) & 0xff);
|
|
128 lwasm_emit(as, l, v & 0xff);
|
|
129 }
|