forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccall.cpp
754 lines (709 loc) · 25.6 KB
/
ccall.cpp
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// --- the ccall intrinsic ---
// --- library symbol lookup ---
// map from "libX" to full soname "libX.so.ver"
#if defined(__linux__)
static std::map<std::string, std::string> sonameMap;
static bool got_sonames = false;
static void read_sonames()
{
char *line=NULL;
size_t sz=0;
FILE *ldc = popen("/sbin/ldconfig -p", "r");
while (!feof(ldc)) {
ssize_t n = getline(&line, &sz, ldc);
if (n == -1)
break;
if (n > 2 && isspace(line[0])) {
int i=0;
while (isspace(line[++i])) ;
char *name = &line[i];
char *dot = strstr(name, ".so");
char *nxt = strchr(name, ' ');
if (dot != NULL && nxt != NULL) {
std::string pfx(name, dot - name);
std::string soname(name, nxt - name);
sonameMap[pfx] = soname;
}
}
}
free(line);
pclose(ldc);
}
extern "C" const char *jl_lookup_soname(char *pfx, size_t n)
{
if (!got_sonames) {
read_sonames();
got_sonames = true;
}
std::string str(pfx, n);
if (sonameMap.find(str) != sonameMap.end()) {
return sonameMap[str].c_str();
}
return NULL;
}
#endif
// map from user-specified lib names to handles
static std::map<std::string, void*> libMap;
int add_library_mapping(char *lib, void *hnd)
{
if(libMap[lib] == NULL && hnd != NULL) {
libMap[lib] = hnd;
return 0;
} else
return -1;
}
static void *add_library_sym(char *name, char *lib)
{
void *hnd;
if (lib == NULL) {
hnd = jl_dl_handle;
}
else {
hnd = libMap[lib];
if (hnd == NULL) {
hnd = jl_load_dynamic_library(lib, JL_RTLD_DEFAULT);
if (hnd != NULL)
libMap[lib] = hnd;
else
return NULL;
}
}
// add a symbol->address mapping for the JIT
void *sval = jl_dlsym_e((uv_lib_t*)hnd, name);
if (lib != NULL && hnd != jl_dl_handle) {
void *exist = sys::DynamicLibrary::SearchForAddressOfSymbol(name);
if (exist != NULL && exist != sval &&
// openlibm conflicts with libm, and lots of our libraries
// (including LLVM) link to libm. fortunately AddSymbol() is
// able to resolve these in favor of openlibm, but this could
// be an issue in the future (TODO).
strcmp(lib,"libopenlibm")) {
ios_printf(ios_stderr, "Warning: Possible conflict in library symbol %s\n", name);
}
sys::DynamicLibrary::AddSymbol(name, sval);
}
return sval;
}
// --- argument passing and scratch space utilities ---
static Function *value_to_pointer_func;
// TODO: per-thread
static char *temp_arg_area;
static const uint32_t arg_area_sz = 4196;
static uint32_t arg_area_loc;
#define N_TEMP_ARG_BLOCKS 1024
static void *temp_arg_blocks[N_TEMP_ARG_BLOCKS];
static uint32_t arg_block_n = 0;
static Function *save_arg_area_loc_func;
static Function *restore_arg_area_loc_func;
static uint64_t save_arg_area_loc()
{
return (((uint64_t)arg_block_n)<<32) | ((uint64_t)arg_area_loc);
}
static void restore_arg_area_loc(uint64_t l)
{
arg_area_loc = l&0xffffffff;
uint32_t ab = l>>32;
while (arg_block_n > ab) {
arg_block_n--;
free(temp_arg_blocks[arg_block_n]);
}
}
static void *alloc_temp_arg_space(uint32_t sz)
{
void *p;
if (arg_area_loc+sz > arg_area_sz) {
#ifdef JL_GC_MARKSWEEP
if (arg_block_n >= N_TEMP_ARG_BLOCKS)
jl_error("internal compiler error: out of temporary argument space in ccall");
p = malloc(sz);
temp_arg_blocks[arg_block_n++] = p;
#else
#error "fixme"
#endif
}
else {
p = &temp_arg_area[arg_area_loc];
arg_area_loc += sz;
}
return p;
}
static void *alloc_temp_arg_copy(void *obj, uint32_t sz)
{
void *p = alloc_temp_arg_space(sz);
memcpy(p, obj, sz);
return p;
}
// this is a run-time function
// warning: cannot allocate memory except using alloc_temp_arg_space
extern "C" void *jl_value_to_pointer(jl_value_t *jt, jl_value_t *v, int argn,
int addressof)
{
jl_value_t *jvt = (jl_value_t*)jl_typeof(v);
if (addressof) {
if (jvt == jt) {
if (jl_is_bitstype(jvt)) {
size_t osz = jl_datatype_size(jt);
return alloc_temp_arg_copy(jl_data_ptr(v), osz);
}
else if (!jl_is_tuple(jvt) && jl_is_leaf_type(jvt) && !jl_is_array_type(jvt)) {
return v + 1;
}
}
goto value_to_pointer_error;
}
else {
if (jl_is_cpointer_type(jvt) && jl_tparam0(jvt) == jt) {
return (void*)jl_unbox_voidpointer(v);
}
}
if (((jl_value_t*)jl_uint8_type == jt ||
(jl_value_t*)jl_int8_type == jt) && jl_is_byte_string(v)) {
return jl_string_data(v);
}
if (jl_is_array_type(jvt)) {
if (jl_tparam0(jl_typeof(v)) == jt || jt==(jl_value_t*)jl_bottom_type)
return ((jl_array_t*)v)->data;
if (jl_is_cpointer_type(jt)) {
jl_array_t *ar = (jl_array_t*)v;
void **temp=(void**)alloc_temp_arg_space(jl_array_len(ar)*sizeof(void*));
size_t i;
for(i=0; i < jl_array_len(ar); i++) {
temp[i] = jl_value_to_pointer(jl_tparam0(jt),
jl_arrayref(ar, i), argn, 0);
}
return temp;
}
}
value_to_pointer_error:
std::map<int, std::string>::iterator it = argNumberStrings.find(argn);
if (it == argNumberStrings.end()) {
std::stringstream msg;
msg << "argument ";
msg << argn;
argNumberStrings[argn] = msg.str();
it = argNumberStrings.find(argn);
}
jl_value_t *targ=NULL, *pty=NULL;
JL_GC_PUSH2(&targ, &pty);
targ = (jl_value_t*)jl_tuple1(jt);
pty = (jl_value_t*)jl_apply_type((jl_value_t*)jl_pointer_type,
(jl_tuple_t*)targ);
jl_type_error_rt("ccall", (*it).second.c_str(), pty, v);
// doesn't return
return (jl_value_t*)jl_null;
}
static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
jl_value_t *argex, bool addressOf,
int argn, jl_codectx_t *ctx,
bool *mightNeedTempSpace)
{
Type *vt = jv->getType();
if (ty == jl_pvalue_llvmt) {
return boxed(jv);
}
else if (ty == vt && !addressOf) {
return jv;
}
else if (vt != jl_pvalue_llvmt) {
// argument value is unboxed
if (addressOf) {
if (ty->isPointerTy() && ty->getContainedType(0)==vt) {
// pass the address of an alloca'd thing, not a box
// since those are immutable.
Value *slot = builder.CreateAlloca(vt);
builder.CreateStore(jv, slot);
return builder.CreateBitCast(slot, ty);
}
}
else if ((vt->isIntegerTy() && ty->isIntegerTy()) ||
(vt->isFloatingPointTy() && ty->isFloatingPointTy()) ||
(vt->isPointerTy() && ty->isPointerTy())) {
if (vt->getPrimitiveSizeInBits() ==
ty->getPrimitiveSizeInBits()) {
return builder.CreateBitCast(jv, ty);
}
}
// error. box for error handling.
jv = boxed(jv);
}
else if (jl_is_cpointer_type(jt)) {
assert(ty->isPointerTy());
jl_value_t *aty = expr_type(argex, ctx);
if (jl_is_array_type(aty) &&
(jl_tparam0(jt) == jl_tparam0(aty) ||
jl_tparam0(jt) == (jl_value_t*)jl_bottom_type)) {
// array to pointer
return builder.CreateBitCast(emit_arrayptr(jv), ty);
}
if (aty == (jl_value_t*)jl_ascii_string_type || aty == (jl_value_t*)jl_utf8_string_type) {
return builder.CreateBitCast(emit_arrayptr(emit_nthptr(jv,1)), ty);
}
if (jl_is_structtype(aty) && jl_is_leaf_type(aty) && !jl_is_array_type(aty)) {
if (!addressOf) {
emit_error("ccall: expected & on argument", ctx);
return literal_pointer_val(jl_nothing);
}
return builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), ty); // skip type tag field
}
*mightNeedTempSpace = true;
Value *p = builder.CreateCall4(value_to_pointer_func,
literal_pointer_val(jl_tparam0(jt)), jv,
ConstantInt::get(T_int32, argn),
ConstantInt::get(T_int32, (int)addressOf));
return builder.CreateBitCast(p, ty);
}
else if (jl_is_structtype(jt)) {
if (addressOf)
jl_error("ccall: unexpected & on argument"); // the only "safe" thing to emit here is the expected struct
assert (ty->isStructTy() && (Type*)((jl_datatype_t*)jt)->struct_decl == ty);
jl_value_t *aty = expr_type(argex, ctx);
if (aty != jt) {
std::stringstream msg;
msg << "ccall argument ";
msg << argn;
emit_typecheck(jv, jt, msg.str(), ctx);
}
//TODO: check instead that prefix matches
//if (!jl_is_structtype(aty))
// emit_typecheck(emit_typeof(jv), (jl_value_t*)jl_struct_kind, "ccall: Struct argument called with something that isn't a struct", ctx);
// //safe thing would be to also check that jl_typeof(aty)->size > sizeof(ty) here and/or at runtime
Value *pjv = builder.CreateBitCast(emit_nthptr_addr(jv, (size_t)1), PointerType::get(ty,0));
return builder.CreateLoad(pjv, false);
}
// TODO: error for & with non-pointer argument type
assert(jl_is_bitstype(jt));
std::stringstream msg;
msg << "ccall argument ";
msg << argn;
emit_typecheck(jv, jt, msg.str(), ctx);
Value *p = data_pointer(jv);
return builder.CreateLoad(builder.CreateBitCast(p,
PointerType::get(ty,0)),
false);
}
static jl_value_t *jl_signed_type=NULL;
typedef struct {
Value *jl_ptr; // if the argument is a run-time computed pointer
void *fptr; // if the argument is a constant pointer
char *f_name; // if the symbol name is known
char *f_lib; // if a library name is specified
} native_sym_arg_t;
// --- parse :sym or (:sym, :lib) argument into address info ---
static native_sym_arg_t interpret_symbol_arg(jl_value_t *arg, jl_codectx_t *ctx, const char *fname)
{
jl_value_t *ptr = NULL;
Value *jl_ptr = NULL;
ptr = static_eval(arg, ctx, true);
if (ptr == NULL) {
jl_value_t *ptr_ty = expr_type(arg, ctx);
Value *arg1 = emit_unboxed(arg, ctx);
if (!jl_is_cpointer_type(ptr_ty)) {
emit_cpointercheck(arg1,
!strcmp(fname,"ccall") ?
"ccall: first argument not a pointer or valid constant expression" :
"cglobal: first argument not a pointer or valid constant expression",
ctx);
}
jl_ptr = emit_unbox(T_size, T_psize, arg1);
}
void *fptr = NULL;
char *f_name = NULL, *f_lib = NULL;
if (ptr != NULL) {
if (jl_is_tuple(ptr) && jl_tuple_len(ptr)==1) {
ptr = jl_tupleref(ptr,0);
}
if (jl_is_symbol(ptr))
f_name = ((jl_sym_t*)ptr)->name;
else if (jl_is_byte_string(ptr))
f_name = jl_string_data(ptr);
if (f_name != NULL) {
// just symbol, default to JuliaDLHandle
#ifdef _OS_WINDOWS_
//TODO: store the f_lib name instead of fptr
fptr = jl_dlsym_win32(f_name);
#else
// will look in process symbol table
#endif
}
else if (jl_is_cpointer_type(jl_typeof(ptr))) {
fptr = *(void**)jl_data_ptr(ptr);
}
else if (jl_is_tuple(ptr) && jl_tuple_len(ptr)>1) {
jl_value_t *t0 = jl_tupleref(ptr,0);
jl_value_t *t1 = jl_tupleref(ptr,1);
if (jl_is_symbol(t0))
f_name = ((jl_sym_t*)t0)->name;
else if (jl_is_byte_string(t0))
f_name = jl_string_data(t0);
else
JL_TYPECHKS(fname, symbol, t0);
if (jl_is_symbol(t1))
f_lib = ((jl_sym_t*)t1)->name;
else if (jl_is_byte_string(t1))
f_lib = jl_string_data(t1);
else
JL_TYPECHKS(fname, symbol, t1);
}
else {
JL_TYPECHKS(fname, pointer, ptr);
}
}
native_sym_arg_t r;
r.jl_ptr = jl_ptr;
r.fptr = fptr;
r.f_name = f_name;
r.f_lib = f_lib;
return r;
}
// --- code generator for cglobal ---
static Value *emit_cglobal(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
{
JL_NARGS(cglobal, 1, 2);
jl_value_t *rt=NULL;
Value *res;
JL_GC_PUSH1(&rt);
if (nargs == 2) {
rt = jl_interpret_toplevel_expr_in(ctx->module, args[2],
&jl_tupleref(ctx->sp,0),
jl_tuple_len(ctx->sp)/2);
JL_TYPECHK(cglobal, type, rt);
rt = (jl_value_t*)jl_apply_type((jl_value_t*)jl_pointer_type, jl_tuple1(rt));
}
else {
rt = (jl_value_t*)jl_voidpointer_type;
}
Type *lrt = julia_type_to_llvm(rt);
if (lrt == NULL) lrt = T_pint8;
native_sym_arg_t sym = interpret_symbol_arg(args[1], ctx, "cglobal");
if (sym.jl_ptr != NULL) {
res = builder.CreateIntToPtr(sym.jl_ptr, lrt);
}
else if (sym.fptr != NULL) {
res = literal_pointer_val(sym.fptr, lrt);
}
else {
void *symaddr;
if (sym.f_lib != NULL)
symaddr = add_library_sym(sym.f_name, sym.f_lib);
else
symaddr = sys::DynamicLibrary::SearchForAddressOfSymbol(sym.f_name);
if (symaddr == NULL) {
std::stringstream msg;
msg << "cglobal: could not find symbol ";
msg << sym.f_name;
if (sym.f_lib != NULL) {
msg << " in library ";
msg << sym.f_lib;
}
emit_error(msg.str(), ctx);
res = literal_pointer_val(NULL, lrt);
}
else {
res = jl_Module->getOrInsertGlobal(sym.f_name,
lrt->getContainedType(0));
}
}
JL_GC_POP();
return mark_julia_type(res, rt);
}
// --- code generator for ccall itself ---
// ccall(pointer, rettype, (argtypes...), args...)
static Value *emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
{
JL_NARGSV(ccall, 3);
jl_value_t *rt=NULL, *at=NULL;
JL_GC_PUSH2(&rt, &at);
native_sym_arg_t symarg = interpret_symbol_arg(args[1], ctx, "ccall");
Value *jl_ptr=NULL;
void *fptr = NULL;
char *f_name = NULL, *f_lib = NULL;
jl_ptr = symarg.jl_ptr;
fptr = symarg.fptr;
f_name = symarg.f_name;
f_lib = symarg.f_lib;
if (f_name == NULL && fptr == NULL && jl_ptr == NULL) {
JL_GC_POP();
emit_error("ccall: null function pointer", ctx);
return literal_pointer_val(jl_nothing);
}
rt = jl_interpret_toplevel_expr_in(ctx->module, args[2],
&jl_tupleref(ctx->sp,0),
jl_tuple_len(ctx->sp)/2);
if (jl_is_tuple(rt)) {
std::string msg = "in " + ctx->funcName +
": ccall: missing return type";
jl_error(msg.c_str());
}
if (rt == (jl_value_t*)jl_pointer_type)
jl_error("ccall: return type Ptr should have an element type, Ptr{T}");
at = jl_interpret_toplevel_expr_in(ctx->module, args[3],
&jl_tupleref(ctx->sp,0),
jl_tuple_len(ctx->sp)/2);
JL_TYPECHK(ccall, type, rt);
JL_TYPECHK(ccall, tuple, at);
JL_TYPECHK(ccall, type, at);
jl_tuple_t *tt = (jl_tuple_t*)at;
std::vector<Type *> fargt(0);
std::vector<Type *> fargt_sig(0);
Type *lrt = julia_struct_to_llvm(rt);
if (lrt == NULL) {
JL_GC_POP();
emit_error("ccall: return type doesn't correspond to a C type", ctx);
return literal_pointer_val(jl_nothing);
}
size_t i;
bool isVa = false;
size_t nargt = jl_tuple_len(tt);
std::vector<AttributeWithIndex> attrs;
for(i=0; i < nargt; i++) {
jl_value_t *tti = jl_tupleref(tt,i);
if (tti == (jl_value_t*)jl_pointer_type)
jl_error("ccall: argument type Ptr should have an element type, Ptr{T}");
if (jl_is_vararg_type(tti)) {
isVa = true;
tti = jl_tparam0(tti);
}
if (jl_is_bitstype(tti)) {
// see pull req #978. need to annotate signext/zeroext for
// small integer arguments.
jl_datatype_t *bt = (jl_datatype_t*)tti;
if (bt->size < 4) {
if (jl_signed_type == NULL) {
jl_signed_type = jl_get_global(jl_core_module,jl_symbol("Signed"));
}
#ifdef LLVM32
Attributes::AttrVal av;
if (jl_signed_type && jl_subtype(tti, jl_signed_type, 0))
av = Attributes::SExt;
else
av = Attributes::ZExt;
attrs.push_back(AttributeWithIndex::get(getGlobalContext(), i+1,
ArrayRef<Attributes::AttrVal>(&av, 1)));
#else
Attribute::AttrConst av;
if (jl_signed_type && jl_subtype(tti, jl_signed_type, 0))
av = Attribute::SExt;
else
av = Attribute::ZExt;
attrs.push_back(AttributeWithIndex::get(i+1, av));
#endif
}
}
Type *t = julia_struct_to_llvm(tti);
if (t == NULL) {
JL_GC_POP();
std::stringstream msg;
msg << "ccall: the type of argument ";
msg << i+1;
msg << " doesn't correspond to a C type";
emit_error(msg.str(), ctx);
return literal_pointer_val(jl_nothing);
}
fargt.push_back(t);
if (!isVa)
fargt_sig.push_back(t);
}
// check for calling convention specifier
CallingConv::ID cc = CallingConv::C;
jl_value_t *last = args[nargs];
if (jl_is_expr(last)) {
jl_sym_t *lhd = ((jl_expr_t*)last)->head;
if (lhd == jl_symbol("stdcall")) {
cc = CallingConv::X86_StdCall;
nargs--;
}
else if (lhd == jl_symbol("cdecl")) {
cc = CallingConv::C;
nargs--;
}
else if (lhd == jl_symbol("fastcall")) {
cc = CallingConv::X86_FastCall;
nargs--;
}
else if (lhd == jl_symbol("thiscall")) {
cc = CallingConv::X86_ThisCall;
nargs--;
}
}
if ((!isVa && jl_tuple_len(tt) != (nargs-2)/2) ||
( isVa && jl_tuple_len(tt)-1 > (nargs-2)/2))
jl_error("ccall: wrong number of arguments to C function");
// some special functions
if (fptr == &jl_array_ptr) {
assert(lrt->isPointerTy());
Value *ary = emit_expr(args[4], ctx);
JL_GC_POP();
return mark_julia_type(builder.CreateBitCast(emit_arrayptr(ary),lrt),
rt);
}
if (fptr == &jl_value_ptr) {
assert(lrt->isPointerTy());
jl_value_t *argi = args[4];
bool addressOf = false;
if (jl_is_expr(argi) && ((jl_expr_t*)argi)->head == amp_sym) {
addressOf = true;
argi = jl_exprarg(argi,0);
}
Value *ary = boxed(emit_expr(argi, ctx));
JL_GC_POP();
return mark_julia_type(
builder.CreateBitCast(emit_nthptr_addr(ary, addressOf?1:0),lrt),
rt);
}
// make LLVM function object for the target
Value *llvmf;
FunctionType *functype = FunctionType::get(lrt, fargt_sig, isVa);
if (jl_ptr != NULL) {
null_pointer_check(jl_ptr,ctx);
Type *funcptype = PointerType::get(functype,0);
llvmf = builder.CreateIntToPtr(jl_ptr, funcptype);
}
else if (fptr != NULL) {
Type *funcptype = PointerType::get(functype,0);
llvmf = literal_pointer_val(fptr, funcptype);
}
else {
void *symaddr;
if (f_lib != NULL)
symaddr = add_library_sym(f_name, f_lib);
else
symaddr = sys::DynamicLibrary::SearchForAddressOfSymbol(f_name);
if (symaddr == NULL) {
JL_GC_POP();
std::stringstream msg;
msg << "ccall: could not find function ";
msg << f_name;
if (f_lib != NULL) {
msg << " in library ";
msg << f_lib;
}
emit_error(msg.str(), ctx);
return literal_pointer_val(jl_nothing);
}
llvmf = jl_Module->getOrInsertFunction(f_name, functype);
}
// save place before arguments, for possible insertion of temp arg
// area saving code.
Value *saveloc=NULL;
Value *stacksave=NULL;
BasicBlock::InstListType &instList = builder.GetInsertBlock()->getInstList();
Instruction *savespot;
if (instList.empty()) {
savespot = NULL;
}
else {
// hey C++, there's this thing called pointers...
Instruction &_savespot = builder.GetInsertBlock()->back();
savespot = &_savespot;
}
// emit arguments
Value *argvals[(nargs-3)/2];
int last_depth = ctx->argDepth;
int nargty = jl_tuple_len(tt);
bool needTempSpace = false;
for(i=4; i < nargs+1; i+=2) {
int ai = (i-4)/2;
jl_value_t *argi = args[i];
bool addressOf = false;
if (jl_is_expr(argi) && ((jl_expr_t*)argi)->head == amp_sym) {
addressOf = true;
argi = jl_exprarg(argi,0);
}
Type *largty;
jl_value_t *jargty;
if (isVa && ai >= nargty-1) {
largty = fargt[nargty-1];
jargty = jl_tparam0(jl_tupleref(tt,nargty-1));
}
else {
largty = fargt[ai];
jargty = jl_tupleref(tt,ai);
}
Value *arg;
if (largty == jl_pvalue_llvmt ||
largty->isStructTy()) {
arg = emit_expr(argi, ctx, true);
}
else {
arg = emit_unboxed(argi, ctx);
if (jl_is_bitstype(expr_type(argi, ctx))) {
if (addressOf)
arg = emit_unbox(largty->getContainedType(0), largty, arg);
else
arg = emit_unbox(largty, PointerType::get(largty,0), arg);
}
}
/*
#ifdef JL_GC_MARKSWEEP
// make sure args are rooted
if (largty->isPointerTy() &&
(largty == jl_pvalue_llvmt ||
!jl_is_bits_type(expr_type(args[i], ctx)))) {
make_gcroot(boxed(arg), ctx);
}
#endif
*/
bool mightNeed=false;
argvals[ai] = julia_to_native(largty, jargty, arg, argi, addressOf,
ai+1, ctx, &mightNeed);
needTempSpace |= mightNeed;
}
if (needTempSpace) {
// save temp argument area stack pointer
// TODO: inline this
saveloc = CallInst::Create(save_arg_area_loc_func);
stacksave = CallInst::Create(Intrinsic::getDeclaration(jl_Module,
Intrinsic::stacksave));
if (savespot)
instList.insertAfter(savespot, (Instruction*)saveloc);
else
instList.push_front((Instruction*)saveloc);
instList.insertAfter((Instruction*)saveloc, (Instruction*)stacksave);
}
// the actual call
Value *result = builder.CreateCall(llvmf,
ArrayRef<Value*>(&argvals[0],(nargs-3)/2));
if (cc != CallingConv::C)
((CallInst*)result)->setCallingConv(cc);
#ifdef LLVM32
((CallInst*)result)->setAttributes(AttrListPtr::get(getGlobalContext(), ArrayRef<AttributeWithIndex>(attrs)));
#else
((CallInst*)result)->setAttributes(AttrListPtr::get(attrs.data(),attrs.size()));
#endif
if (needTempSpace) {
// restore temp argument area stack pointer
assert(saveloc != NULL);
builder.CreateCall(restore_arg_area_loc_func, saveloc);
assert(stacksave != NULL);
builder.CreateCall(Intrinsic::getDeclaration(jl_Module,
Intrinsic::stackrestore),
stacksave);
}
ctx->argDepth = last_depth;
if (0) { // Enable this to turn on SSPREQ (-fstack-protector) on the function containing this ccall
#ifdef LLVM32
ctx->f->addFnAttr(Attributes::StackProtectReq);
#else
ctx->f->addFnAttr(Attribute::StackProtectReq);
#endif
}
JL_GC_POP();
if (lrt == T_void)
return literal_pointer_val((jl_value_t*)jl_nothing);
if (lrt->isStructTy()) {
//fprintf(stderr, "ccall rt: %s -> %s\n", f_name, ((jl_tag_type_t*)rt)->name->name->name);
assert(jl_is_structtype(rt));
Value *strct =
builder.CreateCall(jlallocobj_func,
ConstantInt::get(T_size,
sizeof(void*)+((jl_datatype_t*)rt)->size));
builder.CreateStore(literal_pointer_val((jl_value_t*)rt),
emit_nthptr_addr(strct, (size_t)0));
builder.CreateStore(result,
builder.CreateBitCast(
emit_nthptr_addr(strct, (size_t)1),
PointerType::get(lrt,0)));
return mark_julia_type(strct, rt);
}
return mark_julia_type(result, rt);
}