annotate lwasm/pass1.c @ 448:5cccf90bf838 3.0 tip

Fixed bug with complex external references generating invalid relocations in the object file
author lost@l-w.ca
date Fri, 05 Nov 2010 22:27:00 -0600
parents 4b137a8cf32a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
1 /*
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
2 pass1.c
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
3
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
5
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
7
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
11 version.
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
12
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
16 more details.
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
17
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
20 */
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
21
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
22 #include <config.h>
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
23
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
24 #include <stdio.h>
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
25 #include <string.h>
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
26
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
27 #include <lw_alloc.h>
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 340
diff changeset
28 #include <lw_string.h>
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
29
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
30 #include "lwasm.h"
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 340
diff changeset
31 #include "instab.h"
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
32 #include "input.h"
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
33
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
34 extern int expand_macro(asmstate_t *as, line_t *l, char **p, char *opc);
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
35 extern int expand_struct(asmstate_t *as, line_t *l, char **p, char *opc);
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
36
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
37 /*
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
38 pass 1: parse the lines
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
39
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
40 line format:
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
41
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
42 [<symbol>] <opcode> <operand>[ <comment>]
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
43
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
44 If <symbol> is followed by a :, whitespace may precede the symbol
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
45
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
46 A line may optionally start with a number which must not be preceded by
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
47 white space and must be followed by a single whitespace character. After
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
48 that whitespace character, the line is parsed as if it had no line number.
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
49
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
50 */
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
51 void do_pass1(asmstate_t *as)
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
52 {
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
53 char *line;
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
54 line_t *cl;
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
55 char *p1;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 340
diff changeset
56 int stspace;
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
57 char *tok, *sym;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
58 int opnum;
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
59 int lc = 1;
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
60 for (;;)
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
61 {
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
62 sym = NULL;
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
63 line = input_readline(as);
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
64 if (!line)
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
65 break;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
66 if (line[0] == 1 && line[1] == 1)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
67 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
68 // special internal directive
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
69 // these DO NOT appear in the output anywhere
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
70 // they are generated by the parser to pass information
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
71 // forward
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
72 for (p1 = line + 2; *p1 && !isspace(*p1); p1++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
73 /* do nothing */ ;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
74 *p1++ = '\0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
75 if (!strcmp(line + 2, "SETCONTEXT"))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
76 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
77 as -> context = strtol(p1, NULL, 10);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
78 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
79 lw_free(line);
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
80 lc = 1;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
81 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
82 }
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
83 debug_message(as, 75, "Read line: %s", line);
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
84
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
85 cl = lw_alloc(sizeof(line_t));
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
86 memset(cl, 0, sizeof(line_t));
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 372
diff changeset
87 cl -> outputl = -1;
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
88 cl -> linespec = lw_strdup(input_curspec(as));
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
89 cl -> prev = as -> line_tail;
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
90 cl -> insn = -1;
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
91 cl -> as = as;
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 351
diff changeset
92 cl -> inmod = as -> inmod;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 351
diff changeset
93 cl -> csect = as -> csect;
360
7d91ab7ac7d6 Indexed stage 2; set line structure to track pragmas in effect for that line
lost@starbug
parents: 354
diff changeset
94 cl -> pragmas = as -> pragmas;
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
95 cl -> context = as -> context;
382
eacdae8a1575 Various bugfixes
lost@starbug
parents: 374
diff changeset
96 cl -> ltext = lw_strdup(line);
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
97 cl -> soff = -1;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
98 cl -> dshow = -1;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
99 cl -> dsize = 0;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
100 cl -> dptr = NULL;
418
4b137a8cf32a Implemented "branch points"
lost@l-w.ca
parents: 411
diff changeset
101 cl -> isbrpt = 0;
385
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 383
diff changeset
102 as -> cl = cl;
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
103 if (!as -> line_tail)
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
104 {
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
105 as -> line_head = cl;
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
106 cl -> addr = lw_expr_build(lw_expr_type_int, 0);
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
107 }
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
108 else
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
109 {
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
110 lw_expr_t te;
351
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
111
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
112 cl -> lineno = as -> line_tail -> lineno + 1;
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
113 as -> line_tail -> next = cl;
351
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
114
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
115 // set the line address
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
116 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev);
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
117 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te);
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
118 lw_expr_destroy(te);
385
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 383
diff changeset
119 lwasm_reduce_expr(as, cl -> addr);
cf8c92d70eb1 Various bugfixes
lost@starbug
parents: 383
diff changeset
120 // lw_expr_simplify(cl -> addr, as);
351
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
121
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
122 // carry DP value forward
4dba8c7e242c conditional pseudo ops added
lost@starbug
parents: 347
diff changeset
123 cl -> dpval = cl -> prev -> dpval;
383
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
124
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
125 }
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
126 if (!lc && strcmp(cl -> linespec, cl -> prev -> linespec))
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
127 lc = 1;
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
128 if (lc)
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
129 {
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
130 cl -> lineno = 1;
848d3cca8078 Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents: 382
diff changeset
131 lc = 0;
337
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
132 }
04c80c51b16a Checkpoint development
lost
parents: 332
diff changeset
133 as -> line_tail = cl;
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
134 // blank lines don't count for anything
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
135 // except a local symbol context break
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
136 if (!*line)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
137 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
138 as -> context = lwasm_next_context(as);
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
139 goto nextline;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
140 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
141
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
142 // skip comments
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
143 // commends do not create a context break
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
144 if (*line == '*' || *line == ';' || *line == '#')
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
145 goto nextline;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
146
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
147 p1 = line;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
148 if (isdigit(*p1))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
149 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
150 // skip line number
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
151 while (*p1 && isdigit(*p1))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
152 p1++;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
153 if (!*p1 && !isspace(*p1))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
154 p1 = line;
411
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 410
diff changeset
155 else if (*p1 && !isspace(*p1))
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 410
diff changeset
156 p1 = line;
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
157 else if (*p1 && isspace(*p1))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
158 p1++;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
159 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
160
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
161 // blank line - context break
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
162 if (!*p1)
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
163 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
164 as -> context = lwasm_next_context(as);
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
165 goto nextline;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
166 }
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
167
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
168 // comment - no context break
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
169 if (*p1 == '*' || *p1 == ';' || *p1 == '#')
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
170 goto nextline;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
171
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
172 if (isspace(*p1))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
173 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
174 for (; *p1 && isspace(*p1); p1++)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
175 /* do nothing */ ;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
176 stspace = 1;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
177 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
178 else
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
179 stspace = 0;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
180
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
181 if (*p1 == '*' || *p1 == ';' || *p1 == '#')
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
182 goto nextline;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
183 if (!*p1)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
184 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
185 // nothing but whitespace - context break
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
186 as -> context = lwasm_next_context(as);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
187 goto nextline;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
188 }
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
189
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 340
diff changeset
190 // find the end of the first token
410
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
191 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':' && *p1 != '='; p1++)
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
192 /* do nothing */ ;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
193
410
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
194 if (*p1 == ':' || *p1 == '=' || stspace == 0)
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
195 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
196 // have a symbol here
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
197 sym = lw_strndup(tok, p1 - tok);
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
198 if (*p1 == ':')
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
199 p1++;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
200 for (; *p1 && isspace(*p1); p1++)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
201 /* do nothing */ ;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
202
410
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
203 if (*p1 == '=')
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
204 {
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
205 tok = p1++;
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
206 }
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
207 else
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
208 {
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
209 for (tok = p1; *p1 && !isspace(*p1); p1++)
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
210 /* do nothing */ ;
acaafc70044b Fixed "=" pseudo op to work correctly
lost@l-w.ca
parents: 389
diff changeset
211 }
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
212 }
418
4b137a8cf32a Implemented "branch points"
lost@l-w.ca
parents: 411
diff changeset
213 if (sym && strcmp(sym, "!") == 0)
4b137a8cf32a Implemented "branch points"
lost@l-w.ca
parents: 411
diff changeset
214 cl -> isbrpt = 1;
4b137a8cf32a Implemented "branch points"
lost@l-w.ca
parents: 411
diff changeset
215 else if (sym)
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
216 cl -> sym = lw_strdup(sym);
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
217 cl -> symset = 0;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
218
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
219 // tok points to the opcode for the line or NUL if none
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
220 if (*tok)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
221 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
222 // look up operation code
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
223 sym = lw_strndup(tok, p1 - tok);
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
224 for (; *p1 && isspace(*p1); p1++)
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
225 /* do nothing */ ;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
226
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
227 for (opnum = 0; instab[opnum].opcode; opnum++)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
228 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
229 if (!strcasecmp(instab[opnum].opcode, sym))
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
230 break;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
231 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
232
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
233 // p1 points to the start of the operand
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
234
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
235 // if we're inside a macro definition and not at ENDM,
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
236 // add the line to the macro definition and continue
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
237 if (as -> inmacro && !(instab[opnum].flags & lwasm_insn_endm))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
238 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
239 add_macro_line(as, line);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
240 goto linedone;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
241 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
242
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
243 // if skipping a condition and the operation code doesn't
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
244 // operate within a condition (not a conditional)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
245 // do nothing
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
246 if (as -> skipcond && !(instab[opnum].flags & lwasm_insn_cond))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
247 goto linedone;
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
248
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
249 if (instab[opnum].opcode == NULL)
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
250 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
251 cl -> insn = -1;
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
252 if (*tok != ';' && *tok != '*')
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
253 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
254 // bad opcode; check for macro here
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
255 if (expand_macro(as, cl, &p1, sym) != 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
256 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
257 // macro expansion failed
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
258 if (expand_struct(as, cl, &p1, sym) != 0)
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
259 {
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
260 // structure expansion failed
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
261 lwasm_register_error(as, cl, "Bad opcode");
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
262 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
263 }
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
264 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
265 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
266 else
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
267 {
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
268 cl -> insn = opnum;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
269 // no parse func means operand doesn't matter
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
270 if (instab[opnum].parse)
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
271 {
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
272 if (as -> instruct == 0 || instab[opnum].flags & lwasm_insn_struct)
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
273 {
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
274 cl -> len = -1;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
275 // call parse function
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
276 (instab[opnum].parse)(as, cl, &p1);
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
277
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
278 if (*p1 && !isspace(*p1))
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
279 {
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
280 // flag bad operand error
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
281 lwasm_register_error(as, cl, "Bad operand (%s)", p1);
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
282 }
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
283 }
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 385
diff changeset
284 else if (as -> instruct == 1)
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
285 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
286 lwasm_register_error(as, cl, "Bad operand (%s)", p1);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
287 }
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
288 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
289 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
290 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
291
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
292 linedone:
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
293 lw_free(sym);
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
294
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
295 if (!as -> skipcond && !as -> inmacro)
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
296 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
297 if (cl -> sym && cl -> symset == 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
298 {
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
299 debug_message(as, 50, "Register symbol %s: %s", cl -> sym, lw_expr_print(cl -> addr));
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
300
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
301 // register symbol at line address
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
302 if (!register_symbol(as, cl, cl -> sym, cl -> addr, symbol_flag_none))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
303 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
304 // symbol error
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
305 // lwasm_register_error(as, cl, "Bad symbol '%s'", cl -> sym);
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
306 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 344
diff changeset
307 }
372
90de73ba0cac Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents: 370
diff changeset
308 debug_message(as, 40, "Line address: %s", lw_expr_print(cl -> addr));
340
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
309 }
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
310
1a6fc6ebb31c Checkpoint
lost
parents: 337
diff changeset
311 nextline:
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
312 lw_free(line);
347
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
313
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
314 // if we've hit the "end" bit, finish out
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
315 if (as -> endseen)
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
316 return;
332
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
317 }
67224d8d1024 Basic input layer works
lost
parents:
diff changeset
318 }