• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014, 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 
GenerateSwapInt32(MacroAssembler * masm)32 void 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
64 #ifdef USE_SIMULATOR
main(void)65 int main(void) {
66   // Create and initialize the assembler and the simulator.
67   byte assm_buf[BUF_SIZE];
68   MacroAssembler masm(assm_buf, BUF_SIZE);
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.set_wreg(0, 0x11111111);
80   simulator.set_wreg(1, 0x22222222);
81 
82   printf("Before swap_int32:\n"
83          "x0 = 0x%" PRIx32 "\n"
84          "x1 = 0x%" PRIx32 "\n",
85          simulator.wreg(0), simulator.wreg(1));
86 
87   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&swap_int32));
88 
89   printf("After swap_int32:\n"
90          "x0 = 0x%" PRIx32 "\n"
91          "x1 = 0x%" PRIx32 "\n",
92          simulator.wreg(0), simulator.wreg(1));
93 
94   return 0;
95 }
96 #else
97 // Without the simulator there is nothing to test.
main(void)98 int main(void) { return 0; }
99 #endif  // USE_SIMULATOR
100 #endif  // TEST_EXAMPLES
101