LLVM API Documentation

ParserInternals.h

Go to the documentation of this file.
00001 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file defines the various variables that are shared among the
00011 //  different components of the parser...
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef PARSER_INTERNALS_H
00016 #define PARSER_INTERNALS_H
00017 
00018 #include "llvm/Constants.h"
00019 #include "llvm/DerivedTypes.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/Instructions.h"
00022 #include "llvm/Assembly/Parser.h"
00023 #include "llvm/ADT/StringExtras.h"
00024 
00025 
00026 // Global variables exported from the lexer...
00027 
00028 extern int llvmAsmlineno;
00029 
00030 extern std::string &llvmAsmTextin;
00031 
00032 // functions exported from the lexer
00033 void set_scan_file(FILE * F);
00034 void set_scan_string (const char * str);
00035 
00036 // Globals exported by the parser...
00037 extern char* llvmAsmtext;
00038 extern int   llvmAsmleng;
00039 
00040 namespace llvm {
00041 
00042 // Globals exported by the parser...
00043 extern std::string CurFilename;
00044 
00045 class Module;
00046 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
00047 
00048 // Parse a string directly
00049 Module *RunVMAsmParser(const char * AsmString, Module * M);
00050 
00051 
00052 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
00053 // appropriate character.  If AllowNull is set to false, a \00 value will cause
00054 // an exception to be thrown.
00055 //
00056 // If AllowNull is set to true, the return value of the function points to the
00057 // last character of the string in memory.
00058 //
00059 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
00060 
00061 
00062 // ThrowException - Wrapper around the ParseException class that automatically
00063 // fills in file line number and column number and options info.
00064 //
00065 // This also helps me because I keep typing 'throw new ParseException' instead
00066 // of just 'throw ParseException'... sigh...
00067 //
00068 static inline void ThrowException(const std::string &message,
00069                                   int LineNo = -1) {
00070   if (LineNo == -1) LineNo = llvmAsmlineno;
00071   // TODO: column number in exception
00072   throw ParseException(CurFilename, message, LineNo);
00073 }
00074 
00075 /// InlineAsmDescriptor - This is a simple class that holds info about inline
00076 /// asm blocks, for use by ValID.
00077 struct InlineAsmDescriptor {
00078   std::string AsmString, Constraints;
00079   bool HasSideEffects;
00080   
00081   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
00082     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
00083 };
00084 
00085 
00086 // ValID - Represents a reference of a definition of some sort.  This may either
00087 // be a numeric reference or a symbolic (%var) reference.  This is just a
00088 // discriminated union.
00089 //
00090 // Note that I can't implement this class in a straight forward manner with
00091 // constructors and stuff because it goes in a union.
00092 //
00093 struct ValID {
00094   enum {
00095     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
00096     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
00097   } Type;
00098 
00099   union {
00100     int      Num;         // If it's a numeric reference
00101     char    *Name;        // If it's a named reference.  Memory must be free'd.
00102     int64_t  ConstPool64; // Constant pool reference.  This is the value
00103     uint64_t UConstPool64;// Unsigned constant pool reference.
00104     double   ConstPoolFP; // Floating point constant pool reference
00105     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
00106     InlineAsmDescriptor *IAD;
00107   };
00108 
00109   static ValID create(int Num) {
00110     ValID D; D.Type = NumberVal; D.Num = Num; return D;
00111   }
00112 
00113   static ValID create(char *Name) {
00114     ValID D; D.Type = NameVal; D.Name = Name; return D;
00115   }
00116 
00117   static ValID create(int64_t Val) {
00118     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
00119   }
00120 
00121   static ValID create(uint64_t Val) {
00122     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
00123   }
00124 
00125   static ValID create(double Val) {
00126     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
00127   }
00128 
00129   static ValID createNull() {
00130     ValID D; D.Type = ConstNullVal; return D;
00131   }
00132 
00133   static ValID createUndef() {
00134     ValID D; D.Type = ConstUndefVal; return D;
00135   }
00136 
00137   static ValID createZeroInit() {
00138     ValID D; D.Type = ConstZeroVal; return D;
00139   }
00140   
00141   static ValID create(Constant *Val) {
00142     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
00143   }
00144   
00145   static ValID createInlineAsm(const std::string &AsmString,
00146                                const std::string &Constraints,
00147                                bool HasSideEffects) {
00148     ValID D;
00149     D.Type = InlineAsmVal;
00150     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
00151     return D;
00152   }
00153 
00154   inline void destroy() const {
00155     if (Type == NameVal)
00156       free(Name);    // Free this strdup'd memory.
00157     else if (Type == InlineAsmVal)
00158       delete IAD;
00159   }
00160 
00161   inline ValID copy() const {
00162     if (Type != NameVal) return *this;
00163     ValID Result = *this;
00164     Result.Name = strdup(Name);
00165     return Result;
00166   }
00167 
00168   inline std::string getName() const {
00169     switch (Type) {
00170     case NumberVal     : return std::string("#") + itostr(Num);
00171     case NameVal       : return Name;
00172     case ConstFPVal    : return ftostr(ConstPoolFP);
00173     case ConstNullVal  : return "null";
00174     case ConstUndefVal : return "undef";
00175     case ConstZeroVal  : return "zeroinitializer";
00176     case ConstUIntVal  :
00177     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
00178     case ConstantVal:
00179       if (ConstantValue == ConstantBool::True) return "true";
00180       if (ConstantValue == ConstantBool::False) return "false";
00181       return "<constant expression>";
00182     default:
00183       assert(0 && "Unknown value!");
00184       abort();
00185       return "";
00186     }
00187   }
00188 
00189   bool operator<(const ValID &V) const {
00190     if (Type != V.Type) return Type < V.Type;
00191     switch (Type) {
00192     case NumberVal:     return Num < V.Num;
00193     case NameVal:       return strcmp(Name, V.Name) < 0;
00194     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
00195     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
00196     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
00197     case ConstNullVal:  return false;
00198     case ConstUndefVal: return false;
00199     case ConstZeroVal: return false;
00200     case ConstantVal:   return ConstantValue < V.ConstantValue;
00201     default:  assert(0 && "Unknown value type!"); return false;
00202     }
00203   }
00204 };
00205 
00206 } // End llvm namespace
00207 
00208 #endif