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; } }
/** * 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; } }
LLVMTypeRef ty_get_counted(LLVMTypeRef in) { char *translated = LLVMPrintTypeToString(in); LLVMTypeRef ref = hst_get(&counted_table, translated, NULL, NULL); if(!ref) { ref = LLVMStructCreateNamed(utl_get_current_context(), ""); LLVMTypeRef tys[6]; LLVMGetStructElementTypes(in, tys); LLVMStructSetBody(ref, tys, 6, 0); hst_put(&counted_table, translated, ref, NULL, NULL); } LLVMDisposeMessage(translated); return ref; }
int module_list_globals(void) { LLVMModuleRef M = load_module(false, false); LLVMValueRef g; g = LLVMGetFirstGlobal(M); while (g) { LLVMTypeRef T = LLVMTypeOf(g); char *s = LLVMPrintTypeToString(T); printf("Global%s: %s %s\n", LLVMIsDeclaration(g) ? "Declaration" : "Definition", LLVMGetValueName(g), s); LLVMDisposeMessage(s); g = LLVMGetNextGlobal(g); } LLVMDisposeModule(M); return 0; }