Skip to content

Commit

Permalink
Improve gcc sqrtf asm pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlindholm committed Jan 13, 2024
1 parent 0514701 commit 1c328ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
27 changes: 23 additions & 4 deletions m2c/arch_mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,11 @@ def match(self, matcher: AsmMatcher) -> Optional[Replacement]:
return Replacement([new_instr], len(m.body))


class GccSqrtPattern(SimpleAsmPattern):
class GccSqrtPatternJal(SimpleAsmPattern):
pattern = make_pattern(
"sqrt.s $o, $i",
"c.eq.s",
"nop",
"nop?",
"bc1t",
"*",
"jal sqrtf",
Expand All @@ -438,7 +438,25 @@ class GccSqrtPattern(SimpleAsmPattern):
)

def replace(self, m: AsmMatch) -> Replacement:
return Replacement([m.body[0], m.body[4]], len(m.body))
return Replacement([m.body[0], m.wildcard_items[0]], len(m.body))


class GccSqrtPatternJalr(SimpleAsmPattern):
pattern = make_pattern(
"sqrt.s $o, $i",
"c.eq.s",
"nop?",
"bc1t",
"*",
"lui $x, %hi(sqrtf)",
"addiu $x, $x, %lo(sqrtf)",
"jalr $ra, $x",
"nop",
"mov.s $o, $f0?",
)

def replace(self, m: AsmMatch) -> Replacement:
return Replacement([m.body[0], m.wildcard_items[0]], len(m.body))


class TrapuvPattern(SimpleAsmPattern):
Expand Down Expand Up @@ -1238,7 +1256,8 @@ def eval_fn(s: NodeState, a: InstrArgs) -> None:
UtfPattern(),
FtuPattern(),
Mips1DoubleLoadStorePattern(),
GccSqrtPattern(),
GccSqrtPatternJal(),
GccSqrtPatternJalr(),
TrapuvPattern(),
SetGpPattern(),
AlignedMemcpyPattern(),
Expand Down
2 changes: 1 addition & 1 deletion m2c/arch_ppc.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class PpcArch(Arch):
def missing_return(cls) -> List[Instruction]:
return [cls.parse("blr", [], InstructionMeta.missing())]

# List of all instructions where `$r0` as certian args is interpreted as `0`
# List of all instructions where `$r0` as certain args is interpreted as `0`
# instead of the contents of `$r0`. The dict value represents the argument
# index that is affected.
INSTRS_R0_AS_ZERO: ClassVar[Dict[str, int]] = {
Expand Down
7 changes: 7 additions & 0 deletions m2c/asm_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
AsmLiteral,
BinOp,
JumpTarget,
Macro,
NaiveParsingArch,
Register,
RegFormatter,
Expand Down Expand Up @@ -165,6 +166,12 @@ def match_arg(self, a: Argument, e: Argument) -> bool:
)
if isinstance(e, BinOp):
return isinstance(a, AsmLiteral) and a.value == self.eval_math(e)
if isinstance(e, Macro):
return (
isinstance(a, Macro)
and a.macro_name == e.macro_name
and self.match_arg(a.argument, e.argument)
)
assert False, f"bad pattern part: {e}"

def match_one(self, actual: BodyPart, exp: PatternPart) -> bool:
Expand Down

0 comments on commit 1c328ca

Please sign in to comment.