mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
WASM: Add option to trace all call_indirect opcodes
This commit is contained in:
parent
af10879a81
commit
1335476abe
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user