diff --git a/src/main/java/dk/camelot64/kickc/Compiler.java b/src/main/java/dk/camelot64/kickc/Compiler.java index 033720871..5e93e9819 100644 --- a/src/main/java/dk/camelot64/kickc/Compiler.java +++ b/src/main/java/dk/camelot64/kickc/Compiler.java @@ -217,8 +217,11 @@ public class Compiler { getLog().append(procedureCompilation.getStatementSequence().toString(program)); } } - new Pass1GenerateControlFlowGraph(program).execute(); + if(getLog().isVerbosePass1CreateSsa()) { + getLog().append("FIRST CONTROL FLOW GRAPH"); + getLog().append(program.getGraph().toString(program)); + } new Pass1ResolveForwardReferences(program).execute(); new Pass1AssertProcedureDefined(program).execute(); new Pass1AssertVariableDefined(program).execute(); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateControlFlowGraph.java b/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateControlFlowGraph.java index fb4f927e6..ec2b098be 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateControlFlowGraph.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateControlFlowGraph.java @@ -35,6 +35,13 @@ public class Pass1GenerateControlFlowGraph extends Pass1Base { ControlFlowBlock procBlock = getOrCreateBlock(procedure.getLabel().getRef(), procedure.getRef()); currentBlock = procBlock; for(Statement statement : sequence.getStatements()) { + Symbol currentBlockLabel = getProgram().getScope().getSymbol(currentBlock.getLabel()); + Scope currentBlockScope; + if(currentBlockLabel instanceof Procedure) { + currentBlockScope = (Scope) currentBlockLabel; + } else { + currentBlockScope = currentBlockLabel.getScope(); + } if(statement instanceof StatementProcedureBegin) { // Do nothing } else if(statement instanceof StatementProcedureEnd) { @@ -42,7 +49,7 @@ public class Pass1GenerateControlFlowGraph extends Pass1Base { currentBlock.setDefaultSuccessor(new Label(SymbolRef.PROCEXIT_BLOCK_NAME, programScope, false).getRef()); } else if(statement instanceof StatementLabel) { StatementLabel statementLabel = (StatementLabel) statement; - ControlFlowBlock nextBlock = getOrCreateBlock(statementLabel.getLabel(), procedure.getRef()); + ControlFlowBlock nextBlock = getOrCreateBlock(statementLabel.getLabel(), currentBlock.getScope()); nextBlock.setComments(statementLabel.getComments()); currentBlock.setDefaultSuccessor(nextBlock.getLabel()); currentBlock = nextBlock; @@ -50,13 +57,13 @@ public class Pass1GenerateControlFlowGraph extends Pass1Base { StatementJump statementJump = (StatementJump) statement; ControlFlowBlock jmpBlock = getOrCreateBlock(statementJump.getDestination(), currentBlock.getScope()); currentBlock.setDefaultSuccessor(jmpBlock.getLabel()); - ControlFlowBlock nextBlock = getOrCreateBlock(procedure.addLabelIntermediate().getRef(), currentBlock.getScope()); + ControlFlowBlock nextBlock = getOrCreateBlock(currentBlockScope.addLabelIntermediate().getRef(), currentBlock.getScope()); currentBlock = nextBlock; } else if(statement instanceof StatementConditionalJump) { currentBlock.addStatement(statement); StatementConditionalJump statementConditionalJump = (StatementConditionalJump) statement; ControlFlowBlock jmpBlock = getOrCreateBlock(statementConditionalJump.getDestination(), currentBlock.getScope()); - ControlFlowBlock nextBlock = getOrCreateBlock(procedure.addLabelIntermediate().getRef(), currentBlock.getScope()); + ControlFlowBlock nextBlock = getOrCreateBlock(currentBlockScope.addLabelIntermediate().getRef(), currentBlock.getScope()); currentBlock.setDefaultSuccessor(nextBlock.getLabel()); currentBlock.setConditionalSuccessor(jmpBlock.getLabel()); currentBlock = nextBlock;