]> git.lizzy.rs Git - rust.git/blob - src/librustc_binaryen/BinaryenWrapper.cpp
Rollup merge of #46259 - bjorn3:display_lang_item, r=nagisa
[rust.git] / src / librustc_binaryen / BinaryenWrapper.cpp
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This is a small C API inserted on top of the Binaryen C++ API which we use
12 // from Rust. Once we have a real linker for we'll be able to remove all this,
13 // and otherwise this is just all on a "as we need it" basis for now.
14
15 #include <stdint.h>
16 #include <string>
17 #include <stdlib.h>
18
19 #include "s2wasm.h"
20 #include "wasm-binary.h"
21 #include "wasm-linker.h"
22
23 using namespace wasm;
24
25 struct BinaryenRustModule {
26   BufferWithRandomAccess buffer;
27 };
28
29 struct BinaryenRustModuleOptions {
30   uint64_t globalBase;
31   bool debug;
32   uint64_t stackAllocation;
33   uint64_t initialMem;
34   uint64_t maxMem;
35   bool importMemory;
36   bool ignoreUnknownSymbols;
37   bool debugInfo;
38   std::string startFunction;
39
40   BinaryenRustModuleOptions() :
41     globalBase(0),
42     debug(false),
43     stackAllocation(0),
44     initialMem(0),
45     maxMem(0),
46     importMemory(false),
47     ignoreUnknownSymbols(false),
48     debugInfo(false),
49     startFunction("")
50   {}
51
52 };
53
54 extern "C" BinaryenRustModuleOptions*
55 BinaryenRustModuleOptionsCreate() {
56   return new BinaryenRustModuleOptions;
57 }
58
59 extern "C" void
60 BinaryenRustModuleOptionsFree(BinaryenRustModuleOptions *options) {
61   delete options;
62 }
63
64 extern "C" void
65 BinaryenRustModuleOptionsSetDebugInfo(BinaryenRustModuleOptions *options,
66                                       bool debugInfo) {
67   options->debugInfo = debugInfo;
68 }
69
70 extern "C" void
71 BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
72                                   char *start) {
73   options->startFunction = start;
74 }
75
76 extern "C" void
77 BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
78                                             uint64_t stack) {
79   options->stackAllocation = stack;
80 }
81
82 extern "C" void
83 BinaryenRustModuleOptionsSetImportMemory(BinaryenRustModuleOptions *options,
84                                          bool import) {
85   options->importMemory = import;
86 }
87
88 extern "C" BinaryenRustModule*
89 BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
90                          const char *assembly) {
91   Linker linker(
92       options->globalBase,
93       options->stackAllocation,
94       options->initialMem,
95       options->maxMem,
96       options->importMemory,
97       options->ignoreUnknownSymbols,
98       options->startFunction,
99       options->debug);
100
101   S2WasmBuilder mainbuilder(assembly, options->debug);
102   linker.linkObject(mainbuilder);
103   linker.layout();
104
105   auto ret = make_unique<BinaryenRustModule>();
106   {
107     WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
108     writer.setNamesSection(options->debugInfo);
109     // FIXME: support source maps?
110     // writer.setSourceMap(sourceMapStream.get(), sourceMapUrl);
111
112     // FIXME: support symbol maps?
113     // writer.setSymbolMap(symbolMap);
114     writer.write();
115   }
116   return ret.release();
117 }
118
119 extern "C" const uint8_t*
120 BinaryenRustModulePtr(const BinaryenRustModule *M) {
121   return M->buffer.data();
122 }
123
124 extern "C" size_t
125 BinaryenRustModuleLen(const BinaryenRustModule *M) {
126   return M->buffer.size();
127 }
128
129 extern "C" void
130 BinaryenRustModuleFree(BinaryenRustModule *M) {
131   delete M;
132 }