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