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

be-code-6800.c Pointer dereference is wrong #180

Open
zu2 opened this issue Dec 7, 2024 · 1 comment
Open

be-code-6800.c Pointer dereference is wrong #180

zu2 opened this issue Dec 7, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@zu2
Copy link
Contributor

zu2 commented Dec 7, 2024

test program. it fails 1.

struct complex {
    int r;
    int i;
};

void complex_add(struct complex *com_1, struct complex *com_2, struct complex *com_3){
    com_3->r = com_1->r + com_2->r;
    com_3->i = com_1->i + com_2->i;
}

void complex_add2(struct complex *com_1, struct complex *com_2, struct complex *com_3){
    (*com_3).r = (*com_1).r + (*com_2).r;
    (*com_3).i = (*com_1).i + (*com_2).i;
}

main(){
        struct complex com01, com02, com03;

        com01.r=2;
        com01.i=3;
        com02.r=4;
        com02.i=8;

        complex_add(&com01,&com02,&com03);

	if (com01.r + com02.r != com03.r)
		return 1;
	if (com01.i + com02.i != com03.i)
		return 2;

        complex_add2(&com01,&com02,&com03);
	if (com01.r + com02.r != com03.r)
		return 11;
	if (com01.i + com02.i != com03.i)
		return 12;

	return 0;
}

Part of the compiled program.

The ldx 4,x for the second node has not been generated.

_complex_add:
;make local ptr off 2, rlim 254 noff 2
        tsx
        ldx 2,x
        ldb 1,x
        lda 0,x
;make local ptr off 4, rlim 254 noff 4
        tsx
        addb 5,x
        adca 4,x
;make local ptr off 6, rlim 254 noff 6
        ldx 6,x
        stb 1,x
        sta 0,x

patch. works fine.

This patch fixes op16_on_node, but similar fixes are needed for other opXX_on_node.

--- ../Fuzix-Compiler-Kit.head/be-code-6800.c	2024-12-05 18:32:32.401041117 +0000
+++ be-code-6800.c	2024-12-07 07:35:01.435900981 +0000
@@ -595,10 +595,16 @@
 	switch(r->op) {
 	case T_LSTORE:
 	case T_LREF:
-	case T_LDEREF:
 		off = make_local_ptr(v + off, 254);
 		op16_on_ptr(op, op2, off);
 		break;
+	case T_LDEREF:
+		off = make_local_ptr(v + off, 254);
+		printf("\tldx %u,x\n", off);
+		invalidate_x();
+		v = r->val2;
+		op16_on_ptr(op, op2, v);
+		break;
 	case T_CONSTANT:
 		printf("\t%sb #<%u\n", op, (v + off) & 0xFFFF);
 		printf("\t%sa #>%u\n", op2, (v + off) & 0xFFFF);
@EtchedPixels
Copy link
Owner

Not sure the change is safe as I believe we have case (eg += etc) where we need to preserve X, and the helper has no idea if this is the case.

@EtchedPixels EtchedPixels added the enhancement New feature or request label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants