Mercurial > hg-old > index.cgi
annotate lwasm/insn_rel.c @ 162:02ada556bcc0
Fixed stupid error with for loop
author | lost |
---|---|
date | Sat, 31 Jan 2009 07:03:09 +0000 |
parents | 427e268e876b |
children | 383caf808674 |
rev | line source |
---|---|
29 | 1 /* |
2 insn_rel.c | |
33
74a3fef7c8d0
Added general addressing modes (immediate, base page, extended, indexed)
lost
parents:
32
diff
changeset
|
3 Copyright © 2009 William Astle |
29 | 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 | |
32 | 25 #define __insn_rel_c_seen__ |
29 | 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; | |
77 | 36 lwasm_expr_term_t *t; |
37 int r; | |
29 | 38 |
39 lwasm_emitop(as, l, instab[opnum].ops[0]); | |
77 | 40 |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
41 if ((r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0)) < 0) |
77 | 42 v = 0; |
43 else | |
29 | 44 { |
77 | 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 } | |
29 | 62 } |
77 | 63 if (r == 1 && as -> passnum == 2) |
64 { | |
65 register_error(as, l, 2, "Illegal external or intersegment reference"); | |
66 } | |
29 | 67 if (v < -128 || v > 127) |
68 register_error(as, l, 2, "Byte overflow"); | |
69 lwasm_emit(as, l, v & 0xff); | |
70 } | |
71 | |
77 | 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 */ | |
29 | 76 OPFUNC(insn_rel16) |
77 { | |
78 int v; | |
77 | 79 int r; |
80 lwasm_expr_term_t *t; | |
29 | 81 |
82 lwasm_emitop(as, l, instab[opnum].ops[0]); | |
83 | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
84 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0); |
83 | 85 if (r < 0) |
59 | 86 v = 0; |
77 | 87 else |
88 { | |
89 if (as -> passnum == 1) | |
90 { | |
91 // need to adjust the expression | |
92 if (l -> exprs[0]) | |
93 { | |
83 | 94 t = lwasm_expr_term_create_int(as -> addr + 2); |
77 | 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) | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
108 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
109 // since we have a reference outside this section, add |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
110 // a subtract of the section base to get the right value |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
111 // upon linking |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
112 t = lwasm_expr_term_create_secbase(); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
113 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
114 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
115 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
116 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
117 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
118 |
77 | 119 l -> relocoff = as -> addr - l -> codeaddr; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
120 } |
29 | 121 lwasm_emit(as, l, (v >> 8) & 0xff); |
122 lwasm_emit(as, l, v & 0xff); | |
123 } |