diff --git a/src/main/java/dk/camelot64/kickc/KickC.java b/src/main/java/dk/camelot64/kickc/KickC.java index 0e208ebd4..f3f739d7e 100644 --- a/src/main/java/dk/camelot64/kickc/KickC.java +++ b/src/main/java/dk/camelot64/kickc/KickC.java @@ -352,6 +352,7 @@ public class KickC implements Callable { cFiles.stream().forEach(path -> CFileNames.append(path.toString()).append(" ")); Map effectiveDefines = new LinkedHashMap<>(); + effectiveDefines.put("__KICKC__", "1"); if(defines != null) effectiveDefines.putAll(defines); if(program.getTargetPlatform().getDefines() != null) diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 5bb48956b..02319bb0c 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -24,6 +24,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -664,6 +666,11 @@ public class TestPrograms { compileAndCompare("cstyle-decl-function.c"); } + @Test + public void testPreprocessor14() throws IOException, URISyntaxException { + compileAndCompare("preprocessor-14.c"); + } + @Test public void testPreprocessor13() throws IOException, URISyntaxException { compileAndCompare("preprocessor-13.c"); @@ -4759,7 +4766,11 @@ public class TestPrograms { final TargetPlatform targetPlatform = CTargetPlatformParser.parseTargetPlatformFile(TargetPlatform.DEFAULT_NAME, platformFile, filePath, program.getTargetPlatformPaths()); program.setTargetPlatform(targetPlatform); program.addReservedZps(program.getTargetPlatform().getReservedZps()); - compiler.compile(files, program.getTargetPlatform().getDefines()); + + final Map defines = new HashMap<>(); + defines.put("__KICKC__", "1"); + defines.putAll(program.getTargetPlatform().getDefines()); + compiler.compile(files, defines); compileAsm(fileName, program); boolean success = true; ReferenceHelper helper = new ReferenceHelperFolder(refPath); diff --git a/src/test/kc/preprocessor-14.c b/src/test/kc/preprocessor-14.c new file mode 100644 index 000000000..3b52fb8ee --- /dev/null +++ b/src/test/kc/preprocessor-14.c @@ -0,0 +1,9 @@ +// Test the preprocessor +// Test for existence of the __KICKC__ define + +void main() { + char * const SCREEN = 0x0400; + #ifdef __KICKC__ + *SCREEN = 1; + #endif +} \ No newline at end of file diff --git a/src/test/ref/preprocessor-14.asm b/src/test/ref/preprocessor-14.asm new file mode 100644 index 000000000..4347daf88 --- /dev/null +++ b/src/test/ref/preprocessor-14.asm @@ -0,0 +1,13 @@ +// Test the preprocessor +// Test for existence of the __KICKC__ define +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" +main: { + .label SCREEN = $400 + // *SCREEN = 1 + lda #1 + sta SCREEN + // } + rts +} diff --git a/src/test/ref/preprocessor-14.cfg b/src/test/ref/preprocessor-14.cfg new file mode 100644 index 000000000..1240a5adf --- /dev/null +++ b/src/test/ref/preprocessor-14.cfg @@ -0,0 +1,8 @@ + +(void()) main() +main: scope:[main] from + [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 + to:main::@return +main::@return: scope:[main] from main + [1] return + to:@return diff --git a/src/test/ref/preprocessor-14.log b/src/test/ref/preprocessor-14.log new file mode 100644 index 000000000..fed65d2e8 --- /dev/null +++ b/src/test/ref/preprocessor-14.log @@ -0,0 +1,160 @@ + +CONTROL FLOW GRAPH SSA + +(void()) main() +main: scope:[main] from __start + *((const nomodify byte*) main::SCREEN) ← (number) 1 + to:main::@return +main::@return: scope:[main] from main + 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() +(label) __start::@1 +(label) __start::@return +(void()) main() +(label) main::@return +(const nomodify byte*) main::SCREEN = (byte*)(number) $400 + +Adding number conversion cast (unumber) 1 in *((const nomodify byte*) main::SCREEN) ← (number) 1 +Successful SSA optimization PassNAddNumberTypeConversions +Inlining cast *((const nomodify byte*) main::SCREEN) ← (unumber)(number) 1 +Successful SSA optimization Pass2InlineCast +Simplifying constant pointer cast (byte*) 1024 +Simplifying constant integer cast 1 +Successful SSA optimization PassNCastSimplification +Finalized unsigned number type (byte) 1 +Successful SSA optimization PassNFinalizeNumberTypeConversions +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 +CALL GRAPH + +Created 0 initial phi equivalence classes +Coalesced down to 0 phi equivalence classes + +FINAL CONTROL FLOW GRAPH + +(void()) main() +main: scope:[main] from + [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 + to:main::@return +main::@return: scope:[main] from main + [1] return + to:@return + + +VARIABLE REGISTER WEIGHTS +(void()) main() + +Initial phi equivalence classes +Complete equivalence classes + +INITIAL ASM +Target platform is c64basic / MOS6502X + // File Comments +// Test the preprocessor +// Test for existence of the __KICKC__ define + // Upstart +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" + // Global Constants & labels + // main +main: { + .label SCREEN = $400 + // [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 -- _deref_pbuc1=vbuc2 + lda #1 + sta SCREEN + jmp __breturn + // main::@return + __breturn: + // [1] return + rts +} + // File Data + +REGISTER UPLIFT POTENTIAL REGISTERS +Statement [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 [ ] ( [ ] { } ) always clobbers reg byte a + +REGISTER UPLIFT SCOPES +Uplift Scope [main] +Uplift Scope [] + +Uplifting [main] best 15 combination +Uplifting [] best 15 combination + +ASSEMBLER BEFORE OPTIMIZATION + // File Comments +// Test the preprocessor +// Test for existence of the __KICKC__ define + // Upstart +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" + // Global Constants & labels + // main +main: { + .label SCREEN = $400 + // [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 -- _deref_pbuc1=vbuc2 + lda #1 + sta SCREEN + jmp __breturn + // main::@return + __breturn: + // [1] return + rts +} + // File Data + +ASSEMBLER OPTIMIZATIONS +Removing instruction jmp __breturn +Succesful ASM optimization Pass5NextJumpElimination +Removing instruction __breturn: +Succesful ASM optimization Pass5UnusedLabelElimination + +FINAL SYMBOL TABLE +(void()) main() +(label) main::@return +(const nomodify byte*) main::SCREEN = (byte*) 1024 + + + +FINAL ASSEMBLER +Score: 12 + + // File Comments +// Test the preprocessor +// Test for existence of the __KICKC__ define + // Upstart +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" + // Global Constants & labels + // main +main: { + .label SCREEN = $400 + // *SCREEN = 1 + // [0] *((const nomodify byte*) main::SCREEN) ← (byte) 1 -- _deref_pbuc1=vbuc2 + lda #1 + sta SCREEN + // main::@return + // } + // [1] return + rts +} + // File Data + diff --git a/src/test/ref/preprocessor-14.sym b/src/test/ref/preprocessor-14.sym new file mode 100644 index 000000000..dba588d50 --- /dev/null +++ b/src/test/ref/preprocessor-14.sym @@ -0,0 +1,4 @@ +(void()) main() +(label) main::@return +(const nomodify byte*) main::SCREEN = (byte*) 1024 +