1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-16 18:30:37 +00:00

Working on classic unions. #197

This commit is contained in:
jespergravgaard 2021-07-27 22:32:54 +02:00
parent 5050079229
commit ea70b1f2e3
8 changed files with 546 additions and 7 deletions

View File

@ -335,6 +335,8 @@ public class VariableBuilder {
return false;
else if(isTypeStruct() && config.isStructModelClassic())
return false;
else if(isTypeStruct() && ((SymbolTypeStruct)type).isUnion())
return false;
else {
VariableBuilderConfig.Scope scope = VariableBuilderConfig.getScope(isScopeGlobal(), isScopeLocal(), isScopeIntermediate(), isScopeParameter(), isScopeMember());
VariableBuilderConfig.Type type = VariableBuilderConfig.getType(isTypeInteger(), isArray(), isTypePointer(), isTypeStruct(), isTypeVar());

View File

@ -2238,10 +2238,10 @@ public class TestProgramsFast extends TestPrograms {
}
//@Test
//public void testUnion7() throws IOException {
// compileAndCompare("union-7.c", log().verboseStructUnwind());
//}
@Test
public void testUnion7() throws IOException {
compileAndCompare("union-7.c");
}
@Test
public void testUnion6() throws IOException {

View File

@ -18,7 +18,7 @@ void main() {
}
struct Data sum(char a,char b) {
__ma struct Data d = { a+b, b };
struct Data d = { a+b, b };
return d;
}

View File

@ -53,7 +53,7 @@ sum: {
stx.z $ff
clc
adc.z $ff
// __ma struct Data d = { a+b, b }
// struct Data d = { a+b, b }
sta.z d
stx d+OFFSET_STRUCT_DATA_D
// return d;

View File

@ -513,7 +513,7 @@ sum: {
stx.z $ff
clc
adc.z $ff
// __ma struct Data d = { a+b, b }
// struct Data d = { a+b, b }
// [10] *((byte*)&sum::d) = sum::$0 -- _deref_pbuc1=vbuaa
sta.z d
// [11] *((byte*)&sum::d+OFFSET_STRUCT_DATA_D) = sum::b#2 -- _deref_pbuc1=vbuxx

60
src/test/ref/union-7.asm Normal file
View File

@ -0,0 +1,60 @@
// Minimal union with C-Standard behavior - union parameter
// Commodore 64 PRG executable file
.file [name="union-7.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
.const SIZEOF_UNION_DATA = 2
.label SCREEN = $400
.segment Code
main: {
// print(data1)
ldy #SIZEOF_UNION_DATA
!:
lda data1-1,y
sta print.data-1,y
dey
bne !-
ldx #0
jsr print
// print(data2)
ldy #SIZEOF_UNION_DATA
!:
lda data2-1,y
sta print.data-1,y
dey
bne !-
jsr print
// }
rts
}
// print(union Data zp(2) data)
print: {
.label data = 2
// BYTE1(data.w)
lda.z data+1
// SCREEN[idx++] = BYTE1(data.w)
sta SCREEN,x
// SCREEN[idx++] = BYTE1(data.w);
inx
// SCREEN[idx++] = data.b
lda.z data
sta SCREEN,x
// SCREEN[idx++] = data.b;
inx
// SCREEN[idx++] = ' '
lda #' '
sta SCREEN,x
// SCREEN[idx++] = ' ';
inx
// }
rts
}
.segment Data
data1: .byte $12
.fill 1, 0
data2: .byte $34
.fill 1, 0

28
src/test/ref/union-7.cfg Normal file
View File

@ -0,0 +1,28 @@
void main()
main: scope:[main] from
[0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA)
[1] call print
to:main::@1
main::@1: scope:[main] from main
[2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA)
[3] call print
to:main::@return
main::@return: scope:[main] from main::@1
[4] return
to:@return
void print(union Data print::data)
print: scope:[print] from main main::@1
[5] idx#13 = phi( main/0, main::@1/idx#14 )
[6] print::$0 = byte1 *((word*)&print::data)
[7] SCREEN[idx#13] = print::$0
[8] idx#3 = ++ idx#13
[9] SCREEN[idx#3] = *((byte*)&print::data)
[10] idx#4 = ++ idx#3
[11] SCREEN[idx#4] = ' '
[12] idx#14 = ++ idx#4
to:print::@return
print::@return: scope:[print] from print
[13] return
to:@return

449
src/test/ref/union-7.log Normal file
View File

@ -0,0 +1,449 @@
Inlined call call __init
Removing C-classic struct-unwound assignment print::data = struct-unwound {*(&print::data)}
Removing C-classic struct-unwound assignment print::data = struct-unwound {*(&print::data)}
CONTROL FLOW GRAPH SSA
void main()
main: scope:[main] from __start::@1
idx#17 = phi( __start::@1/idx#18 )
*(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA)
call print
to:main::@1
main::@1: scope:[main] from main
idx#10 = phi( main/idx#6 )
idx#0 = idx#10
*(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA)
call print
to:main::@2
main::@2: scope:[main] from main::@1
idx#11 = phi( main::@1/idx#6 )
idx#1 = idx#11
to:main::@return
main::@return: scope:[main] from main::@2
idx#12 = phi( main::@2/idx#1 )
idx#2 = idx#12
return
to:@return
void print(union Data print::data)
print: scope:[print] from main main::@1
idx#13 = phi( main/idx#17, main::@1/idx#0 )
print::$0 = byte1 *((word*)&print::data+OFFSET_UNION_DATA_W)
SCREEN[idx#13] = print::$0
idx#3 = ++ idx#13
SCREEN[idx#3] = *((byte*)&print::data+OFFSET_UNION_DATA_B)
idx#4 = ++ idx#3
SCREEN[idx#4] = ' '
idx#5 = ++ idx#4
to:print::@return
print::@return: scope:[print] from print
idx#14 = phi( print/idx#5 )
idx#6 = idx#14
return
to:@return
void __start()
__start: scope:[__start] from
to:__start::__init1
__start::__init1: scope:[__start] from __start
idx#7 = 0
to:__start::@1
__start::@1: scope:[__start] from __start::__init1
idx#18 = phi( __start::__init1/idx#7 )
call main
to:__start::@2
__start::@2: scope:[__start] from __start::@1
idx#15 = phi( __start::@1/idx#2 )
idx#8 = idx#15
to:__start::@return
__start::@return: scope:[__start] from __start::@2
idx#16 = phi( __start::@2/idx#8 )
idx#9 = idx#16
return
to:@return
SYMBOL TABLE SSA
constant byte OFFSET_UNION_DATA_B = 0
constant byte OFFSET_UNION_DATA_W = 0
constant byte* const SCREEN = (byte*)$400
constant byte SIZEOF_UNION_DATA = 2
void __start()
union Data data1 loadstore = { b: $12 }
union Data data2 loadstore = { b: $34 }
byte idx
byte idx#0
byte idx#1
byte idx#10
byte idx#11
byte idx#12
byte idx#13
byte idx#14
byte idx#15
byte idx#16
byte idx#17
byte idx#18
byte idx#2
byte idx#3
byte idx#4
byte idx#5
byte idx#6
byte idx#7
byte idx#8
byte idx#9
void main()
void print(union Data print::data)
byte~ print::$0
union Data print::data loadstore
Simplifying constant pointer cast (byte*) 1024
Successful SSA optimization PassNCastSimplification
Alias idx#0 = idx#10
Alias idx#1 = idx#11 idx#12 idx#2
Alias idx#14 = idx#5 idx#6
Alias idx#18 = idx#7
Alias idx#15 = idx#8 idx#16 idx#9
Successful SSA optimization Pass2AliasElimination
Identical Phi Values idx#17 idx#18
Identical Phi Values idx#0 idx#14
Identical Phi Values idx#1 idx#14
Identical Phi Values idx#15 idx#1
Successful SSA optimization Pass2IdenticalPhiElimination
Constant idx#18 = 0
Successful SSA optimization Pass2ConstantIdentification
Simplifying expression containing zero (word*)&print::data in [9] print::$0 = byte1 *((word*)&print::data+OFFSET_UNION_DATA_W)
Simplifying expression containing zero (byte*)&print::data in [12] SCREEN[idx#3] = *((byte*)&print::data+OFFSET_UNION_DATA_B)
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused constant OFFSET_UNION_DATA_W
Eliminating unused constant OFFSET_UNION_DATA_B
Successful SSA optimization PassNEliminateUnusedVars
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::__init1
Removing unused procedure block __start::@1
Removing unused procedure block __start::@2
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Inlining constant with var siblings idx#18
Constant inlined idx#18 = 0
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of main::@2
CALL GRAPH
Calls in [main] to print:1 print:4
Created 1 initial phi equivalence classes
Coalesced [3] idx#19 = idx#14
Coalesced down to 1 phi equivalence classes
Culled Empty Block label main::@2
FINAL CONTROL FLOW GRAPH
void main()
main: scope:[main] from
[0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA)
[1] call print
to:main::@1
main::@1: scope:[main] from main
[2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA)
[3] call print
to:main::@return
main::@return: scope:[main] from main::@1
[4] return
to:@return
void print(union Data print::data)
print: scope:[print] from main main::@1
[5] idx#13 = phi( main/0, main::@1/idx#14 )
[6] print::$0 = byte1 *((word*)&print::data)
[7] SCREEN[idx#13] = print::$0
[8] idx#3 = ++ idx#13
[9] SCREEN[idx#3] = *((byte*)&print::data)
[10] idx#4 = ++ idx#3
[11] SCREEN[idx#4] = ' '
[12] idx#14 = ++ idx#4
to:print::@return
print::@return: scope:[print] from print
[13] return
to:@return
VARIABLE REGISTER WEIGHTS
union Data data1 loadstore = { b: $12 }
union Data data2 loadstore = { b: $34 }
byte idx
byte idx#13 8.0
byte idx#14 3.25
byte idx#3 16.5
byte idx#4 16.5
void main()
void print(union Data print::data)
byte~ print::$0 22.0
union Data print::data loadstore
Initial phi equivalence classes
[ idx#13 idx#14 ]
Added variable print::$0 to live range equivalence class [ print::$0 ]
Added variable idx#3 to live range equivalence class [ idx#3 ]
Added variable idx#4 to live range equivalence class [ idx#4 ]
Added variable data1 to live range equivalence class [ data1 ]
Added variable data2 to live range equivalence class [ data2 ]
Added variable print::data to live range equivalence class [ print::data ]
Complete equivalence classes
[ idx#13 idx#14 ]
[ print::$0 ]
[ idx#3 ]
[ idx#4 ]
[ data1 ]
[ data2 ]
[ print::data ]
Allocated zp[1]:2 [ idx#13 idx#14 ]
Allocated zp[1]:3 [ print::$0 ]
Allocated zp[1]:4 [ idx#3 ]
Allocated zp[1]:5 [ idx#4 ]
Allocated mem[2] [ data1 ]
Allocated mem[2] [ data2 ]
Allocated zp[2]:6 [ print::data ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA) [ print::data data2 ] ( [ print::data data2 ] { } ) always clobbers reg byte a reg byte y
Statement [2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA) [ print::data idx#14 ] ( [ print::data idx#14 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a reg byte y
Removing always clobbered register reg byte a as potential for zp[1]:2 [ idx#13 idx#14 ]
Removing always clobbered register reg byte y as potential for zp[1]:2 [ idx#13 idx#14 ]
Statement [9] SCREEN[idx#3] = *((byte*)&print::data) [ print::data idx#3 ] ( print:1 [ data2 print::data idx#3 ] { } print:3 [ print::data idx#3 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a
Removing always clobbered register reg byte a as potential for zp[1]:4 [ idx#3 ]
Statement [11] SCREEN[idx#4] = ' ' [ print::data idx#4 ] ( print:1 [ data2 print::data idx#4 ] { } print:3 [ print::data idx#4 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a
Removing always clobbered register reg byte a as potential for zp[1]:5 [ idx#4 ]
Statement [0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA) [ print::data data2 ] ( [ print::data data2 ] { } ) always clobbers reg byte a reg byte y
Statement [2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA) [ print::data idx#14 ] ( [ print::data idx#14 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a reg byte y
Statement [9] SCREEN[idx#3] = *((byte*)&print::data) [ print::data idx#3 ] ( print:1 [ data2 print::data idx#3 ] { } print:3 [ print::data idx#3 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a
Statement [11] SCREEN[idx#4] = ' ' [ print::data idx#4 ] ( print:1 [ data2 print::data idx#4 ] { } print:3 [ print::data idx#4 ] { { idx#13 = idx#14 } } ) always clobbers reg byte a
Potential registers zp[1]:2 [ idx#13 idx#14 ] : zp[1]:2 , reg byte x ,
Potential registers zp[1]:3 [ print::$0 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:4 [ idx#3 ] : zp[1]:4 , reg byte x , reg byte y ,
Potential registers zp[1]:5 [ idx#4 ] : zp[1]:5 , reg byte x , reg byte y ,
Potential registers mem[2] [ data1 ] : mem[2] ,
Potential registers mem[2] [ data2 ] : mem[2] ,
Potential registers zp[2]:6 [ print::data ] : zp[2]:6 ,
REGISTER UPLIFT SCOPES
Uplift Scope [] 16.5: zp[1]:4 [ idx#3 ] 16.5: zp[1]:5 [ idx#4 ] 11.25: zp[1]:2 [ idx#13 idx#14 ] 0: mem[2] [ data1 ] 0: mem[2] [ data2 ]
Uplift Scope [print] 22: zp[1]:3 [ print::$0 ] 0: zp[2]:6 [ print::data ]
Uplift Scope [Data]
Uplift Scope [main]
Uplifting [] best 102 combination reg byte x [ idx#3 ] reg byte x [ idx#4 ] reg byte x [ idx#13 idx#14 ] mem[2] [ data1 ] mem[2] [ data2 ]
Uplifting [print] best 96 combination reg byte a [ print::$0 ] zp[2]:6 [ print::data ]
Uplifting [Data] best 96 combination
Uplifting [main] best 96 combination
Allocated (was zp[2]:6) zp[2]:2 [ print::data ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Minimal union with C-Standard behavior - union parameter
// Upstart
// Commodore 64 PRG executable file
.file [name="union-7.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
// Global Constants & labels
.const SIZEOF_UNION_DATA = 2
.label SCREEN = $400
.segment Code
// main
main: {
// [0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA) -- _deref_pssc1=_deref_pssc2_memcpy_vbuc3
ldy #SIZEOF_UNION_DATA
!:
lda data1-1,y
sta print.data-1,y
dey
bne !-
// [1] call print
// [5] phi from main to print [phi:main->print]
print_from_main:
// [5] phi idx#13 = 0 [phi:main->print#0] -- vbuxx=vbuc1
ldx #0
jsr print
jmp __b1
// main::@1
__b1:
// [2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA) -- _deref_pssc1=_deref_pssc2_memcpy_vbuc3
ldy #SIZEOF_UNION_DATA
!:
lda data2-1,y
sta print.data-1,y
dey
bne !-
// [3] call print
// [5] phi from main::@1 to print [phi:main::@1->print]
print_from___b1:
// [5] phi idx#13 = idx#14 [phi:main::@1->print#0] -- register_copy
jsr print
jmp __breturn
// main::@return
__breturn:
// [4] return
rts
}
// print
// print(union Data zp(2) data)
print: {
.label data = 2
// [6] print::$0 = byte1 *((word*)&print::data) -- vbuaa=_byte1__deref_pwuc1
lda.z data+1
// [7] SCREEN[idx#13] = print::$0 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// [8] idx#3 = ++ idx#13 -- vbuxx=_inc_vbuxx
inx
// [9] SCREEN[idx#3] = *((byte*)&print::data) -- pbuc1_derefidx_vbuxx=_deref_pbuc2
lda.z data
sta SCREEN,x
// [10] idx#4 = ++ idx#3 -- vbuxx=_inc_vbuxx
inx
// [11] SCREEN[idx#4] = ' ' -- pbuc1_derefidx_vbuxx=vbuc2
lda #' '
sta SCREEN,x
// [12] idx#14 = ++ idx#4 -- vbuxx=_inc_vbuxx
inx
jmp __breturn
// print::@return
__breturn:
// [13] return
rts
}
// File Data
.segment Data
data1: .byte $12
.fill 1, 0
data2: .byte $34
.fill 1, 0
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction print_from_main:
Removing instruction __b1:
Removing instruction print_from___b1:
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
constant byte* const SCREEN = (byte*) 1024
constant byte SIZEOF_UNION_DATA = 2
union Data data1 loadstore mem[2] = { b: $12 }
union Data data2 loadstore mem[2] = { b: $34 }
byte idx
byte idx#13 reg byte x 8.0
byte idx#14 reg byte x 3.25
byte idx#3 reg byte x 16.5
byte idx#4 reg byte x 16.5
void main()
void print(union Data print::data)
byte~ print::$0 reg byte a 22.0
union Data print::data loadstore zp[2]:2
reg byte x [ idx#13 idx#14 ]
reg byte a [ print::$0 ]
reg byte x [ idx#3 ]
reg byte x [ idx#4 ]
mem[2] [ data1 ]
mem[2] [ data2 ]
zp[2]:2 [ print::data ]
FINAL ASSEMBLER
Score: 87
// File Comments
// Minimal union with C-Standard behavior - union parameter
// Upstart
// Commodore 64 PRG executable file
.file [name="union-7.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
// Global Constants & labels
.const SIZEOF_UNION_DATA = 2
.label SCREEN = $400
.segment Code
// main
main: {
// print(data1)
// [0] *(&print::data) = memcpy(*(&data1), union Data, SIZEOF_UNION_DATA) -- _deref_pssc1=_deref_pssc2_memcpy_vbuc3
ldy #SIZEOF_UNION_DATA
!:
lda data1-1,y
sta print.data-1,y
dey
bne !-
// [1] call print
// [5] phi from main to print [phi:main->print]
// [5] phi idx#13 = 0 [phi:main->print#0] -- vbuxx=vbuc1
ldx #0
jsr print
// main::@1
// print(data2)
// [2] *(&print::data) = memcpy(*(&data2), union Data, SIZEOF_UNION_DATA) -- _deref_pssc1=_deref_pssc2_memcpy_vbuc3
ldy #SIZEOF_UNION_DATA
!:
lda data2-1,y
sta print.data-1,y
dey
bne !-
// [3] call print
// [5] phi from main::@1 to print [phi:main::@1->print]
// [5] phi idx#13 = idx#14 [phi:main::@1->print#0] -- register_copy
jsr print
// main::@return
// }
// [4] return
rts
}
// print
// print(union Data zp(2) data)
print: {
.label data = 2
// BYTE1(data.w)
// [6] print::$0 = byte1 *((word*)&print::data) -- vbuaa=_byte1__deref_pwuc1
lda.z data+1
// SCREEN[idx++] = BYTE1(data.w)
// [7] SCREEN[idx#13] = print::$0 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// SCREEN[idx++] = BYTE1(data.w);
// [8] idx#3 = ++ idx#13 -- vbuxx=_inc_vbuxx
inx
// SCREEN[idx++] = data.b
// [9] SCREEN[idx#3] = *((byte*)&print::data) -- pbuc1_derefidx_vbuxx=_deref_pbuc2
lda.z data
sta SCREEN,x
// SCREEN[idx++] = data.b;
// [10] idx#4 = ++ idx#3 -- vbuxx=_inc_vbuxx
inx
// SCREEN[idx++] = ' '
// [11] SCREEN[idx#4] = ' ' -- pbuc1_derefidx_vbuxx=vbuc2
lda #' '
sta SCREEN,x
// SCREEN[idx++] = ' ';
// [12] idx#14 = ++ idx#4 -- vbuxx=_inc_vbuxx
inx
// print::@return
// }
// [13] return
rts
}
// File Data
.segment Data
data1: .byte $12
.fill 1, 0
data2: .byte $34
.fill 1, 0