-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
523 lines (443 loc) · 17.5 KB
/
main.py
File metadata and controls
523 lines (443 loc) · 17.5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# Collect and print all C definitions from LLVM-C headers
# so we can compare what's missing in the Racket bindings.
#
# Modified from https://github.com/revng/llvmcpy/blob/master/llvmcpy/_generator.py
# under MIT license
import fnmatch
import os
import re
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any, List, MutableMapping, Tuple, Union
from cffi import FFI
Properties = MutableMapping[str, Tuple[Tuple[str, str], Tuple[str, str]]]
EnumMap = MutableMapping[Union[int, str], Union[int, str]]
def extract_enums_from_headers(include_dir: Path) -> MutableMapping[str, EnumMap]:
"""Extract enums directly from header files using regex patterns"""
enums = {}
for header_file in include_dir.glob("llvm-c/**/*.h"):
content = header_file.read_text()
# Pattern: typedef enum { ... } EnumName;
enum_pattern = r"typedef\s+enum\s*\{([^}]+)\}\s*([A-Za-z][A-Za-z0-9_]*)\s*;"
for match in re.finditer(enum_pattern, content, re.DOTALL):
enum_body = match.group(1)
enum_name = match.group(2)
enum_values = {}
current_value = 0
for entry in enum_body.split(","):
entry = entry.strip()
if not entry:
continue
# Remove comments
entry = re.sub(r"/\*.*?\*/", "", entry, flags=re.DOTALL)
entry = re.sub(r"//.*$", "", entry, flags=re.MULTILINE)
entry = re.sub(r"\*.*?\*", "", entry)
entry = re.sub(r"\*.*", "", entry)
entry = entry.strip()
if entry.startswith("*") or not re.match(r"^[A-Za-z]", entry):
continue
if not entry:
continue
if "=" in entry:
name, value_str = entry.split("=", 1)
name = name.strip()
value_str = value_str.strip()
try:
current_value = int(value_str)
except ValueError:
current_value += 1
else:
name = entry.strip()
if name:
enum_values[current_value] = name
enum_values[name] = current_value
current_value += 1
if enum_values:
enums[enum_name] = enum_values
return enums
def clean_include_file(in_path: Path) -> None:
"""Clean LLVM-C API headers for parsing by CFFI"""
header_blacklist = [
"llvm/Support/DataTypes.h",
"llvm-c/DataTypes.h",
"llvm-c/blake3.h",
"math.h",
"stddef.h",
"cstddef",
"sys/types.h",
"stdbool.h",
]
out_path = Path(str(in_path) + ".filtered")
with in_path.open("r", encoding="utf8") as in_file, out_path.open(
"w", encoding="utf8"
) as out_file:
skip_block = False
for line in in_file:
skip = False
for header in header_blacklist:
if line.startswith("#include ") and header in line:
skip = True
if line.startswith("static inline"):
skip_block = True
if skip or skip_block:
out_file.write("// ")
line = re.sub(r"\b0U\b", "0", line)
out_file.write(line)
if line.startswith("}"):
skip_block = False
shutil.move(out_path, in_path)
def parse_headers(
cpp: str, libraries: List[Path], include_dir: Path
) -> Tuple[List[Tuple[Path, str, Any]], FFI, MutableMapping[str, EnumMap]]:
"""Parse LLVM-C headers and return libraries, FFI instance, and enums"""
def recursive_chmod(path: Path):
path.chmod(0o700)
for dirpath_str, _, filenames in os.walk(str(path)):
dirpath = Path(dirpath_str)
dirpath.chmod(0o700)
for filename in filenames:
(dirpath / filename).chmod(0o600)
ffi = FFI()
temp_directory = Path(tempfile.mkdtemp())
try:
llvm_path = temp_directory / "llvm"
llvm_path.mkdir()
llvm_c_path = temp_directory / "llvm-c"
shutil.copytree(include_dir / "llvm-c", llvm_c_path)
shutil.copytree(
include_dir / "llvm" / "Config", temp_directory / "llvm" / "Config"
)
recursive_chmod(temp_directory)
include_files = []
for root_str, _, filenames in os.walk(llvm_c_path):
root = Path(root_str)
for filename in fnmatch.filter(filenames, "*.h"):
if filename != "DataTypes.h" and filename != "blake3.h":
header_path = root / filename
include_files.append(
str(header_path.relative_to(temp_directory))
)
clean_include_file(header_path)
(temp_directory / "llvm-c" / "Deprecated.h").write_text(
"""
#ifndef LLVM_C_DEPRECATED_H
#define LLVM_C_DEPRECATED_H
#endif /* LLVM_C_DEPRECATED_H */
# define LLVM_ATTRIBUTE_C_DEPRECATED(decl, message) decl
"""
)
blake3_h = temp_directory / "llvm-c" / "blake3.h"
if blake3_h.exists():
blake3_h.unlink()
all_c = """
typedef long unsigned int size_t;
typedef int off_t;
"""
all_c += '#include "'
all_c += '"\n#include "'.join(include_files) + '"'
all_c += "\n"
all_c_path = temp_directory / "all.c"
all_c_path.write_text(all_c)
result = subprocess.run(
[
cpp,
"-U__GNUC__",
"-I" + str(temp_directory),
"-I" + str(include_dir),
"-E",
str(all_c_path),
],
capture_output=True,
text=True,
check=True,
)
preprocessed_content = result.stdout
simple_filtered_lines = []
for line in preprocessed_content.split("\n"):
line = line.strip()
if line.startswith("#") or not line or "##" in line:
continue
simple_filtered_lines.append(line)
simple_filtered_content = "\n".join(simple_filtered_lines)
# Remove __attribute__((...)) which CFFI cannot parse
def strip_attributes(text: str) -> str:
result = []
i = 0
while i < len(text):
if text[i:].startswith("__attribute__"):
j = i + len("__attribute__")
# skip whitespace
while j < len(text) and text[j] in " \t\n":
j += 1
if j < len(text) and text[j] == "(":
depth = 0
while j < len(text):
if text[j] == "(":
depth += 1
elif text[j] == ")":
depth -= 1
if depth == 0:
j += 1
break
j += 1
i = j
continue
result.append(text[i])
i += 1
return "".join(result)
simple_filtered_content = strip_attributes(simple_filtered_content)
try:
ffi.cdef(simple_filtered_content, override=True)
except Exception as e:
print(f"CFFI parsing failed: {e}")
print("Falling back to simplified parsing...")
essential_content = """
typedef struct LLVMOpaqueContext *LLVMContextRef;
typedef struct LLVMOpaqueModule *LLVMModuleRef;
typedef struct LLVMOpaqueValue *LLVMValueRef;
typedef struct LLVMOpaqueBuilder *LLVMBuilderRef;
typedef struct LLVMOpaqueType *LLVMTypeRef;
typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;
typedef struct LLVMOpaquePassManager *LLVMPassManagerRef;
typedef struct LLVMOpaqueExecutionEngine *LLVMExecutionEngineRef;
typedef struct LLVMOpaqueGenericValue *LLVMGenericValueRef;
typedef int LLVMBool;
"""
ffi.cdef(essential_content, override=True)
ffi.set_source("ffi", None)
ffi.compile(str(temp_directory))
# Extract enums from the original headers
enums = extract_enums_from_headers(include_dir)
finally:
shutil.rmtree(temp_directory)
def basename(x: Path) -> str:
result = x.name
result = os.path.splitext(result)[0]
result = result.replace(".", "")
result = result.replace("-", "")
return result
libs = [
(lib_file, basename(lib_file), ffi.dlopen(str(lib_file)))
for lib_file in libraries
]
return libs, ffi, enums
def format_cffi_type(cffi_type) -> str:
"""Format a CFFI type as a human-readable C type string"""
if cffi_type.kind == "void":
return "void"
elif cffi_type.kind == "primitive":
return cffi_type.cname
elif cffi_type.kind == "pointer":
pointee = cffi_type.item
return format_cffi_type(pointee) + " *"
elif cffi_type.kind == "struct":
return cffi_type.cname
elif cffi_type.kind == "enum":
return cffi_type.cname
elif cffi_type.kind == "function":
return "function_pointer"
elif cffi_type.kind == "array":
return format_cffi_type(cffi_type.item) + "[]"
else:
return cffi_type.cname if hasattr(cffi_type, "cname") else str(cffi_type)
def format_function_signature(name: str, prototype) -> str:
"""Format a function prototype as a C declaration string"""
ret = format_cffi_type(prototype.result)
args = []
for arg in prototype.args:
args.append(format_cffi_type(arg))
args_str = ", ".join(args) if args else "void"
return f"{ret} {name}({args_str})"
def load_racket_bindings(bindings_path: Path) -> Tuple[set, MutableMapping[str, set]]:
"""Extract #:c-id names and enum values from bindings.rkt.
Returns (bound_functions, bound_enum_values) where bound_enum_values
maps each Racket _enum variable name to a set of integer values defined in it.
"""
content = bindings_path.read_text()
bound_functions = set(re.findall(r"#:c-id\s+(\S+)\)", content))
# Extract enum value sets: find all _enum blocks and collect their integer values
# Each block looks like: (define _name (_enum '(... name = N ...)))
bound_enum_values: MutableMapping[str, set] = {}
for match in re.finditer(
r"\(define\s+(\S+)\s*\n?\s*\(_enum\s+'\((.+?)\)\)\)",
content,
re.DOTALL,
):
enum_var = match.group(1)
enum_body = match.group(2)
values = set()
for val_match in re.finditer(r"=\s*(\d+)", enum_body):
values.add(int(val_match.group(1)))
if values:
bound_enum_values[enum_var] = values
return bound_functions, bound_enum_values
def main():
import argparse
import json
import sys
parser = argparse.ArgumentParser(
description="Collect and print all C definitions from LLVM-C headers"
)
parser.add_argument("--cpp", default="cpp", help="C preprocessor command")
parser.add_argument(
"--include-dir", help="LLVM include directory (uses llvm-config if not specified)"
)
parser.add_argument(
"--lib-dir", help="LLVM library directory (uses llvm-config if not specified)"
)
parser.add_argument(
"--format",
choices=["text", "json"],
default="text",
help="Output format (default: text)",
)
args = parser.parse_args()
# Load existing Racket bindings
bindings_path = Path(__file__).parent / "private" / "bindings.rkt"
if bindings_path.exists():
bound_functions, bound_enum_values = load_racket_bindings(bindings_path)
else:
bound_functions, bound_enum_values = set(), {}
# Get LLVM directories using llvm-config if not provided
if not args.include_dir or not args.lib_dir:
try:
if not args.include_dir:
result = subprocess.run(
["llvm-config", "--includedir"],
capture_output=True,
text=True,
check=True,
)
include_dir = Path(result.stdout.strip())
else:
include_dir = Path(args.include_dir)
if not args.lib_dir:
result = subprocess.run(
["llvm-config", "--libdir"],
capture_output=True,
text=True,
check=True,
)
lib_dir = Path(result.stdout.strip())
else:
lib_dir = Path(args.lib_dir)
except (subprocess.CalledProcessError, FileNotFoundError):
print(
"Error: llvm-config not found. Please specify --include-dir and --lib-dir manually.",
file=sys.stderr,
)
sys.exit(1)
else:
include_dir = Path(args.include_dir)
lib_dir = Path(args.lib_dir)
# Find LLVM libraries
libraries = []
for pattern in ["libLLVM*.so", "libLLVM*.dylib", "libLLVM*.dll"]:
libraries.extend(lib_dir.glob(pattern))
if not libraries:
print(f"Error: No LLVM libraries found in {lib_dir}", file=sys.stderr)
sys.exit(1)
print(f"Using LLVM include dir: {include_dir}", file=sys.stderr)
print(f"Using LLVM lib dir: {lib_dir}", file=sys.stderr)
print(
f"Found LLVM libraries: {[lib.name for lib in libraries]}", file=sys.stderr
)
# Parse headers
libs, ffi, enums = parse_headers(args.cpp, libraries, include_dir)
# Collect all definitions
functions = []
constants = []
seen_functions = set()
for _, library_name, library in libs:
for name in dir(library):
if hasattr(library, name):
field = getattr(library, name)
if isinstance(field, int):
constants.append((name, field))
elif isinstance(field, FFI.CData):
prototype = ffi.typeof(field)
if name not in seen_functions:
seen_functions.add(name)
functions.append((name, prototype))
# Sort for stable output
functions.sort(key=lambda x: x[0])
constants.sort(key=lambda x: x[0])
# Filter out functions already bound in Racket
missing_functions = [(n, p) for n, p in functions if n not in bound_functions]
# Filter enums: find C enum members whose values are not in any Racket _enum
# Match C enums to Racket enums by finding overlapping value sets
all_bound_values = set()
for vals in bound_enum_values.values():
all_bound_values.update(vals)
missing_enums: MutableMapping[str, MutableMapping[str, int]] = {}
for enum_name, enum_values in enums.items():
# Get the C enum's value->name mapping
c_value_to_name = {
v: k for k, v in enum_values.items() if isinstance(k, str)
}
c_values = set(c_value_to_name.keys())
# Find which Racket _enum block best matches this C enum
best_match = None
best_overlap = 0
for rkt_name, rkt_values in bound_enum_values.items():
overlap = len(c_values & rkt_values)
if overlap > best_overlap:
best_overlap = overlap
best_match = rkt_name
if best_match and best_overlap > 0:
rkt_values = bound_enum_values[best_match]
missing_values = c_values - rkt_values
if missing_values:
missing_enums[enum_name] = {
c_value_to_name[v]: v
for v in sorted(missing_values)
if v in c_value_to_name
}
else:
# Entire enum is missing
missing_enums[enum_name] = {
k: v for k, v in enum_values.items() if isinstance(k, str)
}
# Filter out enums with no missing members
missing_enums = {k: v for k, v in missing_enums.items() if v}
if args.format == "json":
output = {
"functions": [],
"enums": {},
}
for name, prototype in missing_functions:
ret = format_cffi_type(prototype.result)
func_args = [format_cffi_type(a) for a in prototype.args]
output["functions"].append(
{
"name": name,
"return_type": ret,
"args": func_args,
"signature": format_function_signature(name, prototype),
}
)
for enum_name, members in missing_enums.items():
output["enums"][enum_name] = members
print(json.dumps(output, indent=2))
else:
# Text output
print(f"=== Missing LLVM-C Bindings ({len(missing_functions)} functions, {len(missing_enums)} enums with missing members) ===")
print()
if missing_enums:
print("--- Missing Enum Members ---")
for enum_name, members in sorted(missing_enums.items()):
print(f"typedef enum {enum_name} {{")
for entry_name, entry_value in sorted(
members.items(), key=lambda x: x[1]
):
print(f" {entry_name} = {entry_value},")
print("}")
print()
if missing_functions:
print("--- Missing Functions ---")
for name, prototype in missing_functions:
print(format_function_signature(name, prototype) + ";")
if __name__ == "__main__":
main()