-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFrontend2.cpp
478 lines (391 loc) · 15.1 KB
/
Frontend2.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "frontend/Frontend2.h"
#include "mozilla/Maybe.h" // mozilla::Maybe
#include "mozilla/OperatorNewExtensions.h" // mozilla::KnownNotNull
#include "mozilla/Range.h" // mozilla::Range
#include "mozilla/Span.h" // mozilla::{Span, MakeSpan}
#include "mozilla/Variant.h" // mozilla::AsVariant
#include <stddef.h> // size_t
#include <stdint.h> // uint8_t, uint32_t
#include "jsapi.h"
#include "frontend/AbstractScopePtr.h" // ScopeIndex
#include "frontend/BytecodeSection.h" // EmitScriptThingsVector
#include "frontend/CompilationInfo.h" // CompilationInfo
#include "frontend/Parser.h" // NewEmptyLexicalScopeData, NewEmptyGlobalScopeData
#include "frontend/smoosh_generated.h" // CVec, Smoosh*, smoosh_*
#include "frontend/SourceNotes.h" // SrcNote
#include "frontend/Stencil.h" // ScopeCreationData, RegExpIndex
#include "frontend/TokenStream.h" // TokenStreamAnyChars
#include "gc/Rooting.h" // RootedScriptSourceObject
#include "irregexp/RegExpAPI.h" // irregexp::CheckPatternSyntax
#include "js/CharacterEncoding.h" // JS::UTF8Chars, UTF8CharsToNewTwoByteCharsZ
#include "js/HeapAPI.h" // JS::GCCellPtr
#include "js/RegExpFlags.h" // JS::RegExpFlag, JS::RegExpFlags
#include "js/RootingAPI.h" // JS::Handle, JS::Rooted
#include "js/TypeDecls.h" // Rooted{Script,Value,String,Object}
#include "js/Utility.h" // JS::UniqueTwoByteChars, StringBufferArena
#include "vm/JSAtom.h" // AtomizeUTF8Chars
#include "vm/JSScript.h" // JSScript
#include "vm/Scope.h" // BindingName
#include "vm/ScopeKind.h" // ScopeKind
#include "vm/SharedStencil.h" // ImmutableScriptData, ScopeNote, TryNote
#include "vm/JSContext-inl.h" // AutoKeepAtoms (used by BytecodeCompiler)
using mozilla::Utf8Unit;
using namespace js::gc;
using namespace js::frontend;
using namespace js;
namespace js {
namespace frontend {
class SmooshScriptStencil : public ScriptStencil {
const SmooshResult& result_;
CompilationInfo& compilationInfo_;
JSAtom** allAtoms_ = nullptr;
public:
SmooshScriptStencil(const SmooshResult& result,
CompilationInfo& compilationInfo)
: ScriptStencil(compilationInfo.cx),
result_(result),
compilationInfo_(compilationInfo) {}
MOZ_MUST_USE bool init(JSContext* cx,
UniquePtr<ImmutableScriptData> immutableData) {
using ImmutableFlags = js::ImmutableScriptFlagsEnum;
const JS::ReadOnlyCompileOptions& options = compilationInfo_.options;
immutableFlags = result_.top_level_script.immutable_flags;
// FIXME: The following flags should be set in jsparagus.
immutableFlags.setFlag(ImmutableFlags::SelfHosted, options.selfHostingMode);
immutableFlags.setFlag(ImmutableFlags::ForceStrict,
options.forceStrictMode());
immutableFlags.setFlag(ImmutableFlags::NoScriptRval, options.noScriptRval);
immutableFlags.setFlag(ImmutableFlags::TreatAsRunOnce, options.isRunOnce);
immutableScriptData = std::move(immutableData);
if (!createAtoms(cx)) {
return false;
}
if (!createGCThings(cx)) {
return false;
}
if (!createScopeCreationData(cx)) {
return false;
}
if (!createRegExpData(cx)) {
return false;
}
return true;
}
private:
bool createAtoms(JSContext* cx) {
size_t numAtoms = result_.all_atoms_len;
auto& alloc = compilationInfo_.allocScope.alloc();
allAtoms_ = alloc.newArray<JSAtom*>(numAtoms);
if (!allAtoms_) {
ReportOutOfMemory(cx);
return false;
}
for (size_t i = 0; i < numAtoms; i++) {
auto s = smoosh_get_atom_at(result_, i);
auto len = smoosh_get_atom_len_at(result_, i);
JSAtom* atom = AtomizeUTF8Chars(cx, s, len);
if (!atom) {
return false;
}
allAtoms_[i] = atom;
}
return true;
}
bool createGCThings(JSContext* cx) {
size_t ngcthings = result_.top_level_script.gcthings.len;
if (!gcThings.reserve(ngcthings)) {
return false;
}
for (size_t i = 0; i < ngcthings; i++) {
SmooshGCThing& item = result_.top_level_script.gcthings.data[i];
switch (item.tag) {
case SmooshGCThing::Tag::Atom: {
gcThings.infallibleAppend(
mozilla::AsVariant(allAtoms_[item.AsAtom()]));
break;
}
case SmooshGCThing::Tag::Scope: {
gcThings.infallibleAppend(
mozilla::AsVariant(ScopeIndex(item.AsScope())));
break;
}
case SmooshGCThing::Tag::RegExp: {
gcThings.infallibleAppend(
mozilla::AsVariant(RegExpIndex(item.AsRegExp())));
break;
}
}
}
return true;
}
private:
// Fill `compilationInfo_.scopeCreationData` with scope data, where
// i-th item corresponds to i-th scope.
bool createScopeCreationData(JSContext* cx) {
auto& alloc = compilationInfo_.allocScope.alloc();
for (size_t i = 0; i < result_.scopes.len; i++) {
SmooshScopeData& scopeData = result_.scopes.data[i];
ScopeIndex index;
switch (scopeData.tag) {
case SmooshScopeData::Tag::Global: {
auto& global = scopeData.AsGlobal();
size_t numBindings = global.bindings.len;
JS::Rooted<GlobalScope::Data*> data(
cx, NewEmptyGlobalScopeData(cx, alloc, numBindings));
if (!data) {
return false;
}
copyBindingNames(global.bindings, data->trailingNames.start());
data->letStart = global.let_start;
data->constStart = global.const_start;
data->length = numBindings;
if (!ScopeCreationData::create(cx, compilationInfo_,
ScopeKind::Global, data, &index)) {
return false;
}
break;
}
case SmooshScopeData::Tag::Lexical: {
auto& lexical = scopeData.AsLexical();
size_t numBindings = lexical.bindings.len;
JS::Rooted<LexicalScope::Data*> data(
cx, NewEmptyLexicalScopeData(cx, alloc, numBindings));
if (!data) {
return false;
}
copyBindingNames(lexical.bindings, data->trailingNames.start());
// NOTE: data->nextFrameSlot is set in ScopeCreationData::create.
data->constStart = lexical.const_start;
data->length = numBindings;
uint32_t firstFrameSlot = lexical.first_frame_slot;
ScopeIndex enclosingIndex(lexical.enclosing);
Rooted<AbstractScopePtr> enclosing(
cx, AbstractScopePtr(compilationInfo_, enclosingIndex));
if (!ScopeCreationData::create(cx, compilationInfo_,
ScopeKind::Lexical, data,
firstFrameSlot, enclosing, &index)) {
return false;
}
break;
}
}
// `finishGCThings` depends on this condition.
MOZ_ASSERT(index == i);
}
return true;
}
// Fill `compilationInfo_.regExpData` with scope data, where
// i-th item corresponds to i-th RegExp.
bool createRegExpData(JSContext* cx) {
for (size_t i = 0; i < result_.regexps.len; i++) {
SmooshRegExpItem& item = result_.regexps.data[i];
auto s = smoosh_get_slice_at(result_, item.pattern);
auto len = smoosh_get_slice_len_at(result_, item.pattern);
JS::RegExpFlags::Flag flags = JS::RegExpFlag::NoFlags;
if (item.global) {
flags |= JS::RegExpFlag::Global;
}
if (item.ignore_case) {
flags |= JS::RegExpFlag::IgnoreCase;
}
if (item.multi_line) {
flags |= JS::RegExpFlag::Multiline;
}
if (item.dot_all) {
flags |= JS::RegExpFlag::DotAll;
}
if (item.sticky) {
flags |= JS::RegExpFlag::Sticky;
}
if (item.unicode) {
flags |= JS::RegExpFlag::Unicode;
}
// FIXME: This check should be done at parse time.
size_t length;
JS::UniqueTwoByteChars pattern(
UTF8CharsToNewTwoByteCharsZ(cx, JS::UTF8Chars(s, len), &length,
StringBufferArena)
.get());
if (!pattern) {
return false;
}
mozilla::Range<const char16_t> range(pattern.get(), length);
TokenStreamAnyChars ts(cx, compilationInfo_.options, /* smg = */ nullptr);
// See Parser<FullParseHandler, Unit>::newRegExp.
LifoAllocScope allocScope(&cx->tempLifoAlloc());
if (!irregexp::CheckPatternSyntax(cx, ts, range, flags)) {
return false;
}
RegExpIndex index(compilationInfo_.regExpData.length());
if (!compilationInfo_.regExpData.emplaceBack()) {
return false;
}
if (!compilationInfo_.regExpData[index].init(cx, range,
JS::RegExpFlags(flags))) {
return false;
}
// `finishGCThings` depends on this condition.
MOZ_ASSERT(index.index == i);
}
return true;
}
void copyBindingNames(CVec<SmooshBindingName>& from, BindingName* to) {
size_t numBindings = from.len;
for (size_t i = 0; i < numBindings; i++) {
SmooshBindingName& name = from.data[i];
new (mozilla::KnownNotNull, &to[i])
BindingName(allAtoms_[name.name], name.is_closed_over,
name.is_top_level_function);
}
}
};
// Free given SmooshResult on leaving scope.
class AutoFreeSmooshResult {
SmooshResult* result_;
public:
AutoFreeSmooshResult() = delete;
explicit AutoFreeSmooshResult(SmooshResult* result) : result_(result) {}
~AutoFreeSmooshResult() {
if (result_) {
smoosh_free(*result_);
}
}
};
// Free given SmooshParseResult on leaving scope.
class AutoFreeSmooshParseResult {
SmooshParseResult* result_;
public:
AutoFreeSmooshParseResult() = delete;
explicit AutoFreeSmooshParseResult(SmooshParseResult* result)
: result_(result) {}
~AutoFreeSmooshParseResult() {
if (result_) {
smoosh_free_parse_result(*result_);
}
}
};
void InitSmoosh() { smoosh_init(); }
void ReportSmooshCompileError(JSContext* cx, ErrorMetadata&& metadata,
int errorNumber, ...) {
va_list args;
va_start(args, errorNumber);
ReportCompileErrorUTF8(cx, std::move(metadata), /* notes = */ nullptr,
errorNumber, &args);
va_end(args);
}
/* static */
JSScript* Smoosh::compileGlobalScript(CompilationInfo& compilationInfo,
JS::SourceText<Utf8Unit>& srcBuf,
bool* unimplemented) {
// FIXME: check info members and return with *unimplemented = true
// if any field doesn't match to smoosh_run.
auto bytes = reinterpret_cast<const uint8_t*>(srcBuf.get());
size_t length = srcBuf.length();
JSContext* cx = compilationInfo.cx;
const auto& options = compilationInfo.options;
SmooshCompileOptions compileOptions;
compileOptions.no_script_rval = options.noScriptRval;
SmooshResult smoosh = smoosh_run(bytes, length, &compileOptions);
AutoFreeSmooshResult afsr(&smoosh);
if (smoosh.error.data) {
*unimplemented = false;
ErrorMetadata metadata;
metadata.filename = "<unknown>";
metadata.lineNumber = 1;
metadata.columnNumber = 0;
metadata.isMuted = false;
ReportSmooshCompileError(cx, std::move(metadata),
JSMSG_SMOOSH_COMPILE_ERROR,
reinterpret_cast<const char*>(smoosh.error.data));
return nullptr;
}
if (smoosh.unimplemented) {
*unimplemented = true;
return nullptr;
}
*unimplemented = false;
auto& smooshScript = smoosh.top_level_script;
auto& smooshScriptData =
smoosh.script_data_list.data[smooshScript.immutable_script_data.AsSome()];
Vector<ScopeNote, 0, SystemAllocPolicy> scopeNotes;
if (!scopeNotes.resize(smooshScriptData.scope_notes.len)) {
return nullptr;
}
for (size_t i = 0; i < smooshScriptData.scope_notes.len; i++) {
SmooshScopeNote& scopeNote = smooshScriptData.scope_notes.data[i];
scopeNotes[i].index = scopeNote.index;
scopeNotes[i].start = scopeNote.start;
scopeNotes[i].length = scopeNote.length;
scopeNotes[i].parent = scopeNote.parent;
}
bool isFunction = false;
int funLength = 0; // Smoosh support for functions isn't complete yet.
auto immutableScriptData = ImmutableScriptData::new_(
cx, smooshScriptData.main_offset, smooshScriptData.nfixed,
smooshScriptData.nslots, smooshScriptData.body_scope_index,
smooshScriptData.num_ic_entries, smooshScriptData.num_bytecode_type_sets,
isFunction, funLength,
mozilla::MakeSpan(smooshScriptData.bytecode.data,
smooshScriptData.bytecode.len),
mozilla::Span<const SrcNote>(), mozilla::Span<const uint32_t>(),
scopeNotes, mozilla::Span<const TryNote>());
if (!immutableScriptData) {
return nullptr;
}
SmooshScriptStencil stencil(smoosh, compilationInfo);
if (!stencil.init(cx, std::move(immutableScriptData))) {
return nullptr;
}
stencil.extent = SourceExtent::makeGlobalExtent(length);
RootedScript script(
cx, JSScript::fromStencil(cx, compilationInfo, stencil, nullptr));
if (!script) {
return nullptr;
}
#if defined(DEBUG) || defined(JS_JITSPEW)
Sprinter sprinter(cx);
if (!sprinter.init()) {
return nullptr;
}
if (!Disassemble(cx, script, true, &sprinter, DisassembleSkeptically::Yes)) {
return nullptr;
}
printf("%s\n", sprinter.string());
if (!Disassemble(cx, script, true, &sprinter, DisassembleSkeptically::No)) {
return nullptr;
}
// (don't bother printing it)
#endif
return script;
}
bool SmooshParseScript(JSContext* cx, const uint8_t* bytes, size_t length) {
SmooshParseResult result = smoosh_test_parse_script(bytes, length);
AutoFreeSmooshParseResult afspr(&result);
if (result.error.data) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
result.unimplemented ? JSMSG_SMOOSH_UNIMPLEMENTED
: JSMSG_SMOOSH_COMPILE_ERROR,
reinterpret_cast<const char*>(result.error.data));
return false;
}
return true;
}
bool SmooshParseModule(JSContext* cx, const uint8_t* bytes, size_t length) {
SmooshParseResult result = smoosh_test_parse_module(bytes, length);
AutoFreeSmooshParseResult afspr(&result);
if (result.error.data) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
result.unimplemented ? JSMSG_SMOOSH_UNIMPLEMENTED
: JSMSG_SMOOSH_COMPILE_ERROR,
reinterpret_cast<const char*>(result.error.data));
return false;
}
return true;
}
} // namespace frontend
} // namespace js