1 // Copyright 2014, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "examples.h"
28
29 using namespace vixl;
30 using namespace vixl::aarch64;
31
32 #define __ masm->
33
GenerateCheckBounds(MacroAssembler * masm)34 void GenerateCheckBounds(MacroAssembler* masm) {
35 // uint64_t check_bounds(uint64_t value, uint64_t low, uint64_t high)
36 // Argument locations:
37 // value -> x0
38 // low -> x1
39 // high -> x2
40
41 // First we compare 'value' with the 'low' bound. If x1 <= x0 the N flag will
42 // be cleared. This configuration can be checked with the 'pl' condition.
43 __ Cmp(x0, x1);
44
45 // Now we will compare 'value' and 'high' (x0 and x2) but only if the 'pl'
46 // condition is verified. If the condition is not verified, we will clear
47 // all the flags except the carry one (C flag).
48 __ Ccmp(x0, x2, CFlag, pl);
49
50 // We set x0 to 1 only if the 'ls' condition is satisfied.
51 // 'ls' performs the following test: !(C==1 && Z==0). If the previous
52 // comparison has been skipped we have C==1 and Z==0, so the 'ls' test
53 // will fail and x0 will be set to 0.
54 // Otherwise if the previous comparison occurred, x0 will be set to 1
55 // only if x0 is less than or equal to x2.
56 __ Cset(x0, ls);
57
58 __ Ret();
59 }
60
61
62 #ifndef TEST_EXAMPLES
63 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
run_function(Simulator * simulator,Instruction * function,uint64_t value,uint64_t low,uint64_t high)64 void run_function(Simulator* simulator,
65 Instruction* function,
66 uint64_t value,
67 uint64_t low,
68 uint64_t high) {
69 simulator->WriteXRegister(0, value);
70 simulator->WriteXRegister(1, low);
71 simulator->WriteXRegister(2, high);
72
73 simulator->RunFrom(function);
74 printf("%" PRIu64 " %s between %" PRIu64 " and %" PRIu64 "\n",
75 value,
76 simulator->ReadXRegister(0) ? "is" : "is not",
77 low,
78 high);
79
80 simulator->ResetState();
81 }
82
main(void)83 int main(void) {
84 MacroAssembler masm;
85 Decoder decoder;
86 Simulator simulator(&decoder);
87
88 // Generate the code for the example function.
89 Label check_bounds;
90 masm.Bind(&check_bounds);
91 GenerateCheckBounds(&masm);
92 masm.FinalizeCode();
93
94 // Run the example function.
95 Instruction* function = masm.GetLabelAddress<Instruction*>(&check_bounds);
96 run_function(&simulator, function, 546, 50, 1000);
97 run_function(&simulator, function, 62, 100, 200);
98 run_function(&simulator, function, 200, 100, 200);
99
100 return 0;
101 }
102 #else
103 // Without the simulator there is nothing to test.
main(void)104 int main(void) { return 0; }
105 #endif // VIXL_INCLUDE_SIMULATOR_AARCH64
106 #endif // TEST_EXAMPLES
107