Skip to content

Commit 3c7b34a

Browse files
committed
CHT:XPR: add unit tests for addressofvar
1 parent b716558 commit 3c7b34a

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

CodeHawk/CHT/CH_tests/xprlib_tests/txprlib/tCHXprlibGenerator.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
(* =============================================================================
2-
CodeHawk Unit Testing Framework
2+
CodeHawk Unit Testing Framework
33
Author: Henny Sipma
44
Adapted from: Kaputt (https://kaputt.x9c.fr/index.html)
55
------------------------------------------------------------------------------
66
The MIT License (MIT)
7-
7+
88
Copyright (c) 2005-2019 Kestrel Technology LLC
99
Copyright (c) 2020-2021 Henny Sipma
10-
Copyright (c) 2022-2023 Aarno Labs LLC
10+
Copyright (c) 2022-2025 Aarno Labs LLC
1111
1212
Permission is hereby granted, free of charge, to any person obtaining a copy
1313
of this software and associated documentation files (the "Software"), to deal
1414
in the Software without restriction, including without limitation the rights
1515
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1616
copies of the Software, and to permit persons to whom the Software is
1717
furnished to do so, subject to the following conditions:
18-
18+
1919
The above copyright notice and this permission notice shall be included in all
2020
copies or substantial portions of the Software.
21-
21+
2222
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2323
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2424
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -61,6 +61,11 @@ let mk_neg (x: xpr_t) = XOp (XNeg, [x])
6161
let mk_var (name: string) = new variable_t (new symbol_t name) NUM_VAR_TYPE
6262

6363

64+
let mk_addressof (name: string) =
65+
let v = mk_var name in
66+
XOp ((Xf "addressofvar"), [XVar v])
67+
68+
6469
let mk_vars (name: string) (n: int) =
6570
let rec aux nn result =
6671
if nn = 0 then

CodeHawk/CHT/CH_tests/xprlib_tests/txprlib/tCHXprlibGenerator.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Copyright (c) 2005-2019 Kestrel Technology LLC
99
Copyright (c) 2020-2021 Henny B. Sipma
10-
Copyright (c) 2022-2024 Aarno Labs LLC
10+
Copyright (c) 2022-2025 Aarno Labs LLC
1111
1212
Permission is hereby granted, free of charge, to any person obtaining a copy
1313
of this software and associated documentation files (the "Software"), to deal
@@ -53,3 +53,5 @@ val mk_neg: xpr_t -> xpr_t
5353
val mk_var: string -> variable_t
5454

5555
val mk_vars: string -> int -> variable_t list
56+
57+
val mk_addressof: string -> xpr_t

CodeHawk/CHT/CH_tests/xprlib_tests/txxprlib/xsimplifyTest.ml

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Copyright (c) 2005-2019 Kestrel Technology LLC
99
Copyright (c) 2020-2021 Henny Sipma
10-
Copyright (c) 2022-2024 Aarno Labs LLC
10+
Copyright (c) 2022-2025 Aarno Labs LLC
1111
1212
Permission is hereby granted, free of charge, to any person obtaining a copy
1313
of this software and associated documentation files (the "Software"), to deal
@@ -42,7 +42,7 @@ let simplify = S.simplify_xpr
4242

4343

4444
let testname = "xsimplifyTest"
45-
let lastupdated = "2024-06-19"
45+
let lastupdated = "2025-02-07"
4646

4747

4848
let basic () =
@@ -562,6 +562,61 @@ let reduce_plus () =
562562
r (simplify
563563
(XOp (XPlus, [XOp (XShiftlt, [x; a]); XOp (XShiftlt, [x; b])]))));
564564

565+
(* (xbase + a) ==> (xbase + a) *)
566+
TS.add_simple_test
567+
~title: "base_no_move"
568+
(fun () ->
569+
let a = XG.mk_ix 2 in
570+
let v = XG.mk_var "x" in
571+
let x = XOp (XPlus, [XOp ((Xf "addressofvar"), [XVar v]); a]) in
572+
XA.equal_xpr x (simplify x));
573+
574+
(* (a + xbase) -> (xbase + a) *)
575+
TS.add_simple_test
576+
~title: "base_move"
577+
(fun () ->
578+
let a = XG.mk_ix 2 in
579+
let aofv = XG.mk_addressof "v" in
580+
let r = XOp (XPlus, [aofv; a]) in
581+
let x = XOp (XPlus, [a; aofv]) in
582+
XA.equal_xpr r (simplify x));
583+
584+
(* (a + xbase) - b ==> xbase + (a - b) *)
585+
TS.add_simple_test
586+
~title: "base_move_out"
587+
(fun () ->
588+
let a = XVar (XG.mk_var "x") in
589+
let b = XVar (XG.mk_var "y") in
590+
let aofv = XG.mk_addressof "v" in
591+
let x = XOp (XMinus, [XOp (XPlus, [a; aofv]); b]) in
592+
let r = XOp (XPlus, [aofv; XOp (XMinus, [a; b])]) in
593+
XA.equal_xpr r (simplify x));
594+
595+
(* (xbase + ((x * y) - z)) => unchanged *)
596+
TS.add_simple_test
597+
~title: "base_index_expr"
598+
(fun () ->
599+
let x = XVar (XG.mk_var "x") in
600+
let y = XVar (XG.mk_var "y") in
601+
let z = XG.mk_ix 2 in
602+
let aofv = XG.mk_addressof "v" in
603+
let p = XOp (XMult, [x; y]) in
604+
let xx = XOp (XPlus, [aofv; XOp (XMinus, [p; z])]) in
605+
XA.equal_xpr xx (simplify xx));
606+
607+
(* ((xbase + y) + z) ==> (xbase + (y + z)) *)
608+
TS.add_simple_test
609+
~title:"base_p_move_out"
610+
(fun () ->
611+
let aofv = XG.mk_addressof "v" in
612+
let y = XVar (XG.mk_var "y") in
613+
let z = XVar (XG.mk_var "z") in
614+
let x = XOp (XPlus, [XOp (XPlus, [aofv; y]); z]) in
615+
let r = XOp (XPlus, [aofv; XOp (XPlus, [y; z])]) in
616+
XA.equal_xpr r (simplify x));
617+
618+
(* ((addressofvar (gv_0x5e1e1c) + ((68 * R0_in[4]_in) - 68)) + 40) *)
619+
565620
TS.launch_tests ()
566621
end
567622

0 commit comments

Comments
 (0)