annotate lwasm/pass1.c @ 340:1a6fc6ebb31c

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