Skip to content

Commit 6ca4f34

Browse files
committed
MIPS: opcode classes for trap instructions
1 parent 941aaea commit 6ca4f34

File tree

4 files changed

+218
-1
lines changed

4 files changed

+218
-1
lines changed

chb/app/CHVersion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
chbversion: str = "0.3.0-20250804"
1+
chbversion: str = "0.3.0-20250805"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# ------------------------------------------------------------------------------
2+
# CodeHawk Binary Analyzer
3+
# Author: Henny Sipma
4+
# ------------------------------------------------------------------------------
5+
# The MIT License (MIT)
6+
#
7+
# Copyright (c) 2025 Aarno Labs LLC
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
# ------------------------------------------------------------------------------
27+
28+
from typing import cast, List, Sequence, TYPE_CHECKING
29+
30+
from chb.app.InstrXData import InstrXData
31+
32+
from chb.invariants.XXpr import XXpr
33+
34+
from chb.mips.MIPSDictionaryRecord import mipsregistry
35+
from chb.mips.MIPSOpcode import MIPSOpcode, simplify_result
36+
from chb.mips.MIPSOperand import MIPSOperand
37+
38+
import chb.util.fileutil as UF
39+
40+
from chb.util.IndexedTable import IndexedTableValue
41+
42+
if TYPE_CHECKING:
43+
from chb.mips.MIPSDictionary import MIPSDictionary
44+
45+
46+
@mipsregistry.register_tag("teqi", MIPSOpcode)
47+
class MIPSTrapIfEqualImmediate(MIPSOpcode):
48+
"""Trap if equal immediate.
49+
50+
TEQI rs, imm
51+
52+
args[0]: index of rs in mipsdictionary
53+
args[1]: index of imm in mipsdictionary
54+
"""
55+
56+
def __init__(
57+
self,
58+
mipsd: "MIPSDictionary",
59+
ixval: IndexedTableValue) -> None:
60+
MIPSOpcode.__init__(self, mipsd, ixval)
61+
62+
@property
63+
def operands(self) -> Sequence[MIPSOperand]:
64+
return [self.mipsd.mips_operand(i) for i in self.args]
65+
66+
def annotation(self, xdata: InstrXData) -> str:
67+
rhs1 = str(xdata.xprs[0])
68+
rhs2 = str(xdata.xprs[1])
69+
result = xdata.xprs[2]
70+
rresult = xdata.xprs[3]
71+
xresult = simplify_result(xdata.args[2], xdata.args[3], result, rresult)
72+
return 'trap if ' + rhs1 + ' == ' + rhs2 + ' (' + xresult + ')'
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# ------------------------------------------------------------------------------
2+
# CodeHawk Binary Analyzer
3+
# Author: Henny Sipma
4+
# ------------------------------------------------------------------------------
5+
# The MIT License (MIT)
6+
#
7+
# Copyright (c) 2025 Aarno Labs LLC
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
# ------------------------------------------------------------------------------
27+
28+
from typing import cast, List, Sequence, TYPE_CHECKING
29+
30+
from chb.app.InstrXData import InstrXData
31+
32+
from chb.invariants.XXpr import XXpr
33+
34+
from chb.mips.MIPSDictionaryRecord import mipsregistry
35+
from chb.mips.MIPSOpcode import MIPSOpcode, simplify_result
36+
from chb.mips.MIPSOperand import MIPSOperand
37+
38+
import chb.util.fileutil as UF
39+
40+
from chb.util.IndexedTable import IndexedTableValue
41+
42+
if TYPE_CHECKING:
43+
from chb.mips.MIPSDictionary import MIPSDictionary
44+
45+
46+
@mipsregistry.register_tag("tltu", MIPSOpcode)
47+
class MIPSTrapIfLessThanUnsigned(MIPSOpcode):
48+
"""Trap if less than unsigned.
49+
50+
TLTU rs, rt
51+
52+
args[0]: code field (bits 15:6)
53+
args[1]: index of rs in mipsdictionary
54+
args[2]: index of rt in mipsdictionary
55+
"""
56+
57+
def __init__(
58+
self,
59+
mipsd: "MIPSDictionary",
60+
ixval: IndexedTableValue) -> None:
61+
MIPSOpcode.__init__(self, mipsd, ixval)
62+
63+
@property
64+
def operands(self) -> Sequence[MIPSOperand]:
65+
return [self.mipsd.mips_operand(i) for i in self.args[1:]]
66+
67+
def annotation(self, xdata: InstrXData) -> str:
68+
rhs1 = str(xdata.xprs[0])
69+
rhs2 = str(xdata.xprs[1])
70+
result = xdata.xprs[2]
71+
rresult = xdata.xprs[3]
72+
xresult = simplify_result(xdata.args[2], xdata.args[3], result, rresult)
73+
return 'trap if ' + rhs1 + ' < ' + rhs2 + ' (' + xresult + ')'
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# ------------------------------------------------------------------------------
2+
# CodeHawk Binary Analyzer
3+
# Author: Henny Sipma
4+
# ------------------------------------------------------------------------------
5+
# The MIT License (MIT)
6+
#
7+
# Copyright (c) 2025 Aarno Labs LLC
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
# ------------------------------------------------------------------------------
27+
28+
from typing import cast, List, Sequence, TYPE_CHECKING
29+
30+
from chb.app.InstrXData import InstrXData
31+
32+
from chb.invariants.XXpr import XXpr
33+
34+
from chb.mips.MIPSDictionaryRecord import mipsregistry
35+
from chb.mips.MIPSOpcode import MIPSOpcode, simplify_result
36+
from chb.mips.MIPSOperand import MIPSOperand
37+
38+
import chb.util.fileutil as UF
39+
40+
from chb.util.IndexedTable import IndexedTableValue
41+
42+
if TYPE_CHECKING:
43+
from chb.mips.MIPSDictionary import MIPSDictionary
44+
45+
46+
@mipsregistry.register_tag("tnei", MIPSOpcode)
47+
class MIPSTrapIfNotEqualImmediate(MIPSOpcode):
48+
"""Trap if not equal immediate.
49+
50+
TNEI rs, imm
51+
52+
args[0]: index of rs in mipsdictionary
53+
args[1]: index of imm in mipsdictionary
54+
"""
55+
56+
def __init__(
57+
self,
58+
mipsd: "MIPSDictionary",
59+
ixval: IndexedTableValue) -> None:
60+
MIPSOpcode.__init__(self, mipsd, ixval)
61+
62+
@property
63+
def operands(self) -> Sequence[MIPSOperand]:
64+
return [self.mipsd.mips_operand(i) for i in self.args]
65+
66+
def annotation(self, xdata: InstrXData) -> str:
67+
rhs1 = str(xdata.xprs[0])
68+
rhs2 = str(xdata.xprs[1])
69+
result = xdata.xprs[2]
70+
rresult = xdata.xprs[3]
71+
xresult = simplify_result(xdata.args[2], xdata.args[3], result, rresult)
72+
return 'trap if ' + rhs1 + ' != ' + rhs2 + ' (' + xresult + ')'

0 commit comments

Comments
 (0)