Skip to content

Commit f303317

Browse files
committed
CHB:MIPS: add support for some trap instructions
1 parent 32ab47a commit f303317

File tree

8 files changed

+255
-166
lines changed

8 files changed

+255
-166
lines changed

CodeHawk/CHB/bchlib/bCHVersion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ end
9595

9696

9797
let version = new version_info_t
98-
~version:"0.6.0_20250804"
99-
~date:"2025-08-04"
98+
~version:"0.6.0_20250805"
99+
~date:"2025-08-05"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

CodeHawk/CHB/bchlibmips32/bCHDisassembleMIPSInstruction.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ let parse_branch
7777
| 2 -> BranchLTZeroLikely (r_op rs RD, tgtop)
7878
| 3 -> BranchGEZeroLikely (r_op rs RD, tgtop)
7979
| 12 -> TrapIfEqualImmediate (r_op rs RD, mips_immediate_op true 2 imm)
80+
| 14 -> TrapIfNotEqualImmediate (r_op rs RD, mips_immediate_op true 2 imm)
8081
| 16 -> BranchLTZeroLink (r_op rs RD, tgtop)
8182
| 17 when rrs = 0 -> BranchLink tgtop
8283
| 17 -> BranchGEZeroLink (r_op rs RD, tgtop)
@@ -213,6 +214,7 @@ let parse_R_opcode
213214
| 39 -> Nor (r_op rd WR, r_op rs RD, r_op rt RD)
214215
| 42 -> SetLT (r_op rd WR, r_op rs RD, r_op rt RD)
215216
| 43 -> SetLTUnsigned (r_op rd WR,r_op rs RD,r_op rt RD)
217+
| 51 -> TrapIfLessThanUnsigned ((rrd lsl 5) + samt, r_op rs RD, r_op rt RD)
216218
| 52 -> TrapIfEqual ((rrd lsl 5) + samt, r_op rs RD, r_op rt RD)
217219
| _ -> NotRecognized ("parse_R_opcode:" ^ (stri fnct), instr)
218220

CodeHawk/CHB/bchlibmips32/bCHFnMIPSDictionary.ml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2020 Kestrel Technology LLC
88
Copyright (c) 2020-2021 Henny Sipma
9-
Copyright (c) 2021-2024 Aarno Labs LLC
9+
Copyright (c) 2021-2025 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
1212
of this software and associated documentation files (the "Software"), to deal
@@ -2022,6 +2022,26 @@ object (self)
20222022
mk_instrx_data ~xprs:[xrs; ximm; result; rresult] ~rdefs () in
20232023
([tagstring], args)
20242024

2025+
| TrapIfLessThanUnsigned (_, rs, rt) ->
2026+
let xrs = rs#to_expr floc in
2027+
let xrt = rt#to_expr floc in
2028+
let result = XOp (XLt, [xrs; xrt]) in
2029+
let rresult = rewrite_expr result in
2030+
let rdefs = [get_rdef xrs; get_rdef xrt] @ (get_all_rdefs rresult) in
2031+
let (tagstring, args) =
2032+
mk_instrx_data ~xprs:[xrs; xrt; result; rresult] ~rdefs () in
2033+
([tagstring], args)
2034+
2035+
| TrapIfNotEqualImmediate (rs, imm) ->
2036+
let xrs = rs#to_expr floc in
2037+
let ximm = imm#to_expr floc in
2038+
let result = XOp (XNe, [xrs; ximm]) in
2039+
let rresult = rewrite_expr result in
2040+
let rdefs = [get_rdef xrs] @ (get_all_rdefs rresult) in
2041+
let (tagstring, args) =
2042+
mk_instrx_data ~xprs:[xrs; ximm; result; rresult] ~rdefs () in
2043+
([tagstring], args)
2044+
20252045
| WordSwapBytesHalfwords (rd, rt) ->
20262046
let vrd = rd#to_variable floc in
20272047
let xrt = rt#to_expr floc in

CodeHawk/CHB/bchlibmips32/bCHMIPSDictionary.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2020 Kestrel Technology LLC
88
Copyright (c) 2020 Henny Sipma
9-
Copyright (c) 2021-2024 Aarno Labs LLC
9+
Copyright (c) 2021-2025 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
1212
of this software and associated documentation files (the "Software"), to deal
@@ -183,7 +183,8 @@ object (self)
183183
| MoveWordToFP (op1, op2)
184184
| ControlWordFromFP (op1,op2)
185185
| ControlWordToFP (op1,op2)
186-
| TrapIfEqualImmediate (op1,op2)
186+
| TrapIfEqualImmediate (op1, op2)
187+
| TrapIfNotEqualImmediate (op1, op2)
187188
-> (tags, [oi op1; oi op2])
188189
(* 3 operands *)
189190
| BranchEqual (op1,op2,op3)
@@ -236,6 +237,7 @@ object (self)
236237
| MovF (cc,op1,op2)
237238
| MovT (cc,op1,op2)
238239
| TrapIfEqual (cc, op1, op2)
240+
| TrapIfLessThanUnsigned (cc, op1, op2)
239241
-> (tags, [cc; oi op1; oi op2])
240242
(* fmt, 2 operands *)
241243
| FPSqrtfmt (fmt,op1,op2)

CodeHawk/CHB/bchlibmips32/bCHMIPSOpcodeRecords.ml

Lines changed: 197 additions & 158 deletions
Large diffs are not rendered by default.

CodeHawk/CHB/bchlibmips32/bCHMIPSTypes.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ type mips_opcode_t =
649649
| TrapIfEqualImmediate of (* TEQI; I-type: memory *)
650650
mips_operand_int (* rs: source *)
651651
* mips_operand_int (* imm: constant to compare against *)
652+
| TrapIfLessThanUnsigned of
653+
int (* code: ignored by hardware *)
654+
* mips_operand_int (* rs: source 1 *)
655+
* mips_operand_int (* rt: source 2 *)
656+
| TrapIfNotEqualImmediate of
657+
mips_operand_int (* rs: source *)
658+
* mips_operand_int (* imm: constant to compare against *)
652659
| Xor of (* XOR; R-type *)
653660
mips_operand_int (* rd: destination *)
654661
* mips_operand_int (* rs: source 1 *)

CodeHawk/CHB/bchlibmips32/bCHTranslateMIPSToCHIF.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,23 @@ let translate_mips_instruction
15841584
let defcmds = floc#get_vardef_commands ~use ~usehigh ctxtiaddr in
15851585
default defcmds
15861586

1587+
(* TODO: add an ASSERT on the condition *)
1588+
| TrapIfLessThanUnsigned (_, rs, rt) ->
1589+
let use = get_register_vars [rs; rt] in
1590+
let xrs = rs#to_expr floc in
1591+
let xrt = rt#to_expr floc in
1592+
let usehigh = get_use_high_vars [xrs; xrt] in
1593+
let defcmds = floc#get_vardef_commands ~use ~usehigh ctxtiaddr in
1594+
default defcmds
1595+
1596+
(* TODO: add an ASSERT on the condition *)
1597+
| TrapIfNotEqualImmediate (rs, _imm) ->
1598+
let use = get_register_vars [rs] in
1599+
let xrs = rs#to_expr floc in
1600+
let usehigh = get_use_high_vars [xrs] in
1601+
let defcmds = floc#get_vardef_commands ~use ~usehigh ctxtiaddr in
1602+
default defcmds
1603+
15871604
| Xor (rd, rs, rt) ->
15881605
let rdreg = rd#to_register in
15891606
let use = get_register_vars [rs; rt] in

CodeHawk/CHT/CHB_tests/bchlibmips32_tests/txbchlibmips32/bCHDisassembleMIPSInstructionTest.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
------------------------------------------------------------------------------
66
The MIT License (MIT)
77
8-
Copyright (c) 2022-2024 Aarno Labs LLC
8+
Copyright (c) 2022-2025 Aarno Labs LLC
99
1010
Permission is hereby granted, free of charge, to any person obtaining a copy
1111
of this software and associated documentation files (the "Software"), to deal
@@ -47,7 +47,7 @@ module TR = CHTraceResult
4747

4848

4949
let testname = "bCHDisassembleMIPSInstructionTest"
50-
let lastupdated = "2023-03-10"
50+
let lastupdated = "2025-08-05"
5151

5252

5353
let make_dw (s: string) = TR.tget_ok (D.string_to_doubleword s)
@@ -68,7 +68,6 @@ let _missing_I_opcodes = [
6868
let _missing_I_opcode_branches = [
6969
"bgezal";
7070
"bltzal";
71-
"teqi";
7271
"blezl";
7372
]
7473

@@ -156,6 +155,8 @@ let mips_I_opcode_be_branch () =
156155
("bltzl", "0x46d864", "04620005", "bltzl $v1, 0x46d87c");
157156
("bne", "0x405560", "1615ffdf", "bne $s0, $s5, 0x4054e0");
158157
("bnel", "0x46dd90", "5462000a", "bnel $v1, $v0, 0x46ddbc");
158+
("teqi", "0x105f8", "046c0000", "teqi $v1, 0");
159+
("tnei", "0x10810", "06ce0000", "tnei $s6, 0");
159160
] in
160161
begin
161162
TS.new_testsuite (testname ^ "_mips_I_opcode_be_branch") lastupdated;
@@ -225,6 +226,7 @@ let mips_R_opcode_be () =
225226
("srl", "0x40b290", "00032e02", "srl $a1, $v1, 0x18");
226227
("srlv", "0x46d804", "00851806", "srlv $v1, $a1, $a0");
227228
("teq", "0x415748", "006001f4", "teq $v1, $zero");
229+
("tltu", "0xc7c8", "02820033", "tltu $s4, $v0");
228230
("xor", "0x40b29c", "00451026", "xor $v0, $v0, $a1");
229231
] in
230232
begin

0 commit comments

Comments
 (0)