EtchedPixels / Fuzix-Compiler-Kit

Fuzix C Compiler Project
Other
49 stars 14 forks source link

be-code-6800.c dereferences to any "easy" expression #155

Closed zu2 closed 1 week ago

zu2 commented 2 weeks ago

It allows you to handle simple dereferences as described in Backend.md.

--- ../Fuzix-Compiler-Kit/be-code-6800.c    2024-11-06 01:14:49
+++ be-code-6800.c  2024-11-06 01:11:24
@@ -570,6 +570,7 @@
    switch(r->op) {
    case T_LSTORE:
    case T_LREF:
+   case T_LDEREF:
        off = make_local_ptr(v + off, 255);
        op8_on_ptr(op, off);
        break;
@@ -598,6 +599,7 @@
    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;
@@ -631,6 +633,7 @@
    switch(r->op) {
    case T_LSTORE:
    case T_LREF:
+   case T_LDEREF:
        off = make_local_ptr(v + off, 254);
        op16d_on_ptr(op, op2, off);
        break;
@@ -655,6 +658,7 @@
    switch(r->op) {
    case T_LSTORE:
    case T_LREF:
+   case T_LDEREF:
        off = make_local_ptr(v + off, 254);
        printf("\t%sy %u,x\n", op, off);
        break;

sample program.

struct x {
    unsigned char val;
};

unsigned foo(struct x *a, struct x *b) {
        return a->val + b->val;
}

before.

;make local ptr off 2, rlim 254 noff 2
    tsx
    ldx 2,x
    ldb 1,x
    lda 0,x
    pshb
    psha
;make local ptr off 4, rlim 254 noff 4
    tsx
    ldx 6,x
    ldb 1,x
    lda 0,x
    jsr __plus

after.

;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

However, if you change the structure variables from int to char, you are back to the original inefficient code.

struct x {
    char val;
};

unsigned foo(struct x *a, struct x *b) {
        return a->val + b->val;
}
;make local ptr off 2, rlim 255 noff 2
    tsx
    ldx 2,x
    ldb 0,x
    clra
    pshb
    psha
;make local ptr off 4, rlim 255 noff 4
    tsx
    ldx 6,x
    ldb 0,x
    clra
    jsr __plus
EtchedPixels commented 2 weeks ago

The char case fails because the tree is actually really return (unsigned)a->val + (unsigned)b->val because of the casts.

EtchedPixels commented 1 week ago

Thanks - merged