annotate lwasm/insn_bitbit.c @ 242:848d55b181f0 2.x

Branched an "ongoing 2.x" stream from trunk in preparation for a major architecture rewrite
author lost
date Sun, 16 Aug 2009 18:34:13 +0000
parents bae1e3ecdce1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
1 /*
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
2 insn_bitbit.c
33
74a3fef7c8d0 Added general addressing modes (immediate, base page, extended, indexed)
lost
parents: 32
diff changeset
3 Copyright © 2009 William Astle
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
4
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
5 This file is part of LWASM.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
6
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
10 version.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
11
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
15 more details.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
16
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
19 */
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
20
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
21 /*
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
22 for handling inherent mode instructions
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
23 */
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
24
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
25 #define __insn_bitbit_c_seen__
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
26
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 151
diff changeset
27 #include <config.h>
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
28 #include <stdlib.h>
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
29
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
30 #include "lwasm.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
31 #include "instab.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
32 #include "expr.h"
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
33
80
8929e1ee99cf Checkpointing deployment of non-constant expression handling
lost
parents: 37
diff changeset
34 // these instructions cannot tolerate external references
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
35 OPFUNC(insn_bitbit)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
36 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
37 int r;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
38 lwasm_expr_stack_t *s;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
39 int v1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
40 int tv;
80
8929e1ee99cf Checkpointing deployment of non-constant expression handling
lost
parents: 37
diff changeset
41
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
42 lwasm_emitop(as, l, instab[opnum].ops[0]);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
43
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
44 r = toupper(*(*p)++);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
45 if (r == 'A')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
46 r = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
47 else if (r == 'B')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
48 r = 2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
49 else if (r == 'C' && toupper(**p) == 'C')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
50 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
51 r = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
52 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
53 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
54 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
55 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
56 register_error(as, l, 1, "Bad register");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
57 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
58 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
59 if (*(*p)++ != ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
60 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
61 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
62 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
63 }
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 80
diff changeset
64 s = lwasm_evaluate_expr(as, l, *p, NULL, 0);
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
65 if (!s)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
66 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
67 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
68 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
69 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
70 if (!lwasm_expr_is_constant(s))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
71 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
72 register_error(as, l, 2, "Incomplete reference");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
73 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
74 v1 = lwasm_expr_get_value(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
75 lwasm_expr_stack_free(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
76 if (v1 < 0 || v1 > 7)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
77 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
78 register_error(as, l, 2, "Invalid bit number");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
79 v1 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
80 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
81 if (*(*p)++ != ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
82 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
83 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
84 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
85 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
86 r = (r << 6) | (v1 << 3);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
87
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 80
diff changeset
88 s = lwasm_evaluate_expr(as, l, *p, NULL, 0);
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
89 if (!s)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
90 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
91 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
92 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
93 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
94 if (!lwasm_expr_is_constant(s))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
95 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
96 register_error(as, l, 1, "Incomplete reference");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
97 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
98 v1 = lwasm_expr_get_value(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
99 lwasm_expr_stack_free(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
100 if (v1 < 0 || v1 > 7)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
101 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
102 register_error(as, l, 2, "Invalid bit number");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
103 v1 = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
104 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
105 if (*(*p)++ != ',')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
106 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
107 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
108 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
109 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
110 r |= v1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
111
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
112 lwasm_emit(as, l, r);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
113
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
114 // ignore base page address modifier
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
115 if (**p == '<')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
116 (*p)++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
117
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 80
diff changeset
118 s = lwasm_evaluate_expr(as, l, *p, NULL, 0);
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
119 if (!s)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
120 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
121 register_error(as, l, 1, "Bad operand");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
122 return;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
123 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
124 if (!lwasm_expr_is_constant(s))
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
125 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
126 register_error(as, l, 1, "Incomplete reference");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
127 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
128 v1 = lwasm_expr_get_value(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
129 lwasm_expr_stack_free(s);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
130 v1 &= 0xFFFF;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
131
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
132 tv = v1 - ((as -> dpval) << 8);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
133 if (tv > 0xFF || tv < 0)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
134 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
135 register_error(as, l, 2, "Byte overflow");
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
136 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
137 lwasm_emit(as, l, tv & 0xff);
9bd0fbfe7405 Added basic indexed mode handling
lost
parents:
diff changeset
138 }