1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-26 12:49:21 +00:00

Fixed cast of reference to constant string. Closes #298

This commit is contained in:
jespergravgaard 2021-08-02 11:37:45 +02:00
parent 41c257a9df
commit 77b7785470
6 changed files with 267 additions and 3 deletions

View File

@ -1,9 +1,11 @@
package dk.camelot64.kickc.model.values;
import dk.camelot64.kickc.model.ConstantNotLiteral;
import dk.camelot64.kickc.model.InternalError;
import dk.camelot64.kickc.model.symbols.ProgramScope;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypePointer;
/** A reference to a named Constant (in the symbol table) */
public class ConstantRef extends SymbolVariableRef implements ConstantValue {
@ -23,6 +25,10 @@ public class ConstantRef extends SymbolVariableRef implements ConstantValue {
@Override
public ConstantLiteral calculateLiteral(ProgramScope scope) {
Variable constantVar = scope.getConstant(this);
if(constantVar.getType() instanceof SymbolTypePointer)
if(((SymbolTypePointer) constantVar.getType()).getArraySpec()!=null)
// Constant array values are not literal
throw new ConstantNotLiteral("not literal");
ConstantValue constantVarValue = constantVar.getInitValue();
return constantVarValue.calculateLiteral(scope);
}

View File

@ -3081,13 +3081,10 @@ public class TestProgramsFast extends TestPrograms {
compileAndCompare("literal-word-pointer-0.c");
}
// TODO: Fix casting literal char* to word https://gitlab.com/camelot/kickc/issues/298
/*
@Test
public void testLiteralWordPointer1() throws IOException {
compileAndCompare("literal-word-pointer-1.c");
}
*/
@Test
public void testLiteralStrings() throws IOException {

View File

@ -0,0 +1,29 @@
// Tests assigning a literal word pointer
// Commodore 64 PRG executable file
.file [name="literal-word-pointer-1.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)
.segment Code
main: {
// print("qwe")
jsr print
// }
rts
.segment Data
str: .text "qwe"
.byte 0
}
.segment Code
print: {
// *(word*)0x80 = (word)str
lda #<main.str
sta $80
lda #>main.str
sta $80+1
// }
rts
}

View File

@ -0,0 +1,17 @@
void main()
main: scope:[main] from
[0] phi()
[1] call print
to:main::@return
main::@return: scope:[main] from main
[2] return
to:@return
void print(byte* print::str)
print: scope:[print] from main
[3] *((word*) 128) = (word)main::str
to:print::@return
print::@return: scope:[print] from print
[4] return
to:@return

View File

@ -0,0 +1,210 @@
CONTROL FLOW GRAPH SSA
void main()
main: scope:[main] from __start
print::str#0 = main::str
call print
to:main::@1
main::@1: scope:[main] from main
to:main::@return
main::@return: scope:[main] from main::@1
return
to:@return
void print(byte* print::str)
print: scope:[print] from main
print::str#1 = phi( main/print::str#0 )
*((word*)$80) = (word)print::str#1
to:print::@return
print::@return: scope:[print] from print
return
to:@return
void __start()
__start: scope:[__start] from
call main
to:__start::@1
__start::@1: scope:[__start] from __start
to:__start::@return
__start::@return: scope:[__start] from __start::@1
return
to:@return
SYMBOL TABLE SSA
void __start()
void main()
constant byte* main::str[4] = "qwe"
void print(byte* print::str)
byte* print::str
byte* print::str#0
byte* print::str#1
Simplifying constant pointer cast (word*) 128
Successful SSA optimization PassNCastSimplification
Identical Phi Values print::str#1 print::str#0
Successful SSA optimization Pass2IdenticalPhiElimination
Constant print::str#0 = main::str
Successful SSA optimization Pass2ConstantIdentification
Constant value identified (word)print::str#0 in [4] *((word*) 128) = (word)print::str#0
Successful SSA optimization Pass2ConstantValues
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::@1
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Constant inlined print::str#0 = main::str
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@1
CALL GRAPH
Calls in [main] to print:1
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block label main::@1
Adding NOP phi() at start of main
FINAL CONTROL FLOW GRAPH
void main()
main: scope:[main] from
[0] phi()
[1] call print
to:main::@return
main::@return: scope:[main] from main
[2] return
to:@return
void print(byte* print::str)
print: scope:[print] from main
[3] *((word*) 128) = (word)main::str
to:print::@return
print::@return: scope:[print] from print
[4] return
to:@return
VARIABLE REGISTER WEIGHTS
void main()
void print(byte* print::str)
byte* print::str
Initial phi equivalence classes
Complete equivalence classes
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [3] *((word*) 128) = (word)main::str [ ] ( print:1 [ ] { } ) always clobbers reg byte a
REGISTER UPLIFT SCOPES
Uplift Scope [main]
Uplift Scope [print]
Uplift Scope []
Uplifting [main] best 36 combination
Uplifting [print] best 36 combination
Uplifting [] best 36 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Tests assigning a literal word pointer
// Upstart
// Commodore 64 PRG executable file
.file [name="literal-word-pointer-1.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
.segment Code
// main
main: {
// [1] call print
jsr print
jmp __breturn
// main::@return
__breturn:
// [2] return
rts
.segment Data
str: .text "qwe"
.byte 0
}
.segment Code
// print
print: {
// [3] *((word*) 128) = (word)main::str -- _deref_pwuc1=vwuc2
lda #<main.str
sta $80
lda #>main.str
sta $80+1
jmp __breturn
// print::@return
__breturn:
// [4] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
void main()
constant byte* main::str[4] = "qwe"
void print(byte* print::str)
byte* print::str
FINAL ASSEMBLER
Score: 30
// File Comments
// Tests assigning a literal word pointer
// Upstart
// Commodore 64 PRG executable file
.file [name="literal-word-pointer-1.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
.segment Code
// main
main: {
// print("qwe")
// [1] call print
jsr print
// main::@return
// }
// [2] return
rts
.segment Data
str: .text "qwe"
.byte 0
}
.segment Code
// print
print: {
// *(word*)0x80 = (word)str
// [3] *((word*) 128) = (word)main::str -- _deref_pwuc1=vwuc2
lda #<main.str
sta $80
lda #>main.str
sta $80+1
// print::@return
// }
// [4] return
rts
}
// File Data

View File

@ -0,0 +1,5 @@
void main()
constant byte* main::str[4] = "qwe"
void print(byte* print::str)
byte* print::str