annotate lwasm/insn_tfm.c @ 369:898a41f7eb59

check for valid expressions in pass 6
author lost@starbug
date Thu, 15 Apr 2010 22:26:49 -0600
parents 0cf4948d53b4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 insn_tfm.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 #include <ctype.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include "instab.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
28 PARSEFUNC(insn_parse_tfm)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 static const char *reglist = "DXYUS AB 00EF";
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 int r0, r1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 char *c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 int tfm = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 c = strchr(reglist, toupper(*(*p)++));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 if (!c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
38 lwasm_register_error(as, l, "Unknown operation");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 r0 = c - reglist;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 if (**p == '+')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 tfm = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 else if (**p == '-')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 tfm = 2;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 if (*(*p)++ != ',')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
54 lwasm_register_error(as, l, "Unknown operation");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 c = strchr(reglist, toupper(*(*p)++));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 if (!c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
60 lwasm_register_error(as, l, "Unknown operation");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 r1 = c - reglist;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 if (**p == '+')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 tfm |= 4;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 else if (**p == '-')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 tfm |= 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 if (**p && !isspace(**p))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
78 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 // valid values of tfm here are:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 // 1: r0+,r1 (2)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 // 4: r0,r1+ (3)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 // 5: r0+,r1+ (0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 // 10: r0-,r1- (1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 switch (tfm)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 case 5: //r0+,r1+
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
90 l -> lint = instab[l -> insn].ops[0];
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 break;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
92
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 case 10: //r0-,r1-
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
94 l -> lint = instab[l -> insn].ops[1];
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 break;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
96
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 case 1: // r0+,r1
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
98 l -> lint = instab[l -> insn].ops[2];
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 break;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
100
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 case 4: // r0,r1+
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
102 l -> lint = instab[l -> insn].ops[3];
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 break;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
104
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 default:
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
106 lwasm_register_error(as, l, "Unknown operation");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 }
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
109 l -> pb = (r0 << 4) | r1;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
110 l -> len = OPLEN(l -> lint);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
113 EMITFUNC(insn_emit_tfm)
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
114 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
115 lwasm_emitop(l, l -> lint);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
116 lwasm_emit(l, l -> pb);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
117 }
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
118
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
119 PARSEFUNC(insn_parse_tfmrtor)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 int r0, r1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 static const char *regs = "D X Y U S A B 0 0 E F ";
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 // register to register (r0,r1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 // registers are in order:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 // D,X,Y,U,S,PC,W,V
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 // A,B,CC,DP,0,0,E,F
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 r0 = lwasm_lookupreg2(regs, p);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 if (r0 < 0 || *(*p)++ != ',')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
131 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 r0 = r1 = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 r1 = lwasm_lookupreg2(regs, p);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 if (r1 < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
139 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 r0 = r1 = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 }
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
143 l -> len = instab[l -> insn].ops[0] + 1;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
144 l -> pb = (r0 << 4) | r1;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 }
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
146
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
147 EMITFUNC(insn_emit_tfmrtor)
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
148 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
149 lwasm_emitop(l, instab[l -> insn].ops[0]);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
150 lwasm_emit(l, l -> pb);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
151 }