From 91e38285dc27e5dbac3291258342835fc3e90eab Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 11 Jul 2016 14:46:57 -0700 Subject: add a tracing option to the c api, which logs out a runnable program from c api calls --- test/example/c-api-kitchen-sink.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/example/c-api-kitchen-sink.c') diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 6fb095d20..4f1d839c0 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -500,10 +500,19 @@ void test_nonvalid() { BinaryenModuleDispose(module); } +void test_tracing() { + BinaryenSetAPITracing(1); + test_core(); + BinaryenSetAPITracing(0); +} + int main() { test_core(); test_relooper(); test_binaries(); test_interpret(); test_nonvalid(); + test_tracing(); + + return 0; } -- cgit v1.2.3 From f050b6300ceb6039d608abc20939ebf241378cdc Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 11 Jul 2016 15:22:58 -0700 Subject: allow multiple traces --- src/binaryen-c.cpp | 39 +++++++++++++++++++++++-------------- test/example/c-api-kitchen-sink.c | 9 +++++---- test/example/c-api-kitchen-sink.txt | 6 +----- 3 files changed, 30 insertions(+), 24 deletions(-) (limited to 'test/example/c-api-kitchen-sink.c') diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 20fefece0..89953e0ff 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -93,19 +93,7 @@ BinaryenType BinaryenFloat64(void) { return f64; } BinaryenModuleRef BinaryenModuleCreate(void) { if (tracing) { - std::cout << "// beginning a Binaryen API trace\n"; - std::cout << "#include \n"; - std::cout << "#include \n"; - std::cout << "#include \"src/binaryen-c.h\"\n"; - std::cout << "int main() {\n"; - std::cout << " std::map functionTypes;\n"; - std::cout << " std::map expressions;\n"; - std::cout << " expressions[size_t(NULL)] = BinaryenExpressionRef(NULL);\n"; - std::cout << " std::map functions;\n"; - std::cout << " std::map relooperBlocks;\n"; - std::cout << " BinaryenModuleRef the_module = BinaryenModuleCreate();\n"; - std::cout << " RelooperRef the_relooper = NULL;\n"; - expressions[NULL] = 0; + std::cout << " the_module = BinaryenModuleCreate();\n"; } return new Module(); @@ -113,8 +101,6 @@ BinaryenModuleRef BinaryenModuleCreate(void) { void BinaryenModuleDispose(BinaryenModuleRef module) { if (tracing) { std::cout << " BinaryenModuleDispose(the_module);\n"; - std::cout << " return 0;\n"; - std::cout << "}\n"; } delete (Module*)module; @@ -967,6 +953,29 @@ BinaryenExpressionRef RelooperRenderAndDispose(RelooperRef relooper, RelooperBlo void BinaryenSetAPITracing(int on) { tracing = on; + + if (tracing) { + std::cout << "// beginning a Binaryen API trace\n"; + std::cout << "#include \n"; + std::cout << "#include \n"; + std::cout << "#include \"src/binaryen-c.h\"\n"; + std::cout << "int main() {\n"; + std::cout << " std::map functionTypes;\n"; + std::cout << " std::map expressions;\n"; + std::cout << " expressions[size_t(NULL)] = BinaryenExpressionRef(NULL);\n"; + std::cout << " std::map functions;\n"; + std::cout << " std::map relooperBlocks;\n"; + std::cout << " BinaryenModuleRef the_module = BinaryenModuleCreate();\n"; + std::cout << " RelooperRef the_relooper = NULL;\n"; + expressions[NULL] = 0; + } else { + std::cout << " return 0;\n"; + std::cout << "}\n"; + functionTypes.clear(); + expressions.clear(); + functions.clear(); + relooperBlocks.clear(); + } } } // extern "C" diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 4f1d839c0..ddfffb0d7 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -53,15 +53,15 @@ BinaryenExpressionRef makeSomething(BinaryenModuleRef module) { // tests -void test_core() { - - // Core types - +void test_types() { printf("BinaryenNone: %d\n", BinaryenNone()); printf("BinaryenInt32: %d\n", BinaryenInt32()); printf("BinaryenInt64: %d\n", BinaryenInt64()); printf("BinaryenFloat32: %d\n", BinaryenFloat32()); printf("BinaryenFloat64: %d\n", BinaryenFloat64()); +} + +void test_core() { // Module creation @@ -507,6 +507,7 @@ void test_tracing() { } int main() { + test_types(); test_core(); test_relooper(); test_binaries(); diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index e6e266f02..9aab653ac 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -817,11 +817,6 @@ module loaded from binary form: ) ) validation: 0 -BinaryenNone: 0 -BinaryenInt32: 1 -BinaryenInt64: 2 -BinaryenFloat32: 3 -BinaryenFloat64: 4 // beginning a Binaryen API trace #include #include @@ -834,6 +829,7 @@ int main() { std::map relooperBlocks; BinaryenModuleRef the_module = BinaryenModuleCreate(); RelooperRef the_relooper = NULL; + the_module = BinaryenModuleCreate(); expressions[1] = BinaryenConst(the_module, BinaryenLiteralInt32(1)); expressions[2] = BinaryenConst(the_module, BinaryenLiteralInt64(2)); expressions[3] = BinaryenConst(the_module, BinaryenLiteralFloat32(3.14)); -- cgit v1.2.3 From 9d9211beb0c1b1d66232eefba74b11987d02bbcc Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 11 Jul 2016 15:38:06 -0700 Subject: relooper tracing + fixes --- check.py | 2 +- src/binaryen-c.cpp | 89 ++- test/example/c-api-kitchen-sink.c | 53 +- test/example/c-api-kitchen-sink.txt | 1138 +++++++++++++++++++++++++------ test/example/c-api-kitchen-sink.txt.txt | 414 +++++++++++ 5 files changed, 1410 insertions(+), 286 deletions(-) (limited to 'test/example/c-api-kitchen-sink.c') diff --git a/check.py b/check.py index 72d585d5e..658746dff 100755 --- a/check.py +++ b/check.py @@ -164,7 +164,7 @@ def run_command(cmd, expected_status=0, stderr=None): def fail(actual, expected): raise Exception("incorrect output, diff:\n\n%s" % ( - ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(expected.split('\n'), actual.split('\n'), fromfile='expected', tofile='actual')])[-1000:] + ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(expected.split('\n'), actual.split('\n'), fromfile='expected', tofile='actual')])[:] )) def fail_if_not_identical(actual, expected): diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 89953e0ff..cdc1d6612 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -74,6 +74,12 @@ std::map expressions; std::map functions; std::map relooperBlocks; +size_t noteExpression(BinaryenExpressionRef expression) { + auto id = expressions.size(); + assert(expressions.find(expression) == expressions.end()); + expressions[expression] = id; + return id; +} extern "C" { @@ -94,6 +100,8 @@ BinaryenType BinaryenFloat64(void) { return f64; } BinaryenModuleRef BinaryenModuleCreate(void) { if (tracing) { std::cout << " the_module = BinaryenModuleCreate();\n"; + std::cout << " expressions[size_t(NULL)] = BinaryenExpressionRef(NULL);\n"; + expressions[NULL] = 0; } return new Module(); @@ -101,6 +109,14 @@ BinaryenModuleRef BinaryenModuleCreate(void) { void BinaryenModuleDispose(BinaryenModuleRef module) { if (tracing) { std::cout << " BinaryenModuleDispose(the_module);\n"; + std::cout << " functionTypes.clear();\n"; + std::cout << " expressions.clear();\n"; + std::cout << " functions.clear();\n"; + std::cout << " relooperBlocks.clear();\n"; + functionTypes.clear(); + expressions.clear(); + functions.clear(); + relooperBlocks.clear(); } delete (Module*)module; @@ -297,8 +313,7 @@ BinaryenExpressionRef BinaryenBlock(BinaryenModuleRef module, const char* name, std::cout << "expressions[" << expressions[children[i]] << "]"; } std::cout << " };\n"; - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenBlock(the_module, "; traceNameOrNULL(name); std::cout << ", children, sizeof(children) / sizeof(BinaryenExpressionRef));\n"; @@ -315,8 +330,7 @@ BinaryenExpressionRef BinaryenIf(BinaryenModuleRef module, BinaryenExpressionRef ret->finalize(); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenIf(the_module, expressions[" << expressions[condition] << "], expressions[" << expressions[ifTrue] << "], expressions[" << expressions[ifFalse] << "]);\n"; } @@ -327,8 +341,7 @@ BinaryenExpressionRef BinaryenLoop(BinaryenModuleRef module, const char* out, co auto* ret = Builder(*((Module*)module)).makeLoop(out ? Name(out) : Name(), in ? Name(in) : Name(), (Expression*)body); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenLoop(the_module, "; traceNameOrNULL(out); std::cout << ", "; @@ -342,8 +355,7 @@ BinaryenExpressionRef BinaryenBreak(BinaryenModuleRef module, const char* name, auto* ret = Builder(*((Module*)module)).makeBreak(name, (Expression*)value, (Expression*)condition); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenBreak(the_module, \"" << name << "\", expressions[" << expressions[condition] << "], expressions[" << expressions[value] << "]);\n"; } @@ -360,8 +372,7 @@ BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **name std::cout << "\"" << names[i] << "\""; } std::cout << " };\n"; - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenSwitch(the_module, names, sizeof(names) / sizeof(const char *), \"" << defaultName << "\", expressions[" << expressions[condition] << "], expressions[" << expressions[value] << "]);\n"; std::cout << " }\n"; } @@ -386,8 +397,7 @@ BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, std::cout << "expressions[" << expressions[operands[i]] << "]"; } std::cout << " };\n"; - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenCall(the_module, \"" << target << "\", operands, " << numOperands << ", " << returnType << ");\n"; std::cout << " }\n"; } @@ -411,8 +421,7 @@ BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *t std::cout << "expressions[" << expressions[operands[i]] << "]"; } std::cout << " };\n"; - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenCallImport(the_module, \"" << target << "\", operands, " << numOperands << ", " << returnType << ");\n"; std::cout << " }\n"; } @@ -437,8 +446,7 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module, BinaryenExp std::cout << "expressions[" << expressions[operands[i]] << "]"; } std::cout << " };\n"; - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenCallIndirect(the_module, expressions[" << expressions[target] << "], operands, " << numOperands << ", \"" << type << "\");\n"; std::cout << " }\n"; } @@ -455,8 +463,7 @@ BinaryenExpressionRef BinaryenGetLocal(BinaryenModuleRef module, BinaryenIndex i auto* ret = ((Module*)module)->allocator.alloc(); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenGetLocal(the_module, " << index << ", " << type << ");\n"; } @@ -469,8 +476,7 @@ BinaryenExpressionRef BinaryenSetLocal(BinaryenModuleRef module, BinaryenIndex i auto* ret = ((Module*)module)->allocator.alloc(); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenSetLocal(the_module, " << index << ", expressions[" << expressions[value] << "]);\n"; } @@ -483,8 +489,7 @@ BinaryenExpressionRef BinaryenLoad(BinaryenModuleRef module, uint32_t bytes, int auto* ret = ((Module*)module)->allocator.alloc(); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenLoad(the_module, " << bytes << ", " << int(signed_) << ", " << offset << ", " << align << ", " << type << ", expressions[" << expressions[ptr] << "]);\n"; } @@ -501,8 +506,7 @@ BinaryenExpressionRef BinaryenStore(BinaryenModuleRef module, uint32_t bytes, ui auto* ret = ((Module*)module)->allocator.alloc(); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenStore(the_module, " << bytes << ", " << offset << ", " << align << ", expressions[" << expressions[ptr] << "], expressions[" << expressions[value] << "]);\n"; } @@ -517,8 +521,7 @@ BinaryenExpressionRef BinaryenStore(BinaryenModuleRef module, uint32_t bytes, ui BinaryenExpressionRef BinaryenConst(BinaryenModuleRef module, BinaryenLiteral value) { auto* ret = Builder(*((Module*)module)).makeConst(fromBinaryenLiteral(value)); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); switch (value.type) { case WasmType::i32: std::cout << " expressions[" << id << "] = BinaryenConst(the_module, BinaryenLiteralInt32(" << value.i32 << "));\n"; break; case WasmType::i64: std::cout << " expressions[" << id << "] = BinaryenConst(the_module, BinaryenLiteralInt64(" << value.i64 << "));\n"; break; @@ -545,8 +548,7 @@ BinaryenExpressionRef BinaryenUnary(BinaryenModuleRef module, BinaryenOp op, Bin auto* ret = Builder(*((Module*)module)).makeUnary(UnaryOp(op), (Expression*)value); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenUnary(the_module, " << op << ", expressions[" << expressions[value] << "]);\n"; } @@ -556,8 +558,7 @@ BinaryenExpressionRef BinaryenBinary(BinaryenModuleRef module, BinaryenOp op, Bi auto* ret = Builder(*((Module*)module)).makeBinary(BinaryOp(op), (Expression*)left, (Expression*)right); if (tracing) { - auto id = expressions.size(); - expressions[ret] = id; + auto id = noteExpression(ret); std::cout << " expressions[" << id << "] = BinaryenBinary(the_module, " << op << ", expressions[" << expressions[left] << "], expressions[" << expressions[right] << "]);\n"; } @@ -567,8 +568,7 @@ BinaryenExpressionRef BinaryenSelect(BinaryenModuleRef module, BinaryenExpressio auto* ret = ((Module*)module)->allocator.alloc