1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-27 06:34:19 +00:00

Added support for const/volatile directives inside & outside structs. Closes #377

This commit is contained in:
Jesper Gravgaard 2020-03-30 13:48:55 +02:00
parent 607f740f8b
commit 5c49f2356e
6 changed files with 324 additions and 0 deletions

View File

@ -1263,6 +1263,12 @@ public class TestPrograms {
assertError("struct-err-0", "Unknown struct type");
}
@Test
public void testStructDirectives() throws IOException, URISyntaxException {
compileAndCompare("struct-directives");
}
@Test
public void testStruct42() throws IOException, URISyntaxException {
compileAndCompare("struct-42");

View File

@ -0,0 +1,10 @@
// Test that struct variable and members can both have type directives
volatile struct { const char x; char z;} y = { 1, 2 };
char* const SCREEN = 0x0400;
void main() {
SCREEN[0] = y.x;
SCREEN[0] = y.z;
}

View File

@ -0,0 +1,16 @@
// Test that struct variable and members can both have type directives
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
main: {
// SCREEN[0] = y.x
lda y
sta SCREEN
// SCREEN[0] = y.z
lda y+1
sta SCREEN
// }
rts
}
y: .byte 1, 2

View File

@ -0,0 +1,18 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
(void()) main()
main: scope:[main] from @1
[4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y)
[5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1)
to:main::@return
main::@return: scope:[main] from main
[6] return
to:@return

View File

@ -0,0 +1,263 @@
Replacing struct member reference (volatile struct $0) y.x with member unwinding reference *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_X)
Replacing struct member reference (volatile struct $0) y.z with member unwinding reference *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_Z)
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@1
(void()) main()
main: scope:[main] from @1
*((const nomodify byte*) SCREEN + (number) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_X)
*((const nomodify byte*) SCREEN + (number) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_Z)
to:main::@return
main::@return: scope:[main] from main
return
to:@return
@1: scope:[] from @begin
call main
to:@2
@2: scope:[] from @1
to:@end
@end: scope:[] from @2
SYMBOL TABLE SSA
(nomodify byte) $0::x
(byte) $0::z
(label) @1
(label) @2
(label) @begin
(label) @end
(const byte) OFFSET_STRUCT_$0_X = (byte) 0
(const byte) OFFSET_STRUCT_$0_Z = (byte) 1
(const nomodify byte*) SCREEN = (byte*)(number) $400
(void()) main()
(label) main::@return
(volatile struct $0) y loadstore = { x: (byte) 1, z: (byte) 2 }
Adding number conversion cast (unumber) 0 in *((const nomodify byte*) SCREEN + (number) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_X)
Adding number conversion cast (unumber) 0 in *((const nomodify byte*) SCREEN + (number) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_Z)
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 0
Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Simplifying expression containing zero (byte*)&y in [0] *((const nomodify byte*) SCREEN + (byte) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_X)
Simplifying expression containing zero SCREEN in [0] *((const nomodify byte*) SCREEN + (byte) 0) ← *((byte*)&(volatile struct $0) y)
Simplifying expression containing zero SCREEN in [1] *((const nomodify byte*) SCREEN + (byte) 0) ← *((byte*)&(volatile struct $0) y+(const byte) OFFSET_STRUCT_$0_Z)
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused constant (const byte) OFFSET_STRUCT_$0_X
Successful SSA optimization PassNEliminateUnusedVars
Constant inlined OFFSET_STRUCT_$0_Z = (byte) 1
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @2
Adding NOP phi() at start of @end
CALL GRAPH
Calls in [] to main:2
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block (label) @2
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
FINAL CONTROL FLOW GRAPH
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
(void()) main()
main: scope:[main] from @1
[4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y)
[5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1)
to:main::@return
main::@return: scope:[main] from main
[6] return
to:@return
VARIABLE REGISTER WEIGHTS
(nomodify byte) $0::x
(byte) $0::z
(void()) main()
(volatile struct $0) y loadstore = { x: (byte) 1, z: (byte) 2 }
Initial phi equivalence classes
Added variable y to live range equivalence class [ y ]
Complete equivalence classes
[ y ]
Allocated mem[2] [ y ]
INITIAL ASM
Target platform is c64basic / MOS6502X
// File Comments
// Test that struct variable and members can both have type directives
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
main: {
// [4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y) -- _deref_pbuc1=_deref_pbuc2
lda y
sta SCREEN
// [5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1) -- _deref_pbuc1=_deref_pbuc2
lda y+1
sta SCREEN
jmp __breturn
// main::@return
__breturn:
// [6] return
rts
}
// File Data
y: .byte 1, 2
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y) [ y ] ( main:2 [ y ] { } ) always clobbers reg byte a
Statement [5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1) [ ] ( main:2 [ ] { } ) always clobbers reg byte a
Potential registers mem[2] [ y ] : mem[2] ,
REGISTER UPLIFT SCOPES
Uplift Scope [$0]
Uplift Scope [main]
Uplift Scope [] 0: mem[2] [ y ]
Uplifting [$0] best 37 combination
Uplifting [main] best 37 combination
Uplifting [] best 37 combination mem[2] [ y ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test that struct variable and members can both have type directives
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
main: {
// [4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y) -- _deref_pbuc1=_deref_pbuc2
lda y
sta SCREEN
// [5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1) -- _deref_pbuc1=_deref_pbuc2
lda y+1
sta SCREEN
jmp __breturn
// main::@return
__breturn:
// [6] return
rts
}
// File Data
y: .byte 1, 2
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1_from___bbegin:
Removing instruction __b1:
Removing instruction __bend_from___b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction __bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(nomodify byte) $0::x
(byte) $0::z
(label) @1
(label) @begin
(label) @end
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) main()
(label) main::@return
(volatile struct $0) y loadstore mem[2] = { x: (byte) 1, z: (byte) 2 }
mem[2] [ y ]
FINAL ASSEMBLER
Score: 22
// File Comments
// Test that struct variable and members can both have type directives
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
// [1] phi from @begin to @1 [phi:@begin->@1]
// @1
// [2] call main
// [3] phi from @1 to @end [phi:@1->@end]
// @end
// main
main: {
// SCREEN[0] = y.x
// [4] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y) -- _deref_pbuc1=_deref_pbuc2
lda y
sta SCREEN
// SCREEN[0] = y.z
// [5] *((const nomodify byte*) SCREEN) ← *((byte*)&(volatile struct $0) y+(byte) 1) -- _deref_pbuc1=_deref_pbuc2
lda y+1
sta SCREEN
// main::@return
// }
// [6] return
rts
}
// File Data
y: .byte 1, 2

View File

@ -0,0 +1,11 @@
(nomodify byte) $0::x
(byte) $0::z
(label) @1
(label) @begin
(label) @end
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) main()
(label) main::@return
(volatile struct $0) y loadstore mem[2] = { x: (byte) 1, z: (byte) 2 }
mem[2] [ y ]