clang API Documentation

ABIInfo.h
Go to the documentation of this file.
00001 //===----- ABIInfo.h - ABI information access & encapsulation ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef CLANG_CODEGEN_ABIINFO_H
00011 #define CLANG_CODEGEN_ABIINFO_H
00012 
00013 #include "clang/AST/Type.h"
00014 #include "llvm/Type.h"
00015 
00016 namespace llvm {
00017   class Value;
00018   class LLVMContext;
00019   class TargetData;
00020 }
00021 
00022 namespace clang {
00023   class ASTContext;
00024 
00025   namespace CodeGen {
00026     class CGFunctionInfo;
00027     class CodeGenFunction;
00028     class CodeGenTypes;
00029   }
00030 
00031   // FIXME: All of this stuff should be part of the target interface
00032   // somehow. It is currently here because it is not clear how to factor
00033   // the targets to support this, since the Targets currently live in a
00034   // layer below types n'stuff.
00035 
00036   /// ABIArgInfo - Helper class to encapsulate information about how a
00037   /// specific C type should be passed to or returned from a function.
00038   class ABIArgInfo {
00039   public:
00040     enum Kind {
00041       /// Direct - Pass the argument directly using the normal converted LLVM
00042       /// type, or by coercing to another specified type stored in
00043       /// 'CoerceToType').  If an offset is specified (in UIntData), then the
00044       /// argument passed is offset by some number of bytes in the memory
00045       /// representation. A dummy argument is emitted before the real argument
00046       /// if the specified type stored in "PaddingType" is not zero.
00047       Direct,
00048 
00049       /// Extend - Valid only for integer argument types. Same as 'direct'
00050       /// but also emit a zero/sign extension attribute.
00051       Extend,
00052 
00053       /// Indirect - Pass the argument indirectly via a hidden pointer
00054       /// with the specified alignment (0 indicates default alignment).
00055       Indirect,
00056 
00057       /// Ignore - Ignore the argument (treat as void). Useful for void and
00058       /// empty structs.
00059       Ignore,
00060 
00061       /// Expand - Only valid for aggregate argument types. The structure should
00062       /// be expanded into consecutive arguments for its constituent fields.
00063       /// Currently expand is only allowed on structures whose fields
00064       /// are all scalar types or are themselves expandable types.
00065       Expand,
00066 
00067       KindFirst=Direct, KindLast=Expand
00068     };
00069 
00070   private:
00071     Kind TheKind;
00072     llvm::Type *TypeData;
00073     llvm::Type *PaddingType; // Currently allowed only for Direct.
00074     unsigned UIntData;
00075     bool BoolData0;
00076     bool BoolData1;
00077 
00078     ABIArgInfo(Kind K, llvm::Type *TD=0, unsigned UI=0,
00079                bool B0 = false, bool B1 = false, llvm::Type* P = 0)
00080       : TheKind(K), TypeData(TD), PaddingType(P), UIntData(UI), BoolData0(B0),
00081         BoolData1(B1) {}
00082 
00083   public:
00084     ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
00085 
00086     static ABIArgInfo getDirect(llvm::Type *T = 0, unsigned Offset = 0,
00087                                 llvm::Type *Padding = 0) {
00088       return ABIArgInfo(Direct, T, Offset, false, false, Padding);
00089     }
00090     static ABIArgInfo getExtend(llvm::Type *T = 0) {
00091       return ABIArgInfo(Extend, T, 0);
00092     }
00093     static ABIArgInfo getIgnore() {
00094       return ABIArgInfo(Ignore);
00095     }
00096     static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true
00097                                   , bool Realign = false) {
00098       return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign);
00099     }
00100     static ABIArgInfo getExpand() {
00101       return ABIArgInfo(Expand);
00102     }
00103 
00104     Kind getKind() const { return TheKind; }
00105     bool isDirect() const { return TheKind == Direct; }
00106     bool isExtend() const { return TheKind == Extend; }
00107     bool isIgnore() const { return TheKind == Ignore; }
00108     bool isIndirect() const { return TheKind == Indirect; }
00109     bool isExpand() const { return TheKind == Expand; }
00110 
00111     bool canHaveCoerceToType() const {
00112       return TheKind == Direct || TheKind == Extend;
00113     }
00114 
00115     // Direct/Extend accessors
00116     unsigned getDirectOffset() const {
00117       assert((isDirect() || isExtend()) && "Not a direct or extend kind");
00118       return UIntData;
00119     }
00120 
00121     llvm::Type *getPaddingType() const {
00122       return PaddingType;
00123     }
00124 
00125     llvm::Type *getCoerceToType() const {
00126       assert(canHaveCoerceToType() && "Invalid kind!");
00127       return TypeData;
00128     }
00129 
00130     void setCoerceToType(llvm::Type *T) {
00131       assert(canHaveCoerceToType() && "Invalid kind!");
00132       TypeData = T;
00133     }
00134 
00135     // Indirect accessors
00136     unsigned getIndirectAlign() const {
00137       assert(TheKind == Indirect && "Invalid kind!");
00138       return UIntData;
00139     }
00140 
00141     bool getIndirectByVal() const {
00142       assert(TheKind == Indirect && "Invalid kind!");
00143       return BoolData0;
00144     }
00145 
00146     bool getIndirectRealign() const {
00147       assert(TheKind == Indirect && "Invalid kind!");
00148       return BoolData1;
00149     }
00150 
00151     void dump() const;
00152   };
00153 
00154   /// ABIInfo - Target specific hooks for defining how a type should be
00155   /// passed or returned from functions.
00156   class ABIInfo {
00157   public:
00158     CodeGen::CodeGenTypes &CGT;
00159 
00160     ABIInfo(CodeGen::CodeGenTypes &cgt) : CGT(cgt) {}
00161     virtual ~ABIInfo();
00162 
00163     ASTContext &getContext() const;
00164     llvm::LLVMContext &getVMContext() const;
00165     const llvm::TargetData &getTargetData() const;
00166 
00167     virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
00168 
00169     /// EmitVAArg - Emit the target dependent code to load a value of
00170     /// \arg Ty from the va_list pointed to by \arg VAListAddr.
00171 
00172     // FIXME: This is a gaping layering violation if we wanted to drop
00173     // the ABI information any lower than CodeGen. Of course, for
00174     // VAArg handling it has to be at this level; there is no way to
00175     // abstract this out.
00176     virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00177                                    CodeGen::CodeGenFunction &CGF) const = 0;
00178   };
00179 }  // end namespace clang
00180 
00181 #endif