332
|
1 /*
|
|
2 pass1.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <stdio.h>
|
|
25
|
|
26 #include <lw_alloc.h>
|
342
|
27 #include <lw_string.h>
|
332
|
28
|
|
29 #include "lwasm.h"
|
342
|
30 #include "instab.h"
|
332
|
31 #include "input.h"
|
|
32
|
337
|
33 /*
|
|
34 pass 1: parse the lines
|
340
|
35
|
|
36 line format:
|
|
37
|
|
38 [<symbol>] <opcode> <operand>[ <comment>]
|
|
39
|
|
40 If <symbol> is followed by a :, whitespace may precede the symbol
|
|
41
|
|
42 A line may optionally start with a number which must not be preceded by
|
|
43 white space and must be followed by a single whitespace character. After
|
|
44 that whitespace character, the line is parsed as if it had no line number.
|
|
45
|
337
|
46 */
|
332
|
47 void do_pass1(asmstate_t *as)
|
|
48 {
|
|
49 char *line;
|
337
|
50 line_t *cl;
|
340
|
51 char *p1;
|
342
|
52 int stspace;
|
340
|
53 char *tok, *sym;
|
|
54 int opnum;
|
|
55
|
332
|
56 for (;;)
|
|
57 {
|
340
|
58 sym = NULL;
|
332
|
59 line = input_readline(as);
|
|
60 if (!line)
|
|
61 break;
|
|
62 printf("%s\n", line);
|
337
|
63
|
|
64 cl = lw_alloc(sizeof(line_t));
|
|
65 cl -> next = NULL;
|
|
66 cl -> prev = as -> line_tail;
|
|
67 cl -> len = -1;
|
|
68 cl -> insn = -1;
|
|
69 if (!as -> line_tail)
|
|
70 {
|
|
71 as -> line_head = cl;
|
|
72 cl -> addr = lw_expr_build(lw_expr_type_int, 0);
|
|
73 }
|
|
74 else
|
|
75 {
|
|
76 lw_expr_t te;
|
|
77 as -> line_tail -> next = cl;
|
|
78 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev);
|
|
79 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te);
|
|
80 lw_expr_destroy(te);
|
|
81 lw_expr_simplify(cl -> addr);
|
|
82 }
|
|
83 as -> line_tail = cl;
|
|
84
|
340
|
85 // blank lines don't count for anything
|
|
86 if (!*line)
|
|
87 {
|
|
88 goto nextline;
|
|
89 }
|
|
90
|
|
91 // skip comments
|
|
92 if (*line == '*' || *line == ';' || *line == '#')
|
|
93 goto nextline;
|
|
94
|
|
95 p1 = line;
|
|
96 if (isdigit(*p1))
|
|
97 {
|
|
98 // skip line number
|
|
99 while (*p1 && isdigit(*p1))
|
|
100 p1++;
|
|
101 if (!*p1 && !isspace(*p1))
|
|
102 p1 = line;
|
|
103 else if (*p1 && isspace(*p1))
|
|
104 p1++;
|
|
105 }
|
|
106
|
|
107 if (!*p1)
|
|
108 goto nextline;
|
|
109
|
|
110 if (*p1 == '*' || *p1 == ';' || *p1 == '#')
|
|
111 goto nextline;
|
|
112
|
|
113 if (isspace(*p1))
|
|
114 {
|
|
115 for (; *p1 && isspace(*p1); p1++)
|
|
116 /* do nothing */ ;
|
|
117 stspace = 1;
|
|
118 }
|
|
119 else
|
|
120 stspace = 0;
|
|
121
|
|
122 if (*p1 == '*' || *p1 == ';' || *p1 == '#' || !*p1)
|
|
123 goto nextline;
|
|
124
|
342
|
125 // find the end of the first token
|
340
|
126 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':'; p1++)
|
|
127 /* do nothing */ ;
|
|
128
|
|
129 if (*p1 == ':' || stspace == 0)
|
|
130 {
|
|
131 // have a symbol here
|
|
132 sym = lw_strndup(tok, p1 - tok);
|
|
133 if (*p1 == ':')
|
|
134 p1++;
|
|
135 for (; *p1 && isspace(*p1); p1++)
|
|
136 /* do nothing */ ;
|
|
137
|
|
138 for (tok = p1; *p1 && !isspace(*p1); p1++)
|
|
139 /* do nothing */ ;
|
|
140 }
|
|
141
|
|
142 cl -> sym = sym;
|
|
143 cl -> symset = 0;
|
|
144
|
|
145 // tok points to the opcode for the line or NUL if none
|
|
146 if (*tok)
|
|
147 {
|
|
148 // look up operation code
|
|
149 sym = lw_strndup(tok, p1 - tok);
|
|
150 for (; *p1 && isspace(p1); p1++)
|
|
151 /* do nothing */ ;
|
|
152
|
|
153 for (opnum = 0; instab[opnum].opcode; opnum++)
|
|
154 {
|
|
155 if (!strcasecmp(instab[opnum].opcode, sym))
|
|
156 break;
|
|
157 }
|
|
158 lw_free(sym);
|
|
159
|
|
160 // p1 points to the start of the operand
|
|
161
|
|
162 if (instab[opnum].opcode == NULL)
|
|
163 {
|
|
164 cl -> insn = -1;
|
|
165 if (*tok != ';' && *tok != '*')
|
|
166 {
|
|
167 // bad opcode; check for macro here
|
|
168 }
|
|
169 }
|
|
170 else
|
|
171 {
|
|
172 cl -> insn = opnum;
|
|
173 // call parse function
|
|
174
|
|
175 if (*p1 && !isspace(*p1))
|
|
176 {
|
|
177 // flag bad operand error
|
|
178 }
|
|
179 }
|
|
180 }
|
|
181
|
|
182 if (cl -> sym && cl -> symset == 0)
|
|
183 {
|
342
|
184 printf("Register symbol %s:", sym);
|
|
185 lw_expr_print(cl -> addr);
|
|
186 printf("\n");
|
|
187
|
340
|
188 // register symbol at line address
|
342
|
189 if (!register_symbol(as, cl -> sym, cl -> addr, symbol_flag_none))
|
|
190 {
|
|
191 // symbol error
|
|
192 }
|
340
|
193 }
|
|
194
|
337
|
195 lw_expr_print(cl -> addr);
|
|
196 printf("\n");
|
|
197 // now parse the line
|
|
198
|
340
|
199 nextline:
|
332
|
200 lw_free(line);
|
|
201 }
|
|
202 }
|