-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
71 lines (59 loc) · 2.24 KB
/
main.cpp
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
#include "binaryen-c.h"
#include "pass.h"
#include "wasm-io.h"
#include "wasm.h"
#include <fstream>
#include <iostream>
#define TARGET_FILE "target.wasm"
#define TARGET_SOURCEMAP_FILE "target.wasm.map"
struct SimpleExpressionWalker
: public wasm::PostWalker<
SimpleExpressionWalker,
wasm::UnifiedExpressionVisitor<SimpleExpressionWalker>> {
void visitExpression(BinaryenExpressionRef curr) {
if (curr->is<wasm::Call>()) {
auto currFun = getFunction();
wasm::Call *callExpr = static_cast<wasm::Call *>(curr);
wasm::Function::DebugLocation callLocation, param1Location,
param2Location;
callLocation.fileIndex = 0;
callLocation.columnNumber = 5;
callLocation.lineNumber = 24;
currFun->debugLocations[curr] = callLocation;
param1Location.fileIndex = 0;
param1Location.columnNumber = 6;
param1Location.lineNumber = 25;
currFun->debugLocations[callExpr->operands[0]] = param1Location;
param2Location.fileIndex = 0;
param2Location.columnNumber = 6;
param2Location.lineNumber = 26;
currFun->debugLocations[callExpr->operands[1]] = param2Location;
}
}
};
int main(int, char **) {
SimpleExpressionWalker walker;
BinaryenModuleRef ref = BinaryenModuleCreate();
std::ifstream file("../testobjs/test.wat");
std::string contents((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto module = BinaryenModuleParse(contents.c_str());
module->debugInfoFileNames.push_back("testobjs/test.wat");
walker.walkModule(module);
BinaryenModulePrint(module); // print correct value
std::cout << "---------below will print incorrect value-------- \n";
auto result = BinaryenModuleAllocateAndWrite(module, "testurl");
std::ofstream oFile(TARGET_FILE);
oFile.write(static_cast<char *>(result.binary), result.binaryBytes);
oFile.flush();
oFile.close();
std::ofstream oDebugFile(TARGET_SOURCEMAP_FILE);
oDebugFile << result.sourceMap;
oDebugFile.flush();
oDebugFile.close();
wasm::ModuleReader reader;
wasm::Module reloadModule;
reader.read(TARGET_FILE, reloadModule, TARGET_SOURCEMAP_FILE);
BinaryenModulePrint(&reloadModule); // print in-correct value
std::cout << "end test!\n";
}