EtchedPixels / Fuzix-Compiler-Kit

Fuzix C Compiler Project
Other
50 stars 14 forks source link

On the MC6800, it is better to reverse the order of branches in a for statement. #173

Open zu2 opened 4 days ago

zu2 commented 4 days ago

backend.c, which processes the for statement:

      case H_FOR:     
                compile_expression();
                /* We will loop back to the conditional */
                gen_label("_l", h.h_name);
                /* A blank conditional on the for is a C oddity and means 'always true' */
                if (compile_expression() != VOID) {
                        /* Exit the loop if false */
                        gen_jfalse("_b", h.h_name);
                }
                /* Jump top the main body if not */
                gen_jump("_n", h.h_name);

This will output the following code:

In most cases, the update part is small and the body is large. Therefore, jeq is expanded to 5 bytes (bne + jmp). If changing jeq to jne and switch the jmp and destination, It can reduce the number of unnecessary jmps.

L29_l: ;; condition
        tsx
        ldb 1,x
        cmpb #8
        jsr boollt
        jeq L29_b
        jmp L29_n
L29_c: ;; update
        tsx
        inc 1,x
;
        jmp L29_l
L29_n: ;; body
        :
        :
L29_b: ;; end of body
EtchedPixels commented 3 days ago

Probably true for a lot of other processors as well (6502, Z80, 8086 etc)