mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-02-11 16:30:56 +00:00
Added pointify step. Still need to output the var to memory. #328
This commit is contained in:
parent
f696f8db18
commit
3ede41bab1
@ -225,6 +225,8 @@ public class Compiler {
|
||||
new Pass1ProcedureCallParameters(program).generate();
|
||||
new PassNUnwindLValueLists(program).execute();
|
||||
|
||||
new Pass1PointifyMemoryVariables(program).execute();
|
||||
|
||||
//getLog().append("CONTROL FLOW GRAPH (CALL PARAMETERS)");
|
||||
//getLog().append(program.getGraph().toString(program));
|
||||
|
||||
|
@ -0,0 +1,47 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
|
||||
import dk.camelot64.kickc.model.symbols.ConstantVar;
|
||||
import dk.camelot64.kickc.model.symbols.Scope;
|
||||
import dk.camelot64.kickc.model.symbols.Variable;
|
||||
import dk.camelot64.kickc.model.types.SymbolTypePointer;
|
||||
import dk.camelot64.kickc.model.values.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** Find memory variables and turn them into pointers */
|
||||
public class Pass1PointifyMemoryVariables extends Pass1Base {
|
||||
|
||||
public Pass1PointifyMemoryVariables(Program program) {
|
||||
super(program);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean step() {
|
||||
Map<VariableRef, ConstantRef> memoryVarPointers = new HashMap<>();
|
||||
for(Variable variable : getScope().getAllVariables(true)) {
|
||||
if(variable.isDeclaredAsMemory()) {
|
||||
Scope scope = variable.getScope();
|
||||
String pointerName = variable.getLocalName() + "_ptr";
|
||||
if(scope.getSymbol(pointerName) == null) {
|
||||
ConstantVar constantPointer = new ConstantVar(pointerName, scope, new SymbolTypePointer(variable.getType()), new ConstantSymbolPointer(variable.getRef()), variable.getDataSegment());
|
||||
scope.add(constantPointer);
|
||||
memoryVarPointers.put(variable.getRef(), constantPointer.getRef());
|
||||
getLog().append("Adding memory variable constant pointer " + constantPointer.toString(getProgram()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ProgramValueIterator.execute(getGraph(), (programValue, currentStmt, stmtIt, currentBlock) -> {
|
||||
Value value = programValue.get();
|
||||
if(memoryVarPointers.containsKey(value)) {
|
||||
programValue.set(new PointerDereferenceSimple(memoryVarPointers.get(value)));
|
||||
getLog().append("Updating memory variable reference " + programValue.get().toString(getProgram()));
|
||||
}
|
||||
});
|
||||
return memoryVarPointers.size() > 0;
|
||||
}
|
||||
|
||||
}
|
@ -37,7 +37,7 @@ public class TestPrograms {
|
||||
|
||||
@Test
|
||||
public void testDelcaredMemoryVar0() throws IOException, URISyntaxException {
|
||||
compileAndCompare("declared-memory-var-0"); //, log().verboseParse().verboseCreateSsa());
|
||||
compileAndCompare("declared-memory-var-0", log().verboseParse().verboseCreateSsa());
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1,11 +1,10 @@
|
||||
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
|
||||
|
||||
memory char idx;
|
||||
memory char idx=0;
|
||||
|
||||
const char* SCREEN = 0x0400;
|
||||
|
||||
void main() {
|
||||
idx = 0;
|
||||
for( char i: 0..5 ) {
|
||||
SCREEN[i] = idx;
|
||||
idx +=i;
|
||||
|
Loading…
x
Reference in New Issue
Block a user