iso_c_binding.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Implementation of the ISO_C_BINDING library helper functions.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Contributed by Christopher Rickett.
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* Implement the functions and subroutines provided by the intrinsic
  21. iso_c_binding module. */
  22. #include "libgfortran.h"
  23. #include "iso_c_binding.h"
  24. #include <stdlib.h>
  25. /* Set the fields of a Fortran pointer descriptor to point to the
  26. given C address. It uses c_f_pointer_u0 for the common
  27. fields, and will set up the information necessary if this C address
  28. is to an array (i.e., offset, type, element size). The parameter
  29. c_ptr_in represents the C address to have Fortran point to. The
  30. parameter f_ptr_out is the Fortran pointer to associate with the C
  31. address. The parameter shape is a one-dimensional array of integers
  32. specifying the upper bound(s) of the array pointed to by the given C
  33. address, if applicable. The shape parameter is optional in Fortran,
  34. which will cause it to come in here as NULL. The parameter type is
  35. the type of the data being pointed to (i.e.,libgfortran.h). The
  36. elem_size parameter is the size, in bytes, of the data element being
  37. pointed to. If the address is for an array, then the size needs to
  38. be the size of a single element (i.e., for an array of doubles, it
  39. needs to be the number of bytes for the size of one double). */
  40. void
  41. ISO_C_BINDING_PREFIX (c_f_pointer) (void *c_ptr_in,
  42. gfc_array_void *f_ptr_out,
  43. const array_t *shape,
  44. int type, int elemSize)
  45. {
  46. if (shape != NULL)
  47. {
  48. f_ptr_out->offset = 0;
  49. /* Set the necessary dtype field for all pointers. */
  50. f_ptr_out->dtype = 0;
  51. /* Put in the element size. */
  52. f_ptr_out->dtype = f_ptr_out->dtype | (elemSize << GFC_DTYPE_SIZE_SHIFT);
  53. /* Set the data type (e.g., BT_INTEGER). */
  54. f_ptr_out->dtype = f_ptr_out->dtype | (type << GFC_DTYPE_TYPE_SHIFT);
  55. }
  56. /* Use the generic version of c_f_pointer to set common fields. */
  57. ISO_C_BINDING_PREFIX (c_f_pointer_u0) (c_ptr_in, f_ptr_out, shape);
  58. }
  59. /* A generic function to set the common fields of all descriptors, no
  60. matter whether it's to a scalar or an array. Access is via the array
  61. descrptor macros. Parameter shape is a rank 1 array of integers
  62. containing the upper bound of each dimension of what f_ptr_out
  63. points to. The length of this array must be EXACTLY the rank of
  64. what f_ptr_out points to, as required by the draft (J3/04-007). If
  65. f_ptr_out points to a scalar, then this parameter will be NULL. */
  66. void
  67. ISO_C_BINDING_PREFIX (c_f_pointer_u0) (void *c_ptr_in,
  68. gfc_array_void *f_ptr_out,
  69. const array_t *shape)
  70. {
  71. int i = 0;
  72. int shapeSize = 0;
  73. GFC_DESCRIPTOR_DATA (f_ptr_out) = c_ptr_in;
  74. if (shape != NULL)
  75. {
  76. index_type source_stride, size;
  77. index_type str = 1;
  78. char *p;
  79. f_ptr_out->offset = str;
  80. shapeSize = 0;
  81. p = shape->base_addr;
  82. size = GFC_DESCRIPTOR_SIZE(shape);
  83. source_stride = GFC_DESCRIPTOR_STRIDE_BYTES(shape,0);
  84. /* shape's length (rank of the output array) */
  85. shapeSize = GFC_DESCRIPTOR_EXTENT(shape,0);
  86. for (i = 0; i < shapeSize; i++)
  87. {
  88. index_type ub;
  89. /* Have to allow for the SHAPE array to be any valid kind for
  90. an INTEGER type. */
  91. switch (size)
  92. {
  93. #ifdef HAVE_GFC_INTEGER_1
  94. case 1:
  95. ub = *((GFC_INTEGER_1 *) p);
  96. break;
  97. #endif
  98. #ifdef HAVE_GFC_INTEGER_2
  99. case 2:
  100. ub = *((GFC_INTEGER_2 *) p);
  101. break;
  102. #endif
  103. #ifdef HAVE_GFC_INTEGER_4
  104. case 4:
  105. ub = *((GFC_INTEGER_4 *) p);
  106. break;
  107. #endif
  108. #ifdef HAVE_GFC_INTEGER_8
  109. case 8:
  110. ub = *((GFC_INTEGER_8 *) p);
  111. break;
  112. #endif
  113. #ifdef HAVE_GFC_INTEGER_16
  114. case 16:
  115. ub = *((GFC_INTEGER_16 *) p);
  116. break;
  117. #endif
  118. default:
  119. internal_error (NULL, "c_f_pointer_u0: Invalid size");
  120. }
  121. p += source_stride;
  122. if (i != 0)
  123. {
  124. str = str * GFC_DESCRIPTOR_EXTENT(f_ptr_out,i-1);
  125. f_ptr_out->offset += str;
  126. }
  127. /* Lower bound is 1, as specified by the draft. */
  128. GFC_DIMENSION_SET(f_ptr_out->dim[i], 1, ub, str);
  129. }
  130. f_ptr_out->offset *= -1;
  131. /* All we know is the rank, so set it, leaving the rest alone.
  132. Make NO assumptions about the state of dtype coming in! If we
  133. shift right by TYPE_SHIFT bits we'll throw away the existing
  134. rank. Then, shift left by the same number to shift in zeros
  135. and or with the new rank. */
  136. f_ptr_out->dtype = ((f_ptr_out->dtype >> GFC_DTYPE_TYPE_SHIFT)
  137. << GFC_DTYPE_TYPE_SHIFT) | shapeSize;
  138. }
  139. }
  140. /* Sets the descriptor fields for a Fortran pointer to a derived type,
  141. using c_f_pointer_u0 for the majority of the work. */
  142. void
  143. ISO_C_BINDING_PREFIX (c_f_pointer_d0) (void *c_ptr_in,
  144. gfc_array_void *f_ptr_out,
  145. const array_t *shape)
  146. {
  147. /* Set the common fields. */
  148. ISO_C_BINDING_PREFIX (c_f_pointer_u0) (c_ptr_in, f_ptr_out, shape);
  149. /* Preserve the size and rank bits, but reset the type. */
  150. if (shape != NULL)
  151. {
  152. f_ptr_out->dtype = f_ptr_out->dtype & (~GFC_DTYPE_TYPE_MASK);
  153. f_ptr_out->dtype = f_ptr_out->dtype
  154. | (BT_DERIVED << GFC_DTYPE_TYPE_SHIFT);
  155. }
  156. }