changeset 21:3c0e5f311c95

Added reading of input file for pass1
author lost
date Fri, 02 Jan 2009 00:43:06 +0000
parents 610710a7859f
children b704f7ffc8ba
files src/lwasm.h src/pass1.c
diffstat 2 files changed, 134 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/lwasm.h	Fri Jan 02 00:42:11 2009 +0000
+++ b/src/lwasm.h	Fri Jan 02 00:43:06 2009 +0000
@@ -26,8 +26,17 @@
 
 #define OUTPUT_DECB		0	// DECB multirecord format
 #define OUTPUT_RAW		1	// raw sequence of bytes
-#define OUTPUT_RAWREL	2	// raw but with ORG as a relative offset
-#define OUTPUT_OBJ		3	// proprietary object file format
+#define OUTPUT_OBJ		2	// proprietary object file format
+
+// structure for keeping track of lines
+typedef struct lwasm_line_s lwasm_line_t;
+struct lwasm_line_s {
+	char *text;			// the actual text of the line
+	int lineno;			// line number within the file
+	char *filename;		// file name reference
+	lwasm_line_t *next;	// next line
+	lwasm_line_t *prev;	// previous line
+};
 
 // keep track of current assembler state
 typedef struct {
@@ -35,13 +44,19 @@
 	int addr;					// current address
 	int errorcount;				// error count
 	int passnum;				// which pass are we on?
+	int execaddr;				// execution address for the program (END ....)
+	int pragmas;				// what pragmas are in effect?
+
+	lwasm_line_t *lineshead;	// first line of source code
+	lwasm_line_t *linestail;	// last line of source code
+
 	const char *infile;			// input file
 	const char *outfile;		// output file
 	const char *listfile;		// output listing file
 	int debug;					// debug mode
 	int outformat;				// output format type
-	int execaddr;				// execution address for the program (END ....)
-	int pragmas;				// what pragmas are in effect?
+	char **filelist;			// files that have been read
+	int filelistlen;			// number of files in the list
 } asmstate_t;
 
 #define PRAGMA_NOINDEX0TONONE	1
--- a/src/pass1.c	Fri Jan 02 00:42:11 2009 +0000
+++ b/src/pass1.c	Fri Jan 02 00:43:06 2009 +0000
@@ -24,27 +24,137 @@
 
 1. read all lines from the main source file, following all "include"
    directives as appropriate
-2. parse each line into a symbol, operation code, and operand as appropriate
-3. each operand is evaluated for syntax and futher for value if there are
+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)
-4. addresses are assigned to every symbol defined in the assembly
-5. macros are defined and expanded at this pass
+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).
 */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "lwasm.h"
+#include "util.h"
+
+// 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, "r");
+	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)
+		{
+			fprintf(stderr, "READ: %s\n", linebuff);
+			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;
+			if (as -> linestail)
+				as -> linestail -> next = nl;
+			else
+				as -> linestail = nl;
+			if (!(as -> lineshead))
+				as -> lineshead = nl;
+		}
+		if (c == EOF)
+			break;
+	}
+	
+	fclose(f);
+	return 0;
+}
 
 void lwasm_pass1(asmstate_t *as)
 {
+	as -> passnum = 1;
+	
+	if (lwasm_read_file(as, as -> infile) < 0)
+	{
+		fprintf(stderr, "Error reading input file '%s'", as -> infile);
+		perror("");
+		exit(1);
+	}
+	
 }