File: as_callfunc_arm64.cpp

package info (click to toggle)
supertuxkart 1.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 767,580 kB
  • sloc: cpp: 412,075; xml: 106,334; ansic: 83,792; asm: 1,559; python: 1,403; sh: 1,366; objc: 452; makefile: 333; javascript: 23; awk: 20
file content (329 lines) | stat: -rw-r--r-- 11,889 bytes parent folder | download | duplicates (4)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
   AngelCode Scripting Library
   Copyright (c) 2020-2021 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/


//
// as_callfunc_arm64.cpp
//
// These functions handle the actual calling of system functions on the arm64 platform
//
// Written by Max Waine in July 2020, based on as_callfunc_arm.cpp
//


#include "as_config.h"

#ifndef AS_MAX_PORTABILITY
#ifdef AS_ARM64

#include "as_callfunc.h"
#include "as_scriptengine.h"
#include "as_texts.h"
#include "as_tokendef.h"
#include "as_context.h"

// ARM64 targets use has no software floating-point ABI, it's all hardware (or totally disabled)

#define HFA_RET_REGISTERS   4 // s0-s3/d0-d3
#define GP_ARG_REGISTERS    8 // x0-x7
#define FLOAT_ARG_REGISTERS 8 // v0-v7

BEGIN_AS_NAMESPACE

// x0-7:   Argument registers (pass params or return results. OK as volatile local variables)
// x8:     Indirect result register (e.g. address of large returned struct)
// x9-15:  Volatile local variable registers
// x16-17: Intra-procedure-call temporary registers
// x18:    Platform register (reserved for use of platform ABIs)
// x19-29: Non-volatile variable registers (must be saved and restored if modified)
// x29:    Frame pointer register
// x30:    Link register (where to return to)

extern "C" void GetHFAReturnDouble(asQWORD *out1, asQWORD *out2, asQWORD returnSize);
extern "C" void GetHFAReturnFloat(asQWORD *out1, asQWORD *out2, asQWORD returnSize);

extern "C" asQWORD CallARM64RetInMemory(
	const asQWORD *gpRegArgs,    asQWORD numGPRegArgs,
	const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
	const asQWORD *stackArgs,    asQWORD numStackArgs,
	void *retPointer,            asFUNCTION_t func
);
extern "C" double CallARM64Double(
	const asQWORD *gpRegArgs,    asQWORD numGPRegArgs,
	const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
	const asQWORD *stackArgs,    asQWORD numStackArgs,
	asFUNCTION_t func
);
extern "C" float CallARM64Float(
	const asQWORD *gpRegArgs,    asQWORD numGPRegArgs,
	const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
	const asQWORD *stackArgs,    asQWORD numStackArgs,
	asFUNCTION_t func
);
extern "C" asQWORD CallARM64(
	const asQWORD *gpRegArgs,    asQWORD numGPRegArgs,
	const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
	const asQWORD *stackArgs,    asQWORD numStackArgs,
	asFUNCTION_t func
);
extern "C" asQWORD CallARM64Ret128(
	const asQWORD *gpRegArgs,    asQWORD numGPRegArgs,
	const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
	const asQWORD *stackArgs,    asQWORD numStackArgs,
	asQWORD *higherQWORD,        asFUNCTION_t func
);

//
// If it's possible to fit in registers,
// there may not be enough float register space even if true is returned
//
static inline bool IsRegisterHFA(const asCDataType &type)
{
	const asCTypeInfo *const typeInfo = type.GetTypeInfo();

	if( typeInfo == nullptr ||
		(typeInfo->flags & asOBJ_APP_CLASS_ALLFLOATS) == 0 ||
		type.IsObjectHandle() || type.IsReference() )
		return false;

	const bool doubles = (typeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0;
	const int maxAllowedSize = doubles ? sizeof(double) * HFA_RET_REGISTERS : sizeof(float) * HFA_RET_REGISTERS;

	return type.GetSizeInMemoryBytes() <= maxAllowedSize;
}

//
// If it's possible to fit it in registers,
// if true is returned there is enough space to fit
//
static inline bool IsRegisterHFAParameter(const asCDataType &type, const asQWORD numFloatRegArgs)
{
	if( !IsRegisterHFA(type) )
		return false;

	const bool doubles = (type.GetTypeInfo()->flags & asOBJ_APP_CLASS_ALIGN8) != 0;
	const int registersUsed = type.GetSizeInMemoryDWords() / (doubles ? sizeof(double) : sizeof(float));

	return numFloatRegArgs + registersUsed <= FLOAT_ARG_REGISTERS;
}

asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2, void *secondObject)
{
	asCScriptEngine *engine = context->m_engine;
	const asSSystemFunctionInterface *const sysFunc = descr->sysFuncIntf;
	const asCDataType &retType = descr->returnType;
	const asCTypeInfo *const retTypeInfo = retType.GetTypeInfo();
	asFUNCTION_t func = sysFunc->func;
	int callConv = sysFunc->callConv;
	asQWORD       retQW     = 0;

	asQWORD       gpRegArgs[GP_ARG_REGISTERS];
	asQWORD       floatRegArgs[FLOAT_ARG_REGISTERS];
	asQWORD       stackArgs[64]; // It's how many x64 users can have
	asQWORD       numGPRegArgs    = 0;
	asQWORD       numFloatRegArgs = 0;
	asQWORD       numStackArgs    = 0;

	asFUNCTION_t *vftable;

	// Optimization to avoid check 12 values (all ICC_ that contains THISCALL)
	if( (callConv >= ICC_THISCALL && callConv <= ICC_VIRTUAL_THISCALL_RETURNINMEM) ||
		(callConv >= ICC_THISCALL_OBJLAST && callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
	{
		// Add the object pointer as the first parameter
		gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
	}

	if( callConv == ICC_CDECL_OBJFIRST || callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
	{
		// Add the object pointer as the first parameter
		gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
	}
	else if( callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM ||
		callConv == ICC_VIRTUAL_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
	{
		// Add the object pointer as the first parameter
		gpRegArgs[numGPRegArgs++] = (asQWORD)secondObject;
	}

	if( callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
	callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
	{
		// Get virtual function table from the object pointer
		vftable = *(asFUNCTION_t**)obj;
		func = vftable[FuncPtrToUInt(func)/sizeof(void*)];
	}

	asUINT argsPos = 0;
	for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
	{
		const asCDataType &parmType = descr->parameterTypes[n];
		const asCTypeInfo *const parmTypeInfo = parmType.GetTypeInfo();

		if( parmType.IsObject() && !parmType.IsObjectHandle() && !parmType.IsReference() )
		{
			const asUINT parmDWords = parmType.GetSizeInMemoryDWords();
			const asUINT parmQWords = (parmDWords >> 1) + (parmDWords & 1);

			const bool passedAsPointer = parmQWords <= 2;
			const bool fitsInRegisters = passedAsPointer ? (numGPRegArgs < GP_ARG_REGISTERS) : (numGPRegArgs + parmQWords <= GP_ARG_REGISTERS);
			asQWORD *const argsArray = fitsInRegisters ? gpRegArgs : stackArgs;
			asQWORD &numArgs = fitsInRegisters ? numGPRegArgs : numStackArgs;

			if( (parmTypeInfo->flags & COMPLEX_MASK) )
			{
				argsArray[numArgs++] = *(asQWORD*)&args[argsPos];
				argsPos += AS_PTR_SIZE;
			}
			else if( IsRegisterHFAParameter(parmType, numFloatRegArgs) )
			{
				if( (parmTypeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0 )
				{
					const asQWORD *const contents = *(asQWORD**)&args[argsPos];
					for( asUINT i = 0; i < parmQWords; i++ )
						floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&contents[i];
				}
				else
				{
					const asDWORD *const contents = *(asDWORD**)&args[argsPos];
					for( asUINT i = 0; i < parmDWords; i++ )
						floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&contents[i];
				}
				engine->CallFree(*(char**)(args+argsPos));
				argsPos += AS_PTR_SIZE;
			}
			else
			{
				// Copy the object's memory to the buffer
				memcpy(&argsArray[numArgs], *(void**)(args+argsPos), parmType.GetSizeInMemoryBytes());

				// Delete the original memory
				engine->CallFree(*(char**)(args+argsPos));
				argsPos += AS_PTR_SIZE;
				numArgs += parmQWords;
			}
		}
		else if( parmType.IsFloatType() && !parmType.IsReference() )
		{
			if( numFloatRegArgs >= FLOAT_ARG_REGISTERS )
				stackArgs[numStackArgs++] = args[argsPos];
			else
				floatRegArgs[numFloatRegArgs++] = args[argsPos];
			argsPos++;
		}
		else if( parmType.IsDoubleType() && !parmType.IsReference() )
		{
			if( numFloatRegArgs >= FLOAT_ARG_REGISTERS )
				stackArgs[numStackArgs++] = *(asQWORD*)&args[argsPos];
			else
				floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&args[argsPos];
			argsPos += 2;
		}
		else
		{
			// Copy the value directly
			const asUINT parmDWords = parmType.GetSizeOnStackDWords();
			const asUINT parmQWords = (parmDWords >> 1) + (parmDWords & 1);

			const bool fitsInRegisters = numGPRegArgs + parmQWords <= GP_ARG_REGISTERS;
			asQWORD *const argsArray = fitsInRegisters ? gpRegArgs : stackArgs;
			asQWORD &numArgs = fitsInRegisters ? numGPRegArgs : numStackArgs;

			memcpy(&argsArray[numArgs], (void*)(args+argsPos), parmDWords * 4);
			argsPos += parmDWords;
			numArgs += parmQWords;
		}
	}

	if( callConv == ICC_CDECL_OBJLAST || callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
	{
		// Add the object pointer as the last parameter
		if( numGPRegArgs < GP_ARG_REGISTERS )
			gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
		else
			stackArgs[numStackArgs++] = (asQWORD)obj;
	}
	else if( callConv == ICC_THISCALL_OBJLAST || callConv == ICC_THISCALL_OBJLAST_RETURNINMEM ||
		callConv == ICC_VIRTUAL_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
	{
		// Add the object pointer as the last parameter
		if( numGPRegArgs < GP_ARG_REGISTERS )
			gpRegArgs[numGPRegArgs++] = (asQWORD)secondObject;
		else
			stackArgs[numStackArgs++] = (asQWORD)secondObject;
	}

	if( IsRegisterHFA(retType) && !(retTypeInfo->flags & COMPLEX_MASK) )
	{
		// This is to deal with HFAs (Homogeneous Floating-point Aggregates):
		// ARM64 will place all-float composite types (of equal precision)
		// with <= 4 members in the float return registers

		const int structSize = retType.GetSizeInMemoryBytes();

		CallARM64(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
		if( (retTypeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0 )
		{
			if( structSize <= sizeof(double) * 2 )
				GetHFAReturnDouble(&retQW, &retQW2, structSize);
			else
				GetHFAReturnDouble((asQWORD*)retPointer, ((asQWORD*)retPointer) + 1, structSize);
		}
		else
			GetHFAReturnFloat(&retQW, &retQW2, structSize);
	}
	else if( sysFunc->hostReturnFloat )
	{
		if( sysFunc->hostReturnSize == 1 )
			*(float*)&retQW = CallARM64Float(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
		else
			*(double*)&retQW = CallARM64Double(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
	}
	else if( sysFunc->hostReturnInMemory )
		retQW = CallARM64RetInMemory(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, retPointer, func);
	else
	{
		if( retType.GetSizeInMemoryBytes() > sizeof(asQWORD) )
			retQW = CallARM64Ret128(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, &retQW2, func);
		else
			retQW = CallARM64(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
	}

	return retQW;
}

END_AS_NAMESPACE

#endif // AS_ARM64
#endif // AS_MAX_PORTABILITY