annotate old-trunk/lwasm/old/insn_rel.c @ 344:0215a0fbf61b

Added assembly error system and additional checks for symbol syntax
author lost@starbug
date Thu, 25 Mar 2010 22:06:50 -0600
parents eb230fa7d28e
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_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 #define __insn_rel_c_seen__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 #include "expr.h"
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
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 OPFUNC(insn_rel8)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 int v;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 lwasm_expr_term_t *t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 int r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 lwasm_emitop(as, l, instab[opnum].ops[0]);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 if (**p == '#')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 if ((r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0)) < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 v = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 if (as -> passnum == 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 // need to adjust the expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 if (l -> exprs[0])
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 t = lwasm_expr_term_create_int(as -> addr + 1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 l -> exprvals[0] -= as -> addr + 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 if (r == 1 && as -> passnum == 2)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 register_error(as, l, 2, "Illegal external or intersegment reference");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 if (v < -128 || v > 127)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 register_error(as, l, 2, "Byte overflow");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 lwasm_emit(as, l, v & 0xff);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 }
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 External and intersegment references are adjusted for the relative addressing mode
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 by adjusting the expression on pass 1 and then treated as absolute references later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 OPFUNC(insn_rel16)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 int v;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 int r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 lwasm_expr_term_t *t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 lwasm_emitop(as, l, instab[opnum].ops[0]);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 if (**p == '#')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &v, 0);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 if (r < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 v = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 if (as -> passnum == 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 // need to adjust the expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 if (l -> exprs[0])
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 t = lwasm_expr_term_create_int(as -> addr + 2);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 l -> exprvals[0] -= as -> addr + 2;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 if (as -> passnum == 2 && r == 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 // since we have a reference outside this section, add
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 // a subtract of the section base to get the right value
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 // upon linking
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 t = lwasm_expr_term_create_secbase();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 lwasm_expr_stack_push(l -> exprs[0], t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 lwasm_expr_term_free(t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 l -> relocoff = as -> addr - l -> codeaddr;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 lwasm_emit(as, l, (v >> 8) & 0xff);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 lwasm_emit(as, l, v & 0xff);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 }