-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsv.py
More file actions
executable file
·676 lines (516 loc) · 18 KB
/
Copy pathsv.py
File metadata and controls
executable file
·676 lines (516 loc) · 18 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#!/usr/bin/env python3.12
from argparse import ArgumentParser, Namespace
from bdb import BdbQuit
from glob import glob
from io import TextIOWrapper
from json import dump, dumps
from os import PathLike, chdir, close
from pathlib import Path
from re import compile
from shutil import which
from subprocess import PIPE, Popen, run
from sys import stdout
from tempfile import mkstemp
from traceback import print_exception
from types import TracebackType
from typing import Iterable, Optional, Type, cast
import sys
try:
from argcomplete import autocomplete # type: ignore
except:
def autocomplete(_: ArgumentParser):
...
type StrOrBytesPath = str | bytes | PathLike[str] | PathLike[bytes]
LINE_RE = compile(r"^(.*?)((?:\s+\\)*)$")
BACKSLASH_RE = compile(r"\\")
BACKSLASH_STR = " \\"
DOUBLE_NEWLINE_RE = compile(r"\n(?:\s*?\n)+")
DOUBLE_NEWLINE_STR = "\n\n"
MODULE_PATH_RE = compile(r"(?:/|^)[A-Z][^/]+\.sv$")
ARG_MAX = 4096
class FormatterNode(list["FormatterNode | str"]):
@staticmethod
def _format(
child: "FormatterNode | str", custom_width: Optional[int] = None
) -> list[str]:
if type(child) is str:
return [child]
assert type(child) is FormatterNode
lines = [j for i in child for j in FormatterNode._format(i, custom_width)]
if custom_width is None:
width = max([len(i) for i in lines])
lines = [
j
for i in child
for j in FormatterNode._format(i, width - len(BACKSLASH_STR))
]
else:
width = custom_width
return [i + " " * (width - len(i)) + BACKSLASH_STR for i in lines]
def format(self):
return "".join([f"{j}\n" for i in self for j in FormatterNode._format(i)])
class FormatterTree:
def __init__(self) -> None:
self.root = FormatterNode()
self.stack = [self.root]
def descend(self):
node = FormatterNode()
self.stack[-1].append(node)
self.stack.append(node)
def ascend(self):
self.stack.pop()
def to(self, depth: int):
assert depth >= 0
old_len = len(self.stack)
new_len = depth + 1
if old_len < new_len:
for _ in range(old_len, new_len):
self.descend()
else:
for _ in range(old_len, new_len, -1):
self.ascend()
def insert(self, value: str):
self.stack[-1].append(value)
def format(source: str):
tree = FormatterTree()
for i in source.splitlines():
match = LINE_RE.match(i)
assert match is not None
tree.to(len(BACKSLASH_RE.findall(match.group(2))))
tree.insert(match.group(1))
return DOUBLE_NEWLINE_RE.sub(DOUBLE_NEWLINE_STR, tree.root.format())
def run_iverilog(
file: Path,
*,
output: Optional[Path] = None,
output_format: Optional[str] = None,
target_flags: list[str] = [],
preprocess_only: bool = False,
input: Optional[Path] = None,
):
args: list[StrOrBytesPath] = [
"iverilog",
"-g2012",
"-Wall",
"-Wno-timescale", # Timescale is ALWAYS inherited from tap.sv
"-I",
".",
file.name,
]
if input:
args.append(f"-DINPUT={dumps(input.absolute().as_posix())}")
else:
args += [
"-L",
Path("vpi").absolute().as_posix(),
"-m",
"io",
]
if output is not None:
args += ["-o", output.absolute().as_posix()]
if output_format:
args.append(f"-t{output_format}")
if preprocess_only:
args.append("-E")
for i in target_flags:
args.append(f"-p{i}")
return run(args, cwd=file.parent, check=True)
def run_vvp(file: Path):
args: list[StrOrBytesPath] = [file]
return run(args, cwd=file.parent, check=True)
def run_vscode(
*files: Path,
wait: bool = False,
):
args: list[StrOrBytesPath] = [
"code",
"-r",
*[i.absolute().as_posix() for i in files],
]
if wait:
args.append("-w")
return run(args, check=True)
def run_batch(args: list[str], *rest_: str, check: bool = True):
args_len = sum([len(i.encode()) + 1 for i in args])
rest = list(rest_)
while rest:
batch_args = args
remaining = ARG_MAX - args_len
while rest and remaining > 0:
arg = rest.pop()
batch_args.append(arg)
remaining -= len(arg.encode()) + 1
run(args, check=check)
def run_verible_formatter(*files: Path):
files_str = [i.absolute().as_posix() for i in files]
args = ["verible-verilog-format", "--inplace", "--column_limit", "80"]
run_batch(args, *files_str, check=True)
def run_verible_syntax(*files: Path):
files_str = [i.absolute().as_posix() for i in files]
args = ["verible-verilog-syntax"]
run_batch(args, *files_str, check=True)
def run_verilator(file: Path, *, no_multidriven: bool = False):
args = [
"verilator",
"--lint-only",
"-Wall",
"-Wpedantic",
"-Wno-PINCONNECTEMPTY", # Better than simply not writing the pin.
"-Wno-VARHIDDEN", # Mostly edge cases. Low priority.
"-Wno-DECLFILENAME", # Usually, the input to verilator is preprocessed.
"+1800-2012ext+sv",
"--timing",
"--top-module",
file.stem.split(".", 1)[0],
file.name,
]
if no_multidriven:
args.append("-Wno-MULTIDRIVEN")
run(args, cwd=file.parent, check=True)
def run_vvp_streaming(file: Path):
proc = Popen(file, stdout=PIPE)
assert proc.stdout
for line in TextIOWrapper(proc.stdout, encoding="utf-8"):
yield line.removesuffix("\n")
assert proc.wait() == 0
def run_curl(
url: str,
*,
method: str = "",
headers: dict[str, str] = {},
input: Optional[bytes] = None,
):
args: list[StrOrBytesPath] = ["curl"]
if method:
args += ["-X", method]
for k, v in headers.items():
args += ["-H", f"{k}: {v}"]
if input is not None:
args += ["--data-binary", "@-"]
args.append(url)
return run(args, input=input, check=True)
def run_xdg_open(url_or_path: StrOrBytesPath):
return run(["xdg-open", url_or_path], check=True)
def run_make(*targets: str):
args: list[StrOrBytesPath] = ["make"]
for i in targets:
args.append(i)
return run(args, cwd=Path("vpi"), check=True)
def run_pnpm(*args: str):
return run(["corepack", "pnpm", *args], cwd=Path("ckl"), check=True)
def run_gcc(*args: str):
return run(["gcc", *args], check=True)
def run_node(program: StrOrBytesPath, *args: str):
return run(["node", program, *args], check=True)
def formatFile(file: Path):
with open(file, "r") as f:
content = f.read()
content = format(content)
if file.name.endswith(".asm.sv"):
content = "".join([i.strip() + "\n" for i in content.splitlines()])
content = "".join(
[
(
" "
if i.startswith("`ASM_")
and not i.startswith("`ASM_SUB")
and not i.startswith("`ASM_LABEL")
else ""
)
+ i
+ "\n"
for i in content.splitlines()
]
)
with open(file, "w") as f:
f.write(content)
def mktemp(file: Optional[Path], suffix: str):
temp = mkstemp(prefix=f"{file.stem}." if file else "", suffix=suffix)
close(temp[0])
return Path(temp[1])
class TestFile:
def __init__(self, file: Path) -> None:
self.failed = False
self.path = file
def is_failed(self):
return self.failed
def run(self, temp_file: Path):
run_iverilog(self.path, output=temp_file)
generator = run_vvp_streaming(temp_file)
for line in generator:
if line.startswith("not ok"):
self.failed = True
yield line
class TestDirectory:
def __init__(self, dir: Path, included_files: list[Path]) -> None:
self.path = dir
self.files = [Path(dir, i) for i in glob("*", root_dir=dir)]
self.subtests: list[TestDirectory | TestFile] = []
for i in self.files:
if i.is_dir():
self.subtests.append(TestDirectory(i, included_files))
elif MODULE_PATH_RE.search(str(i)) and i.absolute() in included_files:
self.subtests.append(TestFile(i))
def is_failed(self) -> bool:
return any([i.is_failed() for i in self.subtests])
def run(self, temp_file: Path) -> Iterable[str]:
yield "TAP version 14"
for i in self.subtests:
yield f"# Subtest: {i.path}"
for j in i.run(temp_file):
if not j.startswith("TAP version "):
yield " " + j
if i.is_failed():
yield f"not ok - {i.path}"
else:
yield f"ok - {i.path}"
yield f"1..{len(self.subtests)}"
class ExceptionAggregator:
def __init__(self) -> None:
self.errors: list[Exception] = []
def __enter__(self) -> None:
pass
def __exit__(
self,
exc_type: Optional[Type[Exception]],
exc_val: Optional[Exception],
exc_tb: Optional[TracebackType],
) -> bool:
if (
exc_type is BdbQuit
or exc_type is SystemExit
or exc_type is KeyboardInterrupt
):
return False
if exc_val is not None:
self.errors.append(exc_val)
print_exception(exc_val)
return True
def assert_no_errors(self):
assert not self.errors
class Action:
CKL = "ckl"
COMPILE = "compile"
FORMAT = "format"
LINT = "lint"
MAKE = "make"
PREPROCESS = "preprocess"
REMAINING_TESTS = "remaining-tests"
RUN = "run"
SYNTHESIZE = "synthesize"
TEST = "test"
class Args(Namespace):
target_flags: Optional[list[str]]
action: Optional[str]
file: Optional[str]
files: Optional[list[str]]
input: Optional[str]
no_iverilog: Optional[bool]
no_verible: Optional[bool]
no_verilator: Optional[bool]
online: Optional[str]
vscode: Optional[bool]
out: Optional[str]
reporter: Optional[str]
sub_action: Optional[str]
type: Optional[str]
wait: Optional[bool]
def parse_args():
parser = ArgumentParser()
subparsers = parser.add_subparsers(dest="action", required=True)
ckl = subparsers.add_parser(Action.CKL)
compile = subparsers.add_parser(Action.COMPILE)
format = subparsers.add_parser(Action.FORMAT)
lint = subparsers.add_parser(Action.LINT)
make = subparsers.add_parser(Action.MAKE)
preprocess = subparsers.add_parser(Action.PREPROCESS)
run = subparsers.add_parser(Action.RUN)
synthesize = subparsers.add_parser(Action.SYNTHESIZE)
test = subparsers.add_parser(Action.TEST)
subparsers.add_parser(Action.REMAINING_TESTS)
for i in {test, lint, format}:
i.add_argument("files", nargs="*")
for i in {synthesize, run, compile, preprocess, ckl}:
i.add_argument("file")
for i in {compile, preprocess, ckl}:
i.add_argument("-o", "--out", required=True)
run.add_argument("--input")
compile.add_argument("-t", "--type")
compile.add_argument("-p", "--target-flag", nargs="*", dest="target_flags")
test.add_argument("-r", "--reporter")
lint.add_argument("--no-verilator", action="store_true", dest="no_verilator")
lint.add_argument("--no-iverilog", action="store_true", dest="no_iverilog")
lint.add_argument("--no-verible", action="store_true", dest="no_verible")
make.add_argument("files", metavar="targets", nargs="*")
synthesize.add_argument("-o", "--online")
synthesize.add_argument("-v", "--vscode", action="store_true")
synthesize.add_argument("-w", "--wait", action="store_true")
autocomplete(parser)
return cast(Args, parser.parse_args())
def with_suffix(path: str, ext: str):
return Path(path).with_suffix("").with_suffix(ext).absolute().as_posix()
def fix_args(args: Args):
while True:
match args.action:
case Action.FORMAT | Action.LINT if not args.files:
args.files = glob("**/*.sv", recursive=True)
case Action.TEST if not args.files:
args.files = glob("test/**/*.sv", recursive=True)
case _:
break
sys.tracebacklimit = 0
chdir(Path(__file__).parent)
args = parse_args()
fix_args(args)
match args.action:
case Action.SYNTHESIZE:
assert args.file
file = Path(args.file)
temp_sv = mktemp(file, ".sv")
run_iverilog(file, output=temp_sv, preprocess_only=True)
run_verible_formatter(temp_sv)
formatFile(temp_sv)
if args.online:
with open(temp_sv, "r") as f:
content = f.read()
print("> curl")
run_curl(
f"{args.online}/api/data",
method="PUT",
headers={"Content-Type": "application/json"},
input=dumps({"": content}).encode(),
)
temp_sv.unlink()
print("\n> xdg-open")
run_xdg_open(f"{args.online}?data={file.stem}")
elif args.vscode:
json = {
"sources": [
{"relpath": temp_sv.name},
],
"options": {
"opt": True,
"transform": True,
"fsm": "yes",
"fsmexpand": True,
},
"devices": {},
"connectors": [],
"subcircuits": {},
}
temp_digitaljs = mktemp(file, ".digitaljs")
with open(temp_digitaljs, "w") as f:
dump(json, f, separators=(",", ":"))
if args.wait:
run_vscode(temp_digitaljs, wait=True)
temp_sv.unlink()
temp_digitaljs.unlink()
else:
run_vscode(temp_digitaljs)
else:
print("Synthesized at:", temp_sv)
case Action.RUN:
assert args.file
file = Path(args.file)
temp_sv = mktemp(file, "")
input = Path(args.input) if args.input else None
run_iverilog(file, output=temp_sv, input=input)
run_vvp(temp_sv)
temp_sv.unlink()
case Action.COMPILE:
assert args.file
assert args.out
run_iverilog(
Path(args.file),
output=Path(args.out),
output_format=args.type,
target_flags=args.target_flags or [],
)
case Action.PREPROCESS:
assert args.file
assert args.out
out = Path(args.out)
run_iverilog(Path(args.file), output=out, preprocess_only=True)
run_verible_formatter(out)
formatFile(out)
case Action.FORMAT:
assert args.files
files = [Path(i) for i in args.files]
exc_aggregator = ExceptionAggregator()
with exc_aggregator:
run_verible_formatter(*files)
for file in files:
print(">", file)
with exc_aggregator:
formatFile(file)
exc_aggregator.assert_no_errors()
case Action.LINT:
assert args.files
files = [Path(i) for i in args.files if MODULE_PATH_RE.search(i)]
exc_aggregator = ExceptionAggregator()
if not args.no_verible:
with exc_aggregator:
print("> verible")
run_verible_syntax(*files)
for file in files:
if not args.no_verilator:
print("> verilator", file)
with exc_aggregator:
temp_sv = mktemp(file, ".sv")
try:
run_iverilog(file, output=temp_sv, preprocess_only=True)
with open(temp_sv, "r") as f:
content = f.read()
has_wor = " wor " in content
content = content.replace("$read_char(", "(0")
content = content.replace(" wor ", " tri ")
with open(temp_sv, "w") as f:
f.write(content)
run_verilator(temp_sv, no_multidriven=has_wor)
finally:
temp_sv.unlink()
if not args.no_iverilog:
print("> iverilog", file)
with exc_aggregator:
run_iverilog(file, output_format="null")
exc_aggregator.assert_no_errors()
case Action.TEST:
assert args.files
tests = TestDirectory(Path("test"), [Path(i).absolute() for i in args.files])
process = None
out = None
if stdout.isatty():
if args.reporter:
process = Popen(args.reporter, stdin=PIPE, text=True)
out = process.stdin
elif which("tap-mocha-reporter"):
process = Popen(["tap-mocha-reporter", "dot"], stdin=PIPE, text=True)
out = process.stdin
for file in tests.run(mktemp(None, ".vvp")):
print(file, file=out, flush=True)
if out:
out.close()
if process:
process.wait()
assert not tests.is_failed()
case Action.REMAINING_TESTS:
tests = glob("test/**/*Test.sv", recursive=True)
tested_files = {
"src/" + i.removeprefix("test/").removesuffix("Test.sv") + ".sv"
for i in tests
}
all_files = glob("src/**/*.sv", recursive=True)
for file in all_files:
if MODULE_PATH_RE.search(file) and file not in tested_files:
print(">", file)
case Action.MAKE:
run_make(*args.files or [])
if not args.files:
run_pnpm("install")
case Action.CKL:
assert args.file
assert args.out
run_gcc("-E", "-ffreestanding", "-o", args.out, "-x", "c", args.file)
run_node("ckl/index.js", args.out, args.out)
case _:
raise ValueError(f"Unknown action: {args.action}")