1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-01 13:30:50 +00:00

Added support for address-of on struct value members. Closes #429

This commit is contained in:
jespergravgaard 2020-04-30 23:42:45 +02:00
parent 30b7e32902
commit 4ffba62276
7 changed files with 423 additions and 20 deletions

View File

@ -2318,26 +2318,13 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
String op = ((TerminalNode) ctx.getChild(0)).getSymbol().getText();
Operator operator = Operators.getUnary(op);
// Special handling of negative literal number
if(child instanceof ConstantInteger && operator.equals(Operators.NEG)) {
if(operator.equals(Operators.ADDRESS_OF)) {
ConstantValue constantAddressOf = constantifyAddressOf(child, new StatementSource(ctx));
if(constantAddressOf != null)
return constantAddressOf;
}
if(operator.equals(Operators.NEG) && child instanceof ConstantInteger) {
return new ConstantInteger(-((ConstantInteger) child).getInteger(), ((ConstantInteger) child).getType());
} else if(operator.equals(Operators.ADDRESS_OF) && child instanceof SymbolRef) {
return new ConstantSymbolPointer((SymbolRef) child);
} else if(operator.equals(Operators.ADDRESS_OF) && child instanceof PointerDereferenceIndexed && ((PointerDereferenceIndexed) child).getPointer() instanceof ConstantValue && ((PointerDereferenceIndexed) child).getIndex() instanceof ConstantValue) {
PointerDereferenceIndexed pointerDeref = (PointerDereferenceIndexed) child;
return new ConstantBinary((ConstantValue) pointerDeref.getPointer(), Operators.PLUS, (ConstantValue) pointerDeref.getIndex());
} else if(operator.equals(Operators.ADDRESS_OF) && child instanceof StructMemberRef && ((StructMemberRef) child).getStruct() instanceof PointerDereferenceSimple && ((PointerDereferenceSimple) ((StructMemberRef) child).getStruct()).getPointer() instanceof ConstantValue) {
final ConstantValue structPointer = (ConstantValue) ((PointerDereferenceSimple) ((StructMemberRef) child).getStruct()).getPointer();
final RValue structValue = ((StructMemberRef) child).getStruct();
final SymbolTypeStruct structType = (SymbolTypeStruct) SymbolTypeInference.inferType(program.getScope(), structValue);
StructDefinition structDefinition = structType.getStructDefinition(program.getScope());
final String memberName = ((StructMemberRef) child).getMemberName();
final Variable member = structDefinition.getMember(memberName);
if(member==null) {
throw new CompileError("Unknown struct member "+memberName+" in struct "+structType.getTypeName(), new StatementSource(ctx));
}
final ConstantRef memberOffset = SizeOfConstants.getStructMemberOffsetConstant(program.getScope(), structDefinition, memberName);
final SymbolType memberType = member.getType();
return new ConstantCastValue(new SymbolTypePointer(memberType), new ConstantBinary(new ConstantCastValue(new SymbolTypePointer(SymbolType.BYTE), structPointer), Operators.PLUS, memberOffset));
} else if(child instanceof ConstantValue) {
return new ConstantUnary((OperatorUnary) operator, (ConstantValue) child);
} else {
@ -2350,6 +2337,44 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
}
}
/**
* Try to constantify an RValue that is affected by the address-of operator.
* @param child The sub expression of the address-of operator
* @param source The statement source (used for errors)
* @return The constant value of the pointer, if it can be constantified. Null otherwise.
*/
private ConstantValue constantifyAddressOf(RValue child, StatementSource source) {
if(child instanceof SymbolRef) {
return new ConstantSymbolPointer((SymbolRef) child);
} else if(child instanceof PointerDereferenceIndexed && ((PointerDereferenceIndexed) child).getPointer() instanceof ConstantValue && ((PointerDereferenceIndexed) child).getIndex() instanceof ConstantValue) {
PointerDereferenceIndexed pointerDeref = (PointerDereferenceIndexed) child;
return new ConstantBinary((ConstantValue) pointerDeref.getPointer(), Operators.PLUS, (ConstantValue) pointerDeref.getIndex());
} else if(child instanceof StructMemberRef && ((StructMemberRef) child).getStruct() instanceof PointerDereferenceSimple && ((PointerDereferenceSimple) ((StructMemberRef) child).getStruct()).getPointer() instanceof ConstantValue) {
final StructMemberRef structMemberRef = (StructMemberRef) child;
final ConstantValue structPointer = (ConstantValue) ((PointerDereferenceSimple) structMemberRef.getStruct()).getPointer();
final SymbolTypeStruct structType = (SymbolTypeStruct) SymbolTypeInference.inferType(program.getScope(), structMemberRef.getStruct());
StructDefinition structDefinition = structType.getStructDefinition(program.getScope());
final Variable member = structDefinition.getMember(structMemberRef.getMemberName());
if(member == null) {
throw new CompileError("Unknown struct member " + structMemberRef.getMemberName() + " in struct " + structType.getTypeName(), source);
}
final ConstantRef memberOffset = SizeOfConstants.getStructMemberOffsetConstant(program.getScope(), structDefinition, structMemberRef.getMemberName());
return new ConstantCastValue(new SymbolTypePointer(member.getType()), new ConstantBinary(new ConstantCastValue(new SymbolTypePointer(SymbolType.BYTE), structPointer), Operators.PLUS, memberOffset));
} else if(child instanceof StructMemberRef && ((StructMemberRef) child).getStruct() instanceof SymbolRef) {
final StructMemberRef structMemberRef = (StructMemberRef) child;
final ConstantValue structPointer = new ConstantSymbolPointer((SymbolRef) structMemberRef.getStruct());
final SymbolTypeStruct structType = (SymbolTypeStruct) SymbolTypeInference.inferType(program.getScope(), structMemberRef.getStruct());
StructDefinition structDefinition = structType.getStructDefinition(program.getScope());
final Variable member = structDefinition.getMember(structMemberRef.getMemberName());
if(member == null) {
throw new CompileError("Unknown struct member " + structMemberRef.getMemberName() + " in struct " + structType.getTypeName(), source);
}
final ConstantRef memberOffset = SizeOfConstants.getStructMemberOffsetConstant(program.getScope(), structDefinition, structMemberRef.getMemberName());
return new ConstantCastValue(new SymbolTypePointer(member.getType()), new ConstantBinary(new ConstantCastValue(new SymbolTypePointer(SymbolType.BYTE), structPointer), Operators.PLUS, memberOffset));
}
return null;
}
@Override
public RValue visitExprTernary(KickCParser.ExprTernaryContext ctx) {
RValue condValue = (RValue) this.visit(ctx.expr(0));

View File

@ -40,9 +40,14 @@ public class TestPrograms {
public TestPrograms() {
}
@Test
public void testStructPointerToMember2() throws IOException, URISyntaxException {
compileAndCompare("struct-pointer-to-member-2.c");
}
@Test
public void testStructPointerToMember() throws IOException, URISyntaxException {
compileAndCompare("struct-pointer-to-member.c", log());
compileAndCompare("struct-pointer-to-member.c");
}
@Test

View File

@ -0,0 +1,23 @@
// Support for pointer to struct member
// Commodore 64 screen colors
struct SCREEN_COLORS {
char BORDER;
char BG0;
char BG1;
char BG2;
char BG3;
};
// Commodore 64 processor port
struct SCREEN_COLORS COLORS;
// The border color
char * const BORDERCOL = &COLORS.BORDER;
// The background color
char * const BGCOL = &COLORS.BG0;
void main() {
*BORDERCOL = 0;
*BGCOL = 6;
}

View File

@ -0,0 +1,22 @@
// Support for pointer to struct member
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.const OFFSET_STRUCT_SCREEN_COLORS_BG0 = 1
.const SIZEOF_STRUCT_SCREEN_COLORS = 5
// The border color
.label BORDERCOL = COLORS
// The background color
.label BGCOL = COLORS+OFFSET_STRUCT_SCREEN_COLORS_BG0
main: {
// *BORDERCOL = 0
lda #0
sta BORDERCOL
// *BGCOL = 6
lda #6
sta BGCOL
// }
rts
}
// Commodore 64 processor port
COLORS: .fill SIZEOF_STRUCT_SCREEN_COLORS, 0

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*) BORDERCOL) ← (byte) 0
[5] *((const nomodify byte*) BGCOL) ← (byte) 6
to:main::@return
main::@return: scope:[main] from main
[6] return
to:@return

View File

@ -0,0 +1,293 @@
Setting struct to load/store in variable affected by address-of (struct SCREEN_COLORS) COLORS
Setting struct to load/store in variable affected by address-of (struct SCREEN_COLORS) COLORS
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@1
(void()) main()
main: scope:[main] from @1
*((const nomodify byte*) BORDERCOL) ← (number) 0
*((const nomodify byte*) BGCOL) ← (number) 6
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
(label) @1
(label) @2
(label) @begin
(label) @end
(const nomodify byte*) BGCOL = (byte*)(byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0
(const nomodify byte*) BORDERCOL = (byte*)(byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BORDER
(struct SCREEN_COLORS) COLORS loadstore = {}
(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0 = (byte) 1
(const byte) OFFSET_STRUCT_SCREEN_COLORS_BORDER = (byte) 0
(byte) SCREEN_COLORS::BG0
(byte) SCREEN_COLORS::BG1
(byte) SCREEN_COLORS::BG2
(byte) SCREEN_COLORS::BG3
(byte) SCREEN_COLORS::BORDER
(void()) main()
(label) main::@return
Adding number conversion cast (unumber) 0 in *((const nomodify byte*) BORDERCOL) ← (number) 0
Adding number conversion cast (unumber) 6 in *((const nomodify byte*) BGCOL) ← (number) 6
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast *((const nomodify byte*) BORDERCOL) ← (unumber)(number) 0
Inlining cast *((const nomodify byte*) BGCOL) ← (unumber)(number) 6
Successful SSA optimization Pass2InlineCast
Simplifying constant integer cast (byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BORDER
Simplifying constant integer cast (byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0
Simplifying constant integer cast 0
Simplifying constant integer cast 6
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 6
Successful SSA optimization PassNFinalizeNumberTypeConversions
Simplifying expression containing zero (byte*)&COLORS in
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused constant (const byte) OFFSET_STRUCT_SCREEN_COLORS_BORDER
Successful SSA optimization PassNEliminateUnusedVars
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*) BORDERCOL) ← (byte) 0
[5] *((const nomodify byte*) BGCOL) ← (byte) 6
to:main::@return
main::@return: scope:[main] from main
[6] return
to:@return
VARIABLE REGISTER WEIGHTS
(struct SCREEN_COLORS) COLORS loadstore = {}
(byte) SCREEN_COLORS::BG0
(byte) SCREEN_COLORS::BG1
(byte) SCREEN_COLORS::BG2
(byte) SCREEN_COLORS::BG3
(byte) SCREEN_COLORS::BORDER
(void()) main()
Initial phi equivalence classes
Added variable COLORS to live range equivalence class [ COLORS ]
Complete equivalence classes
[ COLORS ]
Allocated mem[5] [ COLORS ]
INITIAL ASM
Target platform is c64basic / MOS6502X
// File Comments
// Support for pointer to struct member
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.const OFFSET_STRUCT_SCREEN_COLORS_BG0 = 1
// The border color
.label BORDERCOL = COLORS
// The background color
.label BGCOL = COLORS+OFFSET_STRUCT_SCREEN_COLORS_BG0
// @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*) BORDERCOL) ← (byte) 0 -- _deref_pbuc1=vbuc2
lda #0
sta BORDERCOL
// [5] *((const nomodify byte*) BGCOL) ← (byte) 6 -- _deref_pbuc1=vbuc2
lda #6
sta BGCOL
jmp __breturn
// main::@return
__breturn:
// [6] return
rts
}
// File Data
// Commodore 64 processor port
COLORS: .fill SIZEOF_STRUCT_SCREEN_COLORS, 0
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const nomodify byte*) BORDERCOL) ← (byte) 0 [ ] ( main:2 [ ] { } ) always clobbers reg byte a
Statement [5] *((const nomodify byte*) BGCOL) ← (byte) 6 [ ] ( main:2 [ ] { } ) always clobbers reg byte a
Potential registers mem[5] [ COLORS ] : mem[5] ,
REGISTER UPLIFT SCOPES
Uplift Scope [SCREEN_COLORS]
Uplift Scope [main]
Uplift Scope [] 0: mem[5] [ COLORS ]
Uplifting [SCREEN_COLORS] best 33 combination
Uplifting [main] best 33 combination
Uplifting [] best 33 combination mem[5] [ COLORS ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Support for pointer to struct member
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.const OFFSET_STRUCT_SCREEN_COLORS_BG0 = 1
.const SIZEOF_STRUCT_SCREEN_COLORS = 5
// The border color
.label BORDERCOL = COLORS
// The background color
.label BGCOL = COLORS+OFFSET_STRUCT_SCREEN_COLORS_BG0
// @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*) BORDERCOL) ← (byte) 0 -- _deref_pbuc1=vbuc2
lda #0
sta BORDERCOL
// [5] *((const nomodify byte*) BGCOL) ← (byte) 6 -- _deref_pbuc1=vbuc2
lda #6
sta BGCOL
jmp __breturn
// main::@return
__breturn:
// [6] return
rts
}
// File Data
// Commodore 64 processor port
COLORS: .fill SIZEOF_STRUCT_SCREEN_COLORS, 0
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
(label) @1
(label) @begin
(label) @end
(const nomodify byte*) BGCOL = (byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0
(const nomodify byte*) BORDERCOL = (byte*)&(struct SCREEN_COLORS) COLORS
(struct SCREEN_COLORS) COLORS loadstore mem[5] = {}
(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0 = (byte) 1
(byte) SCREEN_COLORS::BG0
(byte) SCREEN_COLORS::BG1
(byte) SCREEN_COLORS::BG2
(byte) SCREEN_COLORS::BG3
(byte) SCREEN_COLORS::BORDER
(const byte) SIZEOF_STRUCT_SCREEN_COLORS = (byte) 5
(void()) main()
(label) main::@return
mem[5] [ COLORS ]
FINAL ASSEMBLER
Score: 18
// File Comments
// Support for pointer to struct member
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.const OFFSET_STRUCT_SCREEN_COLORS_BG0 = 1
.const SIZEOF_STRUCT_SCREEN_COLORS = 5
// The border color
.label BORDERCOL = COLORS
// The background color
.label BGCOL = COLORS+OFFSET_STRUCT_SCREEN_COLORS_BG0
// @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: {
// *BORDERCOL = 0
// [4] *((const nomodify byte*) BORDERCOL) ← (byte) 0 -- _deref_pbuc1=vbuc2
lda #0
sta BORDERCOL
// *BGCOL = 6
// [5] *((const nomodify byte*) BGCOL) ← (byte) 6 -- _deref_pbuc1=vbuc2
lda #6
sta BGCOL
// main::@return
// }
// [6] return
rts
}
// File Data
// Commodore 64 processor port
COLORS: .fill SIZEOF_STRUCT_SCREEN_COLORS, 0

View File

@ -0,0 +1,17 @@
(label) @1
(label) @begin
(label) @end
(const nomodify byte*) BGCOL = (byte*)&(struct SCREEN_COLORS) COLORS+(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0
(const nomodify byte*) BORDERCOL = (byte*)&(struct SCREEN_COLORS) COLORS
(struct SCREEN_COLORS) COLORS loadstore mem[5] = {}
(const byte) OFFSET_STRUCT_SCREEN_COLORS_BG0 = (byte) 1
(byte) SCREEN_COLORS::BG0
(byte) SCREEN_COLORS::BG1
(byte) SCREEN_COLORS::BG2
(byte) SCREEN_COLORS::BG3
(byte) SCREEN_COLORS::BORDER
(const byte) SIZEOF_STRUCT_SCREEN_COLORS = (byte) 5
(void()) main()
(label) main::@return
mem[5] [ COLORS ]