blob: 08d68507190be88972f5fcd4dc00b469ab82cbd2 [file] [log] [blame]
Alexandre Ramesd3832962016-07-04 15:03:43 +01001// 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
30#include <stdio.h>
31#include <stdint.h>
32#ifndef VIXL_INCLUDE_SIMULATOR
33#include <sys/mman.h>
34#endif
35#include <string>
36#include "aarch32/constants-aarch32.h"
37#include "aarch32/instructions-aarch32.h"
38#include "aarch32/macro-assembler-aarch32.h"
39
Pierre Langlois11000d02016-08-08 17:48:43 +010040using namespace vixl;
Alexandre Ramesd3832962016-07-04 15:03:43 +010041using namespace vixl::aarch32;
42
43#ifndef VIXL_INCLUDE_SIMULATOR
44class ExecutableMemory {
45 public:
46 ExecutableMemory(const byte* code_start, size_t size)
47 : size_(size),
48 buffer_(reinterpret_cast<byte*>(mmap(NULL,
49 size,
50 PROT_READ | PROT_WRITE | PROT_EXEC,
51 MAP_SHARED | MAP_ANONYMOUS,
52 -1,
53 0))) {
54 VIXL_ASSERT(reinterpret_cast<intptr_t>(buffer_) != -1);
55 memcpy(buffer_, code_start, size_);
56 __builtin___clear_cache(buffer_, buffer_ + size_);
57 }
58 ~ExecutableMemory() { munmap(buffer_, size_); }
59
60 template <typename T>
61 T GetEntryPoint(const Label& entry_point) const {
62 uint32_t location = entry_point.GetLocation();
Jacob Bramley10dae1a2016-07-27 09:45:13 +010063 if (entry_point.IsUsingT32()) location += 1;
Alexandre Ramesd3832962016-07-04 15:03:43 +010064 return GetOffsetAddress<T>(location);
65 }
66
67 protected:
68 template <typename T>
69 T GetOffsetAddress(ptrdiff_t offset) const {
70 VIXL_ASSERT((offset >= 0) && (offset <= size_));
71 T function_address;
72 byte* buffer_address = buffer_ + offset;
73 memcpy(&function_address, &buffer_address, sizeof(T));
74 return function_address;
75 }
76
77 private:
78 size_t size_;
79 byte* buffer_;
80};
81#endif
82
83// Generate a function with the following prototype:
84// int32_t abs(int32_t x)
85//
86// The generated function computes the absolute value of an integer.
87void GenerateAbs(MacroAssembler* masm);
88
89// Generate a function with the following prototype:
90// uint32_t demo_function(uint32_t x)
91//
92// This is the example used in doc/getting-started-a32.txt
93void GenerateDemo(MacroAssembler* masm);
94
95#endif /* !VIXL_EXAMPLE_EXAMPLES_H_ */