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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
// ExpressionRunner.Flags.Default = 0
// ExpressionRunner.Flags.PreserveSideeffects = 1
// ExpressionRunner.Flags.TraverseCalls = 2
// beginning a Binaryen API trace
#include <math.h>
#include <map>
#include "binaryen-c.h"
int main() {
std::map<size_t, BinaryenType> types;
std::map<size_t, BinaryenExpressionRef> expressions;
std::map<size_t, BinaryenFunctionRef> functions;
std::map<size_t, BinaryenGlobalRef> globals;
std::map<size_t, BinaryenEventRef> events;
std::map<size_t, BinaryenExportRef> exports;
std::map<size_t, RelooperBlockRef> relooperBlocks;
std::map<size_t, ExpressionRunnerRef> expressionRunners;
BinaryenModuleRef the_module = NULL;
RelooperRef the_relooper = NULL;
the_module = BinaryenModuleCreate();
expressions[size_t(NULL)] = BinaryenExpressionRef(NULL);
expressions[1] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
globals[0] = BinaryenAddGlobal(the_module, "aGlobal", BinaryenTypeInt32(), 1, expressions[1]);
expressionRunners[0] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[2] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[3] = BinaryenConst(the_module, BinaryenLiteralInt32(2));
expressions[4] = BinaryenBinary(the_module, 0, expressions[2], expressions[3]);
expressions[5] = ExpressionRunnerRunAndDispose(expressionRunners[0], expressions[4]);
BinaryenExpressionGetId(expressions[5]);
BinaryenExpressionGetType(expressions[5]);
BinaryenConstGetValueI32(expressions[5]);
expressionRunners[1] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[6] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[7] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
expressions[8] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
expressions[9] = BinaryenConst(the_module, BinaryenLiteralInt32(3));
expressions[10] = BinaryenIf(the_module, expressions[7], expressions[8], expressions[9]);
expressions[11] = BinaryenBinary(the_module, 0, expressions[6], expressions[10]);
expressions[12] = ExpressionRunnerRunAndDispose(expressionRunners[1], expressions[11]);
BinaryenExpressionGetId(expressions[12]);
BinaryenExpressionGetType(expressions[12]);
BinaryenConstGetValueI32(expressions[12]);
expressionRunners[2] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[13] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
expressions[14] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[15] = BinaryenBinary(the_module, 0, expressions[13], expressions[14]);
ExpressionRunnerRunAndDispose(expressionRunners[2], expressions[15]);
expressionRunners[3] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[16] = BinaryenUnreachable(the_module);
ExpressionRunnerRunAndDispose(expressionRunners[3], expressions[16]);
expressionRunners[4] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[17] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
expressions[18] = BinaryenLocalTee(the_module, 0, expressions[17], BinaryenTypeInt32());
expressions[19] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[20] = BinaryenBinary(the_module, 0, expressions[18], expressions[19]);
expressions[21] = ExpressionRunnerRunAndDispose(expressionRunners[4], expressions[20]);
BinaryenExpressionGetId(expressions[21]);
BinaryenExpressionGetType(expressions[21]);
BinaryenConstGetValueI32(expressions[21]);
expressionRunners[5] = ExpressionRunnerCreate(the_module, 1, 0, 0);
expressions[22] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
expressions[23] = BinaryenLocalTee(the_module, 0, expressions[22], BinaryenTypeInt32());
expressions[24] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[25] = BinaryenBinary(the_module, 0, expressions[23], expressions[24]);
ExpressionRunnerRunAndDispose(expressionRunners[5], expressions[25]);
expressionRunners[6] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[26] = BinaryenConst(the_module, BinaryenLiteralInt32(2));
expressions[27] = BinaryenLocalSet(the_module, 0, expressions[26]);
expressions[28] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
{
BinaryenExpressionRef children[] = { expressions[27], expressions[28] };
expressions[29] = BinaryenBlock(the_module, NULL, children, 2, BinaryenTypeInt32());
}
expressions[30] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
expressions[31] = BinaryenGlobalSet(the_module, "aGlobal", expressions[30]);
expressions[32] = BinaryenGlobalGet(the_module, "aGlobal", BinaryenTypeInt32());
{
BinaryenExpressionRef children[] = { expressions[31], expressions[32] };
expressions[33] = BinaryenBlock(the_module, NULL, children, 2, BinaryenTypeInt32());
}
expressions[34] = BinaryenBinary(the_module, 0, expressions[29], expressions[33]);
expressions[35] = ExpressionRunnerRunAndDispose(expressionRunners[6], expressions[34]);
BinaryenExpressionGetId(expressions[35]);
BinaryenExpressionGetType(expressions[35]);
BinaryenConstGetValueI32(expressions[35]);
expressionRunners[7] = ExpressionRunnerCreate(the_module, 1, 0, 0);
expressions[36] = BinaryenConst(the_module, BinaryenLiteralInt32(3));
ExpressionRunnerSetLocalValue(expressionRunners[7], 0, expressions[36]);
expressions[37] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
ExpressionRunnerSetGlobalValue(expressionRunners[7], "aGlobal", expressions[37]);
expressions[38] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
expressions[39] = BinaryenGlobalGet(the_module, "aGlobal", BinaryenTypeInt32());
expressions[40] = BinaryenBinary(the_module, 0, expressions[38], expressions[39]);
expressions[41] = ExpressionRunnerRunAndDispose(expressionRunners[7], expressions[40]);
BinaryenExpressionGetId(expressions[41]);
BinaryenExpressionGetType(expressions[41]);
BinaryenConstGetValueI32(expressions[41]);
expressionRunners[8] = ExpressionRunnerCreate(the_module, 2, 0, 0);
{
BinaryenType t0[] = {BinaryenTypeInt32(), BinaryenTypeInt32()};
types[0] = BinaryenTypeCreate(t0, 2);
}
expressions[42] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
expressions[43] = BinaryenLocalGet(the_module, 1, BinaryenTypeInt32());
expressions[44] = BinaryenBinary(the_module, 0, expressions[42], expressions[43]);
{
BinaryenExpressionRef children[] = { expressions[44] };
expressions[45] = BinaryenBlock(the_module, NULL, children, 1, BinaryenTypeInt32());
}
{
BinaryenType varTypes[] = { 0 };
functions[0] = BinaryenAddFunction(the_module, "add", types[0], BinaryenTypeInt32(), varTypes, 0, expressions[45]);
}
expressions[46] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
ExpressionRunnerSetLocalValue(expressionRunners[8], 0, expressions[46]);
expressions[47] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
expressions[48] = BinaryenConst(the_module, BinaryenLiteralInt32(2));
expressions[49] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
{
BinaryenExpressionRef operands[] = { expressions[48], expressions[49] };
expressions[50] = BinaryenCall(the_module, "add", operands, 2, BinaryenTypeInt32());
}
expressions[51] = BinaryenBinary(the_module, 0, expressions[47], expressions[50]);
expressions[52] = BinaryenLocalGet(the_module, 0, BinaryenTypeInt32());
expressions[53] = BinaryenBinary(the_module, 0, expressions[51], expressions[52]);
expressions[54] = ExpressionRunnerRunAndDispose(expressionRunners[8], expressions[53]);
BinaryenExpressionGetId(expressions[54]);
BinaryenExpressionGetType(expressions[54]);
BinaryenConstGetValueI32(expressions[54]);
expressionRunners[9] = ExpressionRunnerCreate(the_module, 0, 0, 0);
expressions[55] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[56] = BinaryenConst(the_module, BinaryenLiteralInt32(3));
expressions[57] = BinaryenConst(the_module, BinaryenLiteralInt32(4));
{
BinaryenExpressionRef operands[] = { expressions[56], expressions[57] };
expressions[58] = BinaryenCall(the_module, "add", operands, 2, BinaryenTypeInt32());
}
expressions[59] = BinaryenBinary(the_module, 0, expressions[55], expressions[58]);
ExpressionRunnerRunAndDispose(expressionRunners[9], expressions[59]);
expressionRunners[10] = ExpressionRunnerCreate(the_module, 0, 1, 0);
expressions[60] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
{
BinaryenExpressionRef children[] = { expressions[60] };
expressions[61] = BinaryenBlock(the_module, NULL, children, 1, BinaryenTypeInt32());
}
ExpressionRunnerRunAndDispose(expressionRunners[10], expressions[61]);
expressionRunners[11] = ExpressionRunnerCreate(the_module, 0, 50, 3);
expressions[62] = BinaryenBreak(the_module, "theLoop", expressions[0], expressions[0]);
expressions[63] = BinaryenLoop(the_module, "theLoop", expressions[62]);
ExpressionRunnerRunAndDispose(expressionRunners[11], expressions[63]);
BinaryenModuleDispose(the_module);
types.clear();
expressions.clear();
functions.clear();
globals.clear();
events.clear();
exports.clear();
relooperBlocks.clear();
expressionRunners.clear();
return 0;
}
// ending a Binaryen API trace
|