-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim_native_codegen.py
More file actions
57 lines (49 loc) · 1.73 KB
/
dim_native_codegen.py
File metadata and controls
57 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# dim_native_codegen.py — Native binary generation
import subprocess
import os
import tempfile
from typing import Optional
class NativeCodegen:
def __init__(self, ll_file: str, output: str = "a.out"):
self.ll_file = ll_file
self.output = output
def compile_to_object(self) -> str:
obj_file = tempfile.mktemp(suffix=".o")
result = subprocess.run(
["llc", "-filetype=obj", "-o", obj_file, self.ll_file],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"llc failed: {result.stderr}")
return obj_file
def link(self, obj_file: str) -> str:
result = subprocess.run(
["gcc", "-o", self.output, obj_file],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"link failed: {result.stderr}")
return self.output
def compile(self) -> Optional[str]:
try:
obj_file = self.compile_to_object()
return self.link(obj_file)
except FileNotFoundError as e:
print(f"Warning: {e}")
print("Install LLVM tools (llc, lld) to generate native binaries")
return None
except Exception as e:
print(f"Native compilation failed: {e}")
return None
def emit_native(llvm_ir: str, output: str = "a.out") -> Optional[str]:
with tempfile.NamedTemporaryFile(mode="w", suffix=".ll", delete=False) as f:
f.write(llvm_ir)
ll_file = f.name
try:
codegen = NativeCodegen(ll_file, output)
return codegen.compile()
finally:
if os.path.exists(ll_file):
os.unlink(ll_file)