WASM: Add option to trace all call_indirect opcodes

This commit is contained in:
Alexey Andreev 2016-10-27 19:12:37 +03:00
parent af10879a81
commit 1335476abe
2 changed files with 62 additions and 0 deletions

View File

@ -74,6 +74,7 @@ import org.teavm.backend.wasm.render.WasmBinaryVersion;
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.IndirectCallTraceTransformation;
import org.teavm.backend.wasm.transformation.MemoryAccessTraceTransformation; 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;
@ -374,6 +375,9 @@ public class WasmTarget implements TeaVMTarget {
if (Boolean.parseBoolean(System.getProperty("wasm.memoryTrace", "false"))) { if (Boolean.parseBoolean(System.getProperty("wasm.memoryTrace", "false"))) {
new MemoryAccessTraceTransformation(module).apply(); new MemoryAccessTraceTransformation(module).apply();
} }
if (Boolean.parseBoolean(System.getProperty("wasm.indirectCallTrace", "false"))) {
new IndirectCallTraceTransformation(module).apply();
}
WasmBinaryWriter writer = new WasmBinaryWriter(); WasmBinaryWriter writer = new WasmBinaryWriter();
WasmBinaryRenderer renderer = new WasmBinaryRenderer(writer, version); WasmBinaryRenderer renderer = new WasmBinaryRenderer(writer, version);

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.WasmIndirectCall;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmReplacingExpressionVisitor;
public class IndirectCallTraceTransformation {
private WasmModule module;
public IndirectCallTraceTransformation(WasmModule module) {
this.module = module;
}
public void apply() {
WasmFunction traceFunction = new WasmFunction("traceIndirectCall");
traceFunction.setImportModule("debug");
traceFunction.setImportName("traceIndirectCall");
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 WasmIndirectCall) {
WasmIndirectCall indirectCall = (WasmIndirectCall) expression;
WasmCall call = new WasmCall(traceFunction.getName());
call.setImported(true);
call.getArguments().add(new WasmInt32Constant(positionHolder[0]++));
call.getArguments().add(indirectCall.getSelector());
indirectCall.setSelector(call);
}
return expression;
});
for (WasmFunction function : module.getFunctions().values()) {
visitor.replace(function);
}
}
}