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 GenerateSwapInt32(MacroAssembler* masm) { 33 { 34 // In this scope the register x2 will be used by the macro-assembler 35 // as the stack pointer (via peek, poke, push, etc). 36 const Register old_stack_pointer = __ StackPointer(); 37 __ Mov(x2, __ StackPointer()); 38 __ SetStackPointer(x2); 39 40 // This call to Claim is not 16-byte aligned and would have failed 41 // if the current stack pointer was sp. 42 __ Claim(8); 43 44 __ Poke(w0, 0); 45 __ Poke(w1, 4); 46 __ Peek(w1, 0); 47 __ Peek(w0, 4); 48 49 __ Drop(8); 50 51 // Even if we didn't use the system stack pointer, sp might have been 52 // modified because the ABI forbids access to memory below the stack 53 // pointer. 54 __ Mov(old_stack_pointer, __ StackPointer()); 55 __ SetStackPointer(old_stack_pointer); 56 } 57 58 // The stack pointer has now been switched back to sp. 59 __ Ret(); 60} 61 62 63#ifndef TEST_EXAMPLES 64int main(void) { 65 // Create and initialize the assembler and the simulator. 66 byte assm_buf[BUF_SIZE]; 67 MacroAssembler masm(assm_buf, BUF_SIZE); 68 Decoder decoder; 69 Simulator simulator(&decoder); 70 71 // Generate the code for the example function. 72 Label swap_int32; 73 masm.Bind(&swap_int32); 74 GenerateSwapInt32(&masm); 75 masm.FinalizeCode(); 76 77 // Run the example function. 78 simulator.set_wreg(0, 0x11111111); 79 simulator.set_wreg(1, 0x22222222); 80 81 printf("Before swap_int32:\n" 82 "x0 = 0x%" PRIx32 "\n" 83 "x1 = 0x%" PRIx32 "\n", 84 simulator.wreg(0), simulator.wreg(1)); 85 86 simulator.RunFrom(swap_int32.target()); 87 88 printf("After swap_int32:\n" 89 "x0 = 0x%" PRIx32 "\n" 90 "x1 = 0x%" PRIx32 "\n", 91 simulator.wreg(0), simulator.wreg(1)); 92 93 return 0; 94} 95#endif 96