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
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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdint.h>
#include <glib.h>
#include "x86-codegen.h"
#include <pixman/pixman.h>
typedef struct bits_image bits_image_t;
struct bits_image
{
int common[24];
int format;
void *indexed;
int width;
int height;
uint32_t * bits;
uint32_t * free_me;
int rowstride; /* in number of uint32_t's */
};
#define OFFSET(name) \
(((unsigned char *)(&((bits_image_t *)0)->name)) - (unsigned char *)0)
static unsigned char *
print_reg (unsigned char *code, X86_Reg_No reg, const char *title)
{
x86_pushad (code);
x86_push_reg (code, reg);
x86_push_imm (code, title);
x86_call_code (code, printf);
x86_pop_reg (code, X86_EAX);
x86_pop_reg (code, X86_EAX);
x86_popad (code);
return code;
}
static gboolean EAX;
static gboolean EBX;
static gboolean ECX;
static gboolean EDX;
static gboolean ESI;
static gboolean EDI;
static gboolean need_pop_eax;
static X86_Reg_No
reg_alloc (gboolean need_16_bit)
{
#define CHECK_REG(r) \
do \
{ \
if (!(r)) \
{ \
r = TRUE; \
return X86_##r; \
} \
} while (0)
if (!need_16_bit)
{
CHECK_REG(ESI);
CHECK_REG(EDI);
}
CHECK_REG(EBX);
CHECK_REG(ECX);
CHECK_REG(EDX);
if (!need_pop_eax)
{
CHECK_REG(EAX); /* Allocate EAX last since it is required to be available in
* some cases
*/
}
assert (0);
return X86_NREG;
}
static void
reg_unalloc (X86_Reg_No r)
{
#define CASE_REG(r) case X86_##r: (r) = FALSE; break
switch (r)
{
CASE_REG(EAX);
CASE_REG(EBX);
CASE_REG(ECX);
CASE_REG(EDX);
CASE_REG(ESI);
CASE_REG(EDI);
default:
assert (0);
break;
}
}
static unsigned char *
reg_alloc_eax (unsigned char *code)
{
if (!EAX)
{
EAX = TRUE;
}
else
{
if (need_pop_eax)
assert ("Trying to allocate EAX twice" == NULL);
need_pop_eax = TRUE;
x86_push_reg (code, X86_EAX);
}
return code;
}
static unsigned char *
reg_unalloc_eax (unsigned char *code)
{
if (need_pop_eax)
{
x86_pop_reg (code, X86_EAX);
need_pop_eax = FALSE;
}
else
{
EAX = FALSE;
}
return code;
}
static unsigned char *
compute_start (unsigned char *code,
int bpp,
X86_Reg_No stride,
int x_offset,
int y_offset,
X86_Reg_No dest)
{
int shift;
X86_Reg_No tmp;
switch (bpp)
{
case 8: shift = 0; break;
case 16: shift = 1; break;
case 32: shift = 2; break;
default:
return NULL;
break;
}
/* @#$! x86 */
code = reg_alloc_eax (code);
x86_movzwl_reg_membase (code, X86_EAX, X86_EBP, y_offset);
x86_mul_reg (code, stride, FALSE);
tmp = reg_alloc(FALSE);
x86_movzwl_reg_membase (code, tmp, X86_EBP, x_offset);
x86_lea_memindex (code, dest, X86_EAX, 0, tmp, shift);
reg_unalloc (tmp);
code = reg_unalloc_eax (code);
/* dest = stride * y + x * (bpp / 8) */
return code;
}
/* Clobbers stride_reg */
static unsigned char *
compute_skip (unsigned char *code,
int bpp,
X86_Reg_No stride,
X86_Reg_No neg_width,
int dest_offset)
{
int shift;
X86_Reg_No tmp;
/* This function computes the number of bytes to skip after the inner loop.
* the result is stored in dest_offset(%ebp).
*
* The value computed is
*
* stride * 4 - width * (bpp / 8)
*/
switch (bpp)
{
case 8: shift = 0; break;
case 16: shift = 1; break;
case 32: shift = 2; break;
default:
return NULL;
break;
}
/* %eax = stride in bytes */
tmp = reg_alloc (FALSE);
/* %eax = %eax + (-width) << shift */
x86_lea_memindex (code, tmp, stride, 0, neg_width, shift);
x86_mov_membase_reg (code, X86_EBP, dest_offset, tmp, 4);
reg_unalloc (tmp);
code = print_reg (code, X86_EAX, "Skip: %d\n");
return code;
}
static gboolean
generate_compose_func (unsigned char *code,
pixman_format_code_t src_format,
pixman_format_code_t mask_format,
pixman_format_code_t dest_format)
{
/* Stack layout
*
* [ uint16 height ] ebp+52 higher addresses
* [ uint16 width ] ebp+48
* [ int16 yDst ] ebp+44
* [ int16 xDst ] ebp+40
* [ int16 yMask ] ebp+36
* [ int16 xMask ] ebp+32
* [ int16 ySrc ] ebp+28
* [ int16 xSrc ] ebp+24
* [ pointer pDst ] ebp+20
* [ pointer pMask ] ebp+16
* [ pointer pSrc ] ebp+12
* [ uint8 op ] ebp+8
* [ pointer retaddr ] ebp+4
* [ pointer old_ebp ] <- ebp
* [ uint32_t src_skip ] ebp-4
* [ uint32_t mask_skip ] ebp-8
* [ uint32_t dest_skip ] ebp-12
*/
#define HEIGHT 52
#define WIDTH 48
#define SRC 12
#define MASK 16
#define DEST 20
#define X_SRC 24
#define Y_SRC 28
#define X_MASK 32
#define Y_MASK 36
#define X_DEST 40
#define Y_DEST 44
#define N_LOCALS 3
#define SRC_SKIP -4
#define MASK_SKIP -8
#define DEST_SKIP -12
unsigned char *row_loop, *row_test, *row_jump;
unsigned char *column_loop, *column_test, *column_jump;
int bpp, bpp_shift;
X86_Reg_No loop_reg;
X86_Reg_No neg_width;
X86_Reg_No src_bits, mask_bits, dest_bits;
X86_Reg_No stride;
X86_Reg_No tmp;
/* Preamble */
x86_push_reg (code, X86_EBP);
x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
#if 0
x86_mov_memindex_reg (code,
x86_mov_reg_imm (code, X86_EAX, 0x1345134);
x86_mov_reg_mem (code, X86_EAX, 0x1234567, 4);
#endif
x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4 * N_LOCALS, 4); /* 3 local variables */
/* Compute number of bytes to skip after every inner loop */
neg_width = reg_alloc (FALSE);
/* %ecx = -width */
x86_movzwl_reg_membase (code, neg_width, X86_EBP, WIDTH);
x86_neg_reg (code, neg_width);
/* Compute source skip */
stride = reg_alloc(FALSE);
x86_mov_reg_membase (code, stride, X86_EBP, SRC, 4);
x86_mov_reg_membase (code, stride, stride, OFFSET(rowstride), 4);
src_bits = reg_alloc(FALSE);
code = compute_skip (code, PIXMAN_FORMAT_BPP (src_format),
stride, neg_width, SRC_SKIP);
code = compute_start (code, PIXMAN_FORMAT_BPP (src_format),
stride, X_SRC, Y_SRC, src_bits);
tmp = reg_alloc (FALSE);
x86_mov_reg_membase (code, tmp, X86_EBP, SRC, 4);
x86_alu_reg_membase (code, X86_ADD, src_bits, tmp, OFFSET(bits));
reg_unalloc (tmp);
if (!code)
return FALSE;
/* Compute mask skip */
mask_bits = reg_alloc(FALSE);
x86_mov_reg_membase (code, stride, X86_EBP, MASK, 4);
x86_mov_reg_membase (code, stride, stride, OFFSET(rowstride), 4);
code = compute_skip (code, PIXMAN_FORMAT_BPP (mask_format),
stride, neg_width, MASK_SKIP);
code = compute_start (code, PIXMAN_FORMAT_BPP (mask_format),
stride, X_MASK, Y_MASK, mask_bits);
tmp = reg_alloc (FALSE);
x86_mov_reg_membase (code, tmp, X86_EBP, MASK, 4);
x86_alu_reg_membase (code, X86_ADD, mask_bits, tmp, OFFSET(bits));
reg_unalloc (tmp);
if (!code)
return FALSE;
/* Compute dest skip */
dest_bits = reg_alloc(FALSE);
x86_mov_reg_membase (code, stride, X86_EBP, DEST, 4);
x86_mov_reg_membase (code, stride, stride, OFFSET(rowstride), 4);
code = compute_skip (code, PIXMAN_FORMAT_BPP (mask_format),
stride, neg_width, DEST_SKIP);
reg_unalloc (neg_width);
code = compute_start (code, PIXMAN_FORMAT_BPP (mask_format),
stride, X_DEST, Y_DEST, dest_bits);
reg_unalloc (stride);
tmp = reg_alloc (FALSE);
x86_mov_reg_membase (code, tmp, X86_EBP, DEST, 4);
x86_alu_reg_membase (code, X86_ADD, dest_bits, tmp, OFFSET(bits));
reg_unalloc (tmp);
if (!code)
return FALSE;
{ /* Row loop */
/* We use a trick to use %ecx for both the width and height counters:
*
* Height is stored in the high order 16 bit of %ecx, and
* width is store in the low order 16 bit. This allows us to
* refer to width as just %cl. Once we get to the point where
* we need to decrement and test the height, we know that cl
* is 0, so we can just subtract (1 << 16) and test %ecx against 0.
*/
loop_reg = reg_alloc (TRUE);
x86_movzwl_reg_membase (code, loop_reg, X86_EBP, HEIGHT);
x86_shift_reg_imm(code, X86_SHL, loop_reg, 16);
row_jump = code;
#if 0
x86_jump32 (code, 0); /* Jump to test, will patch later */
#endif
/* Start of the row loop */
row_loop = code;
{ /* Column loop */
x86_mov_reg_membase (code, loop_reg, X86_EBP, WIDTH, 2);
column_jump = code;
#if 0
x86_jump32 (code, 0); /* Jump to column test, patch later */
#endif
column_loop = code;
#if 0
x86_push_reg (code, loop_reg); /* ecx is caller save */
x86_push_imm (code, "birnan\n");
x86_call_code (code, printf);
x86_pop_reg (code, X86_EAX);
x86_pop_reg (code, loop_reg);
#endif
x86_mov_reg_memindex (code, X86_EBX, ECX, 0, loop_reg, 4, 2);
x86_alu_reg_imm (code, X86_SUB, loop_reg, 1, 2);
/* Column test */
column_test = code;
x86_branch (code, X86_CC_NZ, column_loop, FALSE);
}
/* Add skip to the pointers */
/* Decrement row counter */
x86_alu_reg_imm (code, X86_SUB, loop_reg, 1 << 16, 4);
/* Row test */
row_test = code;
x86_branch (code, X86_CC_NZ, row_loop, FALSE);
}
/* Postamble */
x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4 * N_LOCALS, 4);
x86_pop_reg (code, X86_EBP);
x86_ret_void (code);
#if 0
/* Patch up jumps */
x86_patch (row_jump, row_test);
x86_patch (column_jump, column_test);
#endif
return TRUE;
}
typedef void (* ComposeFunc) (
uint8_t op,
void *src,
void *mask,
void *dst,
int16_t src_x,
int16_t src_y,
int16_t mask_x,
int16_t mask_y,
int16_t dst_x,
int16_t dst_y,
uint16_t width,
uint16_t height);
int
main ()
{
unsigned char *the_code = g_malloc (65536);
bits_image_t src;
bits_image_t mask;
bits_image_t dest;
if (!generate_compose_func (the_code, PIXMAN_a8r8g8b8, PIXMAN_r5g6b5, PIXMAN_a8r8g8b8))
g_print ("code generation failed\n");
src.rowstride = 100;
mask.rowstride = 50;
dest.rowstride = 20;
if (mprotect ((void *)(((gulong)the_code) & ~((1 << 12) - 1)), 65536, PROT_READ | PROT_EXEC) < 0)
g_print ("mprotect failed: %s\n", strerror (errno));
else
g_print ("mprotect succeeded\n");
((ComposeFunc)the_code)(
100, &src, &mask, &dest,
1, 1, 2, 2, 3, 3,
2, 2);
return 0;
}
|