Skip to content

Commit 3496283

Browse files
committed
Bug fix on address mismatch
Signed-off-by: Afonso Oliveira <[email protected]>
1 parent f7d3af6 commit 3496283

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

ext/binutils-gdb/encoding.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,23 @@ def parse_instruction_files(instruction_files):
132132

133133
return instructions
134134

135-
def match_mask_to_encoding(match, mask):
135+
def match_mask_to_encoding(match, mask, is_compressed=False):
136136
"""
137137
Converts MATCH and MASK values into a binary encoding string with '0', '1', and '-'.
138138
139139
Args:
140140
match (int): The MATCH_* integer value.
141141
mask (int): The MASK_* integer value.
142+
is_compressed (bool): Whether this is a compressed (16-bit) instruction
142143
143144
Returns:
144-
encoding_str (str): A 32-character string representing the encoding.
145+
encoding_str (str): A 16 or 32-character string representing the encoding.
145146
"""
146147
encoding = []
147-
for bit in range(31, -1, -1): # From bit 31 to bit 0
148+
# Use 15 for 16-bit instructions, 31 for 32-bit instructions
149+
max_bit = 15 if is_compressed else 31
150+
151+
for bit in range(max_bit, -1, -1): # From max_bit to bit 0
148152
mask_bit = (mask >> bit) & 1
149153
if mask_bit:
150154
match_bit = (match >> bit) & 1
@@ -157,35 +161,28 @@ def match_mask_to_encoding(match, mask):
157161
def format_encoding(encoding_str):
158162
"""
159163
Formats the encoding string by grouping bits for readability.
160-
161-
Desired format:
162-
match: 0000000----------000-----0110011
163-
164-
Which corresponds to:
165-
- bits 31-25: 7 bits
166-
- bits 24-15: 10 bits
167-
- bits 14-12: 3 bits
168-
- bits 11-7: 5 bits
169-
- bits 6-0: 7 bits
164+
Handles both 32-bit and 16-bit instructions.
170165
171166
Args:
172-
encoding_str (str): The 32-character encoding string.
167+
encoding_str (str): The encoding string.
173168
174169
Returns:
175170
formatted_str (str): The formatted encoding string with separators.
176171
"""
177-
if len(encoding_str) != 32:
178-
print("Error: Encoding string is not 32 bits long.")
179-
return encoding_str # Return as is
180-
181-
group1 = encoding_str[0:7] # bits 31-25
182-
group2 = encoding_str[7:17] # bits 24-15
183-
group3 = encoding_str[17:20] # bits 14-12
184-
group4 = encoding_str[20:25] # bits 11-7
185-
group5 = encoding_str[25:32] # bits 6-0
186-
187-
formatted_str = f"{group1}{group2}{group3}{group4}{group5}"
188-
return formatted_str
172+
if len(encoding_str) == 16: # Compressed instruction
173+
# Compressed instruction format (16 bits)
174+
# Typical format is: func3(3) | imm/register fields | op(2)
175+
return encoding_str # Return as-is for compressed instructions
176+
elif len(encoding_str) == 32: # Standard instruction
177+
group1 = encoding_str[0:7] # bits 31-25
178+
group2 = encoding_str[7:17] # bits 24-15
179+
group3 = encoding_str[17:20] # bits 14-12
180+
group4 = encoding_str[20:25] # bits 11-7
181+
group5 = encoding_str[25:32] # bits 6-0
182+
return f"{group1}{group2}{group3}{group4}{group5}"
183+
else:
184+
print(f"Error: Unexpected encoding string length: {len(encoding_str)}")
185+
return encoding_str
189186

190187
def get_instruction_yaml_files(directory_path):
191188
"""
@@ -308,12 +305,15 @@ def main():
308305

309306
for d in defs:
310307
try:
308+
# Check if it's a compressed instruction
309+
is_compressed = name.startswith('c.') or 'CLASS_C' in d['class']
310+
311311
# Evaluate MATCH expression
312312
match_val = evaluate_expression(d['match_expr'], defines)
313313
# Evaluate MASK expression
314314
mask_val = evaluate_expression(d['mask_expr'], defines)
315-
# Generate the encoding string
316-
encoding_str = match_mask_to_encoding(match_val, mask_val)
315+
# Generate the encoding string with compression flag
316+
encoding_str = match_mask_to_encoding(match_val, mask_val, is_compressed)
317317
# Format the encoding string
318318
formatted_encoding = format_encoding(encoding_str)
319319
# Collect successful encodings
@@ -322,17 +322,16 @@ def main():
322322
# Skip this definition if evaluation fails
323323
continue
324324

325+
325326
if success_encodings:
326327
for class_, encoding in success_encodings:
327-
if success_encodings:
328-
for class_, encoding in success_encodings:
329-
# Simulate a mismatch by modifying the encoding
330-
if True: # Always trigger the mismatch condition
331-
mismatches_found = True
332-
print(f"Error: Encoding mismatch for instruction '{name}' in YAML file '{yaml_file_path}'.")
333-
print(f" YAML match : {yaml_encoding}")
334-
print(f" Generated match: {encoding}\n")
335-
sys.exit(1) # Exit immediately on first mismatch
328+
# Actually compare the encodings
329+
if yaml_encoding.replace(" ", "") != encoding.replace(" ", ""):
330+
mismatches_found = True
331+
print(f"Error: Encoding mismatch for instruction '{name}' in YAML file '{yaml_file_path}'.")
332+
print(f" YAML match : {yaml_encoding}")
333+
print(f" Generated match: {encoding}\n")
334+
sys.exit(1) # Exit immediately on first mismatch
336335
else:
337336
# No valid definitions could be processed for this instruction
338337
print(f"Error: Could not evaluate any MATCH/MASK expressions for instruction '{name}' in YAML file '{yaml_file_path}'.\n")

0 commit comments

Comments
 (0)