Esempio n. 1
0
/**
 * Return the size of the LLVMType in bits.
 * XXX this function doesn't necessarily handle all LLVM types.
 */
unsigned
lp_sizeof_llvm_type(LLVMTypeRef t)
{
   LLVMTypeKind k = LLVMGetTypeKind(t);

   switch (k) {
   case LLVMIntegerTypeKind:
      return LLVMGetIntTypeWidth(t);
   case LLVMFloatTypeKind:
      return 8 * sizeof(float);
   case LLVMDoubleTypeKind:
      return 8 * sizeof(double);
   case LLVMVectorTypeKind:
      {
         LLVMTypeRef elem = LLVMGetElementType(t);
         unsigned len = LLVMGetVectorSize(t);
         return len * lp_sizeof_llvm_type(elem);
      }
      break;
   case LLVMArrayTypeKind:
      {
         LLVMTypeRef elem = LLVMGetElementType(t);
         unsigned len = LLVMGetArrayLength(t);
         return len * lp_sizeof_llvm_type(elem);
      }
      break;
   default:
      assert(0 && "Unexpected type in lp_get_llvm_type_size()");
      return 0;
   }
}
Esempio n. 2
0
/**
 * Print an LLVMTypeRef.  Like LLVMDumpValue().  For debugging.
 */
void
lp_dump_llvmtype(LLVMTypeRef t)
{
   LLVMTypeKind k = LLVMGetTypeKind(t);

   if (k == LLVMVectorTypeKind) {
      LLVMTypeRef te = LLVMGetElementType(t);
      LLVMTypeKind ke = LLVMGetTypeKind(te);
      unsigned len = LLVMGetVectorSize(t);
      if (ke == LLVMIntegerTypeKind) {
         unsigned b = LLVMGetIntTypeWidth(te);
         debug_printf("Vector [%u] of %u-bit Integer\n", len, b);
      }
      else {
         debug_printf("Vector [%u] of %s\n", len, lp_typekind_name(ke));
      }
   }
   else if (k == LLVMArrayTypeKind) {
      LLVMTypeRef te = LLVMGetElementType(t);
      LLVMTypeKind ke = LLVMGetTypeKind(te);
      unsigned len = LLVMGetArrayLength(t);
      debug_printf("Array [%u] of %s\n", len, lp_typekind_name(ke));
   }
   else if (k == LLVMIntegerTypeKind) {
      unsigned b = LLVMGetIntTypeWidth(t);
      debug_printf("%u-bit Integer\n", b);
   }
   else if (k == LLVMPointerTypeKind) {
      LLVMTypeRef te = LLVMGetElementType(t);
      debug_printf("Pointer to ");
      lp_dump_llvmtype(te);
   }
   else {
      debug_printf("%s\n", lp_typekind_name(k));
   }
}
Esempio n. 3
0
/* This function takes cl2llvm_type and an empty string.  The string is
   then filled with the type of the cl2llvm_type */
void cl2llvm_type_to_string(struct cl2llvmTypeWrap *type, char *type_string)
{
	int sign;
	int i, j;
	int vec_size;
	int ptr_count;
	int array_count;
	int array_size[50];
	char type_string_cpy[50];
	LLVMTypeRef bit_type;

	bit_type = cl2llvmTypeWrapGetLlvmType(type);
	sign = cl2llvmTypeWrapGetSign(type);
	array_count = 0;
	ptr_count = 0;
	vec_size = 0;

	/* Get array information */
	if (LLVMGetTypeKind(bit_type) == LLVMArrayTypeKind)
	{
		for (array_count = 0; LLVMGetTypeKind(bit_type)
			== LLVMArrayTypeKind; array_count++)
		{
			array_size[array_count] = LLVMGetArrayLength(bit_type);
			bit_type = LLVMGetElementType(bit_type);
		}
	}
	/* Get pointer information */
	if (LLVMGetTypeKind(bit_type) == LLVMPointerTypeKind)
	{
		for (ptr_count = 0; LLVMGetTypeKind(bit_type) 
			== LLVMPointerTypeKind; ptr_count++)
		{
			bit_type = LLVMGetElementType(bit_type);
		}
	}
	/* Get vector information */
	if (LLVMGetTypeKind(bit_type) == LLVMVectorTypeKind)
	{
		vec_size = LLVMGetVectorSize(bit_type);
		bit_type = LLVMGetElementType(bit_type);
	}
	if (LLVMGetTypeKind(bit_type) == LLVMDoubleTypeKind)
		strcpy(type_string, "double");	
	else if (LLVMGetTypeKind(bit_type) == LLVMFloatTypeKind)
		strcpy(type_string, "float");
	else if (LLVMGetTypeKind(bit_type) == LLVMHalfTypeKind)
		strcpy(type_string, "half");
	else if (LLVMGetTypeKind(bit_type) == LLVMIntegerTypeKind)
	{
		if (bit_type == LLVMInt64Type())
		{
			if (sign)
				strcpy(type_string, "long long");
			else
				strcpy(type_string, "unsigned long long");
		}
		else if (bit_type == LLVMInt32Type())
		{
			if (sign)
				strcpy(type_string, "int");
			else
				strcpy(type_string, "uint");
		}
		else if (bit_type == LLVMInt16Type())
		{
			if (sign)
				strcpy(type_string, "short");
			else 
				strcpy(type_string, "ushort");
		}
		else if (bit_type == LLVMInt8Type())
		{
			if (sign)
				strcpy(type_string, "char");
			else
				strcpy(type_string, "uchar");
		}
		else if (bit_type == LLVMInt1Type())
			strcpy(type_string, "bool");
	}	
	if (vec_size)
	{
		i = 0;
		while (type_string[i] != '\00')
			i++;
		switch (vec_size)
		{
		case 2:
			type_string[i] = '2';
			break;
		case 3:
			type_string[i] = '3';
			break;
		case 4:
			type_string[i] = '4';
			break;
		case 8:
			type_string[i] = '8';
			break;
		case 16:
			type_string[i] = '1';
			type_string[++i] = '6';
			break;
		}
		i++;
		type_string[i] = '\00';
	}
	if (ptr_count)
	{
		i = 0;
		while (type_string[i] != '\00')
			i++;
		type_string[i++] = ' ';
		for (j = 0; j < ptr_count; j++)
			type_string[i++] = '*';
		type_string[i] = '\00';
	}

	if (array_count == 1)
	{
		i = 0;
		while (type_string[i] != '\00')
			i++;
		type_string[i] = '*';
		i++;
		type_string[i] = '\00';
	}
	else if (array_count)
	{
		i = 0;
		while (type_string[i] != '\00')
			i++;
		type_string[i++] = '(';
		type_string[i++] = '*';
		type_string[i++] = ')';
		type_string[i] = '\00';

		for (j = 1; j < array_count; j++)
		{
			strcpy(type_string_cpy, type_string);
			snprintf(type_string, sizeof(char) * 40,
				"%s[%d]", type_string_cpy, array_size[j]);
		}
	}
}