ObjFW  Diff

Differences From Artifact [1d2004b374]:

To Artifact [2253358a2d]:


21
22
23
24
25
26
27


28
29
30
31
32
33
34
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
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, size_t *currentGPR, uint64_t value)
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
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, size_t *currentSSE, double value)
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
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;
}

#ifndef __clang__
#if defined(__SIZEOF_INT128__) && !defined(__clang__)
static void
alignStack(struct call_context **context, size_t alignment)
pushInt128(struct call_context **context, uint_fast8_t *currentGPR,
    uint64_t low, uint64_t high)
{
	size_t stackSize = (*context)->stack_size;
	struct call_context *newContext;
	size_t stackSize;

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

	stackSize += alignment - stackSize % alignment;
	}

	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;
	size_t currentGPR = 0, currentSSE = 0;
	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
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;
			uint64_t int128Tmp[2];
			[invocation getArgument: int128Tmp
			} int128Tmp;
			[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]);
			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);