10#include "TargetInfo.h"
79class TypeStringCache {
80 enum Status {NonRecursive, Recursive,
Incomplete, IncompleteUsed};
87 std::map<const IdentifierInfo *, struct Entry> Map;
88 unsigned IncompleteCount;
89 unsigned IncompleteUsedCount;
91 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
105 FieldEncoding(
bool b, SmallStringEnc &e) : HasName(
b), Enc(e.c_str()) {}
106 StringRef str() {
return Enc; }
107 bool operator<(
const FieldEncoding &rhs)
const {
108 if (HasName != rhs.HasName)
return HasName;
109 return Enc < rhs.Enc;
121 mutable TypeStringCache TSC;
122 void emitTargetMD(
const Decl *
D, llvm::GlobalValue *GV,
129 const llvm::MapVector<GlobalDecl, StringRef>
130 &MangledDeclNames)
const override;
148 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
149 llvm::Type *ArgTy = CGT.ConvertType(Ty);
152 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
160 llvm_unreachable(
"Unsupported ABI kind for va_arg");
162 Val =
Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeAlign);
170 ArgSize = ArgSize.
alignTo(SlotSize);
175 Val =
Address(Builder.CreateLoad(Val), ArgTy, TypeAlign);
182 Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
195 std::string StubEnc) {
199 assert( (
E.Str.empty() ||
E.State == Recursive) &&
200 "Incorrectly use of addIncomplete");
201 assert(!StubEnc.empty() &&
"Passing an empty string to addIncomplete()");
202 E.Swapped.swap(
E.Str);
215 auto I = Map.find(ID);
216 assert(I != Map.end() &&
"Entry not present");
217 Entry &
E = I->second;
219 E.State == IncompleteUsed) &&
220 "Entry must be an incomplete type");
221 bool IsRecursive =
false;
222 if (
E.State == IncompleteUsed) {
225 --IncompleteUsedCount;
227 if (
E.Swapped.empty())
231 E.Swapped.swap(
E.Str);
241void TypeStringCache::addIfComplete(
const IdentifierInfo *ID, StringRef Str,
243 if (!ID || IncompleteUsedCount)
246 if (IsRecursive && !
E.Str.empty()) {
247 assert(
E.State==Recursive &&
E.Str.size() == Str.size() &&
248 "This is not the same Recursive entry");
254 assert(
E.Str.empty() &&
"Entry already present");
256 E.State = IsRecursive? Recursive : NonRecursive;
265 auto I = Map.find(ID);
268 Entry &
E = I->second;
269 if (
E.State == Recursive && IncompleteCount)
274 E.State = IncompleteUsed;
275 ++IncompleteUsedCount;
294 TypeStringCache &TSC);
297void XCoreTargetCodeGenInfo::emitTargetMD(
298 const Decl *
D, llvm::GlobalValue *GV,
302 llvm::LLVMContext &Ctx = CGM.
getModule().getContext();
303 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
304 llvm::MDString::get(Ctx, Enc.str())};
305 llvm::NamedMDNode *MD =
306 CGM.
getModule().getOrInsertNamedMetadata(
"xcore.typestrings");
307 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
311void XCoreTargetCodeGenInfo::emitTargetMetadata(
313 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames)
const {
317 for (
unsigned I = 0; I != MangledDeclNames.size(); ++I) {
318 auto Val = *(MangledDeclNames.begin() + I);
321 const Decl *
D = Val.first.getDecl()->getMostRecentDecl();
322 emitTargetMD(
D, GV, CGM);
329 TypeStringCache &TSC);
337 TypeStringCache &TSC) {
338 for (
const auto *Field : RD->
fields()) {
341 Enc += Field->getName();
343 if (Field->isBitField()) {
345 llvm::raw_svector_ostream OS(Enc);
346 OS << Field->getBitWidthValue(CGM.
getContext());
349 if (!
appendType(Enc, Field->getType(), CGM, TSC))
351 if (Field->isBitField())
354 FE.emplace_back(!Field->getName().empty(), Enc);
366 StringRef TypeString = TSC.lookupStr(ID);
367 if (!TypeString.empty()) {
373 size_t Start = Enc.size();
377 Enc += ID->getName();
381 bool IsRecursive =
false;
388 std::string StubEnc(Enc.substr(Start).str());
390 TSC.addIncomplete(ID, std::move(StubEnc));
392 (void) TSC.removeIncomplete(ID);
395 IsRecursive = TSC.removeIncomplete(ID);
401 unsigned E = FE.size();
402 for (
unsigned I = 0; I !=
E; ++I) {
409 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
415 TypeStringCache &TSC,
418 StringRef TypeString = TSC.lookupStr(ID);
419 if (!TypeString.empty()) {
424 size_t Start = Enc.size();
427 Enc += ID->getName();
433 for (
auto I = ED->enumerator_begin(),
E = ED->enumerator_end(); I !=
E;
435 SmallStringEnc EnumEnc;
437 EnumEnc += I->getName();
439 I->getInitVal().toString(EnumEnc);
441 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
444 unsigned E = FE.size();
445 for (
unsigned I = 0; I !=
E; ++I) {
452 TSC.addIfComplete(ID, Enc.substr(Start),
false);
460 static const char *
const Table[]={
"",
"c:",
"r:",
"cr:",
"v:",
"cv:",
"rv:",
"crv:"};
468 Enc += Table[Lookup];
475 case BuiltinType::Void:
478 case BuiltinType::Bool:
481 case BuiltinType::Char_U:
484 case BuiltinType::UChar:
487 case BuiltinType::SChar:
490 case BuiltinType::UShort:
493 case BuiltinType::Short:
496 case BuiltinType::UInt:
499 case BuiltinType::Int:
502 case BuiltinType::ULong:
505 case BuiltinType::Long:
508 case BuiltinType::ULongLong:
511 case BuiltinType::LongLong:
514 case BuiltinType::Float:
517 case BuiltinType::Double:
520 case BuiltinType::LongDouble:
533 TypeStringCache &TSC) {
545 TypeStringCache &TSC, StringRef NoSizeEnc) {
550 CAT->getSize().toStringUnsigned(Enc);
566 TypeStringCache &TSC) {
573 auto I = FPT->param_type_begin();
574 auto E = FPT->param_type_end();
583 if (FPT->isVariadic())
586 if (FPT->isVariadic())
600 TypeStringCache &TSC) {
634 TypeStringCache &TSC) {
641 return appendType(Enc, FD->getType(), CGM, TSC);
644 if (
const VarDecl *VD = dyn_cast<VarDecl>(
D)) {
647 QualType QT = VD->getType().getCanonicalType();
659std::unique_ptr<TargetCodeGenInfo>
661 return std::make_unique<XCoreTargetCodeGenInfo>(CGM.
getTypes());
static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC, const IdentifierInfo *ID)
Appends structure and union types to Enc and adds encoding to cache.
static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT)
Appends built-in types to Enc.
static bool extractFieldType(SmallVectorImpl< FieldEncoding > &FE, const RecordDecl *RD, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
Helper function for appendRecordType().
static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
Appends a pointer encoding to Enc before calling appendType for the pointee.
static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
Appends a function encoding to Enc, calling appendType for the return type and the arguments.
static bool getTypeString(SmallStringEnc &Enc, const Decl *D, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
The XCore ABI includes a type information section that communicates symbol type information to the li...
static void appendQualifier(SmallStringEnc &Enc, QualType QT)
Appends type's qualifier to Enc.
static bool appendType(SmallStringEnc &Enc, QualType QType, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
Handles the type's qualifier before dispatching a call to handle specific type encodings.
static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, TypeStringCache &TSC, const IdentifierInfo *ID)
Appends enum types to Enc and adds the encoding to the cache.
static bool appendArrayType(SmallStringEnc &Enc, QualType QT, const ArrayType *AT, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC, StringRef NoSizeEnc)
Appends array encoding to Enc before calling appendType for the element.
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
Represents an array type, per C99 6.7.5.2 - Array Declarators.
ArraySizeModifier getSizeModifier() const
QualType getElementType() const
This class is used for builtin types like 'int'.
CharUnits - This is an opaque type for sizes expressed in character units.
bool isZero() const
isZero - Test whether the quantity equals zero.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
void setCoerceToType(llvm::Type *T)
@ Extend
Extend - Valid only for integer argument types.
@ Ignore
Ignore - Ignore the argument (treat as void).
@ IndirectAliased
IndirectAliased - Similar to Indirect, but the pointer may be to an object that is otherwise referenc...
@ Expand
Expand - Only valid for aggregate argument types.
@ InAlloca
InAlloca - Pass the argument directly using the LLVM inalloca attribute.
@ Indirect
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
@ CoerceAndExpand
CoerceAndExpand - Only valid for aggregate argument types.
@ Direct
Direct - Pass the argument directly using the normal converted LLVM type, or by coercing to another s...
llvm::Type * getCoerceToType() const
bool canHaveCoerceToType() const
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
RValue EmitLoadOfAnyValue(LValue V, AggValueSlot Slot=AggValueSlot::ignored(), SourceLocation Loc={})
Like EmitLoadOfLValue but also handles complex and aggregate types.
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
CodeGenTypes & getTypes()
ASTContext & getContext() const
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
DefaultABIInfo - The default implementation for ABI specific details.
RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, AggValueSlot Slot) const override
EmitVAArg - Emit the target dependent code to load a value of.
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
virtual void emitTargetMetadata(CodeGen::CodeGenModule &CGM, const llvm::MapVector< GlobalDecl, StringRef > &MangledDeclNames) const
emitTargetMetadata - Provides a convenient hook to handle extra target-specific metadata for the give...
Represents the canonical version of C arrays with a specified constant size.
Decl - This represents one declaration (or definition), e.g.
EnumDecl * getDefinition() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
EnumDecl * getDecl() const
Represents a function declaration or definition.
Represents a prototype with parameter type info, e.g.
FunctionType - C99 6.7.5.3 - Function Declarators.
QualType getReturnType() const
One of these records is kept for each identifier that is lexed.
PointerType - C99 6.7.5.1 - Pointer Declarators.
QualType getPointeeType() const
A (possibly-)qualified type.
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
const IdentifierInfo * getBaseTypeIdentifier() const
Retrieves a pointer to the name of the base type.
QualType getCanonicalType() const
bool isConstQualified() const
Determine whether this type is const-qualified.
Represents a struct/union/class.
field_range fields() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
RecordDecl * getDecl() const
const RecordType * getAsUnionType() const
NOTE: getAs*ArrayType are methods on ASTContext.
const RecordType * getAsStructureType() const
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
const T * getAs() const
Member-template getAs<specific type>'.
Represents a variable declaration or definition.
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
llvm::Type * getVAListElementType(CodeGenFunction &CGF)
std::unique_ptr< TargetCodeGenInfo > createXCoreTargetCodeGenInfo(CodeGenModule &CGM)
The JSON file list parser is used to communicate input to InstallAPI.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
@ Incomplete
Template argument deduction did not deduce a value for every template parameter.