comparison lwasm/insn_rel.c @ 151:427e268e876b

renamed src to lwasm to better reflect its purpose
author lost
date Fri, 30 Jan 2009 04:01:55 +0000
parents src/insn_rel.c@f59c0916753d
children 383caf808674
comparison
equal deleted inserted replaced
150:f0881c115010 151:427e268e876b
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
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 ((r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0)) < 0)
42 v = 0;
43 else
44 {
45 if (as -> passnum == 1)
46 {
47 // need to adjust the expression
48 if (l -> exprs[0])
49 {
50 t = lwasm_expr_term_create_int(as -> addr + 1);
51 lwasm_expr_stack_push(l -> exprs[0], t);
52 lwasm_expr_term_free(t);
53 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
54 lwasm_expr_stack_push(l -> exprs[0], t);
55 lwasm_expr_term_free(t);
56 }
57 else
58 {
59 l -> exprvals[0] -= as -> addr + 1;
60 }
61 }
62 }
63 if (r == 1 && as -> passnum == 2)
64 {
65 register_error(as, l, 2, "Illegal external or intersegment reference");
66 }
67 if (v < -128 || v > 127)
68 register_error(as, l, 2, "Byte overflow");
69 lwasm_emit(as, l, v & 0xff);
70 }
71
72 /*
73 External and intersegment references are adjusted for the relative addressing mode
74 by adjusting the expression on pass 1 and then treated as absolute references later
75 */
76 OPFUNC(insn_rel16)
77 {
78 int v;
79 int r;
80 lwasm_expr_term_t *t;
81
82 lwasm_emitop(as, l, instab[opnum].ops[0]);
83
84 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0);
85 if (r < 0)
86 v = 0;
87 else
88 {
89 if (as -> passnum == 1)
90 {
91 // need to adjust the expression
92 if (l -> exprs[0])
93 {
94 t = lwasm_expr_term_create_int(as -> addr + 2);
95 lwasm_expr_stack_push(l -> exprs[0], t);
96 lwasm_expr_term_free(t);
97 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
98 lwasm_expr_stack_push(l -> exprs[0], t);
99 lwasm_expr_term_free(t);
100 }
101 else
102 {
103 l -> exprvals[0] -= as -> addr + 2;
104 }
105 }
106 }
107 if (as -> passnum == 2 && r == 1)
108 {
109 // since we have a reference outside this section, add
110 // a subtract of the section base to get the right value
111 // upon linking
112 t = lwasm_expr_term_create_secbase();
113 lwasm_expr_stack_push(l -> exprs[0], t);
114 lwasm_expr_term_free(t);
115 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
116 lwasm_expr_stack_push(l -> exprs[0], t);
117 lwasm_expr_term_free(t);
118
119 l -> relocoff = as -> addr - l -> codeaddr;
120 }
121 lwasm_emit(as, l, (v >> 8) & 0xff);
122 lwasm_emit(as, l, v & 0xff);
123 }