forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-inl.h
More file actions
955 lines (799 loc) · 34.5 KB
/
Copy pathcode-inl.h
File metadata and controls
955 lines (799 loc) · 34.5 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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
// 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/objects/code.h"
// Include the non-inl header before the rest of the headers.
#include "src/baseline/bytecode-offset-iterator.h"
#include "src/codegen/code-desc.h"
#include "src/deoptimizer/deoptimize-reason.h"
#include "src/heap/heap-layout-inl.h"
#include "src/heap/heap-write-barrier-inl.h"
#include "src/objects/deoptimization-data-inl.h"
#include "src/objects/instance-type-inl.h"
#include "src/objects/instruction-stream-inl.h"
#include "src/objects/trusted-object-inl.h"
#include "src/objects/trusted-pointer-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, ExposedTrustedObject)
OBJECT_CONSTRUCTORS_IMPL(GcSafeCode, HeapObject)
Tagged<Code> GcSafeCode::UnsafeCastToCode() const {
return UncheckedCast<Code>(*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(uint32_t, stack_slots)
GCSAFE_CODE_FWD_ACCESSOR(uint16_t, parameter_count)
GCSAFE_CODE_FWD_ACCESSOR(uint16_t, parameter_count_without_receiver)
GCSAFE_CODE_FWD_ACCESSOR(uint16_t, wasm_js_tagged_parameter_count)
GCSAFE_CODE_FWD_ACCESSOR(uint16_t, wasm_js_first_tagged_parameter)
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);
}
bool GcSafeCode::CanDeoptAt(Isolate* isolate, Address pc) const {
if (!UnsafeCastToCode()->uses_deoptimization_data()) return false;
Tagged<DeoptimizationData> deopt_data = UncheckedCast<DeoptimizationData>(
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)
UINT16_ACCESSORS(Code, parameter_count, kParameterCountOffset)
inline uint16_t Code::parameter_count_without_receiver() const {
return parameter_count() - 1;
}
inline Tagged<DeoptimizationData> Code::deoptimization_data() const {
// It's important to CHECK that the Code object uses deoptimization data. We
// trust optimized code to have deoptimization data here, but the reference to
// this code might be corrupted, such that we get type confusion on this field
// in cases where we assume that it must be optimized code.
SBXCHECK(uses_deoptimization_data());
return TrustedCast<DeoptimizationData>(
ReadProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset));
}
inline void Code::set_deoptimization_data(Tagged<DeoptimizationData> value,
WriteBarrierMode mode) {
SBXCHECK(uses_deoptimization_data());
DCHECK(!HeapLayout::InYoungGeneration(value));
WriteProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset, value);
CONDITIONAL_PROTECTED_POINTER_WRITE_BARRIER(
*this, kDeoptimizationDataOrInterpreterDataOffset, value, mode);
}
inline bool Code::uses_deoptimization_data() const {
return CodeKindUsesDeoptimizationData(kind());
}
inline void Code::clear_deoptimization_data_and_interpreter_data() {
ClearProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset);
}
inline bool Code::has_deoptimization_data_or_interpreter_data() const {
return !IsProtectedPointerFieldEmpty(
kDeoptimizationDataOrInterpreterDataOffset);
}
Tagged<TrustedObject> Code::bytecode_or_interpreter_data() const {
// It's important to CHECK that the Code object is baseline code. We trust
// baseline code to have bytecode/interpreter data here, but the reference to
// this code might be corrupted, such that we get type confusion on this field
// in cases where we assume that it must be baseline code.
SBXCHECK_EQ(kind(), CodeKind::BASELINE);
return ReadProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset);
}
void Code::set_bytecode_or_interpreter_data(Tagged<TrustedObject> value,
WriteBarrierMode mode) {
SBXCHECK_EQ(kind(), CodeKind::BASELINE);
DCHECK(IsBytecodeArray(value) || IsInterpreterData(value));
WriteProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset, value);
CONDITIONAL_PROTECTED_POINTER_WRITE_BARRIER(
*this, kDeoptimizationDataOrInterpreterDataOffset, value, mode);
}
inline Tagged<TrustedByteArray> Code::source_position_table() const {
DCHECK(has_source_position_table());
return ReadProtectedPointerField<TrustedByteArray>(kPositionTableOffset);
}
inline void Code::set_source_position_table(Tagged<TrustedByteArray> value,
WriteBarrierMode mode) {
DCHECK(!CodeKindUsesBytecodeOffsetTable(kind()));
WriteProtectedPointerField(kPositionTableOffset, value);
CONDITIONAL_PROTECTED_POINTER_WRITE_BARRIER(*this, kPositionTableOffset,
value, mode);
}
inline Tagged<TrustedByteArray> Code::bytecode_offset_table() const {
DCHECK(has_bytecode_offset_table());
return ReadProtectedPointerField<TrustedByteArray>(kPositionTableOffset);
}
inline void Code::set_bytecode_offset_table(Tagged<TrustedByteArray> value,
WriteBarrierMode mode) {
DCHECK(CodeKindUsesBytecodeOffsetTable(kind()));
WriteProtectedPointerField(kPositionTableOffset, value);
CONDITIONAL_PROTECTED_POINTER_WRITE_BARRIER(*this, kPositionTableOffset,
value, mode);
}
bool Code::has_source_position_table_or_bytecode_offset_table() const {
return TaggedField<Object, kPositionTableOffset>::load(*this) != Smi::zero();
}
bool Code::has_source_position_table() const {
bool has_table = has_source_position_table_or_bytecode_offset_table() &&
!CodeKindUsesBytecodeOffsetTable(kind());
DCHECK_IMPLIES(!CodeKindMayLackSourcePositionTable(kind()), has_table);
return has_table;
}
bool Code::has_bytecode_offset_table() const {
return has_source_position_table_or_bytecode_offset_table() &&
CodeKindUsesBytecodeOffsetTable(kind());
}
void Code::clear_source_position_table_and_bytecode_offset_table() {
TaggedField<Object, kPositionTableOffset>::store(*this, Smi::zero());
}
ACCESSORS(Code, wrapper, Tagged<CodeWrapper>, kWrapperOffset)
Tagged<TrustedByteArray> Code::SourcePositionTable(
Isolate* isolate, Tagged<SharedFunctionInfo> sfi) const {
DisallowGarbageCollection no_gc;
if (kind() == CodeKind::BASELINE) {
return sfi->GetBytecodeArray(isolate)->SourcePositionTable(isolate);
}
if (!has_source_position_table()) {
return *isolate->factory()->empty_trusted_byte_array();
}
return source_position_table();
}
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<ProtectedFixedArray> Code::unchecked_deoptimization_data() const {
return UncheckedCast<ProtectedFixedArray>(
ReadProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset));
}
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 (uses_deoptimization_data()) {
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(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);
// The following check ties together the bytecode being executed in
// Generate_BaselineOrInterpreterEntry with the bytecode that was used to
// compile this baseline code. Together, this ensures that we don't OSR into a
// wrong code object.
auto maybe_bytecodes = bytecode_or_interpreter_data();
if (IsBytecodeArray(maybe_bytecodes)) {
SBXCHECK_EQ(maybe_bytecodes, bytecodes);
} else {
CHECK(IsInterpreterData(maybe_bytecodes));
SBXCHECK_EQ(TrustedCast<InterpreterData>(maybe_bytecodes)->bytecode_array(),
bytecodes);
}
baseline::BytecodeOffsetIterator offset_iterator(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(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_disabled_builtin() const {
return IsDisabledBuiltinField::decode(flags(kRelaxedLoad));
}
inline void Code::set_is_disabled_builtin(bool value) {
DCHECK(is_builtin());
int32_t previous = flags(kRelaxedLoad);
int32_t updated = IsDisabledBuiltinField::update(previous, value);
set_flags(updated, kRelaxedStore);
}
inline bool Code::is_context_specialized() const {
return IsContextSpecializedField::decode(flags(kRelaxedLoad));
}
#if V8_ENABLE_GEARBOX
inline bool Code::is_gearbox_placeholder_builtin() const {
return IsGearboxPlaceholderField::decode(flags(kRelaxedLoad));
}
void Code::set_is_gearbox_placeholder_builtin(bool flag) {
// We should only invoke the setter when we serializing the placeholder object
// in mksnapshot.
DCHECK_IMPLIES(flag, Builtins::IsGearboxPlaceholder(builtin_id()));
int32_t previous = flags(kRelaxedLoad);
int32_t updated = IsGearboxPlaceholderField::update(previous, flag);
set_flags(updated, kRelaxedStore);
}
#endif // V8_ENABLE_GEARBOX
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);
}
// For optimized on-heap wasm-js wrappers, we repurpose the (otherwise unused)
// 32-bit InlinedBytecodeSize field to encode two 16 values needed for scanning
// the frame: the count and starting offset of incoming tagged parameters.
// TODO(wasm): Eventually the wrappers should be managed off-heap by the wasm
// engine. Remove these accessors when that is the case.
void Code::set_wasm_js_tagged_parameter_count(uint16_t count) {
DCHECK_EQ(kind(), CodeKind::WASM_TO_JS_FUNCTION);
RELAXED_WRITE_UINT16_FIELD(*this, kInlinedBytecodeSizeOffset, count);
}
uint16_t Code::wasm_js_tagged_parameter_count() const {
DCHECK_EQ(kind(), CodeKind::WASM_TO_JS_FUNCTION);
return RELAXED_READ_UINT16_FIELD(*this, kInlinedBytecodeSizeOffset);
}
void Code::set_wasm_js_first_tagged_parameter(uint16_t count) {
DCHECK_EQ(kind(), CodeKind::WASM_TO_JS_FUNCTION);
RELAXED_WRITE_UINT16_FIELD(*this, kInlinedBytecodeSizeOffset + 2, count);
}
uint16_t Code::wasm_js_first_tagged_parameter() const {
DCHECK_EQ(kind(), CodeKind::WASM_TO_JS_FUNCTION);
return RELAXED_READ_UINT16_FIELD(*this, kInlinedBytecodeSizeOffset + 2);
}
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();
}
uint32_t Code::stack_slots() const {
DCHECK_IMPLIES(safepoint_table_size() > 0, uses_safepoint_table());
if (safepoint_table_size() == 0) return 0;
DCHECK(safepoint_table_size() >=
static_cast<int>(sizeof(SafepointTableStackSlotsField_t)));
static_assert(kSafepointTableStackSlotsOffset == 0);
return base::Memory<SafepointTableStackSlotsField_t>(
safepoint_table_address() + kSafepointTableStackSlotsOffset);
}
bool Code::marked_for_deoptimization() const {
return MarkedForDeoptimizationField::decode(flags(kRelaxedLoad));
}
void Code::set_marked_for_deoptimization(bool flag) {
DCHECK_IMPLIES(flag, AllowDeoptimization::IsAllowed(Isolate::Current()));
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::code_comments() const {
return metadata_start() + code_comments_offset();
}
int Code::code_comments_size() const {
return jump_table_info_offset() - code_comments_offset();
}
bool Code::has_code_comments() const { return code_comments_size() > 0; }
int32_t Code::jump_table_info_offset() const {
if constexpr (!V8_JUMP_TABLE_INFO_BOOL) {
// Redirection needed since the field doesn't exist in this case.
return unwinding_info_offset();
}
return ReadField<int32_t>(kJumpTableInfoOffsetOffset);
}
void Code::set_jump_table_info_offset(int32_t value) {
if constexpr (!V8_JUMP_TABLE_INFO_BOOL) {
// Redirection needed since the field doesn't exist in this case.
return;
}
DCHECK_LE(value, metadata_size());
WriteField<int32_t>(kJumpTableInfoOffsetOffset, value);
}
Address Code::jump_table_info() const {
return metadata_start() + jump_table_info_offset();
}
int Code::jump_table_info_size() const {
return unwinding_info_offset() - jump_table_info_offset();
}
bool Code::has_jump_table_info() const { return jump_table_info_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 Cast<Map>(object)->CanTransition();
}
return InstanceTypeChecker::IsPropertyCell(map_object) ||
InstanceTypeChecker::IsJSReceiver(map_object) ||
InstanceTypeChecker::IsContext(map_object);
}
bool Code::IsWeakObjectInOptimizedCode(JSDispatchHandle) {
// Dispatch handles are always treated weakly in optimized code.
DCHECK(is_optimized_code());
return true;
}
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(Cast<HeapObject>(object));
}
void Code::IterateDeoptimizationLiterals(RootVisitor* v) {
if (!uses_deoptimization_data()) {
DCHECK(kind() == CodeKind::BASELINE ||
!has_deoptimization_data_or_interpreter_data());
return;
}
auto deopt_data = 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) {
Tagged<MaybeObject> maybe_literal = literals->get_raw(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 || !HeapLayout::InReadOnlySpace(*this));
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 || !HeapLayout::InReadOnlySpace(*this));
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 UncheckedCast<InstructionStream>(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) {
#ifdef V8_ENABLE_SANDBOX
return ReadCodeEntrypointViaCodePointerField(kSelfIndirectPointerOffset,
entrypoint_tag());
#else
return ReadField<Address>(kInstructionStartOffset);
#endif
}
void Code::set_instruction_start(IsolateForSandbox isolate, Address value) {
#ifdef V8_ENABLE_SANDBOX
WriteCodeEntrypointViaCodePointerField(kSelfIndirectPointerOffset, value,
entrypoint_tag());
#else
WriteField<Address>(kInstructionStartOffset, value);
#endif
}
CodeEntrypointTag Code::entrypoint_tag() const {
// TODO(40948502, sandbox): cache the unshifted entrypoint_tag value
// in the Code object to simplify things and avoid the need to execute
// multiple switches for builtin case.
switch (kind()) {
case CodeKind::BYTECODE_HANDLER:
return kBytecodeHandlerEntrypointTag;
case CodeKind::BUILTIN: {
if (is_disabled_builtin()) return kDisabledBuiltinEntrypointTag;
return Builtins::EntrypointTagFor(builtin_id());
}
case CodeKind::REGEXP:
return kRegExpEntrypointTag;
case CodeKind::WASM_FUNCTION:
case CodeKind::WASM_TO_CAPI_FUNCTION:
case CodeKind::WASM_TO_JS_FUNCTION:
return kWasmEntrypointTag;
case CodeKind::JS_TO_WASM_FUNCTION:
return kJSEntrypointTag;
case CodeKind::FOR_TESTING:
return kCodeEntrypointTagForTesting;
case CodeKind::FOR_TESTING_JS:
return kJSEntrypointTag;
case CodeKind::C_WASM_ENTRY:
return kInvalidEntrypointTag;
case CodeKind::WASM_STACK_ENTRY:
// TODO(thibaudm): assign proper entrypoint tag.
UNREACHABLE();
case CodeKind::BASELINE:
case CodeKind::MAGLEV:
case CodeKind::TURBOFAN_JS:
return kJSEntrypointTag;
case CodeKind::INTERPRETED_FUNCTION:
// This kind is never used for Code objects.
UNREACHABLE();
}
UNREACHABLE();
}
CodeSandboxingMode Code::sandboxing_mode() const {
if (is_builtin()) {
return Builtins::SandboxingModeOf(builtin_id());
} else {
// All runtime-generated code should run sandboxed.
return CodeSandboxingMode::kSandboxed;
}
}
void Code::SetInstructionStreamAndInstructionStart(
IsolateForSandbox isolate, Tagged<InstructionStream> code,
WriteBarrierMode mode) {
set_raw_instruction_stream(code, mode);
set_instruction_start(isolate, code->instruction_start());
}
void Code::SetInstructionStartForOffHeapBuiltin(IsolateForSandbox isolate,
Address entry) {
DCHECK(!has_instruction_stream());
set_instruction_start(isolate, entry);
}
void Code::ClearInstructionStartForSerialization(IsolateForSandbox isolate) {
#ifdef V8_ENABLE_SANDBOX
// The instruction start is stored in this object's code pointer table.
WriteField<CodePointerHandle>(kSelfIndirectPointerOffset,
kNullCodePointerHandle);
#else
set_instruction_start(isolate, kNullAddress);
#endif // V8_ENABLE_SANDBOX
}
void Code::UpdateInstructionStart(IsolateForSandbox isolate,
Tagged<InstructionStream> istream) {
DCHECK_EQ(raw_instruction_stream(), istream);
set_instruction_start(isolate, 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_context_specialized,
bool is_turbofanned) {
DCHECK(!CodeKindIsInterpretedJSFunction(kind));
uint32_t value = KindField::encode(kind) |
IsContextSpecializedField::encode(is_context_specialized) |
IsTurbofannedField::encode(is_turbofanned);
static_assert(FIELD_SIZE(kFlagsOffset) == kInt32Size);
set_flags(value, kRelaxedStore);
}
// 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;
}
inline JSDispatchHandle Code::js_dispatch_handle() const {
return JSDispatchHandle(
ReadField<JSDispatchHandle::underlying_type>(kDispatchHandleOffset));
}
inline void Code::set_js_dispatch_handle(JSDispatchHandle handle) {
Relaxed_WriteField<JSDispatchHandle::underlying_type>(kDispatchHandleOffset,
handle.value());
}
OBJECT_CONSTRUCTORS_IMPL(CodeWrapper, Struct)
CODE_POINTER_ACCESSORS(CodeWrapper, code, kCodeOffset)
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_CODE_INL_H_