annotate lwasm/insn_rel.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 9c24d9d485b9
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_rel.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 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 for handling relative mode instructions
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdlib.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 #include <lw_expr.h>
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
29
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #include "instab.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
33 PARSEFUNC(insn_parse_rel8)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 int v;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
36 lw_expr_t t, e1, e2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 int r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
39 // sometimes there is a "#", ignore if there
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 if (**p == '#')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
43 t = lwasm_parse_expr(as, p);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
44 if (!t)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
46 lwasm_register_error(as, l, "Bad operand");
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
47 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 }
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
49 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
50
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
51 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
52 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
53 lw_expr_destroy(e1);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
54 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
55 lw_expr_destroy(e2);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
56 lwasm_save_expr(l, 0, e2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
59 EMITFUNC(insn_emit_rel8)
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
60 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
61 lw_expr_t e;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
62 int offs;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
63
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
64 e = lwasm_fetch_expr(l, 0);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
65 if (!lw_expr_istype(e, lw_expr_type_int))
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
66 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
67 lwasm_register_error(as, l, "Illegal non-constant expression");
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
68 return;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
69 }
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
70
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
71 offs = lw_expr_intval(e);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
72 if (offs < -128 || offs > 127)
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
73 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
74 lwasm_register_error(as, l, "Byte overflow");
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
75 return;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
76 }
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
77
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
78 lwasm_emitop(l, instab[l -> insn].ops[0]);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
79 lwasm_emit(l, offs);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
80 }
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
81
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
82 PARSEFUNC(insn_parse_rel16)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 int v;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
85 lw_expr_t t, e1, e2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 int r;
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
87
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
88 // sometimes there is a "#", ignore if there
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 if (**p == '#')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
92 t = lwasm_parse_expr(as, p);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
93 if (!t)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 {
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
95 lwasm_register_error(as, l, "Bad operand");
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
96 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 }
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
98 l -> len = OPLEN(instab[l -> insn].ops[0]) + 2;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
99
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
100 e1 = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, l);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
101 e2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, e1, l -> addr);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
102 lw_expr_destroy(e1);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
103 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_minus, t, e2);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
104 lw_expr_destroy(e2);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
105 lwasm_save_expr(l, 0, e2);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
106 }
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107
357
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
108 EMITFUNC(insn_emit_rel16)
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
109 {
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
110 lw_expr_t e;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
111 int offs;
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
112
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
113 e = lwasm_fetch_expr(l, 0);
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, instab[l -> insn].ops[0]);
0cf4948d53b4 Checkpoint - adding actual cpu instructions
lost@starbug
parents: 339
diff changeset
116 lwasm_emitexpr(l, e, 2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 }