EtchedPixels / Fuzix-Compiler-Kit

Fuzix C Compiler Project
Other
49 stars 13 forks source link

be-codegen-6800.c ++/-- on char variables calls a helper, but this will be avoided. #142

Closed zu2 closed 2 weeks ago

zu2 commented 3 weeks ago

Currently, ++/-- on char variables calls a helper, but this will be avoided.

This will use 2 extra bytes of memory if you use the modified value, but it will be faster. If you don't need the modified value, the memory size is the same. (If you can output inc instead of addb #1, you can save 1 byte.)

--- ../Fuzix-Compiler-Kit/be-codegen-6800.c 2024-10-24 17:13:18
+++ be-codegen-6800.c   2024-10-28 18:30:40
@@ -1220,13 +1360,13 @@
    case T_PLUSPLUS:
        if (s == 1 && nr && memop_const(n, "inc"))
            return 1;
-       if (s == 2 && add_to_node(n, 1, nr))
+       if (s <= 2 && add_to_node(n, 1, nr))
            return 1;
        return do_xeqop(n, "xplusplus");
    case T_MINUSMINUS:
        if (s == 1 && nr && memop_const(n, "dec"))
            return 1;
-       if (s == 2 && add_to_node(n, -1, nr))
+       if (s <= 2 && add_to_node(n, -1, nr))
            return 1;
        return do_xeqop(n, "xmminus");
    case T_STAREQ:

old: 6bytes

        ldab #1
        tsx
        jsr __xplusplusuc

new: 8bytes (It's faster to do subb #1 than pshb/pulb)

        ldb 0,x
        pshb
        addb #1
        stb 0,x
        pulb

I would like to produce code like this, but this is also possible with rules.6800.

    ldb 0,x
    inc 0,x
zu2 commented 3 weeks ago

The idea is to avoid unnecessary ldb, but there seems to be a better way.

@@ -1300,16 +1413,35 @@
            return 1;
        return do_xeqop(n, "xminuseq");
    case T_PLUSPLUS:
        if (s == 1 && nr && memop_const(n, "inc"))
            return 1;
        if (s == 2 && add_to_node(n, 1, nr))
            return 1;
+       if (s == 1 && cpu == 6800){
+           if (r->op == T_CONSTANT && l->op == T_LOCAL){
+               v = load_x_with(l,0);
+               if (!nr)
+                   uniop_on_ptr("ldb",v,s);
+               uniop_on_ptr("inc", v, s);
+               return 1;
+           }
+       }
        return do_xeqop(n, "xplusplus");
    case T_MINUSMINUS:
        if (s == 1 && nr && memop_const(n, "dec"))
            return 1;
        if (s == 2 && add_to_node(n, -1, nr))
            return 1;
+       if (s == 1 && cpu == 6800){
+           if (r->op == T_CONSTANT && l->op == T_LOCAL){
+               v = load_x_with(l,0);
+               if (!nr)
+                   uniop_on_ptr("ldb",v,s);
+               uniop_on_ptr("dec", v, s);
+               return 1;
+           }
+       }
        return do_xeqop(n, "xmminus");
    case T_STAREQ:
        return do_xeqop(n, "xmuleq");
EtchedPixels commented 2 weeks ago

Fixed it all in memop_const

bcb41f7a466b015348b1794556f119e2709a074c