Skip to content

Commit

Permalink
Yet another rework of HuCC's optimization of conditionals to move the…
Browse files Browse the repository at this point in the history
… detection

of "LD variable; test;" into the same set of peephole rules as the other checks
and then expand the existing peephole rules to understand the list of optimized
i-codes that the "test variable" and "not variable" rules can generate.

Add another peephole rule and set of optimized i-codes for when the result of a
"!" needs to produce a boolean instead of a flag/jump, such as "var = !var".
  • Loading branch information
jbrandwood committed Oct 17, 2024
1 parent 6096d6f commit 260ef30
Show file tree
Hide file tree
Showing 5 changed files with 910 additions and 561 deletions.
143 changes: 136 additions & 7 deletions include/hucc/hucc-codegen.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,17 @@ __tand.wi .macro
.endm

; **************
; convert boolean test result C flag into a 16-bit Y:A integer
; optimized boolean test
; invert C flag

__not.cf .macro
ror a
eor #$80
rol a
.endm

; **************
; convert comparison result C flag into a 16-bit Y:A boolean integer

__bool .macro
cla
Expand All @@ -1659,13 +1669,132 @@ __bool .macro
.endm

; **************
; optimized boolean test
; invert C flag
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if Y:A == 0, else false (0)

__not.cf .macro
ror a
eor #$80
rol a
__boolnot.wr .macro
sty __temp
ora __temp
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.wp .macro
ldy #1
lda [\1], y
ora [\1]
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.wm .macro
lda.l \1
ora.h \1
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.ws .macro ; __STACK
lda.l <__stack + \1, x
ora.h <__stack + \1, x
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.war .macro
asl a
tay
lda.l \1, y
ora.h \1, y
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.up .macro
lda [\1]
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.um .macro
lda \1
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.us .macro ; __STACK
lda.l <__stack + \1, x
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.uar .macro
tay
lda \1, y
cla
bne !+
inc a
!: cly
.endm

; **************
; optimized boolean test used before a store, not a branch
; Y:A is true (1) if memory-value == 0, else false (0)

__boolnot.uay .macro
lda \1, y
cla
bne !+
inc a
!: cly
.endm


Expand Down
63 changes: 61 additions & 2 deletions src/hucc/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,71 @@ void gen_code (INS *tmp)
nl();
break;

case X_NOT_CF:
ol("__not.cf");
break;

case I_BOOLEAN:
ol("__bool");
break;

case X_NOT_CF:
ol("__not.cf");
case X_BOOLNOT_WR:
ol("__boolnot.wr");
break;

case X_BOOLNOT_WP:
ot("__boolnot.wp\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_WM:
ot("__boolnot.wm\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_WS:
ot("__boolnot.ws\t");
outdec((int)data);
nl();
break;

case X_BOOLNOT_WAR:
ot("__boolnot.war\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_UP:
ot("__boolnot.up\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_UM:
ot("__boolnot.um\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_US:
ot("__boolnot.us\t");
outdec((int)data);
outlocal(tmp->sym);
nl();
break;

case X_BOOLNOT_UAR:
ot("__boolnot.uar\t");
out_addr(type, data);
nl();
break;

case X_BOOLNOT_UAY:
ot("__boolnot.uay\t");
out_addr(type, data);
nl();
break;

/* i-codes for loading the primary register */
Expand Down
13 changes: 12 additions & 1 deletion src/hucc/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,20 @@ enum ICODE {
X_NAND_WI,
X_TAND_WI,

I_BOOLEAN,
X_NOT_CF,

I_BOOLEAN,
X_BOOLNOT_WR,
X_BOOLNOT_WP,
X_BOOLNOT_WM,
X_BOOLNOT_WS,
X_BOOLNOT_WAR,
X_BOOLNOT_UP,
X_BOOLNOT_UM,
X_BOOLNOT_US,
X_BOOLNOT_UAR,
X_BOOLNOT_UAY,

/* i-codes for loading the primary register */

I_LD_WI,
Expand Down
Loading

0 comments on commit 260ef30

Please sign in to comment.