1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-28 01:29:44 +00:00

Aligned struct variable creation with normal variables.

This commit is contained in:
jespergravgaard 2019-11-23 18:31:00 +01:00
parent 5ab9b4353c
commit 363a6c4424
27 changed files with 68 additions and 76 deletions

View File

@ -396,7 +396,7 @@ public abstract class Scope implements Symbol, Serializable {
}
}
}
if(symVar.isConstant()) {
if(symVar.isConstant() && symVar.getConstantValue()!=null) {
res.append(" = " + symVar.getConstantValue().toString(program));
}
res.append("\n");

View File

@ -24,7 +24,7 @@ public class StructDefinition extends Scope {
* @return The member variable
*/
public Variable getMember(String name) {
for(Variable member : getAllVariables(false)) {
for(Variable member : getAllVars(false)) {
if(member.getLocalName().equals(name)) {
return member;
}
@ -39,7 +39,7 @@ public class StructDefinition extends Scope {
*/
public long getMemberByteOffset(Variable member, ProgramScope programScope) {
long byteOffset=0;
for(Variable structMember : getAllVariables(false)) {
for(Variable structMember : getAllVars(false)) {
if(structMember.equals(member)) {
break;
} else {

View File

@ -102,7 +102,7 @@ public class SymbolTypeInference {
if(structType instanceof SymbolTypeStruct) {
String typeName = ((SymbolTypeStruct) structType).getStructTypeName();
StructDefinition structDefinition = symbols.getStructDefinition(typeName);
Variable structMember = structDefinition.getVariable(structMemberRef.getMemberName());
Variable structMember = structDefinition.getVar(structMemberRef.getMemberName());
return structMember.getType();
} else {
throw new CompileError("Dot applied to non-struct "+ structMemberRef.getStruct().toString());

View File

@ -53,7 +53,7 @@ public class SymbolTypeStruct implements SymbolType {
*/
public int calculateSizeBytes(StructDefinition structDefinition, ProgramScope programScope) {
int sizeBytes = 0;
for(Variable member : structDefinition.getAllVariables(false)) {
for(Variable member : structDefinition.getAllVars(false)) {
SymbolType memberType = member.getType();
int memberSize = getMemberSizeBytes(memberType, member.getArraySpec(), programScope);
sizeBytes += memberSize;

View File

@ -569,28 +569,9 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
try {
// Find kind
Variable.Kind kind = directiveContext.getKind(declVarType, getCurrentScope(), false, declIsArray, declVarDirectives, statementSource);
// Handle struct member vars
if(declVarStructMember) {
if(initializer != null)
throw new CompileError("Initializers not supported inside structs.", statementSource);
else {
// Create struct member variable
Variable memberVar = getCurrentScope().addVariable(kind, varName, declVarType, defaultMemoryArea, currentDataSegment);
if(declIsArray) {
memberVar.setArraySpec(new ArraySpec(declArraySize));
}
// Add directives
directiveContext.applyDirectives(memberVar, false, declIsArray, declVarDirectives, statementSource);
// Add comments to constant
memberVar.setComments(ensureUnusedComments(declVarComments));
return null;
}
}
if(kind.equals(Variable.Kind.CONSTANT)) {
// Create a Constant
ConstantValue initConstantValue = getConstantValue(initializer, declVarType, statementSource);
ConstantValue initConstantValue = getConstantValue(initializer, declVarType, declVarStructMember, statementSource);
ArraySpec arraySpec = null;
if(declIsArray) {
arraySpec = new ArraySpec(declArraySize);
@ -606,19 +587,26 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
Variable lValue = getCurrentScope().addVariable(kind, varName, declVarType, defaultMemoryArea, currentDataSegment);
// Add directives
directiveContext.applyDirectives(lValue, false, declIsArray, declVarDirectives, statementSource);
if(declVarStructMember && initializer != null)
throw new CompileError("Initializer not supported inside structs " + declVarType.getTypeName(), statementSource);
RValue initValue;
if(initializer != null) {
PrePostModifierHandler.addPreModifiers(this, initializer, statementSource);
initValue = (RValue) visit(initializer);
if(declVarStructMember) {
if(initializer != null) {
throw new CompileError("Initializer not supported inside structs " + declVarType.getTypeName(), statementSource);
} else {
// Struct members have no initializers
return null;
}
} else {
initValue = createZeroValue(declVarType, statementSource);
}
Statement initStmt = new StatementAssignment(lValue.getVariableRef(), initValue, statementSource, ensureUnusedComments(declVarComments));
sequence.addStatement(initStmt);
if(initializer != null) {
PrePostModifierHandler.addPostModifiers(this, initializer, statementSource);
RValue initValue;
if(initializer != null) {
PrePostModifierHandler.addPreModifiers(this, initializer, statementSource);
initValue = (RValue) visit(initializer);
} else {
initValue = createZeroValue(declVarType, statementSource);
}
Statement initStmt = new StatementAssignment(lValue.getVariableRef(), initValue, statementSource, ensureUnusedComments(declVarComments));
sequence.addStatement(initStmt);
if(initializer != null) {
PrePostModifierHandler.addPostModifiers(this, initializer, statementSource);
}
}
}
return null;
@ -636,10 +624,22 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
* @return The constant value.
* @throws CompileError if the initializer does not resolve to a constant value
*/
private ConstantValue getConstantValue(KickCParser.ExprContext initializer, SymbolType type, StatementSource statementSource) {
private ConstantValue getConstantValue(KickCParser.ExprContext initializer, SymbolType type, boolean isStructMember, StatementSource statementSource) {
if(initializer != null && PrePostModifierHandler.hasPrePostModifiers(this, initializer, statementSource)) {
throw new CompileError("Constant value contains a pre/post-modifier.", statementSource);
}
/*
if(isStructMember) {
if(initializer != null) {
throw new CompileError("Initializer not supported inside structs " + declVarType.getTypeName(), statementSource);
} else {
// Struct members have no initializers
return null;
}
}
*/
RValue initValue;
if(initializer == null) {
if(declIsArray) {

View File

@ -206,7 +206,7 @@ public class Pass1UnwindStructValues extends Pass1Base {
// Not inside another struct
StructDefinition structDefinition = ((SymbolTypeStruct) variable.getType()).getStructDefinition(getProgram().getScope());
StructUnwinding.VariableUnwinding variableUnwinding = structUnwinding.createVariableUnwinding(variable.getRef());
for(Variable member : structDefinition.getAllVariables(false)) {
for(Variable member : structDefinition.getAllVars(false)) {
String name = variable.getLocalName() + "_" + member.getLocalName();
Variable.MemoryArea memoryArea = (member.getType() instanceof SymbolTypePointer)?Variable.MemoryArea.ZEROPAGE_MEMORY:variable.getMemoryArea();
Variable memberVariable = scope.add(new Variable( false, name, scope, member.getType(), variable.getKind(), memoryArea, variable.getDataSegment()));

View File

@ -56,7 +56,7 @@ public class Pass2ArrayInStructInlining extends Pass2SsaOptimization {
Value value = programValue.get();
if(programValue instanceof ProgramValue.ProgramValueConstantStructMember) {
SymbolVariableRef memberRef = ((ProgramValue.ProgramValueConstantStructMember) programValue).getMemberRef();
Variable structMemberVar = getScope().getVariable(memberRef);
Variable structMemberVar = getScope().getVar(memberRef);
if(structMemberVar.isArray() && structMemberVar.getArraySpec().getArraySize() != null) {
if(value instanceof ConstantValue) {
ConstantValue constantValue = (ConstantValue) value;

View File

@ -98,7 +98,7 @@ public class Pass2ConstantInitializerValueLists extends Pass2SsaOptimization {
// Check that type of constant values match the struct member types
SymbolTypeStruct declaredStructType = (SymbolTypeStruct) declaredType;
StructDefinition structDefinition = declaredStructType.getStructDefinition(program.getScope());
Collection<Variable> memberDefs = structDefinition.getAllVariables(false);
Collection<Variable> memberDefs = structDefinition.getAllVars(false);
if(memberDefs.size()!=constantValues.size()) {
throw new CompileError(
"Struct initializer has wrong size ("+valueList.getList().size()+"), " +

View File

@ -572,7 +572,7 @@ public class Pass4CodeGeneration {
ConstantStructValue structValue = (ConstantStructValue) value;
for(SymbolVariableRef memberRef : structValue.getMembers()) {
ConstantValue memberValue = structValue.getValue(memberRef);
Variable memberVariable = getScope().getVariable(memberRef);
Variable memberVariable = getScope().getVar(memberRef);
addChunkData(dataChunk, memberValue, memberVariable.getType(), memberVariable.getArraySpec(), scopeRef);
}
} else if(valueType instanceof SymbolTypePointer && valueArraySpec!=null) {

View File

@ -80,7 +80,7 @@ public class PassNAddInitializerValueListTypeCasts extends Pass2SsaOptimization
SymbolTypeStruct declaredStructType = (SymbolTypeStruct) declaredType;
// Recursively cast all sub-elements
StructDefinition structDefinition = declaredStructType.getStructDefinition(program.getScope());
Collection<Variable> memberDefinitions = structDefinition.getAllVariables(false);
Collection<Variable> memberDefinitions = structDefinition.getAllVars(false);
int size = memberDefinitions.size();
if(size!=valueList.getList().size()) {
throw new CompileError(

View File

@ -141,7 +141,7 @@ public class PassNEliminateUnusedVars extends Pass2SsaOptimization {
Collection<Variable> allConstants = getScope().getAllConstants(true);
for(Variable constant : allConstants) {
if(!(constant.getScope() instanceof EnumDefinition)) {
if(!(constant.getScope() instanceof EnumDefinition) && !(constant.getScope() instanceof StructDefinition)) {
if(referenceInfos.isUnused(constant.getRef())) {
if(constant.isDeclaredExport()) {
// Do not eliminate constants declared as export

View File

@ -68,7 +68,7 @@ SYMBOL TABLE SSA
(byte*) bar_thing3 loadstore
(byte) foo::thing1
(byte) foo::thing2
(byte*) foo::thing3
(const byte*) foo::thing3 = { fill( $c, 0) }
(void()) main()
(bool~) main::$0
(byte*~) main::$1
@ -206,7 +206,6 @@ VARIABLE REGISTER WEIGHTS
(byte*) bar_thing3 loadstore 20.0
(byte) foo::thing1
(byte) foo::thing2
(byte*) foo::thing3
(void()) main()
(byte) main::i
(byte) main::i#3 7.333333333333333
@ -475,7 +474,7 @@ FINAL SYMBOL TABLE
(byte*) bar_thing3 loadstore zp[2]:2 20.0
(byte) foo::thing1
(byte) foo::thing2
(byte*) foo::thing3
(const byte*) foo::thing3 = { fill( $c, 0) }
(void()) main()
(label) main::@1
(label) main::@return

View File

@ -9,7 +9,7 @@
(byte*) bar_thing3 loadstore zp[2]:2 20.0
(byte) foo::thing1
(byte) foo::thing2
(byte*) foo::thing3
(const byte*) foo::thing3 = { fill( $c, 0) }
(void()) main()
(label) main::@1
(label) main::@return

View File

@ -465,7 +465,7 @@ SYMBOL TABLE SSA
(const byte) HEXADECIMAL = (number) $10
(const byte) OCTAL = (number) 8
(dword) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 3, 0) }
(const byte) RADIX::BINARY = (number) 2
(const byte) RADIX::DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL = (number) $10
@ -1320,7 +1320,6 @@ ultoa_append::@2: scope:[ultoa_append] from ultoa_append::@1
VARIABLE REGISTER WEIGHTS
(dword) Person::id
(byte*) Person::initials
(void()) main()
(void()) print_char((byte) print_char::ch)
(byte) print_char::ch
@ -2699,7 +2698,7 @@ FINAL SYMBOL TABLE
(label) @end
(const byte*) DIGITS = (string) "0123456789abcdef"z
(dword) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 3, 0) }
(const byte) RADIX::BINARY = (number) 2
(const byte) RADIX::DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL = (number) $10

View File

@ -3,7 +3,7 @@
(label) @end
(const byte*) DIGITS = (string) "0123456789abcdef"z
(dword) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 3, 0) }
(const byte) RADIX::BINARY = (number) 2
(const byte) RADIX::DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL = (number) $10

View File

@ -122,7 +122,7 @@ SYMBOL TABLE SSA
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $40, 0) }
(const byte*) SCREEN = (byte*)(number) $400
(byte) idx
(byte) idx#0
@ -346,7 +346,6 @@ print_person::@2: scope:[print_person] from print_person::@1
VARIABLE REGISTER WEIGHTS
(byte) Person::id
(byte*) Person::name
(byte) idx
(byte) idx#13 3.0
(byte) idx#14 9.75
@ -749,7 +748,7 @@ FINAL SYMBOL TABLE
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $40, 0) }
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#13 reg byte y 3.0

View File

@ -3,7 +3,7 @@
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $40, 0) }
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#13 reg byte y 3.0

View File

@ -85,7 +85,7 @@ SYMBOL TABLE SSA
(const byte) OFFSET_STRUCT_PERSON_ID = (byte) 0
(const byte) OFFSET_STRUCT_PERSON_INITIALS = (byte) 1
(byte) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 4, 0) }
(const byte*) SCREEN = (byte*)(number) $400
(const byte) SIZEOF_STRUCT_PERSON = (byte) 5
(byte) idx
@ -270,7 +270,6 @@ print_person::@return: scope:[print_person] from print_person
VARIABLE REGISTER WEIGHTS
(byte) Person::id
(byte*) Person::initials
(byte) idx
(byte) idx#10 1.0
(byte) idx#15 2.0
@ -706,7 +705,7 @@ FINAL SYMBOL TABLE
(label) @end
(const byte) OFFSET_STRUCT_PERSON_INITIALS = (byte) 1
(byte) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 4, 0) }
(const byte*) SCREEN = (byte*) 1024
(const byte) SIZEOF_STRUCT_PERSON = (byte) 5
(byte) idx

View File

@ -3,7 +3,7 @@
(label) @end
(const byte) OFFSET_STRUCT_PERSON_INITIALS = (byte) 1
(byte) Person::id
(byte*) Person::initials
(const byte*) Person::initials = { fill( 4, 0) }
(const byte*) SCREEN = (byte*) 1024
(const byte) SIZEOF_STRUCT_PERSON = (byte) 5
(byte) idx

View File

@ -96,7 +96,7 @@ SYMBOL TABLE SSA
(const byte) OFFSET_STRUCT_PERSON_ID = (byte) 0
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*)(number) $400
(const byte) SIZEOF_STRUCT_PERSON = (byte) $11
(byte) idx
@ -292,7 +292,6 @@ print_person::@2: scope:[print_person] from print_person::@1
VARIABLE REGISTER WEIGHTS
(byte) Person::id
(byte*) Person::name
(byte) idx
(byte) idx#13 3.0
(byte) idx#14 6.5
@ -729,7 +728,7 @@ FINAL SYMBOL TABLE
(const byte*) DIGIT = (string) "0123456789"
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*) 1024
(const byte) SIZEOF_STRUCT_PERSON = (byte) $11
(byte) idx

View File

@ -4,7 +4,7 @@
(const byte*) DIGIT = (string) "0123456789"
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*) 1024
(const byte) SIZEOF_STRUCT_PERSON = (byte) $11
(byte) idx

View File

@ -69,7 +69,7 @@ SYMBOL TABLE SSA
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(number~) main::$0
@ -294,7 +294,6 @@ main::@return: scope:[main] from main
VARIABLE REGISTER WEIGHTS
(word) Person::age
(byte) Person::id
(byte*) Person::name
(void()) main()
(struct Person*) main::person
@ -480,7 +479,7 @@ FINAL SYMBOL TABLE
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(label) main::@return

View File

@ -5,7 +5,7 @@
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(label) main::@return

View File

@ -35,7 +35,7 @@ SYMBOL TABLE SSA
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(byte*~) main::$0
@ -132,7 +132,6 @@ main::@return: scope:[main] from main
VARIABLE REGISTER WEIGHTS
(word) Person::age
(byte) Person::id
(byte*) Person::name
(void()) main()
(struct Person*) main::person
@ -283,7 +282,7 @@ FINAL SYMBOL TABLE
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(label) main::@return

View File

@ -4,7 +4,7 @@
(const byte) OFFSET_STRUCT_PERSON_NAME = (byte) 1
(word) Person::age
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $d, 0) }
(const byte) SIZEOF_STRUCT_PERSON = (byte) $10
(void()) main()
(label) main::@return

View File

@ -114,7 +114,7 @@ SYMBOL TABLE SSA
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*)(number) $400
(byte) idx
(byte) idx#0
@ -304,7 +304,6 @@ print_person::@2: scope:[print_person] from print_person::@1
VARIABLE REGISTER WEIGHTS
(byte) Person::id
(byte*) Person::name
(byte) idx
(byte) idx#13 3.0
(byte) idx#14 9.75
@ -701,7 +700,7 @@ FINAL SYMBOL TABLE
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#13 reg byte y 3.0

View File

@ -3,7 +3,7 @@
(label) @end
(const byte*) DIGIT = (string) "0123456789"
(byte) Person::id
(byte*) Person::name
(const byte*) Person::name = { fill( $10, 0) }
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#13 reg byte y 3.0