Skip to content

Instantly share code, notes, and snippets.

@hackcasual
Created October 31, 2016 18:08
Show Gist options
  • Save hackcasual/0e59c8f83a7b6a4788e5590ca82ca5e8 to your computer and use it in GitHub Desktop.
Save hackcasual/0e59c8f83a7b6a4788e5590ca82ca5e8 to your computer and use it in GitHub Desktop.
diff --git a/lib/Target/JSBackend/JSBackend.cpp b/lib/Target/JSBackend/JSBackend.cpp
index d1d83a4..bd0e6c4 100644
--- a/lib/Target/JSBackend/JSBackend.cpp
+++ b/lib/Target/JSBackend/JSBackend.cpp
@@ -152,6 +152,10 @@ WebAssembly("emscripten-wasm",
cl::desc("Generate asm.js which will later be compiled to WebAssembly (see emscripten BINARYEN setting)"),
cl::init(false));
+static cl::opt<bool>
+FunctionCoverage("function-coverage",
+ cl::desc("Injects coverage tracking into asm.js functions"),
+ cl::init(false));
extern "C" void LLVMInitializeJSBackendTarget() {
// Register the target.
@@ -2987,6 +2991,9 @@ void JSWriter::printFunctionBody(const Function *F) {
}
}
+ if (FunctionCoverage)
+ Out << "\n HEAP8[" << getGlobalAddress("func_cov") << ">>0] = 1|0;\n";
+
// Emit (relooped) code
char *buffer = Relooper::GetOutputBuffer();
nl(Out) << buffer;
@@ -3142,6 +3149,21 @@ void JSWriter::printFunction(const Function *F) {
}
void JSWriter::printModuleBody() {
+
+ if (FunctionCoverage) {
+ // Count functions
+ int function_count = 0;
+ for (Module::const_iterator II = TheModule->begin(), E = TheModule->end();
+ II != E; ++II) {
+ auto I = &*II;
+ if (!I->isDeclaration()) {
+ function_count++;
+ }
+ }
+
+ allocateZeroInitAddress("func_cov", 4, function_count);
+ }
+
processConstants();
if (Relocatable) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment