Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support6800/__abx.s The subscript calculation of the local array of char is wrong #171

Open
zu2 opened this issue Nov 25, 2024 · 3 comments

Comments

@zu2
Copy link
Contributor

zu2 commented Nov 25, 2024

test program:

executed, error 4 occurs.

unsigned char	verylargearray1[512];

int main(int argc, char *argv[])
{
	unsigned char	k;
	int		i,j;
	unsigned char	verylargearray2[512];

	for(i=0; i<512; i++){
		verylargearray1[i]  = i;
		verylargearray2[i]  = i;
	}

	k = 200;
	i = 400;
	j = 400&0xff;

	if (verylargearray1[400]!=j)
		return 1;
	if (verylargearray1[i]!=j)
		return 2;
	if (verylargearray1[k+k]!=j)
		return 3;
	if (verylargearray2[400]!=j)
		return 4;
	if (verylargearray2[i]!=j)
		return 5;
	if (verylargearray2[k+k]!=j)
		return 6;

	return 0;
}

patch.

--- ../Fuzix-Compiler-Kit.head/support6800/__abx.s	2024-11-21 13:07:40.382956269 +0000
+++ support6800/__abx.s	2024-11-25 09:13:10.699317090 +0000
@@ -7,14 +7,7 @@
 	.code
 
 __abx:
-	stx @tmp	; X where we can manipulate it
-	addb @tmp+1
-	adca #0
-	stab @tmp+1
-	staa @tmp
-	ldx @tmp
-	rts
-
+	clra
 __adx:
 	stx @tmp	; X where we can manipulate it
 	addb @tmp+1
@zu2
Copy link
Contributor Author

zu2 commented Nov 25, 2024

In the code before and after abx, unnecessary pshb/pulb are generated. I am also looking for the cause of this.

;make local ptr off 405, rlim 255 noff 4294902315
        tsx
        pshb
        ldab #150
        jsr __abx
        pulb
        ldb 255,x

@zu2
Copy link
Contributor Author

zu2 commented Nov 25, 2024

Regarding the pshb/pulb issue before and after abx,

When make_local_ptr is called, it is not determined whether ld will follow.

I was able to confirm that psh/pul disappears with the following duty hack, but it is not pretty.

--- ../Fuzix-Compiler-Kit/be-code-6800.c	2024-11-25 12:13:06.100609874 +0000
+++ be-code-6800.c	2024-11-25 12:09:38.800399241 +0000
@@ -442,7 +442,7 @@
 /*
  *	Generate a reference via X to a local
  */
-unsigned make_local_ptr(unsigned off, unsigned rlim)
+unsigned make_local_ptr2(unsigned off, unsigned rlim,unsigned save_d)
 {
 	/* Both relative to frame base */
 	int noff = off - x_fpoff;
@@ -472,27 +472,37 @@
 	   is fine as we push and load so can optimize, but the resulting
 	   case we can currently only invalidate for */
 	if (off - rlim < 256) {
-		puts("\tpshb");
+		if (save_d)
+			puts("\tpshb");
 		load_b_const(off - rlim);
 		if (cpu_has_d)	/* And thus ABX */
 			puts("\tabx");
 		else
 			puts("\tjsr __abx");
-		puts("\tpulb");
+		if (save_d)
+			puts("\tpulb");
 		x_fpoff -= off - rlim;
 		invalidate_work();
 		return rlim;
 	} else {
 		/* This case is (thankfully) fairly rare */
-		puts("\tpshb\n\tpsha");
+		if (save_d)
+			puts("\tpshb\n\tpsha");
 		load_d_const(off);
-		puts("\tjsr __adx\n\tpula\n\tpulb");
+		puts("\tjsr __adx");
+		if (save_d)
+			puts("\tpula\n\tpulb");
 		x_fpoff -= off;
 		invalidate_work();
 		return 0;
 	}
 }
 
+unsigned make_local_ptr(unsigned off, unsigned rlim)
+{
+	return make_local_ptr2(off,rlim,1);
+}
+
 /* Get pointer to the top of stack. We can optimize this in some cases
    when we track but it will be limited. The 6800 is quite weak on ops
    between register so we sometimes need to build ops against top of stack.
@@ -571,7 +581,7 @@
 	case T_LSTORE:
 	case T_LREF:
 	case T_LDEREF:
-		off = make_local_ptr(v + off, 255);
+		off = make_local_ptr2(v + off, 255, strncmp(op,"ld",2));
 		op8_on_ptr(op, off);
 		break;
 	case T_CONSTANT:
@@ -600,7 +610,7 @@
 	case T_LSTORE:
 	case T_LREF:
 	case T_LDEREF:
-		off = make_local_ptr(v + off, 254);
+		off = make_local_ptr2(v + off, 254, strncmp(op,"ld",2));
 		op16_on_ptr(op, op2, off);
 		break;
 	case T_CONSTANT:
@@ -634,7 +644,7 @@
 	case T_LSTORE:
 	case T_LREF:
 	case T_LDEREF:
-		off = make_local_ptr(v + off, 254);
+		off = make_local_ptr2(v + off, 254, strncmp(op,"ld",2));
 		op16d_on_ptr(op, op2, off);
 		break;
 	case T_CONSTANT:
@@ -659,7 +669,7 @@
 	case T_LSTORE:
 	case T_LREF:
 	case T_LDEREF:
-		off = make_local_ptr(v + off, 254);
+		off = make_local_ptr2(v + off, 254, strncmp(op,"ld",2));
 		printf("\t%sy %u,x\n", op, off);
 		break;
 	case T_CONSTANT:
@@ -710,7 +720,7 @@
 	switch(r->op) {
 	case T_LSTORE:
 	case T_LREF:
-		off = make_local_ptr(v + off, 254);
+		off = make_local_ptr2(v + off, 254, strncmp(op,"ld",2));
 		uniop_on_ptr(op, off, s);
 		break;
 	case T_LBSTORE:
@@ -947,6 +957,7 @@
    we want is in D */
 static unsigned load_r_with(char reg, register struct node *r, unsigned off)
 {
+	int	save_d = (reg!='a') && (reg!='b') && (reg!='d');
 	register unsigned v = r->value;
 	switch(r->op) {
 	case T_ARGUMENT:
@@ -954,9 +965,9 @@
 	case T_LOCAL:
 		/* TODO: will need a Y specific rule for HC11 */
 		/* Worst case for size is 252 */
-		return make_local_ptr(v + off, 252);
+		return make_local_ptr2(v + off, 252,save_d);
 	case T_LREF:
-		off = make_local_ptr(v + off, 252);
+		off = make_local_ptr2(v + off, 252,save_d);
 		printf("\tld%c %u,x\n", reg, off);
 		if(reg == 'x')
 			invalidate_x();

@EtchedPixels
Copy link
Owner

Done the fix. Need to think about the rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants