Example #1
0
LLVMValueRef ett_default_value(EagleComplexType *type)
{
    switch(type->type)
    {
        case ETInt1:
        case ETInt8:
        case ETUInt8:
        case ETInt16:
        case ETUInt16:
        case ETInt32:
        case ETUInt32:
        case ETInt64:
        case ETUInt64:
        case ETEnum:
            return LLVMConstInt(ett_llvm_type(type), 0, 0);
        case ETFloat:
        case ETDouble:
            return LLVMConstReal(ett_llvm_type(type), 0.0);
        case ETPointer:
            return LLVMConstPointerNull(ett_llvm_type(type));
        case ETStruct:
        {
            Arraylist *types;

            ty_struct_get_members(type, NULL, &types);
            LLVMValueRef vals[types->count];
            for(int i = 0; i < types->count; i++)
                vals[i] = ett_default_value(types->items[i]);

            return LLVMConstNamedStruct(ett_llvm_type(type), vals, types->count);
        }
        case ETArray:
        {
            EagleArrayType *at = (EagleArrayType *)type;
            LLVMValueRef val = ett_default_value(at->of);
            LLVMValueRef vals[at->ct];
            for(int i = 0; i < at->ct; i++)
                vals[i] = val;

            return LLVMConstArray(ett_llvm_type(at->of), vals, at->ct);
        }
        default:
            return NULL;
    }
}
Example #2
0
LLVMValueRef gen_vecdef(struct node *ast)
{
	LLVMValueRef global, array, init, *ival_list;
	struct node *n;
	int size, initsize, i;

	initsize = count_chain(ast->three);

	if (ast->two)
		size = LLVMConstIntGetZExtValue(codegen(ast->two));
	else
		size = 0;

	if (initsize > size)
		size = initsize;

	ival_list = calloc(sizeof(LLVMValueRef), size);

	if (size > 0 && ival_list == NULL)
		generror("out of memory");

	for (i = 0, n = ast->three; i < initsize; i++, n = n->two)
		/* TODO: handle NAMES (convert global pointer to int) */
		ival_list[initsize - i - 1] = codegen(n->one);

	for (i = initsize; i < size; i++)
		ival_list[i] = CONST(0);

	global = find_or_add_global(ast->one->val);
	array = LLVMAddGlobal(module, TYPE_ARRAY(size), ".gvec");
	LLVMSetLinkage(array, LLVMPrivateLinkage);

	if (initsize)
		init = LLVMConstArray(TYPE_INT, ival_list, size);
	else
		init = LLVMConstNull(TYPE_ARRAY(size));

	LLVMSetInitializer(array, init);
	LLVMSetInitializer(global, LLVMBuildPtrToInt(builder, array, TYPE_INT, ""));

	return NULL;
}
Example #3
0
static LLVMValueRef make_str(const char *str)
{
	LLVMValueRef global, strval, chars[MAX_STRSIZE];
	const char *p;
	int size = 0;

	/* Skip leading " */
	p = str + 1;
	while (p && size < MAX_STRSIZE - 1)
		chars[size++] = CONST(pack_char(&p));

	chars[size++] = CONST(EOT);

	global = LLVMAddGlobal(module, TYPE_ARRAY(size), ".gstr");
	LLVMSetLinkage(global, LLVMPrivateLinkage);

	strval = LLVMConstArray(TYPE_INT, chars, size);
	LLVMSetInitializer(global, strval);

	return lvalue_to_rvalue(global);
}
Example #4
0
static LLVMValueRef make_trait_list(compile_t* c, gentype_t* g)
{
  // The list is an array of integers.
  uint32_t count = trait_count(c, g);
  LLVMTypeRef type = LLVMArrayType(c->i32, count);

  // If we have no traits, return a null pointer to a list.
  if(count == 0)
    return LLVMConstNull(LLVMPointerType(type, 0));

  // Create a constant array of trait identifiers.
  size_t buf_size = count *sizeof(LLVMValueRef);
  LLVMValueRef* list = (LLVMValueRef*)pool_alloc_size(buf_size);

  reachable_type_t* t = reach_type(c->reachable, g->type_name);
  assert(t != NULL);

  size_t i = HASHMAP_BEGIN;
  size_t index = 0;
  reachable_type_t* provide;

  while((provide = reachable_type_cache_next(&t->subtypes, &i)) != NULL)
    list[index++] = make_type_id(c, provide->name);

  LLVMValueRef trait_array = LLVMConstArray(c->i32, list, count);

  // Create a global to hold the array.
  const char* name = genname_traitlist(g->type_name);
  LLVMValueRef global = LLVMAddGlobal(c->module, type, name);
  LLVMSetGlobalConstant(global, true);
  LLVMSetLinkage(global, LLVMInternalLinkage);
  LLVMSetInitializer(global, trait_array);

  pool_free_size(buf_size, list);
  return global;
}
Example #5
0
  while(count < len)
  {
    t = reach_types_next(&c->reach->types, &i);
    pony_assert(t != NULL);

    if(t->is_trait || (t->underlying == TK_STRUCT))
      continue;

    uint32_t type_id = t->type_id;
    if((type_id % 4) == 0)
    {
      size_t type_size = (size_t)LLVMABISizeOfType(c->target_data, t->use_type);
      args[type_id >> 2] = LLVMConstInt(c->i32, type_size, false);
      count++;
    }
  }

  LLVMTypeRef type = LLVMArrayType(c->i32, len);
  LLVMValueRef table = LLVMAddGlobal(c->module, type, "__NumSizeTable");
  LLVMValueRef value = LLVMConstArray(c->i32, args, len);
  LLVMSetInitializer(table, value);
  LLVMSetGlobalConstant(table, true);
  LLVMSetAlignment(table, 4);
  LLVMSetLinkage(table, LLVMPrivateLinkage);

  ponyint_pool_free_size(size, args);

  return table;
}