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