1 // Copyright 2014, ARM Limited
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 #define BUF_SIZE (4096)
30 #define __ masm->
31 
GenerateCheckBounds(MacroAssembler * masm)32 void GenerateCheckBounds(MacroAssembler* masm) {
33   // uint64_t check_bounds(uint64_t value, uint64_t low, uint64_t high)
34   // Argument locations:
35   //   value -> x0
36   //   low   -> x1
37   //   high  -> x2
38 
39   // First we compare 'value' with the 'low' bound. If x1 <= x0 the N flag will
40   // be cleared. This configuration can be checked with the 'pl' condition.
41   __ Cmp(x0, x1);
42 
43   // Now we will compare 'value' and 'high' (x0 and x2) but only if the 'pl'
44   // condition is verified. If the condition is not verified, we will clear
45   // all the flags except the carry one (C flag).
46   __ Ccmp(x0, x2, CFlag, pl);
47 
48   // We set x0 to 1 only if the 'ls' condition is satisfied.
49   // 'ls' performs the following test: !(C==1 && Z==0). If the previous
50   // comparison has been skipped we have C==1 and Z==0, so the 'ls' test
51   // will fail and x0 will be set to 0.
52   // Otherwise if the previous comparison occurred, x0 will be set to 1
53   // only if x0 is less than or equal to x2.
54   __ Cset(x0, ls);
55 
56   __ Ret();
57 }
58 
59 
60 #ifndef TEST_EXAMPLES
61 #ifdef USE_SIMULATOR
run_function(Simulator * simulator,Instruction * function,uint64_t value,uint64_t low,uint64_t high)62 void run_function(Simulator *simulator, Instruction * function,
63                   uint64_t value, uint64_t low, uint64_t high) {
64   simulator->set_xreg(0, value);
65   simulator->set_xreg(1, low);
66   simulator->set_xreg(2, high);
67 
68   simulator->RunFrom(function);
69   printf("%ld %s between %ld and %ld\n", value,
70          simulator->xreg(0) ? "is" : "is not",
71          low, high);
72 
73   simulator->ResetState();
74 }
75 
main(void)76 int main(void) {
77   // Create and initialize the assembler and the simulator.
78   byte assm_buf[BUF_SIZE];
79   MacroAssembler masm(assm_buf, BUF_SIZE);
80   Decoder decoder;
81   Simulator simulator(&decoder);
82 
83   // Generate the code for the example function.
84   Label check_bounds;
85   masm.Bind(&check_bounds);
86   GenerateCheckBounds(&masm);
87   masm.FinalizeCode();
88 
89   // Run the example function.
90   Instruction * function = masm.GetLabelAddress<Instruction*>(&check_bounds);
91   run_function(&simulator, function, 546, 50, 1000);
92   run_function(&simulator, function, 62, 100, 200);
93   run_function(&simulator, function, 200, 100, 200);
94 
95   return 0;
96 }
97 #else
98 // Without the simulator there is nothing to test.
main(void)99 int main(void) { return 0; }
100 #endif  // USE_SIMULATOR
101 #endif  // TEST_EXAMPLES
102