import com.fasterxml.jackson.databind.ObjectMapper; import p.studio.compiler.FrontendRegistryService; import p.studio.compiler.messages.BuildingIssue; import p.studio.compiler.messages.BuilderPipelineConfig; import p.studio.compiler.models.BuilderPipelineContext; import p.studio.compiler.source.identifiers.FileId; import p.studio.compiler.workspaces.BuilderPipelineService; import p.studio.compiler.workspaces.PipelineStage; import p.studio.compiler.workspaces.stages.*; import p.studio.utilities.logs.LogAggregator; import java.nio.file.*; import java.security.MessageDigest; import java.util.*; /** Research-only probe: production stages, isolated source projects, no compiler changes. */ public final class PbsProductionCorpusProbe { static final ObjectMapper MAPPER = new ObjectMapper(); static final String ANALYSIS_ID = UUID.randomUUID().toString(); static int issueIndex; static final List LOG = new ArrayList<>(); public static void main(String[] args) throws Exception { Path root = Path.of(args[0]).toRealPath(); boolean optimize = args[1].equals("optimized"); boolean publicEntry = args.length > 3 && args[3].equals("public"); var result = new LinkedHashMap(); result.put("projectRoot", root.toString()); result.put("optimization", optimize ? "optimized" : "unoptimized"); result.put("entrypoint", publicEntry ? "BuilderPipelineService.compile" : "public analysis + production compile stages"); result.put("frontendOrigin", FrontendRegistryService.require("pbs").getClass().getProtectionDomain().getCodeSource().getLocation().toString()); result.put("sdkOrigin", p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver.class.getProtectionDomain().getCodeSource().getLocation().toString()); result.put("javaVersion", System.getProperty("java.version")); result.put("pipelineOrigin", BuilderPipelineService.class.getProtectionDomain().getCodeSource().getLocation().toString()); var ctx = BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false, root.toString())); var logs = LogAggregator.with(LOG::add); var phases = new ArrayList>(); result.put("phases", phases); result.put("status", "infrastructure_failure"); try { if (publicEntry) { var compiled = BuilderPipelineService.INSTANCE.compile(ctx, logs); result.put("diagnostics", compiled.diagnostics().stream().map(i -> issue(i, ctx)).toList()); result.put("status", "compiled"); } else { var analyzed = BuilderPipelineService.INSTANCE.analyze(ctx, logs); var phase = new LinkedHashMap(); phase.put("phase", "analysis"); phase.put("diagnostics", analyzed.diagnostics().stream().map(i -> issue(i, ctx)).toList()); boolean failed = analyzed.diagnostics().stream().anyMatch(BuildingIssue::isError); phase.put("status", failed ? "failed" : "passed"); phases.add(phase); if (failed) result.put("status", "analysis_failed"); else { var stages = new ArrayList(); stages.add(new LowerToIRVMPipelineStage()); if (optimize) stages.add(new OptimizeIRVMPipelineStage()); stages.add(new EmitBytecodePipelineStage()); stages.add(new LinkBytecodePipelineStage()); stages.add(new VerifyBytecodePipelineStage()); result.put("status", "compiled"); for (var stage : stages) { if (!optimize && stage instanceof EmitBytecodePipelineStage) ctx.optimizedIrvm = ctx.irvm; var issues = stage.run(ctx, logs).asCollection(); var item = new LinkedHashMap(); item.put("phase", stage.getClass().getSimpleName()); item.put("diagnostics", issues.stream().map(i -> issue(i, ctx)).toList()); boolean errors = issues.stream().anyMatch(BuildingIssue::isError); item.put("status", errors ? "failed" : "passed"); phases.add(item); if (errors) { result.put("status", "compile_failed"); break; } } } } if (result.get("status").equals("compiled")) { Path cartridge = root.resolve("cartridge"); Files.createDirectories(cartridge); Files.write(cartridge.resolve("program.pbx"), ctx.bytecodeBytes); var manifest = new LinkedHashMap(); manifest.put("magic", "PMTU"); manifest.put("cartridge_version", 1); manifest.put("app_id", 113); manifest.put("title", "PBS production assertion fixture"); manifest.put("app_version", "1.0.0"); manifest.put("app_mode", "Game"); manifest.put("capabilities", List.of()); manifest.put("asset_table", List.of()); manifest.put("preload", List.of()); MAPPER.writerWithDefaultPrettyPrinter().writeValue(cartridge.resolve("manifest.json").toFile(), manifest); result.put("cartridge", cartridge.toString()); result.put("bytecodeBytes", ctx.bytecodeBytes.length); result.put("bytecodeSha256", HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(ctx.bytecodeBytes))); } } catch (Throwable failure) { result.put("status", failure instanceof p.studio.compiler.exceptions.BuildException ? "public_compile_rejected" : "pipeline_exception"); result.put("exception", causes(failure)); if (failure instanceof p.studio.compiler.exceptions.BuildException buildFailure) result.put("diagnostics", buildFailure.getDiagnostics().stream().map(i -> issue(i,ctx)).toList()); } if (ctx.fileTable != null) { var files = new LinkedHashMap(); for (int i = 0; i < ctx.fileTable.size(); i++) { var f = ctx.fileTable.get(new FileId(i)); files.put(Integer.toString(i), f.getCanonPath().toString()); } result.put("files", files); } if (ctx.irBackend != null) { var functions = new ArrayList>(); for (var fn : ctx.irBackend.getExecutableFunctions()) { var file = ctx.fileTable.get(fn.fileId()).getCanonPath(); if (!file.startsWith(root)) continue; var f = new LinkedHashMap(); f.put("callableId", fn.callableId().toString()); f.put("name", fn.callableName()); f.put("file", root.relativize(file).toString()); f.put("instructions", fn.instructions().stream().map(Object::toString).toList()); functions.add(f); } result.put("projectFunctions", functions); } result.put("inputs", p.studio.compiler.source.diagnostics.CompilerDiagnosticProjection.inputs( p.studio.compiler.source.diagnostics.SourceReference.snapshot(ctx.fileTable), root)); result.put("logs", LOG); Path report = Path.of(args[2]); Files.createDirectories(report.getParent()); MAPPER.writerWithDefaultPrettyPrinter().writeValue(report.toFile(), result); System.out.println(result.get("status") + " " + root.getFileName()); } static Map issue(BuildingIssue i, BuilderPipelineContext ctx) { var sources = p.studio.compiler.source.diagnostics.SourceReference.snapshot(ctx.fileTable); return p.studio.compiler.source.diagnostics.CompilerDiagnosticProjection.issue(i, sources, p.studio.compiler.source.diagnostics.CompilerDiagnosticProjection.capturedText(ctx.fileTable), ctx.rootProjectPathCanon, ANALYSIS_ID + ":" + issueIndex++); } static List> causes(Throwable t) { var result = new ArrayList>(); for (int n = 0; t != null && n < 8; n++, t = t.getCause()) result.add(Map.of("class", t.getClass().getName(), "message", String.valueOf(t.getMessage()))); return result; } }