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