cpyte 4.7.4__tar.gz → 4.7.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cpyte-4.7.4/source/cpyte.egg-info → cpyte-4.7.5}/PKG-INFO +1 -1
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/__init__.py +1 -1
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/astparse.py +23 -55
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/bytecoding.py +19 -48
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/compiling.py +113 -65
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/extension_hooks.py +108 -6
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/lexar.py +30 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/linker.py +2 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/mainpie.py +155 -29
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/package_manifest.py +26 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/semantic_analasis.py +53 -31
- {cpyte-4.7.4 → cpyte-4.7.5/source/cpyte.egg-info}/PKG-INFO +1 -1
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_compiler_regressions.py +73 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/MANIFEST.in +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/pyproject.toml +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/readme.md +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/setup.cfg +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/__main__.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/_bignum_bc.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/_runtime_bc.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/bignum.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/bignum_protos.h +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/clib.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/elf2sef.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/floats_runtime.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/formatter.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/gc_runtime.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/gc_runtime_addition.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/generate_bc.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/lsp_server.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/mksef.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/opreof_opt.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/opreof_pipeline.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/optimizations.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/runpy_runtime.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/runtime.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/runtime_scorpion.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/scorpion_abi.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/sef.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/ugc.h +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/ui.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/update_check.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/winjit_patch.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte/winjit_stubs.c +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte.egg-info/SOURCES.txt +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte.egg-info/dependency_links.txt +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte.egg-info/entry_points.txt +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte.egg-info/requires.txt +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/source/cpyte.egg-info/top_level.txt +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_bignum_jit.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_fuzz_del_has.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_opreof_proofs.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_scorpion_abi.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_scorpion_dl.py +0 -0
- {cpyte-4.7.4 → cpyte-4.7.5}/test/test_sef_tools.py +0 -0
|
@@ -9,7 +9,13 @@ from .lexar import Lexer, Token, TokenType, _unescape_run
|
|
|
9
9
|
# runtime-typed array.
|
|
10
10
|
_FIXED_ARRAY_RE = re.compile(r"^(.*?)\[(\d+)\]$")
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
# Set by parse_file() so expression dispatch respects --no-userspace too.
|
|
13
|
+
_parser_extensions_enabled = True
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from .extension_hooks import get_global_hook_dispatcher
|
|
17
|
+
except ImportError: # pragma: no cover - extension_hooks is always shipped
|
|
18
|
+
get_global_hook_dispatcher = None # type: ignore[assignment]
|
|
13
19
|
|
|
14
20
|
# User-declared type names (structs, type aliases, classes) collected in a
|
|
15
21
|
# pre-scan so bare-name casts like `(MyInt)x` are recognized as casts at parse
|
|
@@ -37,33 +43,6 @@ def _pre_scan_user_types(tokens):
|
|
|
37
43
|
return names
|
|
38
44
|
|
|
39
45
|
|
|
40
|
-
def register_parser_hook(hook: Any) -> None:
|
|
41
|
-
_parser_hooks.append(hook)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
# Bruh, dead code.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def get_parser_hooks() -> list[Any]:
|
|
48
|
-
return _parser_hooks.copy()
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def _get_all_parser_hooks(enable_extensions: bool) -> list[Any]:
|
|
52
|
-
if not enable_extensions:
|
|
53
|
-
return []
|
|
54
|
-
hooks = list(_parser_hooks)
|
|
55
|
-
try:
|
|
56
|
-
from .extension_hooks import HookStage, ParserHook, get_global_hook_registry
|
|
57
|
-
|
|
58
|
-
registry = get_global_hook_registry()
|
|
59
|
-
for hook in registry.get(HookStage.PARSER):
|
|
60
|
-
if isinstance(hook, ParserHook) and hook not in hooks:
|
|
61
|
-
hooks.append(hook)
|
|
62
|
-
except ImportError:
|
|
63
|
-
pass
|
|
64
|
-
return hooks
|
|
65
|
-
|
|
66
|
-
|
|
67
46
|
def _loc(node):
|
|
68
47
|
token = getattr(node, "_token", None)
|
|
69
48
|
if token:
|
|
@@ -620,15 +599,12 @@ def _try_parse_cast_type(tokens, pos):
|
|
|
620
599
|
def _parse_atom(tokens: list[Token], pos: int):
|
|
621
600
|
tok = tokens[pos]
|
|
622
601
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
tokens, pos
|
|
626
|
-
)
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
pos,
|
|
630
|
-
_make_parser_ctx(tokens, pos),
|
|
631
|
-
)
|
|
602
|
+
if _parser_extensions_enabled and get_global_hook_dispatcher is not None:
|
|
603
|
+
parsed = get_global_hook_dispatcher().parse_expression(
|
|
604
|
+
tokens, pos, _make_parser_ctx(tokens, pos)
|
|
605
|
+
)
|
|
606
|
+
if parsed is not None:
|
|
607
|
+
return parsed
|
|
632
608
|
|
|
633
609
|
if tok.type == TokenType.KEYWORD and tok.value == "borrow":
|
|
634
610
|
pos += 1
|
|
@@ -1300,6 +1276,8 @@ def _parse_expr_iterative(tokens: list[Token], pos: int, min_prec: int):
|
|
|
1300
1276
|
|
|
1301
1277
|
|
|
1302
1278
|
def parse_file(tokens: list[Token], pos: int = 0, enable_extensions: bool = True):
|
|
1279
|
+
global _parser_extensions_enabled
|
|
1280
|
+
_parser_extensions_enabled = enable_extensions
|
|
1303
1281
|
_pre_scan_user_types(tokens)
|
|
1304
1282
|
nodes = []
|
|
1305
1283
|
while pos < len(tokens) and tokens[pos].type not in (
|
|
@@ -1312,24 +1290,14 @@ def parse_file(tokens: list[Token], pos: int = 0, enable_extensions: bool = True
|
|
|
1312
1290
|
if pos >= len(tokens) or tokens[pos].type in (TokenType.EOF, TokenType.DEDENT):
|
|
1313
1291
|
break
|
|
1314
1292
|
|
|
1315
|
-
|
|
1316
|
-
if
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
) and hook.should_handle_statement(tokens, pos):
|
|
1324
|
-
node, pos = hook.parse_statement(tokens, pos, ctx)
|
|
1325
|
-
nodes.append(node)
|
|
1326
|
-
handled = True
|
|
1327
|
-
break
|
|
1328
|
-
except Exception:
|
|
1329
|
-
pass
|
|
1330
|
-
if not handled:
|
|
1331
|
-
node, pos = _parse_standard_statement(tokens, pos)
|
|
1332
|
-
nodes.append(node)
|
|
1293
|
+
parsed = None
|
|
1294
|
+
if enable_extensions and get_global_hook_dispatcher is not None:
|
|
1295
|
+
parsed = get_global_hook_dispatcher().parse_statement(
|
|
1296
|
+
tokens, pos, _make_parser_ctx(tokens, pos)
|
|
1297
|
+
)
|
|
1298
|
+
if parsed is not None:
|
|
1299
|
+
node, pos = parsed
|
|
1300
|
+
nodes.append(node)
|
|
1333
1301
|
else:
|
|
1334
1302
|
node, pos = _parse_standard_statement(tokens, pos)
|
|
1335
1303
|
nodes.append(node)
|
|
@@ -10,11 +10,9 @@ from llvmlite.ir import instructions
|
|
|
10
10
|
from .astparse import *
|
|
11
11
|
from .clib import _BUILTIN_LIB_HEADERS
|
|
12
12
|
from .extension_hooks import (
|
|
13
|
-
CodegenHook,
|
|
14
13
|
CompilerContext,
|
|
15
|
-
HookLoadError,
|
|
16
14
|
HookStage,
|
|
17
|
-
|
|
15
|
+
get_global_hook_dispatcher,
|
|
18
16
|
get_global_hook_registry,
|
|
19
17
|
)
|
|
20
18
|
from .lexar import TokenType
|
|
@@ -1766,36 +1764,17 @@ class LLVM:
|
|
|
1766
1764
|
import tempfile
|
|
1767
1765
|
|
|
1768
1766
|
context = self._hook_context()
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
fd, tmp_path = tempfile.mkstemp(
|
|
1781
|
-
suffix=".c", prefix="hook_runtime_"
|
|
1782
|
-
)
|
|
1783
|
-
with os.fdopen(fd, "w") as f:
|
|
1784
|
-
f.write(runtime_code)
|
|
1785
|
-
self.import_src_files.append(tmp_path)
|
|
1786
|
-
except Exception as _hook_exc:
|
|
1787
|
-
# A runtime hook that raises is skipped (its code is not
|
|
1788
|
-
# linked). Surface it under CPYTE_JIT_DEBUG so a stale hook
|
|
1789
|
-
# (e.g. an old get_runtime_code(self) signature) is not a
|
|
1790
|
-
# fully silent failure.
|
|
1791
|
-
if os.environ.get("CPYTE_JIT_DEBUG"):
|
|
1792
|
-
import sys as _sys
|
|
1793
|
-
|
|
1794
|
-
print(
|
|
1795
|
-
f"[bytecoding] runtime hook {hook.name!r} skipped: "
|
|
1796
|
-
f"{type(_hook_exc).__name__}: {_hook_exc}",
|
|
1797
|
-
file=_sys.stderr,
|
|
1798
|
-
)
|
|
1767
|
+
dispatcher = get_global_hook_dispatcher()
|
|
1768
|
+
for hook, rel_file in dispatcher.runtime_files(context):
|
|
1769
|
+
base = os.path.dirname(hook.hook_path) if hook.hook_path else ""
|
|
1770
|
+
abs_path = os.path.join(base, rel_file)
|
|
1771
|
+
if os.path.isfile(abs_path):
|
|
1772
|
+
self.import_src_files.append(abs_path)
|
|
1773
|
+
for runtime_code in dispatcher.runtime_code(context):
|
|
1774
|
+
fd, tmp_path = tempfile.mkstemp(suffix=".c", prefix="hook_runtime_")
|
|
1775
|
+
with os.fdopen(fd, "w") as f:
|
|
1776
|
+
f.write(runtime_code)
|
|
1777
|
+
self.import_src_files.append(tmp_path)
|
|
1799
1778
|
|
|
1800
1779
|
if os.environ.get("CPYTE_JIT_DEBUG"):
|
|
1801
1780
|
import sys as _sys
|
|
@@ -2440,22 +2419,14 @@ class LLVM:
|
|
|
2440
2419
|
del self._emit_memo[k]
|
|
2441
2420
|
|
|
2442
2421
|
def _emit_recursive(self, node: Node | dict) -> _IRValue:
|
|
2443
|
-
# Try codegen hooks if extensions are enabled
|
|
2422
|
+
# Try codegen hooks if extensions are enabled. The dispatcher applies
|
|
2423
|
+
# the registry's warn/skip (or --strict) policy to callback failures.
|
|
2444
2424
|
if self.enable_extensions:
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
return hook.emit_node(
|
|
2451
|
-
node,
|
|
2452
|
-
self.builder,
|
|
2453
|
-
self._hook_context(data={"node": node}),
|
|
2454
|
-
)
|
|
2455
|
-
except Exception as e:
|
|
2456
|
-
raise HookLoadError(
|
|
2457
|
-
f"codegen hook {hook.__class__.__name__} failed: {e}"
|
|
2458
|
-
) from e
|
|
2425
|
+
emitted = get_global_hook_dispatcher().emit_node(
|
|
2426
|
+
node, self.builder, self._hook_context(data={"node": node})
|
|
2427
|
+
)
|
|
2428
|
+
if emitted is not None:
|
|
2429
|
+
return emitted
|
|
2459
2430
|
|
|
2460
2431
|
if isinstance(node, dict):
|
|
2461
2432
|
if node.get("type") == "for":
|
|
@@ -12,7 +12,7 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
12
12
|
|
|
13
13
|
from .generate_bc import _find_gmp_include, _remove_probe_stack_ir
|
|
14
14
|
from .linker import Linker, LinkerNotFoundError, format_cc_diag
|
|
15
|
-
from .ui import print_err, print_ok
|
|
15
|
+
from .ui import print_err, print_ok, print_warn
|
|
16
16
|
from .winjit_patch import link_jit_anchors, patch_windows_gnu_relocs
|
|
17
17
|
|
|
18
18
|
# Suppress ctypes callback cleanup warning during shutdown (harmless)
|
|
@@ -879,7 +879,11 @@ def optimize(mod, opt_level=3, opt_size=False):
|
|
|
879
879
|
binding.initialize_native_target()
|
|
880
880
|
binding.initialize_native_asmprinter()
|
|
881
881
|
if opt_level <= 0 and not opt_size:
|
|
882
|
-
return
|
|
882
|
+
return mod
|
|
883
|
+
from .extension_hooks import CompilerContext, get_global_hook_dispatcher
|
|
884
|
+
|
|
885
|
+
ctx = CompilerContext(optimization_level=opt_level, llvm_module=mod)
|
|
886
|
+
mod = get_global_hook_dispatcher().optimize_module(mod, ctx)
|
|
883
887
|
target_machine = make_target_machine(opt=0 if opt_level <= 0 else opt_level)
|
|
884
888
|
|
|
885
889
|
if opt_size:
|
|
@@ -1003,29 +1007,15 @@ def optimize(mod, opt_level=3, opt_size=False):
|
|
|
1003
1007
|
# Default module pipeline (includes inliner, GVN, DCE, loop and vectorization opts)
|
|
1004
1008
|
npm = pb.getModulePassManager()
|
|
1005
1009
|
|
|
1006
|
-
# Let extension hooks add their own passes
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
CompilerContext,
|
|
1010
|
-
HookStage,
|
|
1011
|
-
OptimizeHook,
|
|
1012
|
-
get_global_hook_registry,
|
|
1013
|
-
)
|
|
1010
|
+
# Let extension hooks add their own passes. The dispatcher records and
|
|
1011
|
+
# skips failing callbacks (or raises under --strict); do not swallow them.
|
|
1012
|
+
from .extension_hooks import CompilerContext, get_global_hook_dispatcher
|
|
1014
1013
|
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
for hook in registry.get(HookStage.OPTIMIZE):
|
|
1018
|
-
if not isinstance(hook, OptimizeHook):
|
|
1019
|
-
continue
|
|
1020
|
-
try:
|
|
1021
|
-
if hook.should_add_passes(ctx):
|
|
1022
|
-
hook.add_module_passes(npm, ctx)
|
|
1023
|
-
except Exception as e:
|
|
1024
|
-
print_err(f"optimize hook {hook.__class__.__name__} failed: {e}")
|
|
1025
|
-
except Exception:
|
|
1026
|
-
pass
|
|
1014
|
+
ctx = CompilerContext(optimization_level=opt_level, llvm_module=mod)
|
|
1015
|
+
get_global_hook_dispatcher().add_optimization_passes(mod, npm, ctx)
|
|
1027
1016
|
|
|
1028
1017
|
npm.run(mod, pb)
|
|
1018
|
+
return mod
|
|
1029
1019
|
|
|
1030
1020
|
|
|
1031
1021
|
def _maybe_compile(module, use_native_eh=False):
|
|
@@ -1127,33 +1117,28 @@ def _mpfr_include_dir() -> str | None:
|
|
|
1127
1117
|
def _find_openssl_paths():
|
|
1128
1118
|
"""Find OpenSSL include and lib dirs (for linking crypto into ccode/JIT/AOT)."""
|
|
1129
1119
|
# Try hook-provided paths first (from loaded build hooks, e.g. cryptography)
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
if pth and os.path.isdir(pth):
|
|
1142
|
-
if os.path.isfile(os.path.join(pth, 'openssl', 'evp.h')):
|
|
1143
|
-
inc = pth
|
|
1144
|
-
break
|
|
1145
|
-
for pth in (ctx.library_paths or []):
|
|
1146
|
-
if pth and os.path.isdir(pth):
|
|
1147
|
-
libdir = pth
|
|
1120
|
+
from .extension_hooks import CompilerContext, get_global_hook_dispatcher
|
|
1121
|
+
|
|
1122
|
+
ctx = CompilerContext()
|
|
1123
|
+
# Do not swallow HookInvocationError here: --strict must surface failures.
|
|
1124
|
+
get_global_hook_dispatcher().collect_build_configuration(ctx)
|
|
1125
|
+
inc = None
|
|
1126
|
+
libdir = None
|
|
1127
|
+
for pth in (ctx.include_paths or []):
|
|
1128
|
+
if pth and os.path.isdir(pth):
|
|
1129
|
+
if os.path.isfile(os.path.join(pth, "openssl", "evp.h")):
|
|
1130
|
+
inc = pth
|
|
1148
1131
|
break
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1132
|
+
for pth in (ctx.library_paths or []):
|
|
1133
|
+
if pth and os.path.isdir(pth):
|
|
1134
|
+
libdir = pth
|
|
1135
|
+
break
|
|
1136
|
+
if inc and libdir:
|
|
1137
|
+
return inc, libdir
|
|
1138
|
+
if inc:
|
|
1139
|
+
cand = os.path.join(os.path.dirname(inc), "lib")
|
|
1140
|
+
if os.path.isdir(cand):
|
|
1141
|
+
return inc, cand
|
|
1157
1142
|
inc = os.environ.get("CPYTE_OPENSSL_INCLUDE")
|
|
1158
1143
|
libdir = os.environ.get("CPYTE_OPENSSL_LIBDIR") or os.environ.get("CPYTE_OPENSSL_LIB")
|
|
1159
1144
|
if inc and libdir:
|
|
@@ -1207,6 +1192,30 @@ def _find_openssl_paths():
|
|
|
1207
1192
|
return None, None
|
|
1208
1193
|
|
|
1209
1194
|
|
|
1195
|
+
def hook_build_context():
|
|
1196
|
+
"""Collect build settings contributed by loaded `BuildHook`s."""
|
|
1197
|
+
from .extension_hooks import CompilerContext, get_global_hook_dispatcher
|
|
1198
|
+
|
|
1199
|
+
ctx = CompilerContext()
|
|
1200
|
+
# Failures follow the registry policy; --strict must not be swallowed.
|
|
1201
|
+
get_global_hook_dispatcher().collect_build_configuration(ctx)
|
|
1202
|
+
return ctx
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
def hook_compile_flags(ctx) -> list[str]:
|
|
1206
|
+
"""Translate a hook build context into clang/gcc compile flags."""
|
|
1207
|
+
flags: list[str] = []
|
|
1208
|
+
for path in ctx.include_paths:
|
|
1209
|
+
if path:
|
|
1210
|
+
flags.append(f"-I{path}")
|
|
1211
|
+
for name, value in ctx.defines.items():
|
|
1212
|
+
if not name:
|
|
1213
|
+
continue
|
|
1214
|
+
flags.append(f"-D{name}={value}" if value is not None else f"-D{name}")
|
|
1215
|
+
flags.extend(ctx.compiler_flags)
|
|
1216
|
+
return flags
|
|
1217
|
+
|
|
1218
|
+
|
|
1210
1219
|
def _runtime_extra_flags(src_path: str) -> tuple:
|
|
1211
1220
|
"""Extra clang flags needed to compile a runtime C source.
|
|
1212
1221
|
|
|
@@ -1281,16 +1290,41 @@ _GMP_LINK_NEEDLE = re.compile(r"(?:gmp[a-z]*_|mpfr_|libgmp|libmpfr|-lgmp|-lmpfr)
|
|
|
1281
1290
|
|
|
1282
1291
|
|
|
1283
1292
|
def aot_link_executable(linker, objects, output, **kwargs):
|
|
1284
|
-
"""Link an AOT executable, pulling in GMP/MPFR/OpenSSL only when needed.
|
|
1285
|
-
|
|
1293
|
+
"""Link an AOT executable, pulling in GMP/MPFR/OpenSSL only when needed.
|
|
1294
|
+
|
|
1295
|
+
Build-hook libraries, library paths and linker flags are always applied;
|
|
1296
|
+
the built-in dependency probe only adds the compiler's own requirements.
|
|
1297
|
+
"""
|
|
1298
|
+
ctx = hook_build_context()
|
|
1299
|
+
hook_libs = list(dict.fromkeys(ctx.libraries))
|
|
1300
|
+
hook_paths = [p for p in ctx.library_paths if p]
|
|
1301
|
+
hook_flags = list(ctx.linker_flags)
|
|
1302
|
+
|
|
1303
|
+
libs = list(dict.fromkeys(["m", *hook_libs]))
|
|
1304
|
+
lp = [p for p in (kwargs.get("library_paths") or []) if p]
|
|
1305
|
+
for path in hook_paths:
|
|
1306
|
+
if path not in lp:
|
|
1307
|
+
lp.append(path)
|
|
1308
|
+
kwargs["library_paths"] = lp
|
|
1309
|
+
kwargs["extra_flags"] = [
|
|
1310
|
+
*(kwargs.get("extra_flags") or ()),
|
|
1311
|
+
*hook_flags,
|
|
1312
|
+
]
|
|
1313
|
+
|
|
1286
1314
|
ok, stderr = linker.link(objects, output, libraries=libs, check=False, **kwargs)
|
|
1287
1315
|
if ok:
|
|
1288
1316
|
return output
|
|
1289
1317
|
# If missing GMP/MPFR or OpenSSL symbols, add libraries
|
|
1290
|
-
if
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1318
|
+
if (
|
|
1319
|
+
_GMP_LINK_NEEDLE.search(stderr or "")
|
|
1320
|
+
or "EVP_" in (stderr or "")
|
|
1321
|
+
or "HMAC" in (stderr or "")
|
|
1322
|
+
or "openssl" in (stderr or "").lower()
|
|
1323
|
+
):
|
|
1324
|
+
libs = list(dict.fromkeys([*aot_link_libraries(), *hook_libs]))
|
|
1325
|
+
for path in _gmp_library_paths():
|
|
1326
|
+
if path not in lp:
|
|
1327
|
+
lp.append(path)
|
|
1294
1328
|
try:
|
|
1295
1329
|
_o_inc, _o_lib = _find_openssl_paths()
|
|
1296
1330
|
if _o_lib and _o_lib not in lp:
|
|
@@ -1385,7 +1419,9 @@ def _cached_c_ir(
|
|
|
1385
1419
|
_binding.initialize_native_target()
|
|
1386
1420
|
_binding.initialize_native_asmprinter()
|
|
1387
1421
|
opt_mod = _binding.parse_assembly(_remove_probe_stack_ir(ir_text))
|
|
1388
|
-
|
|
1422
|
+
opt_mod = optimize(
|
|
1423
|
+
opt_mod, jit_opt_level, opt_size=jit_opt_size
|
|
1424
|
+
)
|
|
1389
1425
|
opt_text = str(opt_mod)
|
|
1390
1426
|
if opt_text.lstrip().startswith(";"):
|
|
1391
1427
|
tmp_path = cache_path + ".tmp"
|
|
@@ -1426,6 +1462,21 @@ def run_jit(
|
|
|
1426
1462
|
with _llvm_binding_lock:
|
|
1427
1463
|
mod = binding.parse_assembly(llvm_ir)
|
|
1428
1464
|
llvm_cc = _find_llvm_cc()
|
|
1465
|
+
hook_ctx = hook_build_context()
|
|
1466
|
+
hook_flags = hook_compile_flags(hook_ctx)
|
|
1467
|
+
from .extension_hooks import get_global_hook_dispatcher
|
|
1468
|
+
|
|
1469
|
+
for library in get_global_hook_dispatcher().runtime_libraries(hook_ctx):
|
|
1470
|
+
# JIT symbols must be present in the host process; loading the shared
|
|
1471
|
+
# object is the JIT equivalent of the AOT `-l` flag.
|
|
1472
|
+
library_name = ctypes.util.find_library(library) or library
|
|
1473
|
+
try:
|
|
1474
|
+
ctypes.CDLL(library_name)
|
|
1475
|
+
except OSError as exc:
|
|
1476
|
+
print_warn(
|
|
1477
|
+
f"could not load runtime library {library!r} requested by an "
|
|
1478
|
+
f"extension hook: {exc}"
|
|
1479
|
+
)
|
|
1429
1480
|
if src_files:
|
|
1430
1481
|
# Compile every ccode module in parallel across all cores; the clang
|
|
1431
1482
|
# subprocesses dominate wall time and are independent, while the
|
|
@@ -1437,13 +1488,10 @@ def run_jit(
|
|
|
1437
1488
|
if src.endswith(".ll"):
|
|
1438
1489
|
with open(src) as f:
|
|
1439
1490
|
return src, f.read(), None
|
|
1440
|
-
extra =
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
extra.append(f"-I{openssl_inc}")
|
|
1445
|
-
except Exception:
|
|
1446
|
-
pass
|
|
1491
|
+
extra = list(hook_flags)
|
|
1492
|
+
openssl_inc, _ = _find_openssl_paths()
|
|
1493
|
+
if openssl_inc:
|
|
1494
|
+
extra.append(f"-I{openssl_inc}")
|
|
1447
1495
|
r = subprocess.run(
|
|
1448
1496
|
[
|
|
1449
1497
|
llvm_cc,
|
|
@@ -1496,7 +1544,7 @@ def run_jit(
|
|
|
1496
1544
|
# final whole-module re-optimization over the remaining runtime helpers is
|
|
1497
1545
|
# then skipped.
|
|
1498
1546
|
if opt_level > 0:
|
|
1499
|
-
optimize(mod, opt_level)
|
|
1547
|
+
mod = optimize(mod, opt_level)
|
|
1500
1548
|
|
|
1501
1549
|
# Link the C runtime (print/assign/dynamic dispatch/array registry) so the
|
|
1502
1550
|
# JIT can resolve native helpers that have no Python callback mirror.
|
|
@@ -2076,7 +2124,7 @@ def run_aot(
|
|
|
2076
2124
|
fn.linkage = "internal"
|
|
2077
2125
|
mod.verify()
|
|
2078
2126
|
_prune_module(mod)
|
|
2079
|
-
optimize(mod, opt_level)
|
|
2127
|
+
mod = optimize(mod, opt_level)
|
|
2080
2128
|
mod.verify()
|
|
2081
2129
|
|
|
2082
2130
|
target_machine = make_target_machine(pic=pic, codemodel="small", opt=opt_level)
|
|
@@ -2245,7 +2293,7 @@ def run_scorpion(
|
|
|
2245
2293
|
mod = binding.parse_assembly(llvm_ir)
|
|
2246
2294
|
|
|
2247
2295
|
mod.verify()
|
|
2248
|
-
optimize(mod, opt_level)
|
|
2296
|
+
mod = optimize(mod, opt_level)
|
|
2249
2297
|
mod.verify()
|
|
2250
2298
|
|
|
2251
2299
|
defined = {f.name for f in mod.functions if not getattr(f, "is_declaration", True)}
|
|
@@ -205,6 +205,7 @@ class HookStage(Enum):
|
|
|
205
205
|
CODEGEN = "codegen"
|
|
206
206
|
OPTIMIZE = "optimize"
|
|
207
207
|
LINK = "link"
|
|
208
|
+
SYMBOL = "symbol"
|
|
208
209
|
RUNTIME = "runtime"
|
|
209
210
|
|
|
210
211
|
|
|
@@ -400,7 +401,7 @@ class SymbolResolverHook(CompilerHook):
|
|
|
400
401
|
generated declarations, etc.
|
|
401
402
|
"""
|
|
402
403
|
|
|
403
|
-
stage = HookStage.
|
|
404
|
+
stage = HookStage.SYMBOL
|
|
404
405
|
|
|
405
406
|
def can_resolve_import(self, name: str) -> bool:
|
|
406
407
|
return False
|
|
@@ -933,15 +934,19 @@ class HookRegistry:
|
|
|
933
934
|
) -> None:
|
|
934
935
|
self._context = context
|
|
935
936
|
|
|
937
|
+
if context is None:
|
|
938
|
+
return
|
|
936
939
|
for hook in self.all():
|
|
937
|
-
hook
|
|
940
|
+
self.invoke_context(hook, "initialize", context)
|
|
938
941
|
|
|
939
942
|
def shutdown_all(
|
|
940
943
|
self,
|
|
941
944
|
context: CompilerContext,
|
|
942
945
|
) -> None:
|
|
946
|
+
if context is None:
|
|
947
|
+
return
|
|
943
948
|
for hook in reversed(self.all()):
|
|
944
|
-
hook
|
|
949
|
+
self.invoke_context(hook, "shutdown", context)
|
|
945
950
|
|
|
946
951
|
def clear(self) -> None:
|
|
947
952
|
self._hooks = {stage: [] for stage in HookStage}
|
|
@@ -984,7 +989,12 @@ class HookLoader:
|
|
|
984
989
|
)
|
|
985
990
|
|
|
986
991
|
for hook in hooks:
|
|
987
|
-
|
|
992
|
+
# `initialize` is a callback, so it follows the same
|
|
993
|
+
# arity-compat and failure policy as every other hook
|
|
994
|
+
# method. A failed initializer is skipped; a hook *file*
|
|
995
|
+
# that cannot be loaded is still a hard error above.
|
|
996
|
+
if registry.invoke_context(hook, "initialize", context) is _HOOK_FAILED:
|
|
997
|
+
continue
|
|
988
998
|
registry.register(hook)
|
|
989
999
|
loaded += 1
|
|
990
1000
|
|
|
@@ -1145,6 +1155,59 @@ class HookDispatcher:
|
|
|
1145
1155
|
return replacement
|
|
1146
1156
|
return None
|
|
1147
1157
|
|
|
1158
|
+
# ------------------------------------------------------------------
|
|
1159
|
+
# Parser
|
|
1160
|
+
# ------------------------------------------------------------------
|
|
1161
|
+
|
|
1162
|
+
def parse_statement(
|
|
1163
|
+
self,
|
|
1164
|
+
tokens: Any,
|
|
1165
|
+
pos: int,
|
|
1166
|
+
context: CompilerContext,
|
|
1167
|
+
) -> Any | None:
|
|
1168
|
+
"""Return ``(node, pos)`` from the first parser hook that handles it.
|
|
1169
|
+
|
|
1170
|
+
Failures are recorded (and skipped, unless strict mode is on) by the
|
|
1171
|
+
registry, so a broken parser hook falls through to the built-in parser
|
|
1172
|
+
instead of silently swallowing the exception.
|
|
1173
|
+
"""
|
|
1174
|
+
for hook in self._hooks(HookStage.PARSER):
|
|
1175
|
+
if not isinstance(hook, ParserHook):
|
|
1176
|
+
continue
|
|
1177
|
+
should = self.registry.invoke_plain(
|
|
1178
|
+
hook, "should_handle_statement", tokens, pos
|
|
1179
|
+
)
|
|
1180
|
+
if should is _HOOK_FAILED or not should:
|
|
1181
|
+
continue
|
|
1182
|
+
parsed = self.registry.invoke_context(
|
|
1183
|
+
hook, "parse_statement", context, tokens, pos
|
|
1184
|
+
)
|
|
1185
|
+
if parsed is not _HOOK_FAILED:
|
|
1186
|
+
return parsed
|
|
1187
|
+
return None
|
|
1188
|
+
|
|
1189
|
+
def parse_expression(
|
|
1190
|
+
self,
|
|
1191
|
+
tokens: Any,
|
|
1192
|
+
pos: int,
|
|
1193
|
+
context: CompilerContext,
|
|
1194
|
+
) -> Any | None:
|
|
1195
|
+
"""Return an expression produced by the first matching parser hook."""
|
|
1196
|
+
for hook in self._hooks(HookStage.PARSER):
|
|
1197
|
+
if not isinstance(hook, ParserHook):
|
|
1198
|
+
continue
|
|
1199
|
+
should = self.registry.invoke_plain(
|
|
1200
|
+
hook, "should_handle_expression", tokens, pos
|
|
1201
|
+
)
|
|
1202
|
+
if should is _HOOK_FAILED or not should:
|
|
1203
|
+
continue
|
|
1204
|
+
parsed = self.registry.invoke_context(
|
|
1205
|
+
hook, "parse_expression", context, tokens, pos
|
|
1206
|
+
)
|
|
1207
|
+
if parsed is not _HOOK_FAILED:
|
|
1208
|
+
return parsed
|
|
1209
|
+
return None
|
|
1210
|
+
|
|
1148
1211
|
# ------------------------------------------------------------------
|
|
1149
1212
|
# AST transformation
|
|
1150
1213
|
# ------------------------------------------------------------------
|
|
@@ -1257,7 +1320,7 @@ class HookDispatcher:
|
|
|
1257
1320
|
name: str,
|
|
1258
1321
|
context: CompilerContext,
|
|
1259
1322
|
) -> Any | None:
|
|
1260
|
-
for hook in self._hooks(HookStage.
|
|
1323
|
+
for hook in self._hooks(HookStage.SYMBOL):
|
|
1261
1324
|
if not isinstance(hook, SymbolResolverHook):
|
|
1262
1325
|
continue
|
|
1263
1326
|
can = self.registry.invoke_plain(hook, "can_resolve_import", name)
|
|
@@ -1275,7 +1338,7 @@ class HookDispatcher:
|
|
|
1275
1338
|
name: str,
|
|
1276
1339
|
context: CompilerContext,
|
|
1277
1340
|
) -> Any | None:
|
|
1278
|
-
for hook in self._hooks(HookStage.
|
|
1341
|
+
for hook in self._hooks(HookStage.SYMBOL):
|
|
1279
1342
|
if not isinstance(hook, SymbolResolverHook):
|
|
1280
1343
|
continue
|
|
1281
1344
|
can = self.registry.invoke_plain(hook, "can_resolve_symbol", name)
|
|
@@ -1349,6 +1412,25 @@ class HookDispatcher:
|
|
|
1349
1412
|
module = optimized
|
|
1350
1413
|
return module
|
|
1351
1414
|
|
|
1415
|
+
def add_optimization_passes(
|
|
1416
|
+
self,
|
|
1417
|
+
module: Any,
|
|
1418
|
+
pass_manager: Any,
|
|
1419
|
+
context: CompilerContext,
|
|
1420
|
+
) -> None:
|
|
1421
|
+
"""Let optimize hooks extend the active LLVM pass manager."""
|
|
1422
|
+
for hook in self._hooks(HookStage.OPTIMIZE):
|
|
1423
|
+
if not isinstance(hook, OptimizeHook):
|
|
1424
|
+
continue
|
|
1425
|
+
should = self.registry.invoke_context(
|
|
1426
|
+
hook, "should_add_passes", context
|
|
1427
|
+
)
|
|
1428
|
+
if should is _HOOK_FAILED or not should:
|
|
1429
|
+
continue
|
|
1430
|
+
self.registry.invoke_context(
|
|
1431
|
+
hook, "add_module_passes", context, pass_manager
|
|
1432
|
+
)
|
|
1433
|
+
|
|
1352
1434
|
# ------------------------------------------------------------------
|
|
1353
1435
|
# Build
|
|
1354
1436
|
# ------------------------------------------------------------------
|
|
@@ -1380,6 +1462,22 @@ class HookDispatcher:
|
|
|
1380
1462
|
# Runtime
|
|
1381
1463
|
# ------------------------------------------------------------------
|
|
1382
1464
|
|
|
1465
|
+
def runtime_files(
|
|
1466
|
+
self,
|
|
1467
|
+
context: CompilerContext,
|
|
1468
|
+
) -> list[tuple[CompilerHook, str]]:
|
|
1469
|
+
"""``(hook, relative_path)`` pairs for runtime C source files."""
|
|
1470
|
+
result: list[tuple[CompilerHook, str]] = []
|
|
1471
|
+
for hook in self._hooks(HookStage.RUNTIME):
|
|
1472
|
+
if not isinstance(hook, RuntimeHook):
|
|
1473
|
+
continue
|
|
1474
|
+
value = self.registry.invoke_context(hook, "get_runtime_files", context)
|
|
1475
|
+
if value is not _HOOK_FAILED and value:
|
|
1476
|
+
for rel_file in value:
|
|
1477
|
+
if isinstance(rel_file, str):
|
|
1478
|
+
result.append((hook, rel_file))
|
|
1479
|
+
return result
|
|
1480
|
+
|
|
1383
1481
|
def runtime_code(
|
|
1384
1482
|
self,
|
|
1385
1483
|
context: CompilerContext,
|
|
@@ -1429,6 +1527,10 @@ class HookDispatcher:
|
|
|
1429
1527
|
if isinstance(hook, LifecycleHook):
|
|
1430
1528
|
self.registry.invoke_context(hook, "on_compile_error", context)
|
|
1431
1529
|
|
|
1530
|
+
def shutdown_all(self, context: CompilerContext) -> None:
|
|
1531
|
+
"""Run `shutdown(context)` for every registered hook, in reverse order."""
|
|
1532
|
+
self.registry.shutdown_all(context)
|
|
1533
|
+
|
|
1432
1534
|
|
|
1433
1535
|
# ============================================================================
|
|
1434
1536
|
# Global Registry
|