Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 1 | // Copyright 2015, 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 | #ifndef VIXL_EXAMPLE_EXAMPLES_H_ |
| 28 | #define VIXL_EXAMPLE_EXAMPLES_H_ |
| 29 | |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 30 | extern "C" { |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 31 | #include <stdint.h> |
Pierre Langlois | 1e85b7f | 2016-08-05 14:20:36 +0100 | [diff] [blame] | 32 | #ifndef VIXL_INCLUDE_SIMULATOR_AARCH32 |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 33 | #include <sys/mman.h> |
| 34 | #endif |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | #include <cstdio> |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 38 | #include <string> |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 39 | |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 40 | #include "aarch32/constants-aarch32.h" |
| 41 | #include "aarch32/instructions-aarch32.h" |
| 42 | #include "aarch32/macro-assembler-aarch32.h" |
| 43 | |
Pierre Langlois | 11000d0 | 2016-08-08 17:48:43 +0100 | [diff] [blame] | 44 | using namespace vixl; |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 45 | using namespace vixl::aarch32; |
| 46 | |
Pierre Langlois | 1e85b7f | 2016-08-05 14:20:36 +0100 | [diff] [blame] | 47 | #ifndef VIXL_INCLUDE_SIMULATOR_AARCH32 |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 48 | class ExecutableMemory { |
| 49 | public: |
| 50 | ExecutableMemory(const byte* code_start, size_t size) |
| 51 | : size_(size), |
| 52 | buffer_(reinterpret_cast<byte*>(mmap(NULL, |
| 53 | size, |
| 54 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 55 | MAP_SHARED | MAP_ANONYMOUS, |
| 56 | -1, |
| 57 | 0))) { |
| 58 | VIXL_ASSERT(reinterpret_cast<intptr_t>(buffer_) != -1); |
| 59 | memcpy(buffer_, code_start, size_); |
| 60 | __builtin___clear_cache(buffer_, buffer_ + size_); |
| 61 | } |
| 62 | ~ExecutableMemory() { munmap(buffer_, size_); } |
| 63 | |
| 64 | template <typename T> |
Georgia Kouveli | 963fa8a | 2017-02-09 11:34:10 +0000 | [diff] [blame] | 65 | T GetEntryPoint(const Label& entry_point, InstructionSet isa) const { |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 66 | int32_t location = entry_point.GetLocation(); |
Georgia Kouveli | 963fa8a | 2017-02-09 11:34:10 +0000 | [diff] [blame] | 67 | if (isa == T32) location += 1; |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 68 | return GetOffsetAddress<T>(location); |
| 69 | } |
| 70 | |
| 71 | protected: |
| 72 | template <typename T> |
Pierre Langlois | f5348ce | 2016-09-22 11:15:35 +0100 | [diff] [blame] | 73 | T GetOffsetAddress(int32_t offset) const { |
Pierre Langlois | 36d16be | 2016-08-09 16:06:16 +0100 | [diff] [blame] | 74 | VIXL_ASSERT((offset >= 0) && (static_cast<size_t>(offset) <= size_)); |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 75 | T function_address; |
| 76 | byte* buffer_address = buffer_ + offset; |
| 77 | memcpy(&function_address, &buffer_address, sizeof(T)); |
| 78 | return function_address; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | size_t size_; |
| 83 | byte* buffer_; |
| 84 | }; |
| 85 | #endif |
| 86 | |
| 87 | // Generate a function with the following prototype: |
| 88 | // int32_t abs(int32_t x) |
| 89 | // |
| 90 | // The generated function computes the absolute value of an integer. |
| 91 | void GenerateAbs(MacroAssembler* masm); |
| 92 | |
| 93 | // Generate a function with the following prototype: |
| 94 | // uint32_t demo_function(uint32_t x) |
| 95 | // |
Pierre Langlois | d9dc46e | 2016-06-06 15:20:34 +0100 | [diff] [blame] | 96 | // This is the example used in doc/getting-started-aarch32.md |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 97 | void GenerateDemo(MacroAssembler* masm); |
| 98 | |
Alexandre Rames | 9c4e289 | 2016-09-22 11:06:28 +0100 | [diff] [blame] | 99 | #endif // VIXL_EXAMPLE_EXAMPLES_H_ |