From 4ab7fe059648625bc8c54d435487605c3f005409 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Mon, 18 Nov 2019 22:56:18 +0100 Subject: [PATCH] Moving array properties from type to variable. Fixed the last few tests. --- .../kickc/passes/Pass3AssertArrayLengths.java | 72 +++++++++---------- src/test/ref/struct-ptr-23.log | 1 + src/test/ref/struct-ptr-33.log | 1 + 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass3AssertArrayLengths.java b/src/main/java/dk/camelot64/kickc/passes/Pass3AssertArrayLengths.java index b8b43884d..b3bcb2f35 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass3AssertArrayLengths.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass3AssertArrayLengths.java @@ -4,7 +4,6 @@ import dk.camelot64.kickc.model.CompileError; import dk.camelot64.kickc.model.Program; import dk.camelot64.kickc.model.symbols.Variable; import dk.camelot64.kickc.model.types.SymbolType; -import dk.camelot64.kickc.model.types.SymbolTypeArray; import dk.camelot64.kickc.model.values.*; import java.util.Collection; @@ -23,50 +22,45 @@ public class Pass3AssertArrayLengths extends Pass2SsaAssertion { Collection allConstants = getScope().getAllConstants(true); for(Variable constantVar : allConstants) { SymbolType constantType = constantVar.getType(); - if(constantType instanceof SymbolTypeArray) { - RValue declaredSize = ((SymbolTypeArray) constantType).getSize(); - if(declaredSize != null) { - if(!(declaredSize instanceof ConstantValue)) { - throw new CompileError("Error! Array declared size is not constant " + constantType.toString()); - } - ConstantLiteral declaredSizeVal = ((ConstantValue) declaredSize).calculateLiteral(getScope()); - if(!(declaredSizeVal instanceof ConstantInteger)) { + if(constantVar.isArray() && constantVar.getArraySize() != null) { + ConstantValue declaredSize = constantVar.getArraySize(); + ConstantLiteral declaredSizeVal = declaredSize.calculateLiteral(getScope()); + if(!(declaredSizeVal instanceof ConstantInteger)) { + throw new CompileError("Error! Array declared size is not integer " + constantType.toString()); + } + Integer declaredSizeInt = ((ConstantInteger) declaredSizeVal).getInteger().intValue(); + // A constant size was found - Check that a value with the same size is present + ConstantValue constantValue = constantVar.getConstantValue(); + if(constantValue == null) { + throw new CompileError("Error! Array with a size not initialized " + constantVar.toString(getProgram())); + } else if(constantValue instanceof ConstantArrayFilled) { + ConstantValue assignedSize = ((ConstantArrayFilled) constantValue).getSize(); + ConstantLiteral assignedSizeVal = assignedSize.calculateLiteral(getScope()); + if(!(assignedSizeVal instanceof ConstantInteger)) { throw new CompileError("Error! Array declared size is not integer " + constantType.toString()); } - Integer declaredSizeInt = ((ConstantInteger) declaredSizeVal).getInteger().intValue(); - // A constant size was found - Check that a value with the same size is present - ConstantValue constantValue = constantVar.getConstantValue(); - if(constantValue == null) { - throw new CompileError("Error! Array with a size not initialized " + constantVar.toString(getProgram())); - } else if(constantValue instanceof ConstantArrayFilled) { - ConstantValue assignedSize = ((ConstantArrayFilled) constantValue).getSize(); - ConstantLiteral assignedSizeVal = assignedSize.calculateLiteral(getScope()); - if(!(assignedSizeVal instanceof ConstantInteger)) { - throw new CompileError("Error! Array declared size is not integer " + constantType.toString()); - } - Integer assignedSizeInt = ((ConstantInteger) declaredSizeVal).getInteger().intValue(); - if(!assignedSizeInt.equals(declaredSizeInt)) { - throw new CompileError("Error! Array length mismatch " + constantVar.toString(getProgram())); - } - } else if(constantValue instanceof ConstantArrayList) { - Integer assignedSizeVal = ((ConstantArrayList) constantValue).getElements().size(); + Integer assignedSizeInt = ((ConstantInteger) declaredSizeVal).getInteger().intValue(); + if(!assignedSizeInt.equals(declaredSizeInt)) { + throw new CompileError("Error! Array length mismatch " + constantVar.toString(getProgram())); + } + } else if(constantValue instanceof ConstantArrayList) { + Integer assignedSizeVal = ((ConstantArrayList) constantValue).getElements().size(); + if(assignedSizeVal > declaredSizeInt) { + throw new CompileError("Error! Array length mismatch " + constantVar.toString(getProgram())); + } + } else if(constantValue instanceof ConstantArrayKickAsm) { + // KickAsm array initializer is assumed good! + } else { + ConstantLiteral constantLiteral = constantValue.calculateLiteral(getScope()); + if(constantLiteral instanceof ConstantString) { + Integer assignedSizeVal = ((ConstantString) constantLiteral).getStringLength(); if(assignedSizeVal > declaredSizeInt) { throw new CompileError("Error! Array length mismatch " + constantVar.toString(getProgram())); } - } else if(constantValue instanceof ConstantArrayKickAsm) { - // KickAsm array initializer is assumed good! + } else if(constantLiteral instanceof ConstantPointer) { + // Constant Pointers are OK for sized arrays } else { - ConstantLiteral constantLiteral = constantValue.calculateLiteral(getScope()); - if(constantLiteral instanceof ConstantString) { - Integer assignedSizeVal = ((ConstantString) constantLiteral).getStringLength(); - if(assignedSizeVal > declaredSizeInt) { - throw new CompileError("Error! Array length mismatch " + constantVar.toString(getProgram())); - } - } else if(constantLiteral instanceof ConstantPointer) { - // Constant Pointers are OK for sized arrays - } else { - throw new AssertionFailed("Error! Array with a size unknown initialization value " + constantVar.toString(getProgram())); - } + throw new AssertionFailed("Error! Array with a size unknown initialization value " + constantVar.toString(getProgram())); } } } diff --git a/src/test/ref/struct-ptr-23.log b/src/test/ref/struct-ptr-23.log index a3483de1d..d20a4775f 100644 --- a/src/test/ref/struct-ptr-23.log +++ b/src/test/ref/struct-ptr-23.log @@ -1,5 +1,6 @@ Fixing struct type size struct Person to 5 Fixing struct type size struct Person to 5 +Fixing struct type size struct Person to 5 Fixing pointer increment (struct Person*) main::person ← ++ (struct Person*) main::person Rewriting struct pointer member access *((struct Person*) print_person::person).id Rewriting struct pointer member access *((struct Person*) print_person::person).initials diff --git a/src/test/ref/struct-ptr-33.log b/src/test/ref/struct-ptr-33.log index 7b0297c9f..40aa75b66 100644 --- a/src/test/ref/struct-ptr-33.log +++ b/src/test/ref/struct-ptr-33.log @@ -1,4 +1,5 @@ Fixing struct type size struct Person to 16 +Fixing struct type size struct Person to 16 Fixing pointer increment (struct Person*) main::person ← ++ (struct Person*) main::person Rewriting struct pointer member access *((struct Person*) main::person).name Rewriting struct pointer member access *((struct Person*) main::person).name