forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIonIC.cpp
503 lines (418 loc) · 16.4 KB
/
IonIC.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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 "jit/IonIC.h"
#include "mozilla/Maybe.h"
#include "jit/CacheIRCompiler.h"
#include "jit/Linker.h"
#include "jit/MacroAssembler-inl.h"
#include "vm/Interpreter-inl.h"
using namespace js;
using namespace js::jit;
using mozilla::Maybe;
void
IonIC::updateBaseAddress(JitCode* code, MacroAssembler& masm)
{
fallbackLabel_.repoint(code, &masm);
rejoinLabel_.repoint(code, &masm);
codeRaw_ = fallbackLabel_.raw();
}
Register
IonIC::scratchRegisterForEntryJump()
{
switch (kind_) {
case CacheKind::GetProp:
case CacheKind::GetElem: {
Register temp = asGetPropertyIC()->maybeTemp();
if (temp != InvalidReg)
return temp;
TypedOrValueRegister output = asGetPropertyIC()->output();
return output.hasValue() ? output.valueReg().scratchReg() : output.typedReg().gpr();
}
case CacheKind::GetPropSuper:
case CacheKind::GetElemSuper: {
TypedOrValueRegister output = asGetPropSuperIC()->output();
return output.valueReg().scratchReg();
}
case CacheKind::SetProp:
case CacheKind::SetElem:
return asSetPropertyIC()->temp();
case CacheKind::GetName:
return asGetNameIC()->temp();
case CacheKind::BindName:
return asBindNameIC()->temp();
case CacheKind::In:
return asInIC()->temp();
case CacheKind::HasOwn:
return asHasOwnIC()->output();
case CacheKind::GetIterator:
return asGetIteratorIC()->temp1();
case CacheKind::Call:
case CacheKind::Compare:
case CacheKind::TypeOf:
MOZ_CRASH("Unsupported IC");
}
MOZ_CRASH("Invalid kind");
}
void
IonIC::discardStubs(Zone* zone)
{
if (firstStub_ && zone->needsIncrementalBarrier()) {
// We are removing edges from IonIC to gcthings. Perform one final trace
// of the stub for incremental GC, as it must know about those edges.
trace(zone->barrierTracer());
}
#ifdef JS_CRASH_DIAGNOSTICS
IonICStub* stub = firstStub_;
while (stub) {
IonICStub* next = stub->next();
stub->poison();
stub = next;
}
#endif
firstStub_ = nullptr;
codeRaw_ = fallbackLabel_.raw();
state_.trackUnlinkedAllStubs();
}
void
IonIC::reset(Zone* zone)
{
discardStubs(zone);
state_.reset();
}
void
IonIC::trace(JSTracer* trc)
{
if (script_)
TraceManuallyBarrieredEdge(trc, &script_, "IonIC::script_");
uint8_t* nextCodeRaw = codeRaw_;
for (IonICStub* stub = firstStub_; stub; stub = stub->next()) {
JitCode* code = JitCode::FromExecutable(nextCodeRaw);
TraceManuallyBarrieredEdge(trc, &code, "ion-ic-code");
TraceCacheIRStub(trc, stub, stub->stubInfo());
nextCodeRaw = stub->nextCodeRaw();
}
MOZ_ASSERT(nextCodeRaw == fallbackLabel_.raw());
}
/* static */ bool
IonGetPropertyIC::update(JSContext* cx, HandleScript outerScript, IonGetPropertyIC* ic,
HandleValue val, HandleValue idVal, MutableHandleValue res)
{
// Override the return value if we are invalidated (bug 728188).
IonScript* ionScript = outerScript->ionScript();
AutoDetectInvalidation adi(cx, res, ionScript);
// If the IC is idempotent, we will redo the op in the interpreter.
if (ic->idempotent())
adi.disable();
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
bool attached = false;
if (ic->state().canAttachStub()) {
// IonBuilder calls PropertyReadNeedsTypeBarrier to determine if it
// needs a type barrier. Unfortunately, PropertyReadNeedsTypeBarrier
// does not account for getters, so we should only attach a getter
// stub if we inserted a type barrier.
jsbytecode* pc = ic->idempotent() ? nullptr : ic->pc();
bool isTemporarilyUnoptimizable = false;
GetPropIRGenerator gen(cx, outerScript, pc, ic->kind(), ic->state().mode(),
&isTemporarilyUnoptimizable, val, idVal, val,
ic->resultFlags());
if (ic->idempotent() ? gen.tryAttachIdempotentStub() : gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached && !isTemporarilyUnoptimizable)
ic->state().trackNotAttached();
}
if (!attached && ic->idempotent()) {
// Invalidate the cache if the property was not found, or was found on
// a non-native object. This ensures:
// 1) The property read has no observable side-effects.
// 2) There's no need to dynamically monitor the return type. This would
// be complicated since (due to GVN) there can be multiple pc's
// associated with a single idempotent cache.
JitSpew(JitSpew_IonIC, "Invalidating from idempotent cache %s:%zu",
outerScript->filename(), outerScript->lineno());
outerScript->setInvalidatedIdempotentCache();
// Do not re-invalidate if the lookup already caused invalidation.
if (outerScript->hasIonScript())
Invalidate(cx, outerScript);
// We will redo the potentially effectful lookup in Baseline.
return true;
}
if (ic->kind() == CacheKind::GetProp) {
RootedPropertyName name(cx, idVal.toString()->asAtom().asPropertyName());
if (!GetProperty(cx, val, name, res))
return false;
} else {
MOZ_ASSERT(ic->kind() == CacheKind::GetElem);
if (!GetElementOperation(cx, JSOp(*ic->pc()), val, idVal, res))
return false;
}
if (!ic->idempotent()) {
// Monitor changes to cache entry.
if (!ic->monitoredResult())
TypeScript::Monitor(cx, ic->script(), ic->pc(), res);
}
return true;
}
/* static */ bool
IonGetPropSuperIC::update(JSContext* cx, HandleScript outerScript, IonGetPropSuperIC* ic,
HandleObject obj, HandleValue receiver, HandleValue idVal, MutableHandleValue res)
{
// Override the return value if we are invalidated (bug 728188).
IonScript* ionScript = outerScript->ionScript();
AutoDetectInvalidation adi(cx, res, ionScript);
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
bool attached = false;
if (ic->state().canAttachStub()) {
RootedValue val(cx, ObjectValue(*obj));
bool isTemporarilyUnoptimizable = false;
GetPropIRGenerator gen(cx, outerScript, ic->pc(), ic->kind(), ic->state().mode(),
&isTemporarilyUnoptimizable, val, idVal, receiver,
GetPropertyResultFlags::All);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached && !isTemporarilyUnoptimizable)
ic->state().trackNotAttached();
}
RootedId id(cx);
if (!ValueToId<CanGC>(cx, idVal, &id))
return false;
if (!GetProperty(cx, obj, receiver, id, res))
return false;
// Monitor changes to cache entry.
TypeScript::Monitor(cx, ic->script(), ic->pc(), res);
return true;
}
/* static */ bool
IonSetPropertyIC::update(JSContext* cx, HandleScript outerScript, IonSetPropertyIC* ic,
HandleObject obj, HandleValue idVal, HandleValue rhs)
{
RootedShape oldShape(cx);
RootedObjectGroup oldGroup(cx);
IonScript* ionScript = outerScript->ionScript();
bool attached = false;
bool isTemporarilyUnoptimizable = false;
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
oldShape = obj->maybeShape();
oldGroup = JSObject::getGroup(cx, obj);
if (!oldGroup)
return false;
if (obj->is<UnboxedPlainObject>()) {
MOZ_ASSERT(!oldShape);
if (UnboxedExpandoObject* expando = obj->as<UnboxedPlainObject>().maybeExpando())
oldShape = expando->lastProperty();
}
RootedValue objv(cx, ObjectValue(*obj));
RootedScript script(cx, ic->script());
jsbytecode* pc = ic->pc();
SetPropIRGenerator gen(cx, script, pc, ic->kind(), ic->state().mode(),
&isTemporarilyUnoptimizable,
objv, idVal, rhs, ic->needsTypeBarrier(), ic->guardHoles());
if (gen.tryAttachStub()) {
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached,
gen.typeCheckInfo());
}
}
jsbytecode* pc = ic->pc();
if (ic->kind() == CacheKind::SetElem) {
if (*pc == JSOP_INITELEM_INC) {
if (!InitArrayElemOperation(cx, pc, obj, idVal.toInt32(), rhs))
return false;
} else if (IsPropertyInitOp(JSOp(*pc))) {
if (!InitElemOperation(cx, pc, obj, idVal, rhs))
return false;
} else {
MOZ_ASSERT(IsPropertySetOp(JSOp(*pc)));
if (!SetObjectElement(cx, obj, idVal, rhs, ic->strict()))
return false;
}
} else {
MOZ_ASSERT(ic->kind() == CacheKind::SetProp);
if (*pc == JSOP_INITGLEXICAL) {
RootedScript script(cx, ic->script());
MOZ_ASSERT(!script->hasNonSyntacticScope());
InitGlobalLexicalOperation(cx, &cx->global()->lexicalEnvironment(), script, pc, rhs);
} else if (IsPropertyInitOp(JSOp(*pc))) {
// This might be a JSOP_INITELEM op with a constant string id. We
// can't call InitPropertyOperation here as that function is
// specialized for JSOP_INIT*PROP (it does not support arbitrary
// objects that might show up here).
if (!InitElemOperation(cx, pc, obj, idVal, rhs))
return false;
} else {
MOZ_ASSERT(IsPropertySetOp(JSOp(*pc)));
RootedPropertyName name(cx, idVal.toString()->asAtom().asPropertyName());
if (!SetProperty(cx, obj, name, rhs, ic->strict(), pc))
return false;
}
}
if (attached)
return true;
// The SetProperty call might have entered this IC recursively, so try
// to transition.
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
RootedValue objv(cx, ObjectValue(*obj));
RootedScript script(cx, ic->script());
jsbytecode* pc = ic->pc();
SetPropIRGenerator gen(cx, script, pc, ic->kind(), ic->state().mode(),
&isTemporarilyUnoptimizable,
objv, idVal, rhs, ic->needsTypeBarrier(), ic->guardHoles());
if (gen.tryAttachAddSlotStub(oldGroup, oldShape)) {
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached,
gen.typeCheckInfo());
} else {
gen.trackNotAttached();
}
if (!attached && !isTemporarilyUnoptimizable)
ic->state().trackNotAttached();
}
return true;
}
/* static */ bool
IonGetNameIC::update(JSContext* cx, HandleScript outerScript, IonGetNameIC* ic,
HandleObject envChain, MutableHandleValue res)
{
IonScript* ionScript = outerScript->ionScript();
jsbytecode* pc = ic->pc();
RootedPropertyName name(cx, ic->script()->getName(pc));
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
bool attached = false;
RootedScript script(cx, ic->script());
GetNameIRGenerator gen(cx, script, pc, ic->state().mode(), envChain, name);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached)
ic->state().trackNotAttached();
}
RootedObject obj(cx);
RootedObject holder(cx);
Rooted<PropertyResult> prop(cx);
if (!LookupName(cx, name, envChain, &obj, &holder, &prop))
return false;
if (*GetNextPc(pc) == JSOP_TYPEOF) {
if (!FetchName<GetNameMode::TypeOf>(cx, obj, holder, name, prop, res))
return false;
} else {
if (!FetchName<GetNameMode::Normal>(cx, obj, holder, name, prop, res))
return false;
}
// No need to call TypeScript::Monitor, IonBuilder always inserts a type
// barrier after GetName ICs.
return true;
}
/* static */ JSObject*
IonBindNameIC::update(JSContext* cx, HandleScript outerScript, IonBindNameIC* ic,
HandleObject envChain)
{
IonScript* ionScript = outerScript->ionScript();
jsbytecode* pc = ic->pc();
RootedPropertyName name(cx, ic->script()->getName(pc));
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
bool attached = false;
RootedScript script(cx, ic->script());
BindNameIRGenerator gen(cx, script, pc, ic->state().mode(), envChain, name);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached)
ic->state().trackNotAttached();
}
RootedObject holder(cx);
if (!LookupNameUnqualified(cx, name, envChain, &holder))
return nullptr;
return holder;
}
/* static */ JSObject*
IonGetIteratorIC::update(JSContext* cx, HandleScript outerScript, IonGetIteratorIC* ic,
HandleValue value)
{
IonScript* ionScript = outerScript->ionScript();
jsbytecode* pc = ic->pc();
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
bool attached = false;
RootedScript script(cx, ic->script());
GetIteratorIRGenerator gen(cx, script, pc, ic->state().mode(), value);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached)
ic->state().trackNotAttached();
}
uint8_t flags = GET_UINT8(pc);
return ValueToIterator(cx, flags, value);
}
/* static */ bool
IonHasOwnIC::update(JSContext* cx, HandleScript outerScript, IonHasOwnIC* ic,
HandleValue val, HandleValue idVal, int32_t* res)
{
IonScript* ionScript = outerScript->ionScript();
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
jsbytecode* pc = ic->pc();
if (ic->state().canAttachStub()) {
bool attached = false;
RootedScript script(cx, ic->script());
HasPropIRGenerator gen(cx, script, pc, CacheKind::HasOwn, ic->state().mode(), idVal, val);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached)
ic->state().trackNotAttached();
}
bool found;
if (!HasOwnProperty(cx, val, idVal, &found))
return false;
*res = found;
return true;
}
/* static */ bool
IonInIC::update(JSContext* cx, HandleScript outerScript, IonInIC* ic,
HandleValue key, HandleObject obj, bool* res)
{
IonScript* ionScript = outerScript->ionScript();
if (ic->state().maybeTransition())
ic->discardStubs(cx->zone());
if (ic->state().canAttachStub()) {
bool attached = false;
RootedScript script(cx, ic->script());
RootedValue objV(cx, ObjectValue(*obj));
jsbytecode* pc = ic->pc();
HasPropIRGenerator gen(cx, script, pc, CacheKind::In, ic->state().mode(), key, objV);
if (gen.tryAttachStub())
ic->attachCacheIRStub(cx, gen.writerRef(), gen.cacheKind(), ionScript, &attached);
if (!attached)
ic->state().trackNotAttached();
}
return OperatorIn(cx, key, obj, res);
}
uint8_t*
IonICStub::stubDataStart()
{
return reinterpret_cast<uint8_t*>(this) + stubInfo_->stubDataOffset();
}
void
IonIC::attachStub(IonICStub* newStub, JitCode* code)
{
MOZ_ASSERT(newStub);
MOZ_ASSERT(code);
if (firstStub_) {
IonICStub* last = firstStub_;
while (IonICStub* next = last->next())
last = next;
last->setNext(newStub, code);
} else {
firstStub_ = newStub;
codeRaw_ = code->raw();
}
state_.trackAttached();
}