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

📄 ffi.c

📁 linux下编程用 编译软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -----------------------------------------------------------------------   ffi.c - Copyright (c) 2002, 2003, 2004, 2005 Kaz Kojima      SuperH Foreign Function Interface    Permission is hereby granted, free of charge, to any person obtaining   a copy of this software and associated documentation files (the   ``Software''), to deal in the Software without restriction, including   without limitation the rights to use, copy, modify, merge, publish,   distribute, sublicense, and/or sell copies of the Software, and to   permit persons to whom the Software is furnished to do so, subject to   the following conditions:   The above copyright notice and this permission notice shall be included   in all copies or substantial portions of the Software.   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR   OTHER DEALINGS IN THE SOFTWARE.   ----------------------------------------------------------------------- */#include <ffi.h>#include <ffi_common.h>#include <stdlib.h>#define NGREGARG 4#if defined(__SH4__)#define NFREGARG 8#endif#if defined(__HITACHI__)#define STRUCT_VALUE_ADDRESS_WITH_ARG 1#else#define STRUCT_VALUE_ADDRESS_WITH_ARG 0#endif/* If the structure has essentialy an unique element, return its type.  */static intsimple_type (ffi_type *arg){  if (arg->type != FFI_TYPE_STRUCT)    return arg->type;  else if (arg->elements[1])    return FFI_TYPE_STRUCT;  return simple_type (arg->elements[0]);}static intreturn_type (ffi_type *arg){  unsigned short type;  if (arg->type != FFI_TYPE_STRUCT)    return arg->type;  type = simple_type (arg->elements[0]);  if (! arg->elements[1])    {      switch (type)	{	case FFI_TYPE_SINT8:	case FFI_TYPE_UINT8:	case FFI_TYPE_SINT16:	case FFI_TYPE_UINT16:	case FFI_TYPE_SINT32:	case FFI_TYPE_UINT32:	  return FFI_TYPE_INT;	default:	  return type;	}    }  /* gcc uses r0/r1 pair for some kind of structures.  */  if (arg->size <= 2 * sizeof (int))    {      int i = 0;      ffi_type *e;      while ((e = arg->elements[i++]))	{	  type = simple_type (e);	  switch (type)	    {	    case FFI_TYPE_SINT32:	    case FFI_TYPE_UINT32:	    case FFI_TYPE_INT:	    case FFI_TYPE_FLOAT:	      return FFI_TYPE_UINT64;	    default:	      break;	    }	}    }  return FFI_TYPE_STRUCT;}/* ffi_prep_args is called by the assembly routine once stack space   has been allocated for the function's arguments *//*@-exportheader@*/void ffi_prep_args(char *stack, extended_cif *ecif)/*@=exportheader@*/{  register unsigned int i;  register int tmp;  register unsigned int avn;  register void **p_argv;  register char *argp;  register ffi_type **p_arg;  int greg, ireg;#if defined(__SH4__)  int freg = 0;#endif  tmp = 0;  argp = stack;  if (return_type (ecif->cif->rtype) == FFI_TYPE_STRUCT)    {      *(void **) argp = ecif->rvalue;      argp += 4;      ireg = STRUCT_VALUE_ADDRESS_WITH_ARG ? 1 : 0;    }  else    ireg = 0;  /* Set arguments for registers.  */  greg = ireg;  avn = ecif->cif->nargs;  p_argv = ecif->avalue;  for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)    {      size_t z;      z = (*p_arg)->size;      if (z < sizeof(int))	{	  if (greg++ >= NGREGARG)	    continue;	  z = sizeof(int);	  switch ((*p_arg)->type)	    {	    case FFI_TYPE_SINT8:	      *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);	      break;  	    case FFI_TYPE_UINT8:	      *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);	      break;  	    case FFI_TYPE_SINT16:	      *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);	      break;  	    case FFI_TYPE_UINT16:	      *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);	      break;  	    case FFI_TYPE_STRUCT:	      *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);	      break;	    default:	      FFI_ASSERT(0);	    }	  argp += z;	}      else if (z == sizeof(int))	{#if defined(__SH4__)	  if ((*p_arg)->type == FFI_TYPE_FLOAT)	    {	      if (freg++ >= NFREGARG)		continue;	    }	  else#endif	    {	      if (greg++ >= NGREGARG)		continue;	    }	  *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);	  argp += z;	}#if defined(__SH4__)      else if ((*p_arg)->type == FFI_TYPE_DOUBLE)	{	  if (freg + 1 >= NFREGARG)	    continue;	  freg = (freg + 1) & ~1;	  freg += 2;	  memcpy (argp, *p_argv, z);	  argp += z;	}#endif      else	{	  int n = (z + sizeof (int) - 1) / sizeof (int);#if defined(__SH4__)	  if (greg + n - 1 >= NGREGARG)	    continue;#else	  if (greg >= NGREGARG)	    continue;#endif	  greg += n;	  memcpy (argp, *p_argv, z);	  argp += n * sizeof (int);	}    }  /* Set arguments on stack.  */  greg = ireg;#if defined(__SH4__)  freg = 0;#endif  p_argv = ecif->avalue;  for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)    {      size_t z;      z = (*p_arg)->size;      if (z < sizeof(int))	{	  if (greg++ < NGREGARG)	    continue;	  z = sizeof(int);	  switch ((*p_arg)->type)	    {	    case FFI_TYPE_SINT8:	      *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);	      break;  	    case FFI_TYPE_UINT8:	      *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);	      break;  	    case FFI_TYPE_SINT16:	      *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);	      break;  	    case FFI_TYPE_UINT16:	      *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);	      break;  	    case FFI_TYPE_STRUCT:	      *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);	      break;	    default:	      FFI_ASSERT(0);	    }	  argp += z;	}      else if (z == sizeof(int))	{#if defined(__SH4__)	  if ((*p_arg)->type == FFI_TYPE_FLOAT)	    {	      if (freg++ < NFREGARG)		continue;	    }	  else#endif	    {	      if (greg++ < NGREGARG)		continue;	    }	  *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);	  argp += z;	}#if defined(__SH4__)      else if ((*p_arg)->type == FFI_TYPE_DOUBLE)	{	  if (freg + 1 < NFREGARG)	    {	      freg = (freg + 1) & ~1;	      freg += 2;	      continue;	    }	  memcpy (argp, *p_argv, z);	  argp += z;	}#endif      else	{	  int n = (z + sizeof (int) - 1) / sizeof (int);	  if (greg + n - 1 < NGREGARG)	    {	      greg += n;	      continue;	    }#if (! defined(__SH4__))	  else if (greg < NGREGARG)	    {	      greg = NGREGARG;	      continue;	    }#endif	  memcpy (argp, *p_argv, z);	  argp += n * sizeof (int);	}    }  return;}/* Perform machine dependent cif processing */ffi_status ffi_prep_cif_machdep(ffi_cif *cif){  int i, j;  int size, type;  int n, m;  int greg;#if defined(__SH4__)  int freg = 0;#endif  cif->flags = 0;  greg = ((return_type (cif->rtype) == FFI_TYPE_STRUCT) &&	  STRUCT_VALUE_ADDRESS_WITH_ARG) ? 1 : 0;#if defined(__SH4__)  for (i = j = 0; i < cif->nargs && j < 12; i++)    {      type = (cif->arg_types)[i]->type;      switch (type)	{	case FFI_TYPE_FLOAT:	  if (freg >= NFREGARG)	    continue;	  freg++;	  cif->flags += ((cif->arg_types)[i]->type) << (2 * j);	  j++;	  break;	case FFI_TYPE_DOUBLE:	  if ((freg + 1) >= NFREGARG)	    continue;	  freg = (freg + 1) & ~1;	  freg += 2;	  cif->flags += ((cif->arg_types)[i]->type) << (2 * j);	  j++;	  break;	      	default:	  size = (cif->arg_types)[i]->size;	  n = (size + sizeof (int) - 1) / sizeof (int);	  if (greg + n - 1 >= NGREGARG)		continue;	  greg += n;

⌨️ 快捷键说明

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