1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-06-30 00:29:45 +00:00

Added volatile/const booleans to SymbolType. So far they are always false!

This commit is contained in:
jespergravgaard 2021-04-16 08:54:02 +02:00
parent 76edc3b8c8
commit 8b2186e634
17 changed files with 160 additions and 49 deletions

View File

@ -13,7 +13,7 @@ public class EnumDefinition extends Scope {
@Override
public SymbolType getType() {
return new SymbolTypeEnum(this);
return new SymbolTypeEnum(this, false, false);
}
@Override

View File

@ -15,7 +15,7 @@ public class StructDefinition extends Scope {
@Override
public SymbolType getType() {
return new SymbolTypeStruct(this);
return new SymbolTypeStruct(this, false, false);
}
/**

View File

@ -5,18 +5,24 @@ import java.io.Serializable;
/** Symbol Types */
public interface SymbolType extends Serializable {
/** Specifies that the value of the variable may change at any time, so the optimizer must not make assumptions. The variable must always live in memory to be available for any multi-threaded access (eg. in interrupts). (volatile keyword) */
boolean isVolatile();
/** Specifies that the variable is not allowed to be modified (const keyword). The compiler should try to detect modifications and generate compile-time errors if they occur.*/
boolean isNomodify();
/** Unsigned byte (8 bits)). */
SymbolTypeIntegerFixed BYTE = new SymbolTypeIntegerFixed("byte", 0, 255, false, 8);
SymbolTypeIntegerFixed BYTE = new SymbolTypeIntegerFixed("byte", 0, 255, false, 8, false, false);
/** Signed byte (8 bits). */
SymbolTypeIntegerFixed SBYTE = new SymbolTypeIntegerFixed("signed byte", -128, 127, true, 8);
SymbolTypeIntegerFixed SBYTE = new SymbolTypeIntegerFixed("signed byte", -128, 127, true, 8, false, false);
/** Unsigned word (2 bytes, 16 bits). */
SymbolTypeIntegerFixed WORD = new SymbolTypeIntegerFixed("word", 0, 65_535, false, 16);
SymbolTypeIntegerFixed WORD = new SymbolTypeIntegerFixed("word", 0, 65_535, false, 16, false, false);
/** Signed word (2 bytes, 16 bits). */
SymbolTypeIntegerFixed SWORD = new SymbolTypeIntegerFixed("signed word", -32_768, 32_767, true, 16);
SymbolTypeIntegerFixed SWORD = new SymbolTypeIntegerFixed("signed word", -32_768, 32_767, true, 16, false, false);
/** Unsigned double word (4 bytes, 32 bits). */
SymbolTypeIntegerFixed DWORD = new SymbolTypeIntegerFixed("dword", 0, 4_294_967_296L, false, 32);
SymbolTypeIntegerFixed DWORD = new SymbolTypeIntegerFixed("dword", 0, 4_294_967_296L, false, 32, false, false);
/** Signed double word (4 bytes, 32 bits). */
SymbolTypeIntegerFixed SDWORD = new SymbolTypeIntegerFixed("signed dword", -2_147_483_648, 2_147_483_647, true, 32);
SymbolTypeIntegerFixed SDWORD = new SymbolTypeIntegerFixed("signed dword", -2_147_483_648, 2_147_483_647, true, 32, false, false);
/** Integer with unknown size and unknown signedness (used for constant expressions). */
SymbolTypeIntegerAuto NUMBER = new SymbolTypeIntegerAuto("number");
/** Unsigned integer with unknown size (used for constant expressions). */
@ -24,15 +30,15 @@ public interface SymbolType extends Serializable {
/** Signed integer with unknown size (used for constant expressions). */
SymbolTypeIntegerAuto SNUMBER = new SymbolTypeIntegerAuto("snumber");
/** Boolean value. */
SymbolTypeNamed BOOLEAN = new SymbolTypeNamed("bool", 1);
SymbolTypeNamed BOOLEAN = new SymbolTypeNamed("bool", 1, false, false);
/** Numeric floating point value. */
SymbolTypeNamed DOUBLE = new SymbolTypeNamed("double", 1);
SymbolTypeNamed DOUBLE = new SymbolTypeNamed("double", 1, false, false);
/** A label. Name of functions of jump-targets. */
SymbolTypeNamed LABEL = new SymbolTypeNamed("label", 1);
SymbolTypeNamed LABEL = new SymbolTypeNamed("label", 1, false, false);
/** Void type representing no value. */
SymbolTypeNamed VOID = new SymbolTypeNamed("void", 0);
SymbolTypeNamed VOID = new SymbolTypeNamed("void", 0, false, false);
/** An unresolved type. Will be infered later. */
SymbolTypeNamed VAR = new SymbolTypeNamed("var", -1);
SymbolTypeNamed VAR = new SymbolTypeNamed("var", -1, false, false);
/**
* Get a simple symbol type from the type name.
@ -43,45 +49,30 @@ public interface SymbolType extends Serializable {
static SymbolType get(String name) {
switch(name) {
case "byte":
return BYTE;
case "unsigned byte":
return BYTE;
case "signed byte":
return SBYTE;
case "char":
return BYTE;
case "unsigned char":
return BYTE;
case "signed byte":
case "signed char":
return SBYTE;
case "word":
return WORD;
case "unsigned word":
return WORD;
case "signed word":
return SWORD;
case "short":
return SWORD;
case "unsigned short":
return WORD;
case "signed short":
return SWORD;
case "int":
return SWORD;
case "unsigned int":
return WORD;
case "signed word":
case "short":
case "signed short":
case "int":
case "signed int":
return SWORD;
case "dword":
return DWORD;
case "unsigned dword":
return DWORD;
case "signed dword":
return SDWORD;
case "long":
return SDWORD;
case "unsigned long":
return DWORD;
case "signed dword":
case "long":
case "signed long":
return SDWORD;
case "bool":
@ -94,7 +85,6 @@ public interface SymbolType extends Serializable {
return null;
}
/**
* Get the name of the type
*

View File

@ -12,6 +12,16 @@ public class SymbolTypeBlockScope implements SymbolType {
return "BLOCK";
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
@Override
public int getSizeBytes() {
return -1;

View File

@ -13,9 +13,24 @@ public class SymbolTypeEnum implements SymbolType {
/** The enum definition. */
private EnumDefinition definition;
public SymbolTypeEnum(EnumDefinition definition) {
private final boolean isVolatile;
private final boolean isNomodify;
public SymbolTypeEnum(EnumDefinition definition, boolean isVolatile, boolean isNomodify) {
this.definition = definition;
this.name = definition.getLocalName();
this.isVolatile = isVolatile;
this.isNomodify = isNomodify;
}
@Override
public boolean isVolatile() {
return isVolatile;
}
@Override
public boolean isNomodify() {
return isNomodify;
}
@Override

View File

@ -11,6 +11,16 @@ public class SymbolTypeIntegerAuto implements SymbolTypeInteger {
this.typeName = typeName;
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
@Override
public String getTypeName() {
return typeName;
@ -26,7 +36,6 @@ public class SymbolTypeIntegerAuto implements SymbolTypeInteger {
return getTypeName();
}
@Override
public boolean equals(Object o) {
if(this == o) return true;

View File

@ -12,13 +12,17 @@ public class SymbolTypeIntegerFixed implements SymbolTypeInteger {
private final long maxValue;
private final boolean signed;
private final int bits;
private final boolean isVolatile;
private final boolean isNomodify;
SymbolTypeIntegerFixed(String typeName, long minValue, long maxValue, boolean signed, int bits) {
SymbolTypeIntegerFixed(String typeName, long minValue, long maxValue, boolean signed, int bits, boolean isVolatile, boolean isNomodify) {
this.typeName = typeName;
this.minValue = minValue;
this.maxValue = maxValue;
this.signed = signed;
this.bits = bits;
this.isVolatile = isVolatile;
this.isNomodify = isNomodify;
}
/**
@ -62,6 +66,15 @@ public class SymbolTypeIntegerFixed implements SymbolTypeInteger {
return null;
}
@Override
public boolean isVolatile() {
return isVolatile;
}
@Override
public boolean isNomodify() {
return isNomodify;
}
/**
* Determines if a value can be represented by the type without loss of information

View File

@ -4,12 +4,25 @@ package dk.camelot64.kickc.model.types;
public class SymbolTypeNamed implements SymbolType {
private String typeName;
private int sizeBytes;
private final boolean isVolatile;
private final boolean isNomodify;
SymbolTypeNamed(String typeName, int sizeBytes) {
SymbolTypeNamed(String typeName, int sizeBytes, boolean isVolatile, boolean isNomodify) {
this.typeName = typeName;
this.sizeBytes = sizeBytes;
this.isVolatile = isVolatile;
this.isNomodify = isNomodify;
}
@Override
public boolean isVolatile() {
return isVolatile;
}
@Override
public boolean isNomodify() {
return isNomodify;
}
public String getTypeName() {

View File

@ -15,13 +15,29 @@ public class SymbolTypePointer implements SymbolType {
/** If non-null the pointer is an array. */
private ArraySpec arraySpec;
public SymbolTypePointer(SymbolType elementType, ArraySpec arraySpec) {
private final boolean isVolatile;
private final boolean isNomodify;
public SymbolTypePointer(SymbolType elementType, ArraySpec arraySpec, boolean isVolatile, boolean isNomodify) {
this.elementType = elementType;
this.arraySpec = arraySpec;
this.isVolatile = isVolatile;
this.isNomodify = isNomodify;
}
public SymbolTypePointer(SymbolType elementType) {
this(elementType, null);
this(elementType, null, false, false);
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
public SymbolType getElementType() {

View File

@ -11,6 +11,16 @@ public class SymbolTypeProcedure implements SymbolType {
this.returnType = returnType;
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
@Override
public int getSizeBytes() {
return -1;

View File

@ -7,6 +7,16 @@ public class SymbolTypeProgram implements SymbolType {
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
@Override
public String getTypeName() {
return "PROGRAM";

View File

@ -18,9 +18,24 @@ public class SymbolTypeStruct implements SymbolType {
/** Size of the struct in bytes. */
private int sizeBytes;
public SymbolTypeStruct(StructDefinition structDefinition) {
private final boolean isVolatile;
private final boolean isNomodify;
public SymbolTypeStruct(StructDefinition structDefinition, boolean isVolatile, boolean isNomodify) {
this.name = structDefinition.getLocalName();
this.sizeBytes = calculateSizeBytes(structDefinition, null);
this.isVolatile = isVolatile;
this.isNomodify = isNomodify;
}
@Override
public boolean isVolatile() {
return isVolatile;
}
@Override
public boolean isNomodify() {
return isNomodify;
}
@Override

View File

@ -7,6 +7,16 @@ public class SymbolTypeTypeDefScope implements SymbolType {
}
@Override
public boolean isVolatile() {
return false;
}
@Override
public boolean isNomodify() {
return false;
}
@Override
public String getTypeName() {
return "TYPEDEF";

View File

@ -980,7 +980,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
}
final VariableDeclaration.VariableDeclType elementDeclType = varDecl.getEffectiveDeclType();
VariableDeclaration.VariableDeclType arrayDeclType = new VariableDeclaration.VariableDeclType();
arrayDeclType.setType(new SymbolTypePointer(elementDeclType.getType(), arraySpec));
arrayDeclType.setType(new SymbolTypePointer(elementDeclType.getType(), arraySpec, false, false));
arrayDeclType.setElementDeclType(elementDeclType);
varDecl.setVarDeclType(arrayDeclType);
return null;
@ -2096,7 +2096,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
arraySpec = new ArraySpec();
}
arrayDeclType.setType(new SymbolTypePointer(elementDeclType.getType(), arraySpec));
arrayDeclType.setType(new SymbolTypePointer(elementDeclType.getType(), arraySpec, false, false));
arrayDeclType.setElementDeclType(elementDeclType);
varDecl.setVarDeclType(arrayDeclType);
return null;

View File

@ -78,7 +78,7 @@ public class Pass1ExtractInlineStrings extends Pass1Base {
}
final long stringLength = constantString.getStringLength();
final ConstantInteger arraySize = new ConstantInteger(stringLength, stringLength<256?SymbolType.BYTE : SymbolType.WORD);
Variable strConst = Variable.createConstant(name, new SymbolTypePointer(SymbolType.BYTE, new ArraySpec(arraySize)), blockScope, constantString, blockScope.getSegmentData());
Variable strConst = Variable.createConstant(name, new SymbolTypePointer(SymbolType.BYTE, new ArraySpec(arraySize), false, false), blockScope, constantString, blockScope.getSegmentData());
blockScope.add(strConst);
if(getLog().isVerbosePass1CreateSsa()) {
getLog().append("Creating constant string variable for inline " + strConst.toString(getProgram()) + " \"" + constantString.getValue() + "\"");

View File

@ -30,7 +30,7 @@ public class Pass1StructTypeSizeFix extends Pass2SsaOptimization {
// Update all SIZEOF_XXX constants
for(Scope subScope : getScope().getAllScopes(false)) {
if(subScope instanceof StructDefinition) {
SymbolTypeStruct typeStruct = new SymbolTypeStruct((StructDefinition) subScope);
SymbolTypeStruct typeStruct = new SymbolTypeStruct((StructDefinition) subScope, false, false);
StructDefinition structDefinition = typeStruct.getStructDefinition(getScope());
int sizeBytes = typeStruct.calculateSizeBytes(structDefinition, getScope());
if(sizeBytes != typeStruct.getSizeBytes()) {

View File

@ -93,7 +93,7 @@ public class Pass2ConstantStringConsolidation extends Pass2SsaOptimization {
String localName = getRootName(constantVars);
final long stringLength = constString.getStringLength();
final ConstantInteger arraySize = new ConstantInteger(stringLength, stringLength<256?SymbolType.BYTE : SymbolType.WORD);
Variable newRootConstant = Variable.createConstant(localName, new SymbolTypePointer(SymbolType.BYTE, new ArraySpec(arraySize)), rootScope, constString, segmentData);
Variable newRootConstant = Variable.createConstant(localName, new SymbolTypePointer(SymbolType.BYTE, new ArraySpec(arraySize), false, false), rootScope, constString, segmentData);
rootScope.add(newRootConstant);
rootConstant = newRootConstant;
}