llvm.org GIT mirror llvm / 1181c40
[LLVM-C] Improve Bindings For Aliases Summary: Add wrappers for a module's alias iterators and a getter and setter for the aliasee value. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: llvm-commits, harlanhaskins Differential Revision: https://reviews.llvm.org/D46808 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332826 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 1 year, 3 months ago
4 changed file(s) with 190 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
21172117 const char *Name);
21182118
21192119 /**
2120 * Obtain a GlobalAlias value from a Module by its name.
2121 *
2122 * The returned value corresponds to a llvm::GlobalAlias value.
2123 *
2124 * @see llvm::Module::getNamedAlias()
2125 */
2126 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2127 const char *Name, size_t NameLen);
2128
2129 /**
2130 * Obtain an iterator to the first GlobalAlias in a Module.
2131 *
2132 * @see llvm::Module::alias_begin()
2133 */
2134 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M);
2135
2136 /**
2137 * Obtain an iterator to the last GlobalAlias in a Module.
2138 *
2139 * @see llvm::Module::alias_end()
2140 */
2141 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M);
2142
2143 /**
2144 * Advance a GlobalAlias iterator to the next GlobalAlias.
2145 *
2146 * Returns NULL if the iterator was already at the end and there are no more
2147 * global aliases.
2148 */
2149 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA);
2150
2151 /**
2152 * Decrement a GlobalAlias iterator to the previous GlobalAlias.
2153 *
2154 * Returns NULL if the iterator was already at the beginning and there are
2155 * no previous global aliases.
2156 */
2157 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA);
2158
2159 /**
2160 * Retrieve the target value of an alias.
2161 */
2162 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias);
2163
2164 /**
2165 * Set the target value of an alias.
2166 */
2167 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee);
2168
2169 /**
21202170 * @}
21212171 */
21222172
19641964 unwrap(Aliasee), unwrap(M)));
19651965 }
19661966
1967 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
1968 const char *Name, size_t NameLen) {
1969 return wrap(unwrap(M)->getNamedAlias(Name));
1970 }
1971
1972 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
1973 Module *Mod = unwrap(M);
1974 Module::alias_iterator I = Mod->alias_begin();
1975 if (I == Mod->alias_end())
1976 return nullptr;
1977 return wrap(&*I);
1978 }
1979
1980 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
1981 Module *Mod = unwrap(M);
1982 Module::alias_iterator I = Mod->alias_end();
1983 if (I == Mod->alias_begin())
1984 return nullptr;
1985 return wrap(&*--I);
1986 }
1987
1988 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
1989 GlobalAlias *Alias = unwrap(GA);
1990 Module::alias_iterator I(Alias);
1991 if (++I == Alias->getParent()->alias_end())
1992 return nullptr;
1993 return wrap(&*I);
1994 }
1995
1996 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
1997 GlobalAlias *Alias = unwrap(GA);
1998 Module::alias_iterator I(Alias);
1999 if (I == Alias->getParent()->alias_begin())
2000 return nullptr;
2001 return wrap(&*--I);
2002 }
2003
2004 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2005 return wrap(unwrap(Alias)->getAliasee());
2006 }
2007
2008 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2009 unwrap(Alias)->setAliasee(unwrap(Aliasee));
2010 }
2011
19672012 /*--.. Operations on functions .............................................--*/
19682013
19692014 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2020 @protected = protected global i32 23
2121 @section = global i32 27, section ".custom"
2222 @align = global i32 31, align 4
23
24 @aliased1 = alias i32, i32* @var
25 @aliased2 = internal alias i32, i32* @var
26 @aliased3 = external alias i32, i32* @var
27 @aliased4 = weak alias i32, i32* @var
28 @aliased5 = weak_odr alias i32, i32* @var
2329
2430 define { i64, %S* } @unpackrepack(%S %s) {
2531 %1 = extractvalue %S %s, 0
247247 // Try global variable
248248 if (LLVMIsAGlobalVariable(Cst)) {
249249 check_value_kind(Cst, LLVMGlobalVariableValueKind);
250 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
250 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
251251 if (Dst)
252252 return Dst;
253 report_fatal_error("Could not find function");
253 report_fatal_error("Could not find variable");
254 }
255
256 // Try global alias
257 if (LLVMIsAGlobalAlias(Cst)) {
258 check_value_kind(Cst, LLVMGlobalAliasValueKind);
259 LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
260 if (Dst)
261 return Dst;
262 report_fatal_error("Could not find alias");
254263 }
255264
256265 fprintf(stderr, "Could not find @%s\n", Name);
821830 };
822831
823832 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
833 auto Ctx = LLVMGetModuleContext(M);
834
824835 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
825836 LLVMValueRef End = LLVMGetLastGlobal(Src);
826837
859870 if (!Begin) {
860871 if (End != nullptr)
861872 report_fatal_error("Range has an end but no beginning");
862 return;
863 }
864
865 auto Ctx = LLVMGetModuleContext(M);
873 goto AliasDecl;
874 }
866875
867876 Cur = Begin;
868877 Next = nullptr;
899908
900909 Cur = Next;
901910 }
911
912 AliasDecl:
913 Begin = LLVMGetFirstGlobalAlias(Src);
914 End = LLVMGetLastGlobalAlias(Src);
915 if (!Begin) {
916 if (End != nullptr)
917 report_fatal_error("Range has an end but no beginning");
918 return;
919 }
920
921 Cur = Begin;
922 Next = nullptr;
923 while (true) {
924 size_t NameLen;
925 const char *Name = LLVMGetValueName2(Cur, &NameLen);
926 if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
927 report_fatal_error("Global alias already cloned");
928 LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
929 // FIXME: Allow NULL aliasee.
930 LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
931
932 Next = LLVMGetNextGlobalAlias(Cur);
933 if (Next == nullptr) {
934 if (Cur != End)
935 report_fatal_error("");
936 break;
937 }
938
939 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
940 if (Prev != Cur)
941 report_fatal_error("Next.Previous global is not Current");
942
943 Cur = Next;
944 }
902945 }
903946
904947 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
952995 if (!Begin) {
953996 if (End != nullptr)
954997 report_fatal_error("Range has an end but no beginning");
955 return;
998 goto AliasClone;
956999 }
9571000
9581001 Cur = Begin;
9871030 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
9881031 if (Prev != Cur)
9891032 report_fatal_error("Next.Previous function is not Current");
1033
1034 Cur = Next;
1035 }
1036
1037 AliasClone:
1038 Begin = LLVMGetFirstGlobalAlias(Src);
1039 End = LLVMGetLastGlobalAlias(Src);
1040 if (!Begin) {
1041 if (End != nullptr)
1042 report_fatal_error("Range has an end but no beginning");
1043 return;
1044 }
1045
1046 Cur = Begin;
1047 Next = nullptr;
1048 while (true) {
1049 size_t NameLen;
1050 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1051 LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1052 if (!Alias)
1053 report_fatal_error("Global alias must have been declared already");
1054
1055 if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1056 LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1057 }
1058
1059 LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1060 LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1061
1062 Next = LLVMGetNextGlobalAlias(Cur);
1063 if (Next == nullptr) {
1064 if (Cur != End)
1065 report_fatal_error("Last global alias does not match End");
1066 break;
1067 }
1068
1069 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1070 if (Prev != Cur)
1071 report_fatal_error("Next.Previous global alias is not Current");
9901072
9911073 Cur = Next;
9921074 }