Skip to content

Instantly share code, notes, and snippets.

@lqd
Last active October 5, 2016 06:15
Show Gist options
  • Save lqd/b1572fdfa97fd3cc2c9bcbffdf76512e to your computer and use it in GitHub Desktop.
Save lqd/b1572fdfa97fd3cc2c9bcbffdf76512e to your computer and use it in GitHub Desktop.
// beginning a Binaryen API trace
#include <math.h>
#include <map>
#include "src/binaryen-c.h"
int main() {
std::map<size_t, BinaryenFunctionTypeRef> functionTypes;
std::map<size_t, BinaryenExpressionRef> expressions;
std::map<size_t, BinaryenFunctionRef> functions;
std::map<size_t, RelooperBlockRef> relooperBlocks;
BinaryenModuleRef the_module = NULL;
RelooperRef the_relooper = NULL;
{
const char* segments[] = { 0 };
BinaryenExpressionRef segmentOffsets[] = { 0 };
BinaryenIndex segmentSizes[] = { 0 };
BinaryenSetMemory(the_module, 256, 256, "memory", segments, segmentOffsets, segmentSizes, 0);
}
the_relooper = RelooperCreate();
{
BinaryenExpressionRef children[] = { 0 };
expressions[0] = BinaryenBlock(the_module, "bb0", children, 0);
}
relooperBlocks[0] = RelooperAddBlock(the_relooper, expressions[0]);
expressions[1] = BinaryenGetLocal(the_module, 0, 1);
expressions[2] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
expressions[3] = BinaryenStore(the_module, 4, 0, 0, expressions[2], expressions[1], 1);
expressions[4] = BinaryenGetLocal(the_module, 0, 1);
expressions[5] = BinaryenReturn(the_module, expressions[4]);
{
BinaryenExpressionRef children[] = { expressions[3], expressions[5] };
expressions[6] = BinaryenBlock(the_module, "bb1", children, 2);
}
relooperBlocks[1] = RelooperAddBlock(the_relooper, expressions[6]);
RelooperAddBranch(relooperBlocks[0], relooperBlocks[1], expressions[0], expressions[0]);
{
BinaryenIndex paramTypes[] = { 0 };
functionTypes[0] = BinaryenAddFunctionType(the_module, "rustfn-0-3", 0, paramTypes, 0);
}
expressions[8] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
expressions[9] = BinaryenLoad(the_module, 4, 0, 0, 0, 1, expressions[8]);
expressions[10] = BinaryenSetLocal(the_module, 0, expressions[9]);
relooperBlocks[2] = RelooperAddBlock(the_relooper, expressions[10]);
RelooperAddBranch(relooperBlocks[2], relooperBlocks[0], expressions[0], expressions[0]);
expressions[11] = RelooperRenderAndDispose(the_relooper, relooperBlocks[2], 1, the_module);
{
BinaryenType varTypes[] = { 1, 1, 2 };
functions[0] = BinaryenAddFunction(the_module, "main", functionTypes[0], varTypes, 3, expressions[11]);
}
BinaryenAddExport(the_module, "main", "main");
{
BinaryenIndex paramTypes[] = { 0 };
functionTypes[1] = BinaryenAddFunctionType(the_module, "__wasm_start", 0, paramTypes, 0);
}
{
const char* segments[] = { 0 };
BinaryenExpressionRef segmentOffsets[] = { 0 };
BinaryenIndex segmentSizes[] = { 0 };
BinaryenSetMemory(the_module, 1, 1, NULL, segments, segmentOffsets, segmentSizes, 0);
}
expressions[12] = BinaryenConst(the_module, BinaryenLiteralInt32(65535));
expressions[13] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
expressions[14] = BinaryenStore(the_module, 4, 0, 0, expressions[13], expressions[12], 1);
{
BinaryenExpressionRef operands[] = { 0 };
expressions[15] = BinaryenCall(the_module, "main", operands, 0, 0);
}
expressions[16] = BinaryenDrop(the_module, expressions[15]);
{
BinaryenExpressionRef children[] = { expressions[14], expressions[16] };
expressions[17] = BinaryenBlock(the_module, NULL, children, 2);
}
{
BinaryenType varTypes[] = { 0 };
functions[1] = BinaryenAddFunction(the_module, "__wasm_start", functionTypes[1], varTypes, 0, expressions[17]);
}
BinaryenSetStart(the_module, functions[1]);
BinaryenModuleValidate(the_module);
BinaryenModuleOptimize(the_module);
BinaryenModuleValidate(the_module);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment