annotate src/insn_gen.c @ 17:df0c4a46af8f

Started adding expression handling infrastructure
author lost
date Thu, 01 Jan 2009 02:26:26 +0000
parents 34568fab6058
children 74a3fef7c8d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
1 /*
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
2 insn_gen.c, Copyright © 2008 William Astle
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
3
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
4 This file is part of LWASM.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
5
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
6 LWASM is free software: you can redistribute it and/or modify it under the
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
7 terms of the GNU General Public License as published by the Free Software
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
8 Foundation, either version 3 of the License, or (at your option) any later
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
9 version.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
10
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
11 This program is distributed in the hope that it will be useful, but WITHOUT
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
14 more details.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
15
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
16 You should have received a copy of the GNU General Public License along with
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
17 this program. If not, see <http://www.gnu.org/licenses/>.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
18
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
19 Contains code for parsing general addressing modes (IMM+DIR+EXT+IND)
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
20 */
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
21
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
22 #include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 #include "instab.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 extern void insn_indexed_aux(asmstate_t *as, sourceline_t *cl, char **optr, int *b1, int *b2, int *b3);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 void insn_gen_aux(asmstate_t *as, sourceline_t *cl, char **optr, int *b1, int *b2, int *b3, int *op)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 *b1 = *b2 = *b3 = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 char *optr2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 int v1, tv, rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 optr2 = *optr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35 while (*optr2 && !isspace(*optr2) && *optr2 != ',') optr2++
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36 ;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37 if (*optr2 != ',' && **optr != '[')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
38 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39 // not indexed
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40 if (**optr == '<')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
41 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
43 rval = eval_expr(as, cl, optr, &v1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44 v1 = v1 & 0xffff;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 tv = v1 - ((cl -> dpval) << 8);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 if (tv < 0 || tv > 0xff)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48 errorp2(ERR_OVERFLOW);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 v1 = v1 & 0xff;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 goto ins_gen_dir;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 else if (**optr == '>')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55 // extended mode
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 rval = eval_expr(as, cl, optr, &v1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 goto ins_gen_ext;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 // eval expr and see how big it is
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 rval = eval_expr(as, cl, optr, &v1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 v1 = v1 & 0xFFFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 if (cl -> undef && as -> passnum == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 cl -> p1f16 = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 goto ins_gen_ext;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 tv = v1 - (cl -> dpval << 8);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 if (tv < 0 || tv > 0xff || cl -> p1f16)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
75 goto ins_gen_ext;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
76 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 v1 = v1 & 0xff;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79 goto ins_gen_dir;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
80 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
81 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
82 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
83 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85 *op = instab[cl -> opcode].ops[1];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 insn_indexed_aux(as, cl, optr, b1, b2, b3);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 ins_gen_dir:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 *op = instab[cl -> opcode].ops[0];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91 // cl -> code_operlen = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 cl -> addrmode = OPER_DIR;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 if ((v1 & 0xFFFF) > 0xff)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 errorp2(ERR_OVERFLOW);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 *b1 = (v1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98 ins_gen_ext:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99 *op = instab[cl -> opcode].ops[2];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 // cl -> code_operlen = 2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 cl -> addrmode = OPER_EXT;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102 *b1 = (v1 >> 8);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 *b2 = (v1 & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
105 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
107 void insn_gen(asmstate_t *as, sourceline_t *cl, char **optr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
109 int rval;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 int v1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111 int b1, b2, b3, op;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
112
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
113 if (**optr == '#')
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
114 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
115 // immediate mode has perterbations on instruction size
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
116 (*optr)++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
117 emitop(instab[cl -> opcode].ops[3]);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
118 rval = eval_expr(as, cl, optr, &v1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
119
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
120 switch (instab[cl -> opcode].instype)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
121 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
122 case INSTYPE_GEN8:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
123 case INSTYPE_IMM8:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
124 cl -> addrmode = OPER_IMM8;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
125 // cl -> code_operlen = 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
126 emit(v1 & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
127 if (v1 < -128 || v1 > 255)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
128 errorp2(ERR_OVERFLOW);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
129 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
130
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
131 case INSTYPE_GEN:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
132 cl -> addrmode = OPER_IMM16;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
133 // cl -> code_operlen = 2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
134 emit(v1 >> 8);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
135 emit(v1 & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
136 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
137
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
138 case INSTYPE_GEN32:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
139 cl -> addrmode = OPER_IMM32;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
140 // cl -> code_operlen = 4;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
141 emit(v1 >> 24);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
142 emit((v1 >> 16) & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
143 emit((v1 >> 8) & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
144 emit(v1 & 0xff);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
145 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
146
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
147 default:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
148 errorp1(ERR_BADOPER);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
149 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
150
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
151 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
152
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
153 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
154 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
155 if (instab[cl -> opcode].instype == INSTYPE_IMM8)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
156 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
157 errorp1(ERR_BADOPER);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
158 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
159 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
160
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
161 insn_gen_aux(as, cl, optr, &b1, &b2, &b3, &op);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
162 emitop(op);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
163 if (b1 != -1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
164 emit(b1);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
165 if (b2 != -1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
166 emit(b2);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
167 if (b3 != -1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
168 emit(b3);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
169 }