view src/list.c @ 3:46e532283947

Add TODO list
author lost
date Fri, 03 Oct 2008 06:16:20 +0000
parents 57495da01900
children 34568fab6058
line wrap: on
line source

/*
 * list.c
 *
 * code for displaying a program listing in lwasm
 */

//#include <ctype.h>
#include <errno.h>
#include <stdio.h>
//#include <stdlib.h>
#include <string.h>
#define __list_c_seen__
//#include "instab.h"
#include "lwasm.h"

const char *errlist[] =
{
	"No error",
	"Bad opcode",
	"Illegal Symbol",
	"Multiply defined symbol",
	"Symbol required but not present",
	"Forward references not permitted",
	"Byte overflow",
	"Phase error",
	"Bad operand",
	"Symbol not permitted here",
	"Undefined symbol",
	"Bit number out of range",
	"Invalid expression",
	"Invalid register",
	"Bad file name",
	"ENDM without MACRO",
	"Redefined macro",
	"Nested namespace",
	"Bad condition",
	"User error",
	"Bad pragma",
	""
};

void list_code(asmstate_t *as)
{
	FILE *lf;
	sourceline_t *cl;
	int bn;
	int c;
	char *t;
	
	if (as -> listfile && strcmp(as -> listfile, "-"))
	{
		lf = fopen(as -> listfile, "w");
		if (!lf)
		{
			perror("Cannot open list file");
			return;
		}
	}
	else
	{
		lf = stdout;
	}
	
	for (cl = as -> source_head; cl; cl = cl -> next)
	{
		bn = 0;
		if (cl -> errors)
		{
			errortab_t *e;
			for (e = cl -> errors; e; e = e -> next)
			{
				if (e -> errnum < ERR_MAXERR && e -> errnum != ERR_USER)
					fprintf(lf, "*****ERROR: %s\n", errlist[e -> errnum]);
			}
			if (cl -> user_error)
			{
				fprintf(lf, "*****ERROR: %s\n", cl -> user_error);
			}
		}
		if (cl -> skipped)
		{
			fprintf(lf, "%-15.15s", "<skipped>");
		}
		else if (cl -> macrodef)
		{
			fprintf(lf, "%-15.15s", "<macrodef>");
		}
		else if (cl -> opcode >= 0 && cl -> numcodebytes > 0)
		{
			fprintf(lf, "%04X ", cl -> addr);
			while (bn < 5 && bn < cl -> numcodebytes)
			{
				fprintf(lf, "%02X", cl -> codebytes[bn]);
				bn++;
			}
			while (bn < 5)
			{
				fprintf(lf, "  ");
				bn++;
			}
		}
		else if (cl -> addrset || (cl -> len && cl -> numcodebytes == 0))
		{
			fprintf(lf, "%04X %10s", cl -> addr, "");
		}
		else if (cl -> isequ)
		{
			fprintf(lf, "     %04X      ", cl -> symaddr);
		}
		else if (cl -> issetdp)
		{
			fprintf(lf, "     %02X        ", cl -> dpval);
		}
		else
			fprintf(lf, "               ");
		fprintf(lf, " %15.15s:%06d ", cl -> sourcefile, cl -> lineno);
		// actually display the line from the file
		for (c = 0, t = cl -> line; *t; t++)
		{
			if (*t == '\n' || *t == '\r')
				break;
			if (*t == '\t')
			{
				do
				{
					fprintf(lf, " ");
					c++;
				} while (c % 8);
			}
			else
			{
				c++;
				fprintf(lf, "%c", *t);
			}
		}
//		fprintf(lf, "\n");

		while (bn < cl -> numcodebytes)
		{
			if (bn % 5 == 0)
				fprintf(lf, "\n%04X ", (cl -> addr + bn) & 0xFFFF);
			fprintf(lf, "%02X", cl -> codebytes[bn]);
			bn++;
		}
		fprintf(lf, "\n");
	}

	fprintf(lf, "\n");
	list_symbols(as, lf);
	
	if (lf != stdout)
		fclose(lf);
}