blob: 43effe5cb09879bb1c0840f31973196c9d8639c0 [file] [log] [blame]
Alexandre Ramesb78f1392016-07-01 14:22:22 +01001// Copyright 2015, VIXL authors
Pierre Langlois88c46b82016-06-02 18:15:32 +01002// 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
Jacob Bramley5523f6c2019-06-28 11:37:26 +010029using namespace vixl;
30using namespace vixl::aarch64;
31
Pierre Langlois88c46b82016-06-02 18:15:32 +010032#define __ masm.
33
Pierre Langlois1e85b7f2016-08-05 14:20:36 +010034#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
Pierre Langlois88c46b82016-06-02 18:15:32 +010035int64_t LiteralExample(int64_t a, int64_t b) {
36 // Create and initialize the macro-assembler and the simulator.
Alexandre Rames9aded352016-07-11 16:52:26 +010037 MacroAssembler masm;
Pierre Langlois88c46b82016-06-02 18:15:32 +010038 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 {
Pierre Langlois59bfe1c2017-01-24 19:04:12 +000048 ExactAssemblyScope scope(&masm,
49 kInstructionSize + sizeof(int64_t),
50 ExactAssemblyScope::kExactSize);
Pierre Langlois88c46b82016-06-02 18:15:32 +010051 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 }
Alexandre Rames6a049f92016-09-21 14:55:05 +010070 memcpy(code, masm.GetBuffer()->GetStartAddress<void*>(), code_size);
Pierre Langlois88c46b82016-06-02 18:15:32 +010071
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
Pierre Langlois1e85b7f2016-08-05 14:20:36 +010092#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
Pierre Langlois88c46b82016-06-02 18:15:32 +010093int main(void) {
94 VIXL_CHECK(LiteralExample(1, 2) == 3);
95 return 0;
96}
97#else
98// Without the simulator there is nothing to test.
99int main(void) { return 0; }
Pierre Langlois1e85b7f2016-08-05 14:20:36 +0100100#endif // VIXL_INCLUDE_SIMULATOR_AARCH64
Pierre Langlois88c46b82016-06-02 18:15:32 +0100101#endif // TEST_EXAMPLES