llvm.org GIT mirror llvm / b5e7dcc
[LLVM-C] Add Bindings For Module Flags Summary: The first foray into merging debug info into the echo tests. - Add bindings to Module::getModuleFlagsMetadata() in the form of LLVMCopyModuleFlagsMetadata - Add the opaque type LLVMModuleFlagEntry to represent Module::ModuleFlagEntry - Add accessors for LLVMModuleFlagEntry's behavior, key, and metadata node. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: aprantl, JDevlieghere, llvm-commits, harlanhaskins Differential Revision: https://reviews.llvm.org/D46792 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332219 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 1 year, 4 months ago
5 changed file(s) with 235 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
380380 LLVMInlineAsmDialectIntel
381381 } LLVMInlineAsmDialect;
382382
383 typedef enum {
384 /**
385 * Emits an error if two values disagree, otherwise the resulting value is
386 * that of the operands.
387 *
388 * @see Module::ModFlagBehavior::Error
389 */
390 LLVMModuleFlagBehaviorError,
391 /**
392 * Emits a warning if two values disagree. The result value will be the
393 * operand for the flag from the first module being linked.
394 *
395 * @see Module::ModFlagBehavior::Warning
396 */
397 LLVMModuleFlagBehaviorWarning,
398 /**
399 * Adds a requirement that another module flag be present and have a
400 * specified value after linking is performed. The value must be a metadata
401 * pair, where the first element of the pair is the ID of the module flag
402 * to be restricted, and the second element of the pair is the value the
403 * module flag should be restricted to. This behavior can be used to
404 * restrict the allowable results (via triggering of an error) of linking
405 * IDs with the **Override** behavior.
406 *
407 * @see Module::ModFlagBehavior::Require
408 */
409 LLVMModuleFlagBehaviorRequire,
410 /**
411 * Uses the specified value, regardless of the behavior or value of the
412 * other module. If both modules specify **Override**, but the values
413 * differ, an error will be emitted.
414 *
415 * @see Module::ModFlagBehavior::Override
416 */
417 LLVMModuleFlagBehaviorOverride,
418 /**
419 * Appends the two values, which are required to be metadata nodes.
420 *
421 * @see Module::ModFlagBehavior::Append
422 */
423 LLVMModuleFlagBehaviorAppend,
424 /**
425 * Appends the two values, which are required to be metadata
426 * nodes. However, duplicate entries in the second list are dropped
427 * during the append operation.
428 *
429 * @see Module::ModFlagBehavior::AppendUnique
430 */
431 LLVMModuleFlagBehaviorAppendUnique,
432 } LLVMModuleFlagBehavior;
433
383434 /**
384435 * Attribute index are either LLVMAttributeReturnIndex,
385436 * LLVMAttributeFunctionIndex or a parameter number from 1 to N.
663714 * @see Module::setTargetTriple()
664715 */
665716 void LLVMSetTarget(LLVMModuleRef M, const char *Triple);
717
718 /**
719 * Returns the module flags as an array of flag-key-value triples. The caller
720 * is responsible for freeing this array by calling
721 * \c LLVMDisposeModuleFlagsMetadata.
722 *
723 * @see Module::getModuleFlagsMetadata()
724 */
725 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len);
726
727 /**
728 * Destroys module flags metadata entries.
729 */
730 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries);
731
732 /**
733 * Returns the flag behavior for a module flag entry at a specific index.
734 *
735 * @see Module::ModuleFlagEntry::Behavior
736 */
737 LLVMModuleFlagBehavior
738 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
739 unsigned Index);
740
741 /**
742 * Returns the key for a module flag entry at a specific index.
743 *
744 * @see Module::ModuleFlagEntry::Key
745 */
746 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
747 unsigned Index, size_t *Len);
748
749 /**
750 * Returns the metadata for a module flag entry at a specific index.
751 *
752 * @see Module::ModuleFlagEntry::Val
753 */
754 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
755 unsigned Index);
756
757 /**
758 * Add a module-level flag to the module-level flags metadata if it doesn't
759 * already exist.
760 *
761 * @see Module::getModuleFlag()
762 */
763 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
764 const char *Key, size_t KeyLen);
765
766 /**
767 * Add a module-level flag to the module-level flags metadata if it doesn't
768 * already exist.
769 *
770 * @see Module::addModuleFlag()
771 */
772 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
773 const char *Key, size_t KeyLen,
774 LLVMMetadataRef Val);
666775
667776 /**
668777 * Dump a representation of a module to stderr.
139139 typedef struct LLVMComdat *LLVMComdatRef;
140140
141141 /**
142 * @see llvm::Module::ModuleFlagEntry
143 */
144 typedef struct LLVMOpaqueModuleFlagEntry LLVMModuleFlagEntry;
145
146 /**
142147 * @}
143148 */
144149
264264 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
265265 unwrap(M)->setTargetTriple(Triple);
266266 }
267
268 /*--.. Module flags ........................................................--*/
269 struct LLVMOpaqueModuleFlagEntry {
270 LLVMModuleFlagBehavior Behavior;
271 const char *Key;
272 size_t KeyLen;
273 LLVMMetadataRef Metadata;
274 };
275
276 static Module::ModFlagBehavior
277 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
278 switch (Behavior) {
279 case LLVMModuleFlagBehaviorError:
280 return Module::ModFlagBehavior::Error;
281 case LLVMModuleFlagBehaviorWarning:
282 return Module::ModFlagBehavior::Warning;
283 case LLVMModuleFlagBehaviorRequire:
284 return Module::ModFlagBehavior::Require;
285 case LLVMModuleFlagBehaviorOverride:
286 return Module::ModFlagBehavior::Override;
287 case LLVMModuleFlagBehaviorAppend:
288 return Module::ModFlagBehavior::Append;
289 case LLVMModuleFlagBehaviorAppendUnique:
290 return Module::ModFlagBehavior::AppendUnique;
291 }
292 }
293
294 static LLVMModuleFlagBehavior
295 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
296 switch (Behavior) {
297 case Module::ModFlagBehavior::Error:
298 return LLVMModuleFlagBehaviorError;
299 case Module::ModFlagBehavior::Warning:
300 return LLVMModuleFlagBehaviorWarning;
301 case Module::ModFlagBehavior::Require:
302 return LLVMModuleFlagBehaviorRequire;
303 case Module::ModFlagBehavior::Override:
304 return LLVMModuleFlagBehaviorOverride;
305 case Module::ModFlagBehavior::Append:
306 return LLVMModuleFlagBehaviorAppend;
307 case Module::ModFlagBehavior::AppendUnique:
308 return LLVMModuleFlagBehaviorAppendUnique;
309 default:
310 llvm_unreachable("Unhandled Flag Behavior");
311 }
312 }
313
314 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
315 SmallVector MFEs;
316 unwrap(M)->getModuleFlagsMetadata(MFEs);
317
318 LLVMOpaqueModuleFlagEntry *Result = static_cast(
319 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
320 for (unsigned i = 0; i < MFEs.size(); ++i) {
321 const auto &ModuleFlag = MFEs[i];
322 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
323 Result[i].Key = ModuleFlag.Key->getString().data();
324 Result[i].KeyLen = ModuleFlag.Key->getString().size();
325 Result[i].Metadata = wrap(ModuleFlag.Val);
326 }
327 *Len = MFEs.size();
328 return Result;
329 }
330
331 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
332 free(Entries);
333 }
334
335 LLVMModuleFlagBehavior
336 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
337 unsigned Index) {
338 LLVMOpaqueModuleFlagEntry MFE =
339 static_cast(Entries[Index]);
340 return MFE.Behavior;
341 }
342
343 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
344 unsigned Index, size_t *Len) {
345 LLVMOpaqueModuleFlagEntry MFE =
346 static_cast(Entries[Index]);
347 *Len = MFE.KeyLen;
348 return MFE.Key;
349 }
350
351 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
352 unsigned Index) {
353 LLVMOpaqueModuleFlagEntry MFE =
354 static_cast(Entries[Index]);
355 return MFE.Metadata;
356 }
357
358 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
359 const char *Key, size_t KeyLen) {
360 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
361 }
362
363 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
364 const char *Key, size_t KeyLen,
365 LLVMMetadataRef Val) {
366 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
367 {Key, KeyLen}, unwrap(Val));
368 }
369
370 /*--.. Printing modules ....................................................--*/
267371
268372 void LLVMDumpModule(LLVMModuleRef M) {
269373 unwrap(M)->print(errs(), nullptr,
158158 exit:
159159 ret void
160160 }
161
162 !llvm.module.flags = !{!1}
163
164 !1 = !{i32 2, !"Debug Info Version", i32 3}
996996 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
997997 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
998998
999 size_t SourceFlagsLen;
1000 LLVMModuleFlagEntry *ModuleFlags =
1001 LLVMCopyModuleFlagsMetadata(Src, &SourceFlagsLen);
1002 for (unsigned i = 0; i < SourceFlagsLen; ++i) {
1003 size_t EntryNameLen;
1004 const char *EntryName =
1005 LLVMModuleFlagEntriesGetKey(ModuleFlags, i, &EntryNameLen);
1006 LLVMAddModuleFlag(M, LLVMModuleFlagEntriesGetFlagBehavior(ModuleFlags, i),
1007 EntryName, EntryNameLen,
1008 LLVMModuleFlagEntriesGetMetadata(ModuleFlags, i));
1009 }
1010
9991011 LLVMSetTarget(M, LLVMGetTarget(Src));
10001012 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
10011013 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
10101022 char *Str = LLVMPrintModuleToString(M);
10111023 fputs(Str, stdout);
10121024
1025 LLVMDisposeModuleFlagsMetadata(ModuleFlags);
10131026 LLVMDisposeMessage(Str);
10141027 LLVMDisposeModule(Src);
10151028 LLVMDisposeModule(M);