1// Copyright 2016, 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#include "aarch64/disasm-aarch64.h"
30#include "aarch64/macro-assembler-aarch64.h"
31#include "aarch64/simulator-aarch64.h"
32
33#ifdef VIXL_SIMULATED_RUNTIME_CALL_SUPPORT
34
35#define __ masm->
36
37int32_t add_int32s(int32_t a, int32_t b) {
38  printf("add_int32s(%d, %d)\n", a, b);
39  return a + b;
40}
41
42float int32_to_float(int32_t value) {
43  printf("int32_to_float(%d)\n", value);
44  return static_cast<float>(value);
45}
46
47int32_t float_to_int32(float value) {
48  printf("float_to_int32(%f)\n", value);
49  return static_cast<int32_t>(value);
50}
51
52void overload_function(float value) {
53  printf("overload_function(float %f)\n", value);
54}
55
56void overload_function(int32_t value) {
57  printf("overload_function(int32_t %d)\n", value);
58}
59
60void GenerateRuntimeCallExamples(MacroAssembler* masm) {
61  // The arguments are expected in the appropriate registers (following the
62  // Aarch64 ABI).
63  __ CallRuntime(add_int32s);
64  // The result is in `w0`, as per the ABI.
65  // Of course, one can use assembly to work with the results.
66  __ Lsl(w0, w0, 2);
67  __ CallRuntime(int32_to_float);
68  // The result is in `s0`.
69  // `CallRuntime` can infer template arguments when the call is not ambiguous.
70  // Otherwise the template arguments must be specified in the form:
71  // `__ CallRuntime<return_type, parameter_type_0, parameter_type_1, ...>();`
72  __ CallRuntime<void, float>(overload_function);
73  __ CallRuntime(float_to_int32);
74  __ CallRuntime<void, int32_t>(overload_function);
75  __ Ret();
76}
77
78
79#ifndef TEST_EXAMPLES
80#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
81
82int main(void) {
83  MacroAssembler masm;
84
85  // Generate the code for the example function.
86  Label call_runtime_add_floats;
87  masm.Bind(&call_runtime_add_floats);
88  GenerateRuntimeCallExamples(&masm);
89  masm.FinalizeCode();
90
91  Instruction* start =
92      masm.GetLabelAddress<Instruction*>(&call_runtime_add_floats);
93
94  // Disassemble the generated code.
95  PrintDisassembler disassembler(stdout);
96  disassembler.DisassembleBuffer(start, masm.GetSizeOfCodeGenerated());
97
98  Decoder decoder;
99  Simulator simulator(&decoder);
100
101  int32_t input_a = 1;
102  int32_t input_b = 2;
103  simulator.WriteWRegister(0, input_a);
104  simulator.WriteWRegister(1, input_b);
105  simulator.RunFrom(start);
106  printf("The final result is %d\n", simulator.ReadWRegister(0));
107
108  return 0;
109}
110#else
111// TODO: Support running natively.
112int main(void) { return 0; }
113#endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
114#endif  // TEST_EXAMPLES
115#else
116#ifndef TEST_EXAMPLES
117int main(void) { return 0; }
118#endif  // TEST_EXAMPLES
119#endif  // VIXL_SIMULATED_RUNTIME_CALL_SUPPORT
120