annotate src/lwasm.c @ 76:2fe5fd7d65a3

Checkpointing object target implementation
author lost
date Thu, 08 Jan 2009 02:57:24 +0000
parents c8c772ef5df9
children a338d496350e
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 lwasm.c
55
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
3 Copyright © 2009 William Astle
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
5 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
6
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
7 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
8 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
9 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
10 version.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
11
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
12 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
13 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
14 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
15 more details.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
16
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
17 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
18 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
19
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
20
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
21 Contains random functions used by the assembler
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
22 */
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
24 #define __lwasm_c_seen__
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
25
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
26 #include <stdarg.h>
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27 #include <stdlib.h>
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
28 #include <stdio.h>
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
29
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 #include "lwasm.h"
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
31 #include "util.h"
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
32 #include "expr.h"
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
34 int debug_level = 0;
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
35
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
36 int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37 {
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
38 lwasm_error_t *e;
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
39 va_list args;
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
40 char errbuff[1024];
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
41 int r;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
43 if (as -> passnum != pass)
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
44 return;
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
45
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
46 va_start(args, fmt);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
48 e = lwasm_alloc(sizeof(lwasm_error_t));
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
49
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
50 e -> next = l -> err;
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
51 l -> err = e;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 as -> errorcount++;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
55 r = vsnprintf(errbuff, 1024, fmt, args);
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
56 e -> mess = lwasm_strdup(errbuff);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
58 va_end(args);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59
26
d2e86babd958 Added error tracking infrastructure
lost
parents: 13
diff changeset
60 return r;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 }
27
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
62
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
63 void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
64 {
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
65 as -> addr += 1;
58
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
66 as -> addr &= 0xffff;
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
67
74
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
68 if (as -> outformat == OUTPUT_OBJ && !(as -> csect))
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
69 {
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
70 register_error(as, l, 1, "Output not allowed outside sections with obj target");
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
71 return;
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
72 }
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
73 if (as -> outformat == OUTPUT_OBJ && as -> csect -> flags & SECTION_BSS)
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
74 {
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
75 register_error(as, l, 1, "Output not allowed inside BSS sections");
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
76 return;
c8c772ef5df9 Checkpointing object target implementation
lost
parents: 60
diff changeset
77 }
27
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
78 if (as -> passnum == 1)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
79 return;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
80
42
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
81
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
82 if (l -> codelen >= l -> codesize)
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
83 {
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
84 l -> bytes = realloc(l -> bytes, l -> codesize + 16);
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
85 l -> codesize += 16;
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
86 }
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
87 l -> bytes[l -> codelen] = b & 0xff;
4bb7b723e5b7 Added pass2 code generation to lwasm_emit()
lost
parents: 39
diff changeset
88 l -> codelen += 1;
27
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
89 }
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
90
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
91 void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
92 {
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
93 if (o >= 0x100)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
94 lwasm_emit(as, l, o >> 8);
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
95 lwasm_emit(as, l, o & 0xff);
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
96 }
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
97
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
98 int lwasm_lookupreg2(const char *reglist, char **str)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
99 {
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
100 int rval = 0;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
101
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
102 while (*reglist)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
103 {
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
104 if (toupper(**str) == *reglist)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
105 {
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
106 // first char matches
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
107 if (reglist[1] == ' ' && !isalpha(*(*str + 1)))
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
108 break;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
109 if (toupper(*(*str + 1)) == reglist[1])
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
110 break;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
111 }
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
112 reglist += 2;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
113 rval++;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
114 }
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
115 if (!*reglist)
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
116 return -1;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
117 if (reglist[1] == ' ')
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
118 (*str)++;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
119 else
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
120 (*str) += 2;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
121 return rval;
f736579569b4 Added handlers for inherent and register to register instructions
lost
parents: 26
diff changeset
122 }
32
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
123
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
124 int lwasm_lookupreg3(const char *rlist, const char **str)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
125 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
126 int rval = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
127 int f = 0;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
128 const char *reglist = rlist;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
129
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
130 while (*reglist)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
131 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
132 if (toupper(**str) == *reglist)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
133 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
134 // first char matches
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
135 if (reglist[1] == ' ')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
136 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
137 f = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
138 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
139 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
140 if (toupper(*(*str + 1)) == reglist[1])
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
141 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
142 // second char matches
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
143 if (reglist[2] == ' ')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
144 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
145 f = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
146 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
147 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
148 if (toupper(*(*str + 2)) == reglist[2])
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
149 {
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
150 f = 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
151 break;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
152 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
153 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
154 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
155 reglist += 3;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
156 rval++;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
157 }
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
158 if (f == 0)
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
159 return -1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
160
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
161
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
162 reglist = rval * 3 + rlist;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
163 if (reglist[1] == ' ')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
164 (*str) += 1;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
165 else if (reglist[2] == ' ')
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
166 (*str) += 2;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
167 else
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
168 (*str)+=3;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
169 return rval;
9bd0fbfe7405 Added basic indexed mode handling
lost
parents: 27
diff changeset
170 }
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
171
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
172 struct symstateinfo
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
173 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
174 asmstate_t *as;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
175 lwasm_line_t *l;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
176 };
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
177
76
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
178 lwasm_expr_stack_t *lwasm_expr_lookup_symbol(char *sym, void *state)
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
179 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
180 lwasm_symbol_ent_t *se;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
181 struct symstateinfo *st;
76
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
182 lwasm_expr_stack_t *rs;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
183 lwasm_expr_term_t *t;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
184 lwasm_expr_stack_node_t *n;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
185
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
186 int val;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
187
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
188 st = state;
52
b9856da2674a Added file inclusion
lost
parents: 42
diff changeset
189 debug_message(3, "lwasm_expr_lookup_symbol(): find '%s' (context=%d)", sym, st -> as -> context);
b9856da2674a Added file inclusion
lost
parents: 42
diff changeset
190
60
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
191 // check for special symbols first...
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
192 if (sym[1] == '\0')
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
193 {
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
194 switch (sym[0])
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
195 {
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
196 // current line address
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
197 case '*':
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
198 case '.':
76
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
199 val = st -> l -> codeaddr;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
200 goto retconst;
60
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
201
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
202 case '<':
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
203 // previous branch point
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
204 // not implemented
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
205 break;
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
206 case '>':
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
207 // next branch point
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
208 // not implemented
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
209 break;
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
210 }
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
211 }
309810f39ab7 Implemented the * and . special symbols
lost
parents: 58
diff changeset
212
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
213 // look for local symbol first then global symbol
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
214 se = lwasm_find_symbol(st -> as, sym, st -> as -> context);
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
215 if (!se)
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
216 se = lwasm_find_symbol(st -> as, sym, -1);
52
b9856da2674a Added file inclusion
lost
parents: 42
diff changeset
217 debug_message(3, "lwasm_expr_lookup_symbol(): got '%p'", se);
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
218 if (!se)
76
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
219 return NULL;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
220 if (st -> as -> outformat != OUTPUT_OBJ || se -> sect == NULL || se -> sect == st -> as -> csect)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
221 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
222 // global symbol, intrasegment reference, or not an object target
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
223 val = se -> value;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
224 goto retconst;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
225 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
226 // an intersegment reference will return as NULL (to be resolved at output/link time)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
227 // if se -> expr is NULL, it has to be an intersegment reference here
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
228 if (se -> expr == NULL)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
229 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
230 return NULL;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
231 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
232
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
233 // duplicate the expression for return
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
234 rs = lwasm_expr_stack_create();
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
235 for (n = se -> expr -> head; n; n = n -> next)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
236 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
237 lwasm_expr_stack_push(rs, n -> term);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
238 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
239 return rs;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
240
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
241 retconst:
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
242 rs = lwasm_expr_stack_create();
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
243 t = lwasm_expr_term_create_int(val);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
244 lwasm_expr_stack_push(rs, t);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
245 lwasm_expr_term_free(t);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
246 return rs;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
247 }
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
248
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
249 lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp)
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
250 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
251 struct symstateinfo st;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
252
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
253 st.as = as;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
254 st.l = l;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
255
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
256 debug_message(2, "Evaluate expression: %s", inp);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
257
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
258 return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st));
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 32
diff changeset
259 }
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
260
76
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
261 // return 1 if no undefined symbols (externals and incompletes are okay)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
262 // return 0 if there are undefined symbols
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
263 int lwasm_expr_result_ckconst(asmstate_t *as, lwasm_expr_stack_t *s)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
264 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
265 lwasm_expr_stack_node_t *n;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
266 lwasm_symbol_ent_t *se;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
267
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
268 for (n = s -> head; n; n = n -> next)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
269 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
270 if (n -> term -> term_type == LWASM_TERM_SYM)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
271 {
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
272 se = lwasm_find_symbol(as, n -> term -> symbol, as -> context);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
273 if (!se)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
274 se = lwasm_find_symbol(as, n -> term -> symbol, -1);
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
275 if (!se)
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
276 return 0;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
277 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
278 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
279 return 1;
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
280 }
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
281
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
282 /*
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
283 Evaluate an expression according to the flag value. Return 0 if a constant result was
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
284 obtained, 1 if an incomplete result was obtained, and -1 if an error was flagged.
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
285
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
286 Symbol resolution will be modified for the object target as follows:
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
287 - a symbol which is not defined within a section will evaluate as a constant
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
288 - a symbol which is defined within the same section will evaluate as a constant
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
289 - a symbol defined in another section will remain unresolved
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
290 - external references will also remain unresolved
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
291
2fe5fd7d65a3 Checkpointing object target implementation
lost
parents: 74
diff changeset
292 */
55
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
293 int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val)
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
294 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
295 lwasm_expr_stack_t *s;
58
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
296 const char *ep;
55
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
297 int rval;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
298
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
299 s = lwasm_evaluate_expr(as, l, *inp, &ep);
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
300 if (!s)
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
301 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
302 register_error(as, l, 1, "Bad expression");
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
303 *val = 0;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
304 return -1;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
305 }
58
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
306 *inp = (char *)ep;
55
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
307
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
308 if (flag & EXPR_PASS1CONST && as -> passnum == 1 && !lwasm_expr_is_constant(s))
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
309 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
310 register_error(as, l, 1, "Illegal incomplete reference (pass 1)");
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
311 *val = 0;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
312 lwasm_expr_stack_free(s);
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
313 return -1;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
314 }
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
315 if (flag & EXPR_PASS2CONST && as -> passnum == 2 && !lwasm_expr_is_constant(s))
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
316 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
317 register_error(as, l, 2, "Incomplete reference (pass 2)");
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
318 *val = 0;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
319 lwasm_expr_stack_free(s);
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
320 return -1;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
321 }
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
322 *val = lwasm_expr_get_value(s);
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
323 lwasm_expr_stack_free(s);
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
324
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
325 if (flag & EXPR_BYTE && as -> passnum == 2 && (*val < -128 || *val > 255))
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
326 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
327 register_error(as, l, 2, "Byte overflow");
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
328 *val &= 0xff;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
329 return -1;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
330 }
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
331 if (flag & EXPR_BYTE)
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
332 {
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
333 *val &= 0xff;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
334 }
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
335
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
336 return 0;
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
337 }
8e32696380f3 added expression evaluation and checking function
lost
parents: 52
diff changeset
338
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
339 void debug_message(int level, const char *fmt, ...)
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
340 {
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
341 va_list args;
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
342
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
343 va_start(args, fmt);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
344 if (debug_level >= level)
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
345 {
39
efa19ec69df9 tweaked debugging system for expression handler
lost
parents: 38
diff changeset
346 if (level > 0)
efa19ec69df9 tweaked debugging system for expression handler
lost
parents: 38
diff changeset
347 fprintf(stderr, "DEBUG %d: ", level);
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
348 vfprintf(stderr, fmt, args);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
349 fputc('\n', stderr);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
350 }
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
351 va_end(args);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
352 }
57
035b95a3690f Added conditional assembly and macros
lost
parents: 55
diff changeset
353
035b95a3690f Added conditional assembly and macros
lost
parents: 55
diff changeset
354 int lwasm_next_context(asmstate_t *as)
035b95a3690f Added conditional assembly and macros
lost
parents: 55
diff changeset
355 {
58
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
356 int r;
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
357 r = as -> nextcontext;
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
358 as -> nextcontext += 1;
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
359 debug_message(3, "lwasm_next_context(): %d (%d) pass %d", r, as -> nextcontext, as -> passnum);
b1d81800bc91 Added symbol listing to list file; various fixes
lost
parents: 57
diff changeset
360 return r;
57
035b95a3690f Added conditional assembly and macros
lost
parents: 55
diff changeset
361 }
035b95a3690f Added conditional assembly and macros
lost
parents: 55
diff changeset
362