ObjFW  Check-in [15eed89995]

Overview
Comment:invoke-x86_64.m: Change __int128 handling

When using GCC (and thus conforming to the ABI), do not pass the low
half of an __int128 in a register while passing the high of an __int128
on the stack.

For Clang, just handle it as two longs.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 15eed899955ea3c59bebdf8d7e4c72743ee7a284540c7f44faeb0eeae89991a0
User & Date: js on 2017-09-17 02:20:44
Other Links: manifest | tags
Context
2017-09-17
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
2017-09-16
20:37
OFInvocationTests: Slightly change __int128 test check-in: c2941ac553 user: js tags: trunk
Changes

Modified src/invocation/invoke-x86_64.m from [1d2004b374] to [2253358a2d].

21
22
23
24
25
26
27


28
29
30
31
32
33
34
#include <xmmintrin.h>

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

#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"



#define NUM_GPR_IN 6
#define NUM_GPR_OUT 2
#define NUM_SSE_IN 8
#define NUM_X87_OUT 2

enum {







>
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <xmmintrin.h>

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

#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

#import "macros.h"

#define NUM_GPR_IN 6
#define NUM_GPR_OUT 2
#define NUM_SSE_IN 8
#define NUM_X87_OUT 2

enum {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	uint64_t stack_size;
	uint64_t stack[];
};

extern void of_invocation_call(struct call_context *);

static void
pushGPR(struct call_context **context, size_t *currentGPR, uint64_t value)
{
	struct call_context *newContext;

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







|







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	uint64_t stack_size;
	uint64_t stack[];
};

extern void of_invocation_call(struct call_context *);

static void
pushGPR(struct call_context **context, uint_fast8_t *currentGPR, uint64_t value)
{
	struct call_context *newContext;

	if (*currentGPR < NUM_GPR_IN) {
		(*context)->gpr[(*currentGPR)++] = value;
		return;
	}
70
71
72
73
74
75
76
77

78
79
80
81
82
83
84

	newContext->stack[newContext->stack_size] = value;
	newContext->stack_size++;
	*context = newContext;
}

static void
pushDouble(struct call_context **context, size_t *currentSSE, double value)

{
	struct call_context *newContext;

	if (*currentSSE < NUM_SSE_IN) {
		(*context)->sse[(*currentSSE)++] = (__m128)_mm_set_sd(value);
		(*context)->num_sse_used++;
		return;







|
>







72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

	newContext->stack[newContext->stack_size] = value;
	newContext->stack_size++;
	*context = newContext;
}

static void
pushDouble(struct call_context **context, uint_fast8_t *currentSSE,
    double value)
{
	struct call_context *newContext;

	if (*currentSSE < NUM_SSE_IN) {
		(*context)->sse[(*currentSSE)++] = (__m128)_mm_set_sd(value);
		(*context)->num_sse_used++;
		return;
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
	}

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

#ifndef __clang__
static void
alignStack(struct call_context **context, size_t alignment)

{
	size_t stackSize = (*context)->stack_size;
	struct call_context *newContext;


	if (stackSize % alignment == 0)


		return;

	stackSize += alignment - stackSize % alignment;


	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);


	newContext->stack_size = stackSize;
	*context = newContext;
}
#endif

void
of_invocation_invoke(OFInvocation *invocation)
{
	OFMethodSignature *methodSignature = [invocation methodSignature];
	size_t numberOfArguments = [methodSignature numberOfArguments];
	struct call_context *context;
	const char *typeEncoding;
	size_t currentGPR = 0, currentSSE = 0;

	if ((context = calloc(sizeof(*context), 1)) == NULL)
		@throw [OFOutOfMemoryException exception];

	for (size_t i = 0; i < numberOfArguments; i++) {
		typeEncoding = [methodSignature argumentTypeAtIndex: i];








|

|
>

<

>

|
>
>

|
|
>










>
>












|







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
	}

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

#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;
	}

	stackSize = OF_ROUND_UP_POW2(2, (*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);
	newContext->stack[stackSize - 2] = low;
	newContext->stack[stackSize - 1] = high;
	newContext->stack_size = stackSize;
	*context = newContext;
}
#endif

void
of_invocation_invoke(OFInvocation *invocation)
{
	OFMethodSignature *methodSignature = [invocation methodSignature];
	size_t numberOfArguments = [methodSignature numberOfArguments];
	struct call_context *context;
	const char *typeEncoding;
	uint_fast8_t currentGPR = 0, currentSSE = 0;

	if ((context = calloc(sizeof(*context), 1)) == NULL)
		@throw [OFOutOfMemoryException exception];

	for (size_t i = 0; i < numberOfArguments; i++) {
		typeEncoding = [methodSignature argumentTypeAtIndex: i];

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
		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':;


			uint64_t int128Tmp[2];
			[invocation getArgument: int128Tmp
					atIndex: i];
# ifndef __clang__
			/*
			 * Clang violates the x86_64 ABI and does not properly
			 * align __int128 on the stack.
			 */
			alignStack(&context, 2);

# endif
			pushGPR(&context, &currentGPR, int128Tmp[0]);
			pushGPR(&context, &currentGPR, int128Tmp[1]);

			break;
#endif
		case 'f':;
			float floatTmp;
			[invocation getArgument: &floatTmp
					atIndex: i];
			pushDouble(&context, &currentSSE, floatTmp);







>
>
|
|


<
<
<
<
|
>
|
|
|
>







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
		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;
			[invocation getArgument: &floatTmp
					atIndex: i];
			pushDouble(&context, &currentSSE, floatTmp);

Modified tests/OFInvocationTests.m from [2ee831b079] to [3e2e21ca4d].

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
	    d12 + d13 + d14 + d15 + d16) / 16;
}

#ifdef __SIZEOF_INT128__
__extension__
- (__int128)invocationTestMethod5: (int)i1
				 : (__int128)i2
				 : (int)i3
				 : (__int128)i4
				 : (int)i5
				 : (__int128)i6
				 : (__int128)i7
				 : (__int128)i8
				 : (__int128)i9
				 : (__int128)i10
				 : (__int128)i11
				 : (__int128)i12
				 : (__int128)i13
				 : (__int128)i14
				 : (__int128)i15
				 : (__int128)i16
{
	__int128 mask = (__int128)0xFFFFFFFFFFFFFFFF << 64;

	OF_ENSURE(i1 == 1);
	OF_ENSURE(i2 == mask + 2);
	OF_ENSURE(i3 == 3);
	OF_ENSURE(i4 == mask + 4);
	OF_ENSURE(i5 == 5);
	OF_ENSURE(i6 == mask + 6);
	OF_ENSURE(i7 == mask + 7);
	OF_ENSURE(i8 == mask + 8);
	OF_ENSURE(i9 == mask + 9);
	OF_ENSURE(i10 == mask + 10);
	OF_ENSURE(i11 == mask + 11);
	OF_ENSURE(i12 == mask + 12);
	OF_ENSURE(i13 == mask + 13);
	OF_ENSURE(i14 == mask + 14);
	OF_ENSURE(i15 == mask + 15);
	OF_ENSURE(i16 == mask + 16);

	return ((i1 + (int)i2 + i3 + (int)i4 + i5 + (int)i6 + (int)i7 +
	    (int)i8 + (int)i9 + (int)i10 + (int)i11 + (int)i12 + (int)i13 +
	    (int)i14 + (int)i15 + (int)i16) / 16) + mask;
}
#endif

- (void)invocationTests
{







|


















|














|







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
	    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
				 : (__int128)i9
				 : (__int128)i10
				 : (__int128)i11
				 : (__int128)i12
				 : (__int128)i13
				 : (__int128)i14
				 : (__int128)i15
				 : (__int128)i16
{
	__int128 mask = (__int128)0xFFFFFFFFFFFFFFFF << 64;

	OF_ENSURE(i1 == 1);
	OF_ENSURE(i2 == mask + 2);
	OF_ENSURE(i3 == mask + 3);
	OF_ENSURE(i4 == mask + 4);
	OF_ENSURE(i5 == 5);
	OF_ENSURE(i6 == mask + 6);
	OF_ENSURE(i7 == mask + 7);
	OF_ENSURE(i8 == mask + 8);
	OF_ENSURE(i9 == mask + 9);
	OF_ENSURE(i10 == mask + 10);
	OF_ENSURE(i11 == mask + 11);
	OF_ENSURE(i12 == mask + 12);
	OF_ENSURE(i13 == mask + 13);
	OF_ENSURE(i14 == mask + 14);
	OF_ENSURE(i15 == mask + 15);
	OF_ENSURE(i16 == mask + 16);

	return ((i1 + (int)i2 + (int)i3 + (int)i4 + i5 + (int)i6 + (int)i7 +
	    (int)i8 + (int)i9 + (int)i10 + (int)i11 + (int)i12 + (int)i13 +
	    (int)i14 + (int)i15 + (int)i16) / 16) + mask;
}
#endif

- (void)invocationTests
{
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
			atIndex: 1];

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

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








|







277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
			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];
	}