-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simp_compose_and_mask improvements #1347
base: master
Are you sure you want to change the base?
Changes from all commits
f9d9674
5f39d01
2e10c94
a43d4a3
9a0528b
1736a6a
9d513c3
7dd421b
6192b6b
c092ec7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1753,19 +1753,39 @@ def simp_compose_and_mask(_, expr): | |
if not arg2.is_int(): | ||
return expr | ||
int2 = int(arg2) | ||
if (int2 + 1) & int2 != 0: | ||
return expr | ||
mask_size = int2.bit_length() + 7 // 8 | ||
mask_size = (int2.bit_length() + 7) // 8 * 8 | ||
if int2 == int(arg1.mask): | ||
return arg1 | ||
out = [] | ||
mask_needed = False | ||
for offset, arg in arg1.iter_args(): | ||
if offset == mask_size: | ||
return ExprCompose(*out).zeroExtend(expr.size) | ||
elif mask_size > offset and mask_size < offset+arg.size and arg.is_int(): | ||
out.append(ExprSlice(arg, 0, mask_size-offset)) | ||
return ExprCompose(*out).zeroExtend(expr.size) | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, maybe here, instead of a break, juste append a
But this will be simplified by another rule into:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is true |
||
else: | ||
out.append(arg) | ||
return expr | ||
if arg.is_int() and offset < mask_size < offset+arg.size: | ||
arg = ExprSlice(arg, 0, mask_size-offset) | ||
|
||
arg_mask = (int(arg.mask) << offset) | ||
if int2 & arg_mask != 0: | ||
out.append(arg) | ||
if int2 & arg_mask != arg_mask: | ||
mask_needed = True | ||
elif mask_size > offset + arg.size: | ||
out.append(ExprInt(0, arg.size)) | ||
|
||
if mask_size <= offset + arg.size: | ||
break | ||
|
||
if len(out) == 0: | ||
return ExprInt(0, expr.size) | ||
else: | ||
size = sum(arg.size for arg in out) | ||
if size != expr.size: | ||
out.append(ExprInt(0, expr.size - size)) | ||
result = ExprCompose(*out) | ||
if mask_needed: | ||
result = result & arg2 | ||
return result | ||
|
||
def simp_bcdadd_cf(_, expr): | ||
"""bcdadd(const, const) => decimal""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why is there a +7 here
Can you explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the mask size is rounded up to the next multiple of 8. i think this is not actually needed, you could just use
int2.bit_length()
instead, but this may result in more complex expressions because of this line in the loop:this might be the result without rounding up:
with rounding up:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. The trick is that in Miasm,
ExprCompose
can have, and have components of different sizes. Not only 8 bit arguments.So maybe we will have a problem here. I think we can distinguish 2 big cases.
So maybe we can use this metric to do or not, this simplification
So the code may be:
ExprCompose
arguments is reduced (compared to the original one).This way, the simplification should not generate a "more complex" expression (in term of number of operations)
I am aware of the fact that it may do some work for nothing, but hey, the 'simplification' problem is hard 😄
What do you think of this proposition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is a good idea, but it would be better to check if the resulting
ExprCompose
is simpler than the old one. i.e. if parts of the expression were replaced by constants. for me the whole reason for this simplification is to get rid of unneededExprId
s to reduce the dependencies of the expression.