mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-08-09 04:25:12 +00:00
Now identifying undeclared variables
This commit is contained in:
@@ -32,7 +32,7 @@ public class Compiler {
|
|||||||
|
|
||||||
return program;
|
return program;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(log.getLog());
|
//System.out.println(log.getLog());
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -60,10 +60,8 @@ public class AsmFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String assignmentWithAluSignature(StatementAssignment assignment, StatementAssignment assignmentAlu) {
|
private String assignmentWithAluSignature(StatementAssignment assignment, StatementAssignment assignmentAlu) {
|
||||||
RValue assignmentRValue2 = assignment.getrValue2();
|
VariableRef assignmentRValue2 = (VariableRef) assignment.getrValue2();
|
||||||
Variable assignmentVar = program.getScope().getVariable((VariableRef) assignmentRValue2);
|
RegisterAllocation.Register rVal2Register = program.getAllocation().getRegister(assignmentRValue2);
|
||||||
|
|
||||||
RegisterAllocation.Register rVal2Register = program.getRegister(assignmentVar);
|
|
||||||
if(!rVal2Register.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
|
if(!rVal2Register.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
|
||||||
throw new RuntimeException("Error! ALU register only allowed as rValue2. "+assignment);
|
throw new RuntimeException("Error! ALU register only allowed as rValue2. "+assignment);
|
||||||
}
|
}
|
||||||
@@ -220,7 +218,7 @@ public class AsmFragment {
|
|||||||
value = program.getScope().getVariable((VariableRef) value);
|
value = program.getScope().getVariable((VariableRef) value);
|
||||||
}
|
}
|
||||||
if (value instanceof Variable) {
|
if (value instanceof Variable) {
|
||||||
value = program.getRegister((Variable) value);
|
value = program.getAllocation().getRegister(((Variable) value).getRef());
|
||||||
} else if (value instanceof PointerDereferenceSimple) {
|
} else if (value instanceof PointerDereferenceSimple) {
|
||||||
PointerDereferenceSimple deref = (PointerDereferenceSimple) value;
|
PointerDereferenceSimple deref = (PointerDereferenceSimple) value;
|
||||||
return "_star_" + bind(deref.getPointer());
|
return "_star_" + bind(deref.getPointer());
|
||||||
|
11
src/main/java/dk/camelot64/kickc/icl/CompileError.java
Normal file
11
src/main/java/dk/camelot64/kickc/icl/CompileError.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package dk.camelot64.kickc.icl;
|
||||||
|
|
||||||
|
/** Signals some error in the code (or compilation) */
|
||||||
|
public class CompileError extends RuntimeException {
|
||||||
|
|
||||||
|
public CompileError(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@@ -105,14 +105,6 @@ public class Program {
|
|||||||
return allocation;
|
return allocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RegisterAllocation.Register getRegister(Variable variable) {
|
|
||||||
RegisterAllocation.Register register = null;
|
|
||||||
if (allocation != null) {
|
|
||||||
register = allocation.getRegister(variable.getRef());
|
|
||||||
}
|
|
||||||
return register;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLiveRangeVariables(LiveRangeVariables liveRangeVariables) {
|
public void setLiveRangeVariables(LiveRangeVariables liveRangeVariables) {
|
||||||
this.liveRangeVariables = liveRangeVariables;
|
this.liveRangeVariables = liveRangeVariables;
|
||||||
}
|
}
|
||||||
|
@@ -413,6 +413,10 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
@Override
|
@Override
|
||||||
public RValue visitExprId(KickCParser.ExprIdContext ctx) {
|
public RValue visitExprId(KickCParser.ExprIdContext ctx) {
|
||||||
Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText());
|
Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText());
|
||||||
|
if(variable == null) {
|
||||||
|
program.getLog().append("ERROR! Line "+ctx.getStart().getLine()+". Unknown variable "+ctx.NAME().getText());
|
||||||
|
throw new CompileError("ERROR! Line "+ctx.getStart().getLine()+". Unknown variable "+ctx.NAME().getText());
|
||||||
|
}
|
||||||
return variable.getRef();
|
return variable.getRef();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -87,8 +87,7 @@ public class Pass4CodeGeneration {
|
|||||||
boolean isAlu = false;
|
boolean isAlu = false;
|
||||||
if (lValue instanceof VariableRef) {
|
if (lValue instanceof VariableRef) {
|
||||||
VariableRef lValueRef = (VariableRef) lValue;
|
VariableRef lValueRef = (VariableRef) lValue;
|
||||||
Variable lValueVar = getScope().getVariable(lValueRef);
|
RegisterAllocation.Register lValRegister = program.getAllocation().getRegister(lValueRef);
|
||||||
RegisterAllocation.Register lValRegister = program.getRegister(lValueVar);
|
|
||||||
if (lValRegister.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
|
if (lValRegister.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
|
||||||
asm.addComment(statement + " // ALU");
|
asm.addComment(statement + " // ALU");
|
||||||
StatementAssignment assignmentAlu = assignment;
|
StatementAssignment assignmentAlu = assignment;
|
||||||
@@ -195,8 +194,7 @@ public class Pass4CodeGeneration {
|
|||||||
private RegisterAllocation.Register getRegister(RValue rValue) {
|
private RegisterAllocation.Register getRegister(RValue rValue) {
|
||||||
if (rValue instanceof VariableRef) {
|
if (rValue instanceof VariableRef) {
|
||||||
VariableRef rValueRef = (VariableRef) rValue;
|
VariableRef rValueRef = (VariableRef) rValue;
|
||||||
Variable rValueVar = getScope().getVariable(rValueRef);
|
return program.getAllocation().getRegister(rValueRef);
|
||||||
return program.getRegister(rValueVar);
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package dk.camelot64.kickc.test;
|
package dk.camelot64.kickc.test;
|
||||||
|
|
||||||
import dk.camelot64.kickc.Compiler;
|
import dk.camelot64.kickc.Compiler;
|
||||||
|
import dk.camelot64.kickc.icl.CompileError;
|
||||||
import dk.camelot64.kickc.icl.Program;
|
import dk.camelot64.kickc.icl.Program;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
@@ -73,7 +74,13 @@ public class TestCompilationOutput extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testUseUndeclared() throws IOException, URISyntaxException {
|
public void testUseUndeclared() throws IOException, URISyntaxException {
|
||||||
compileAndCompare("useundeclared");
|
try {
|
||||||
|
compileAndCompare("useundeclared");
|
||||||
|
} catch (CompileError e) {
|
||||||
|
// expecting error!
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fail("Expected compile error.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user