~grimmware/lc3

77a9b54890eb5d521445b05b973a63980946748a — glenda 1 year, 2 months ago 1529e3e
Replaced majority of hexnparse with litnparse
2 files changed, 16 insertions(+), 49 deletions(-)

M asm.c
M tests/hello.asm
M asm.c => asm.c +9 -42
@@ 153,18 153,6 @@ parseargs(char* t, Inst* in)
	// FIXME should probably find some way to indicate larger arity errors here?
}

u16int 
hexnparselit(int b, char *s)
{
	u16int n;
	long m;
	m = strtol(s, 0, 16);
	n = (u16int) m;
	if(n != m || n >= (1 << b))
		syntaxerr(smprint("Hex value %s is too big to be represented in %d bits", s, b));
	return n;
}

u16int
basenparse(int bits, int base, char *s)
{


@@ 178,27 166,6 @@ basenparse(int bits, int base, char *s)
}

u16int
binnparselit(int b, char *s)
{
	return 1;
}

u16int
decnparselit(int b, char *s)
{
	return 1;
}

u16int
hexnparse(int b, char *s)
{
	if(s[0] != 'x')
		syntaxerr(smprint("Not a hex value, must indicate base with a leading `x`: %s", s));
	s++;
	return hexnparselit(b, s);
}

u16int
litnparse(int b, char *s)
{
	switch(s[0]){


@@ 255,13 222,13 @@ validate(Inst *in)
		if(in->op == OP_ADD || in->op == OP_AND){
			p = findreg(in->arg[2]);
			if(p == -1){
				in->p_arg[2] = hexnparse(5, in->arg[2]);
				in->p_arg[2] = litnparse(5, in->arg[2]);
				in->mode++;
			} else {
				in->p_arg[2] = (u16int) p;
			}
		} else { // STR and LDR which have the same signature
			in->p_arg[2] = hexnparse(6, in->arg[2]);
			in->p_arg[2] = litnparse(6, in->arg[2]);
		}
	}



@@ 284,7 251,7 @@ validate(Inst *in)
		case OP_ST:
		case OP_STI:
			if(in->arg[1][0] == 'x'){
				in->p_arg[1] = hexnparse(9, in->arg[1]);
				in->p_arg[1] = litnparse(9, in->arg[1]);
			} else {
				unmapped[unmappedcount++] = pc;
			}


@@ 296,7 263,7 @@ validate(Inst *in)
		switch(in->op){
		case OP_BR:
			if(in->arg[0][0] == 'x'){
				in->p_arg[0] = hexnparse(9, in->arg[0]);
				in->p_arg[0] = litnparse(9, in->arg[0]);
			} else {
				unmapped[unmappedcount++] = pc;
			}


@@ 305,7 272,7 @@ validate(Inst *in)
			// FIXME Make sure mode is set by the parser!
			if(in->mode){ // JSR
				if(in->arg[0][0] == 'x'){
					in->p_arg[0] = hexnparse(11, in->arg[0]);
					in->p_arg[0] = litnparse(11, in->arg[0]);
				} else {
					unmapped[unmappedcount++] = pc;
				}


@@ 314,10 281,10 @@ validate(Inst *in)
			}
			break;
		case OP_TRAP:
			in->p_arg[0] = hexnparse(8, in->arg[0]);
			in->p_arg[0] = litnparse(8, in->arg[0]);
			break;
		case OP_DATA:
			in->p_arg[0] = hexnparse(16, in->arg[0]);
			in->p_arg[0] = litnparse(16, in->arg[0]);
			break;
		default:
		Reg:


@@ 384,7 351,7 @@ parseline(char *l)
			} else if(strcmp(t,".BLKW") == 0) {
				l++;
				gettoken(&t, l);
				for(int i = 0; i<hexnparse(16, t); i++){
				for(int i = 0; i<litnparse(16, t); i++){
					inst = newinst(OP_DATA);
					inst->arg[0] = "x0";
					inst->argc = 1;


@@ 402,7 369,7 @@ parseline(char *l)
					if(t[i] != 0)
						p|= t[i+1] << 8;
					inst = newinst(OP_DATA);
					inst->p_arg[0] = hexnparselit(16, (char *) &p);
					inst->p_arg[0] = basenparse(16, 16, (char *) &p);
					inst->argc++;
					pc++;
				}

M tests/hello.asm => tests/hello.asm +7 -7
@@ 8,17 8,17 @@ LOOPH	ADD	R0,R0,xf
	ADD	R3,R1,R2	; compare if we're at 6 loops yet
	BRn	LOOPH
	ADD	R0,R0,xe
	TRAP	x21
E	.FILL	x65
L	.FILL	x6c
	OUT
E	.FILL	#101		; testing decimal
L	.FILL	b01101100	; testing binary
O	.FILL	x6f
	LD	R0,E
	TRAP	x21
	OUT
	LD	R0,L
	TRAP	x21
	TRAP	x21
	OUT
	OUT
	LD	R0,O
	TRAP	x21
	TRAP	x21		; testing manual trap
	.BLKW	x5
	LEA	R0,WRLD
	PUTSP