1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-22 21:29:50 +00:00

Now identifying undeclared variables

This commit is contained in:
jespergravgaard 2017-08-01 01:33:42 +02:00
parent b56f634b1e
commit 14c1f7e8eb
7 changed files with 29 additions and 19 deletions

View File

@ -32,7 +32,7 @@ public class Compiler {
return program;
} catch (Exception e) {
System.out.println(log.getLog());
//System.out.println(log.getLog());
throw e;
}
}

View File

@ -60,10 +60,8 @@ public class AsmFragment {
}
private String assignmentWithAluSignature(StatementAssignment assignment, StatementAssignment assignmentAlu) {
RValue assignmentRValue2 = assignment.getrValue2();
Variable assignmentVar = program.getScope().getVariable((VariableRef) assignmentRValue2);
RegisterAllocation.Register rVal2Register = program.getRegister(assignmentVar);
VariableRef assignmentRValue2 = (VariableRef) assignment.getrValue2();
RegisterAllocation.Register rVal2Register = program.getAllocation().getRegister(assignmentRValue2);
if(!rVal2Register.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
throw new RuntimeException("Error! ALU register only allowed as rValue2. "+assignment);
}
@ -220,7 +218,7 @@ public class AsmFragment {
value = program.getScope().getVariable((VariableRef) value);
}
if (value instanceof Variable) {
value = program.getRegister((Variable) value);
value = program.getAllocation().getRegister(((Variable) value).getRef());
} else if (value instanceof PointerDereferenceSimple) {
PointerDereferenceSimple deref = (PointerDereferenceSimple) value;
return "_star_" + bind(deref.getPointer());

View 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);
}
}

View File

@ -105,14 +105,6 @@ public class Program {
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) {
this.liveRangeVariables = liveRangeVariables;
}

View File

@ -413,6 +413,10 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public RValue visitExprId(KickCParser.ExprIdContext ctx) {
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();
}

View File

@ -87,8 +87,7 @@ public class Pass4CodeGeneration {
boolean isAlu = false;
if (lValue instanceof VariableRef) {
VariableRef lValueRef = (VariableRef) lValue;
Variable lValueVar = getScope().getVariable(lValueRef);
RegisterAllocation.Register lValRegister = program.getRegister(lValueVar);
RegisterAllocation.Register lValRegister = program.getAllocation().getRegister(lValueRef);
if (lValRegister.getType().equals(RegisterAllocation.RegisterType.REG_ALU_BYTE)) {
asm.addComment(statement + " // ALU");
StatementAssignment assignmentAlu = assignment;
@ -195,8 +194,7 @@ public class Pass4CodeGeneration {
private RegisterAllocation.Register getRegister(RValue rValue) {
if (rValue instanceof VariableRef) {
VariableRef rValueRef = (VariableRef) rValue;
Variable rValueVar = getScope().getVariable(rValueRef);
return program.getRegister(rValueVar);
return program.getAllocation().getRegister(rValueRef);
} else {
return null;
}

View File

@ -1,6 +1,7 @@
package dk.camelot64.kickc.test;
import dk.camelot64.kickc.Compiler;
import dk.camelot64.kickc.icl.CompileError;
import dk.camelot64.kickc.icl.Program;
import junit.framework.TestCase;
import org.antlr.v4.runtime.CharStream;
@ -73,7 +74,13 @@ public class TestCompilationOutput extends TestCase {
}
public void testUseUndeclared() throws IOException, URISyntaxException {
compileAndCompare("useundeclared");
try {
compileAndCompare("useundeclared");
} catch (CompileError e) {
// expecting error!
return;
}
fail("Expected compile error.");
}