summaryrefslogtreecommitdiff
path: root/test/example
diff options
context:
space:
mode:
authorAbbas Mashayekh <martianboy2005@gmail.com>2021-02-10 01:17:28 +0330
committerGitHub <noreply@github.com>2021-02-09 13:47:28 -0800
commit3da8b08ecd57f5662bebc69ea73bf59e1928341e (patch)
tree2902eedc161579eaf37a1aed463de95916eee703 /test/example
parenta12a8250da24aa5b5787bf89562b243fdc514302 (diff)
downloadbinaryen-3da8b08ecd57f5662bebc69ea73bf59e1928341e.tar.gz
binaryen-3da8b08ecd57f5662bebc69ea73bf59e1928341e.tar.bz2
binaryen-3da8b08ecd57f5662bebc69ea73bf59e1928341e.zip
[reference-types] remove single table restriction in IR (#3517)
Adds support for modules with multiple tables. Adds a field for the table name to `CallIndirect` and updates the C/JS APIs accordingly.
Diffstat (limited to 'test/example')
-rw-r--r--test/example/c-api-kitchen-sink.c5
-rw-r--r--test/example/c-api-kitchen-sink.txt6
-rw-r--r--test/example/c-api-multiple-tables.c90
-rw-r--r--test/example/c-api-multiple-tables.txt20
-rw-r--r--test/example/module-splitting.txt16
5 files changed, 126 insertions, 11 deletions
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index 907ab58c9..b45039c69 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -318,6 +318,8 @@ void test_core() {
BinaryenAddEvent(
module, "a-event", 0, BinaryenTypeInt32(), BinaryenTypeNone());
+ BinaryenAddTable(module, "tab", 0, 100, NULL, 0, makeInt32(module, 0));
+
// Exception handling
// (try
@@ -680,6 +682,7 @@ void test_core() {
BinaryenUnary(module,
BinaryenEqZInt32(), // check the output type of the call node
BinaryenCallIndirect(module,
+ "tab",
makeInt32(module, 2449),
callOperands4b,
4,
@@ -704,6 +707,7 @@ void test_core() {
BinaryenReturnCall(
module, "kitchen()sinker", callOperands4, 4, BinaryenTypeInt32()),
BinaryenReturnCallIndirect(module,
+ "tab",
makeInt32(module, 2449),
callOperands4b,
4,
@@ -840,6 +844,7 @@ void test_core() {
void test_unreachable() {
BinaryenModuleRef module = BinaryenModuleCreate();
BinaryenExpressionRef body = BinaryenCallIndirect(module,
+ "invalid-table",
BinaryenUnreachable(module),
NULL,
0,
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 8b06c1109..f03e4ec6c 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -38,7 +38,7 @@ BinaryenFeatureAll: 8191
(memory $0 (shared 1 256))
(data (i32.const 10) "hello, world")
(data passive "I am passive")
- (table $0 1 1 funcref)
+ (table $tab 1 1 funcref)
(elem (i32.const 0) "$kitchen()sinker")
(global $a-global i32 (i32.const 7))
(global $a-mutable-global (mut f32) (f32.const 7.5))
@@ -1666,7 +1666,7 @@ BinaryenFeatureAll: 8191
)
(drop
(i32.eqz
- (call_indirect (type $i32_i64_f32_f64_=>_i32)
+ (call_indirect $tab (type $i32_i64_f32_f64_=>_i32)
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
@@ -1730,7 +1730,7 @@ BinaryenFeatureAll: 8191
(f32.const 1.2999999523162842)
(f64.const 3.7)
)
- (return_call_indirect (type $i32_i64_f32_f64_=>_i32)
+ (return_call_indirect $tab (type $i32_i64_f32_f64_=>_i32)
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
diff --git a/test/example/c-api-multiple-tables.c b/test/example/c-api-multiple-tables.c
new file mode 100644
index 000000000..4ba612658
--- /dev/null
+++ b/test/example/c-api-multiple-tables.c
@@ -0,0 +1,90 @@
+#include <assert.h>
+#include <string.h>
+#include <binaryen-c.h>
+
+// "hello world" type example: create a function that adds two i32s and returns
+// the result
+
+int main() {
+ BinaryenModuleRef module = BinaryenModuleCreate();
+ BinaryenModuleSetFeatures(module, BinaryenFeatureReferenceTypes());
+
+ // Create a function type for i32 (i32, i32)
+ BinaryenType ii[2] = {BinaryenTypeInt32(), BinaryenTypeInt32()};
+ BinaryenType params = BinaryenTypeCreate(ii, 2);
+ BinaryenType results = BinaryenTypeInt32();
+
+ assert(BinaryenGetNumTables(module) == 0);
+
+ {
+ // Get the 0 and 1 arguments, and add them
+ BinaryenExpressionRef x = BinaryenLocalGet(module, 0, BinaryenTypeInt32()),
+ y = BinaryenLocalGet(module, 1, BinaryenTypeInt32());
+ BinaryenExpressionRef add =
+ BinaryenBinary(module, BinaryenAddInt32(), x, y);
+
+ // Create the add function
+ // Note: no additional local variables
+ // Note: no basic blocks here, we are an AST. The function body is just an
+ // expression node.
+ BinaryenFunctionRef adder =
+ BinaryenAddFunction(module, "adder", params, results, NULL, 0, add);
+
+ const char* funcNames[] = {"adder"};
+ BinaryenAddTable(module,
+ "tab",
+ 1,
+ 1,
+ funcNames,
+ 1,
+ BinaryenConst(module, BinaryenLiteralInt32(0)));
+ assert(BinaryenGetTable(module, "tab") != NULL);
+
+ BinaryenAddTable(module,
+ "t2",
+ 1,
+ 1,
+ funcNames,
+ 1,
+ BinaryenConst(module, BinaryenLiteralInt32(0)));
+ BinaryenTableRef t2 = BinaryenGetTableByIndex(module, 1);
+ assert(t2 != NULL);
+
+ assert(strcmp(BinaryenTableGetName(t2), "t2") == 0);
+ assert(BinaryenTableGetInitial(t2) == 1);
+ assert(BinaryenTableHasMax(t2) == 1);
+ assert(BinaryenTableGetMax(t2) == 1);
+ assert(strcmp(BinaryenTableImportGetModule(t2), "") == 0);
+ assert(strcmp(BinaryenTableImportGetBase(t2), "") == 0);
+
+ assert(BinaryenGetNumTables(module) == 2);
+ }
+
+ {
+ // Get the 0 and 1 arguments, and add them
+ BinaryenExpressionRef operands[] = {
+ BinaryenLocalGet(module, 0, BinaryenTypeInt32()),
+ BinaryenLocalGet(module, 1, BinaryenTypeInt32())};
+
+ BinaryenExpressionRef add_indirect =
+ BinaryenCallIndirect(module,
+ "tab",
+ BinaryenConst(module, BinaryenLiteralInt32(0)),
+ operands,
+ 2,
+ params,
+ results);
+ BinaryenCallIndirectSetTable(add_indirect, "t2");
+
+ BinaryenFunctionRef call_adder_indirectly = BinaryenAddFunction(
+ module, "call_adder_indirect", params, results, NULL, 0, add_indirect);
+ }
+
+ // Print it out
+ BinaryenModulePrint(module);
+
+ // Clean up the module, which owns all the objects we created above
+ BinaryenModuleDispose(module);
+
+ return 0;
+}
diff --git a/test/example/c-api-multiple-tables.txt b/test/example/c-api-multiple-tables.txt
new file mode 100644
index 000000000..c0f2da53b
--- /dev/null
+++ b/test/example/c-api-multiple-tables.txt
@@ -0,0 +1,20 @@
+(module
+ (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
+ (table $tab 1 1 funcref)
+ (elem (table $tab) (i32.const 0) func $adder)
+ (table $t2 1 1 funcref)
+ (elem (table $t2) (i32.const 0) func $adder)
+ (func $adder (param $0 i32) (param $1 i32) (result i32)
+ (i32.add
+ (local.get $0)
+ (local.get $1)
+ )
+ )
+ (func $call_adder_indirect (param $0 i32) (param $1 i32) (result i32)
+ (call_indirect $t2 (type $i32_i32_=>_i32)
+ (local.get $0)
+ (local.get $1)
+ (i32.const 0)
+ )
+ )
+)
diff --git a/test/example/module-splitting.txt b/test/example/module-splitting.txt
index 32d579c2c..93a766652 100644
--- a/test/example/module-splitting.txt
+++ b/test/example/module-splitting.txt
@@ -311,7 +311,7 @@ After:
(export "foo" (func $foo))
(export "%table" (table $0))
(func $foo (param $0 i32) (result i32)
- (call_indirect (type $i32_=>_i32)
+ (call_indirect $0 (type $i32_=>_i32)
(local.get $0)
(i32.const 0)
)
@@ -377,7 +377,7 @@ After:
(export "foo" (func $foo))
(export "%table" (table $table))
(func $foo (param $0 i32) (result i32)
- (call_indirect (type $i32_=>_i32)
+ (call_indirect $table (type $i32_=>_i32)
(local.get $0)
(i32.const 42)
)
@@ -417,7 +417,7 @@ After:
(export "%table" (table $table))
(export "%global" (global $base))
(func $foo (param $0 i32) (result i32)
- (call_indirect (type $i32_=>_i32)
+ (call_indirect $table (type $i32_=>_i32)
(local.get $0)
(global.get $base)
)
@@ -467,7 +467,7 @@ After:
(nop)
)
(func $foo (param $0 i32) (result i32)
- (call_indirect (type $i32_=>_i32)
+ (call_indirect $table (type $i32_=>_i32)
(local.get $0)
(i32.add
(global.get $base)
@@ -564,7 +564,7 @@ After:
(elem (i32.const 0) $placeholder_0)
(export "%table" (table $0))
(func $foo
- (call_indirect (type $none_=>_none)
+ (call_indirect $0 (type $none_=>_none)
(i32.const 0)
)
)
@@ -631,7 +631,7 @@ After:
(nop)
)
(func $bar
- (call_indirect (type $none_=>_none)
+ (call_indirect $0 (type $none_=>_none)
(i32.const 0)
)
)
@@ -923,7 +923,7 @@ After:
(export "%foo" (func $foo))
(export "%table" (table $table))
(func $foo (param $0 i32) (result i32)
- (call_indirect (type $i32_=>_i32)
+ (call_indirect $table (type $i32_=>_i32)
(i32.const 0)
(i32.const 1)
)
@@ -963,7 +963,7 @@ After:
(export "foo2" (func $foo))
(export "%table" (table $0))
(func $foo
- (call_indirect (type $none_=>_none)
+ (call_indirect $0 (type $none_=>_none)
(i32.const 0)
)
)