Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | // Copyright 2015, VIXL authors |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 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 | |
Jacob Bramley | 5523f6c | 2019-06-28 11:37:26 +0100 | [diff] [blame] | 29 | using namespace vixl; |
| 30 | using namespace vixl::aarch64; |
| 31 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 32 | #define __ masm. |
| 33 | |
Pierre Langlois | 1e85b7f | 2016-08-05 14:20:36 +0100 | [diff] [blame] | 34 | #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64 |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 35 | int64_t LiteralExample(int64_t a, int64_t b) { |
| 36 | // Create and initialize the macro-assembler and the simulator. |
Alexandre Rames | 9aded35 | 2016-07-11 16:52:26 +0100 | [diff] [blame] | 37 | MacroAssembler masm; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 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 | { |
Pierre Langlois | 59bfe1c | 2017-01-24 19:04:12 +0000 | [diff] [blame] | 48 | ExactAssemblyScope scope(&masm, |
| 49 | kInstructionSize + sizeof(int64_t), |
| 50 | ExactAssemblyScope::kExactSize); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 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 | } |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 70 | memcpy(code, masm.GetBuffer()->GetStartAddress<void*>(), code_size); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 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 |
Pierre Langlois | 1e85b7f | 2016-08-05 14:20:36 +0100 | [diff] [blame] | 92 | #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64 |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 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. |
| 99 | int main(void) { return 0; } |
Pierre Langlois | 1e85b7f | 2016-08-05 14:20:36 +0100 | [diff] [blame] | 100 | #endif // VIXL_INCLUDE_SIMULATOR_AARCH64 |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 101 | #endif // TEST_EXAMPLES |