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

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

Open
zu2 opened this issue Nov 26, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@zu2
Copy link
Contributor

zu2 commented Nov 26, 2024

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 EtchedPixels added the enhancement New feature or request label Nov 27, 2024
@EtchedPixels
Copy link
Owner

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

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