Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit fdb2960

Browse files
committed
Bug 1827914 - Merge some Baseline compiler and interpreter functions r=iain
When the bytecode will be shared (for now, just in the self-hosted case), it is the same as the BaselineInterpreter bytecode, so we reduce code duplication. One consequence is that scriptInternal() becomes less useful. If I keep it, I need to add an implementation for BaselineInterpreterHandler which would be identical to maybeScript(). Having three potential methods to get the script is confusing, and we already have a pattern of using maybeScript() when implementing combined compiler and interpreter methods. Differential Revision: https://phabricator.services.mozilla.com/D245768
1 parent 0767142 commit fdb2960

File tree

4 files changed

+71
-127
lines changed

4 files changed

+71
-127
lines changed

js/src/jit/BaselineCodeGen.cpp

Lines changed: 60 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -494,27 +494,22 @@ static void LoadInlineValueOperand(MacroAssembler& masm, ValueOperand dest) {
494494
masm.loadUnalignedValue(Address(pc, sizeof(jsbytecode)), dest);
495495
}
496496

497-
template <>
498-
void BaselineCompilerCodeGen::loadScript(Register dest) {
499-
if (handler.isSelfHosted()) {
497+
template <typename Handler>
498+
void BaselineCodeGen<Handler>::loadScript(Register dest) {
499+
if (handler.realmIndependentJitcode()) {
500500
masm.loadPtr(frame.addressOfInterpreterScript(), dest);
501501
} else {
502-
masm.movePtr(ImmGCPtr(handler.scriptInternal()), dest);
502+
masm.movePtr(ImmGCPtr(handler.maybeScript()), dest);
503503
}
504504
}
505505

506-
template <>
507-
void BaselineInterpreterCodeGen::loadScript(Register dest) {
508-
masm.loadPtr(frame.addressOfInterpreterScript(), dest);
509-
}
510-
511506
template <typename Handler>
512507
void BaselineCodeGen<Handler>::loadJitScript(Register dest) {
513-
if (handler.isSelfHosted()) {
508+
if (handler.realmIndependentJitcode()) {
514509
loadScript(dest);
515510
masm.loadPtr(Address(dest, JSScript::offsetOfWarmUpData()), dest);
516511
} else {
517-
masm.movePtr(ImmPtr(handler.scriptInternal()->jitScript()), dest);
512+
masm.movePtr(ImmPtr(handler.maybeScript()->jitScript()), dest);
518513
}
519514
}
520515

@@ -1022,39 +1017,30 @@ void BaselineInterpreterCodeGen::subtractScriptSlotsSize(Register reg,
10221017
masm.subPtr(scratch, reg);
10231018
}
10241019

1025-
template <>
1026-
void BaselineCompilerCodeGen::loadGlobalLexicalEnvironment(Register dest) {
1027-
MOZ_ASSERT(!handler.script()->hasNonSyntacticScope());
1028-
masm.movePtr(ImmGCPtr(handler.globalLexicalEnvironment()), dest);
1029-
}
1030-
1031-
template <>
1032-
void BaselineInterpreterCodeGen::loadGlobalLexicalEnvironment(Register dest) {
1033-
masm.loadGlobalObjectData(dest);
1034-
masm.loadPtr(Address(dest, GlobalObjectData::offsetOfLexicalEnvironment()),
1035-
dest);
1020+
template <typename Handler>
1021+
void BaselineCodeGen<Handler>::loadGlobalLexicalEnvironment(Register dest) {
1022+
if (handler.realmIndependentJitcode()) {
1023+
masm.loadGlobalObjectData(dest);
1024+
masm.loadPtr(Address(dest, GlobalObjectData::offsetOfLexicalEnvironment()),
1025+
dest);
1026+
} else {
1027+
MOZ_ASSERT(!handler.maybeScript()->hasNonSyntacticScope());
1028+
masm.movePtr(ImmGCPtr(handler.maybeGlobalLexicalEnvironment()), dest);
1029+
}
10361030
}
10371031

1038-
template <>
1039-
void BaselineCompilerCodeGen::pushGlobalLexicalEnvironmentValue(
1032+
template <typename Handler>
1033+
void BaselineCodeGen<Handler>::pushGlobalLexicalEnvironmentValue(
10401034
ValueOperand scratch) {
1041-
if (handler.isSelfHosted()) {
1035+
if (handler.realmIndependentJitcode()) {
10421036
loadGlobalLexicalEnvironment(scratch.scratchReg());
10431037
masm.tagValue(JSVAL_TYPE_OBJECT, scratch.scratchReg(), scratch);
10441038
frame.push(scratch);
10451039
} else {
1046-
frame.push(ObjectValue(*handler.globalLexicalEnvironment()));
1040+
frame.push(ObjectValue(*handler.maybeGlobalLexicalEnvironment()));
10471041
}
10481042
}
10491043

1050-
template <>
1051-
void BaselineInterpreterCodeGen::pushGlobalLexicalEnvironmentValue(
1052-
ValueOperand scratch) {
1053-
loadGlobalLexicalEnvironment(scratch.scratchReg());
1054-
masm.tagValue(JSVAL_TYPE_OBJECT, scratch.scratchReg(), scratch);
1055-
frame.push(scratch);
1056-
}
1057-
10581044
template <>
10591045
void BaselineCompilerCodeGen::loadGlobalThisValue(ValueOperand dest) {
10601046
JSObject* thisObj = handler.globalThis();
@@ -1070,20 +1056,15 @@ void BaselineInterpreterCodeGen::loadGlobalThisValue(ValueOperand dest) {
10701056
masm.loadValue(Address(scratch, SlotOffset), dest);
10711057
}
10721058

1073-
template <>
1074-
void BaselineCompilerCodeGen::pushScriptArg() {
1075-
if (handler.isSelfHosted()) {
1059+
template <typename Handler>
1060+
void BaselineCodeGen<Handler>::pushScriptArg() {
1061+
if (handler.realmIndependentJitcode()) {
10761062
pushArg(frame.addressOfInterpreterScript());
10771063
} else {
1078-
pushArg(ImmGCPtr(handler.scriptInternal()));
1064+
pushArg(ImmGCPtr(handler.maybeScript()));
10791065
}
10801066
}
10811067

1082-
template <>
1083-
void BaselineInterpreterCodeGen::pushScriptArg() {
1084-
pushArg(frame.addressOfInterpreterScript());
1085-
}
1086-
10871068
template <>
10881069
void BaselineCompilerCodeGen::pushBytecodePCArg() {
10891070
pushArg(ImmPtr(handler.pc()));
@@ -1168,12 +1149,11 @@ template <>
11681149
void BaselineCompilerCodeGen::loadScriptGCThing(ScriptGCThingType type,
11691150
Register dest,
11701151
Register scratch) {
1171-
if (handler.isSelfHosted()) {
1152+
if (handler.realmIndependentJitcode()) {
11721153
masm.move32(Imm32(GET_GCTHING_INDEX(handler.pc())), scratch);
11731154
loadScriptGCThingInternal(type, dest, scratch);
11741155
} else {
1175-
gc::Cell* thing =
1176-
GetScriptGCThing(handler.scriptInternal(), handler.pc(), type);
1156+
gc::Cell* thing = GetScriptGCThing(handler.script(), handler.pc(), type);
11771157
masm.movePtr(ImmGCPtr(thing), dest);
11781158
}
11791159
}
@@ -1198,28 +1178,20 @@ void BaselineInterpreterCodeGen::loadScriptGCThing(ScriptGCThingType type,
11981178
#endif
11991179
}
12001180

1201-
template <>
1202-
void BaselineCompilerCodeGen::pushScriptGCThingArg(ScriptGCThingType type,
1203-
Register scratch1,
1204-
Register scratch2) {
1205-
if (handler.isSelfHosted()) {
1181+
template <typename Handler>
1182+
void BaselineCodeGen<Handler>::pushScriptGCThingArg(ScriptGCThingType type,
1183+
Register scratch1,
1184+
Register scratch2) {
1185+
if (handler.realmIndependentJitcode()) {
12061186
loadScriptGCThing(type, scratch1, scratch2);
12071187
pushArg(scratch1);
12081188
} else {
12091189
gc::Cell* thing =
1210-
GetScriptGCThing(handler.scriptInternal(), handler.pc(), type);
1190+
GetScriptGCThing(handler.maybeScript(), handler.maybePC(), type);
12111191
pushArg(ImmGCPtr(thing));
12121192
}
12131193
}
12141194

1215-
template <>
1216-
void BaselineInterpreterCodeGen::pushScriptGCThingArg(ScriptGCThingType type,
1217-
Register scratch1,
1218-
Register scratch2) {
1219-
loadScriptGCThing(type, scratch1, scratch2);
1220-
pushArg(scratch1);
1221-
}
1222-
12231195
template <typename Handler>
12241196
void BaselineCodeGen<Handler>::pushScriptNameArg(Register scratch1,
12251197
Register scratch2) {
@@ -1292,21 +1264,22 @@ void BaselineCompilerCodeGen::emitInitFrameFields(Register nonFunctionEnv) {
12921264
Register scratch2 = R2.scratchReg();
12931265
MOZ_ASSERT(nonFunctionEnv != scratch && nonFunctionEnv != scratch2);
12941266

1295-
uint32_t flags = handler.isSelfHosted() ? BaselineFrame::SELF_HOSTED : 0;
1267+
uint32_t flags =
1268+
handler.realmIndependentJitcode() ? BaselineFrame::REALM_INDEPENDENT : 0;
12961269
masm.store32(Imm32(flags), frame.addressOfFlags());
12971270

12981271
if (handler.function()) {
12991272
masm.loadFunctionFromCalleeToken(frame.addressOfCalleeToken(), scratch);
13001273
masm.unboxObject(Address(scratch, JSFunction::offsetOfEnvironment()),
13011274
scratch2);
13021275
masm.storePtr(scratch2, frame.addressOfEnvironmentChain());
1303-
if (handler.isSelfHosted()) {
1276+
if (handler.realmIndependentJitcode()) {
13041277
masm.loadPrivate(Address(scratch, JSFunction::offsetOfJitInfoOrScript()),
13051278
scratch);
13061279
masm.storePtr(scratch, frame.addressOfInterpreterScript());
13071280
}
13081281
} else {
1309-
if (handler.isSelfHosted()) {
1282+
if (handler.realmIndependentJitcode()) {
13101283
masm.loadPtr(frame.addressOfCalleeToken(), scratch);
13111284
masm.andPtr(Imm32(uint32_t(CalleeTokenMask)), scratch);
13121285
masm.storePtr(scratch, frame.addressOfInterpreterScript());
@@ -1328,16 +1301,16 @@ void BaselineCompilerCodeGen::emitInitFrameFields(Register nonFunctionEnv) {
13281301

13291302
// Otherwise, store this script's default ICSCript in the frame.
13301303
masm.bind(&notInlined);
1331-
if (handler.isSelfHosted()) {
1332-
// When self-hosted JitCode is reused in a new realm, the frames baked into
1304+
if (handler.realmIndependentJitcode()) {
1305+
// When JitCode is reused in a new realm, the frames baked into
13331306
// the native bytecode need to refer to the IC list for the new JitScript or
13341307
// they will execute the IC scripts using the IC stub fields from the wrong
13351308
// script.
13361309
loadJitScript(scratch);
13371310
masm.addPtr(Imm32(JitScript::offsetOfICScript()), scratch);
13381311
masm.storePtr(scratch, frame.addressOfICScript());
13391312
} else {
1340-
masm.storePtr(ImmPtr(handler.scriptInternal()->jitScript()->icScript()),
1313+
masm.storePtr(ImmPtr(handler.script()->jitScript()->icScript()),
13411314
frame.addressOfICScript());
13421315
}
13431316
masm.bind(&done);
@@ -1430,7 +1403,7 @@ bool BaselineCompilerCodeGen::initEnvironmentChain() {
14301403
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
14311404
Register temp = regs.takeAny();
14321405
Label done;
1433-
if (!handler.isSelfHosted()) {
1406+
if (!handler.realmIndependentJitcode()) {
14341407
// Allocate a NamedLambdaObject and/or a CallObject. If the function needs
14351408
// both, the NamedLambdaObject must enclose the CallObject. If one of the
14361409
// allocations fails, we perform the whole operation in C++.
@@ -2728,57 +2701,38 @@ bool BaselineInterpreterCodeGen::emit_Double() {
27282701
return true;
27292702
}
27302703

2731-
template <>
2732-
bool BaselineCompilerCodeGen::emit_BigInt() {
2733-
if (handler.isSelfHosted()) {
2704+
template <typename Handler>
2705+
bool BaselineCodeGen<Handler>::emit_BigInt() {
2706+
if (handler.realmIndependentJitcode()) {
27342707
frame.syncStack(0);
27352708
Register scratch1 = R0.scratchReg();
27362709
Register scratch2 = R1.scratchReg();
27372710
loadScriptGCThing(ScriptGCThingType::BigInt, scratch1, scratch2);
27382711
masm.tagValue(JSVAL_TYPE_BIGINT, scratch1, R0);
27392712
frame.push(R0);
27402713
} else {
2741-
BigInt* bi = handler.scriptInternal()->getBigInt(handler.pc());
2714+
BigInt* bi = handler.maybeScript()->getBigInt(handler.maybePC());
27422715
frame.push(BigIntValue(bi));
27432716
}
27442717
return true;
27452718
}
27462719

2747-
template <>
2748-
bool BaselineInterpreterCodeGen::emit_BigInt() {
2749-
Register scratch1 = R0.scratchReg();
2750-
Register scratch2 = R1.scratchReg();
2751-
loadScriptGCThing(ScriptGCThingType::BigInt, scratch1, scratch2);
2752-
masm.tagValue(JSVAL_TYPE_BIGINT, scratch1, R0);
2753-
frame.push(R0);
2754-
return true;
2755-
}
2756-
2757-
template <>
2758-
bool BaselineCompilerCodeGen::emit_String() {
2759-
if (handler.isSelfHosted()) {
2720+
template <typename Handler>
2721+
bool BaselineCodeGen<Handler>::emit_String() {
2722+
if (handler.realmIndependentJitcode()) {
27602723
frame.syncStack(0);
27612724
Register scratch1 = R0.scratchReg();
27622725
Register scratch2 = R1.scratchReg();
27632726
loadScriptGCThing(ScriptGCThingType::String, scratch1, scratch2);
27642727
masm.tagValue(JSVAL_TYPE_STRING, scratch1, R0);
27652728
frame.push(R0);
27662729
} else {
2767-
frame.push(StringValue(handler.scriptInternal()->getString(handler.pc())));
2730+
frame.push(
2731+
StringValue(handler.maybeScript()->getString(handler.maybePC())));
27682732
}
27692733
return true;
27702734
}
27712735

2772-
template <>
2773-
bool BaselineInterpreterCodeGen::emit_String() {
2774-
Register scratch1 = R0.scratchReg();
2775-
Register scratch2 = R1.scratchReg();
2776-
loadScriptGCThing(ScriptGCThingType::String, scratch1, scratch2);
2777-
masm.tagValue(JSVAL_TYPE_STRING, scratch1, R0);
2778-
frame.push(R0);
2779-
return true;
2780-
}
2781-
27822736
template <>
27832737
bool BaselineCompilerCodeGen::emit_Symbol() {
27842738
unsigned which = GET_UINT8(handler.pc());
@@ -2801,30 +2755,21 @@ bool BaselineInterpreterCodeGen::emit_Symbol() {
28012755
return true;
28022756
}
28032757

2804-
template <>
2805-
bool BaselineCompilerCodeGen::emit_Object() {
2806-
if (handler.isSelfHosted()) {
2758+
template <typename Handler>
2759+
bool BaselineCodeGen<Handler>::emit_Object() {
2760+
if (handler.realmIndependentJitcode()) {
28072761
Register scratch1 = R0.scratchReg();
28082762
Register scratch2 = R1.scratchReg();
28092763
loadScriptGCThing(ScriptGCThingType::Object, scratch1, scratch2);
28102764
masm.tagValue(JSVAL_TYPE_OBJECT, scratch1, R0);
28112765
frame.push(R0);
28122766
} else {
2813-
frame.push(ObjectValue(*handler.scriptInternal()->getObject(handler.pc())));
2767+
frame.push(
2768+
ObjectValue(*handler.maybeScript()->getObject(handler.maybePC())));
28142769
}
28152770
return true;
28162771
}
28172772

2818-
template <>
2819-
bool BaselineInterpreterCodeGen::emit_Object() {
2820-
Register scratch1 = R0.scratchReg();
2821-
Register scratch2 = R1.scratchReg();
2822-
loadScriptGCThing(ScriptGCThingType::Object, scratch1, scratch2);
2823-
masm.tagValue(JSVAL_TYPE_OBJECT, scratch1, R0);
2824-
frame.push(R0);
2825-
return true;
2826-
}
2827-
28282773
template <typename Handler>
28292774
bool BaselineCodeGen<Handler>::emit_CallSiteObj() {
28302775
return emit_Object();
@@ -3702,10 +3647,10 @@ bool BaselineCodeGen<Handler>::emit_GetGName() {
37023647

37033648
template <>
37043649
bool BaselineCompilerCodeGen::tryOptimizeBindUnqualifiedGlobalName() {
3705-
if (handler.isSelfHosted()) {
3650+
if (handler.realmIndependentJitcode()) {
37063651
return false;
37073652
}
3708-
JSScript* script = handler.scriptInternal();
3653+
JSScript* script = handler.script();
37093654
MOZ_ASSERT(!script->hasNonSyntacticScope());
37103655

37113656
if (handler.compilingOffThread()) {
@@ -6361,8 +6306,8 @@ bool BaselineCodeGen<Handler>::emitEnterGeneratorCode(Register script,
63616306
masm.storePtr(scratch, icScriptAddr);
63626307

63636308
Label noBaselineScript;
6364-
// Self-hosted frames need the interpreterScript pointer
6365-
if (handler.isSelfHosted()) {
6309+
// Frames with shared bytecode need the interpreterScript pointer
6310+
if (handler.realmIndependentJitcode()) {
63666311
masm.storePtr(script, frame.addressOfInterpreterScript());
63676312
}
63686313
masm.loadJitScript(script, scratch);
@@ -6628,7 +6573,7 @@ bool BaselineCodeGen<Handler>::emit_Resume() {
66286573

66296574
// After the generator returns, we restore the stack pointer, switch back to
66306575
// the current realm, push the return value, and we're done.
6631-
if (handler.maybeScript() && !handler.isSelfHosted()) {
6576+
if (!handler.realmIndependentJitcode()) {
66326577
masm.switchToRealm(handler.maybeScript()->realm(), R2.scratchReg());
66336578
} else {
66346579
masm.switchToBaselineFrameRealm(R2.scratchReg());
@@ -6943,7 +6888,7 @@ bool BaselineCodeGen<Handler>::emitPrologue() {
69436888

69446889
frame.assertSyncedStack();
69456890

6946-
if (handler.maybeScript() && !handler.isSelfHosted()) {
6891+
if (!handler.realmIndependentJitcode()) {
69476892
masm.debugAssertContextRealm(handler.maybeScript()->realm(),
69486893
R1.scratchReg());
69496894
}

0 commit comments

Comments
 (0)