Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Created May 30, 2018 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerrorJack/9943e24dd4bd325e0725e7b2322ffb54 to your computer and use it in GitHub Desktop.
Save TerrorJack/9943e24dd4bd325e0725e7b2322ffb54 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <binaryen-c.h>
static BinaryenExpressionRef build_sea_of_blocks(BinaryenModuleRef m,
int block_num) {
std::vector<std::string> labels;
labels.reserve(block_num);
std::vector<const char *> label_ptrs;
label_ptrs.reserve(block_num);
for (auto i = 0; i < block_num; ++i) {
labels.push_back(std::to_string(i));
label_ptrs.push_back(labels[i].c_str());
}
BinaryenExpressionRef block_exprs[] = {
BinaryenSwitch(m, label_ptrs.data(), block_num, label_ptrs[0],
BinaryenGetLocal(m, 0, BinaryenTypeInt32()), nullptr),
BinaryenUnreachable(m)};
for (auto i = 0; i < block_num; ++i) {
block_exprs[0] =
BinaryenBlock(m, label_ptrs[i], block_exprs, 2, BinaryenTypeNone());
block_exprs[1] =
BinaryenSetLocal(m, 1, BinaryenConst(m, BinaryenLiteralInt32(i)));
}
block_exprs[0] =
BinaryenBlock(m, nullptr, block_exprs, 2, BinaryenTypeNone());
block_exprs[1] = BinaryenGetLocal(m, 1, BinaryenTypeInt32());
return BinaryenBlock(m, nullptr, block_exprs, 2, BinaryenTypeInt32());
}
int main() {
auto m = BinaryenModuleCreate();
auto i32 = BinaryenTypeInt32();
auto ftype = BinaryenAddFunctionType(m, "ftype", i32, &i32, 1);
auto expr = build_sea_of_blocks(m, 10000000);
BinaryenAddFunction(m, "f", ftype, &i32, 1, expr);
std::cout << "Starting validation" << std::endl;
std::cout << BinaryenModuleValidate(m);
BinaryenModuleDispose(m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment