view src/index.c @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
line wrap: on
line source

/*
 * index.c
 *
 * code for parsing indexed addressing modes
 */

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


int parse_index_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *postbyte, int *opmode, int *v1)
{
	static const char *regs = "X  Y  U  S  W  PCRPC ";
	static const struct { char *opstr; int pb; } simpleindex[] =
	{
		{",x", 0x84},		{",y", 0xa4},		{",u", 0xc4},		{",s", 0xe4},
		{",x+", 0x80},		{",y+", 0xa0},		{",u+", 0xc0},		{",s+", 0xe0},
		{",x++", 0x81},		{",y++", 0xa1},		{",u++", 0xc1},		{",s++", 0xe1},
		{",-x", 0x82},		{",-y", 0xa2},		{",-u", 0xc2},		{",-s", 0xe2},
		{",--x", 0x83},		{",--y", 0xa3},		{",--u", 0xc3},		{",--s", 0xe3},
		{"a,x", 0x86},		{"a,y", 0xa6},		{"a,u", 0xc6},		{"a,s", 0xe6},
		{"b,x", 0x85},		{"b,y", 0xa5},		{"b,u", 0xc5},		{"b,s", 0xe5},
		{"e,x", 0x87},		{"e,y", 0xa7},		{"e,u", 0xc7},		{"e,s", 0xe7},
		{"f,x",	0x8a},		{"f,y",	0xaa},		{"f,u", 0xca},		{"f,s", 0xea},
		{"d,x", 0x8b},		{"d,y", 0xab},		{"d,u", 0xcb},		{"d,s", 0xed},
		{"w,x", 0x8e},		{"w,y", 0xae},		{"w,u", 0xce},		{"w,s", 0xee},
		{",w", 0x8f},							{",w++", 0xcf},		{",--w", 0xef},
		
		{"[,x]", 0x94},		{"[,y]", 0xb4},		{"[,u", 0xd4},		{"[,s]", 0xf4},
		{"[,x++]", 0x91},	{"[,y++]", 0xb1},	{"[,u++]", 0xd1},	{"[,s++]", 0xf1},
		{"[,--x]", 0x93},	{"[,--y]", 0xb3},	{"[,--u]", 0xd3},	{"[,--s]", 0xf3},
		{"[a,x]", 0x96},	{"[a,y]", 0xb6},	{"[a,u]", 0xd6},	{"[a,s]", 0xf6},
		{"[b,x]", 0x95},	{"[b,y]", 0xb5},	{"[b,u]", 0xd5},	{"[b,s]", 0xf5},
		{"[e,x]", 0x97},	{"[e,y]", 0xb7},	{"[e,u]", 0xd7},	{"[e,s]", 0xf7},
		{"[f,x]", 0x9a},	{"[f,y]", 0xba},	{"[f,u]", 0xda},	{"[f,s]", 0xfa},
		{"[d,x]", 0x9b},	{"[d,y]", 0xbb},	{"[d,u]", 0xdb},	{"[d,s]", 0xfd},
		{"[w,x]", 0x9e},	{"[w,y]", 0xbe},	{"[w,u]", 0xde},	{"[w,s]", 0xfe},
		{"[,w]", 0x90},							{"[,w++]", 0xd0},	{"[,--w]", 0xf0},
		
		{ "", -1 }
	};
	char stbuf[25];
	int i;
	int f8 = 0, f16 = 0;
	int rn;
	int indir = 0;
	int rval;
	int f0 = 0;
 
	for (i = 0; i < 24; i++)
	{
		if (*((*optr) + i) && !isspace(*((*optr) + i)))
			stbuf[i] = *((*optr) + i);
		else
			break;
	}
	stbuf[i] = '\0';
	if (!*((*optr) + i) || isspace(*((*optr) + i)))
	{
		int j;
		// do simple lookup
		for (j = 0; simpleindex[j].opstr[0]; j++)
		{
			if (!strcasecmp(stbuf, simpleindex[j].opstr))
				break;
		}
		if (simpleindex[j].opstr[0])
		{
			*postbyte = simpleindex[j].pb;
			*opmode = OPER_INDEX;
			(*optr) += i;
			return 0;
		}
	}
	
	// now we have the hard ones to work out here
	
	if (**optr == '[')
	{
		indir = 1;
		(*optr)++;
	}
	
	rn = 0;
	for (i = 0; (*optr)[i] && !isspace((*optr)[i]); i++)
	{
		if ((*optr)[i] == ',')
		{
			rn = 1;
			break;
		}
	}
		
	if (!rn && indir)
	{
		// extended indir
		*postbyte = 0x9f;
		*opmode = OPER_EXTIND;
		rval = eval_expr(as, cl, optr, v1);
		if (**optr != ']')
		{
			errorp1(ERR_BADOPER);
		}
		(*optr)++;
		return 0;
	}

	if (cl -> p1f16)
		f16 = 1;	
	if (**optr == '<')
	{
		f8 = 1;
		(*optr)++;
	}
	else if (**optr == '>')
	{
		f16 = 1;
		(*optr)++;
	}

	if (**optr == '0' && *(*optr+1) == ',')
	{
		f0 = 1;
	}
	
	// now we have to evaluate the expression
	rval = eval_expr(as, cl, optr, v1);

	if (cl -> undef && as -> passnum == 1)
	{
		cl -> p1f16 = 1;
		f16 = 1;
	}

	// now look for a comma; if not present, explode
	if (rval == -1 || *(*optr)++ != ',')
	{
		// syntax error; force 0 bit
		*postbyte = -1;
		*opmode = OPER_INDEX;
		return -2;
	}
	
	// now get the register
	rn = lookupreg3(regs, optr);
	if (rn < 0)
		goto reterr;

//	debug("Reg: %d\n", rn);

	if (indir && **optr != ']')
		goto reterr;
	else
		(*optr)++;
	
	// nnnn,W is only 16 bit
	if (rn == 4)
	{
		if (f8)
			goto reterr;
		if (!f16 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE) && *v1 == 0)
		{
			*opmode = OPER_INDEX;
			if (indir)
				*postbyte = 0x90;
			else
				*postbyte = 0x8f;
			return 0;
		}
		*opmode = OPER_INDEX16;
		if (indir)
			*postbyte = 0xb0;
		else
			*postbyte = 0xcf;
		return 0;
	}
	
	if (indir) indir = 0x10;
	
	// forward ref; we can't figure anything
	if (cl -> undef)
	{
		f16 = 1;
	}

	// PCR? redo v1, v2 relative to current address
	if (rn == 5)
	{
		*v1 -= as -> addr;
		
		// we have a slight problem here
		// PCR based on current insn loc is really
		// -125 <= offset <= +130 (8 bit offset)
		if (f8 || (!f16 && *v1 >= -125 && *v1 <= 130))
		{
			*opmode = OPER_INDEX8;
			*postbyte = indir | 0x8C;
			*v1 -= 3;
			if (*v1 < -128 || *v1 > 127)
				errorp2(ERR_OVERFLOW);
			return 0;
		}
		
		// anything else is 16 bit offset
		// need 16 bit
		*opmode = OPER_INDEX16;
		*postbyte = indir | 0x8D;
		*v1 -= 4;
		return 0;
	}

	// constant offset from PC
	if (rn == 6)
	{
		if (f8 || (!f16 && *v1 >= -128 && *v1 <= 127))
		{
			*opmode = OPER_INDEX8;
			*postbyte = indir | 0x8C;
			if (*v1 < -128 || *v1 > 127)
				errorp2(ERR_OVERFLOW);
			return 0;
		}
		
		// everything else must be 16 bit
		// need 16 bit
		*opmode = OPER_INDEX16;
		*postbyte = indir | 0x8D;
		return 0;
		
	}

	// we only have to deal with x,y,u,s here
	if (!f8 && !f16 && *v1 >= -16 && *v1 <= 15)
	{
		if (*v1 == 0 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE))
		{
			*postbyte = rn << 5 | indir | 0x80 | 0x04;
			*opmode = OPER_INDEX;
			return 0;
		}
		// we pick the smallest value here
		if (indir)
		{
			f8 = 1;
			goto no5bit;
		}
		*postbyte = rn << 5 | (*v1 & 0x1F);
		*opmode = OPER_INDEX5;
		return 0;
	}
no5bit:
	if (f16 || (!f8 && (*v1 < -128 || *v1 > 127)))
	{
		// must be a 16 bit offset here
		*postbyte = rn << 5 | indir | 0x80 | 0x09;
		*opmode = OPER_INDEX16;
		return 0;
	}
	
	// if we're here, we have an 8 bit offset
	*postbyte = rn << 5 | indir | 0x80 | 0x08;
	*opmode = OPER_INDEX8;
	if (*v1 < -128 || *v1 > 127)
		errorp2(ERR_OVERFLOW);
	return 0;
reterr:
	*postbyte = -1;
	*opmode = OPER_INDEX;
	return -2;
}

void insn_indexed_aux(asmstate_t *as, sourceline_t *cl, char **optr, int *b1, int *b2, int *b3)
{
	int rval;
	int v1, pb = -1;

	*b1 = *b2 = *b3 = -1;

	rval = parse_index_expr(as, cl, optr, &pb, &(cl -> addrmode), &v1);
	if (rval < 0)
		errorp1(ERR_BADOPER);
	*b1 = pb;

	if (cl -> addrmode == OPER_INDEX8)
	{
		*b2 = v1 & 0xff;
	}
	else if (cl -> addrmode == OPER_INDEX16 || cl -> addrmode == OPER_EXTIND)
	{
		*b2 = (v1 & 0xffff) >> 8;
		*b3 = v1 & 0xff;
	}
}

void insn_indexed(asmstate_t *as, sourceline_t *cl, char **optr)
{
	int b1, b2, b3;
	
	emitop(instab[cl -> opcode].ops[0]);
	insn_indexed_aux(as, cl, optr, &b1, &b2, &b3);
	if (b1 != -1)
		emit(b1);
	if (b2 != -1)
		emit(b2);
	if (b3 != -1)
		emit(b3);
}