interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / universal / translation / mutants / operators.py

179 lines · 6233 bytes Raw Blame History
1"""Mutation operators over translation TOOL OUTPUTS (#623, translation-v1.md).
2
3Each operator takes the text a tool emitted for one unit and returns the mutated text, or None when the
4operator has no site in that text. Operators never read tool sources; they model the faults a lowering or
5export implementation could make. validate_g1.py runs every mutant through the same comparisons as the
6real outputs and counts a mutant as killed only when at least one comparison fails on it.
7
8Lowered unit grammar (section 5): `put v <k> <rhs>` lines, `v = new ulong <count> 0`, `ret get v <count-1>`.
9Exported C grammar (section 6): one `return <expr>;` with `i<s>`, `<decimal>ULL`, ` + `, ` - `, ` * `, `(`, `)`.
10"""
11import re
12
13MASK = (1 << 64) - 1
14SWAP = {'add': 'sub', 'sub': 'add', 'mul': 'add'}
15C_SWAP = {'+': '-', '-': '+', '*': '+'}
16
17
18def _om_lines(text):
19 return text.split('\n')
20
21
22def om_op_swap(text):
23 """First arithmetic result computes a different operation (add->sub, sub->add, mul->add)."""
24 lines = _om_lines(text)
25 for i, ln in enumerate(lines):
26 m = re.match(r'^put v (\d+) (add|sub|mul) (.*)$', ln)
27 if m:
28 lines[i] = 'put v %s %s %s' % (m.group(1), SWAP[m.group(2)], m.group(3))
29 return '\n'.join(lines)
30 return None
31
32
33def om_const_off_by_one(text):
34 """First constant result is one larger (modulo 2^64)."""
35 lines = _om_lines(text)
36 for i, ln in enumerate(lines):
37 m = re.match(r'^put v (\d+) (\d+)$', ln)
38 if m:
39 lines[i] = 'put v %s %d' % (m.group(1), (int(m.group(2)) + 1) & MASK)
40 return '\n'.join(lines)
41 return None
42
43
44def om_slot_swap(text):
45 """Input slots 0 and 1 exchanged in the SSA input records (signature unchanged)."""
46 lines = _om_lines(text)
47 a = [i for i, ln in enumerate(lines) if re.match(r'^put v \d+ i0$', ln)]
48 b = [i for i, ln in enumerate(lines) if re.match(r'^put v \d+ i1$', ln)]
49 if len(a) != 1 or len(b) != 1:
50 return None
51 lines[a[0]] = lines[a[0]][:-2] + 'i1'
52 lines[b[0]] = lines[b[0]][:-2] + 'i0'
53 return '\n'.join(lines)
54
55
56def _om_shape(lines):
57 puts = [i for i, ln in enumerate(lines) if ln.startswith('put v ')]
58 new = [i for i, ln in enumerate(lines) if re.match(r'^v = new ulong \d+ 0$', ln)]
59 ret = [i for i, ln in enumerate(lines) if re.match(r'^ret get v \d+$', ln)]
60 if len(new) != 1 or len(ret) != 1 or not puts:
61 return None
62 return puts, new[0], ret[0]
63
64
65def om_plus0_strip(text):
66 """The trailing `+0` pair (const 0, add(r, const)) removed; returns r directly."""
67 lines = _om_lines(text)
68 shape = _om_shape(lines)
69 if not shape:
70 return None
71 puts, new, ret = shape
72 n = len(puts)
73 if n < 3:
74 return None
75 zero = re.match(r'^put v %d 0$' % (n - 2), lines[puts[-2]])
76 last = re.match(r'^put v %d add get v (\d+) get v %d$' % (n - 1, n - 2), lines[puts[-1]])
77 if not zero or not last or int(last.group(1)) >= n - 2:
78 return None
79 r = int(last.group(1))
80 lines[new] = 'v = new ulong %d 0' % (n - 2)
81 lines[ret] = 'ret get v %d' % r
82 del lines[puts[-1]]
83 del lines[puts[-2]]
84 return '\n'.join(lines)
85
86
87def om_plus0_dup(text):
88 """A second `+0` pair appended after the last result."""
89 lines = _om_lines(text)
90 shape = _om_shape(lines)
91 if not shape:
92 return None
93 puts, new, ret = shape
94 n = len(puts)
95 if n + 2 > 64:
96 return None
97 lines[new] = 'v = new ulong %d 0' % (n + 2)
98 lines[ret] = 'ret get v %d' % (n + 1)
99 lines[puts[-1] + 1:puts[-1] + 1] = ['put v %d 0' % n, 'put v %d add get v %d get v %d' % (n + 1, n - 1, n)]
100 return '\n'.join(lines)
101
102
103def _c_split(text):
104 m = re.match(r'^(.*\{ return )(.*)(; \}\n)$', text, re.S)
105 return (m.group(1), m.group(2), m.group(3)) if m else None
106
107
108def c_op_swap(text):
109 """First binary operator of the returned expression replaced (+->-, -->+, *->+)."""
110 s = _c_split(text)
111 if not s:
112 return None
113 m = re.search(r' ([+*-]) ', s[1])
114 if not m:
115 return None
116 e = s[1][:m.start(1)] + C_SWAP[m.group(1)] + s[1][m.end(1):]
117 return s[0] + e + s[2]
118
119
120def c_paren_drop(text):
121 """First (outermost-leftmost) parenthesis pair removed, e.g. `i0 - (i1 - i1)` -> `i0 - i1 - i1`."""
122 s = _c_split(text)
123 if not s:
124 return None
125 e = s[1]
126 o = e.find('(')
127 if o < 0:
128 return None
129 depth = 0
130 for j in range(o, len(e)):
131 depth += {'(': 1, ')': -1}.get(e[j], 0)
132 if depth == 0:
133 return s[0] + e[:o] + e[o + 1:j] + e[j + 1:] + s[2]
134 return None
135
136
137def c_const_off_by_one(text):
138 """First decimal constant one larger (modulo 2^64)."""
139 s = _c_split(text)
140 if not s:
141 return None
142 m = re.search(r'\b(\d+)ULL\b', s[1])
143 if not m:
144 return None
145 e = s[1][:m.start(1)] + str((int(m.group(1)) + 1) & MASK) + s[1][m.end(1):]
146 return s[0] + e + s[2]
147
148
149def c_slot_swap(text):
150 """Parameters i0 and i1 exchanged inside the returned expression (signature unchanged)."""
151 s = _c_split(text)
152 if not s or not re.search(r'\bi1\b', s[0]) or not re.search(r'\bi[01]\b', s[1]):
153 return None
154 e = re.sub(r'\bi([01])\b', lambda m: 'i' + ('1' if m.group(1) == '0' else '0'), s[1])
155 return s[0] + e + s[2]
156
157
158def c_plus0_strip(text):
159 """A trailing source-written ` + 0ULL` removed from the returned expression."""
160 s = _c_split(text)
161 if not s or not s[1].endswith(' + 0ULL'):
162 return None
163 return s[0] + s[1][:-len(' + 0ULL')] + s[2]
164
165
166def c_plus0_dup(text):
167 """An extra ` + 0ULL` appended to the returned expression (a duplicated normalization pair)."""
168 s = _c_split(text)
169 if not s:
170 return None
171 return s[0] + s[1] + ' + 0ULL' + s[2]
172
173
174OM_OPERATORS = [('om-op-swap', om_op_swap), ('om-const-off-by-one', om_const_off_by_one),
175 ('om-slot-swap', om_slot_swap), ('om-plus0-strip', om_plus0_strip),
176 ('om-plus0-dup', om_plus0_dup)]
177C_OPERATORS = [('c-op-swap', c_op_swap), ('c-paren-drop', c_paren_drop),
178 ('c-const-off-by-one', c_const_off_by_one), ('c-slot-swap', c_slot_swap),
179 ('c-plus0-strip', c_plus0_strip), ('c-plus0-dup', c_plus0_dup)]