1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-26 12:49:21 +00:00

Working on #554 variables used in ASM.

This commit is contained in:
jespergravgaard 2020-11-01 21:51:16 +01:00
commit 6bec9849b6
10 changed files with 64 additions and 15 deletions

View File

@ -18,7 +18,6 @@ import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
@ -277,6 +276,7 @@ public class Compiler {
new PassNAddTypeConversionAssignment(program, true).execute();
new Pass1EarlyConstantIdentification(program).execute();
// new Pass1AsmUsesHandling(program).execute();
new PassNAssertConstantModification(program).execute();
new PassNAssertTypeDeref(program).check();

View File

@ -8,7 +8,10 @@ import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.symbols.Symbol;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.types.SymbolTypeStruct;
import dk.camelot64.kickc.model.values.*;
import dk.camelot64.kickc.model.values.ConstantSymbolPointer;
import dk.camelot64.kickc.model.values.RValue;
import dk.camelot64.kickc.model.values.SymbolVariableRef;
import dk.camelot64.kickc.model.values.Value;
/**
* Update variables properly if address-of is used
@ -21,6 +24,7 @@ public class Pass1AddressOfHandling extends Pass2SsaOptimization {
@Override
public boolean step() {
// Expressions using & operator
ProgramExpressionIterator.execute(getProgram(), (programExpression, currentStmt, stmtIt, currentBlock) -> {
if(Operators.ADDRESS_OF.equals(programExpression.getOperator())) {
RValue rValue = ((ProgramExpressionUnary) programExpression).getOperand();
@ -36,13 +40,16 @@ public class Pass1AddressOfHandling extends Pass2SsaOptimization {
});
ProgramValueIterator.execute(getProgram(), (programValue, currentStmt, stmtIt, currentBlock) -> {
if(programValue.get() instanceof ConstantSymbolPointer) {
// Values containing constant pointers
Value value = ((ConstantSymbolPointer) programValue.get()).getToSymbol();
if(value instanceof SymbolVariableRef) {
Symbol toSymbol = getScope().getSymbol((SymbolVariableRef) value);
if(toSymbol instanceof Variable) {
final Variable variable = (Variable) toSymbol;
final String stmtStr = currentStmt==null?toSymbol.toString(getProgram()):currentStmt.toString(getProgram(), false);
updateAddressOfVariable(variable, stmtStr);
if(!variable.isNoModify() && !variable.isVolatile()) {
final String stmtStr = currentStmt == null ? toSymbol.toString(getProgram()) : currentStmt.toString(getProgram(), false);
updateAddressOfVariable(variable, stmtStr);
}
}
}
}
@ -54,10 +61,12 @@ public class Pass1AddressOfHandling extends Pass2SsaOptimization {
if(variable.getType() instanceof SymbolTypeStruct) {
variable.setKind(Variable.Kind.LOAD_STORE);
getLog().append("Setting struct to load/store in variable affected by address-of " + stmtStr);
//getLog().append("Setting struct to load/store in variable affected by address-of: " + variable.toString() + " in " + stmtStr);
} else {
variable.setKind(Variable.Kind.LOAD_STORE);
variable.setVolatile(true);
getLog().append("Setting inferred volatile on symbol affected by address-of " + stmtStr);
//getLog().append("Setting inferred volatile on symbol affected by address-of: " + variable.toString() + " in " + stmtStr);
}
}

View File

@ -0,0 +1,51 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.iterator.ProgramValue;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.types.SymbolTypeStruct;
import dk.camelot64.kickc.model.values.SymbolVariableRef;
import dk.camelot64.kickc.model.values.Value;
/**
* Update variables properly if address-of is used
*/
public class Pass1AsmUsesHandling extends Pass2SsaOptimization {
public Pass1AsmUsesHandling(Program program) {
super(program);
}
@Override
public boolean step() {
ProgramValueIterator.execute(getProgram(), (programValue, currentStmt, stmtIt, currentBlock) -> {
if(programValue instanceof ProgramValue.ProgramValueAsmReferenced || programValue instanceof ProgramValue.KickAsmUses || programValue instanceof ProgramValue.ProgramValueConstantArrayKickAsmUses) {
// Symbol used in inline ASM or KickAsm
final Value value = programValue.get();
if(value instanceof SymbolVariableRef) {
Variable variable = getScope().getVariable((SymbolVariableRef) value);
if(!variable.isKindConstant() && !variable.isVolatile()) {
final String stmtStr = currentStmt == null ? value.toString(getProgram()) : currentStmt.toString(getProgram(), false);
updateAddressOfVariable(variable, stmtStr);
}
}
}
});
return false;
}
private void updateAddressOfVariable(Variable variable, String stmtStr) {
if(variable.getType() instanceof SymbolTypeStruct) {
variable.setKind(Variable.Kind.LOAD_STORE);
getLog().append("Setting struct to load/store in variable affected by address-of: " + variable.toString() + " in " + stmtStr);
} else {
variable.setKind(Variable.Kind.LOAD_STORE);
variable.setVolatile(true);
getLog().append("Setting inferred volatile on symbol affected by address-of: " + variable.toString() + " in " + stmtStr);
}
}
}

View File

@ -1,7 +1,4 @@
Setting inferred volatile on symbol affected by address-of utoa16w::$2 = call utoa16n utoa16w::$1 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$5 = call utoa16n utoa16w::$4 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$8 = call utoa16n utoa16w::$7 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$11 = call utoa16n utoa16w::$10 &utoa16w::dst 1
CONTROL FLOW GRAPH SSA

View File

@ -1,7 +1,4 @@
Setting inferred volatile on symbol affected by address-of utoa16w::$2 = call utoa16n utoa16w::$1 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$5 = call utoa16n utoa16w::$4 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$8 = call utoa16n utoa16w::$7 &utoa16w::dst utoa16w::started
Setting inferred volatile on symbol affected by address-of utoa16w::$11 = call utoa16n utoa16w::$10 &utoa16w::dst 1
CONTROL FLOW GRAPH SSA

View File

@ -1,5 +1,4 @@
Setting inferred volatile on symbol affected by address-of main::$0 = call setscreen &screen screen1
Setting inferred volatile on symbol affected by address-of main::$1 = call setscreen &screen screen2
Inlined call call __init
CONTROL FLOW GRAPH SSA

View File

@ -1,5 +1,4 @@
Setting inferred volatile on symbol affected by address-of main::$0 = call sub 'a' &main::screen
Setting inferred volatile on symbol affected by address-of main::$1 = call sub 'b' &main::screen
CONTROL FLOW GRAPH SSA

View File

@ -1,4 +1,3 @@
Setting inferred volatile on symbol affected by address-of main::wp = &main::w
Resolving sizeof() main::$2 = sizeof main::idx
Resolving sizeof() main::$4 = sizeof main::b
Resolving sizeof() main::$7 = sizeof main::$6

View File

@ -1,4 +1,3 @@
Setting struct to load/store in variable affected by address-of main::q = &main::p
CONTROL FLOW GRAPH SSA

View File

@ -1,4 +1,3 @@
Setting struct to load/store in variable affected by address-of main::q = &main::p
CONTROL FLOW GRAPH SSA