ObjFW  Check-in [b35074d74a]

Overview
Comment:invoke-x86_64.m: Support complex {float,double}
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b35074d74abbb50c747707f74640252b79f3ad141ccbd7a4dcf55fe324f331e5
User & Date: js on 2017-09-17 03:18:26
Other Links: manifest | tags
Context
2017-09-17
03:27
call-x86_64-elf.S: Fix use of overwritten %rdi check-in: 9adb8518bb user: js tags: trunk
03:18
invoke-x86_64.m: Support complex {float,double} check-in: b35074d74a user: js tags: trunk
02:20
invoke-x86_64.m: Change __int128 handling check-in: 15eed89995 user: js tags: trunk
Changes

Modified src/invocation/invoke-x86_64.m from [2253358a2d] to [567ef57186].

94
95
96
97
98
99
100































101
102
103
104
105
106
107
		    sizeof(**context) + ((*context)->stack_size + 1) * 8];
	}

	memcpy(&newContext->stack[newContext->stack_size], &value, 8);
	newContext->stack_size++;
	*context = newContext;
}
































static void
pushLongDouble(struct call_context **context, long double value)
{
	struct call_context *newContext;

	if ((newContext = realloc(*context,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
		    sizeof(**context) + ((*context)->stack_size + 1) * 8];
	}

	memcpy(&newContext->stack[newContext->stack_size], &value, 8);
	newContext->stack_size++;
	*context = newContext;
}

static void
pushQuad(struct call_context **context, uint_fast8_t *currentSSE,
    double low, double high)
{
	size_t stackSize;
	struct call_context *newContext;

	if (*currentSSE + 1 < NUM_SSE_IN) {
		(*context)->sse[(*currentSSE)++] = (__m128)_mm_set_sd(low);
		(*context)->sse[(*currentSSE)++] = (__m128)_mm_set_sd(high);
		(*context)->num_sse_used += 2;
		return;
	}

	stackSize = (*context)->stack_size + 2;

	if ((newContext = realloc(*context,
	    sizeof(**context) + stackSize * 8)) == NULL) {
		free(*context);
		@throw [OFOutOfMemoryException exceptionWithRequestedSize:
		    sizeof(**context) + stackSize * 8];
	}

	memset(&newContext->stack[newContext->stack_size], '\0',
	    (stackSize - newContext->stack_size) * 8);
	memcpy(&newContext->stack[stackSize - 2], &low, 8);
	memcpy(&newContext->stack[stackSize - 1], &high, 8);
	newContext->stack_size = stackSize;
	*context = newContext;
}

static void
pushLongDouble(struct call_context **context, long double value)
{
	struct call_context *newContext;

	if ((newContext = realloc(*context,
117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132
}

#if defined(__SIZEOF_INT128__) && !defined(__clang__)
static void
pushInt128(struct call_context **context, uint_fast8_t *currentGPR,
    uint64_t low, uint64_t high)
{
	struct call_context *newContext;
	size_t stackSize;


	if (*currentGPR + 1 < NUM_GPR_IN) {
		(*context)->gpr[(*currentGPR)++] = low;
		(*context)->gpr[(*currentGPR)++] = high;
		return;
	}








<

>







148
149
150
151
152
153
154

155
156
157
158
159
160
161
162
163
}

#if defined(__SIZEOF_INT128__) && !defined(__clang__)
static void
pushInt128(struct call_context **context, uint_fast8_t *currentGPR,
    uint64_t low, uint64_t high)
{

	size_t stackSize;
	struct call_context *newContext;

	if (*currentGPR + 1 < NUM_GPR_IN) {
		(*context)->gpr[(*currentGPR)++] = low;
		(*context)->gpr[(*currentGPR)++] = high;
		return;
	}

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
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)
		CASE_GPR('l', long)
		CASE_GPR('L', unsigned long)
		CASE_GPR('q', long long)
		CASE_GPR('Q', unsigned long long)






#ifdef __SIZEOF_INT128__
		case 't':
		case 'T':;
			struct {
				uint64_t low, high;
			} int128Tmp;
			[invocation getArgument: &int128Tmp
					atIndex: i];
# ifndef __clang__
			pushInt128(&context, &currentGPR,
			    int128Tmp.low, int128Tmp.high);
# else

			pushGPR(&context, &currentGPR, int128Tmp.low);
			pushGPR(&context, &currentGPR, int128Tmp.high);
# endif
			break;
#endif
		case 'f':;
			float floatTmp;







>
>
>
>
>
>












>







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
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)
		CASE_GPR('l', long)
		CASE_GPR('L', unsigned long)
		CASE_GPR('q', long long)
		CASE_GPR('Q', unsigned long long)
		CASE_GPR('B', _Bool)
		CASE_GPR('*', uintptr_t)
		CASE_GPR('@', uintptr_t)
		CASE_GPR('#', uintptr_t)
		CASE_GPR(':', uintptr_t)
		CASE_GPR('^', uintptr_t)
#ifdef __SIZEOF_INT128__
		case 't':
		case 'T':;
			struct {
				uint64_t low, high;
			} int128Tmp;
			[invocation getArgument: &int128Tmp
					atIndex: i];
# ifndef __clang__
			pushInt128(&context, &currentGPR,
			    int128Tmp.low, int128Tmp.high);
# else
			/* See https://bugs.llvm.org/show_bug.cgi?id=34646 */
			pushGPR(&context, &currentGPR, int128Tmp.low);
			pushGPR(&context, &currentGPR, int128Tmp.high);
# endif
			break;
#endif
		case 'f':;
			float floatTmp;
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
			break;
		case 'D':;
			long double longDoubleTmp;
			[invocation getArgument: &longDoubleTmp
					atIndex: i];
			pushLongDouble(&context, longDoubleTmp);
			break;
		CASE_GPR('B', _Bool)
		CASE_GPR('*', uintptr_t)
		CASE_GPR('@', uintptr_t)
		CASE_GPR('#', uintptr_t)






		CASE_GPR(':', uintptr_t)






		/* TODO: '[' */







		/* TODO: '{' */
		/* TODO: '(' */
		CASE_GPR('^', uintptr_t)
#ifndef __STDC_NO_COMPLEX__
		/* TODO: 'j' */
#endif
		default:
			free(context);
			@throw [OFInvalidFormatException exception];
#undef CASE_GPR
		}
	}








|
|
|
|
>
>
>
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
|
<
<
|
<







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
			break;
		case 'D':;
			long double longDoubleTmp;
			[invocation getArgument: &longDoubleTmp
					atIndex: i];
			pushLongDouble(&context, longDoubleTmp);
			break;
#ifndef __STDC_NO_COMPLEX__
		case 'j':
			switch (typeEncoding[1]) {
			case 'f':;
				double complexFloatTmp;
				[invocation getArgument: &complexFloatTmp
						atIndex: i];
				pushDouble(&context, &currentSSE,
				    complexFloatTmp);
				break;
			case 'd':;
				double complexDoubleTmp[2];
				[invocation getArgument: &complexDoubleTmp
						atIndex: i];
				pushQuad(&context, &currentSSE,
				    complexDoubleTmp[0], complexDoubleTmp[1]);
				break;
			/* TODO: 'D' */
			default:
				free(context);
				@throw [OFInvalidFormatException exception];
			}

			break;
#endif
		/* TODO: '[' */
		/* TODO: '{' */


		/* TODO: '(' */

		default:
			free(context);
			@throw [OFInvalidFormatException exception];
#undef CASE_GPR
		}
	}

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
	case 'I':
	case 's':
	case 'S':
	case 'l':
	case 'L':
	case 'q':
	case 'Q':
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
#endif
	case 'f':
	case 'd':
	case 'B':
	case '*':
	case '@':
	case '#':
	case ':':
	case '^':






		context->return_type = RETURN_TYPE_NORMAL;
		break;
	case 'D':
		context->return_type = RETURN_TYPE_X87;
		break;















	/* TODO: '[' */
	/* TODO: '{' */
	/* TODO: '(' */
#ifndef __STDC_NO_COMPLEX__
	/* TODO: 'j' */
#endif
	default:
		free(context);
		@throw [OFInvalidFormatException exception];
	}

	of_invocation_call(context);








<
<
<
<
<
<






>
>
>
>
>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
<
<







306
307
308
309
310
311
312






313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347



348
349
350
351
352
353
354
	case 'I':
	case 's':
	case 'S':
	case 'l':
	case 'L':
	case 'q':
	case 'Q':






	case 'B':
	case '*':
	case '@':
	case '#':
	case ':':
	case '^':
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
#endif
	case 'f':
	case 'd':
		context->return_type = RETURN_TYPE_NORMAL;
		break;
	case 'D':
		context->return_type = RETURN_TYPE_X87;
		break;
#ifndef __STDC_NO_COMPLEX__
	case 'j':
		switch (typeEncoding[1]) {
		case 'f':
		case 'd':
			context->return_type = RETURN_TYPE_NORMAL;
			break;
		/* TODO: 'D' */
		default:
			free(context);
			@throw [OFInvalidFormatException exception];
		}

		break;
#endif
	/* TODO: '[' */
	/* TODO: '{' */
	/* TODO: '(' */



	default:
		free(context);
		@throw [OFInvalidFormatException exception];
	}

	of_invocation_call(context);

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





330







331







332
333
334
335
336
337
338
339
340
341
342
343
344
345
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)
		CASE_GPR('l', long)
		CASE_GPR('L', unsigned long)
		CASE_GPR('q', long long)
		CASE_GPR('Q', unsigned long long)






#ifdef __SIZEOF_INT128__
		case 't':
		case 'T':;
			[invocation setReturnValue: &context->gpr[NUM_GPR_IN]];
			break;
#endif
		case 'f':;
			float floatTmp;
			_mm_store_ss(&floatTmp, context->sse[0]);
			[invocation setReturnValue: &floatTmp];
			break;
		case 'd':;
			double doubleTmp;
			_mm_store_sd(&doubleTmp, (__m128d)context->sse[0]);
			[invocation setReturnValue: &doubleTmp];
			break;
		case 'D':
			[invocation setReturnValue: &context->x87[0]];
			break;
		CASE_GPR('B', _Bool)
		CASE_GPR('*', uintptr_t)
		CASE_GPR('@', uintptr_t)
		CASE_GPR('#', uintptr_t)





		CASE_GPR(':', uintptr_t)







		/* TODO: '[' */







		/* TODO: '{' */
		/* TODO: '(' */
		CASE_GPR('^', uintptr_t)
#ifndef __STDC_NO_COMPLEX__
		/* TODO: 'j' */
#endif
		default:
			free(context);
			@throw [OFInvalidFormatException exception];
#undef CASE_GPR
	}

	free(context);
}







>
>
>
>
>
>



















|
|
|
|
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
|
<
<
|
<








366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424


425

426
427
428
429
430
431
432
433
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)
		CASE_GPR('l', long)
		CASE_GPR('L', unsigned long)
		CASE_GPR('q', long long)
		CASE_GPR('Q', unsigned long long)
		CASE_GPR('B', _Bool)
		CASE_GPR('*', uintptr_t)
		CASE_GPR('@', uintptr_t)
		CASE_GPR('#', uintptr_t)
		CASE_GPR(':', uintptr_t)
		CASE_GPR('^', uintptr_t)
#ifdef __SIZEOF_INT128__
		case 't':
		case 'T':;
			[invocation setReturnValue: &context->gpr[NUM_GPR_IN]];
			break;
#endif
		case 'f':;
			float floatTmp;
			_mm_store_ss(&floatTmp, context->sse[0]);
			[invocation setReturnValue: &floatTmp];
			break;
		case 'd':;
			double doubleTmp;
			_mm_store_sd(&doubleTmp, (__m128d)context->sse[0]);
			[invocation setReturnValue: &doubleTmp];
			break;
		case 'D':
			[invocation setReturnValue: &context->x87[0]];
			break;
#ifndef __STDC_NO_COMPLEX__
		case 'j':
			switch (typeEncoding[1]) {
			case 'f':;
				double complexFloatTmp;
				_mm_store_sd(&complexFloatTmp,
				    (__m128d)context->sse[0]);
				[invocation setReturnValue: &complexFloatTmp];
				break;
			case 'd':;
				double complexDoubleTmp[2];
				_mm_store_sd(&complexDoubleTmp[0],
				    (__m128d)context->sse[0]);
				_mm_store_sd(&complexDoubleTmp[1],
				    (__m128d)context->sse[1]);
				[invocation setReturnValue: &complexDoubleTmp];
				break;
			/* TODO: 'D' */
			default:
				free(context);
				@throw [OFInvalidFormatException exception];
			}

			break;
#endif
		/* TODO: '[' */
		/* TODO: '{' */


		/* TODO: '(' */

		default:
			free(context);
			@throw [OFInvalidFormatException exception];
#undef CASE_GPR
	}

	free(context);
}

Modified tests/OFInvocationTests.m from [3e2e21ca4d] to [86c5f0ef13].

13
14
15
16
17
18
19




20
21
22
23
24
25
26
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>





#import "OFInvocation.h"
#import "OFMethodSignature.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"








>
>
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>

#ifndef __STDC_NO_COMPLEX__
# include <complex.h>
#endif

#import "OFInvocation.h"
#import "OFMethodSignature.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

98
99
100
101
102
103
104
105








































106
107
108
109
110
111
112
113
114
115
				    : (long double)d14
				    : (long double)d15
				    : (long double)d16
{
	return (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10 + d11 +
	    d12 + d13 + d14 + d15 + d16) / 16;
}









































#ifdef __SIZEOF_INT128__
__extension__
- (__int128)invocationTestMethod5: (int)i1
				 : (__int128)i2
				 : (__int128)i3
				 : (__int128)i4
				 : (int)i5
				 : (__int128)i6
				 : (__int128)i7
				 : (__int128)i8








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|







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
				    : (long double)d14
				    : (long double)d15
				    : (long double)d16
{
	return (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10 + d11 +
	    d12 + d13 + d14 + d15 + d16) / 16;
}

#ifndef __STDC_NO_COMPLEX__
- (complex double)invocationTestMethod5: (complex float)c1
				       : (complex double)c2
				       : (complex float)c3
				       : (complex double)c4
				       : (complex float)c5
				       : (complex double)c6
				       : (complex float)c7
				       : (complex double)c8
				       : (complex float)c9
				       : (complex double)c10
				       : (complex float)c11
				       : (complex double)c12
				       : (complex float)c13
				       : (complex double)c14
				       : (complex float)c15
				       : (complex double)c16
{
	OF_ENSURE(creal(c1) == 1.0 && cimag(c1) == 0.5);
	OF_ENSURE(creal(c2) == 2.0 && cimag(c2) == 1.0);
	OF_ENSURE(creal(c3) == 3.0 && cimag(c3) == 1.5);
	OF_ENSURE(creal(c4) == 4.0 && cimag(c4) == 2.0);
	OF_ENSURE(creal(c5) == 5.0 && cimag(c5) == 2.5);
	OF_ENSURE(creal(c6) == 6.0 && cimag(c6) == 3.0);
	OF_ENSURE(creal(c7) == 7.0 && cimag(c7) == 3.5);
	OF_ENSURE(creal(c8) == 8.0 && cimag(c8) == 4.0);
	OF_ENSURE(creal(c9) == 9.0 && cimag(c9) == 4.5);
	OF_ENSURE(creal(c10) == 10.0 && cimag(c10) == 5.0);
	OF_ENSURE(creal(c11) == 11.0 && cimag(c11) == 5.5);
	OF_ENSURE(creal(c12) == 12.0 && cimag(c12) == 6.0);
	OF_ENSURE(creal(c13) == 13.0 && cimag(c13) == 6.5);
	OF_ENSURE(creal(c14) == 14.0 && cimag(c14) == 7.0);
	OF_ENSURE(creal(c15) == 15.0 && cimag(c15) == 7.5);
	OF_ENSURE(creal(c16) == 16.0 && cimag(c16) == 8.0);

	return (c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11 +
	    c12 + c13 + c14 + c15 + c16) / 16;
}
#endif

#ifdef __SIZEOF_INT128__
__extension__
- (__int128)invocationTestMethod6: (int)i1
				 : (__int128)i2
				 : (__int128)i3
				 : (__int128)i4
				 : (int)i5
				 : (__int128)i6
				 : (__int128)i7
				 : (__int128)i8
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

		long double longDoubleResult;
		TEST(@"-[invoke] #3", R([invocation invoke]) &&
		    R([invocation getReturnValue: &longDoubleResult]) &&
		    longDoubleResult == 8.5)
	}

# ifdef __SIZEOF_INT128__
	/* -[invoke] #4 */
	selector = @selector(invocationTestMethod5::::::::::::::::);
	invocation = [OFInvocation invocationWithMethodSignature:
	    [self methodSignatureForSelector: selector]];






























	[invocation setArgument: &self
			atIndex: 0];
	[invocation setArgument: &selector
			atIndex: 1];

	for (int i = 1; i <= 16; i++) {
		__extension__ __int128 i128 = 0xFFFFFFFFFFFFFFFF;
		i128 <<= 64;
		i128 |= i;

		if (i == 1 || i == 5)
			[invocation setArgument: &i
					atIndex: i + 1];
		else
			[invocation setArgument: &i128
					atIndex: i + 1];
	}

	__extension__ __int128 int128Result;
	TEST(@"-[invoke] #4", R([invocation invoke]) &&
	    R([invocation getReturnValue: &int128Result]) &&
	    int128Result == __extension__ ((__int128)0xFFFFFFFFFFFFFFFF << 64) +
	    8)
# endif
#endif

	[pool drain];
}
@end







|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



















|









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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

		long double longDoubleResult;
		TEST(@"-[invoke] #3", R([invocation invoke]) &&
		    R([invocation getReturnValue: &longDoubleResult]) &&
		    longDoubleResult == 8.5)
	}

# ifndef __STDC_NO_COMPLEX__
	/* -[invoke] #4 */
	selector = @selector(invocationTestMethod5::::::::::::::::);
	invocation = [OFInvocation invocationWithMethodSignature:
	    [self methodSignatureForSelector: selector]];

	[invocation setArgument: &self
			atIndex: 0];
	[invocation setArgument: &selector
			atIndex: 1];

	for (int i = 1; i <= 16; i++) {
		complex float cf = i + 0.5 * i * I;
		complex double cd = i + 0.5 * i * I;

		if (i & 1)
			[invocation setArgument: &cf
					atIndex: i + 1];
		else
			[invocation setArgument: &cd
					atIndex: i + 1];
	}

	complex double complexDoubleResult;
	TEST(@"-[invoke] #4", R([invocation invoke]) &&
	    R([invocation getReturnValue: &complexDoubleResult]) &&
	    complexDoubleResult == 8.5 + 4.25 * I)
# endif

# ifdef __SIZEOF_INT128__
	/* -[invoke] #5 */
	selector = @selector(invocationTestMethod6::::::::::::::::);
	invocation = [OFInvocation invocationWithMethodSignature:
	    [self methodSignatureForSelector: selector]];

	[invocation setArgument: &self
			atIndex: 0];
	[invocation setArgument: &selector
			atIndex: 1];

	for (int i = 1; i <= 16; i++) {
		__extension__ __int128 i128 = 0xFFFFFFFFFFFFFFFF;
		i128 <<= 64;
		i128 |= i;

		if (i == 1 || i == 5)
			[invocation setArgument: &i
					atIndex: i + 1];
		else
			[invocation setArgument: &i128
					atIndex: i + 1];
	}

	__extension__ __int128 int128Result;
	TEST(@"-[invoke] #5", R([invocation invoke]) &&
	    R([invocation getReturnValue: &int128Result]) &&
	    int128Result == __extension__ ((__int128)0xFFFFFFFFFFFFFFFF << 64) +
	    8)
# endif
#endif

	[pool drain];
}
@end