1b8021494Sopenharmony_ci// Copyright 2016, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#include "examples.h"
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ciusing namespace vixl;
30b8021494Sopenharmony_ciusing namespace vixl::aarch32;
31b8021494Sopenharmony_ci
32b8021494Sopenharmony_ci#define __ masm->
33b8021494Sopenharmony_ci
34b8021494Sopenharmony_civoid GenerateDemo(MacroAssembler* masm) {
35b8021494Sopenharmony_ci  // uint32_t demo(uint32_t x)
36b8021494Sopenharmony_ci
37b8021494Sopenharmony_ci  // Load a constant in r1 using the literal pool.
38b8021494Sopenharmony_ci  __ Ldr(r1, 0x12345678);
39b8021494Sopenharmony_ci  __ And(r0, r0, r1);
40b8021494Sopenharmony_ci  __ Bx(lr);
41b8021494Sopenharmony_ci}
42b8021494Sopenharmony_ci
43b8021494Sopenharmony_ci
44b8021494Sopenharmony_ci#ifndef TEST_EXAMPLES
45b8021494Sopenharmony_ciint main() {
46b8021494Sopenharmony_ci  MacroAssembler masm;
47b8021494Sopenharmony_ci  // Generate the code for the example function.
48b8021494Sopenharmony_ci  Label demo;
49b8021494Sopenharmony_ci  // Tell the macro assembler that the label "demo" refer to the current
50b8021494Sopenharmony_ci  // location in the buffer.
51b8021494Sopenharmony_ci  masm.Bind(&demo);
52b8021494Sopenharmony_ci  GenerateDemo(&masm);
53b8021494Sopenharmony_ci  // Ensure that everything is generated and that the generated buffer is
54b8021494Sopenharmony_ci  // ready to use.
55b8021494Sopenharmony_ci  masm.FinalizeCode();
56b8021494Sopenharmony_ci#ifdef VIXL_INCLUDE_SIMULATOR_AARCH32
57b8021494Sopenharmony_ci// There is no simulator defined for VIXL AArch32.
58b8021494Sopenharmony_ci#else
59b8021494Sopenharmony_ci  byte* code = masm.GetBuffer()->GetStartAddress<byte*>();
60b8021494Sopenharmony_ci  uint32_t code_size = masm.GetSizeOfCodeGenerated();
61b8021494Sopenharmony_ci  ExecutableMemory memory(code, code_size);
62b8021494Sopenharmony_ci  // Run the example function.
63b8021494Sopenharmony_ci  uint32_t (*demo_function)(uint32_t) =
64b8021494Sopenharmony_ci      memory
65b8021494Sopenharmony_ci          .GetEntryPoint<uint32_t (*)(uint32_t)>(demo,
66b8021494Sopenharmony_ci                                                 masm.GetInstructionSetInUse());
67b8021494Sopenharmony_ci  uint32_t input_value = 0x89abcdef;
68b8021494Sopenharmony_ci  uint32_t output_value = (*demo_function)(input_value);
69b8021494Sopenharmony_ci  printf("native: demo(0x%08x) = 0x%08x\n", input_value, output_value);
70b8021494Sopenharmony_ci#endif
71b8021494Sopenharmony_ci  return 0;
72b8021494Sopenharmony_ci}
73b8021494Sopenharmony_ci#endif  // TEST_EXAMPLES
74