/** * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with * intrinsic names). */ void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize) { LLVMTypeRef elem_type = type; assert(bufsize >= 8); if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) { int ret = snprintf(buf, bufsize, "v%u", LLVMGetVectorSize(type)); if (ret < 0) { char *type_name = LLVMPrintTypeToString(type); fprintf(stderr, "Error building type name for: %s\n", type_name); return; } elem_type = LLVMGetElementType(type); buf += ret; bufsize -= ret; } switch (LLVMGetTypeKind(elem_type)) { default: break; case LLVMIntegerTypeKind: snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type)); break; case LLVMFloatTypeKind: snprintf(buf, bufsize, "f32"); break; case LLVMDoubleTypeKind: snprintf(buf, bufsize, "f64"); break; } }
LLVMValueRef codegen_addfun(compile_t* c, const char* name, LLVMTypeRef type) { // Add the function and set the calling convention. LLVMValueRef fun = LLVMAddFunction(c->module, name, type); LLVMSetFunctionCallConv(fun, c->callconv); LLVMValueRef arg = LLVMGetFirstParam(fun); uint32_t i = 1; while(arg != NULL) { LLVMTypeRef type = LLVMTypeOf(arg); if(LLVMGetTypeKind(type) == LLVMPointerTypeKind) { LLVMTypeRef elem = LLVMGetElementType(type); if(LLVMGetTypeKind(elem) == LLVMStructTypeKind) { size_t size = (size_t)LLVMABISizeOfType(c->target_data, elem); LLVMSetDereferenceable(fun, i, size); } } arg = LLVMGetNextParam(arg); i++; } return fun; }
LLVMValueRef gen_name(struct node *ast) { LLVMValueRef func, ptr, val; LLVMTypeRef type; ptr = lvalue(ast); type = LLVMTypeOf(ptr); if (LLVMGetTypeKind(type) == LLVMLabelTypeKind) { func = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)); return LLVMBuildPtrToInt(builder, LLVMBlockAddress(func, (LLVMBasicBlockRef)ptr), TYPE_INT, ""); } type = LLVMGetElementType(LLVMTypeOf(ptr)); switch (LLVMGetTypeKind(type)) { case LLVMIntegerTypeKind: val = LLVMBuildLoad(builder, ptr, ast->val); if (LLVMIsAGlobalValue(ptr)) val = LLVMBuildLShr(builder, val, CONST(WORDPOW), ""); return val; default: generror("unexpected type '%s'", LLVMPrintTypeToString(type)); return NULL; } }
static LLVMValueRef translateBinOp(SymbolTable *TyTable, SymbolTable *ValTable, ASTNode *Node) { ASTNode *NodeE1 = (ASTNode*) ptrVectorGet(&(Node->Child), 0), *NodeE2 = (ASTNode*) ptrVectorGet(&(Node->Child), 1); LLVMValueRef ValueE1Ptr = translateExpr(TyTable, ValTable, NodeE1), ValueE2Ptr = translateExpr(TyTable, ValTable, NodeE2); LLVMValueRef ValueE1 = LLVMBuildLoad(Builder, ValueE1Ptr, "binop.ld.e1."), ValueE2 = LLVMBuildLoad(Builder, ValueE2Ptr, "binop.ld.e2."); LLVMValueRef ResultVal = NULL; switch (LLVMGetTypeKind(LLVMTypeOf(ValueE1))) { case LLVMIntegerTypeKind: ResultVal = translateIntBinOp (Node->Kind, ValueE1, ValueE2); break; case LLVMFloatTypeKind: ResultVal = translateFloatBinOp (Node->Kind, ValueE1, ValueE2); break; case LLVMStructTypeKind: ResultVal = translateStructBinOp(Node->Kind, ValueE1Ptr, ValueE2Ptr); break; case LLVMPointerTypeKind: ResultVal = translateStringBinOp(Node->Kind, ValueE1, ValueE2); break; default: return NULL; } switch (LLVMGetTypeKind(LLVMTypeOf(ResultVal))) { case LLVMIntegerTypeKind: ResultVal = LLVMBuildZExt(Builder, ResultVal, LLVMInt32Type(), ""); break; default: break; } return wrapValue(ResultVal); }
/** * Print a LLVM value of any type */ LLVMValueRef lp_build_print_value(struct gallivm_state *gallivm, const char *msg, LLVMValueRef value) { LLVMBuilderRef builder = gallivm->builder; LLVMTypeKind type_kind; LLVMTypeRef type_ref; LLVMValueRef params[2 + LP_MAX_VECTOR_LENGTH]; char type_fmt[4] = " %x"; char format[2 + 3 * LP_MAX_VECTOR_LENGTH + 2] = "%s"; unsigned length; unsigned i; type_ref = LLVMTypeOf(value); type_kind = LLVMGetTypeKind(type_ref); if (type_kind == LLVMVectorTypeKind) { length = LLVMGetVectorSize(type_ref); type_ref = LLVMGetElementType(type_ref); type_kind = LLVMGetTypeKind(type_ref); } else { length = 1; } if (type_kind == LLVMFloatTypeKind || type_kind == LLVMDoubleTypeKind) { type_fmt[2] = 'f'; } else if (type_kind == LLVMIntegerTypeKind) { if (LLVMGetIntTypeWidth(type_ref) == 8) { type_fmt[2] = 'u'; } else { type_fmt[2] = 'i'; } } else { /* Unsupported type */ assert(0); } /* Create format string and arguments */ assert(strlen(format) + strlen(type_fmt) * length + 2 <= sizeof format); params[1] = lp_build_const_string(gallivm, msg); if (length == 1) { util_strncat(format, type_fmt, sizeof(format) - strlen(format) - 1); params[2] = value; } else { for (i = 0; i < length; ++i) { util_strncat(format, type_fmt, sizeof(format) - strlen(format) - 1); params[2 + i] = LLVMBuildExtractElement(builder, value, lp_build_const_int32(gallivm, i), ""); } } util_strncat(format, "\n", sizeof(format) - strlen(format) - 1); params[0] = lp_build_const_string(gallivm, format); return lp_build_print_args(gallivm, 2 + length, params); }
void lp_build_array_set(struct gallivm_state *gallivm, LLVMValueRef ptr, LLVMValueRef index, LLVMValueRef value) { LLVMValueRef element_ptr; assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); element_ptr = lp_build_array_get_ptr(gallivm, ptr, index); LLVMBuildStore(gallivm->builder, value, element_ptr); }
LLVMValueRef lp_build_struct_get(struct gallivm_state *gallivm, LLVMValueRef ptr, unsigned member, const char *name) { LLVMValueRef member_ptr; LLVMValueRef res; assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind); member_ptr = lp_build_struct_get_ptr(gallivm, ptr, member, name); res = LLVMBuildLoad(gallivm->builder, member_ptr, ""); lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name); return res; }
static int llvm_type_index(LLVMTypeRef type) { // LLVMDumpType(type); switch (LLVMGetTypeKind(type)) { case LLVMFloatTypeKind: return SCM_FOREIGN_TYPE_FLOAT; case LLVMDoubleTypeKind: return SCM_FOREIGN_TYPE_DOUBLE; case LLVMIntegerTypeKind: switch (LLVMGetIntTypeWidth(type)) { case 1: return SCM_FOREIGN_TYPE_BOOL; case 8: return SCM_FOREIGN_TYPE_INT8; case 16: return SCM_FOREIGN_TYPE_INT16; case 32: return SCM_FOREIGN_TYPE_INT32; case 64: return SCM_FOREIGN_TYPE_INT64; default: return SCM_FOREIGN_TYPE_VOID; }; default: return SCM_FOREIGN_TYPE_VOID; }; }
LLVMValueRef lp_build_struct_get_ptr(struct gallivm_state *gallivm, LLVMValueRef ptr, unsigned member, const char *name) { LLVMValueRef indices[2]; LLVMValueRef member_ptr; assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind); indices[0] = lp_build_const_int32(gallivm, 0); indices[1] = lp_build_const_int32(gallivm, member); member_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, Elements(indices), ""); lp_build_name(member_ptr, "%s.%s_ptr", LLVMGetValueName(ptr), name); return member_ptr; }
/** * Converts int16 half-float to float32 * Note this can be performed in 1 instruction if vcvtph2ps exists (f16c/cvt16) * [llvm.x86.vcvtph2ps / _mm_cvtph_ps] * * @param src value to convert * */ LLVMValueRef lp_build_half_to_float(struct gallivm_state *gallivm, LLVMValueRef src) { LLVMBuilderRef builder = gallivm->builder; LLVMTypeRef src_type = LLVMTypeOf(src); unsigned src_length = LLVMGetTypeKind(src_type) == LLVMVectorTypeKind ? LLVMGetVectorSize(src_type) : 1; struct lp_type f32_type = lp_type_float_vec(32, 32 * src_length); struct lp_type i32_type = lp_type_int_vec(32, 32 * src_length); LLVMTypeRef int_vec_type = lp_build_vec_type(gallivm, i32_type); LLVMValueRef h; if (util_cpu_caps.has_f16c && HAVE_LLVM >= 0x0301 && (src_length == 4 || src_length == 8)) { const char *intrinsic = NULL; if (src_length == 4) { src = lp_build_pad_vector(gallivm, src, 8); intrinsic = "llvm.x86.vcvtph2ps.128"; } else { intrinsic = "llvm.x86.vcvtph2ps.256"; } return lp_build_intrinsic_unary(builder, intrinsic, lp_build_vec_type(gallivm, f32_type), src); } /* Convert int16 vector to int32 vector by zero ext (might generate bad code) */ h = LLVMBuildZExt(builder, src, int_vec_type, ""); return lp_build_smallfloat_to_float(gallivm, f32_type, h, 10, 5, 0, true); }
/** * 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; } }
int test_kal_codegen_function() { kal_named_value *val; unsigned int arg_count = 1; char **args = malloc(sizeof(char*) * arg_count); args[0] = "foo"; LLVMModuleRef module = LLVMModuleCreateWithName("kal"); LLVMBuilderRef builder = LLVMCreateBuilder(); kal_ast_node *prototype = kal_ast_prototype_create("my_func", args, arg_count); kal_ast_node *lhs = kal_ast_variable_create("foo"); kal_ast_node *rhs = kal_ast_number_create(20); kal_ast_node *body = kal_ast_binary_expr_create(KAL_BINOP_PLUS, lhs, rhs); kal_ast_node *node = kal_ast_function_create(prototype, body); kal_codegen_reset(); LLVMValueRef value = kal_codegen(node, module, builder); mu_assert(value != NULL, ""); mu_assert(LLVMGetNamedFunction(module, "my_func") == value, ""); mu_assert(LLVMCountParams(value) == 1, ""); val = kal_codegen_named_value("foo"); mu_assert(val->value == LLVMGetParam(value, 0), ""); mu_assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(value, 0))) == LLVMDoubleTypeKind, ""); LLVMDisposeBuilder(builder); LLVMDisposeModule(module); kal_ast_node_free(node); return 0; }
static LLVMValueRef raw_is_box(compile_t* c, ast_t* left_type, LLVMValueRef l_value, LLVMValueRef r_value) { pony_assert(LLVMGetTypeKind(LLVMTypeOf(r_value)) == LLVMPointerTypeKind); LLVMValueRef r_desc = gendesc_fetch(c, r_value); LLVMValueRef same_type = gendesc_isentity(c, r_desc, left_type); pony_assert(same_type != GEN_NOVALUE); LLVMBasicBlockRef this_block = LLVMGetInsertBlock(c->builder); LLVMBasicBlockRef value_block = codegen_block(c, "is_value"); LLVMBasicBlockRef post_block = codegen_block(c, "is_post"); LLVMBuildCondBr(c->builder, same_type, value_block, post_block); LLVMPositionBuilderAtEnd(c->builder, value_block); r_value = gen_unbox(c, left_type, r_value); LLVMValueRef is_value = gen_is_value(c, left_type, left_type, l_value, r_value); LLVMBuildBr(c->builder, post_block); value_block = LLVMGetInsertBlock(c->builder); LLVMPositionBuilderAtEnd(c->builder, post_block); LLVMValueRef phi = LLVMBuildPhi(c->builder, c->i1, ""); LLVMValueRef zero = LLVMConstInt(c->i1, 0, false); LLVMAddIncoming(phi, &is_value, &value_block, 1); LLVMAddIncoming(phi, &zero, &this_block, 1); return phi; }
void expand_vectors(struct list_t *elem_list) { int index; int vec_index; struct cl2llvm_val_t *cl2llvm_index; struct cl2llvm_val_t *current_vec_elem; struct cl2llvm_val_t *current_elem; LIST_FOR_EACH(elem_list, index) { current_elem = list_get(elem_list, index); if (LLVMGetTypeKind(cl2llvmTypeWrapGetLlvmType(current_elem->type)) == LLVMVectorTypeKind) { for(vec_index = 0; vec_index < LLVMGetVectorSize(cl2llvmTypeWrapGetLlvmType(current_elem->type)); vec_index++) { cl2llvm_index = cl2llvm_val_create_w_init( LLVMConstInt( LLVMInt32Type(), vec_index, 0), 1); snprintf(temp_var_name, sizeof(temp_var_name), "tmp_%d", temp_var_count++); current_vec_elem = cl2llvm_val_create_w_init( LLVMBuildExtractElement(cl2llvm_builder, current_elem->val, cl2llvm_index->val, temp_var_name), cl2llvmTypeWrapGetSign(current_elem->type)); list_insert(elem_list, index + vec_index, current_vec_elem); cl2llvm_val_free(cl2llvm_index); } cl2llvm_val_free(current_elem); list_remove(elem_list, current_elem); } }
static void emit_store( struct lp_build_tgsi_context * bld_base, const struct tgsi_full_instruction * inst, const struct tgsi_opcode_info * info, LLVMValueRef dst[4]) { struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base); struct gallivm_state *gallivm = bld->bld_base.base.gallivm; struct lp_build_context base = bld->bld_base.base; const struct tgsi_full_dst_register *reg = &inst->Dst[0]; LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; LLVMValueRef temp_ptr; unsigned chan, chan_index; boolean is_vec_store = FALSE; if (dst[0]) { LLVMTypeKind k = LLVMGetTypeKind(LLVMTypeOf(dst[0])); is_vec_store = (k == LLVMVectorTypeKind); } if (is_vec_store) { LLVMValueRef values[4] = {}; TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst, chan) { LLVMValueRef index = lp_build_const_int32(gallivm, chan); values[chan] = LLVMBuildExtractElement(gallivm->builder, dst[0], index, ""); } bld_base->emit_store(bld_base, inst, info, values); return; }
LLVMValueRef lp_build_array_get(struct gallivm_state *gallivm, LLVMValueRef ptr, LLVMValueRef index) { LLVMValueRef element_ptr; LLVMValueRef res; assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); element_ptr = lp_build_array_get_ptr(gallivm, ptr, index); res = LLVMBuildLoad(gallivm->builder, element_ptr, ""); #ifdef DEBUG lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index)); #endif return res; }
/** * Generates LLVM IR to call debug_printf. */ static LLVMValueRef lp_build_print_args(struct gallivm_state* gallivm, int argcount, LLVMValueRef* args) { LLVMBuilderRef builder = gallivm->builder; LLVMContextRef context = gallivm->context; LLVMValueRef func_printf; LLVMTypeRef printf_type; int i; assert(args); assert(argcount > 0); assert(LLVMTypeOf(args[0]) == LLVMPointerType(LLVMInt8TypeInContext(context), 0)); /* Cast any float arguments to doubles as printf expects */ for (i = 1; i < argcount; i++) { LLVMTypeRef type = LLVMTypeOf(args[i]); if (LLVMGetTypeKind(type) == LLVMFloatTypeKind) args[i] = LLVMBuildFPExt(builder, args[i], LLVMDoubleTypeInContext(context), ""); } printf_type = LLVMFunctionType(LLVMInt32TypeInContext(context), NULL, 0, 1); func_printf = lp_build_const_int_pointer(gallivm, func_to_pointer((func_pointer)debug_printf)); func_printf = LLVMBuildBitCast(builder, func_printf, LLVMPointerType(printf_type, 0), "debug_printf"); return LLVMBuildCall(builder, func_printf, args, argcount, ""); }
LLVMValueRef lp_build_broadcast(struct gallivm_state *gallivm, LLVMTypeRef vec_type, LLVMValueRef scalar) { LLVMValueRef res; if (LLVMGetTypeKind(vec_type) != LLVMVectorTypeKind) { /* scalar */ assert(vec_type == LLVMTypeOf(scalar)); res = scalar; } else { LLVMBuilderRef builder = gallivm->builder; const unsigned length = LLVMGetVectorSize(vec_type); LLVMValueRef undef = LLVMGetUndef(vec_type); /* The shuffle vector is always made of int32 elements */ LLVMTypeRef i32_type = LLVMInt32TypeInContext(gallivm->context); LLVMTypeRef i32_vec_type = LLVMVectorType(i32_type, length); assert(LLVMGetElementType(vec_type) == LLVMTypeOf(scalar)); res = LLVMBuildInsertElement(builder, undef, scalar, LLVMConstNull(i32_type), ""); res = LLVMBuildShuffleVector(builder, res, undef, LLVMConstNull(i32_vec_type), ""); } return res; }
static LLVMValueRef translateRecordExpr(SymbolTable *TyTable, SymbolTable *ValTable, ASTNode *Node) { PtrVector *V = &(Node->Child); ASTNode *FieldNode = (ASTNode*) ptrVectorGet(V, 0); Type *ThisType = createType(IdTy, Node->Value); LLVMTypeRef RecordType = getLLVMTypeFromType(TyTable, ThisType); LLVMValueRef RecordVal = LLVMBuildMalloc(Builder, RecordType, ""); unsigned FieldNumber = LLVMCountStructElementTypes(RecordType), I; for (I = 0; I < FieldNumber; ++I) { LLVMValueRef ElemIdx[] = { getSConstInt(0), getSConstInt(I) }; LLVMValueRef ElemI = LLVMBuildInBoundsGEP(Builder, RecordVal, ElemIdx, 2, ""); LLVMValueRef FieldPtr = translateExpr(TyTable, ValTable, ptrVectorGet(&(FieldNode->Child), I)); LLVMValueRef ValueFrom = NULL; switch (LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(FieldPtr)))) { case LLVMIntegerTypeKind: case LLVMFloatTypeKind: ValueFrom = LLVMBuildLoad(Builder, FieldPtr, ""); break; case LLVMPointerTypeKind: ValueFrom = toDynamicMemory(FieldPtr); break; default: ValueFrom = FieldPtr; } LLVMBuildStore(Builder, ValueFrom, ElemI); } return RecordVal; }
LLVMValueRef gen_return(compile_t* c, ast_t* ast) { ast_t* expr = ast_child(ast); LLVMValueRef value = gen_expr(c, expr); size_t clause; ast_t* try_expr = ast_try_clause(ast, &clause); // Do the then block only if we return in the body or else clause. // In the then block, return without doing the then block. if((try_expr != NULL) && (clause != 2)) gen_expr(c, ast_childidx(try_expr, 2)); LLVMTypeRef f_type = LLVMGetElementType(LLVMTypeOf(codegen_fun(c))); LLVMTypeRef r_type = LLVMGetReturnType(f_type); codegen_debugloc(c, ast); if(LLVMGetTypeKind(r_type) != LLVMVoidTypeKind) { LLVMValueRef ret = gen_assign_cast(c, r_type, value, ast_type(expr)); codegen_scope_lifetime_end(c); LLVMBuildRet(c->builder, ret); } else { codegen_scope_lifetime_end(c); LLVMBuildRetVoid(c->builder); } codegen_debugloc(c, NULL); return GEN_NOVALUE; }
int test_kal_codegen_number() { kal_ast_node *node = kal_ast_number_create(10); LLVMValueRef value = kal_codegen(node, NULL, NULL); LLVMTypeRef type = LLVMTypeOf(value); mu_assert(LLVMGetTypeKind(type) == LLVMDoubleTypeKind, ""); mu_assert(LLVMIsConstant(value), ""); kal_ast_node_free(node); return 0; }
LLVMValueRef lp_build_array_get_ptr(struct gallivm_state *gallivm, LLVMValueRef ptr, LLVMValueRef index) { LLVMValueRef indices[2]; LLVMValueRef element_ptr; assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind); assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind); indices[0] = lp_build_const_int32(gallivm, 0); indices[1] = index; element_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, Elements(indices), ""); #ifdef DEBUG lp_build_name(element_ptr, "&%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index)); #endif return element_ptr; }
LLVMValueRef codegen_addfun(compile_t* c, const char* name, LLVMTypeRef type, bool pony_abi) { // Add the function and set the calling convention and the linkage type. LLVMValueRef fun = LLVMAddFunction(c->module, name, type); LLVMSetFunctionCallConv(fun, c->callconv); LLVMSetLinkage(fun, c->linkage); LLVMSetUnnamedAddr(fun, true); if(pony_abi) { LLVMValueRef md = LLVMMDNodeInContext(c->context, NULL, 0); LLVMSetMetadataStr(fun, "pony.abi", md); } LLVMValueRef arg = LLVMGetFirstParam(fun); uint32_t i = 1; while(arg != NULL) { LLVMTypeRef type = LLVMTypeOf(arg); if(LLVMGetTypeKind(type) == LLVMPointerTypeKind) { LLVMTypeRef elem = LLVMGetElementType(type); if(LLVMGetTypeKind(elem) == LLVMStructTypeKind) { size_t size = (size_t)LLVMABISizeOfType(c->target_data, elem); #if PONY_LLVM >= 309 LLVM_DECLARE_ATTRIBUTEREF(deref_attr, dereferenceable, size); LLVMAddAttributeAtIndex(fun, i, deref_attr); #else LLVMSetDereferenceable(fun, i, size); #endif } } arg = LLVMGetNextParam(arg); i++; } return fun; }
LLVMValueRef gen_assign_cast(compile_t* c, LLVMTypeRef l_type, LLVMValueRef r_value, ast_t* type) { if(r_value <= GEN_NOVALUE) return r_value; LLVMTypeRef r_type = LLVMTypeOf(r_value); if(r_type == l_type) return r_value; switch(LLVMGetTypeKind(l_type)) { case LLVMIntegerTypeKind: case LLVMHalfTypeKind: case LLVMFloatTypeKind: case LLVMDoubleTypeKind: assert(LLVMGetTypeKind(r_type) == LLVMPointerTypeKind); return gen_unbox(c, type, r_value); case LLVMPointerTypeKind: r_value = gen_box(c, type, r_value); if(r_value == NULL) return NULL; return LLVMBuildBitCast(c->builder, r_value, l_type, ""); case LLVMStructTypeKind: if(LLVMGetTypeKind(r_type) == LLVMPointerTypeKind) { r_value = gen_unbox(c, type, r_value); assert(LLVMGetTypeKind(LLVMTypeOf(r_value)) == LLVMStructTypeKind); } return assign_to_tuple(c, l_type, r_value, type); default: {} } assert(0); return NULL; }
static LLVMValueRef translateFunCallExpr(SymbolTable *TyTable, SymbolTable *ValTable, ASTNode *Node) { PtrVector *V = &(Node->Child); ASTNode *ExprNode = (ASTNode*) ptrVectorGet(V, 0), *ParamsNode = (ASTNode*) ptrVectorGet(V, 1); LLVMTypeRef ReturnType = toTransitionType(getLLVMTypeFromType(TyTable, Node->Value)), *ParamsType = NULL, FunctionType; LLVMValueRef *ParamsValue = NULL; unsigned Count = 0; if (ParamsNode) { ParamsValue = (LLVMValueRef*) malloc(sizeof(LLVMValueRef) * ParamsNode->Child.Size); for (Count = 0; Count < ParamsNode->Child.Size; ++Count) { LLVMValueRef ExprVal = translateExpr(TyTable, ValTable, ptrVectorGet(&(ParamsNode->Child), Count)); LLVMTypeRef ExprType = LLVMGetElementType(LLVMTypeOf(ExprVal)); switch (LLVMGetTypeKind(ExprType)) { case LLVMIntegerTypeKind: case LLVMFloatTypeKind: case LLVMPointerTypeKind: ExprVal = LLVMBuildLoad(Builder, ExprVal, "load.4.call"); break; default: break; } ParamsValue[Count] = ExprVal; if (!ParamsType) ParamsType = (LLVMTypeRef*) malloc(sizeof(LLVMTypeRef) * ParamsNode->Child.Size); ParamsType[Count] = LLVMTypeOf(ExprVal); } } FunctionType = LLVMFunctionType(ReturnType, ParamsType, Count, 0); FunctionType = LLVMPointerType(FunctionType, 0); LLVMValueRef Closure = translateExpr(TyTable, ValTable, ExprNode); LLVMValueRef CallValue = callClosure(FunctionType, Closure, ParamsValue, Count); switch (getLLVMValueTypeKind(CallValue)) { case LLVMIntegerTypeKind: case LLVMFloatTypeKind: case LLVMPointerTypeKind: { if (getLLVMValueTypeKind(CallValue) == LLVMPointerTypeKind && getLLVMElementTypeKind(CallValue) == LLVMStructTypeKind) break; LLVMValueRef PtrMem = LLVMBuildAlloca(Builder, LLVMTypeOf(CallValue), ""); LLVMBuildStore(Builder, CallValue, PtrMem); return PtrMem; } } return CallValue; }
static LLVMValueRef gen_digestof_box(compile_t* c, reach_type_t* type, LLVMValueRef value, int boxed_subtype) { pony_assert(LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMPointerTypeKind); LLVMBasicBlockRef box_block = NULL; LLVMBasicBlockRef nonbox_block = NULL; LLVMBasicBlockRef post_block = NULL; LLVMValueRef desc = gendesc_fetch(c, value); if((boxed_subtype & SUBTYPE_KIND_UNBOXED) != 0) { box_block = codegen_block(c, "digestof_box"); nonbox_block = codegen_block(c, "digestof_nonbox"); post_block = codegen_block(c, "digestof_post"); // Check if it's a boxed value. LLVMValueRef type_id = gendesc_typeid(c, desc); LLVMValueRef boxed_mask = LLVMConstInt(c->i32, 1, false); LLVMValueRef is_boxed = LLVMBuildAnd(c->builder, type_id, boxed_mask, ""); LLVMValueRef zero = LLVMConstInt(c->i32, 0, false); is_boxed = LLVMBuildICmp(c->builder, LLVMIntEQ, is_boxed, zero, ""); LLVMBuildCondBr(c->builder, is_boxed, box_block, nonbox_block); LLVMPositionBuilderAtEnd(c->builder, box_block); } // Call the type-specific __digestof function, which will unbox the value. reach_method_t* digest_fn = reach_method(type, TK_BOX, stringtab("__digestof"), NULL); pony_assert(digest_fn != NULL); LLVMValueRef func = gendesc_vtable(c, desc, digest_fn->vtable_index); LLVMTypeRef fn_type = LLVMFunctionType(c->intptr, &c->object_ptr, 1, false); func = LLVMBuildBitCast(c->builder, func, LLVMPointerType(fn_type, 0), ""); LLVMValueRef box_digest = codegen_call(c, func, &value, 1, true); if((boxed_subtype & SUBTYPE_KIND_UNBOXED) != 0) { LLVMBuildBr(c->builder, post_block); // Just cast the address. LLVMPositionBuilderAtEnd(c->builder, nonbox_block); LLVMValueRef nonbox_digest = LLVMBuildPtrToInt(c->builder, value, c->intptr, ""); LLVMBuildBr(c->builder, post_block); LLVMPositionBuilderAtEnd(c->builder, post_block); LLVMValueRef phi = LLVMBuildPhi(c->builder, c->intptr, ""); LLVMAddIncoming(phi, &box_digest, &box_block, 1); LLVMAddIncoming(phi, &nonbox_digest, &nonbox_block, 1); return phi; } else { return box_digest; } }
static LLVMValueRef gen_digestof_value(compile_t* c, LLVMValueRef value) { LLVMTypeRef type = LLVMTypeOf(value); switch(LLVMGetTypeKind(type)) { case LLVMFloatTypeKind: value = LLVMBuildBitCast(c->builder, value, c->i32, ""); return LLVMBuildZExt(c->builder, value, c->i64, ""); case LLVMDoubleTypeKind: return LLVMBuildBitCast(c->builder, value, c->i64, ""); case LLVMIntegerTypeKind: { uint32_t width = LLVMGetIntTypeWidth(type); if(width < 64) { value = LLVMBuildZExt(c->builder, value, c->i64, ""); } else if(width == 128) { LLVMValueRef shift = LLVMConstInt(c->i128, 64, false); LLVMValueRef high = LLVMBuildLShr(c->builder, value, shift, ""); high = LLVMBuildTrunc(c->builder, high, c->i64, ""); value = LLVMBuildTrunc(c->builder, value, c->i64, ""); value = LLVMBuildXor(c->builder, value, high, ""); } return value; } case LLVMStructTypeKind: { uint32_t count = LLVMCountStructElementTypes(type); LLVMValueRef result = LLVMConstInt(c->i64, 0, false); for(uint32_t i = 0; i < count; i++) { LLVMValueRef elem = LLVMBuildExtractValue(c->builder, value, i, ""); elem = gen_digestof_value(c, elem); result = LLVMBuildXor(c->builder, result, elem, ""); } return result; } case LLVMPointerTypeKind: return LLVMBuildPtrToInt(c->builder, value, c->i64, ""); default: {} } assert(0); return NULL; }
static LLVMValueRef cast_ffi_arg(compile_t* c, ffi_decl_t* decl, ast_t* ast, LLVMValueRef arg, LLVMTypeRef param, const char* name) { if(arg == NULL) return NULL; LLVMTypeRef arg_type = LLVMTypeOf(arg); if(param == arg_type) return arg; if((LLVMABISizeOfType(c->target_data, param) != LLVMABISizeOfType(c->target_data, arg_type))) { report_ffi_type_err(c, decl, ast, name); return NULL; } switch(LLVMGetTypeKind(param)) { case LLVMPointerTypeKind: if(LLVMGetTypeKind(arg_type) == LLVMIntegerTypeKind) return LLVMBuildIntToPtr(c->builder, arg, param, ""); else return LLVMBuildBitCast(c->builder, arg, param, ""); case LLVMIntegerTypeKind: if(LLVMGetTypeKind(arg_type) == LLVMPointerTypeKind) return LLVMBuildPtrToInt(c->builder, arg, param, ""); break; case LLVMStructTypeKind: pony_assert(LLVMGetTypeKind(arg_type) == LLVMStructTypeKind); return arg; default: {} } pony_assert(false); return NULL; }
void lp_format_intrinsic(char *name, size_t size, const char *name_root, LLVMTypeRef type) { unsigned length = 0; unsigned width; char c; LLVMTypeKind kind = LLVMGetTypeKind(type); if (kind == LLVMVectorTypeKind) { length = LLVMGetVectorSize(type); type = LLVMGetElementType(type); kind = LLVMGetTypeKind(type); } switch (kind) { case LLVMIntegerTypeKind: c = 'i'; width = LLVMGetIntTypeWidth(type); break; case LLVMFloatTypeKind: c = 'f'; width = 32; break; case LLVMDoubleTypeKind: c = 'f'; width = 64; break; default: unreachable("unexpected LLVMTypeKind"); } if (length) { util_snprintf(name, size, "%s.v%u%c%u", name_root, length, c, width); } else { util_snprintf(name, size, "%s.%c%u", name_root, c, width); } }
static LLVMValueRef bitcast_to_float(struct ac_llvm_context *ctx, LLVMValueRef value) { LLVMTypeRef type = LLVMTypeOf(value); LLVMTypeRef new_type; if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) new_type = LLVMVectorType(ctx->f32, LLVMGetVectorSize(type)); else new_type = ctx->f32; return LLVMBuildBitCast(ctx->builder, value, new_type, ""); }