• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015, 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 
34 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
LiteralExample(int64_t a,int64_t b)35 int64_t LiteralExample(int64_t a, int64_t b) {
36   // Create and initialize the macro-assembler and the simulator.
37   MacroAssembler masm;
38   Decoder decoder;
39   Simulator simulator(&decoder);
40 
41   Literal<int64_t> automatically_placed_literal(111, masm.GetLiteralPool());
42   Literal<int64_t> manually_placed_literal(222);
43 
44   // Generate some code.
45   Label start;
46   masm.Bind(&start);
47   {
48     ExactAssemblyScope scope(&masm,
49                              kInstructionSize + sizeof(int64_t),
50                              ExactAssemblyScope::kExactSize);
51     Label over_literal;
52     __ b(&over_literal);
53     __ place(&manually_placed_literal);
54     __ bind(&over_literal);
55   }
56   __ Ldr(x1, &manually_placed_literal);
57   __ Ldr(x2, &automatically_placed_literal);
58   __ Add(x0, x1, x2);
59   __ Ret();
60 
61   masm.FinalizeCode();
62 
63   // Usually, compilers will move the code to another place in memory before
64   // executing it. Emulate that.
65   size_t code_size = masm.GetSizeOfCodeGenerated();
66   uint8_t* code = reinterpret_cast<uint8_t*>(malloc(code_size));
67   if (code == NULL) {
68     return 1;
69   }
70   memcpy(code, masm.GetBuffer()->GetStartAddress<void*>(), code_size);
71 
72   // Run the code.
73   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&start));
74   printf("111 + 222 = %" PRId64 "\n", simulator.ReadXRegister(0));
75 
76   // Now let's modify the values of the literals.
77   automatically_placed_literal.UpdateValue(a, code);
78   manually_placed_literal.UpdateValue(b, code);
79 
80   // Run the code again.
81   simulator.RunFrom(reinterpret_cast<Instruction*>(code));
82   printf("%" PRId64 " + %" PRId64 " = %" PRId64 "\n",
83          a,
84          b,
85          simulator.ReadXRegister(0));
86 
87   return simulator.ReadXRegister(0);
88 }
89 #endif
90 
91 #ifndef TEST_EXAMPLES
92 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
main(void)93 int main(void) {
94   VIXL_CHECK(LiteralExample(1, 2) == 3);
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