1b78f13911bfe6eda303e91ef215c87a165aae8aeAlexandre Rames// Copyright 2016, VIXL authors
2ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// All rights reserved.
3ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
4ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// Redistribution and use in source and binary forms, with or without
5ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// modification, are permitted provided that the following conditions are met:
6ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
7ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions of source code must retain the above copyright notice,
8ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer.
9ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions in binary form must reproduce the above copyright notice,
10ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer in the documentation
11ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     and/or other materials provided with the distribution.
12ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
13ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     used to endorse or promote products derived from this software without
14ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     specific prior written permission.
15ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
16ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
27ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl#include "examples.h"
28ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
29ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl#define __ masm->
30ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
3188c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisvoid GenerateDemo(MacroAssembler* masm) {
3288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // uint32_t demo(uint32_t x)
33ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
3488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // Load a constant in r1 using the literal pool.
3588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ Ldr(r1, 0x12345678);
3688c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ And(r0, r0, r1);
3788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ Bx(lr);
3888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois}
39ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
40ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
4188c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#ifndef TEST_EXAMPLES
4288c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisint main() {
4388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  MacroAssembler masm;
44ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  // Generate the code for the example function.
4588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  Label demo;
4688c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // Tell the macro assembler that the label "demo" refer to the current
4788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // location in the buffer.
4888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  masm.Bind(&demo);
4988c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  GenerateDemo(&masm);
5088c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // Ensure that everything is generated and that the generated buffer is
5188c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // ready to use.
52ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  masm.FinalizeCode();
531e85b7f2e8ad2bfb233de29405aade635ed207cePierre Langlois#ifdef VIXL_INCLUDE_SIMULATOR_AARCH32
5488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois// There is no simulator defined for VIXL AArch32.
5588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#else
566a049f97861bd71c69d81f643e42308d28c5de31Alexandre Rames  byte* code = masm.GetBuffer()->GetStartAddress<byte*>();
57919e3fe28a5024c53ede42922092bbc32e89dcb8Alexandre Rames  uint32_t code_size = masm.GetSizeOfCodeGenerated();
5888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  ExecutableMemory memory(code, code_size);
59ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  // Run the example function.
601bce007699e07bd855b7d194ca93fa5504a73edaPierre Langlois  uint32_t (*demo_function)(uint32_t) = memory.GetEntryPoint<uint32_t (*)(
611bce007699e07bd855b7d194ca93fa5504a73edaPierre Langlois      uint32_t)>(demo, masm.GetInstructionSetInUse());
6288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  uint32_t input_value = 0x89abcdef;
6388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  uint32_t output_value = (*demo_function)(input_value);
6488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  printf("native: demo(0x%08x) = 0x%08x\n", input_value, output_value);
6588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#endif
66ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  return 0;
67ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl}
68c68cb64496485710cdb5b8480f8fee287058c93farmvixl#endif  // TEST_EXAMPLES
69