summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Include/WAVM/LLVMJIT/LLVMJIT.h
blob: 777c3b0c5788dd1c29ffad484c3ab71c58ef0452 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once

#include <memory>
#include <string>
#include <vector>
#include "WAVM/IR/Types.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/HashMap.h"
#include "WAVM/RuntimeABI/RuntimeABI.h"

// Forward declarations
namespace WAVM { namespace IR {
	struct Module;
	struct UntaggedValue;
	struct FeatureSpec;

	enum class CallingConvention;
}}
namespace WAVM { namespace Runtime {
	struct ContextRuntimeData;
	struct ExceptionType;
	struct Function;
	struct Instance;
}}

namespace WAVM { namespace LLVMJIT {

	struct TargetSpec
	{
		std::string triple;
		std::string cpu;
	};

	enum class TargetValidationResult
	{
		valid,
		invalidTargetSpec,
		unsupportedArchitecture,
		x86CPUDoesNotSupportSSE41,
		wavmDoesNotSupportSIMDOnArch,
		memory64Requires64bitTarget,
		table64Requires64bitTarget
	};

	WAVM_API TargetSpec getHostTargetSpec();

	WAVM_API TargetValidationResult validateTarget(const TargetSpec& targetSpec,
												   const IR::FeatureSpec& featureSpec);

	struct Version
	{
		Uptr llvmMajor;
		Uptr llvmMinor;
		Uptr llvmPatch;

		Uptr llvmjitVersion;
	};

	WAVM_API Version getVersion();

	// Compile a module to object code with the host target spec.
	// Cannot fail if validateTarget(targetSpec, irModule.featureSpec) == valid.
	WAVM_API std::vector<U8> compileModule(const IR::Module& irModule,
										   const TargetSpec& targetSpec);

	WAVM_API std::string emitLLVMIR(const IR::Module& irModule,
									const TargetSpec& targetSpec,
									bool optimize);

	WAVM_API std::string disassembleObject(const TargetSpec& targetSpec,
										   const std::vector<U8>& objectBytes);

	// An opaque type that can be used to reference a loaded JIT module.
	struct Module;

	//
	// Structs that are passed to loadModule to bind undefined symbols in object code to values.
	//

	struct InstanceBinding
	{
		Uptr id;
	};

	struct FunctionBinding
	{
		const void* code;
	};

	struct TableBinding
	{
		Uptr id;
	};

	struct MemoryBinding
	{
		Uptr id;
	};

	struct GlobalBinding
	{
		IR::GlobalType type;
		union
		{
			const IR::UntaggedValue* immutableValuePointer;
			Uptr mutableGlobalIndex;
		};
	};

	struct ExceptionTypeBinding
	{
		Uptr id;
	};

	// Loads a module from object code, and binds its undefined symbols to the provided bindings.
	WAVM_API std::shared_ptr<Module> loadModule(
		const std::vector<U8>& objectFileBytes,
		HashMap<std::string, FunctionBinding>&& wavmIntrinsicsExportMap,
		std::vector<IR::FunctionType>&& types,
		std::vector<FunctionBinding>&& functionImports,
		std::vector<TableBinding>&& tables,
		std::vector<MemoryBinding>&& memories,
		std::vector<GlobalBinding>&& globals,
		std::vector<ExceptionTypeBinding>&& exceptionTypes,
		InstanceBinding instance,
		Uptr tableReferenceBias,
		const std::vector<Runtime::FunctionMutableData*>& functionDefMutableDatas,
		const std::unordered_map<Uptr, Uptr>& importIndexToSelfDefinedFunctionIndex,
		std::string&& debugName);

	struct InstructionSource
	{
		Runtime::Function* function;
		Uptr instructionIndex;
	};

	// Finds the JIT function and instruction index at the given address. If no JIT function
	// contains the given address, returns an InstructionSourceInfo with function==nullptr.
	WAVM_API bool getInstructionSourceByAddress(Uptr address, InstructionSource& outSource);

	// Generates an invoke thunk for a specific function type.
	WAVM_API Runtime::InvokeThunkPointer getInvokeThunk(IR::FunctionType functionType);
}}