forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhvm.cpp
More file actions
154 lines (133 loc) · 5.05 KB
/
hvm.cpp
File metadata and controls
154 lines (133 loc) · 5.05 KB
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "llvh/Support/CommandLine.h"
#include "llvh/Support/FileSystem.h"
#include "llvh/Support/InitLLVM.h"
#include "llvh/Support/MemoryBuffer.h"
#include "llvh/Support/Path.h"
#include "llvh/Support/PrettyStackTrace.h"
#include "llvh/Support/Program.h"
#include "llvh/Support/Signals.h"
#include "llvh/Support/raw_ostream.h"
#ifndef HERMESVM_LEAN
#include "hermes/BCGen/HBC/BytecodeDisassembler.h"
#endif
#include "hermes/BCGen/HBC/BytecodeStream.h"
#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/ConsoleHost/ConsoleHost.h"
#include "hermes/ConsoleHost/RuntimeFlags.h"
#include "hermes/Public/Buffer.h"
#include "hermes/Support/Algorithms.h"
#include "hermes/Support/MemoryBuffer.h"
#include "hermes/Support/PageAccessTracker.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/Runtime.h"
#define DEBUG_TYPE "hvm"
using namespace hermes;
static llvh::cl::opt<std::string> InputFilename(
llvh::cl::desc("input file"),
llvh::cl::init("-"),
llvh::cl::Positional);
// Lean VM doesn't include the disassembler.
#ifndef HERMESVM_LEAN
static llvh::cl::opt<bool> Disassemble(
"d",
llvh::cl::desc("Disassemble bytecode"));
static llvh::cl::opt<bool> PrettyDisassemble(
"pretty-disassemble",
llvh::cl::init(true),
llvh::cl::desc("Pretty print the disassembled bytecode"));
#endif
static llvh::cl::opt<unsigned> Repeat(
"Xrepeat",
llvh::cl::desc("Repeat execution N number of times"),
llvh::cl::init(1),
llvh::cl::Hidden);
static llvh::cl::opt<bool> GCPrintStats(
"gc-print-stats",
llvh::cl::desc("Output summary garbage collection statistics at exit"),
llvh::cl::cat(cl::GCCategory),
llvh::cl::init(false));
// This is the vm driver.
int main(int argc, char **argv) {
// Normalize the arg vector.
llvh::InitLLVM initLLVM(argc, argv);
llvh::sys::PrintStackTraceOnErrorSignal("hvm");
llvh::PrettyStackTraceProgram X(argc, argv);
llvh::llvm_shutdown_obj Y;
llvh::cl::ParseCommandLineOptions(argc, argv, "Hermes VM driver\n");
llvh::ErrorOr<std::unique_ptr<llvh::MemoryBuffer>> FileBufOrErr =
llvh::MemoryBuffer::getFileOrSTDIN(InputFilename);
std::string filename = FileBufOrErr.get()->getBufferIdentifier().str();
if (!FileBufOrErr) {
llvh::errs() << "Error! Failed to open file: " << InputFilename << "\n";
return -1;
}
auto buffer = llvh::make_unique<MemoryBuffer>(FileBufOrErr.get().get());
auto ret =
hbc::BCProviderFromBuffer::createBCProviderFromBuffer(std::move(buffer));
if (!ret.first) {
llvh::errs() << ret.second;
return 1;
}
std::unique_ptr<hbc::BCProvider> bytecode = std::move(ret.first);
#ifndef HERMESVM_LEAN
if (Disassemble) {
hermes::hbc::BytecodeDisassembler disassembler(std::move(bytecode));
disassembler.setOptions(
PrettyDisassemble ? hermes::hbc::DisassemblyOptions::Pretty
: hermes::hbc::DisassemblyOptions::None);
disassembler.disassemble(llvh::outs());
}
#endif
ExecuteOptions options;
options.runtimeConfig =
vm::RuntimeConfig::Builder()
.withGCConfig(
vm::GCConfig::Builder()
.withInitHeapSize(cl::InitHeapSize.bytes)
.withMaxHeapSize(cl::MaxHeapSize.bytes)
.withSanitizeConfig(
vm::GCSanitizeConfig::Builder()
.withSanitizeRate(cl::GCSanitizeRate)
.withRandomSeed(cl::GCSanitizeRandomSeed)
.build())
.withShouldRandomizeAllocSpace(cl::GCRandomizeAllocSpace)
.withShouldRecordStats(
GCPrintStats && !cl::StableInstructionCount)
.withShouldReleaseUnused(vm::kReleaseUnusedNone)
.withName("hvm")
.build())
.withES6Proxy(cl::ES6Proxy)
.withES6Symbol(cl::ES6Symbol)
.withTrackIO(cl::TrackBytecodeIO)
.withEnableHermesInternal(cl::EnableHermesInternal)
.withEnableHermesInternalTestMethods(
cl::EnableHermesInternalTestMethods)
.build();
options.stabilizeInstructionCount = cl::StableInstructionCount;
options.stopAfterInit = cl::StopAfterInit;
#ifdef HERMESVM_PROFILER_EXTERN
options.patchProfilerSymbols = cl::PatchProfilerSymbols;
options.profilerSymbolsFile = cl::ProfilerSymbolsFile;
#endif
bool success;
if (Repeat <= 1) {
success = executeHBCBytecode(std::move(bytecode), options, &filename);
} else {
// The runtime is supposed to own the bytecode exclusively, but we
// want to keep it around in this special case, so we can reuse it
// between iterations.
std::shared_ptr<hbc::BCProvider> sharedBytecode = std::move(bytecode);
success = true;
for (unsigned i = 0; i < Repeat; ++i) {
success &= executeHBCBytecode(
std::shared_ptr<hbc::BCProvider>{sharedBytecode}, options, &filename);
}
}
return success ? 0 : 1;
}