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
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// This is a small C API inserted on top of the Binaryen C++ API which we use
// from Rust. Once we have a real linker for we'll be able to remove all this,
// and otherwise this is just all on a "as we need it" basis for now.
#include <stdint.h>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "s2wasm.h"
#include "wasm-binary.h"
#include "wasm-linker.h"
using namespace wasm;
struct BinaryenRustModule {
BufferWithRandomAccess buffer;
std::string sourceMapJSON;
};
struct BinaryenRustModuleOptions {
uint64_t globalBase;
bool debug;
uint64_t stackAllocation;
uint64_t initialMem;
uint64_t maxMem;
bool importMemory;
bool ignoreUnknownSymbols;
bool debugInfo;
std::string startFunction;
std::string sourceMapUrl;
BinaryenRustModuleOptions() :
globalBase(0),
debug(false),
stackAllocation(0),
initialMem(0),
maxMem(0),
importMemory(false),
ignoreUnknownSymbols(false),
debugInfo(false),
startFunction(""),
sourceMapUrl("")
{}
};
extern "C" BinaryenRustModuleOptions*
BinaryenRustModuleOptionsCreate() {
return new BinaryenRustModuleOptions;
}
extern "C" void
BinaryenRustModuleOptionsFree(BinaryenRustModuleOptions *options) {
delete options;
}
extern "C" void
BinaryenRustModuleOptionsSetDebugInfo(BinaryenRustModuleOptions *options,
bool debugInfo) {
options->debugInfo = debugInfo;
}
extern "C" void
BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
char *start) {
options->startFunction = start;
}
extern "C" void
BinaryenRustModuleOptionsSetSourceMapUrl(BinaryenRustModuleOptions *options,
char *sourceMapUrl) {
options->sourceMapUrl = sourceMapUrl;
}
extern "C" void
BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
uint64_t stack) {
options->stackAllocation = stack;
}
extern "C" void
BinaryenRustModuleOptionsSetImportMemory(BinaryenRustModuleOptions *options,
bool import) {
options->importMemory = import;
}
extern "C" BinaryenRustModule*
BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
const char *assembly) {
Linker linker(
options->globalBase,
options->stackAllocation,
options->initialMem,
options->maxMem,
options->importMemory,
options->ignoreUnknownSymbols,
options->startFunction,
options->debug);
S2WasmBuilder mainbuilder(assembly, options->debug);
linker.linkObject(mainbuilder);
linker.layout();
auto ret = make_unique<BinaryenRustModule>();
{
WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
writer.setNamesSection(options->debugInfo);
std::unique_ptr<std::ostringstream> sourceMapStream = nullptr;
{
sourceMapStream = make_unique<std::ostringstream>();
writer.setSourceMap(sourceMapStream.get(), options->sourceMapUrl);
}
// FIXME: support symbol maps?
// writer.setSymbolMap(symbolMap);
writer.write();
if (sourceMapStream) {
ret->sourceMapJSON = sourceMapStream->str();
}
}
return ret.release();
}
extern "C" const uint8_t*
BinaryenRustModulePtr(const BinaryenRustModule *M) {
return M->buffer.data();
}
extern "C" size_t
BinaryenRustModuleLen(const BinaryenRustModule *M) {
return M->buffer.size();
}
extern "C" const char*
BinaryenRustModuleSourceMapPtr(const BinaryenRustModule *M) {
return M->sourceMapJSON.data();
}
extern "C" size_t
BinaryenRustModuleSourceMapLen(const BinaryenRustModule *M) {
return M->sourceMapJSON.length();
}
extern "C" void
BinaryenRustModuleFree(BinaryenRustModule *M) {
delete M;
}
|