| 1 |
import sys |
| 2 |
import json |
| 3 |
import glob |
| 4 |
from collections import OrderedDict, defaultdict |
| 5 |
import re |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
def extract_module_names(file_path): |
| 9 |
"""Extract module names from a Verilog file, preserving order.""" |
| 10 |
with open(file_path, 'r', encoding='utf-8') as f: |
| 11 |
content = f.read() |
| 12 |
|
| 13 |
module_pattern = r'module\s+([A-Za-z0-9_]+)\s*[\(;]' |
| 14 |
modules = [] |
| 15 |
for match in re.finditer(module_pattern, content): |
| 16 |
module_name = match.group(1) |
| 17 |
if module_name not in modules: |
| 18 |
modules.append(module_name) |
| 19 |
|
| 20 |
return modules |
| 21 |
|
| 22 |
def analyze_used_primitives(json_pattern): |
| 23 |
"""Extract primitives used in example designs.""" |
| 24 |
primitive_counts = defaultdict(int) |
| 25 |
|
| 26 |
for filename in glob.glob(json_pattern): |
| 27 |
with open(filename) as f: |
| 28 |
design = json.load(f) |
| 29 |
|
| 30 |
for module in design['modules'].values(): |
| 31 |
for cell in module.get('cells', {}).values(): |
| 32 |
primitive_counts[cell['type']] += 1 |
| 33 |
|
| 34 |
return set(primitive_counts.keys()) |
| 35 |
|
| 36 |
def escape_markdown(text): |
| 37 |
"""Escape underscores for markdown.""" |
| 38 |
return text.replace('_', '\\_') |
| 39 |
|
| 40 |
def generate_markdown_table(files_modules, column_names, apicula_primitives): |
| 41 |
"""Generate a markdown table including Apicula usage data.""" |
| 42 |
all_modules = [] |
| 43 |
for modules in files_modules.values(): |
| 44 |
for module in modules: |
| 45 |
if module not in all_modules: |
| 46 |
all_modules.append(module) |
| 47 |
|
| 48 |
headers = ['Primitive'] + list(column_names.values()) + ['Apicula'] |
| 49 |
header_row = '| ' + ' | '.join(headers) + ' |' |
| 50 |
separator_row = '| ' + ' | '.join(['---' for _ in headers]) + ' |' |
| 51 |
|
| 52 |
rows = [] |
| 53 |
for module in all_modules: |
| 54 |
row = [f'[[{escape_markdown(module)}]]'] |
| 55 |
for file in files_modules.keys(): |
| 56 |
row.append('✓' if module in files_modules[file] else '') |
| 57 |
row.append('✓' if module in apicula_primitives else '') |
| 58 |
rows.append('| ' + ' | '.join(row) + ' |') |
| 59 |
|
| 60 |
return '\n'.join([header_row, separator_row] + rows) |
| 61 |
|
| 62 |
def main(): |
| 63 |
if len(sys.argv) < 3 or len(sys.argv) % 2 != 1: |
| 64 |
print("Usage: script.py <name1> <file1> <name2> <file2> ...") |
| 65 |
sys.exit(1) |
| 66 |
|
| 67 |
files_modules = OrderedDict() |
| 68 |
column_names = {} |
| 69 |
|
| 70 |
for i in range(1, len(sys.argv), 2): |
| 71 |
column_name = sys.argv[i] |
| 72 |
file_path = sys.argv[i + 1] |
| 73 |
files_modules[file_path] = extract_module_names(file_path) |
| 74 |
column_names[file_path] = column_name |
| 75 |
|
| 76 |
|
| 77 |
apicula_primitives = analyze_used_primitives('examples/himbaechel/*-synth.json') |
| 78 |
|
| 79 |
table = generate_markdown_table(files_modules, column_names, apicula_primitives) |
| 80 |
print(table) |
| 81 |
|
| 82 |
if __name__ == "__main__": |
| 83 |
main() |