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