Skip to content

Commit f0c3d96

Browse files
authored
Merge pull request #338 from slothy-optimizer/multi-line-comments
Add support for single-line and multi-line /* */ comments
2 parents 1de3195 + c6dcec1 commit f0c3d96

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

slothy/helper.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737

3838
from unicorn import Uc, UcError
3939

40+
# Markers for /* */ comment preservation
41+
_BLOCK_COMMENT_MARKER = "__SLOTHY_BLOCK__"
42+
_NEWLINE_MARKER = "__SLOTHY_NL__"
43+
4044

4145
class SourceLine:
4246
"""Representation of a single line of source code"""
@@ -232,7 +236,21 @@ def to_string(self, indentation=True, comments=True, tags=True):
232236
additional = []
233237

234238
if comments is True:
235-
additional += list(map(lambda s: f"// {s}", double_comments))
239+
# Convert comments with markers back to /* */ format
240+
def format_comment(s):
241+
if _NEWLINE_MARKER in s:
242+
# Restore as multi-line /* */ comment
243+
lines = s.split(_NEWLINE_MARKER)
244+
lines = [line.strip() for line in lines]
245+
return "/* " + "\n ".join(lines) + " */"
246+
elif s.startswith(_BLOCK_COMMENT_MARKER):
247+
# Restore as single-line /* */ comment
248+
return f"/* {s[len(_BLOCK_COMMENT_MARKER):]} */"
249+
else:
250+
# Regular // comment - keep as is
251+
return f"// {s}"
252+
253+
additional += list(map(format_comment, double_comments))
236254
additional += list(map(lambda s: f"///{s}", triple_comments))
237255

238256
if tags is True:
@@ -346,6 +364,21 @@ def read_multiline(s, reduce=True):
346364
if isinstance(s, str):
347365
# Retain newline termination
348366
terminated_by_newline = len(s) > 0 and s[-1] == "\n"
367+
368+
# Convert /* */ comments to // style with markers for restoration
369+
def replace_comment(match):
370+
content = match.group(1)
371+
if "\n" in content:
372+
# Multi-line /* */ comment - mark newlines
373+
content = content.replace("\n", f" {_NEWLINE_MARKER} ")
374+
content = " ".join(content.split())
375+
return f"// {content}" if content else ""
376+
else:
377+
# Single-line /* */ comment - mark with prefix
378+
content = content.strip()
379+
return f"// {_BLOCK_COMMENT_MARKER}{content}" if content else ""
380+
381+
s = re.sub(r"/\*(.*?)\*/", replace_comment, s, flags=re.DOTALL)
349382
s = s.splitlines()
350383
if terminated_by_newline:
351384
s.append("")

tests/naive/aarch64/_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,21 @@ def core(self, slothy):
287287
slothy.fusion_region(start="start", end="end", ssa=False)
288288

289289

290+
class AArch64CStyleComments(OptimizationRunner):
291+
def __init__(self, var="", arch=AArch64_Neon, target=Target_CortexA55):
292+
name = "aarch64_cstyle_comments"
293+
infile = name
294+
295+
super().__init__(
296+
infile, name, rename=True, arch=arch, target=target, base_dir="tests"
297+
)
298+
299+
def core(self, slothy):
300+
slothy.config.variable_size = True
301+
slothy.config.constraints.stalls_first_attempt = 32
302+
slothy.optimize(start="start", end="end")
303+
304+
290305
test_instances = [
291306
Instructions(),
292307
Instructions(target=Target_CortexA72),
@@ -309,4 +324,5 @@ def core(self, slothy):
309324
AArch64LoopLabels(),
310325
AArch64LoopBranch(),
311326
AArch64FusionVeor(),
327+
AArch64CStyleComments(),
312328
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Comment before optimization region */
2+
// Another comment before
3+
ldr q0, [x1, #0] /* This should be preserved */
4+
5+
start:
6+
/* Single line C-style comment */
7+
ldr q1, [x2, #0] /* inline comment */
8+
9+
/* Multi-line with
10+
// what looks like a comment
11+
but should be ignored */
12+
ldr q8, [x0]
13+
ldr q9, [x0, #1*16]
14+
ldr q10, [x0, #2*16]
15+
ldr q11, [x0, #3*16]
16+
17+
mul v24.8h, v9.8h, v0.h[0] /* first */ /* second */ // third
18+
sqrdmulh v9.8h, v9.8h, v0.h[1]
19+
mls v24.8h, v9.8h, v1.h[0]
20+
sub v9.8h, v8.8h, v24.8h
21+
add v8.8h, v8.8h, v24.8h
22+
23+
/**
24+
* Javadoc style before stores
25+
*/
26+
str q8, [x0], #4*16
27+
str q9, [x0, #-3*16] /* store with offset */
28+
str q10, [x0, #-2*16]
29+
str q11, [x0, #-1*16]
30+
end:
31+
32+
/* Comment after optimization region */
33+
// Another comment after
34+
str q0, [x1] /* This should also be preserved */

0 commit comments

Comments
 (0)