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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_CODE_INL_H_
#define V8_OBJECTS_CODE_INL_H_
#include "src/baseline/bytecode-offset-iterator.h"
#include "src/codegen/code-desc.h"
#include "src/heap/heap-write-barrier-inl.h"
#include "src/objects/code.h"
#include "src/objects/deoptimization-data-inl.h"
#include "src/objects/instance-type-inl.h"
#include "src/objects/instruction-stream-inl.h"
#include "src/snapshot/embedded/embedded-data-inl.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
OBJECT_CONSTRUCTORS_IMPL(Code, HeapObject)
OBJECT_CONSTRUCTORS_IMPL(GcSafeCode, HeapObject)
CAST_ACCESSOR(GcSafeCode)
CAST_ACCESSOR(Code)
Tagged<Code> GcSafeCode::UnsafeCastToCode() const {
return Code::unchecked_cast(*this);
}
#define GCSAFE_CODE_FWD_ACCESSOR(ReturnType, Name) \
ReturnType GcSafeCode::Name() const { return UnsafeCastToCode()->Name(); }
GCSAFE_CODE_FWD_ACCESSOR(Address, instruction_start)
GCSAFE_CODE_FWD_ACCESSOR(Address, instruction_end)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_builtin)
GCSAFE_CODE_FWD_ACCESSOR(Builtin, builtin_id)
GCSAFE_CODE_FWD_ACCESSOR(CodeKind, kind)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_interpreter_trampoline_builtin)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_baseline_trampoline_builtin)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_baseline_leave_frame_builtin)
GCSAFE_CODE_FWD_ACCESSOR(bool, has_instruction_stream)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_maglevved)
GCSAFE_CODE_FWD_ACCESSOR(bool, is_turbofanned)
GCSAFE_CODE_FWD_ACCESSOR(bool, has_tagged_outgoing_params)
GCSAFE_CODE_FWD_ACCESSOR(bool, marked_for_deoptimization)
GCSAFE_CODE_FWD_ACCESSOR(Tagged<Object>, raw_instruction_stream)
GCSAFE_CODE_FWD_ACCESSOR(int, stack_slots)
GCSAFE_CODE_FWD_ACCESSOR(Address, constant_pool)
GCSAFE_CODE_FWD_ACCESSOR(Address, safepoint_table_address)
#undef GCSAFE_CODE_FWD_ACCESSOR
int GcSafeCode::GetOffsetFromInstructionStart(Isolate* isolate,
Address pc) const {
return UnsafeCastToCode()->GetOffsetFromInstructionStart(isolate, pc);
}
Address GcSafeCode::InstructionStart(Isolate* isolate, Address pc) const {
return UnsafeCastToCode()->InstructionStart(isolate, pc);
}
Address GcSafeCode::InstructionEnd(Isolate* isolate, Address pc) const {
return UnsafeCastToCode()->InstructionEnd(isolate, pc);
}
Address GcSafeCode::constant_pool(Tagged<InstructionStream> istream) const {
return UnsafeCastToCode()->constant_pool(istream);
}
bool GcSafeCode::CanDeoptAt(Isolate* isolate, Address pc) const {
Tagged<DeoptimizationData> deopt_data = DeoptimizationData::unchecked_cast(
UnsafeCastToCode()->unchecked_deoptimization_data());
Address code_start_address = instruction_start();
for (int i = 0; i < deopt_data->DeoptCount(); i++) {
if (deopt_data->Pc(i).value() == -1) continue;
Address address = code_start_address + deopt_data->Pc(i).value();
if (address == pc && deopt_data->GetBytecodeOffsetOrBuiltinContinuationId(
i) != BytecodeOffset::None()) {
return true;
}
}
return false;
}
Tagged<Object> GcSafeCode::raw_instruction_stream(
PtrComprCageBase code_cage_base) const {
return UnsafeCastToCode()->raw_instruction_stream(code_cage_base);
}
INT_ACCESSORS(Code, instruction_size, kInstructionSizeOffset)
INT_ACCESSORS(Code, metadata_size, kMetadataSizeOffset)
INT_ACCESSORS(Code, handler_table_offset, kHandlerTableOffsetOffset)
INT_ACCESSORS(Code, code_comments_offset, kCodeCommentsOffsetOffset)
INT32_ACCESSORS(Code, unwinding_info_offset, kUnwindingInfoOffsetOffset)
ACCESSORS_CHECKED2(Code, deoptimization_data, Tagged<FixedArray>,
kDeoptimizationDataOrInterpreterDataOffset,
kind() != CodeKind::BASELINE,
kind() != CodeKind::BASELINE &&
!ObjectInYoungGeneration(value))
ACCESSORS_CHECKED2(Code, bytecode_or_interpreter_data, Tagged<HeapObject>,
kDeoptimizationDataOrInterpreterDataOffset,
kind() == CodeKind::BASELINE,
kind() == CodeKind::BASELINE &&
!ObjectInYoungGeneration(value))
ACCESSORS_CHECKED2(Code, source_position_table, Tagged<ByteArray>,
kPositionTableOffset, kind() != CodeKind::BASELINE,
kind() != CodeKind::BASELINE &&
!ObjectInYoungGeneration(value))
ACCESSORS_CHECKED2(Code, bytecode_offset_table, Tagged<ByteArray>,
kPositionTableOffset, kind() == CodeKind::BASELINE,
kind() == CodeKind::BASELINE &&
!ObjectInYoungGeneration(value))
Tagged<ByteArray> Code::SourcePositionTable(
Isolate* isolate, Tagged<SharedFunctionInfo> sfi) const {
if (!has_instruction_stream()) {
return GetReadOnlyRoots().empty_byte_array();
}
DisallowGarbageCollection no_gc;
if (kind() == CodeKind::BASELINE) {
return sfi->GetBytecodeArray(isolate)->SourcePositionTable(isolate);
}
return source_position_table(isolate);
}
Address Code::body_start() const { return instruction_start(); }
Address Code::body_end() const { return body_start() + body_size(); }
int Code::body_size() const { return instruction_size() + metadata_size(); }
Address Code::instruction_end() const {
return instruction_start() + instruction_size();
}
Address Code::metadata_start() const {
if (has_instruction_stream()) {
static_assert(InstructionStream::kOnHeapBodyIsContiguous);
return instruction_start() + instruction_size();
}
// An embedded builtin. Remapping is irrelevant wrt the metadata section so
// we can simply use the global blob.
// TODO(jgruber): Consider adding this as a physical Code field to avoid the
// lookup. Alternatively, rename this (and callers) to camel-case to clarify
// it's more than a simple accessor.
static_assert(!InstructionStream::kOffHeapBodyIsContiguous);
return EmbeddedData::FromBlob().MetadataStartOf(builtin_id());
}
Address Code::InstructionStart(Isolate* isolate, Address pc) const {
if (V8_LIKELY(has_instruction_stream())) return instruction_start();
// Note we intentionally don't bounds-check that `pc` is within the returned
// instruction area.
return EmbeddedData::FromBlobForPc(isolate, pc)
.InstructionStartOf(builtin_id());
}
Address Code::InstructionEnd(Isolate* isolate, Address pc) const {
return InstructionStart(isolate, pc) + instruction_size();
}
int Code::GetOffsetFromInstructionStart(Isolate* isolate, Address pc) const {
const Address offset = pc - InstructionStart(isolate, pc);
DCHECK_LE(offset, instruction_size());
return static_cast<int>(offset);
}
Address Code::metadata_end() const {
return metadata_start() + metadata_size();
}
Address Code::safepoint_table_address() const {
return metadata_start() + safepoint_table_offset();
}
int Code::safepoint_table_size() const {
return handler_table_offset() - safepoint_table_offset();
}
bool Code::has_safepoint_table() const { return safepoint_table_size() > 0; }
Address Code::handler_table_address() const {
return metadata_start() + handler_table_offset();
}
int Code::handler_table_size() const {
return constant_pool_offset() - handler_table_offset();
}
bool Code::has_handler_table() const { return handler_table_size() > 0; }
int Code::constant_pool_size() const {
const int size = code_comments_offset() - constant_pool_offset();
if (!V8_EMBEDDED_CONSTANT_POOL_BOOL) {
DCHECK_EQ(size, 0);
return 0;
}
DCHECK_GE(size, 0);
return size;
}
bool Code::has_constant_pool() const { return constant_pool_size() > 0; }
Tagged<FixedArray> Code::unchecked_deoptimization_data() const {
return FixedArray::unchecked_cast(
TaggedField<HeapObject, kDeoptimizationDataOrInterpreterDataOffset>::load(
*this));
}
uint8_t* Code::relocation_start() const {
return V8_LIKELY(has_instruction_stream())
? instruction_stream()->relocation_start()
: nullptr;
}
uint8_t* Code::relocation_end() const {
return V8_LIKELY(has_instruction_stream())
? instruction_stream()->relocation_end()
: nullptr;
}
int Code::relocation_size() const {
return V8_LIKELY(has_instruction_stream())
? instruction_stream()->relocation_size()
: 0;
}
bool Code::contains(Isolate* isolate, Address inner_pointer) const {
const Address start = InstructionStart(isolate, inner_pointer);
if (inner_pointer < start) return false;
return inner_pointer < start + instruction_size();
}
int Code::InstructionStreamObjectSize() const {
return InstructionStream::SizeFor(body_size());
}
int Code::SizeIncludingMetadata() const {
int size = InstructionStreamObjectSize();
size += relocation_size();
if (kind() != CodeKind::BASELINE) {
size += deoptimization_data()->Size();
}
return size;
}
CodeKind Code::kind() const { return KindField::decode(flags(kRelaxedLoad)); }
int Code::GetBytecodeOffsetForBaselinePC(Address baseline_pc,
Tagged<BytecodeArray> bytecodes) {
DisallowGarbageCollection no_gc;
CHECK(!is_baseline_trampoline_builtin());
if (is_baseline_leave_frame_builtin()) return kFunctionExitBytecodeOffset;
CHECK_EQ(kind(), CodeKind::BASELINE);
baseline::BytecodeOffsetIterator offset_iterator(
ByteArray::cast(bytecode_offset_table()), bytecodes);
Address pc = baseline_pc - instruction_start();
offset_iterator.AdvanceToPCOffset(pc);
return offset_iterator.current_bytecode_offset();
}
uintptr_t Code::GetBaselinePCForBytecodeOffset(
int bytecode_offset, BytecodeToPCPosition position,
Tagged<BytecodeArray> bytecodes) {
DisallowGarbageCollection no_gc;
CHECK_EQ(kind(), CodeKind::BASELINE);
baseline::BytecodeOffsetIterator offset_iterator(
ByteArray::cast(bytecode_offset_table()), bytecodes);
offset_iterator.AdvanceToBytecodeOffset(bytecode_offset);
uintptr_t pc = 0;
if (position == kPcAtStartOfBytecode) {
pc = offset_iterator.current_pc_start_offset();
} else {
DCHECK_EQ(position, kPcAtEndOfBytecode);
pc = offset_iterator.current_pc_end_offset();
}
return pc;
}
uintptr_t Code::GetBaselineStartPCForBytecodeOffset(
int bytecode_offset, Tagged<BytecodeArray> bytecodes) {
return GetBaselinePCForBytecodeOffset(bytecode_offset, kPcAtStartOfBytecode,
bytecodes);
}
uintptr_t Code::GetBaselineEndPCForBytecodeOffset(
int bytecode_offset, Tagged<BytecodeArray> bytecodes) {
return GetBaselinePCForBytecodeOffset(bytecode_offset, kPcAtEndOfBytecode,
bytecodes);
}
uintptr_t Code::GetBaselinePCForNextExecutedBytecode(
int bytecode_offset, Tagged<BytecodeArray> bytecodes) {
DisallowGarbageCollection no_gc;
CHECK_EQ(kind(), CodeKind::BASELINE);
baseline::BytecodeOffsetIterator offset_iterator(
ByteArray::cast(bytecode_offset_table()), bytecodes);
Handle<BytecodeArray> bytecodes_handle(
reinterpret_cast<Address*>(&bytecodes));
interpreter::BytecodeArrayIterator bytecode_iterator(bytecodes_handle,
bytecode_offset);
interpreter::Bytecode bytecode = bytecode_iterator.current_bytecode();
if (bytecode == interpreter::Bytecode::kJumpLoop) {
return GetBaselineStartPCForBytecodeOffset(
bytecode_iterator.GetJumpTargetOffset(), bytecodes);
} else {
DCHECK(!interpreter::Bytecodes::IsJump(bytecode));
DCHECK(!interpreter::Bytecodes::IsSwitch(bytecode));
DCHECK(!interpreter::Bytecodes::Returns(bytecode));
return GetBaselineEndPCForBytecodeOffset(bytecode_offset, bytecodes);
}
}
inline bool Code::checks_tiering_state() const {
bool checks_state = (builtin_id() == Builtin::kCompileLazy ||
builtin_id() == Builtin::kInterpreterEntryTrampoline ||
CodeKindCanTierUp(kind()));
return checks_state ||
(CodeKindCanDeoptimize(kind()) && marked_for_deoptimization());
}
inline constexpr bool CodeKindHasTaggedOutgoingParams(CodeKind kind) {
return kind != CodeKind::JS_TO_WASM_FUNCTION &&
kind != CodeKind::C_WASM_ENTRY && kind != CodeKind::WASM_FUNCTION;
}
inline bool Code::has_tagged_outgoing_params() const {
#if V8_ENABLE_WEBASSEMBLY
return CodeKindHasTaggedOutgoingParams(kind()) &&
builtin_id() != Builtin::kWasmCompileLazy;
#else
return CodeKindHasTaggedOutgoingParams(kind());
#endif
}
inline bool Code::is_turbofanned() const {
return IsTurbofannedField::decode(flags(kRelaxedLoad));
}
inline bool Code::is_maglevved() const { return kind() == CodeKind::MAGLEV; }
unsigned Code::inlined_bytecode_size() const {
unsigned size = RELAXED_READ_UINT_FIELD(*this, kInlinedBytecodeSizeOffset);
DCHECK(CodeKindIsOptimizedJSFunction(kind()) || size == 0);
return size;
}
void Code::set_inlined_bytecode_size(unsigned size) {
DCHECK(CodeKindIsOptimizedJSFunction(kind()) || size == 0);
RELAXED_WRITE_UINT_FIELD(*this, kInlinedBytecodeSizeOffset, size);
}
BytecodeOffset Code::osr_offset() const {
return BytecodeOffset(RELAXED_READ_INT32_FIELD(*this, kOsrOffsetOffset));
}
void Code::set_osr_offset(BytecodeOffset offset) {
RELAXED_WRITE_INT32_FIELD(*this, kOsrOffsetOffset, offset.ToInt());
}
bool Code::uses_safepoint_table() const {
return is_turbofanned() || is_maglevved() || is_wasm_code();
}
int Code::stack_slots() const {
const int slots = StackSlotsField::decode(flags(kRelaxedLoad));
DCHECK_IMPLIES(!uses_safepoint_table(), slots == 0);
return slots;
}
bool Code::marked_for_deoptimization() const {
return MarkedForDeoptimizationField::decode(flags(kRelaxedLoad));
}
void Code::set_marked_for_deoptimization(bool flag) {
DCHECK_IMPLIES(flag, AllowDeoptimization::IsAllowed(
GetIsolateFromWritableObject(*this)));
int32_t previous = flags(kRelaxedLoad);
int32_t updated = MarkedForDeoptimizationField::update(previous, flag);
set_flags(updated, kRelaxedStore);
}
bool Code::embedded_objects_cleared() const {
return Code::EmbeddedObjectsClearedField::decode(flags(kRelaxedLoad));
}
void Code::set_embedded_objects_cleared(bool flag) {
DCHECK_IMPLIES(flag, marked_for_deoptimization());
int32_t previous = flags(kRelaxedLoad);
int32_t updated = Code::EmbeddedObjectsClearedField::update(previous, flag);
set_flags(updated, kRelaxedStore);
}
inline bool Code::can_have_weak_objects() const {
return CanHaveWeakObjectsField::decode(flags(kRelaxedLoad));
}
inline void Code::set_can_have_weak_objects(bool value) {
int32_t previous = flags(kRelaxedLoad);
int32_t updated = CanHaveWeakObjectsField::update(previous, value);
set_flags(updated, kRelaxedStore);
}
bool Code::is_wasm_code() const { return kind() == CodeKind::WASM_FUNCTION; }
int Code::constant_pool_offset() const {
if (!V8_EMBEDDED_CONSTANT_POOL_BOOL) {
// Redirection needed since the field doesn't exist in this case.
return code_comments_offset();
}
return ReadField<int>(kConstantPoolOffsetOffset);
}
void Code::set_constant_pool_offset(int value) {
if (!V8_EMBEDDED_CONSTANT_POOL_BOOL) {
// Redirection needed since the field doesn't exist in this case.
return;
}
DCHECK_LE(value, metadata_size());
WriteField<int>(kConstantPoolOffsetOffset, value);
}
Address Code::constant_pool() const {
if (!has_constant_pool()) return kNullAddress;
return metadata_start() + constant_pool_offset();
}
Address Code::constant_pool(
Tagged<InstructionStream> instruction_stream) const {
if (!has_constant_pool()) return kNullAddress;
static_assert(InstructionStream::kOnHeapBodyIsContiguous);
return instruction_stream->instruction_start() + instruction_size() +
constant_pool_offset();
}
Address Code::code_comments() const {
return metadata_start() + code_comments_offset();
}
int Code::code_comments_size() const {
return unwinding_info_offset() - code_comments_offset();
}
bool Code::has_code_comments() const { return code_comments_size() > 0; }
Address Code::unwinding_info_start() const {
return metadata_start() + unwinding_info_offset();
}
Address Code::unwinding_info_end() const { return metadata_end(); }
int Code::unwinding_info_size() const {
return static_cast<int>(unwinding_info_end() - unwinding_info_start());
}
bool Code::has_unwinding_info() const { return unwinding_info_size() > 0; }
// static
Tagged<Code> Code::FromTargetAddress(Address address) {
return InstructionStream::FromTargetAddress(address)->code(kAcquireLoad);
}
bool Code::CanContainWeakObjects() {
return is_optimized_code() && can_have_weak_objects();
}
bool Code::IsWeakObject(Tagged<HeapObject> object) {
return (CanContainWeakObjects() && IsWeakObjectInOptimizedCode(object));
}
bool Code::IsWeakObjectInOptimizedCode(Tagged<HeapObject> object) {
Tagged<Map> map_object = object->map(kAcquireLoad);
if (InstanceTypeChecker::IsMap(map_object)) {
return Map::cast(object)->CanTransition();
}
return InstanceTypeChecker::IsPropertyCell(map_object) ||
InstanceTypeChecker::IsJSReceiver(map_object) ||
InstanceTypeChecker::IsContext(map_object);
}
bool Code::IsWeakObjectInDeoptimizationLiteralArray(Tagged<Object> object) {
// Maps must be strong because they can be used as part of the description for
// how to materialize an object upon deoptimization, in which case it is
// possible to reach the code that requires the Map without anything else
// holding a strong pointer to that Map.
return IsHeapObject(object) && !IsMap(object) &&
Code::IsWeakObjectInOptimizedCode(HeapObject::cast(object));
}
void Code::IterateDeoptimizationLiterals(RootVisitor* v) {
if (kind() == CodeKind::BASELINE) return;
auto deopt_data = DeoptimizationData::cast(deoptimization_data());
if (deopt_data->length() == 0) return;
Tagged<DeoptimizationLiteralArray> literals = deopt_data->LiteralArray();
const int literals_length = literals->length();
for (int i = 0; i < literals_length; ++i) {
MaybeObject maybe_literal = literals->Get(i);
Tagged<HeapObject> heap_literal;
if (maybe_literal.GetHeapObject(&heap_literal)) {
v->VisitRootPointer(Root::kStackRoots, "deoptimization literal",
FullObjectSlot(&heap_literal));
}
}
}
Tagged<Object> Code::raw_instruction_stream() const {
PtrComprCageBase cage_base = code_cage_base();
return Code::raw_instruction_stream(cage_base);
}
Tagged<Object> Code::raw_instruction_stream(PtrComprCageBase cage_base) const {
return ExternalCodeField<Object>::load(cage_base, *this);
}
void Code::set_raw_instruction_stream(Tagged<Object> value,
WriteBarrierMode mode) {
ExternalCodeField<Object>::Release_Store(*this, value);
CONDITIONAL_WRITE_BARRIER(*this, kInstructionStreamOffset, value, mode);
}
bool Code::has_instruction_stream() const {
#if defined(V8_COMPRESS_POINTERS) || !defined(V8_HOST_ARCH_64_BIT)
const uint32_t value = ReadField<uint32_t>(kInstructionStreamOffset);
#else
const uint64_t value = ReadField<uint64_t>(kInstructionStreamOffset);
#endif
SLOW_DCHECK(value == 0 || !InReadOnlySpace());
return value != 0;
}
bool Code::has_instruction_stream(RelaxedLoadTag tag) const {
#if defined(V8_COMPRESS_POINTERS) || !defined(V8_HOST_ARCH_64_BIT)
const uint32_t value =
RELAXED_READ_INT32_FIELD(*this, kInstructionStreamOffset);
#else
const uint64_t value =
RELAXED_READ_INT64_FIELD(*this, kInstructionStreamOffset);
#endif
SLOW_DCHECK(value == 0 || !InReadOnlySpace());
return value != 0;
}
PtrComprCageBase Code::code_cage_base() const {
#ifdef V8_EXTERNAL_CODE_SPACE
return PtrComprCageBase(ExternalCodeCompressionScheme::base());
#else // V8_EXTERNAL_CODE_SPACE
// Without external code space: `code_cage_base == main_cage_base`. We can
// get the main cage base from any heap object, including objects in RO
// space.
return GetPtrComprCageBase(*this);
#endif // V8_EXTERNAL_CODE_SPACE
}
Tagged<InstructionStream> Code::instruction_stream() const {
PtrComprCageBase cage_base = code_cage_base();
return Code::instruction_stream(cage_base);
}
Tagged<InstructionStream> Code::unchecked_instruction_stream() const {
return InstructionStream::unchecked_cast(raw_instruction_stream());
}
Tagged<InstructionStream> Code::instruction_stream(
PtrComprCageBase cage_base) const {
DCHECK(has_instruction_stream());
return ExternalCodeField<InstructionStream>::load(cage_base, *this);
}
Tagged<InstructionStream> Code::instruction_stream(RelaxedLoadTag tag) const {
PtrComprCageBase cage_base = code_cage_base();
return Code::instruction_stream(cage_base, tag);
}
Tagged<InstructionStream> Code::instruction_stream(PtrComprCageBase cage_base,
RelaxedLoadTag tag) const {
DCHECK(has_instruction_stream());
return ExternalCodeField<InstructionStream>::Relaxed_Load(cage_base, *this);
}
Tagged<Object> Code::raw_instruction_stream(RelaxedLoadTag tag) const {
PtrComprCageBase cage_base = code_cage_base();
return Code::raw_instruction_stream(cage_base, tag);
}
Tagged<Object> Code::raw_instruction_stream(PtrComprCageBase cage_base,
RelaxedLoadTag tag) const {
return ExternalCodeField<Object>::Relaxed_Load(cage_base, *this);
}
DEF_GETTER(Code, instruction_start, Address) {
return ReadCodeEntrypointField(kInstructionStartOffset);
}
void Code::init_instruction_start(Isolate* isolate, Address value) {
#ifdef V8_CODE_POINTER_SANDBOXING
// In this case, the instruction_start is stored in this Code's code pointer
// table entry, so initialize that instead.
InitCodePointerTableEntryField(kCodePointerTableEntryOffset, isolate, *this,
value);
#else
WriteCodeEntrypointField(kInstructionStartOffset, value);
#endif
}
void Code::set_instruction_start(Isolate* isolate, Address value) {
WriteCodeEntrypointField(kInstructionStartOffset, value);
}
void Code::SetInstructionStreamAndInstructionStart(
Isolate* isolate_for_sandbox, Tagged<InstructionStream> code,
WriteBarrierMode mode) {
set_raw_instruction_stream(code, mode);
set_instruction_start(isolate_for_sandbox, code->instruction_start());
}
void Code::SetInstructionStartForOffHeapBuiltin(Isolate* isolate_for_sandbox,
Address entry) {
DCHECK(!has_instruction_stream());
set_instruction_start(isolate_for_sandbox, entry);
}
void Code::ClearInstructionStartForSerialization(Isolate* isolate) {
#ifdef V8_CODE_POINTER_SANDBOXING
WriteField<CodePointerHandle>(kInstructionStartOffset,
kNullCodePointerHandle);
#else
set_instruction_start(isolate, kNullAddress);
#endif // V8_CODE_POINTER_SANDBOXING
}
void Code::UpdateInstructionStart(Isolate* isolate_for_sandbox,
Tagged<InstructionStream> istream) {
DCHECK_EQ(raw_instruction_stream(), istream);
set_instruction_start(isolate_for_sandbox, istream->instruction_start());
}
void Code::clear_padding() {
memset(reinterpret_cast<void*>(address() + kUnalignedSize), 0,
kSize - kUnalignedSize);
}
RELAXED_UINT32_ACCESSORS(Code, flags, kFlagsOffset)
void Code::initialize_flags(CodeKind kind, bool is_turbofanned,
int stack_slots) {
CHECK(0 <= stack_slots && stack_slots < StackSlotsField::kMax);
DCHECK(!CodeKindIsInterpretedJSFunction(kind));
uint32_t value = KindField::encode(kind) |
IsTurbofannedField::encode(is_turbofanned) |
StackSlotsField::encode(stack_slots);
static_assert(FIELD_SIZE(kFlagsOffset) == kInt32Size);
set_flags(value, kRelaxedStore);
DCHECK_IMPLIES(stack_slots != 0, uses_safepoint_table());
DCHECK_IMPLIES(!uses_safepoint_table(), stack_slots == 0);
}
// Ensure builtin_id field fits into int16_t, so that we can rely on sign
// extension to convert int16_t{-1} to kNoBuiltinId.
// If the asserts fail, update the code that use kBuiltinIdOffset below.
static_assert(static_cast<int>(Builtin::kNoBuiltinId) == -1);
static_assert(Builtins::kBuiltinCount < std::numeric_limits<int16_t>::max());
void Code::set_builtin_id(Builtin builtin_id) {
static_assert(FIELD_SIZE(kBuiltinIdOffset) == kInt16Size);
Relaxed_WriteField<int16_t>(kBuiltinIdOffset,
static_cast<int16_t>(builtin_id));
}
Builtin Code::builtin_id() const {
// Rely on sign-extension when converting int16_t to int to preserve
// kNoBuiltinId value.
static_assert(FIELD_SIZE(kBuiltinIdOffset) == kInt16Size);
static_assert(static_cast<int>(static_cast<int16_t>(Builtin::kNoBuiltinId)) ==
static_cast<int>(Builtin::kNoBuiltinId));
int value = ReadField<int16_t>(kBuiltinIdOffset);
return static_cast<Builtin>(value);
}
bool Code::is_builtin() const { return builtin_id() != Builtin::kNoBuiltinId; }
bool Code::is_optimized_code() const {
return CodeKindIsOptimizedJSFunction(kind());
}
inline bool Code::is_interpreter_trampoline_builtin() const {
return IsInterpreterTrampolineBuiltin(builtin_id());
}
inline bool Code::is_baseline_trampoline_builtin() const {
return IsBaselineTrampolineBuiltin(builtin_id());
}
inline bool Code::is_baseline_leave_frame_builtin() const {
return builtin_id() == Builtin::kBaselineLeaveFrame;
}
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_CODE_INL_H_
|