1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-26 03:32:23 +00:00

Added support for __intrinsic only being present in .c-file while absent in .h-file. Closes #417

This commit is contained in:
jespergravgaard 2020-04-25 15:46:27 +02:00
parent 4c3b000fbd
commit 3b6c14be7a
9 changed files with 280 additions and 10 deletions

View File

@ -319,7 +319,7 @@ public class VariableBuilder {
return Variable.MemoryArea.ZEROPAGE_MEMORY;
else if(hasDirective(Directive.MemMain.class))
return Variable.MemoryArea.MAIN_MEMORY;
Directive.Address addressDirective = findDirective(Directive.Address.class);
Directive.Address addressDirective = findDirective(Directive.Address.class, directives);
if(addressDirective != null)
return (addressDirective.address < 0x100) ? Variable.MemoryArea.ZEROPAGE_MEMORY : Variable.MemoryArea.MAIN_MEMORY;
else if(!isConstant() && isOptimize())
@ -341,7 +341,7 @@ public class VariableBuilder {
* @return The memory alignment
*/
public Integer getAlignment() {
Directive.Align alignDirective = findDirective(Directive.Align.class);
Directive.Align alignDirective = findDirective(Directive.Align.class, directives);
if(alignDirective != null) {
if(isArray()) {
return alignDirective.alignment;
@ -359,7 +359,7 @@ public class VariableBuilder {
* @return Hard-coded register allocation. Null if not hard-coded.
*/
public Registers.Register getRegister() {
Directive.Address addressDirective = findDirective(Directive.Address.class);
Directive.Address addressDirective = findDirective(Directive.Address.class, directives);
if(addressDirective != null) {
Variable.MemoryArea memoryArea = (addressDirective.address < 0x100) ? Variable.MemoryArea.ZEROPAGE_MEMORY : Variable.MemoryArea.MAIN_MEMORY;
if(Variable.MemoryArea.ZEROPAGE_MEMORY.equals(memoryArea)) {
@ -370,7 +370,7 @@ public class VariableBuilder {
}
}
Directive.NamedRegister registerDirective = findDirective(Directive.NamedRegister.class);
Directive.NamedRegister registerDirective = findDirective(Directive.NamedRegister.class, directives);
if(registerDirective != null) {
Registers.Register register = Registers.getRegister(registerDirective.name);
if(register == null) {
@ -391,8 +391,18 @@ public class VariableBuilder {
* @param <DirectiveClass> The class of the type to look for
* @return true if the directive if found. false otherwise.
*/
public <DirectiveClass extends Directive> boolean hasDirective(Class<DirectiveClass> directiveClass) {
return findDirective(directiveClass) != null;
private <DirectiveClass extends Directive> boolean hasDirective(Class<DirectiveClass> directiveClass) {
return findDirective(directiveClass, directives) != null;
}
/**
* Examines whether a specific directive is present in the source
*
* @param directiveClass The class of the type to look for
* @param <DirectiveClass> The class of the type to look for
* @return true if the directive if found. false otherwise.
*/
public static <DirectiveClass extends Directive> boolean hasDirective(Class<DirectiveClass> directiveClass, List<Directive> directives) {
return findDirective(directiveClass, directives) != null;
}
/**
@ -402,7 +412,7 @@ public class VariableBuilder {
* @param <DirectiveClass> The class of the type to look for
* @return The directive if found. null if not found.
*/
private <DirectiveClass extends Directive> DirectiveClass findDirective(Class<DirectiveClass> directiveClass) {
private static <DirectiveClass extends Directive> DirectiveClass findDirective(Class<DirectiveClass> directiveClass, List<Directive> directives) {
if(directives == null) return null;
for(Directive directive : directives) {
if(directiveClass.isInstance(directive)) {

View File

@ -261,12 +261,15 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
program.getScope().add(procedure);
}
if(ctx.declFunctionBody() != null) {
// Make sure directives and more are taken from the procedure with the body!
if(ctx.declFunctionBody() != null || VariableBuilder.hasDirective(Directive.Intrinsic.class, directives)) {
// Make sure directives and more are taken from the procedure with the body / intrinsic declaration!
if(existingSymbol != null) {
program.getScope().remove(existingSymbol);
program.getScope().add(procedure);
}
}
if(ctx.declFunctionBody() != null) {
// Check that the body has not already been added
for(Statement statement : sequence.getStatements())

View File

@ -6,7 +6,7 @@
// Print a formatted string.
// https://en.wikipedia.org/wiki/Printf_format_string
// This implementation supports decimal, octal and hexadecimal radix. It supports min length, left/right justify, zero-padding and always-sign.
__intrinsic void printf(char* format, ...);
void printf(char* format, ...);
// Clear the screen. Also places cursor at the top left.
void printf_cls();

View File

@ -205,6 +205,11 @@ public class TestPrograms {
assertError("cstyle-decl-function-mismatch.c", "Error! Conflicting declarations for: sum");
}
@Test
public void testCStyleDeclFunctionIntrinsic() throws IOException, URISyntaxException {
compileAndCompare("cstyle-decl-function-intrinsic.c");
}
@Test
public void testCStyleDeclFunction() throws IOException, URISyntaxException {
compileAndCompare("cstyle-decl-function.c");

View File

@ -0,0 +1,17 @@
// Test declaration of intrinsic without body
// Declaration of printf-function
void printf(char* format, ...);
// Intrinsic definition of printf-function
__intrinsic void printf(char* format, ...);
// Definition of main()
void main() {
}

View File

@ -0,0 +1,9 @@
// Test declaration of intrinsic without body
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Definition of main()
main: {
// }
rts
}

View File

@ -0,0 +1,17 @@
@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] phi()
to:main::@return
main::@return: scope:[main] from main
[5] return
to:@return

View File

@ -0,0 +1,203 @@
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@1
(void()) main()
main: scope:[main] from @1
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
(void()) main()
(label) main::@return
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
Adding NOP phi() at start of main
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
Adding NOP phi() at start of main
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] phi()
to:main::@return
main::@return: scope:[main] from main
[5] 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 declaration of intrinsic without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
main_from___b1:
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
// Definition of main()
main: {
jmp __breturn
// main::@return
__breturn:
// [5] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
REGISTER UPLIFT SCOPES
Uplift Scope [main]
Uplift Scope []
Uplifting [main] best 48 combination
Uplifting [] best 48 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test declaration of intrinsic without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
main_from___b1:
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
// Definition of main()
main: {
jmp __breturn
// main::@return
__breturn:
// [5] return
rts
}
// File Data
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 main_from___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
(void()) main()
(label) main::@return
FINAL ASSEMBLER
Score: 6
// File Comments
// Test declaration of intrinsic without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
// @begin
// [1] phi from @begin to @1 [phi:@begin->@1]
// @1
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
// [3] phi from @1 to @end [phi:@1->@end]
// @end
// main
// Definition of main()
main: {
// main::@return
// }
// [5] return
rts
}
// File Data

View File

@ -0,0 +1,6 @@
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@return