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
464
465
|
/* { dg-do compile { target x86_64-*-* } } */
#include "libgccjit++.h"
#include "harness.h"
/**********************************************************************
Support fns for creating code.
**********************************************************************/
/* Make a "void FUNC_NAME (void)" function with a single block, returning
that block. */
static gccjit::block
make_single_block_func (gccjit::context ctxt, const char *func_name)
{
gccjit::type void_type = ctxt.get_type (GCC_JIT_TYPE_VOID);
std::vector<gccjit::param> params;
gccjit::function func
= ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
void_type,
func_name, params, 0);
return func.new_block ("initial");
}
/**********************************************************************
Support fns for verifying code.
**********************************************************************/
typedef void (*void_void_fn) (void);
static void_void_fn
get_test_fn (gcc_jit_result *result, const char *func_name)
{
return (void_void_fn)gcc_jit_result_get_code (result, func_name);
}
/**********************************************************************
test_i386_basic_asm_1: simple example of asm
**********************************************************************/
/* Create the equivalent of:
int src;
int dst;
void test_i386_basic_asm_1 (void)
{
// Quote from here in docs/cp/topics/asm.rst: example 1: C
asm ("mov %1, %0\n\t"
"add $1, %0"
: "=r" (dst)
: "r" (src));
// Quote up to here in docs/cp/topics/asm.rst: example 1: C
}
i.e. copy src to dst and add 1 to dst. */
static void
create_test_i386_basic_asm_1 (gcc_jit_context *c_ctxt)
{
gccjit::context ctxt (c_ctxt);
gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);
gccjit::lvalue dst
= ctxt.new_global (GCC_JIT_GLOBAL_EXPORTED, int_type, "dst");
gccjit::lvalue src
= ctxt.new_global (GCC_JIT_GLOBAL_EXPORTED, int_type, "src");
gccjit::block block
= make_single_block_func (ctxt, "test_i386_basic_asm_1");
gccjit::extended_asm ext_asm =
/* Quote from here in docs/cp/topics/asm.rst: example 1: jit. */
block.add_extended_asm ("mov %1, %0\n\t"
"add $1, %0")
.add_output_operand ("=r", dst)
.add_input_operand ("r", src);
/* Quote up to here in docs/cp/topics/asm.rst: example 1: jit. */
std::string desc = ext_asm.get_debug_string ();
CHECK_STRING_VALUE
(desc.c_str (),
"asm (\"mov %1, %0\\n\\tadd $1, %0\" : \"=r\" (dst) : \"r\" (src) : )");
block.end_with_return ();
}
static void
verify_code_1 (gcc_jit_context *ctxt, gcc_jit_result *result)
{
void_void_fn test_i386_basic_asm_1
= get_test_fn (result, "test_i386_basic_asm_1");
CHECK_NON_NULL (test_i386_basic_asm_1);
int *dst_ptr = (int *)gcc_jit_result_get_global (result, "dst");
CHECK_NON_NULL (dst_ptr);
int *src_ptr = (int *)gcc_jit_result_get_global (result, "src");
CHECK_NON_NULL (src_ptr);
*src_ptr = 42;
*dst_ptr = 0;
test_i386_basic_asm_1 ();
CHECK_VALUE (*src_ptr, 42);
CHECK_VALUE (*dst_ptr, 43);
}
/**********************************************************************
test_i386_basic_asm_2: test of symbolic names and clobbers
**********************************************************************/
/* Create the equivalent of:
uint32_t test_i386_basic_asm_2 (uint32_t Mask)
{
uint32_t Index;
// Quote from here in docs/cp/topics/asm.rst: example 2: C
asm ("bsfl %[aMask], %[aIndex]"
: [aIndex] "=r" (Index)
: [aMask] "r" (Mask)
: "cc");
// Quote up to here in docs/cp/topics/asm.rst: example 2: C
return Index;
}
i.e. return the first bit set in "Mask"
This exercises symbolic names and clobbers. */
static void
create_test_i386_basic_asm_2 (gcc_jit_context *c_ctxt)
{
gccjit::context ctxt (c_ctxt);
gccjit::type uint32_type = ctxt.get_int_type (4, 0);
gccjit::param mask = ctxt.new_param (uint32_type, "Mask");
std::vector<gccjit::param> params {mask};
gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
uint32_type,
"test_i386_basic_asm_2",
params, 0);
gccjit::lvalue index = func.new_local (uint32_type, "Index");
gccjit::block block = func.new_block ("initial");
gccjit::extended_asm ext_asm =
/* Quote from here in docs/cp/topics/asm.rst: example 2: jit. */
block.add_extended_asm ("bsfl %[aMask], %[aIndex]")
.add_output_operand ("aIndex", "=r", index)
.add_input_operand ("aMask", "r", mask)
.add_clobber ("cc");
/* Quote up to here in docs/cp/topics/asm.rst: example 2: jit. */
std::string desc = ext_asm.get_debug_string ();
CHECK_STRING_VALUE
(desc.c_str (),
"asm (\"bsfl %[aMask], %[aIndex]\""
" : [aIndex] \"=r\" (Index) : [aMask] \"r\" (Mask) : \"cc\")");
block.end_with_return (index);
}
static void
verify_code_2 (gcc_jit_context *ctxt, gcc_jit_result *result)
{
typedef uint32_t (*fntype) (uint32_t);
fntype test_i386_basic_asm_2
= (fntype)gcc_jit_result_get_code (result, "test_i386_basic_asm_2");
CHECK_NON_NULL (test_i386_basic_asm_2);
CHECK_VALUE (test_i386_basic_asm_2 (1), 0);
CHECK_VALUE (test_i386_basic_asm_2 (2), 1);
CHECK_VALUE (test_i386_basic_asm_2 (4), 2);
CHECK_VALUE (test_i386_basic_asm_2 (8), 3);
}
/**********************************************************************
test_i386_basic_asm_3a/b: test of control flow: "asm goto"
**********************************************************************/
/* Create the equivalent of:
int test_i386_basic_asm_3a (int p1, int p2)
{
asm goto ("btl %1, %0\n\t"
"jc %l2"
: // No outputs
: "r" (p1), "r" (p2)
: "cc"
: carry);
return 0;
carry:
return 1;
}
or (the "_3b" variant) using a name rather than a number for the goto
label:
// Quote from here in docs/cp/topics/asm.rst: example 3b: C
asm goto ("btl %1, %0\n\t"
"jc %l[carry]"
: // No outputs
: "r" (p1), "r" (p2)
: "cc"
: carry);
// Quote up to here in docs/cp/topics/asm.rst: example 3b: C
This exercises control flow with an asm. */
static void
create_test_i386_basic_asm_3 (gcc_jit_context *c_ctxt,
const char *funcname,
int use_name)
{
gccjit::context ctxt (c_ctxt);
gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);
gccjit::param p1 = ctxt.new_param (int_type, "p1");
gccjit::param p2 = ctxt.new_param (int_type, "p2");
std::vector<gccjit::param> params ({p1, p2});
gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
int_type,
funcname,
params, 0);
gccjit::block b_start = func.new_block ("start");
gccjit::block b_fallthru = func.new_block ("fallthru");
gccjit::block b_carry = func.new_block ("carry");
gccjit::rvalue zero = ctxt.new_rvalue (int_type, 0);
gccjit::rvalue one = ctxt.new_rvalue (int_type, 1);
/* Quote from here in docs/cp/topics/asm.rst: example 3: jit. */
const char *asm_template =
(use_name
? /* Label referred to by name: "%l[carry]". */
("btl %1, %0\n\t"
"jc %l[carry]")
: /* Label referred to numerically: "%l2". */
("btl %1, %0\n\t"
"jc %l2"));
std::vector<gccjit::block> goto_blocks ({b_carry});
gccjit::extended_asm ext_asm
= (b_start.end_with_extended_asm_goto (asm_template,
goto_blocks,
&b_fallthru)
.add_input_operand ("r", p1)
.add_input_operand ("r", p2)
.add_clobber ("cc"));
/* Quote up to here in docs/cp/topics/asm.rst: example 3: jit. */
std::string desc = ext_asm.get_debug_string ();
CHECK_STRING_VALUE
(desc.c_str (),
(use_name
? ("asm goto (\"btl %1, %0\\n\\tjc %l[carry]\" "
": : \"r\" (p1), \"r\" (p2) : \"cc\" "
": carry [fallthrough: fallthru])")
: ("asm goto (\"btl %1, %0\\n\\tjc %l2\" "
": : \"r\" (p1), \"r\" (p2) : \"cc\" "
": carry [fallthrough: fallthru])")));
b_fallthru.end_with_return (zero);
b_carry.end_with_return (one);
}
static void
verify_code_3 (gcc_jit_context *ctxt, gcc_jit_result *result,
const char *funcname)
{
typedef int (*test_i386_basic_asm_3_type) (int, int);
test_i386_basic_asm_3_type test_i386_basic_asm_3
= (test_i386_basic_asm_3_type) gcc_jit_result_get_code (result, funcname);
CHECK_NON_NULL (test_i386_basic_asm_3);
/* The fn should test bits, returning 0 or 1. */
/* Bit 0. */
CHECK_VALUE (test_i386_basic_asm_3 (0x0000, 0), 0);
CHECK_VALUE (test_i386_basic_asm_3 (0x0001, 0), 1);
CHECK_VALUE (test_i386_basic_asm_3 (0x0002, 0), 0);
CHECK_VALUE (test_i386_basic_asm_3 (0x0003, 0), 1);
CHECK_VALUE (test_i386_basic_asm_3 (0x0004, 0), 0);
/* Bit 1. */
CHECK_VALUE (test_i386_basic_asm_3 (0x0000, 1), 0);
CHECK_VALUE (test_i386_basic_asm_3 (0x0001, 1), 0);
CHECK_VALUE (test_i386_basic_asm_3 (0x0002, 1), 1);
CHECK_VALUE (test_i386_basic_asm_3 (0x0003, 1), 1);
CHECK_VALUE (test_i386_basic_asm_3 (0x0004, 1), 0);
for (int i = 0; i < 15; i++)
{
CHECK_VALUE (test_i386_basic_asm_3 (0x0000, i), 0);
CHECK_VALUE (test_i386_basic_asm_3 (0xffff, i), 1);
}
}
/**********************************************************************
test_i386_basic_asm_4: test of "volatile"
**********************************************************************/
/* Create the equivalent of:
uint64_t test_i386_basic_asm_4 (void)
{
uint64_t start_time, end_time;
// Get start time
asm volatile ("rdtsc\n\t" // Returns the time in EDX:EAX.
"shl $32, %%rdx\n\t" // Shift the upper bits left.
"or %%rdx, %0" // 'Or' in the lower bits.
: "=a" (start_time)
:
: "rdx");
// could do other work here
// Get end time
asm volatile ("rdtsc\n\t" // Returns the time in EDX:EAX.
"shl $32, %%rdx\n\t" // Shift the upper bits left.
"or %%rdx, %0" // 'Or' in the lower bits.
: "=a" (start_time)
:
: "rdx");
// Get elapsed time
return end_time - start_time;
}
This exercises "volatile"; without it, the optimizer can assume that
both asm generate the same value and thus the time difference is zero. */
static void
add_rdtsc (gccjit::block block, gccjit::lvalue msr)
{
/* Quote from here in docs/cp/topics/asm.rst: example 4: jit. */
gccjit::extended_asm ext_asm
= block.add_extended_asm
("rdtsc\n\t" /* Returns the time in EDX:EAX. */
"shl $32, %%rdx\n\t" /* Shift the upper bits left. */
"or %%rdx, %0") /* 'Or' in the lower bits. */
.set_volatile_flag (true)
.add_output_operand ("=a", msr)
.add_clobber ("rdx");
/* Quote up to here in docs/cp/topics/asm.rst: example 4: jit. */
std::string desc = ext_asm.get_debug_string ();
CHECK_STRING_STARTS_WITH (desc.c_str (), "asm volatile (");
}
static void
create_test_i386_basic_asm_4 (gcc_jit_context *c_ctxt)
{
gccjit::context ctxt (c_ctxt);
gccjit::type uint64_type = ctxt.get_int_type (8, 0);
std::vector<gccjit::param> params;
gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
uint64_type,
"test_i386_basic_asm_4",
params, 0);
gccjit::block block = func.new_block ();
gccjit::lvalue start_time = func.new_local (uint64_type, "start_time");
add_rdtsc (block, start_time);
block.add_comment ("other work here");
gccjit::lvalue end_time = func.new_local (uint64_type, "end_time");
add_rdtsc (block, end_time);
block.end_with_return (end_time - start_time);
}
static void
verify_code_4 (gcc_jit_context *ctxt, gcc_jit_result *result)
{
typedef uint64_t (*fntype) (void);
fntype test_i386_basic_asm_4
= (fntype)gcc_jit_result_get_code (result, "test_i386_basic_asm_4");
CHECK_NON_NULL (test_i386_basic_asm_4);
test_i386_basic_asm_4 ();
}
/**********************************************************************
test_i386_basic_asm_5: test of top-level asm
**********************************************************************/
/* Create the equivalent of:
// Quote from here in docs/cp/topics/asm.rst: example 5: C
asm ("\t.pushsection .text\n"
"\t.globl add_asm\n"
"\t.type add_asm, @function\n"
"add_asm:\n"
"\tmovq %rdi, %rax\n"
"\tadd %rsi, %rax\n"
"\tret\n"
"\t.popsection\n");
// Quote up to here in docs/cp/topics/asm.rst: example 5: C
to add a simple function ("add_asm") directly in assembly language. */
static void
create_test_i386_basic_asm_5 (gcc_jit_context *c_ctxt)
{
gccjit::context ctxt (c_ctxt);
#if __APPLE__
/* Darwin's assemblers do not support push/pop section, do not use .type
and external symbols should use __USER_LABEL_PREFIX__. */
ctxt.add_top_level_asm ("\t.text\n"
"\t.globl _add_asm\n"
"_add_asm:\n"
"\tmovq %rdi, %rax\n"
"\tadd %rsi, %rax\n"
"\tret\n"
"\t# some asm here\n");
#else
/* Quote from here in docs/cp/topics/asm.rst: example 5: jit. */
ctxt.add_top_level_asm ("\t.pushsection .text\n"
"\t.globl add_asm\n"
"\t.type add_asm, @function\n"
"add_asm:\n"
"\tmovq %rdi, %rax\n"
"\tadd %rsi, %rax\n"
"\tret\n"
"\t# some asm here\n"
"\t.popsection\n");
/* Quote up to here in docs/cp/topics/asm.rst: example 5: jit. */
#endif
}
static void
verify_code_5 (gcc_jit_context *ctxt, gcc_jit_result *result)
{
typedef int (*test_i386_basic_asm_5_type) (int, int);
test_i386_basic_asm_5_type test_i386_basic_asm_5
= (test_i386_basic_asm_5_type) gcc_jit_result_get_code (result, "add_asm");
CHECK_NON_NULL (test_i386_basic_asm_5);
CHECK_VALUE (test_i386_basic_asm_5 (2, 2), 4);
CHECK_VALUE (test_i386_basic_asm_5 (20, 7), 27);
}
/**********************************************************************
Code for harness
**********************************************************************/
void
create_code (gcc_jit_context *ctxt, void *user_data)
{
create_test_i386_basic_asm_1 (ctxt);
create_test_i386_basic_asm_2 (ctxt);
create_test_i386_basic_asm_3 (ctxt, "test_i386_basic_asm_3a", 0);
create_test_i386_basic_asm_3 (ctxt, "test_i386_basic_asm_3b", 1);
create_test_i386_basic_asm_4 (ctxt);
create_test_i386_basic_asm_5 (ctxt);
}
void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
CHECK_NON_NULL (result);
verify_code_1 (ctxt, result);
verify_code_2 (ctxt, result);
verify_code_3 (ctxt, result, "test_i386_basic_asm_3a");
verify_code_3 (ctxt, result, "test_i386_basic_asm_3b");
verify_code_4 (ctxt, result);
verify_code_5 (ctxt, result);
}
|