WASM: creating memory access trace transformation

This commit is contained in:
Alexey Andreev 2016-09-22 00:58:21 +03:00
parent ff6a917bc3
commit 4f218b17f8
14 changed files with 340 additions and 8 deletions

View File

@ -74,6 +74,10 @@
<value /> <value />
</option> </option>
</inspection_tool> </inspection_tool>
<inspection_tool class="MissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreObjectMethods" value="true" />
<option name="ignoreAnonymousClassMethods" value="false" />
</inspection_tool>
<inspection_tool class="NonOsgiMavenDependency" enabled="false" level="WARNING" enabled_by_default="false" /> <inspection_tool class="NonOsgiMavenDependency" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="NullableProblems" enabled="true" level="WEAK WARNING" enabled_by_default="true"> <inspection_tool class="NullableProblems" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="REPORT_NULLABLE_METHOD_OVERRIDES_NOTNULL" value="true" /> <option name="REPORT_NULLABLE_METHOD_OVERRIDES_NOTNULL" value="true" />

View File

@ -72,6 +72,7 @@ import org.teavm.backend.wasm.render.WasmBinaryRenderer;
import org.teavm.backend.wasm.render.WasmBinaryWriter; import org.teavm.backend.wasm.render.WasmBinaryWriter;
import org.teavm.backend.wasm.render.WasmCRenderer; import org.teavm.backend.wasm.render.WasmCRenderer;
import org.teavm.backend.wasm.render.WasmRenderer; import org.teavm.backend.wasm.render.WasmRenderer;
import org.teavm.backend.wasm.transformation.MemoryAccessTraceTransformation;
import org.teavm.dependency.ClassDependency; import org.teavm.dependency.ClassDependency;
import org.teavm.dependency.DependencyChecker; import org.teavm.dependency.DependencyChecker;
import org.teavm.dependency.DependencyListener; import org.teavm.dependency.DependencyListener;
@ -341,6 +342,10 @@ public class WasmTarget implements TeaVMTarget {
new UnusedFunctionElimination(module).apply(); new UnusedFunctionElimination(module).apply();
if (Boolean.parseBoolean(System.getProperty("wasm.memoryTrace", "false"))) {
new MemoryAccessTraceTransformation(module).apply();
}
WasmBinaryWriter writer = new WasmBinaryWriter(); WasmBinaryWriter writer = new WasmBinaryWriter();
WasmBinaryRenderer renderer = new WasmBinaryRenderer(writer); WasmBinaryRenderer renderer = new WasmBinaryRenderer(writer);
renderer.render(module); renderer.render(module);

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmLoadFloat32 extends WasmExpression { public class WasmLoadFloat32 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
@ -35,10 +35,12 @@ public class WasmLoadFloat32 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmLoadFloat64 extends WasmExpression { public class WasmLoadFloat64 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
@ -35,10 +35,12 @@ public class WasmLoadFloat64 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmLoadInt32 extends WasmExpression { public class WasmLoadInt32 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmInt32Subtype convertFrom; private WasmInt32Subtype convertFrom;
@ -38,10 +38,12 @@ public class WasmLoadInt32 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmLoadInt64 extends WasmExpression { public class WasmLoadInt64 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmInt64Subtype convertFrom; private WasmInt64Subtype convertFrom;
@ -38,10 +38,12 @@ public class WasmLoadInt64 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -0,0 +1,22 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.model.expression;
public interface WasmMemoryAccess {
WasmExpression getIndex();
void setIndex(WasmExpression index);
}

View File

@ -0,0 +1,224 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.model.expression;
import java.util.List;
import java.util.function.Function;
import org.teavm.backend.wasm.model.WasmFunction;
public class WasmReplacingExpressionVisitor implements WasmExpressionVisitor {
private Function<WasmExpression, WasmExpression> mapper;
public WasmReplacingExpressionVisitor(
Function<WasmExpression, WasmExpression> mapper) {
this.mapper = mapper;
}
public void replace(WasmFunction function) {
replaceExpressions(function.getBody());
}
private void replaceExpressions(List<WasmExpression> expressions) {
for (int i = 0; i < expressions.size(); ++i) {
WasmExpression part = expressions.get(i);
part.acceptVisitor(this);
part = mapper.apply(part);
expressions.set(i, part);
}
}
@Override
public void visit(WasmBlock expression) {
replaceExpressions(expression.getBody());
}
@Override
public void visit(WasmBranch expression) {
expression.getCondition().acceptVisitor(this);
expression.setCondition(mapper.apply(expression.getCondition()));
if (expression.getResult() != null) {
expression.getResult().acceptVisitor(this);
expression.setResult(mapper.apply(expression.getResult()));
}
}
@Override
public void visit(WasmBreak expression) {
if (expression.getResult() != null) {
expression.getResult().acceptVisitor(this);
expression.setResult(mapper.apply(expression.getResult()));
}
}
@Override
public void visit(WasmSwitch expression) {
expression.getSelector().acceptVisitor(this);
expression.setSelector(mapper.apply(expression.getSelector()));
}
@Override
public void visit(WasmConditional expression) {
expression.getCondition().acceptVisitor(this);
expression.setCondition(mapper.apply(expression.getCondition()));
replaceExpressions(expression.getThenBlock().getBody());
replaceExpressions(expression.getElseBlock().getBody());
}
@Override
public void visit(WasmReturn expression) {
if (expression.getValue() != null) {
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}
}
@Override
public void visit(WasmUnreachable expression) {
}
@Override
public void visit(WasmInt32Constant expression) {
}
@Override
public void visit(WasmInt64Constant expression) {
}
@Override
public void visit(WasmFloat32Constant expression) {
}
@Override
public void visit(WasmFloat64Constant expression) {
}
@Override
public void visit(WasmGetLocal expression) {
}
@Override
public void visit(WasmSetLocal expression) {
expression.getValue().acceptVisitor(this);
}
@Override
public void visit(WasmIntBinary expression) {
expression.getFirst().acceptVisitor(this);
expression.setFirst(mapper.apply(expression.getFirst()));
expression.getSecond().acceptVisitor(this);
expression.setSecond(mapper.apply(expression.getSecond()));
}
@Override
public void visit(WasmFloatBinary expression) {
expression.getFirst().acceptVisitor(this);
expression.setFirst(mapper.apply(expression.getFirst()));
expression.getSecond().acceptVisitor(this);
expression.setSecond(mapper.apply(expression.getSecond()));
}
@Override
public void visit(WasmIntUnary expression) {
expression.getOperand().acceptVisitor(this);
expression.setOperand(mapper.apply(expression.getOperand()));
}
@Override
public void visit(WasmFloatUnary expression) {
expression.getOperand().acceptVisitor(this);
expression.setOperand(mapper.apply(expression.getOperand()));
}
@Override
public void visit(WasmConversion expression) {
expression.getOperand().acceptVisitor(this);
expression.setOperand(mapper.apply(expression.getOperand()));
}
@Override
public void visit(WasmCall expression) {
replaceExpressions(expression.getArguments());
}
@Override
public void visit(WasmIndirectCall expression) {
expression.getSelector().acceptVisitor(this);
expression.setSelector(mapper.apply(expression.getSelector()));
replaceExpressions(expression.getArguments());
}
@Override
public void visit(WasmDrop expression) {
expression.getOperand().acceptVisitor(this);
expression.setOperand(mapper.apply(expression.getOperand()));
}
@Override
public void visit(WasmLoadInt32 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
}
@Override
public void visit(WasmLoadInt64 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
}
@Override
public void visit(WasmLoadFloat32 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
}
@Override
public void visit(WasmLoadFloat64 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
}
@Override
public void visit(WasmStoreInt32 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}
@Override
public void visit(WasmStoreInt64 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}
@Override
public void visit(WasmStoreFloat32 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}
@Override
public void visit(WasmStoreFloat64 expression) {
expression.getIndex().acceptVisitor(this);
expression.setIndex(mapper.apply(expression.getIndex()));
expression.getValue().acceptVisitor(this);
expression.setValue(mapper.apply(expression.getValue()));
}
}

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmStoreFloat32 extends WasmExpression { public class WasmStoreFloat32 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmExpression value; private WasmExpression value;
@ -38,10 +38,12 @@ public class WasmStoreFloat32 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmStoreFloat64 extends WasmExpression { public class WasmStoreFloat64 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmExpression value; private WasmExpression value;
@ -38,10 +38,12 @@ public class WasmStoreFloat64 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmStoreInt32 extends WasmExpression { public class WasmStoreInt32 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmExpression value; private WasmExpression value;
@ -42,10 +42,12 @@ public class WasmStoreInt32 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -17,7 +17,7 @@ package org.teavm.backend.wasm.model.expression;
import java.util.Objects; import java.util.Objects;
public class WasmStoreInt64 extends WasmExpression { public class WasmStoreInt64 extends WasmExpression implements WasmMemoryAccess {
private int alignment; private int alignment;
private WasmExpression index; private WasmExpression index;
private WasmExpression value; private WasmExpression value;
@ -42,10 +42,12 @@ public class WasmStoreInt64 extends WasmExpression {
this.alignment = alignment; this.alignment = alignment;
} }
@Override
public WasmExpression getIndex() { public WasmExpression getIndex() {
return index; return index;
} }
@Override
public void setIndex(WasmExpression index) { public void setIndex(WasmExpression index) {
Objects.requireNonNull(index); Objects.requireNonNull(index);
this.index = index; this.index = index;

View File

@ -0,0 +1,58 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.transformation;
import org.teavm.backend.wasm.model.WasmFunction;
import org.teavm.backend.wasm.model.WasmModule;
import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmCall;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmMemoryAccess;
import org.teavm.backend.wasm.model.expression.WasmReplacingExpressionVisitor;
public class MemoryAccessTraceTransformation {
private WasmModule module;
public MemoryAccessTraceTransformation(WasmModule module) {
this.module = module;
}
public void apply() {
WasmFunction traceFunction = new WasmFunction("traceMemoryAccess");
traceFunction.setImportModule("debug");
traceFunction.setImportName("traceMemoryAccess");
traceFunction.getParameters().add(WasmType.INT32);
traceFunction.getParameters().add(WasmType.INT32);
traceFunction.setResult(WasmType.INT32);
module.add(traceFunction);
int[] positionHolder = { 0 };
WasmReplacingExpressionVisitor visitor = new WasmReplacingExpressionVisitor(expression -> {
if (expression instanceof WasmMemoryAccess) {
WasmMemoryAccess memoryAccess = (WasmMemoryAccess) expression;
WasmCall call = new WasmCall(traceFunction.getName());
call.setImported(true);
call.getArguments().add(new WasmInt32Constant(positionHolder[0]++));
call.getArguments().add(memoryAccess.getIndex());
memoryAccess.setIndex(call);
}
return expression;
});
for (WasmFunction function : module.getFunctions().values()) {
visitor.replace(function);
}
}
}

View File

@ -10,6 +10,9 @@
<root url="file://$MODULE_DIR$/target/generated/js" relative="/" /> <root url="file://$MODULE_DIR$/target/generated/js" relative="/" />
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" /> <root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
</webroots> </webroots>
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
</sourceRoots>
</configuration> </configuration>
</facet> </facet>
</component> </component>