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>
|
|
27
|
|
28 #include "lwasm.h"
|
|
29 #include "input.h"
|
|
30
|
337
|
31 /*
|
|
32 pass 1: parse the lines
|
340
|
33
|
|
34 line format:
|
|
35
|
|
36 [<symbol>] <opcode> <operand>[ <comment>]
|
|
37
|
|
38 If <symbol> is followed by a :, whitespace may precede the symbol
|
|
39
|
|
40 A line may optionally start with a number which must not be preceded by
|
|
41 white space and must be followed by a single whitespace character. After
|
|
42 that whitespace character, the line is parsed as if it had no line number.
|
|
43
|
337
|
44 */
|
332
|
45 void do_pass1(asmstate_t *as)
|
|
46 {
|
|
47 char *line;
|
337
|
48 line_t *cl;
|
340
|
49 char *p1;
|
|
50 int stpace;
|
|
51 char *tok, *sym;
|
|
52 int opnum;
|
|
53
|
332
|
54 for (;;)
|
|
55 {
|
340
|
56 sym = NULL;
|
332
|
57 line = input_readline(as);
|
|
58 if (!line)
|
|
59 break;
|
|
60 printf("%s\n", line);
|
337
|
61
|
|
62 cl = lw_alloc(sizeof(line_t));
|
|
63 cl -> next = NULL;
|
|
64 cl -> prev = as -> line_tail;
|
|
65 cl -> len = -1;
|
|
66 cl -> insn = -1;
|
|
67 if (!as -> line_tail)
|
|
68 {
|
|
69 as -> line_head = cl;
|
|
70 cl -> addr = lw_expr_build(lw_expr_type_int, 0);
|
|
71 }
|
|
72 else
|
|
73 {
|
|
74 lw_expr_t te;
|
|
75 as -> line_tail -> next = cl;
|
|
76 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev);
|
|
77 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te);
|
|
78 lw_expr_destroy(te);
|
|
79 lw_expr_simplify(cl -> addr);
|
|
80 }
|
|
81 as -> line_tail = cl;
|
|
82
|
340
|
83 // blank lines don't count for anything
|
|
84 if (!*line)
|
|
85 {
|
|
86 goto nextline;
|
|
87 }
|
|
88
|
|
89 // skip comments
|
|
90 if (*line == '*' || *line == ';' || *line == '#')
|
|
91 goto nextline;
|
|
92
|
|
93 p1 = line;
|
|
94 if (isdigit(*p1))
|
|
95 {
|
|
96 // skip line number
|
|
97 while (*p1 && isdigit(*p1))
|
|
98 p1++;
|
|
99 if (!*p1 && !isspace(*p1))
|
|
100 p1 = line;
|
|
101 else if (*p1 && isspace(*p1))
|
|
102 p1++;
|
|
103 }
|
|
104
|
|
105 if (!*p1)
|
|
106 goto nextline;
|
|
107
|
|
108 if (*p1 == '*' || *p1 == ';' || *p1 == '#')
|
|
109 goto nextline;
|
|
110
|
|
111 if (isspace(*p1))
|
|
112 {
|
|
113 for (; *p1 && isspace(*p1); p1++)
|
|
114 /* do nothing */ ;
|
|
115 stspace = 1;
|
|
116 }
|
|
117 else
|
|
118 stspace = 0;
|
|
119
|
|
120 if (*p1 == '*' || *p1 == ';' || *p1 == '#' || !*p1)
|
|
121 goto nextline;
|
|
122
|
|
123
|
|
124 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':'; p1++)
|
|
125 /* do nothing */ ;
|
|
126
|
|
127 if (*p1 == ':' || stspace == 0)
|
|
128 {
|
|
129 // have a symbol here
|
|
130 sym = lw_strndup(tok, p1 - tok);
|
|
131 if (*p1 == ':')
|
|
132 p1++;
|
|
133 for (; *p1 && isspace(*p1); p1++)
|
|
134 /* do nothing */ ;
|
|
135
|
|
136 for (tok = p1; *p1 && !isspace(*p1); p1++)
|
|
137 /* do nothing */ ;
|
|
138 }
|
|
139
|
|
140 cl -> sym = sym;
|
|
141 cl -> symset = 0;
|
|
142
|
|
143 // tok points to the opcode for the line or NUL if none
|
|
144 if (*tok)
|
|
145 {
|
|
146 // look up operation code
|
|
147 sym = lw_strndup(tok, p1 - tok);
|
|
148 for (; *p1 && isspace(p1); p1++)
|
|
149 /* do nothing */ ;
|
|
150
|
|
151 for (opnum = 0; instab[opnum].opcode; opnum++)
|
|
152 {
|
|
153 if (!strcasecmp(instab[opnum].opcode, sym))
|
|
154 break;
|
|
155 }
|
|
156 lw_free(sym);
|
|
157
|
|
158 // p1 points to the start of the operand
|
|
159
|
|
160 if (instab[opnum].opcode == NULL)
|
|
161 {
|
|
162 cl -> insn = -1;
|
|
163 if (*tok != ';' && *tok != '*')
|
|
164 {
|
|
165 // bad opcode; check for macro here
|
|
166 }
|
|
167 }
|
|
168 else
|
|
169 {
|
|
170 cl -> insn = opnum;
|
|
171 // call parse function
|
|
172
|
|
173 if (*p1 && !isspace(*p1))
|
|
174 {
|
|
175 // flag bad operand error
|
|
176 }
|
|
177 }
|
|
178 }
|
|
179
|
|
180 if (cl -> sym && cl -> symset == 0)
|
|
181 {
|
|
182 // register symbol at line address
|
|
183 }
|
|
184
|
337
|
185 lw_expr_print(cl -> addr);
|
|
186 printf("\n");
|
|
187 // now parse the line
|
|
188
|
340
|
189 nextline:
|
332
|
190 lw_free(line);
|
|
191 }
|
|
192 }
|