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

📄 ffi64.c

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -----------------------------------------------------------------------   ffi.c - Copyright (c) 2002  Bo Thorsen <bo@suse.de>      x86-64 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>#include <stdarg.h>/* ffi_prep_args is called by the assembly routine once stack space   has been allocated for the function's arguments */#ifdef __x86_64__#define MAX_GPR_REGS 6#define MAX_SSE_REGS 8typedef struct{  /* Registers for argument passing.  */  long gpr[MAX_GPR_REGS];  __int128_t sse[MAX_SSE_REGS];  /* Stack space for arguments.  */  char argspace[0];} stackLayout;/* All reference to register classes here is identical to the code in   gcc/config/i386/i386.c. Do *not* change one without the other.  *//* Register class used for passing given 64bit part of the argument.   These represent classes as documented by the PS ABI, with the exception   of SSESF, SSEDF classes, that are basically SSE class, just gcc will   use SF or DFmode move instead of DImode to avoid reformating penalties.   Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves   whenever possible (upper half does contain padding). */enum x86_64_reg_class  {    X86_64_NO_CLASS,    X86_64_INTEGER_CLASS,    X86_64_INTEGERSI_CLASS,    X86_64_SSE_CLASS,    X86_64_SSESF_CLASS,    X86_64_SSEDF_CLASS,    X86_64_SSEUP_CLASS,    X86_64_X87_CLASS,    X86_64_X87UP_CLASS,    X86_64_MEMORY_CLASS  };#define MAX_CLASSES 4/* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal   of this code is to classify each 8bytes of incoming argument by the register   class and assign registers accordingly.  *//* Return the union class of CLASS1 and CLASS2.   See the x86-64 PS ABI for details.  */static enum x86_64_reg_classmerge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2){  /* Rule #1: If both classes are equal, this is the resulting class.  */  if (class1 == class2)    return class1;  /* Rule #2: If one of the classes is NO_CLASS, the resulting class is     the other class.  */  if (class1 == X86_64_NO_CLASS)    return class2;  if (class2 == X86_64_NO_CLASS)    return class1;  /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */  if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)    return X86_64_MEMORY_CLASS;  /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */  if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)      || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))    return X86_64_INTEGERSI_CLASS;  if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS      || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)    return X86_64_INTEGER_CLASS;  /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used.  */  if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS      || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)    return X86_64_MEMORY_CLASS;  /* Rule #6: Otherwise class SSE is used.  */  return X86_64_SSE_CLASS;}/* Classify the argument of type TYPE and mode MODE.   CLASSES will be filled by the register class used to pass each word   of the operand.  The number of words is returned.  In case the parameter   should be passed in memory, 0 is returned. As a special case for zero   sized containers, classes[0] will be NO_CLASS and 1 is returned.   See the x86-64 PS ABI for details.*/static intclassify_argument (ffi_type *type, enum x86_64_reg_class classes[],		   int *byte_offset){  /* First, align to the right place.  */  *byte_offset = ALIGN(*byte_offset, type->alignment);  switch (type->type)    {    case FFI_TYPE_UINT8:    case FFI_TYPE_SINT8:    case FFI_TYPE_UINT16:    case FFI_TYPE_SINT16:    case FFI_TYPE_UINT32:    case FFI_TYPE_SINT32:    case FFI_TYPE_UINT64:    case FFI_TYPE_SINT64:    case FFI_TYPE_POINTER:      if (((*byte_offset) % 8 + type->size) <= 4)	classes[0] = X86_64_INTEGERSI_CLASS;      else	classes[0] = X86_64_INTEGER_CLASS;      return 1;    case FFI_TYPE_FLOAT:      if (((*byte_offset) % 8) == 0)	classes[0] = X86_64_SSESF_CLASS;      else	classes[0] = X86_64_SSE_CLASS;      return 1;    case FFI_TYPE_DOUBLE:      classes[0] = X86_64_SSEDF_CLASS;      return 1;    case FFI_TYPE_LONGDOUBLE:      classes[0] = X86_64_X87_CLASS;      classes[1] = X86_64_X87UP_CLASS;      return 2;    case FFI_TYPE_STRUCT:      {	const int UNITS_PER_WORD = 8;	int words = (type->size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;	ffi_type **ptr; 	int i;	enum x86_64_reg_class subclasses[MAX_CLASSES];	/* If the struct is larger than 16 bytes, pass it on the stack.  */	if (type->size > 16)	  return 0;	for (i = 0; i < words; i++)	  classes[i] = X86_64_NO_CLASS;	/* Merge the fields of structure.  */	for (ptr=type->elements; (*ptr)!=NULL; ptr++)	  {	    int num;	    num = classify_argument (*ptr, subclasses, byte_offset);	    if (num == 0)	      return 0;	    for (i = 0; i < num; i++)	      {		int pos = *byte_offset / 8;		classes[i + pos] =		  merge_classes (subclasses[i], classes[i + pos]);	      }	    if ((*ptr)->type != FFI_TYPE_STRUCT)	      *byte_offset += (*ptr)->size;	  }	/* Final merger cleanup.  */	for (i = 0; i < words; i++)	  {	    /* If one class is MEMORY, everything should be passed in	       memory.  */	    if (classes[i] == X86_64_MEMORY_CLASS)	      return 0;	    /* The X86_64_SSEUP_CLASS should be always preceded by	       X86_64_SSE_CLASS.  */	    if (classes[i] == X86_64_SSEUP_CLASS		&& (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))	      classes[i] = X86_64_SSE_CLASS;	    /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */	    if (classes[i] == X86_64_X87UP_CLASS		&& (i == 0 || classes[i - 1] != X86_64_X87_CLASS))	      classes[i] = X86_64_SSE_CLASS;	  }	return words;      }    default:      FFI_ASSERT(0);    }  return 0; /* Never reached.  */}/* Examine the argument and return set number of register required in each   class.  Return 0 iff parameter should be passed in memory.  */static intexamine_argument (ffi_type *type, int in_return, int *int_nregs,int *sse_nregs){  enum x86_64_reg_class class[MAX_CLASSES];  int offset = 0;  int n;  n = classify_argument (type, class, &offset);  if (n == 0)    return 0;  *int_nregs = 0;  *sse_nregs = 0;  for (n--; n>=0; n--)    switch (class[n])      {      case X86_64_INTEGER_CLASS:      case X86_64_INTEGERSI_CLASS:	(*int_nregs)++;	break;      case X86_64_SSE_CLASS:      case X86_64_SSESF_CLASS:      case X86_64_SSEDF_CLASS:	(*sse_nregs)++;	break;      case X86_64_NO_CLASS:      case X86_64_SSEUP_CLASS:	break;      case X86_64_X87_CLASS:      case X86_64_X87UP_CLASS:	if (!in_return)	  return 0;	break;      default:	abort ();      }  return 1;}/* Functions to load floats and double to an SSE register placeholder.  */extern void float2sse (float, __int128_t *);extern void double2sse (double, __int128_t *);extern void floatfloat2sse (void *, __int128_t *);/* Functions to put the floats and doubles back.  */extern float sse2float (__int128_t *);extern double sse2double (__int128_t *);extern void sse2floatfloat(__int128_t *, void *);/*@-exportheader@*/voidffi_prep_args (stackLayout *stack, extended_cif *ecif)/*@=exportheader@*/{  int gprcount, ssecount, i, g, s;  void **p_argv;  void *argp = &stack->argspace;  ffi_type **p_arg;  /* First check if the return value should be passed in memory. If so,     pass the pointer as the first argument.  */  gprcount = ssecount = 0;  if (ecif->cif->rtype->type != FFI_TYPE_VOID       && examine_argument (ecif->cif->rtype, 1, &g, &s) == 0)    (void *)stack->gpr[gprcount++] = ecif->rvalue;  for (i=ecif->cif->nargs, p_arg=ecif->cif->arg_types, p_argv = ecif->avalue;       i!=0; i--, p_arg++, p_argv++)    {      int in_register = 0;      switch ((*p_arg)->type)	{	case FFI_TYPE_SINT8:	case FFI_TYPE_SINT16:	case FFI_TYPE_SINT32:	case FFI_TYPE_SINT64:	case FFI_TYPE_UINT8:	case FFI_TYPE_UINT16:	case FFI_TYPE_UINT32:	case FFI_TYPE_UINT64:	case FFI_TYPE_POINTER:	  if (gprcount < MAX_GPR_REGS)	    {	      stack->gpr[gprcount] = 0;	      stack->gpr[gprcount++] = *(long long *)(*p_argv);	      in_register = 1;	    }	  break;	case FFI_TYPE_FLOAT:	  if (ssecount < MAX_SSE_REGS)	    {	      float2sse (*(float *)(*p_argv), &stack->sse[ssecount++]);	      in_register = 1;	    }	  break;	case FFI_TYPE_DOUBLE:	  if (ssecount < MAX_SSE_REGS)	    {	      double2sse (*(double *)(*p_argv), &stack->sse[ssecount++]);	      in_register = 1;	    }	  break;	}      if (in_register)	continue;      /* Either all places in registers where filled, or this is a	 type that potentially goes into a memory slot.  */      if (examine_argument (*p_arg, 0, &g, &s) == 0	  || gprcount + g > MAX_GPR_REGS || ssecount + s > MAX_SSE_REGS)	{	  /* Pass this argument in memory.  */	  argp = (void *)ALIGN(argp, (*p_arg)->alignment);	  memcpy (argp, *p_argv, (*p_arg)->size);	  argp += (*p_arg)->size;	}      else	{	  /* All easy cases are eliminated. Now fire the big guns.  */	  enum x86_64_reg_class classes[MAX_CLASSES];	  int offset = 0, j, num;	  void *a;

⌨️ 快捷键说明

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