diff old-trunk/lwasm/old/pass1.c @ 339:eb230fa7d28e

Prepare for migration to hg
author lost
date Fri, 19 Mar 2010 02:54:14 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/old-trunk/lwasm/old/pass1.c	Fri Mar 19 02:54:14 2010 +0000
@@ -0,0 +1,178 @@
+/*
+pass1.c
+Copyright © 2008 William Astle
+
+This file is part of LWASM.
+
+LWASM is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+Handles first pass of assembly
+
+First pass involves the following:
+
+1. read all lines from the main source file, following all "include"
+   directives as appropriate
+2. each operand is evaluated for syntax and futher for value if there are
+   multiple addressing sizes available; any undefined or not fully resolved
+   value will default to the largest addressing size available (16 bit)
+3. addresses are assigned to every symbol defined in the assembly
+4. macros are defined and expanded at this pass
+
+* note: the lines are re-evaluated on the second pass
+
+All source lines are read into memory with a record of the file name and
+line number within the files.
+
+Lines are one of the following formats:
+
+<symbol> <opcode> <operand> <comment>
+<symbol> <opcode> <comment>
+ <opcode> <operand> <comment>
+ <opcode> <comment>
+
+A "*" or ";" appearing anywhere on the line that is not otherwise interpreted
+as part of an operation code or operand introduces a comment.
+
+Certain lwasm specific operations are prefixed with a "*" to aid in source
+code portability (like *pragma).
+*/
+
+#include <config.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lwasm.h"
+#include "util.h"
+
+
+extern int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l);
+
+// we can't use standard line inputting functions here because we have to
+// handle non-standard line terminations (CR, LF, CRLF, or LFCR)
+int lwasm_read_file(asmstate_t *as, const char *filename)
+{
+	FILE *f;
+	int c, c2;
+	lwasm_line_t *nl;
+	int lineno = 1;
+	char *fnref;
+	
+	// ought to be long enough...we truncate longer lines
+	char linebuff[2049];
+	int lbloc = 0;
+	int eol = 0;
+
+	// add filename to list
+	as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1));
+	fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename);
+	as -> filelistlen += 1;
+	
+	f = fopen(filename, "rb");
+	if (!f)
+		return -1;
+
+	for (;;)
+	{
+		c = fgetc(f);
+		if (c == EOF)
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+		}
+		else if (c == '\r')
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+			// check for '\n':
+			c2 = fgetc(f);
+			if (c2 == EOF)
+				c = EOF;
+			else if (c2 != '\n')
+				ungetc(c2, f);
+		}
+		else if (c == '\n')
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+			// check for '\r':
+			c2 = fgetc(f);
+			if (c2 == EOF)
+				c = EOF;
+			else if (c2 != '\r')
+				ungetc(c2, f);
+		}
+		else
+		{
+			// silently ignore characters past 2K on a line... FIXME
+			if (lbloc < 2048)
+				linebuff[lbloc++] = c;
+		}
+		if (eol)
+		{
+			eol = 0;
+			lbloc = 0;
+			nl = lwasm_alloc(sizeof(lwasm_line_t));
+			nl -> text = lwasm_strdup(linebuff);
+			nl -> lineno = lineno++;
+			nl -> filename = fnref;
+			nl -> next = NULL;
+			nl -> prev = as -> linestail;
+			nl -> err = NULL;
+			nl -> fsize = 0;
+			nl -> sym = NULL;
+			nl -> bytes = NULL;
+			nl -> codelen = 0;
+			nl -> codesize = 0;
+			nl -> nocodelen = 0;
+			nl -> addrset = 0;
+			nl -> symaddr = -1;
+			nl -> badop = 0;
+			nl -> relocoff = -1;
+			if (as -> linestail)
+				as -> linestail -> next = nl;
+			as -> linestail = nl;
+			if (!(as -> lineshead))
+				as -> lineshead = nl;
+			lwasm_parse_line(as, nl);
+			if (as -> endseen)
+				break;
+		}
+		if (c == EOF)
+			break;
+	}
+	
+	fclose(f);
+	return 0;
+}
+
+void lwasm_pass1(asmstate_t *as)
+{
+	as -> passnum = 1;
+	as -> addr = 0;
+	as -> nextcontext = 1;
+
+	as -> inmod = 0;
+	
+	debug_message(1, "Entering pass 1");
+	if (lwasm_read_file(as, as -> infile) < 0)
+	{
+		fprintf(stderr, "Error reading input file '%s'", as -> infile);
+		perror("");
+		exit(1);
+	}
+	
+}