summaryrefslogtreecommitdiff
path: root/test/example/c-api-multiple-tables.c
blob: 4ba612658bc2ea737fa7905bd587df9a59861117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}