128 lines
6.0 KiB
Python
128 lines
6.0 KiB
Python
# SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
"""Compiler Explorer, the oracle every corpus item is checked against.
|
|
|
|
Requests follow Mojo Quest's (src/lib/compile.ts), which its CI runs against
|
|
the same compiler id, so a result here means what it means there:
|
|
|
|
run compile and execute in one request (an "executor request"); the
|
|
answer has buildResult.code, didExecute, code, stdout, stderr, timedOut
|
|
asm compile only and return the Intel-syntax listing with source lines
|
|
both compile with the listing AND execute; whether one request returns
|
|
both is itself a probe (probes/godbolt-asm-and-run.mojo)
|
|
|
|
Every answer is normalized to one shape, `Result`, and kept beside the raw
|
|
JSON (minus a listing duplicated in `asm`) so a later reading can change.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from typing import Any
|
|
|
|
from .common import PipelineError
|
|
|
|
USER_AGENT = "programming-plus-mojo-corpus/0.1 (+https://pplus.scient.ing; corpus verification, low rate)"
|
|
|
|
ASM_FILTERS = {"binary": False, "execute": False, "intel": True, "demangle": True, "labels": True,
|
|
"libraryCode": False, "directives": True, "commentOnly": True, "trim": True}
|
|
|
|
|
|
def request_body(source: str, mode: str = "run", stdin: str = "", args: str = "") -> dict[str, Any]:
|
|
if mode == "run":
|
|
options = {"userArguments": args, "executeParameters": {"args": [], "stdin": stdin},
|
|
"compilerOptions": {"executorRequest": True, "skipAsm": True},
|
|
"filters": {"execute": True}, "tools": [], "libraries": []}
|
|
elif mode == "asm":
|
|
options = {"userArguments": args, "compilerOptions": {"executorRequest": False, "skipAsm": False},
|
|
"filters": dict(ASM_FILTERS), "tools": [], "libraries": []}
|
|
elif mode == "both":
|
|
options = {"userArguments": args, "executeParameters": {"args": [], "stdin": stdin},
|
|
"compilerOptions": {"executorRequest": False, "skipAsm": False},
|
|
"filters": {**ASM_FILTERS, "execute": True}, "tools": [], "libraries": []}
|
|
else:
|
|
raise PipelineError(f"unknown godbolt mode {mode!r}")
|
|
return {"source": source, "options": options, "lang": "mojo", "allowStoreCodeDebug": False}
|
|
|
|
|
|
def _lines(arr: Any) -> str:
|
|
return "\n".join((x.get("text", "") if isinstance(x, dict) else str(x)) for x in arr) if isinstance(arr, list) else ""
|
|
|
|
|
|
def normalize(mode: str, data: dict[str, Any]) -> dict[str, Any]:
|
|
"""One shape for every mode: compiled, buildStderr, executed, exitCode, stdout, stderr, timedOut, asm."""
|
|
if mode == "run":
|
|
build = data.get("buildResult") or {}
|
|
return {
|
|
"compiled": build.get("code") == 0,
|
|
"buildStderr": _lines(build.get("stderr")),
|
|
"executed": bool(data.get("didExecute")),
|
|
"exitCode": data.get("code") if isinstance(data.get("code"), int) else None,
|
|
"stdout": _lines(data.get("stdout")),
|
|
"stderr": _lines(data.get("stderr")),
|
|
"timedOut": bool(data.get("timedOut")),
|
|
"asm": None,
|
|
}
|
|
exe = data.get("execResult") or {}
|
|
asm = [asm_line(a) for a in data.get("asm") or [] if isinstance(a, dict)]
|
|
return {
|
|
"compiled": data.get("code") == 0,
|
|
"buildStderr": _lines(data.get("stderr")),
|
|
"executed": bool(exe.get("didExecute")) if exe else False,
|
|
"exitCode": exe.get("code") if isinstance(exe.get("code"), int) else None,
|
|
"stdout": _lines(exe.get("stdout")),
|
|
"stderr": _lines(exe.get("stderr")),
|
|
"timedOut": bool(exe.get("timedOut")),
|
|
"asm": asm,
|
|
"returnedAsm": bool(asm),
|
|
"returnedExec": bool(exe),
|
|
}
|
|
|
|
|
|
def asm_line(a: dict[str, Any]) -> dict[str, Any]:
|
|
"""One listing line, with where it came from. `file` and `main` keep godbolt's marking of the source
|
|
file as given (`source.file`, `source.mainsource`), so the instructions inlined from the standard
|
|
library, whose lines are lines of the library's files, can be told from the program's own."""
|
|
src = a.get("source") or {}
|
|
out: dict[str, Any] = {"text": a.get("text", ""), "line": src.get("line")}
|
|
if src:
|
|
out["file"] = src.get("file")
|
|
if "mainsource" in src:
|
|
out["main"] = src.get("mainsource")
|
|
return out
|
|
|
|
|
|
def slim_raw(data: dict[str, Any]) -> dict[str, Any]:
|
|
return {k: v for k, v in data.items() if k not in ("asm", "popularArguments")}
|
|
|
|
|
|
def post(base: str, compiler: str, body: dict[str, Any], timeout: int = 90, retries: int = 5,
|
|
attempts: list[int] | None = None) -> dict[str, Any]:
|
|
"""POST and return the answer. `attempts`, if given, receives how many tries it took, so a slow
|
|
answer can be told apart from a slow compile (godbolt reports its own compile time separately)."""
|
|
url = f"{base}/api/compiler/{urllib.request.quote(compiler, safe='')}/compile"
|
|
data = json.dumps(body).encode()
|
|
delay = 2.0
|
|
for attempt in range(retries + 1):
|
|
req = urllib.request.Request(url, data=data, method="POST", headers={
|
|
"Content-Type": "application/json", "Accept": "application/json", "User-Agent": USER_AGENT})
|
|
if attempts is not None:
|
|
attempts[:] = [attempt + 1]
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
return json.loads(r.read().decode("utf-8"))
|
|
except urllib.error.HTTPError as e:
|
|
if e.code in (429, 500, 502, 503, 504) and attempt < retries:
|
|
wait = float(e.headers.get("Retry-After") or delay)
|
|
time.sleep(min(wait, 120)); delay *= 2
|
|
continue
|
|
raise PipelineError(f"godbolt answered HTTP {e.code} for {compiler}") from e
|
|
except (urllib.error.URLError, TimeoutError) as e:
|
|
if attempt < retries:
|
|
time.sleep(delay); delay *= 2
|
|
continue
|
|
raise PipelineError(f"godbolt unreachable: {e}") from e
|
|
raise AssertionError("unreachable")
|