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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#include <stdio.h>
#include <stdlib.h>
#include <binaryen-c.h>
// kitchen sink, tests the full API
// helpers
BinaryenExpressionRef makeUnary(BinaryenModuleRef module, BinaryenOp op, BinaryenType inputType) {
if (inputType == BinaryenInt32()) return BinaryenUnary(module, op, BinaryenConst(module, BinaryenLiteralInt32(-10)));
if (inputType == BinaryenInt64()) return BinaryenUnary(module, op, BinaryenConst(module, BinaryenLiteralInt64(-22)));
if (inputType == BinaryenFloat32()) return BinaryenUnary(module, op, BinaryenConst(module, BinaryenLiteralFloat32(-33.612)));
if (inputType == BinaryenFloat64()) return BinaryenUnary(module, op, BinaryenConst(module, BinaryenLiteralFloat64(-9005.841)));
abort();
}
BinaryenExpressionRef makeBinary(BinaryenModuleRef module, BinaryenOp op, BinaryenType type) {
if (type == BinaryenInt32()) return BinaryenBinary(module, op, BinaryenConst(module, BinaryenLiteralInt32(-10)), BinaryenConst(module, BinaryenLiteralInt32(-11)));
if (type == BinaryenInt64()) return BinaryenBinary(module, op, BinaryenConst(module, BinaryenLiteralInt64(-22)), BinaryenConst(module, BinaryenLiteralInt64(-23)));
if (type == BinaryenFloat32()) return BinaryenBinary(module, op, BinaryenConst(module, BinaryenLiteralFloat32(-33.612)), BinaryenConst(module, BinaryenLiteralFloat32(-62.5)));
if (type == BinaryenFloat64()) return BinaryenBinary(module, op, BinaryenConst(module, BinaryenLiteralFloat64(-9005.841)), BinaryenConst(module, BinaryenLiteralFloat64(-9007.333)));
abort();
}
BinaryenExpressionRef makeInt32(BinaryenModuleRef module, int x) {
return BinaryenConst(module, BinaryenLiteralInt32(x));
}
BinaryenExpressionRef makeFloat32(BinaryenModuleRef module, float x) {
return BinaryenConst(module, BinaryenLiteralFloat32(x));
}
BinaryenExpressionRef makeInt64(BinaryenModuleRef module, int64_t x) {
return BinaryenConst(module, BinaryenLiteralInt64(x));
}
BinaryenExpressionRef makeFloat64(BinaryenModuleRef module, double x) {
return BinaryenConst(module, BinaryenLiteralFloat64(x));
}
BinaryenExpressionRef makeSomething(BinaryenModuleRef module) {
return makeInt32(module, 1337);
}
// tests
void test_core() {
// Core 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());
// Module creation
BinaryenModuleRef module = BinaryenModuleCreate();
// Literals and consts
BinaryenExpressionRef constI32 = BinaryenConst(module, BinaryenLiteralInt32(1)),
constI64 = BinaryenConst(module, BinaryenLiteralInt64(2)),
constF32 = BinaryenConst(module, BinaryenLiteralFloat32(3.14)),
constF64 = BinaryenConst(module, BinaryenLiteralFloat64(2.1828)),
constF32Bits = BinaryenConst(module, BinaryenLiteralFloat32Bits(0xffff1234)),
constF64Bits = BinaryenConst(module, BinaryenLiteralFloat64Bits(0xffff12345678abcdLL));
const char* switchNames[] = { "the-body" };
BinaryenExpressionRef callOperands2[] = { makeInt32(module, 13), makeFloat64(module, 3.7) };
BinaryenExpressionRef callOperands4[] = { makeInt32(module, 13), makeInt64(module, 37), makeFloat32(module, 1.3), makeFloat64(module, 3.7) };
BinaryenType params[4] = { BinaryenInt32(), BinaryenInt64(), BinaryenFloat32(), BinaryenFloat64() };
BinaryenFunctionTypeRef iiIfF = BinaryenAddFunctionType(module, "iiIfF", BinaryenInt32(), params, 4);
BinaryenExpressionRef bodyList[] = {
// Unary
makeUnary(module, BinaryenClz(), 1),
makeUnary(module, BinaryenCtz(), 2),
makeUnary(module, BinaryenPopcnt(), 1),
makeUnary(module, BinaryenNeg(), 3),
makeUnary(module, BinaryenAbs(), 4),
makeUnary(module, BinaryenCeil(), 3),
makeUnary(module, BinaryenFloor(), 4),
makeUnary(module, BinaryenTrunc(), 3),
makeUnary(module, BinaryenNearest(), 3),
makeUnary(module, BinaryenSqrt(), 4),
makeUnary(module, BinaryenEqZ(), 1),
makeUnary(module, BinaryenExtendSInt32(), 1),
makeUnary(module, BinaryenExtentUInt32(), 1),
makeUnary(module, BinaryenWrapInt64(), 2),
makeUnary(module, BinaryenTruncSFloat32ToInt32(), 3),
makeUnary(module, BinaryenTruncSFloat32ToInt64(), 3),
makeUnary(module, BinaryenTruncUFloat32ToInt32(), 3),
makeUnary(module, BinaryenTruncUFloat32ToInt64(), 3),
makeUnary(module, BinaryenTruncSFloat64ToInt32(), 4),
makeUnary(module, BinaryenTruncSFloat64ToInt64(), 4),
makeUnary(module, BinaryenTruncUFloat64ToInt32(), 4),
makeUnary(module, BinaryenTruncUFloat64ToInt64(), 4),
makeUnary(module, BinaryenReinterpretFloat32(), 3),
makeUnary(module, BinaryenReinterpretFloat64(), 4),
makeUnary(module, BinaryenConvertSInt32ToFloat32(), 1),
makeUnary(module, BinaryenConvertSInt32ToFloat64(), 1),
makeUnary(module, BinaryenConvertUInt32ToFloat32(), 1),
makeUnary(module, BinaryenConvertUInt32ToFloat64(), 1),
makeUnary(module, BinaryenConvertSInt64ToFloat32(), 2),
makeUnary(module, BinaryenConvertSInt64ToFloat64(), 2),
makeUnary(module, BinaryenConvertUInt64ToFloat32(), 2),
makeUnary(module, BinaryenConvertUInt64ToFloat64(), 2),
makeUnary(module, BinaryenPromoteFloat32(), 3),
makeUnary(module, BinaryenDemoteFloat64(), 4),
makeUnary(module, BinaryenReinterpretInt32(), 1),
makeUnary(module, BinaryenReinterpretInt64(), 1),
// Binary
makeBinary(module, BinaryenAdd(), 1),
makeBinary(module, BinaryenSub(), 4),
makeBinary(module, BinaryenDivS(), 1),
makeBinary(module, BinaryenDivU(), 2),
makeBinary(module, BinaryenRemS(), 2),
makeBinary(module, BinaryenRemU(), 1),
makeBinary(module, BinaryenAnd(), 1),
makeBinary(module, BinaryenOr(), 2),
makeBinary(module, BinaryenXor(), 1),
makeBinary(module, BinaryenShl(), 2),
makeBinary(module, BinaryenShrU(), 2),
makeBinary(module, BinaryenShrS(), 1),
makeBinary(module, BinaryenRotL(), 1),
makeBinary(module, BinaryenRotR(), 2),
makeBinary(module, BinaryenDiv(), 3),
makeBinary(module, BinaryenCopySign(), 4),
makeBinary(module, BinaryenMin(), 3),
makeBinary(module, BinaryenMax(), 4),
makeBinary(module, BinaryenEq(), 1),
makeBinary(module, BinaryenNe(), 3),
makeBinary(module, BinaryenLtS(), 1),
makeBinary(module, BinaryenLtU(), 2),
makeBinary(module, BinaryenLeS(), 2),
makeBinary(module, BinaryenLeU(), 1),
makeBinary(module, BinaryenGtS(), 2),
makeBinary(module, BinaryenGtU(), 1),
makeBinary(module, BinaryenGeS(), 1),
makeBinary(module, BinaryenGeU(), 2),
makeBinary(module, BinaryenLt(), 3),
makeBinary(module, BinaryenLe(), 4),
makeBinary(module, BinaryenGt(), 4),
makeBinary(module, BinaryenGe(), 3),
// All the rest
BinaryenBlock(module, NULL, NULL, 0), // block with no name
BinaryenIf(module, makeInt32(module, 1), makeInt32(module, 2), makeInt32(module, 3)),
BinaryenIf(module, makeInt32(module, 4), makeInt32(module, 5), NULL),
BinaryenLoop(module, "out", "in", makeInt32(module, 0)),
BinaryenLoop(module, NULL, "in2", makeInt32(module, 0)),
BinaryenLoop(module, NULL, NULL, makeInt32(module, 0)),
BinaryenBreak(module, "the-body", makeInt32(module, 0), makeInt32(module, 1)),
BinaryenBreak(module, "the-body", makeInt32(module, 2), NULL),
BinaryenBreak(module, "the-body", NULL, makeInt32(module, 3)),
BinaryenBreak(module, "the-body", NULL, NULL),
BinaryenSwitch(module, switchNames, 1, "the-body", makeInt32(module, 0), makeInt32(module, 1)),
BinaryenSwitch(module, switchNames, 1, "the-body", makeInt32(module, 2), NULL),
BinaryenCall(module, "kitchen-sinker", callOperands4, 4),
BinaryenCallImport(module, "an-imported", callOperands2, 2),
BinaryenCallIndirect(module, makeInt32(module, 2449), callOperands4, 4, iiIfF),
BinaryenGetLocal(module, 0, BinaryenInt32()),
BinaryenSetLocal(module, 0, makeInt32(module, 101)),
BinaryenLoad(module, 4, 0, 0, 0, BinaryenInt32(), makeInt32(module, 1)),
BinaryenLoad(module, 1, 1, 2, 4, BinaryenInt64(), makeInt32(module, 8)),
BinaryenLoad(module, 4, 0, 0, 0, BinaryenFloat32(), makeInt32(module, 2)),
BinaryenLoad(module, 8, 0, 2, 8, BinaryenFloat64(), makeInt32(module, 9)),
BinaryenStore(module, 4, 0, 0, makeInt32(module, 10), makeInt32(module, 11)),
BinaryenStore(module, 8, 2, 4, makeInt32(module, 110), makeInt64(module, 111)),
BinaryenSelect(module, makeInt32(module, 1), makeInt32(module, 3), makeInt32(module, 5)),
BinaryenReturn(module, NULL),
BinaryenReturn(module, makeFloat32(module, 1)),
// TODO: Host
BinaryenNop(module),
BinaryenUnreachable(module),
};
// Make the main body of the function
BinaryenExpressionRef body = BinaryenBlock(module, "the-body", bodyList, sizeof(bodyList) / sizeof(BinaryenExpressionRef));
// Create the function
BinaryenType localTypes[] = { BinaryenInt32() };
BinaryenFunctionRef sinker = BinaryenAddFunction(module, "kitchen-sinker", iiIfF, localTypes, 1, body);
// Imports
BinaryenType iparams[2] = { BinaryenInt32(), BinaryenFloat64() };
BinaryenFunctionTypeRef viF = BinaryenAddFunctionType(module, "viF", BinaryenNone(), iparams, 2);
BinaryenAddImport(module, "an-imported", "module", "base", viF);
// Exports
BinaryenAddExport(module, "kitchen-sinker", "kitchen_sinker");
// Function table. One per module
BinaryenFunctionRef functions[] = { sinker };
BinaryenSetFunctionTable(module, functions, 1);
// Memory. One per module
const char *segments[] = { "hello, world" };
BinaryenIndex segmentOffsets[] = { 10 };
BinaryenIndex segmentSizes[] = { 12 };
BinaryenSetMemory(module, 1, 256, "mem", segments, segmentOffsets, segmentSizes, 1);
// Start function. One per module
BinaryenSetStart(module, "sinker");
// Print it out
BinaryenModulePrint(module);
// Clean up the module, which owns all the objects we created above
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);
}
{ // nontrivial loop + phi to head
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));
RelooperBlockRef block4 = RelooperAddBlock(relooper, makeInt32(module, 4));
RelooperBlockRef block5 = RelooperAddBlock(relooper, makeInt32(module, 5));
RelooperBlockRef block6 = RelooperAddBlock(relooper, makeInt32(module, 6));
RelooperAddBranch(block0, block1, NULL, makeInt32(module, 10));
RelooperAddBranch(block1, block2, makeInt32(module, -2), NULL);
RelooperAddBranch(block1, block6, NULL, makeInt32(module, 20));
RelooperAddBranch(block2, block3, makeInt32(module, -6), NULL);
RelooperAddBranch(block2, block1, NULL, makeInt32(module, 30));
RelooperAddBranch(block3, block4, makeInt32(module, -10), NULL);
RelooperAddBranch(block3, block5, NULL, NULL);
RelooperAddBranch(block4, block5, NULL, NULL);
RelooperAddBranch(block5, block6, NULL, makeInt32(module, 40));
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();
}
|