ObjFW  Diff

Differences From Artifact [e0c747a83e]:

To Artifact [f56f3831ec]:


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
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

#import "invoke-x86_64.h"

#import "macros.h"

struct call_context {
	uint64_t GPR[NUM_GPR_IN + NUM_GPR_OUT];
	__m128 SSE[NUM_SSE_INOUT];
	long double X87[NUM_X87_OUT];
	uint8_t numSSEUsed;
	uint8_t returnType;
	uint64_t stackSize;
	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;
	}

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

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

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

	if (*currentSSE < NUM_SSE_INOUT) {
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(value);
		(*context)->numSSEUsed++;
		return;
	}

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

	memcpy(&newContext->stack[newContext->stackSize], &value, 8);
	newContext->stackSize++;
	*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_INOUT) {
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(low);
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(high);
		(*context)->numSSEUsed += 2;
		return;
	}

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







|
|
|
|






|


|

|

|

















|


|

|


















|



|

|







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
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

#import "invoke-x86_64.h"

#import "macros.h"

struct CallContext {
	uint64_t GPR[numGPRIn + numGPROut];
	__m128 SSE[numSSEInOut];
	long double X87[numX87Out];
	uint8_t numSSEUsed;
	uint8_t returnType;
	uint64_t stackSize;
	uint64_t stack[];
};

extern void OFInvocationCall(struct CallContext *);

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

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

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

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

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

	if (*currentSSE < numSSEInOut) {
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(value);
		(*context)->numSSEUsed++;
		return;
	}

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

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

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

	if (*currentSSE + 1 < numSSEInOut) {
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(low);
		(*context)->SSE[(*currentSSE)++] = (__m128)_mm_set_sd(high);
		(*context)->numSSEUsed += 2;
		return;
	}

	stackSize = (*context)->stackSize + 2;
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
	memcpy(&newContext->stack[stackSize - 2], &low, 8);
	memcpy(&newContext->stack[stackSize - 1], &high, 8);
	newContext->stackSize = stackSize;
	*context = newContext;
}

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

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

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

static void
pushLongDoublePair(struct call_context **context, long double value[2])
{
	size_t stackSize;
	struct call_context *newContext;

	stackSize = OF_ROUND_UP_POW2(2UL, (*context)->stackSize) + 4;

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

	memset(&newContext->stack[newContext->stackSize], '\0',
	    (stackSize - newContext->stackSize) * 8);
	memcpy(&newContext->stack[stackSize - 4], value, 32);
	newContext->stackSize = stackSize;
	*context = newContext;
}

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

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

	stackSize = OF_ROUND_UP_POW2(2, (*context)->stackSize) + 2;








|

|














|


|

|

















|



|

|







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
	memcpy(&newContext->stack[stackSize - 2], &low, 8);
	memcpy(&newContext->stack[stackSize - 1], &high, 8);
	newContext->stackSize = stackSize;
	*context = newContext;
}

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

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

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

static void
pushLongDoublePair(struct CallContext **context, long double value[2])
{
	size_t stackSize;
	struct CallContext *newContext;

	stackSize = OFRoundUpToPowerOf2(2UL, (*context)->stackSize) + 4;

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

	memset(&newContext->stack[newContext->stackSize], '\0',
	    (stackSize - newContext->stackSize) * 8);
	memcpy(&newContext->stack[stackSize - 4], value, 32);
	newContext->stackSize = stackSize;
	*context = newContext;
}

#if defined(__SIZEOF_INT128__) && !defined(__clang__)
static void
pushInt128(struct CallContext **context, uint_fast8_t *currentGPR,
    uint64_t value[2])
{
	size_t stackSize;
	struct CallContext *newContext;

	if (*currentGPR + 1 < numGPRIn) {
		(*context)->GPR[(*currentGPR)++] = value[0];
		(*context)->GPR[(*currentGPR)++] = value[1];
		return;
	}

	stackSize = OF_ROUND_UP_POW2(2, (*context)->stackSize) + 2;

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	memcpy(&newContext->stack[stackSize - 2], value, 16);
	newContext->stackSize = 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++) {







|



|







185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	memcpy(&newContext->stack[stackSize - 2], value, 16);
	newContext->stackSize = stackSize;
	*context = newContext;
}
#endif

void
OFInvocationInvoke(OFInvocation *invocation)
{
	OFMethodSignature *methodSignature = invocation.methodSignature;
	size_t numberOfArguments = methodSignature.numberOfArguments;
	struct CallContext *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++) {
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
376
377
378
379
380
381
382
383
384
385
	case '^':
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
#endif
	case 'f':
	case 'd':
		context->returnType = RETURN_TYPE_NORMAL;
		break;
	case 'D':
		context->returnType = RETURN_TYPE_X87;
		break;
	case 'j':
		switch (typeEncoding[1]) {
		case 'f':
		case 'd':
			context->returnType = RETURN_TYPE_NORMAL;
			break;
		case 'D':
			context->returnType = RETURN_TYPE_COMPLEX_X87;
			break;
		default:
			free(context);
			@throw [OFInvalidFormatException exception];
		}

		break;
	/* TODO: '[' */
	/* TODO: '{' */
	/* TODO: '(' */
	default:
		free(context);
		@throw [OFInvalidFormatException exception];
	}

	of_invocation_call(context);

	switch (*typeEncoding) {
		case 'v':
			break;
#define CASE_GPR(encoding, type)					   \
		case encoding:						   \
			{						   \
				type tmp = (type)context->GPR[NUM_GPR_IN]; \
				[invocation setReturnValue: &tmp];	   \
			}						   \
			break;
		CASE_GPR('c', char)
		CASE_GPR('C', unsigned char)
		CASE_GPR('i', int)
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)







|


|





|


|















|




|
|
|
|
|
|







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
376
377
378
379
380
381
382
383
384
385
	case '^':
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
#endif
	case 'f':
	case 'd':
		context->returnType = returnTypeNormal;
		break;
	case 'D':
		context->returnType = returnTypeX87;
		break;
	case 'j':
		switch (typeEncoding[1]) {
		case 'f':
		case 'd':
			context->returnType = returnTypeNormal;
			break;
		case 'D':
			context->returnType = returnTypeComplexX87;
			break;
		default:
			free(context);
			@throw [OFInvalidFormatException exception];
		}

		break;
	/* TODO: '[' */
	/* TODO: '{' */
	/* TODO: '(' */
	default:
		free(context);
		@throw [OFInvalidFormatException exception];
	}

	OFInvocationCall(context);

	switch (*typeEncoding) {
		case 'v':
			break;
#define CASE_GPR(encoding, type)					 \
		case encoding:						 \
			{						 \
				type tmp = (type)context->GPR[numGPRIn]; \
				[invocation setReturnValue: &tmp];	 \
			}						 \
			break;
		CASE_GPR('c', char)
		CASE_GPR('C', unsigned char)
		CASE_GPR('i', int)
		CASE_GPR('I', unsigned int)
		CASE_GPR('s', short)
		CASE_GPR('S', unsigned short)
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
		CASE_GPR('#', Class)
		CASE_GPR(':', SEL)
		CASE_GPR('^', void *)
#undef CASE_GPR
#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;







|







393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
		CASE_GPR('#', Class)
		CASE_GPR(':', SEL)
		CASE_GPR('^', void *)
#undef CASE_GPR
#ifdef __SIZEOF_INT128__
		case 't':
		case 'T':;
			[invocation setReturnValue: &context->GPR[numGPRIn]];
			break;
#endif
		case 'f':;
			float floatTmp;
			_mm_store_ss(&floatTmp, context->SSE[0]);
			[invocation setReturnValue: &floatTmp];
			break;