-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStage_3_CRedPython.py
More file actions
1235 lines (1010 loc) · 45.9 KB
/
Stage_3_CRedPython.py
File metadata and controls
1235 lines (1010 loc) · 45.9 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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Stage 3b: Token-level reducer — optimised C-Reduce replacement.
OPTIMISED VERSION with TCC fast-reject guard, global caches, syntax validator, and more efficient passes.
Key optimisations:
1. TCC fast-reject for syntactically safe passes (saves IDO calls)
2. Global CompileCache per file (across all passes)
3. Central syntax validator BEFORE compiler call
4. Include normalisation once per file
5. count('\n') instead of splitlines() for line counting
6. pass_peep_subexpr restricted to complex expressions
7. _fast_paren_cleanup in a single pass instead of iteratively
"""
import os, sys, re, subprocess, signal, tempfile, shutil, hashlib
import json, time, argparse, multiprocessing, resource
from datetime import datetime
from tqdm import tqdm
BASE_DIR = "/home/lukas/code_generator/deadCodeRemover"
PROJECT_ROOT = os.path.join(BASE_DIR, "IDO_Compiler")
IDO_DIR = os.path.abspath(os.path.join(PROJECT_ROOT, "tools", "ido"))
IDO_CC = os.path.join(IDO_DIR, "cc")
DATASET_DIR = os.path.join(BASE_DIR, "dataset")
INPUT_DIR = os.path.join(DATASET_DIR, "Stage_3_IN")
OUTPUT_DIR = os.path.join(DATASET_DIR, "Stage_3_OUT")
HEADERS_DIR = os.path.join(DATASET_DIR, "Stage_0_headers")
OBJDUMP = "mips-linux-gnu-objdump"
TMP_ROOT = "/dev/shm"
GROUPS = [
"Input_Group",
]
INCLUDE_DIRS = [
os.path.join(PROJECT_ROOT, "include"),
os.path.join(PROJECT_ROOT, "src"),
os.path.join(PROJECT_ROOT, "include", "PR"),
os.path.join(PROJECT_ROOT, "lib", "ultralib", "include"),
os.path.join(BASE_DIR, "csmith_install/include/csmith-2.3.0"),
]
_asm_cache: dict = {}
_peep_local_fail_cache = set()
MAX_PASSES = 10
# =====================================================================
# TCC FAST-REJECT GUARD
# =====================================================================
TCC_SAFE_PASSES = {
"blank", "blank_final",
"balanced_parens_only", "balanced_curly_empty",
"balanced_parens_zero", "balanced_remove_curly",
"balanced_remove_parens", "balanced_remove_square",
"ternary_modus_b", "ternary_modus_c",
"peep_while", "regex_atoms",
}
def _tcc_available() -> bool:
"""Checks whether tcc is available in PATH."""
return shutil.which("tcc") is not None
def tcc_syntax_check(c_source: str, tmp_dir: str, header_dir: str) -> bool:
"""
Fast syntax check using TCC.
Returns True if TCC passes without errors.
Uses subprocess.call with DEVNULL to avoid OS pipe overhead.
Timeout: 5 seconds (TCC is extremely fast).
"""
c_path = os.path.join(tmp_dir, "_tcc.c")
try:
with open(c_path, "w") as f:
f.write(c_source)
cmd = ["tcc", "-fsyntax-only", "-c", "-xc", "-D_LANGUAGE_C", "-D_MIPS_SZLONG=32"]
for inc in INCLUDE_DIRS:
cmd += ["-I", inc]
if header_dir:
cmd += ["-I", header_dir]
cmd += [c_path]
# Direct subprocess.call with DEVNULL eliminates OS pipe overhead
rc = subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)
return rc == 0
except subprocess.TimeoutExpired:
return False
except Exception:
return False
finally:
try:
if os.path.exists(c_path):
os.unlink(c_path)
except OSError:
pass
# =====================================================================
# GLOBAL COMPILE CACHE (with TCC integration)
# =====================================================================
class CompileCache:
"""
Central cache for compilation results per file.
Stores both successful and failed hashes.
Integrates TCC fast-reject for syntactically safe passes.
"""
def __init__(self, tcc_baseline_ok: bool = False):
self._success = {} # key -> hash
self._rejected = set() # key -> True (IDO failed)
self._tcc_rejected = set() # key -> True (TCC failed)
self._tcc_baseline_ok = tcc_baseline_ok and _tcc_available()
# Statistics counters
self.stats = {
"cache_hits": 0,
"tcc_checks": 0,
"tcc_rejects": 0,
"ido_checks": 0,
"ido_rejects": 0,
"ido_success": 0
}
def try_candidate(self, candidate: str, tmp_dir: str, header_dir: str,
baseline: str, pass_name: str = "") -> tuple[str | None, str]:
"""
Tries to compile a candidate.
Returns (hash, "") on success,
(None, reason) if rejected or failed.
"""
if not candidate or not candidate.strip():
return None, "empty"
key = hashlib.md5(candidate.encode()).hexdigest()
# 1. Check IDO cache
if key in self._rejected:
self.stats["cache_hits"] += 1
return None, "rejected"
if key in self._success:
self.stats["cache_hits"] += 1
return self._success[key], ""
# 2. TCC fast-reject (only for safe passes & when baseline is ok)
if self._tcc_baseline_ok and pass_name in TCC_SAFE_PASSES:
if key in self._tcc_rejected:
self.stats["cache_hits"] += 1
return None, "tcc_rejected"
self.stats["tcc_checks"] += 1
if not tcc_syntax_check(candidate, tmp_dir, header_dir):
self._tcc_rejected.add(key)
self.stats["tcc_rejects"] += 1
return None, "tcc_fail"
# 3. IDO gold-standard check
self.stats["ido_checks"] += 1
h, err = compile_to_hash(candidate, tmp_dir, header_dir)
if h is None:
self._rejected.add(key)
self.stats["ido_rejects"] += 1
return None, err
self._success[key] = h
self.stats["ido_success"] += 1
return h, ""
def is_rejected(self, candidate: str) -> bool:
key = hashlib.md5(candidate.encode()).hexdigest()
return key in self._rejected or key in self._tcc_rejected
# =====================================================================
# INFRASTRUCTURE
# =====================================================================
def run_cmd_safely(cmd: list[str], cwd: str | None = None, env: dict | None = None, timeout: int = 30):
try:
proc = subprocess.Popen(cmd, cwd=cwd, env=env,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
stdout, stderr = proc.communicate(timeout=timeout)
return proc.returncode, stdout, stderr
except subprocess.TimeoutExpired:
try: os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
except (ProcessLookupError, OSError): pass
try: proc.communicate(timeout=5)
except: proc.kill()
raise
def compile_to_hash(c_source: str, tmp_dir: str, header_dir: str) -> tuple[str | None, str]:
"""
Compiles C source via gcc -E and IDO to a normalised ASM hash.
"""
fixed = c_source
cache_key = hashlib.md5((fixed + (header_dir or "")).encode()).hexdigest()
if cache_key in _asm_cache:
return _asm_cache[cache_key], ""
c_path = os.path.join(tmp_dir, "_tr.c")
i_path = os.path.join(tmp_dir, "_tr.i")
o_path = os.path.join(tmp_dir, "_tr.o")
def _rm(p):
try:
if os.path.exists(p): os.unlink(p)
except OSError: pass
def _cleanup():
for p in [c_path, i_path, o_path]: _rm(p)
try:
with open(c_path, "w") as f: f.write(fixed)
cmd = ["gcc", "-E", "-P", "-xc", "-D_LANGUAGE_C", "-D_MIPS_SZLONG=32"]
for inc in INCLUDE_DIRS: cmd += ["-I", inc]
if header_dir: cmd += ["-I", header_dir]
cmd += [c_path, "-o", i_path]
try: rc, _, _ = run_cmd_safely(cmd, timeout=30)
except subprocess.TimeoutExpired: _cleanup(); return None, "gcc timeout"
_rm(c_path)
if rc != 0: _cleanup(); return None, "gcc fail"
cmd = [IDO_CC, "-c", "-O2", "-mips2", "-G", "0", "-w", i_path, "-o", o_path]
try: rc, _, _ = run_cmd_safely(cmd, cwd=tmp_dir, env=_IDO_ENV, timeout=30)
except subprocess.TimeoutExpired: _cleanup(); return None, "IDO timeout"
_rm(i_path)
if rc != 0: _cleanup(); return None, "IDO fail"
# objdump -d: disassemble .text (instructions)
cmd = [OBJDUMP, "-d", "-z", o_path]
try: rc, stdout_text, _ = run_cmd_safely(cmd, timeout=30)
except subprocess.TimeoutExpired: _cleanup(); return None, "objdump timeout"
if rc != 0: _rm(o_path); _cleanup(); return None, "objdump fail"
# objdump -s: raw data from .rodata, .data, .bss (string constants etc.)
cmd = [OBJDUMP, "-s", "-j", ".rodata", "-j", ".data", "-j", ".bss", o_path]
try: rc2, stdout_data, _ = run_cmd_safely(cmd, timeout=30)
except subprocess.TimeoutExpired: stdout_data = b""
_rm(o_path)
asm = []
# 1. Normalise instructions from .text
for line in stdout_text.decode(errors="replace").splitlines():
m = re.match(r'^\s*[0-9a-fA-F]+:\s+[0-9a-fA-F]+\s+(.*)', line)
if not m: continue
s = m.group(1).strip().split('#')[0].strip()
if not s: continue
s = re.sub(r'addiu\s+\$?sp,\s*\$?sp,\s*-?\d+', 'addiu sp,sp,OFFSET', s)
s = re.sub(r'-?\d+\(\$?sp\)', 'OFFSET(sp)', s)
s = re.sub(r'-?\d+\(\$?fp\)', 'OFFSET(fp)', s)
s = re.sub(r'%[a-z0-9_.]+\([^)]*\)', 'SYMBOL', s)
asm.append(s)
# 2. Append raw data from .rodata/.data/.bss (hex dump)
if stdout_data:
for line in stdout_data.decode(errors="replace").splitlines():
# objdump -s format: " 0000 41424344 45464748 ..."
m = re.match(r'^\s*[0-9a-fA-F]+\s+((?:[0-9a-fA-F]+\s*)+)', line)
if m:
asm.append("DATA:" + m.group(1).strip())
if not asm: _cleanup(); return None, "no asm"
h = hashlib.md5("\n".join(asm).encode()).hexdigest()
_asm_cache[cache_key] = h
return h, ""
except Exception as e:
_cleanup(); return None, str(e)
def _get_ido_env():
env = os.environ.copy()
env["COMPILER_PATH"] = IDO_DIR
env["LD_LIBRARY_PATH"] = f"{IDO_DIR}:{env.get('LD_LIBRARY_PATH', '')}"
return env
_IDO_ENV = _get_ido_env()
def _get_header_dir(path: str):
for g in GROUPS:
if g in path: return os.path.join(HEADERS_DIR, f"{g}_headers")
return ""
def check_disk_space(min_free_gb: int = 2):
while True:
if shutil.disk_usage("/").free / (1024**3) >= min_free_gb: return
time.sleep(30)
# =====================================================================
# HELPER FUNCTIONS
# =====================================================================
def _find_matching(s: str, open_pos: int, open_char: str, close_char: str) -> int:
"""Finds the closing bracket matching open_pos. Returns -1 if not found."""
depth = 0
for i in range(open_pos, len(s)):
if s[i] == open_char:
depth += 1
elif s[i] == close_char:
depth -= 1
if depth == 0:
return i
return -1
def _find_all_balanced(src: str, open_c: str, close_c: str) -> list[tuple[int, int]]:
stack = []
pairs = []
for i, c in enumerate(src):
if c == open_c:
stack.append(i)
elif c == close_c:
if stack:
start = stack.pop()
pairs.append((start, i))
return pairs
def _split_args(args_str: str) -> list[str]:
args = []
depth = 0
in_string = False
current = []
for ch in args_str:
if ch == '"': in_string = not in_string
if not in_string:
if ch in '([{': depth += 1
elif ch in ')]}': depth -= 1
if ch == ',' and depth == 0 and not in_string:
args.append(''.join(current).strip())
current = []
else:
current.append(ch)
if current or args:
args.append(''.join(current).strip())
return [a for a in args if a != '']
# Optimised version — single pass instead of iterative
def _fast_paren_cleanup(src: str) -> str:
"""
Removes fully redundant wrappings such as ((x)) -> (x).
Optimised: single pass with mapping instead of iterative reconstruction.
"""
pairs = _find_all_balanced(src, '(', ')')
if not pairs:
return src
# Mapping: start -> end for fast lookup
end_map = {s: e for s, e in pairs}
to_remove = set()
for start, end in pairs:
# Find the first character after the opening bracket
inner_start = start + 1
while inner_start < end and src[inner_start].isspace():
inner_start += 1
# Find the last character before the closing bracket
inner_end = end - 1
while inner_end > start and src[inner_end].isspace():
inner_end -= 1
# Check: is the exact content of this pair simply
# another complete bracket pair?
if inner_start in end_map and end_map[inner_start] == inner_end:
# The outer pair is structurally redundant
to_remove.add(start)
to_remove.add(end)
if not to_remove:
return src
# Rebuild string in one pass
return "".join(c for i, c in enumerate(src) if i not in to_remove)
# =====================================================================
# PYTHON PASSES (with pass_name for TCC guard)
# =====================================================================
def pass_blank(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "blank") -> tuple[str, bool]:
"""
Normalises whitespace:
- Preserves indentation (tabs).
- Brackets are kept tight: (x).
- Ternary operators (? and :) get exactly one space for readability.
"""
changed = False
lines = src.splitlines()
new_lines = []
for line in lines:
if not line.strip():
new_lines.append("")
continue
# 1. Preserve indentation
match = re.match(r'^(\s*)', line)
indent = match.group(1) if match else ""
content = line[len(indent):].strip()
# 2. Collapse all whitespace to single spaces
content = re.sub(r'\s{2,}', ' ', content)
# 3. Re-space ternary operators: " ? " and " : "
content = re.sub(r'\s*([?:])\s*', r' \1 ', content)
# 4. Tighten brackets (no spaces inside)
content = re.sub(r'\(\s+', '(', content)
content = re.sub(r'\s+\)', ')', content)
# 5. Commas and semicolons: one space to the right, none to the left
content = re.sub(r'\s*([,;])\s*', r'\1 ', content)
# 6. Final cleanup of double spaces
content = re.sub(r'\s{2,}', ' ', content).strip()
new_lines.append(indent + content)
candidate = "\n".join(new_lines)
if candidate != src:
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
# Vertical pass: empty lines and preprocessor noise
lines = src.splitlines()
no_pre = [l for l in lines if not l.strip().startswith('#')]
if len(no_pre) < len(lines):
candidate = "\n".join(no_pre)
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
return src, changed
def pass_includes(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "includes") -> tuple[str, bool]:
"""
Removes #include lines individually or all at once.
Not in TCC_SAFE_PASSES because TCC and IDO use different header ecosystems.
"""
lines = src.splitlines(keepends=True)
include_indices = [i for i, l in enumerate(lines) if re.match(r'^\s*#\s*include', l)]
if not include_indices:
return src, False
changed = False
# Optional "nuclear strike": try removing all includes at once first
candidate_all = "".join([l for i, l in enumerate(lines) if i not in include_indices])
h_all, _ = cache.try_candidate(candidate_all, tmp_dir, header_dir, baseline, pass_name)
if h_all == baseline:
return candidate_all, True
# Individual check
for idx in reversed(include_indices):
candidate = "".join(lines[:idx] + lines[idx+1:])
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
lines.pop(idx)
changed = True
return "".join(lines), changed
def pass_lines(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "lines") -> tuple[str, bool]:
"""
Removes line chunks via binary search.
Not in TCC_SAFE_PASSES because removing forward declarations
can produce false negatives with the stricter TCC (IDO tolerates K&R).
"""
# 1. Pre-clean: remove all genuinely empty lines
lines = [l for l in src.splitlines(keepends=True) if l.strip()]
n = len(lines)
if n == 0: return src, False
chunk = n
changed = False
while chunk >= 1:
i = n
made_progress = False
while i > 0:
start = max(0, i - chunk)
candidate_lines = lines[:start] + lines[i:]
candidate = "".join(candidate_lines)
if not candidate.strip():
i -= chunk
continue
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
lines = candidate_lines
n = len(lines)
changed = True
made_progress = True
else:
i -= chunk
if not made_progress:
chunk //= 2
return "".join(lines), changed
def pass_balanced(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, mode="parens-only",
pass_name: str = "balanced") -> tuple[str, bool]:
"""
Removes or modifies balanced bracket pairs.
Supports round, curly, and square brackets.
"""
changed = False
fail_cache = set()
delimiters = {
"curly": ('{', '}'),
"parens": ('(', ')'),
"square": ('[', ']')
}
d_type = "curly" if "curly" in mode else ("square" if "square" in mode else "parens")
open_c, close_c = delimiters[d_type]
while True:
# 1. Fast path for redundant brackets
if d_type == "parens":
cleaned_src = _fast_paren_cleanup(src)
if cleaned_src != src:
src = cleaned_src
changed = True
pairs = _find_all_balanced(src, open_c, close_c)
pairs.sort(key=lambda x: x[1] - x[0])
made_progress = False
for start, end in pairs:
inner = src[start+1:end]
# --- FILTER ---
if (mode in ("parens-only", "curly-only", "square-only")) and d_type == "parens":
if start > 0 and (src[start-1].isalnum() or src[start-1] == '_'):
continue
# --- CACHE CHECK ---
pre_ctx = src[max(0, start - 30):start]
post_ctx = src[end+1:min(len(src), end + 31)]
matched_text = src[start:end+1]
ctx_string = f"{mode}|{pre_ctx}|{matched_text}|{post_ctx}"
cache_key = hashlib.md5(ctx_string.encode()).hexdigest()
if cache_key in fail_cache:
continue
# --- TRANSFORMATIONS ---
if mode in ("parens-only", "curly-only", "square-only"):
candidate = src[:start] + inner + src[end+1:]
elif mode == "curly-to-empty":
candidate = src[:start] + "{}" + src[end+1:]
elif mode == "parens-to-zero":
candidate = src[:start] + "0" + src[end+1:]
elif "remove-block" in mode:
candidate = src[:start] + src[end+1:]
else:
continue
# Prevents the pass from failing on a clean {} pair
if candidate == src:
continue
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
else:
fail_cache.add(cache_key)
if not made_progress:
break
return src, changed
def pass_peep_args(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "peep_args") -> tuple[str, bool]:
"""
pass_peep: removes individual function arguments or replaces them with 0.
"""
changed = False
call_pattern = re.compile(r'\b([A-Za-z_][A-Za-z0-9_]*)\s*(\()')
for _iteration in range(20):
matches = list(call_pattern.finditer(src))
made_progress = False
for match in matches:
func_name = match.group(1)
open_pos = match.start(2)
close_pos = _find_matching(src, open_pos, '(', ')')
if close_pos == -1:
continue
args_str = src[open_pos+1:close_pos]
args = _split_args(args_str)
if len(args) <= 0:
continue
# Test: remove each argument
for i in range(len(args)):
new_args = args[:i] + args[i+1:]
new_call = f"{func_name}({', '.join(new_args)})"
candidate = src[:match.start()] + new_call + src[close_pos+1:]
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
if made_progress:
break
# Test: replace argument with 0
for i in range(len(args)):
if args[i].strip() == '0':
continue
new_args = args[:i] + ['0'] + args[i+1:]
new_call = f"{func_name}({', '.join(new_args)})"
candidate = src[:match.start()] + new_call + src[close_pos+1:]
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
if made_progress:
break
if not made_progress:
break
return src, changed
def pass_ternary(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, mode="b", pass_name: str = "ternary") -> tuple[str, bool]:
"""
pass_ternary: a ? b : c -> b (mode 'b') or c (mode 'c').
"""
changed = False
fail_cache = set()
pattern = re.compile(r'([^?]+)\?([^?:]+):([^?;\)]+)')
while True:
matches = list(pattern.finditer(src))
if not matches:
break
made_progress = False
for m in reversed(matches):
a_cond = m.group(1).strip()
b_true = m.group(2).strip()
c_false = m.group(3).strip()
replacement = b_true if mode == "b" else c_false
# --- CACHE CHECK ---
pre_ctx = src[max(0, m.start() - 30):m.start()]
post_ctx = src[m.end():min(len(src), m.end() + 30)]
matched_text = m.group(0)
ctx_string = f"ternary_{mode}|{pre_ctx}|{matched_text}|{post_ctx}"
cache_key = hashlib.md5(ctx_string.encode()).hexdigest()
if cache_key in fail_cache:
continue
candidate = src[:m.start()] + replacement + src[m.end():]
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
else:
fail_cache.add(cache_key)
if not made_progress:
break
return src, changed
# Optimised — complex expressions only, not individual identifiers
def pass_peep_subexpr(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "peep_subexpr") -> tuple[str, bool]:
"""
Replaces complex expressions with 0, 1, or empty string.
Not in TCC_SAFE_PASSES because type errors (e.g. "" instead of int)
may be handled differently by TCC and IDO.
"""
changed = False
blacklist = {
"return", "if", "else", "for", "while", "do", "switch", "case", "default",
"struct", "union", "enum", "typedef", "sizeof",
"goto", "break", "continue"
}
# Only complex expressions (at least operator + operand)
patterns = [
# Binary expressions with two identifiers/literals
r'\b[A-Za-z_][A-Za-z0-9_]*\s*[+\-*/&|^%]\s*[A-Za-z_][A-Za-z0-9_]*',
# Unary expressions
r'[!~]\s*[A-Za-z_][A-Za-z0-9_]*',
# Comparisons
r'\b[A-Za-z_][A-Za-z0-9_]*\s*[<>=!]+\s*[A-Za-z_][A-Za-z0-9_]*',
# Assignments (excluding declarations)
r'\b[A-Za-z_][A-Za-z0-9_]*\s*=\s*[^=;]+',
]
replacements = ["0", "1", ""]
for pattern in patterns:
regex = re.compile(pattern)
matches = list(regex.finditer(src))
for match in reversed(matches):
matched_text = match.group(0).strip()
if matched_text in blacklist:
continue
made_progress = False
for repl in replacements:
if matched_text == repl:
continue
# Local context cache
pre_ctx = src[max(0, match.start() - 30):match.start()]
post_ctx = src[match.end():min(len(src), match.end() + 30)]
ctx_string = f"{matched_text}|{repl}|{pre_ctx}|{post_ctx}"
cache_key = hashlib.md5(ctx_string.encode()).hexdigest()
if cache_key in _peep_local_fail_cache:
continue
candidate = src[:match.start()] + repl + src[match.end():]
# Fast-fail (syntax killer)
start_idx = max(0, match.start() - 4)
end_idx = min(len(candidate), match.start() + len(repl) + 4)
compact = re.sub(r'\s+', '', candidate[start_idx:end_idx])
if ",," in compact or "(, " in compact or ",)" in compact or "=;" in compact:
_peep_local_fail_cache.add(cache_key)
continue
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
else:
_peep_local_fail_cache.add(cache_key)
if made_progress and pattern == patterns[-1]:
pass
return src, changed
def pass_peep_while(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "peep_while") -> tuple[str, bool]:
"""
Peep 'c' from C-Reduce: replaces while(cond) { body } with body.
"""
changed = False
pattern = re.compile(r'\bwhile\s*\(')
matches = list(pattern.finditer(src))
for match in reversed(matches):
open_p = match.end() - 1
close_p = _find_matching(src, open_p, '(', ')')
if close_p == -1: continue
after_parens = src[close_p+1:].lstrip()
if after_parens.startswith('{'):
open_b = src.find('{', close_p)
close_b = _find_matching(src, open_b, '{', '}')
if close_b != -1:
body = src[open_b+1:close_b]
candidate = src[:match.start()] + body + src[close_b+1:]
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
return src, changed
def pass_regex_atoms(src: str, baseline: str, tmp_dir: str, header_dir: str,
cache: CompileCache, pass_name: str = "regex_atoms") -> tuple[str, bool]:
"""
Applies atomic regex substitutions (e.g. += -> =, unsigned -> "").
"""
changed = False
fail_cache = set()
rules = [
(r'\+\=', '='), (r'\-\=', '='), (r'\*\=', '='), (r'\/\=', '='),
(r'\%\=', '='), (r'\&\=', '='), (r'\|\=', '='), (r'\^\=', '='),
(r'\<\<\=', '='), (r'\>\>\=', '='),
(r'\bunsigned\s+', ''), (r'\bsigned\s+', ''),
(r'\bextern\s+', ''), (r'\bstatic\s+', ''),
(r'\bconst\s+', ''), (r'\bregister\s+', ''),
(r'\bvolatile\s+', ''),
]
while True:
made_progress = False
for pattern_str, replacement in rules:
pattern = re.compile(pattern_str)
matches = list(pattern.finditer(src))
for match in reversed(matches):
pre_ctx = src[max(0, match.start() - 30):match.start()]
post_ctx = src[match.end():min(len(src), match.end() + 30)]
matched_text = match.group(0)
ctx_string = f"atom|{pre_ctx}|{matched_text}|{replacement}|{post_ctx}"
cache_key = hashlib.md5(ctx_string.encode()).hexdigest()
if cache_key in fail_cache:
continue
candidate = src[:match.start()] + replacement + src[match.end():]
h, _ = cache.try_candidate(candidate, tmp_dir, header_dir, baseline, pass_name)
if h == baseline:
src = candidate
changed = True
made_progress = True
break
else:
fail_cache.add(cache_key)
if made_progress:
break
if not made_progress:
break
return src, changed
# =====================================================================
# MAIN FUNCTION PER FILE
# =====================================================================
def reduce_file(c_filepath: str, header_dir: str, output_path: str,
dry_run: bool = False, verbose: bool = False):
"""
Reduces a single C file through iterative Python passes.
Uses TCC as a fast-reject guard for syntactically safe passes
when the baseline is compilable with TCC.
"""
global _peep_local_fail_cache
_peep_local_fail_cache.clear()
result = {"file": c_filepath, "status": "clean",
"original_lines": 0, "reduced_lines": 0, "error": None,
"tcc_enabled": False, "tcc_baseline_ok": False}
with open(c_filepath, "r", encoding="utf-8", errors="replace") as f:
original_src = f.read()
# Include normalisation once per file
original_src = re.sub(r'#include\s+"[^"]*?([^/"]+\.h)"', r'#include "\1"', original_src)
# Use count('\n') instead of splitlines()
result["original_lines"] = original_src.count('\n') + (1 if original_src and not original_src.endswith('\n') else 0)
tmp_dir = tempfile.mkdtemp(dir=TMP_ROOT, prefix="tr_")
try:
baseline, err = compile_to_hash(original_src, tmp_dir, header_dir)
if baseline is None:
result["status"] = "error"
result["error"] = f"Baseline does not compile: {err}"
return result
if dry_run:
result["status"] = "would_reduce"; return result
# TCC baseline check: is TCC usable for this file at all?
tcc_baseline_ok = False
if _tcc_available():
tcc_baseline_ok = tcc_syntax_check(original_src, tmp_dir, header_dir)
result["tcc_baseline_ok"] = tcc_baseline_ok
if verbose:
status = "OK" if tcc_baseline_ok else "FAIL (TCC disabled)"
print(f" TCC Baseline Check: {status}")
# Global cache for this file (with TCC integration)
cache = CompileCache(tcc_baseline_ok=tcc_baseline_ok)
result["tcc_enabled"] = tcc_baseline_ok
# Prevents the ASM cache from growing too large
global _asm_cache
if len(_asm_cache) > 10000:
_asm_cache.clear()
src = original_src
total_changed = False
if verbose:
print(f" Baseline: {result['original_lines']} lines")
# --- PASS DEFINITIONS (with cache parameter and pass_name) ---
pass_dict = {
"blank": lambda s, b, t, h, n="blank": pass_blank(s, b, t, h, cache, n),
"includes": lambda s, b, t, h, n="includes": pass_includes(s, b, t, h, cache, n),
"lines": lambda s, b, t, h, n="lines": pass_lines(s, b, t, h, cache, n),
"regex_atoms": lambda s, b, t, h, n="regex_atoms": pass_regex_atoms(s, b, t, h, cache, n),
"peep_while": lambda s, b, t, h, n="peep_while": pass_peep_while(s, b, t, h, cache, n),
"balanced_parens_only": lambda s, b, t, h, n="balanced_parens_only": pass_balanced(s, b, t, h, cache, mode="parens-only", pass_name=n),
"balanced_curly_empty": lambda s, b, t, h, n="balanced_curly_empty": pass_balanced(s, b, t, h, cache, mode="curly-to-empty", pass_name=n),
"balanced_parens_zero": lambda s, b, t, h, n="balanced_parens_zero": pass_balanced(s, b, t, h, cache, mode="parens-to-zero", pass_name=n),
"balanced_remove_curly": lambda s, b, t, h, n="balanced_remove_curly": pass_balanced(s, b, t, h, cache, mode="curly-remove-block", pass_name=n),
"balanced_remove_parens": lambda s, b, t, h, n="balanced_remove_parens": pass_balanced(s, b, t, h, cache, mode="parens-remove-block", pass_name=n),
"balanced_remove_square": lambda s, b, t, h, n="balanced_remove_square": pass_balanced(s, b, t, h, cache, mode="square-remove-block", pass_name=n),
"peep_subexpr": lambda s, b, t, h, n="peep_subexpr": pass_peep_subexpr(s, b, t, h, cache, n),
"peep_args": lambda s, b, t, h, n="peep_args": pass_peep_args(s, b, t, h, cache, n),
"ternary_modus_b": lambda s, b, t, h, n="ternary_modus_b": pass_ternary(s, b, t, h, cache, mode="b", pass_name=n),
"ternary_modus_c": lambda s, b, t, h, n="ternary_modus_c": pass_ternary(s, b, t, h, cache, mode="c", pass_name=n),
"blank_final": lambda s, b, t, h, n="blank_final": pass_blank(s, b, t, h, cache, n),
}
PASS_DEPENDENCIES = {
"balanced_remove_block": ["lines"],
"balanced_curly_empty": ["lines"],
"peep_while": ["lines", "balanced_remove_block"],
"peep_subexpr": ["peep_args", "peep_subexpr"],
"peep_args": ["peep_subexpr"]
}
def run_phase(phase_name: str, pass_names: list, max_iters: int, require_line_drop: bool = True) -> bool:
"""
Runs a phase of passes.
require_line_drop=True: repeats only when actual lines disappear.
require_line_drop=False: repeats on ANY text change.
"""
nonlocal src, total_changed
phase_made_progress = False
if verbose: print(f"\n === {phase_name} ===")
dirty_passes = set(pass_names)
for i in range(max_iters):
iter_progress = False
for p_name in pass_names:
if p_name not in dirty_passes:
if verbose: print(f" [{p_name}] skipped (not dirty)")
continue
# Use count('\n') instead of splitlines()
lines_before = src.count('\n') + (1 if src and not src.endswith('\n') else 0)
new_src, changed = pass_dict[p_name](src, baseline, tmp_dir, header_dir, p_name)
if changed:
lines_after = new_src.count('\n') + (1 if new_src and not new_src.endswith('\n') else 0)
src = new_src
total_changed = True
if require_line_drop:
if lines_after < lines_before:
iter_progress = True
phase_made_progress = True
else:
iter_progress = True