Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updates to help cross platform compatability.

  • Loading branch information...
commit eb6e40d2d8435b1b02dad83b049278559afafdb9 1 parent 2e56f29
@ashgti authored
Showing with 121 additions and 96 deletions.
  1. +121 −96 config/gen/libffi/nci-ffi.pmc.in
View
217 config/gen/libffi/nci-ffi.pmc.in
@@ -124,7 +124,10 @@ find_matching(PARROT_INTERP, STRING* sig, size_t start, size_t sig_length,
typedef struct pmc_holder_t {
PMC* p;
- INTVAL* val;
+ union {
+ INTVAL* ival;
+ void** pval;
+ };
} pmc_holder_t;
/*
@@ -155,7 +158,7 @@ parse_sig(PARROT_INTERP, STRING *sig, size_t sig_length, Parrot_NCI_attributes *
arg_types = mem_internal_allocate_n_zeroed_typed(1, ffi_type*);
arg_types[0] = &ffi_type_void;
nci_info->pcc_params_signature = string_make(interp, "", 1, NULL, 0);
- nci_info->arg_translation = "";
+ nci_info->arg_translation = NULL;
nci_info->arity = 0;
}
}
@@ -164,7 +167,7 @@ parse_sig(PARROT_INTERP, STRING *sig, size_t sig_length, Parrot_NCI_attributes *
arg_types[0] = &ffi_type_void;
return_type = &ffi_type_void;
nci_info->pcc_params_signature = string_make(interp, "", 1, NULL, 0);
- nci_info->arg_translation = "";
+ nci_info->arg_translation = NULL;
nci_info->arity = 0;
}
@@ -335,18 +338,6 @@ parse_identifier(PARROT_INTERP,
*type_obj = &ffi_type_schar;
}
break;
- /*
- case (INTVAL)'b':
- translation[(*translation_count) - 1] = 'b';
- type[(*pmc_count)++] = 'I';
- if (prefix & PREFIX_UNSIGNED) {
- *type_obj = &ffi_type_uint8;
- }
- else {
- *type_obj = &ffi_type_sint8;
- }
- break;
- */
case (INTVAL)'B':
translation[(*translation_count) - 1] = 'B';
type[(*pmc_count)++] = 'S';
@@ -460,7 +451,7 @@ parse_identifier(PARROT_INTERP,
type[(*pmc_count)++] = 'N';
*type_obj = &ffi_type_double;
break;
-#if (DOUBEL_SIZE > 8) /* FLOATVAL is a long double */
+#if (DOUBLE_SIZE > 8) /* FLOATVAL is a long double */
case (INTVAL)'N':
#endif
case (INTVAL)'D':
@@ -478,7 +469,7 @@ parse_identifier(PARROT_INTERP,
type[(*pmc_count)++] = 'P';
type[(*pmc_count)++] = 's';
break;
- defualt:
+ default:
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_JIT_ERROR,
"Unknown param Signature %c\n", (char)c);
@@ -1021,7 +1012,7 @@ class, the PMC arguments are shifted down.
INTVAL return_size = sizeof (void*);
PMC *ctx = CURRENT_CONTEXT(interp);
PMC *call_object = Parrot_pcc_get_signature(interp, ctx);
- void* (*func)(); /* a function pointer for our function to call */
+ void (*func)(void*,void*,void*); /* a function pointer for our function to call */
if (PObj_flag_TEST(private2, SELF)) {
void *orig_func;
@@ -1029,11 +1020,11 @@ class, the PMC arguments are shifted down.
GET_ATTR_orig_func(INTERP, SELF, orig_func);
GET_ATTR_fb_info(INTERP, SELF, fb_info);
- func = FFI_FN(orig_func);
+ func = (void (*)(void*, void*, void*))orig_func;
if (!func) {
/* build the thunk only when necessary */
- func = FFI_FN(build_func(interp, nci_info));
+ func = (void (*)(void*, void*, void*))build_func(interp, nci_info);
if (!func)
Parrot_ex_throw_from_c_args(INTERP, NULL,
@@ -1041,23 +1032,21 @@ class, the PMC arguments are shifted down.
"attempt to call NULL function");
}
- func = FFI_FN(D2FPTR(orig_func));
+ func = (void (*)(void*, void*, void*))(orig_func);
func(INTERP, SELF, fb_info);
}
else {
PMC *positional, *arg_iter;
STRING *void_return;
- void **values, **middle_man, **pcc_values, **translation_pointers;
+ void **values, **middle_man = NULL, **pcc_ptr, **translation_pointers = NULL, **pcc_val = NULL;
void *return_data;
size_t count, i, j_offset;
char *tmp_sig;
ffi_cif *cif, pcc_cif;
ffi_type **pcc_args;
- PMC * pmc_3;
-
- cif = nci_info->cif;
- GET_ATTR_func(INTERP, SELF, func);
+ cif = (ffi_cif*)nci_info->cif;
+ func = (void (*)(void*, void*, void*))nci_info->func;
if (!cif) {
/* build the thunk only when necessary */
@@ -1066,48 +1055,66 @@ class, the PMC arguments are shifted down.
if (!cif && !func)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
+
"attempt to call NULL function");
}
if (nci_info->arity > 0) {
+ size_t pcc_argc, pcc_values_offset, pcc_values_size, values_size;
/* Function has arguments */
- values = mem_internal_allocate_n_zeroed_typed(nci_info->arity + 1, void*);
pcc_args = mem_internal_allocate_n_zeroed_typed(nci_info->arity + 4, ffi_type*);
-
- for (i = 0; i < nci_info->arity + 3; i++) {
- pcc_args[i] = &ffi_type_pointer;
+ tmp_sig = Parrot_str_to_cstring(interp, nci_info->pcc_params_signature);
+
+ pcc_args[0] = &ffi_type_pointer;
+ pcc_args[1] = &ffi_type_pointer;
+ pcc_args[2] = &ffi_type_pointer;
+ pcc_values_size = 0;
+ values_size = 0;
+ pcc_argc = Parrot_str_length(interp, nci_info->pcc_params_signature);
+
+ /* Add up the size of memory needed for the actual call */
+ for (i = 0; i < (size_t)nci_info->arity; i++) {
+ values_size += cif->arg_types[i]->size;
}
- if (ffi_prep_cif(&pcc_cif, FFI_DEFAULT_ABI, 3 + nci_info->arity,
+ pcc_ptr = (void**)mem_internal_allocate_n_zeroed_typed(pcc_argc + 4, void*);
+
+ /* Setup Parrot_pcc_fill_params_from_c_args required arguments */
+ pcc_ptr[0] = &interp;
+ pcc_ptr[1] = &call_object;
+ pcc_ptr[2] = &tmp_sig;
+
+ pcc_val = (void**)mem_internal_allocate_n_zeroed_typed(pcc_argc, void*);
+ values = (void**)mem_internal_allocate_zeroed(values_size + sizeof(void*));
+ /* Middle man is used to contain */
+ middle_man = mem_internal_allocate_n_zeroed_typed(nci_info->arity, void*);
+
+ /* Add up the size of the pcc arguments */
+ for (i = 0; i < pcc_argc; i++) {
+ pcc_args[i + 3] = &ffi_type_pointer;
+ if (tmp_sig[i] == 'N') {
+ pcc_val[i] = malloc(sizeof(FLOATVAL));
+ pcc_ptr[i+3] = &pcc_val[i];
+ }
+ else if (tmp_sig[i] == 'I') {
+ pcc_val[i] = malloc(sizeof(INTVAL));
+ pcc_ptr[i+3] = &pcc_val[i];
+ }
+ else if (tmp_sig[i] == 'P' || tmp_sig[i] == 'S') {
+ pcc_val[i] = malloc(sizeof(void*));
+ pcc_ptr[i+3] = &pcc_val[i];
+ }
+ }
+
+ if (ffi_prep_cif(&pcc_cif, FFI_DEFAULT_ABI, 3 + pcc_argc,
&ffi_type_void, pcc_args) != FFI_OK) {
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
"Bad signature generated for Parrot_pcc_fill_params_from_c_args in NCI");
}
- pcc_values = mem_internal_allocate_n_zeroed_typed(nci_info->arity + 4, void*);
- if (nci_info->pcc_params_signature)
- tmp_sig = Parrot_str_to_cstring(interp, nci_info->pcc_params_signature);
-
- /* Setup Parrot_pcc_fill_params_from_c_args required arguments */
- pcc_values[0] = &interp;
- pcc_values[1] = &call_object;
- pcc_values[2] = &tmp_sig;
-
- middle_man = mem_internal_allocate_n_zeroed_typed(nci_info->arity + 1, void*);
-
- /* Fill in arg references, J is magically inserted, so we need
- * to offset the other variables if a J is supposed to be used
- */
- j_offset = 0;
- for (i = 0; i < nci_info->arity; i++) {
- if (((char*)nci_info->arg_translation)[i] == 'J')
- j_offset++;
- middle_man[i] = &values[i + j_offset];
- pcc_values[i + 3] = &middle_man[i];
- }
-
- ffi_call(&pcc_cif, FFI_FN(Parrot_pcc_fill_params_from_c_args), NULL, pcc_values);
+ ffi_call(&pcc_cif, FFI_FN(Parrot_pcc_fill_params_from_c_args), NULL, pcc_ptr);
+
Parrot_str_free_cstring(tmp_sig);
/*
@@ -1117,103 +1124,107 @@ class, the PMC arguments are shifted down.
* but other transformations might apply later, like packing an
* object into a ManagedStruct
*/
+ j_offset = 0;
translation_pointers = mem_internal_allocate_n_zeroed_typed(nci_info->arity, void*);
- for (i = 0; i < nci_info->arity; i++) {
+ for (i = 0; i < (size_t)nci_info->arity; i++) {
switch ((INTVAL)((char*)nci_info->arg_translation)[i]) {
case 'J':
values[i] = &interp;
+ j_offset++;
break;
case 't':
- if (STRING_IS_NULL((STRING*)values[i])) {
+ if (STRING_IS_NULL(*(STRING**)pcc_val[i - j_offset])) {
translation_pointers[i] = (char*) NULL;
}
else {
- translation_pointers[i] = Parrot_str_to_cstring(interp, (STRING*)values[i]);
+ translation_pointers[i] = Parrot_str_to_cstring(interp, *(STRING**)pcc_val[i - j_offset]);
}
values[i] = &translation_pointers[i];
break;
case 'B':
- if (STRING_IS_NULL((STRING*)values[i])) {
+ if (STRING_IS_NULL(*(STRING**)pcc_val[i - j_offset])) {
translation_pointers[i] = (char*) NULL;
}
else {
- translation_pointers[i] = Parrot_str_to_cstring(interp, (STRING*)values[i]);
+ translation_pointers[i] = Parrot_str_to_cstring(interp, *(STRING**)pcc_val[i - j_offset]);
}
middle_man[i] = &translation_pointers[i];
values[i] = &middle_man[i];
break;
case 'b':
- values[i] = &Buffer_bufstart((STRING*)values[i]);
+ values[i] = &Buffer_bufstart(*(STRING**)pcc_val[i - j_offset]);
break;
case 'c':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(char);
- *((char**)translation_pointers)[i] = (char)*(INTVAL*)middle_man[i];
+ *((char**)translation_pointers)[i] = (char)*(INTVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case '2':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(pmc_holder_t);
- ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)middle_man[i];
- ((pmc_holder_t*)translation_pointers[i])->val = mem_internal_allocate_zeroed_typed(short);
- *((pmc_holder_t*)translation_pointers[i])->val = (short)VTABLE_get_integer(interp, *(PMC**)middle_man[i]);
- values[i] = &((pmc_holder_t*)translation_pointers[i])->val;
+ ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)pcc_val[i - j_offset];
+ ((pmc_holder_t*)translation_pointers[i])->ival = (INTVAL*)mem_internal_allocate_zeroed_typed(short);
+ *((pmc_holder_t*)translation_pointers[i])->ival = (short)VTABLE_get_integer(interp, *(PMC**)pcc_ptr[i - j_offset]);
+ values[i] = &((pmc_holder_t*)translation_pointers[i])->ival;
break;
case 's':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(short);
- *((short**)translation_pointers)[i] = (short)*(INTVAL*)middle_man[i];
+ *((short**)translation_pointers)[i] = (short)*(INTVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case '3':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(pmc_holder_t);
- ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)middle_man[i];
- ((pmc_holder_t*)translation_pointers[i])->val = mem_internal_allocate_zeroed_typed(int);
- *((pmc_holder_t*)translation_pointers[i])->val = (int)VTABLE_get_integer(interp, *(PMC**)middle_man[i]);
- values[i] = &((pmc_holder_t*)translation_pointers[i])->val;
+ ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)pcc_val[i - j_offset];
+ ((pmc_holder_t*)translation_pointers[i])->ival = (INTVAL*)mem_internal_allocate_zeroed_typed(int);
+ *((pmc_holder_t*)translation_pointers[i])->ival = (int)VTABLE_get_integer(interp, *(PMC**)pcc_val[i - j_offset]);
+ values[i] = &((pmc_holder_t*)translation_pointers[i])->ival;
break;
case 'i':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(int);
- *((int**)translation_pointers)[i] = (int)*(INTVAL*)middle_man[i];
+ *((int**)translation_pointers)[i] = (int)*(INTVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case '4':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(pmc_holder_t);
- ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)middle_man[i];
- ((pmc_holder_t*)translation_pointers[i])->val = mem_internal_allocate_zeroed_typed(long);
- *((pmc_holder_t*)translation_pointers[i])->val = (long)VTABLE_get_integer(interp, *(PMC**)middle_man[i]);
- values[i] = &((pmc_holder_t*)translation_pointers[i])->val;
+ ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)pcc_val[i - j_offset];
+ ((pmc_holder_t*)translation_pointers[i])->ival = (INTVAL*)mem_internal_allocate_zeroed_typed(long);
+ *((pmc_holder_t*)translation_pointers[i])->ival = (long)VTABLE_get_integer(interp, *(PMC**)pcc_val[i - j_offset]);
+ values[i] = &((pmc_holder_t*)translation_pointers[i])->ival;
break;
case 'l':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(long);
- *((long**)translation_pointers)[i] = (long)*(INTVAL*)middle_man[i];
+ *((long**)translation_pointers)[i] = (long)*(INTVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case 'q':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(long long);
- *((long long**)translation_pointers)[i] = (long long)*(INTVAL*)middle_man[i];
+ *((long long**)translation_pointers)[i] = (long long)*(INTVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case 'V':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(pmc_holder_t);
- ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)middle_man[i];
- ((pmc_holder_t*)translation_pointers[i])->val = mem_internal_allocate_zeroed_typed(void*);
- *((pmc_holder_t*)translation_pointers[i])->val = (void*)VTABLE_get_pointer(interp, *(PMC**)middle_man[i]);
- values[i] = &((pmc_holder_t*)translation_pointers[i])->val;
+ ((pmc_holder_t*)translation_pointers[i])->p = *(PMC**)pcc_val[i - j_offset];
+ ((pmc_holder_t*)translation_pointers[i])->pval = (void**)mem_internal_allocate_zeroed_typed(void*);
+ *((pmc_holder_t*)translation_pointers[i])->pval = PMC_IS_NULL(*(PMC**)pcc_ptr[i - j_offset]) ?
+ (void *)NULL : (void*)VTABLE_get_pointer(interp, *(PMC**)pcc_val[i - j_offset]);
+ values[i] = &((pmc_holder_t*)translation_pointers[i])->pval;
break;
case 'P':
- translation_pointers[i] = values[i];
+ translation_pointers[i] = *(PMC**)pcc_val[i - j_offset];
values[i] = &translation_pointers[i];
break;
case 'p':
- translation_pointers[i] = PMC_IS_NULL((PMC*)values[i]) ? (void *)NULL : VTABLE_get_pointer(interp, (PMC*)values[i]);
+ translation_pointers[i] = PMC_IS_NULL(*(PMC**)pcc_ptr[i - j_offset]) ?
+ (void *)NULL : VTABLE_get_pointer(interp, *(PMC**)pcc_val[i - j_offset]);
values[i] = &translation_pointers[i];
break;
case 'f':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(float);
- *((float**)translation_pointers)[i] = (float)*(FLOATVAL*)middle_man[i];
+ *((float**)translation_pointers)[i] = (float)*(FLOATVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case 'd':
translation_pointers[i] = mem_internal_allocate_zeroed_typed(double);
- *((double**)translation_pointers)[i] = *(FLOATVAL*)middle_man[i];
+ *((double**)translation_pointers)[i] = *(FLOATVAL*)pcc_val[i - j_offset];
values[i] = translation_pointers[i];
break;
case ' ':
@@ -1225,8 +1236,14 @@ class, the PMC arguments are shifted down.
if (pcc_args) {
mem_sys_free(pcc_args);
}
- if (pcc_values) {
- mem_sys_free(pcc_values);
+ if (pcc_ptr) {
+ mem_sys_free(pcc_ptr);
+ }
+ if (pcc_val) {
+ for (i = 0; i < pcc_argc; i++) {
+ mem_sys_free(pcc_val[i]);
+ }
+ mem_sys_free(pcc_val);
}
}
else {
@@ -1250,11 +1267,10 @@ class, the PMC arguments are shifted down.
case 'p':
{
PMC *final_destination = PMCNULL;
-
+
if (*(void**)return_data != NULL) {
final_destination = Parrot_pmc_new(interp, enum_class_UnManagedStruct);
VTABLE_set_pointer(interp, final_destination, *(void**)return_data);
-
}
ret_object = Parrot_pcc_build_call_from_c_args(interp,
call_object,
@@ -1305,38 +1321,46 @@ class, the PMC arguments are shifted down.
* Free memory used for cstrings,
* and any other translations that use temporary memory
*/
- for (i = 0; i < nci_info->arity; i++) {
+ for (i = 0; i < (size_t)nci_info->arity; i++) {
switch ((INTVAL)((char*)nci_info->arg_translation)[i]) {
case (INTVAL)'B':
if (translation_pointers[i]) {
- Parrot_str_free_cstring(translation_pointers[i]);
+ Parrot_str_free_cstring((char*)translation_pointers[i]);
}
break;
case (INTVAL)'t':
if (translation_pointers[i]) {
- Parrot_str_free_cstring(translation_pointers[i]);
+ Parrot_str_free_cstring((char*)translation_pointers[i]);
}
break;
case (INTVAL)'2':
- VTABLE_set_integer_native(interp, ((pmc_holder_t*)translation_pointers[i])->p, (INTVAL)*(short*)((pmc_holder_t*)translation_pointers[i])->val);
+ VTABLE_set_integer_native(interp,
+ ((pmc_holder_t*)translation_pointers[i])->p,
+ (INTVAL)*(short*)((pmc_holder_t*)translation_pointers[i])->ival);
if (translation_pointers[i]) {
mem_sys_free(translation_pointers[i]);
}
break;
case (INTVAL)'3':
- VTABLE_set_integer_native(interp, ((pmc_holder_t*)translation_pointers[i])->p, (INTVAL)*(int*)((pmc_holder_t*)translation_pointers[i])->val);
+ VTABLE_set_integer_native(interp,
+ ((pmc_holder_t*)translation_pointers[i])->p,
+ (INTVAL)*(int*)((pmc_holder_t*)translation_pointers[i])->ival);
if (translation_pointers[i]) {
mem_sys_free(translation_pointers[i]);
}
break;
case (INTVAL)'4':
- VTABLE_set_integer_native(interp, ((pmc_holder_t*)translation_pointers[i])->p, (INTVAL)*(long*)((pmc_holder_t*)translation_pointers[i])->val);
+ VTABLE_set_integer_native(interp,
+ ((pmc_holder_t*)translation_pointers[i])->p,
+ (INTVAL)*(long*)((pmc_holder_t*)translation_pointers[i])->ival);
if (translation_pointers[i]) {
mem_sys_free(translation_pointers[i]);
}
break;
case (INTVAL)'V':
- VTABLE_set_pointer(interp, ((pmc_holder_t*)translation_pointers[i])->p, (void*)*(void**)((pmc_holder_t*)translation_pointers[i])->val);
+ VTABLE_set_pointer(interp,
+ ((pmc_holder_t*)translation_pointers[i])->p,
+ (PMC*)*((pmc_holder_t*)translation_pointers[i])->pval);
if (translation_pointers[i]) {
mem_sys_free(translation_pointers[i]);
}
@@ -1476,3 +1500,4 @@ Updates by John Harrison, Summer 2010, GSoC.
*/
+
Please sign in to comment.
Something went wrong with that request. Please try again.