implements one-shot PBS boot guard in published wrapper lowering
This commit is contained in:
+104
-4
@@ -240,9 +240,19 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
&& entryPointCallableName != null
|
||||
&& !entryPointCallableName.isBlank()
|
||||
&& !entryPointModuleId.isNone()) {
|
||||
final var existingBootGuard = globals.stream()
|
||||
.filter(global -> moduleIdsMatch(entryPointModuleId, global.moduleId()))
|
||||
.filter(global -> global.hiddenKind() == IRHiddenGlobalKind.BOOT_GUARD)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
final var bootGuardSlot = existingBootGuard != null
|
||||
? existingBootGuard.slot()
|
||||
: nextGlobalSlot(globals, entryPointModuleId);
|
||||
final var wrapperCallableId = new CallableId(callableSignatures.size());
|
||||
callableSignatures.add(new CallableSignatureRef(entryPointModuleId, entryPointCallableName, 0, "() -> unit"));
|
||||
final var instructions = new ArrayList<IRBackendExecutableFunction.Instruction>();
|
||||
instructions.add(getGlobalInstruction(bootGuardSlot, frameExecutable.span()));
|
||||
instructions.add(jumpIfTrueInstruction("bootstrap_done", frameExecutable.span()));
|
||||
for (final var moduleId : sortedModuleIds) {
|
||||
final var moduleInitCallableId = moduleInitCallableIds.get(moduleId);
|
||||
if (moduleInitCallableId == null) {
|
||||
@@ -271,6 +281,9 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
0,
|
||||
frameExecutable.span()));
|
||||
}
|
||||
instructions.add(pushBoolInstruction(true, frameExecutable.span()));
|
||||
instructions.add(setGlobalInstruction(bootGuardSlot, frameExecutable.span()));
|
||||
instructions.add(labelInstruction("bootstrap_done", frameExecutable.span()));
|
||||
instructions.add(callInstruction(frameExecutable, frameExecutable.span()));
|
||||
instructions.add(retInstruction(frameExecutable.span()));
|
||||
executableFunctions.add(new IRBackendExecutableFunction(
|
||||
@@ -287,15 +300,13 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
ReadOnlyList.wrap(instructions),
|
||||
frameExecutable.span()));
|
||||
|
||||
if (globals.stream().noneMatch(global ->
|
||||
moduleIdsMatch(entryPointModuleId, global.moduleId())
|
||||
&& global.hiddenKind() == IRHiddenGlobalKind.BOOT_GUARD)) {
|
||||
if (existingBootGuard == null) {
|
||||
globals.add(new IRGlobal(
|
||||
frameExecutable.fileId(),
|
||||
entryPointModuleId,
|
||||
PbsFrontendCompiler.bootGuardGlobalName(entryPointModuleId),
|
||||
"bool",
|
||||
0,
|
||||
bootGuardSlot,
|
||||
IRGlobalVisibility.MODULE,
|
||||
true,
|
||||
IRHiddenGlobalKind.BOOT_GUARD,
|
||||
@@ -325,6 +336,19 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
.build();
|
||||
}
|
||||
|
||||
private int nextGlobalSlot(
|
||||
final ArrayList<IRGlobal> globals,
|
||||
final ModuleId moduleId) {
|
||||
var nextSlot = 0;
|
||||
for (final var global : globals) {
|
||||
if (!moduleIdsMatch(moduleId, global.moduleId())) {
|
||||
continue;
|
||||
}
|
||||
nextSlot = Math.max(nextSlot, global.slot() + 1);
|
||||
}
|
||||
return nextSlot;
|
||||
}
|
||||
|
||||
private ArrayList<IRGlobal> normalizeGlobalSlots(final ArrayList<IRGlobal> globals) {
|
||||
final var nextSlotByModule = new LinkedHashMap<ModuleId, Integer>();
|
||||
final var normalized = new ArrayList<IRGlobal>(globals.size());
|
||||
@@ -390,6 +414,82 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
span);
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction.Instruction getGlobalInstruction(
|
||||
final int slot,
|
||||
final Span span) {
|
||||
return new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.GET_GLOBAL,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
slot,
|
||||
null,
|
||||
span);
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction.Instruction setGlobalInstruction(
|
||||
final int slot,
|
||||
final Span span) {
|
||||
return new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.SET_GLOBAL,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
slot,
|
||||
null,
|
||||
span);
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction.Instruction pushBoolInstruction(
|
||||
final boolean value,
|
||||
final Span span) {
|
||||
return new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.PUSH_BOOL,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
value ? "true" : "false",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
span);
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction.Instruction labelInstruction(
|
||||
final String label,
|
||||
final Span span) {
|
||||
return new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.LABEL,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
label,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
span);
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction.Instruction jumpIfTrueInstruction(
|
||||
final String targetLabel,
|
||||
final Span span) {
|
||||
return new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.JMP_IF_TRUE,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
targetLabel,
|
||||
null,
|
||||
null,
|
||||
span);
|
||||
}
|
||||
|
||||
private ModuleId normalizeModuleId(final ModuleId moduleId) {
|
||||
return moduleId == null ? ModuleId.none() : moduleId;
|
||||
}
|
||||
|
||||
+11
-5
@@ -369,15 +369,21 @@ class PBSFrontendPhaseServiceTest {
|
||||
.orElseThrow();
|
||||
assertTrue(bootGuard.isHidden());
|
||||
assertTrue(bootGuard.name().startsWith("__pbs.boot_guard$m"));
|
||||
assertEquals(1, bootGuard.slot());
|
||||
final var wrapper = irBackend.getExecutableFunctions().stream()
|
||||
.filter(function -> irBackend.getEntryPointCallableName().equals(function.callableName()))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertEquals(4, wrapper.instructions().size());
|
||||
assertTrue(wrapper.instructions().get(0).calleeCallableName().startsWith("__pbs.module_init$m"));
|
||||
assertTrue(wrapper.instructions().get(1).calleeCallableName().startsWith("__pbs.project_init$m"));
|
||||
assertEquals("frame", wrapper.instructions().get(2).calleeCallableName());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.RET, wrapper.instructions().get(3).kind());
|
||||
assertEquals(9, wrapper.instructions().size());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.GET_GLOBAL, wrapper.instructions().get(0).kind());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP_IF_TRUE, wrapper.instructions().get(1).kind());
|
||||
assertTrue(wrapper.instructions().get(2).calleeCallableName().startsWith("__pbs.module_init$m"));
|
||||
assertTrue(wrapper.instructions().get(3).calleeCallableName().startsWith("__pbs.project_init$m"));
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_BOOL, wrapper.instructions().get(4).kind());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.SET_GLOBAL, wrapper.instructions().get(5).kind());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.LABEL, wrapper.instructions().get(6).kind());
|
||||
assertEquals("frame", wrapper.instructions().get(7).calleeCallableName());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.RET, wrapper.instructions().get(8).kind());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user