annotate src/pass1.c @ 25:3b818f05dc2a

imported instruction table from older version to development version
author lost
date Fri, 02 Jan 2009 02:03:48 +0000
parents b704f7ffc8ba
children d2e86babd958
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
1 /*
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
2 pass1.c
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
4
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
5 This file is part of LWASM.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
6
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
10 version.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
11
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
15 more details.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
16
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
19
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
20
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
21 Handles first pass of assembly
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
22
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
23 First pass involves the following:
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
24
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
25 1. read all lines from the main source file, following all "include"
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
26 directives as appropriate
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
27 2. each operand is evaluated for syntax and futher for value if there are
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
28 multiple addressing sizes available; any undefined or not fully resolved
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
29 value will default to the largest addressing size available (16 bit)
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
30 3. addresses are assigned to every symbol defined in the assembly
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
31 4. macros are defined and expanded at this pass
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
32
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
33 * note: the lines are re-evaluated on the second pass
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
34
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
35 All source lines are read into memory with a record of the file name and
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
36 line number within the files.
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
37
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
38 Lines are one of the following formats:
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
39
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
40 <symbol> <opcode> <operand> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
41 <symbol> <opcode> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
42 <opcode> <operand> <comment>
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
43 <opcode> <comment>
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
44
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
45 A "*" or ";" appearing anywhere on the line that is not otherwise interpreted
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
46 as part of an operation code or operand introduces a comment.
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
47
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
48 Certain lwasm specific operations are prefixed with a "*" to aid in source
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
49 code portability (like *pragma).
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
50 */
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
51
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
52 #ifdef HAVE_CONFIG_H
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
53 #include "config.h"
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
54 #endif
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
55
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
56 #include <errno.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
57 #include <stdio.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
58 #include <stdlib.h>
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
59
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
60 #include "lwasm.h"
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
61 #include "util.h"
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
62
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
63 // we can't use standard line inputting functions here because we have to
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
64 // handle non-standard line terminations (CR, LF, CRLF, or LFCR)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
65 int lwasm_read_file(asmstate_t *as, const char *filename)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
66 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
67 FILE *f;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
68 int c, c2;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
69 lwasm_line_t *nl;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
70 int lineno = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
71 char *fnref;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
72
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
73 // ought to be long enough...we truncate longer lines
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
74 char linebuff[2049];
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
75 int lbloc = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
76 int eol = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
77
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
78 // add filename to list
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
79 as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1));
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
80 fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
81 as -> filelistlen += 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
82
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
83 f = fopen(filename, "r");
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
84 if (!f)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
85 return -1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
86
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
87 for (;;)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
88 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
89 c = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
90 if (c == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
91 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
92 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
93 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
94 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
95 else if (c == '\r')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
96 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
97 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
98 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
99 // check for '\n':
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
100 c2 = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
101 if (c2 == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
102 c = EOF;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
103 else if (c2 != '\n')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
104 ungetc(c2, f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
105 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
106 else if (c == '\n')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
107 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
108 linebuff[lbloc] = '\0';
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
109 eol = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
110 // check for '\r':
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
111 c2 = fgetc(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
112 if (c2 == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
113 c = EOF;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
114 else if (c2 != '\r')
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
115 ungetc(c2, f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
116 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
117 else
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
118 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
119 // silently ignore characters past 2K on a line... FIXME
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
120 if (lbloc < 2048)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
121 linebuff[lbloc++] = c;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
122 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
123 if (eol)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
124 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
125 eol = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
126 lbloc = 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
127 nl = lwasm_alloc(sizeof(lwasm_line_t));
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
128 nl -> text = lwasm_strdup(linebuff);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
129 nl -> lineno = lineno++;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
130 nl -> filename = fnref;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
131 nl -> next = NULL;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
132 nl -> prev = as -> linestail;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
133 if (as -> linestail)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
134 as -> linestail -> next = nl;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
135 else
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
136 as -> linestail = nl;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
137 if (!(as -> lineshead))
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
138 as -> lineshead = nl;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
139 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
140 if (c == EOF)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
141 break;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
142 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
143
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
144 fclose(f);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
145 return 0;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
146 }
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
147
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
148 void lwasm_pass1(asmstate_t *as)
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
149 {
21
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
150 as -> passnum = 1;
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
151
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
152 if (lwasm_read_file(as, as -> infile) < 0)
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
153 {
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
154 fprintf(stderr, "Error reading input file '%s'", as -> infile);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
155 perror("");
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
156 exit(1);
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
157 }
3c0e5f311c95 Added reading of input file for pass1
lost
parents: 13
diff changeset
158
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
159 }