1// Copyright 2013, 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
32void 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
61void run_function(Simulator *simulator, Label *function,
62                  uint64_t value, uint64_t low, uint64_t high) {
63  simulator->set_xreg(0, value);
64  simulator->set_xreg(1, low);
65  simulator->set_xreg(2, high);
66
67  simulator->RunFrom(function->target());
68  printf("%ld %s between %ld and %ld\n", value,
69         simulator->xreg(0) ? "is" : "is not",
70         low, high);
71
72  simulator->ResetState();
73}
74
75int main(void) {
76  // Create and initialize the assembler and the simulator.
77  byte assm_buf[BUF_SIZE];
78  MacroAssembler masm(assm_buf, BUF_SIZE);
79  Decoder decoder;
80  Simulator simulator(&decoder);
81
82  // Generate the code for the example function.
83  Label check_bounds;
84  masm.Bind(&check_bounds);
85  GenerateCheckBounds(&masm);
86  masm.FinalizeCode();
87
88  // Run the example function.
89  run_function(&simulator, &check_bounds, 546, 50, 1000);
90  run_function(&simulator, &check_bounds, 62, 100, 200);
91  run_function(&simulator, &check_bounds, 200, 100, 200);
92
93  return 0;
94}
95#endif
96