annotate lwasm/insn_rel.c @ 267:bec6d2480f08 2.6

Added add'l generated files for release
author lost
date Tue, 22 Dec 2009 05:33:32 +0000
parents bae1e3ecdce1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
1 /*
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
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
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
4
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
5 This file is part of LWASM.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
6
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
10 version.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
11
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
15 more details.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
16
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
19 */
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
20
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
21 /*
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
22 for handling relative mode instructions
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
23 */
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
24
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 29
diff changeset
25 #define __insn_rel_c_seen__
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 196
diff changeset
26 #include <config.h>
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
27 #include <stdlib.h>
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
28
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
29 #include "expr.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
30 #include "lwasm.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
31 #include "instab.h"
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
32
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
33 OPFUNC(insn_rel8)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
34 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
35 int v;
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
36 lwasm_expr_term_t *t;
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
37 int r;
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
38
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
39 lwasm_emitop(as, l, instab[opnum].ops[0]);
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
40
195
383caf808674 Allow relative addressing to ignore a # before the operand
lost
parents: 151
diff changeset
41 if (**p == '#')
383caf808674 Allow relative addressing to ignore a # before the operand
lost
parents: 151
diff changeset
42 (*p)++;
383caf808674 Allow relative addressing to ignore a # before the operand
lost
parents: 151
diff changeset
43
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
44 if ((r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0)) < 0)
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
45 v = 0;
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
46 else
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
47 {
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
48 if (as -> passnum == 1)
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
49 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
50 // need to adjust the expression
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
51 if (l -> exprs[0])
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
52 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
53 t = lwasm_expr_term_create_int(as -> addr + 1);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
54 lwasm_expr_stack_push(l -> exprs[0], t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
55 lwasm_expr_term_free(t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
56 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
57 lwasm_expr_stack_push(l -> exprs[0], t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
58 lwasm_expr_term_free(t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
59 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
60 else
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
61 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
62 l -> exprvals[0] -= as -> addr + 1;
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
63 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
64 }
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
65 }
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
66 if (r == 1 && as -> passnum == 2)
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
67 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
68 register_error(as, l, 2, "Illegal external or intersegment reference");
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
69 }
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
70 if (v < -128 || v > 127)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
71 register_error(as, l, 2, "Byte overflow");
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
72 lwasm_emit(as, l, v & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
73 }
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
74
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
75 /*
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
76 External and intersegment references are adjusted for the relative addressing mode
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
77 by adjusting the expression on pass 1 and then treated as absolute references later
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
78 */
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
79 OPFUNC(insn_rel16)
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
80 {
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
81 int v;
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
82 int r;
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
83 lwasm_expr_term_t *t;
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
84
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
85 lwasm_emitop(as, l, instab[opnum].ops[0]);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
86
196
e7309c716059 Allow relative addressing to ignore a # before the operand (16 bit too!)
lost
parents: 195
diff changeset
87 if (**p == '#')
e7309c716059 Allow relative addressing to ignore a # before the operand (16 bit too!)
lost
parents: 195
diff changeset
88 (*p)++;
e7309c716059 Allow relative addressing to ignore a # before the operand (16 bit too!)
lost
parents: 195
diff changeset
89
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
90 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0);
83
964d68cde469 Fixed problems with re8 and rel16 addressing
lost
parents: 77
diff changeset
91 if (r < 0)
59
89657cb3fdf8 Moved insn_rel.c to simplified expression evaluation
lost
parents: 37
diff changeset
92 v = 0;
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
93 else
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
94 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
95 if (as -> passnum == 1)
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
96 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
97 // need to adjust the expression
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
98 if (l -> exprs[0])
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
99 {
83
964d68cde469 Fixed problems with re8 and rel16 addressing
lost
parents: 77
diff changeset
100 t = lwasm_expr_term_create_int(as -> addr + 2);
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
101 lwasm_expr_stack_push(l -> exprs[0], t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
102 lwasm_expr_term_free(t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
103 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
104 lwasm_expr_stack_push(l -> exprs[0], t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
105 lwasm_expr_term_free(t);
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
106 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
107 else
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
108 {
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
109 l -> exprvals[0] -= as -> addr + 2;
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
110 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
111 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
112 }
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
113 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
114 {
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
115 // 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
116 // 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
117 // upon linking
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
118 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
119 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
120 lwasm_expr_term_free(t);
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
121 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
122 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
123 lwasm_expr_term_free(t);
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 98
diff changeset
124
77
a338d496350e Checkpointing conversion to allow object target
lost
parents: 59
diff changeset
125 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
126 }
29
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
127 lwasm_emit(as, l, (v >> 8) & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
128 lwasm_emit(as, l, v & 0xff);
37aec845aef3 Added relative addressing handler
lost
parents:
diff changeset
129 }