summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/example/c-api-kitchen-sink.c125
-rw-r--r--test/example/c-api-kitchen-sink.txt187
2 files changed, 310 insertions, 2 deletions
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index bfcb1ddea..28fd901a4 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -46,9 +46,9 @@ BinaryenExpressionRef makeSomething(BinaryenModuleRef module) {
return makeInt32(module, 1337);
}
-// main
+// tests
-int main() {
+void test_core() {
// Core types
@@ -221,3 +221,124 @@ int main() {
BinaryenModuleDispose(module);
}
+void test_relooper() {
+ BinaryenModuleRef module = BinaryenModuleCreate();
+ BinaryenFunctionTypeRef v = BinaryenAddFunctionType(module, "v", BinaryenNone(), NULL, 0);
+ BinaryenType localTypes[] = { BinaryenInt32() };
+
+ { // trivial: just one block
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block = RelooperAddBlock(relooper, makeSomething(module));
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "just-one-block", v, localTypes, 1, body);
+ }
+ { // two blocks
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperAddBranch(block0, block1, NULL, NULL); // no condition, no code on branch
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "two-blocks", v, localTypes, 1, body);
+ }
+ { // two blocks with code between them
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperAddBranch(block0, block1, NULL, makeInt32(module, 77)); // code on branch
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "two-blocks-plus-code", v, localTypes, 1, body);
+ }
+ { // two blocks in a loop
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperAddBranch(block0, block1, NULL, NULL);
+ RelooperAddBranch(block1, block0, NULL, NULL);
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "loop", v, localTypes, 1, body);
+ }
+ { // two blocks in a loop with codes
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperAddBranch(block0, block1, NULL, makeInt32(module, 33));
+ RelooperAddBranch(block1, block0, NULL, makeInt32(module, -66));
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "loop-plus-code", v, localTypes, 1, body);
+ }
+ { // split
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperAddBranch(block0, block1, makeInt32(module, 55), NULL);
+ RelooperAddBranch(block0, block2, NULL, NULL);
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "split", v, localTypes, 1, body);
+ }
+ { // split + code
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperAddBranch(block0, block1, makeInt32(module, 55), makeInt32(module, 10));
+ RelooperAddBranch(block0, block2, NULL, makeInt32(module, 20));
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "split-plus-code", v, localTypes, 1, body);
+ }
+ { // if
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperAddBranch(block0, block1, makeInt32(module, 55), NULL);
+ RelooperAddBranch(block0, block2, NULL, NULL);
+ RelooperAddBranch(block1, block2, NULL, NULL);
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "if", v, localTypes, 1, body);
+ }
+ { // if + code
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperAddBranch(block0, block1, makeInt32(module, 55), makeInt32(module, -1));
+ RelooperAddBranch(block0, block2, NULL, makeInt32(module, -2));
+ RelooperAddBranch(block1, block2, NULL, makeInt32(module, -3));
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "if-plus-code", v, localTypes, 1, body);
+ }
+ { // if-else
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperBlockRef block3 = RelooperAddBlock(relooper, makeInt32(module, 3));
+ RelooperAddBranch(block0, block1, makeInt32(module, 55), NULL);
+ RelooperAddBranch(block0, block2, NULL, NULL);
+ RelooperAddBranch(block1, block3, NULL, NULL);
+ RelooperAddBranch(block2, block3, NULL, NULL);
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "if-else", v, localTypes, 1, body);
+ }
+ { // loop+tail
+ RelooperRef relooper = RelooperCreate();
+ RelooperBlockRef block0 = RelooperAddBlock(relooper, makeInt32(module, 0));
+ RelooperBlockRef block1 = RelooperAddBlock(relooper, makeInt32(module, 1));
+ RelooperBlockRef block2 = RelooperAddBlock(relooper, makeInt32(module, 2));
+ RelooperAddBranch(block0, block1, NULL, NULL);
+ RelooperAddBranch(block1, block0, makeInt32(module, 10), NULL);
+ RelooperAddBranch(block1, block2, NULL, NULL);
+ BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block0, 0, module);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "loop-tail", v, localTypes, 1, body);
+ }
+
+ BinaryenModulePrint(module);
+ BinaryenModuleDispose(module);
+}
+
+int main() {
+ test_core();
+ test_relooper();
+}
+
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 573d159ed..734038a74 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -346,3 +346,190 @@ BinaryenFloat64: 4
)
)
)
+(module
+ (memory 0)
+ (type $v (func))
+ (func $just-one-block (type $v)
+ (local $0 i32)
+ (i32.const 1337)
+ )
+ (func $two-blocks (type $v)
+ (local $0 i32)
+ (block
+ (i32.const 0)
+ )
+ (block
+ (i32.const 1)
+ )
+ )
+ (func $two-blocks-plus-code (type $v)
+ (local $0 i32)
+ (block
+ (i32.const 0)
+ (block
+ (i32.const 77)
+ )
+ )
+ (block
+ (i32.const 1)
+ )
+ )
+ (func $loop (type $v)
+ (local $0 i32)
+ (loop $shape$0$break $shape$0$continue
+ (block
+ (i32.const 0)
+ )
+ (block
+ (i32.const 1)
+ (block
+ (br $shape$0$continue)
+ )
+ )
+ )
+ )
+ (func $loop-plus-code (type $v)
+ (local $0 i32)
+ (loop $shape$0$break $shape$0$continue
+ (block
+ (i32.const 0)
+ (block
+ (i32.const 33)
+ )
+ )
+ (block
+ (i32.const 1)
+ (block
+ (i32.const -66)
+ (br $shape$0$continue)
+ )
+ )
+ )
+ )
+ (func $split (type $v)
+ (local $0 i32)
+ (i32.const 0)
+ (block $shape$1$break
+ (if
+ (i32.const 55)
+ (block
+ (i32.const 1)
+ )
+ (block
+ (i32.const 2)
+ )
+ )
+ )
+ )
+ (func $split-plus-code (type $v)
+ (local $0 i32)
+ (i32.const 0)
+ (block $shape$1$break
+ (if
+ (i32.const 55)
+ (block
+ (i32.const 10)
+ (block
+ (i32.const 1)
+ )
+ )
+ (block
+ (i32.const 20)
+ (block
+ (i32.const 2)
+ )
+ )
+ )
+ )
+ )
+ (func $if (type $v)
+ (local $0 i32)
+ (block
+ (i32.const 0)
+ (block $shape$1$break
+ (if
+ (i32.const 55)
+ (block
+ (i32.const 1)
+ (block
+ (br $shape$1$break)
+ )
+ )
+ )
+ )
+ )
+ (block
+ (i32.const 2)
+ )
+ )
+ (func $if-plus-code (type $v)
+ (local $0 i32)
+ (block
+ (i32.const 0)
+ (block $shape$1$break
+ (if
+ (i32.const 55)
+ (block
+ (i32.const -1)
+ (block
+ (i32.const 1)
+ (block
+ (i32.const -3)
+ (br $shape$1$break)
+ )
+ )
+ )
+ (i32.const -2)
+ )
+ )
+ )
+ (block
+ (i32.const 2)
+ )
+ )
+ (func $if-else (type $v)
+ (local $0 i32)
+ (block
+ (i32.const 0)
+ (block $shape$1$break
+ (if
+ (i32.const 55)
+ (block
+ (i32.const 1)
+ (block
+ (br $shape$1$break)
+ )
+ )
+ (block
+ (i32.const 2)
+ (block
+ (br $shape$1$break)
+ )
+ )
+ )
+ )
+ )
+ (block
+ (i32.const 3)
+ )
+ )
+ (func $loop-tail (type $v)
+ (local $0 i32)
+ (loop $shape$0$break $shape$0$continue
+ (block
+ (i32.const 0)
+ )
+ (block
+ (i32.const 1)
+ (if
+ (i32.const 10)
+ (br $shape$0$continue)
+ (br $shape$0$break)
+ )
+ )
+ )
+ (block
+ (i32.const 2)
+ )
+ )
+)