⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ffi.c

📁 gcc的组建
💻 C
📖 第 1 页 / 共 3 页
字号:
	      size_t first = (char *) gpr_end - (char *) next_arg;	      memcpy((char *) next_arg, (char *) *p_argv, first);	      memcpy((char *) rest, (char *) *p_argv + first,		     (*ptr)->size - first);	      next_arg = (unsigned long *) ((char *) rest + words * 8 - first);	    }	  else	    {	      char *where = (char *) next_arg;	      /* Structures with size less than eight bytes are passed		 left-padded.  */	      if ((*ptr)->size < 8)		where += 8 - (*ptr)->size;	      memcpy (where, (char *) *p_argv, (*ptr)->size);	      next_arg += words;	      if (next_arg == gpr_end)		next_arg = rest;	    }	  break;	case FFI_TYPE_UINT8:	  gprvalue = *(unsigned char *)*p_argv;	  goto putgpr;	case FFI_TYPE_SINT8:	  gprvalue = *(signed char *)*p_argv;	  goto putgpr;	case FFI_TYPE_UINT16:	  gprvalue = *(unsigned short *)*p_argv;	  goto putgpr;	case FFI_TYPE_SINT16:	  gprvalue = *(signed short *)*p_argv;	  goto putgpr;	case FFI_TYPE_UINT32:	  gprvalue = *(unsigned int *)*p_argv;	  goto putgpr;	case FFI_TYPE_INT:	case FFI_TYPE_SINT32:	  gprvalue = *(signed int *)*p_argv;	  goto putgpr;	case FFI_TYPE_UINT64:	case FFI_TYPE_SINT64:	case FFI_TYPE_POINTER:	  gprvalue = *(unsigned long *)*p_argv;	putgpr:	  *next_arg++ = gprvalue;	  if (next_arg == gpr_end)	    next_arg = rest;	  break;	}    }  FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS	     || (next_arg >= gpr_base && next_arg <= gpr_base + 4));}/* Perform machine dependent cif processing */ffi_status ffi_prep_cif_machdep(ffi_cif *cif){  /* All this is for the SYSV and LINUX64 ABI.  */  int i;  ffi_type **ptr;  unsigned bytes;  int fparg_count = 0, intarg_count = 0;  unsigned flags = 0;  unsigned struct_copy_size = 0;  unsigned type = cif->rtype->type;  unsigned size = cif->rtype->size;  if (cif->abi != FFI_LINUX64)    {      /* All the machine-independent calculation of cif->bytes will be wrong.	 Redo the calculation for SYSV.  */      /* Space for the frame pointer, callee's LR, and the asm's temp regs.  */      bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof(int);      /* Space for the GPR registers.  */      bytes += NUM_GPR_ARG_REGISTERS * sizeof(int);    }  else    {      /* 64-bit ABI.  */      /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp	 regs.  */      bytes = (6 + ASM_NEEDS_REGISTERS64) * sizeof(long);      /* Space for the mandatory parm save area and general registers.  */      bytes += 2 * NUM_GPR_ARG_REGISTERS64 * sizeof(long);#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE      if (type == FFI_TYPE_LONGDOUBLE)	type = FFI_TYPE_DOUBLE;#endif    }  /* Return value handling.  The rules for SYSV are as follows:     - 32-bit (or less) integer values are returned in gpr3;     - Structures of size <= 4 bytes also returned in gpr3;     - 64-bit integer values and structures between 5 and 8 bytes are returned     in gpr3 and gpr4;     - Single/double FP values are returned in fpr1;     - Larger structures and long double (if not equivalent to double) values     are allocated space and a pointer is passed as the first argument.     For LINUX64:     - integer values in gpr3;     - Structures/Unions by reference;     - Single/double FP values in fpr1, long double in fpr1,fpr2.  */  switch (type)    {    case FFI_TYPE_DOUBLE:      flags |= FLAG_RETURNS_64BITS;      /* Fall through.  */    case FFI_TYPE_FLOAT:      flags |= FLAG_RETURNS_FP;      break;    case FFI_TYPE_UINT64:    case FFI_TYPE_SINT64:      flags |= FLAG_RETURNS_64BITS;      break;    case FFI_TYPE_STRUCT:      if (cif->abi == FFI_SYSV)	{	  /* The final SYSV ABI says that structures smaller or equal 8 bytes	     are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them	     in memory.  */	  /* Treat structs with size <= 8 bytes.  */	  if (size <= 8) {	    flags |= FLAG_RETURNS_SMST;	    /* These structs are returned in r3. We pack the type and the	       precalculated shift value (needed in the sysv.S) into flags.	       The same applies for the structs returned in r3/r4.  */	    if (size <= 4) {	      flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 1 )		| (8 * (4 - size) << 4);	      break;	    }	    /* These structs are returned in r3 and r4. See above.   */	    if  (size <= 8) {	      flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 2 )		| (8 * (8 - size) << 4);	    break;	    }	  }	}      /* else fall through.  */#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE    case FFI_TYPE_LONGDOUBLE:      if (type == FFI_TYPE_LONGDOUBLE && cif->abi == FFI_LINUX64)	{	  flags |= FLAG_RETURNS_128BITS;	  flags |= FLAG_RETURNS_FP;	  break;	}#endif      intarg_count++;      flags |= FLAG_RETVAL_REFERENCE;      /* Fall through.  */    case FFI_TYPE_VOID:      flags |= FLAG_RETURNS_NOTHING;      break;    default:      /* Returns 32-bit integer, or similar.  Nothing to do here.  */      break;    }  if (cif->abi != FFI_LINUX64)    /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the       first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest       goes on the stack.  Structures and long doubles (if not equivalent       to double) are passed as a pointer to a copy of the structure.       Stuff on the stack needs to keep proper alignment.  */    for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)      {	switch ((*ptr)->type)	  {	  case FFI_TYPE_FLOAT:	    fparg_count++;	    /* floating singles are not 8-aligned on stack */	    break;	  case FFI_TYPE_DOUBLE:	    fparg_count++;	    /* If this FP arg is going on the stack, it must be	       8-byte-aligned.  */	    if (fparg_count > NUM_FPR_ARG_REGISTERS		&& intarg_count >= NUM_GPR_ARG_REGISTERS		&& intarg_count % 2 != 0)	      intarg_count++;	    break;	  case FFI_TYPE_UINT64:	  case FFI_TYPE_SINT64:	    /* 'long long' arguments are passed as two words, but	       either both words must fit in registers or both go	       on the stack.  If they go on the stack, they must	       be 8-byte-aligned.	       Also, only certain register pairs can be used for	       passing long long int -- specifically (r3,r4), (r5,r6),	       (r7,r8), (r9,r10).	    */	    if (intarg_count == NUM_GPR_ARG_REGISTERS-1		|| intarg_count%2 != 0)	      intarg_count++;	    intarg_count += 2;	    break;	  case FFI_TYPE_STRUCT:#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE	  case FFI_TYPE_LONGDOUBLE:#endif	    /* We must allocate space for a copy of these to enforce	       pass-by-value.  Pad the space up to a multiple of 16	       bytes (the maximum alignment required for anything under	       the SYSV ABI).  */	    struct_copy_size += ((*ptr)->size + 15) & ~0xF;	    /* Fall through (allocate space for the pointer).  */	  default:	    /* Everything else is passed as a 4-byte word in a GPR, either	       the object itself or a pointer to it.  */	    intarg_count++;	    break;	  }      }  else    for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)      {	switch ((*ptr)->type)	  {#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE	  case FFI_TYPE_LONGDOUBLE:	    fparg_count += 2;	    intarg_count += 2;	    break;#endif	  case FFI_TYPE_FLOAT:	  case FFI_TYPE_DOUBLE:	    fparg_count++;	    intarg_count++;	    break;	  case FFI_TYPE_STRUCT:	    intarg_count += ((*ptr)->size + 7) / 8;	    break;	  default:	    /* Everything else is passed as a 8-byte word in a GPR, either	       the object itself or a pointer to it.  */	    intarg_count++;	    break;	  }      }  if (fparg_count != 0)    flags |= FLAG_FP_ARGUMENTS;  if (intarg_count > 4)    flags |= FLAG_4_GPR_ARGUMENTS;  if (struct_copy_size != 0)    flags |= FLAG_ARG_NEEDS_COPY;  if (cif->abi != FFI_LINUX64)    {      /* Space for the FPR registers, if needed.  */      if (fparg_count != 0)	bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);      /* Stack space.  */      if (intarg_count > NUM_GPR_ARG_REGISTERS)	bytes += (intarg_count - NUM_GPR_ARG_REGISTERS) * sizeof(int);      if (fparg_count > NUM_FPR_ARG_REGISTERS)	bytes += (fparg_count - NUM_FPR_ARG_REGISTERS) * sizeof(double);    }  else    {      /* Space for the FPR registers, if needed.  */      if (fparg_count != 0)	bytes += NUM_FPR_ARG_REGISTERS64 * sizeof(double);      /* Stack space.  */      if (intarg_count > NUM_GPR_ARG_REGISTERS64)	bytes += (intarg_count - NUM_GPR_ARG_REGISTERS64) * sizeof(long);    }  /* The stack space allocated needs to be a multiple of 16 bytes.  */  bytes = (bytes + 15) & ~0xF;  /* Add in the space for the copied structures.  */  bytes += struct_copy_size;  cif->flags = flags;  cif->bytes = bytes;  return FFI_OK;}/*@-declundef@*//*@-exportheader@*/extern void ffi_call_SYSV(/*@out@*/ extended_cif *,			  unsigned, unsigned,			  /*@out@*/ unsigned *,			  void (*fn)());extern void FFI_HIDDEN ffi_call_LINUX64(/*@out@*/ extended_cif *,					unsigned long, unsigned long,					/*@out@*/ unsigned long *,					void (*fn)());/*@=declundef@*//*@=exportheader@*/void ffi_call(/*@dependent@*/ ffi_cif *cif,	      void (*fn)(),	      /*@out@*/ void *rvalue,	      /*@dependent@*/ void **avalue){  extended_cif ecif;  ecif.cif = cif;  ecif.avalue = avalue;  /* If the return value is a struct and we don't have a return	*/  /* value address then we need to make one		        */  if ((rvalue == NULL) &&      (cif->rtype->type == FFI_TYPE_STRUCT))    {      /*@-sysunrecog@*/      ecif.rvalue = alloca(cif->rtype->size);      /*@=sysunrecog@*/    }  else    ecif.rvalue = rvalue;  switch (cif->abi)    {#ifndef POWERPC64    case FFI_SYSV:    case FFI_GCC_SYSV:      /*@-usedef@*/      ffi_call_SYSV(&ecif, -cif->bytes,		    cif->flags, ecif.rvalue, fn);      /*@=usedef@*/      break;#else    case FFI_LINUX64:      /*@-usedef@*/      ffi_call_LINUX64(&ecif, -(long) cif->bytes,		       cif->flags, ecif.rvalue, fn);      /*@=usedef@*/      break;#endif    default:      FFI_ASSERT(0);      break;    }}#ifndef POWERPC64static void flush_icache(char *, int);#define MIN_CACHE_LINE_SIZE 8static void flush_icache(char * addr1, int size){  int i;  char * addr;  for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE) {    addr = addr1 + i;    __asm__ volatile ("icbi 0,%0;" "dcbf 0,%0;" : : "r"(addr) : "memory");  }  addr = addr1 + size - 1;  __asm__ volatile ("icbi 0,%0;" "dcbf 0,%0;" "sync;" "isync;" : : "r"(addr) : "memory");}#endifffi_statusffi_prep_closure (ffi_closure* closure,		  ffi_cif* cif,		  void (*fun)(ffi_cif*, void*, void**, void*),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -