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