2003-10-13 11:32:08 +08:00
|
|
|
//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
|
2005-04-22 05:10:11 +08:00
|
|
|
//
|
2003-10-21 23:17:13 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 05:10:11 +08:00
|
|
|
//
|
2003-10-21 23:17:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
2005-04-22 05:10:11 +08:00
|
|
|
// This header file defines the various variables that are shared among the
|
2001-06-07 04:29:01 +08:00
|
|
|
// different components of the parser...
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef PARSER_INTERNALS_H
|
|
|
|
#define PARSER_INTERNALS_H
|
|
|
|
|
2002-04-29 03:55:58 +08:00
|
|
|
#include "llvm/Constants.h"
|
2001-09-08 00:33:01 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-04-09 14:17:21 +08:00
|
|
|
#include "llvm/ParameterAttributes.h"
|
2004-07-29 20:17:34 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-09-07 02:13:44 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2005-05-20 11:25:47 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
// Global variables exported from the lexer...
|
2005-05-20 11:25:47 +08:00
|
|
|
|
2006-08-18 16:43:06 +08:00
|
|
|
extern int llvmAsmlineno; /// FIXME: Not threading friendly
|
|
|
|
extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2005-05-20 11:25:47 +08:00
|
|
|
extern std::string &llvmAsmTextin;
|
|
|
|
|
|
|
|
// functions exported from the lexer
|
|
|
|
void set_scan_file(FILE * F);
|
|
|
|
void set_scan_string (const char * str);
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
// Globals exported by the parser...
|
|
|
|
extern char* llvmAsmtext;
|
|
|
|
extern int llvmAsmleng;
|
|
|
|
|
|
|
|
namespace llvm {
|
2007-05-23 02:52:21 +08:00
|
|
|
class Module;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
// Globals exported by the parser...
|
2006-08-18 16:43:06 +08:00
|
|
|
extern std::string CurFilename; /// FIXME: Not threading friendly
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2007-05-23 02:52:21 +08:00
|
|
|
// RunVMAsmParser - Parse a file and return Module
|
2002-01-21 06:54:45 +08:00
|
|
|
Module *RunVMAsmParser(const std::string &Filename, FILE *F);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2005-05-20 11:25:47 +08:00
|
|
|
// Parse a string directly
|
|
|
|
Module *RunVMAsmParser(const char * AsmString, Module * M);
|
|
|
|
|
2001-07-29 01:48:55 +08:00
|
|
|
// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
|
2007-05-23 02:52:21 +08:00
|
|
|
// appropriate character.
|
|
|
|
char *UnEscapeLexed(char *Buffer);
|
2001-07-29 01:48:55 +08:00
|
|
|
|
2007-05-23 02:52:21 +08:00
|
|
|
// GenerateError - Wrapper around the ParseException class that automatically
|
2001-06-07 04:29:01 +08:00
|
|
|
// fills in file line number and column number and options info.
|
|
|
|
//
|
2005-04-22 05:10:11 +08:00
|
|
|
// This also helps me because I keep typing 'throw new ParseException' instead
|
2001-06-07 04:29:01 +08:00
|
|
|
// of just 'throw ParseException'... sigh...
|
|
|
|
//
|
2006-08-18 16:43:06 +08:00
|
|
|
extern void GenerateError(const std::string &message, int LineNo = -1);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2006-01-26 06:26:43 +08:00
|
|
|
/// InlineAsmDescriptor - This is a simple class that holds info about inline
|
|
|
|
/// asm blocks, for use by ValID.
|
|
|
|
struct InlineAsmDescriptor {
|
|
|
|
std::string AsmString, Constraints;
|
|
|
|
bool HasSideEffects;
|
|
|
|
|
|
|
|
InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
|
|
|
|
: AsmString(as), Constraints(c), HasSideEffects(HSE) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
// ValID - Represents a reference of a definition of some sort. This may either
|
2005-04-22 05:10:11 +08:00
|
|
|
// be a numeric reference or a symbolic (%var) reference. This is just a
|
2001-06-07 04:29:01 +08:00
|
|
|
// discriminated union.
|
|
|
|
//
|
2005-04-22 05:10:11 +08:00
|
|
|
// Note that I can't implement this class in a straight forward manner with
|
2003-12-24 04:05:15 +08:00
|
|
|
// constructors and stuff because it goes in a union.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
struct ValID {
|
2001-10-01 06:46:54 +08:00
|
|
|
enum {
|
2007-01-26 16:04:51 +08:00
|
|
|
LocalID, GlobalID, LocalName, GlobalName,
|
|
|
|
ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
|
2006-01-26 06:26:43 +08:00
|
|
|
ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
|
2001-10-01 06:46:54 +08:00
|
|
|
} Type;
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
union {
|
2007-01-26 16:04:51 +08:00
|
|
|
unsigned Num; // If it's a numeric reference like %1234
|
2007-05-23 02:52:21 +08:00
|
|
|
std::string *Name; // If it's a named reference. Memory must be deleted.
|
2001-06-07 04:29:01 +08:00
|
|
|
int64_t ConstPool64; // Constant pool reference. This is the value
|
|
|
|
uint64_t UConstPool64;// Unsigned constant pool reference.
|
2007-09-07 02:13:44 +08:00
|
|
|
APFloat *ConstPoolFP; // Floating point constant pool reference
|
2002-08-17 05:14:40 +08:00
|
|
|
Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
|
2006-01-26 06:26:43 +08:00
|
|
|
InlineAsmDescriptor *IAD;
|
2007-09-07 02:13:44 +08:00
|
|
|
};
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2007-01-26 16:04:51 +08:00
|
|
|
static ValID createLocalID(unsigned Num) {
|
|
|
|
ValID D; D.Type = LocalID; D.Num = Num; return D;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2007-01-26 16:04:51 +08:00
|
|
|
static ValID createGlobalID(unsigned Num) {
|
|
|
|
ValID D; D.Type = GlobalID; D.Num = Num; return D;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2007-05-23 02:52:21 +08:00
|
|
|
static ValID createLocalName(const std::string &Name) {
|
|
|
|
ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
|
2007-01-26 16:04:51 +08:00
|
|
|
}
|
2007-05-23 02:52:21 +08:00
|
|
|
static ValID createGlobalName(const std::string &Name) {
|
|
|
|
ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
|
2007-01-26 16:04:51 +08:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
static ValID create(int64_t Val) {
|
2001-10-01 06:46:54 +08:00
|
|
|
ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ValID create(uint64_t Val) {
|
2001-10-01 06:46:54 +08:00
|
|
|
ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2007-09-07 02:13:44 +08:00
|
|
|
static ValID create(APFloat *Val) {
|
2001-10-01 06:46:54 +08:00
|
|
|
ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ValID createNull() {
|
|
|
|
ValID D; D.Type = ConstNullVal; return D;
|
2001-07-15 08:17:01 +08:00
|
|
|
}
|
|
|
|
|
2004-10-17 02:17:13 +08:00
|
|
|
static ValID createUndef() {
|
|
|
|
ValID D; D.Type = ConstUndefVal; return D;
|
|
|
|
}
|
|
|
|
|
2005-12-22 01:53:02 +08:00
|
|
|
static ValID createZeroInit() {
|
|
|
|
ValID D; D.Type = ConstZeroVal; return D;
|
|
|
|
}
|
|
|
|
|
2002-08-17 05:14:40 +08:00
|
|
|
static ValID create(Constant *Val) {
|
|
|
|
ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
|
|
|
|
}
|
2006-01-26 06:26:43 +08:00
|
|
|
|
|
|
|
static ValID createInlineAsm(const std::string &AsmString,
|
|
|
|
const std::string &Constraints,
|
|
|
|
bool HasSideEffects) {
|
|
|
|
ValID D;
|
|
|
|
D.Type = InlineAsmVal;
|
|
|
|
D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
|
|
|
|
return D;
|
|
|
|
}
|
2002-08-17 05:14:40 +08:00
|
|
|
|
2001-07-27 00:29:15 +08:00
|
|
|
inline void destroy() const {
|
2007-01-26 16:04:51 +08:00
|
|
|
if (Type == LocalName || Type == GlobalName)
|
2007-05-23 02:52:21 +08:00
|
|
|
delete Name; // Free this strdup'd memory.
|
2006-01-26 06:26:43 +08:00
|
|
|
else if (Type == InlineAsmVal)
|
|
|
|
delete IAD;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ValID copy() const {
|
2007-01-26 16:04:51 +08:00
|
|
|
if (Type != LocalName && Type != GlobalName) return *this;
|
2001-06-07 04:29:01 +08:00
|
|
|
ValID Result = *this;
|
2007-05-23 02:52:21 +08:00
|
|
|
Result.Name = new std::string(*Name);
|
2001-06-07 04:29:01 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
inline std::string getName() const {
|
2001-06-07 04:29:01 +08:00
|
|
|
switch (Type) {
|
2007-01-26 16:04:51 +08:00
|
|
|
case LocalID : return '%' + utostr(Num);
|
|
|
|
case GlobalID : return '@' + utostr(Num);
|
2007-05-23 02:52:21 +08:00
|
|
|
case LocalName : return *Name;
|
|
|
|
case GlobalName : return *Name;
|
2007-09-07 02:13:44 +08:00
|
|
|
case ConstFPVal : return ftostr(*ConstPoolFP);
|
2001-10-01 06:46:54 +08:00
|
|
|
case ConstNullVal : return "null";
|
2004-10-17 02:17:13 +08:00
|
|
|
case ConstUndefVal : return "undef";
|
2005-12-22 01:53:02 +08:00
|
|
|
case ConstZeroVal : return "zeroinitializer";
|
2001-10-01 06:46:54 +08:00
|
|
|
case ConstUIntVal :
|
2002-01-21 06:54:45 +08:00
|
|
|
case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
|
2002-08-17 05:14:40 +08:00
|
|
|
case ConstantVal:
|
2007-01-11 20:24:14 +08:00
|
|
|
if (ConstantValue == ConstantInt::getTrue()) return "true";
|
|
|
|
if (ConstantValue == ConstantInt::getFalse()) return "false";
|
2002-08-17 05:14:40 +08:00
|
|
|
return "<constant expression>";
|
2001-10-01 06:46:54 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unknown value!");
|
|
|
|
abort();
|
2002-01-21 06:54:45 +08:00
|
|
|
return "";
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
2001-10-13 14:37:47 +08:00
|
|
|
|
|
|
|
bool operator<(const ValID &V) const {
|
|
|
|
if (Type != V.Type) return Type < V.Type;
|
|
|
|
switch (Type) {
|
2007-01-26 16:04:51 +08:00
|
|
|
case LocalID:
|
|
|
|
case GlobalID: return Num < V.Num;
|
|
|
|
case LocalName:
|
2007-05-23 02:52:21 +08:00
|
|
|
case GlobalName: return *Name < *V.Name;
|
2001-10-13 14:37:47 +08:00
|
|
|
case ConstSIntVal: return ConstPool64 < V.ConstPool64;
|
|
|
|
case ConstUIntVal: return UConstPool64 < V.UConstPool64;
|
2007-09-07 02:13:44 +08:00
|
|
|
case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
|
|
|
|
APFloat::cmpLessThan;
|
2001-10-13 14:37:47 +08:00
|
|
|
case ConstNullVal: return false;
|
2004-10-17 02:17:13 +08:00
|
|
|
case ConstUndefVal: return false;
|
2005-12-22 01:53:02 +08:00
|
|
|
case ConstZeroVal: return false;
|
2002-08-17 05:14:40 +08:00
|
|
|
case ConstantVal: return ConstantValue < V.ConstantValue;
|
2001-10-13 14:37:47 +08:00
|
|
|
default: assert(0 && "Unknown value type!"); return false;
|
|
|
|
}
|
|
|
|
}
|
2007-03-20 02:34:28 +08:00
|
|
|
|
|
|
|
bool operator==(const ValID &V) const {
|
|
|
|
if (Type == V.Type) {
|
|
|
|
switch (Type) {
|
|
|
|
case LocalID:
|
|
|
|
case GlobalID: return Num == V.Num;
|
|
|
|
case LocalName:
|
2007-05-23 02:52:21 +08:00
|
|
|
case GlobalName: return *Name == *(V.Name);
|
2007-03-20 02:34:28 +08:00
|
|
|
case ConstSIntVal: return ConstPool64 == V.ConstPool64;
|
|
|
|
case ConstUIntVal: return UConstPool64 == V.UConstPool64;
|
2007-09-07 02:13:44 +08:00
|
|
|
case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
|
|
|
|
APFloat::cmpEqual;
|
2007-03-20 02:34:28 +08:00
|
|
|
case ConstantVal: return ConstantValue == V.ConstantValue;
|
|
|
|
case ConstNullVal: return true;
|
|
|
|
case ConstUndefVal: return true;
|
|
|
|
case ConstZeroVal: return true;
|
|
|
|
default: assert(0 && "Unknown value type!"); return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
};
|
|
|
|
|
2006-12-31 13:40:51 +08:00
|
|
|
struct TypeWithAttrs {
|
|
|
|
llvm::PATypeHolder *Ty;
|
2007-04-09 14:17:21 +08:00
|
|
|
uint16_t Attrs;
|
2006-12-31 13:40:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
|
|
|
|
|
|
|
|
struct ArgListEntry {
|
2007-04-09 14:17:21 +08:00
|
|
|
uint16_t Attrs;
|
2006-12-31 13:40:51 +08:00
|
|
|
llvm::PATypeHolder *Ty;
|
2007-05-23 02:52:21 +08:00
|
|
|
std::string *Name;
|
2006-12-31 13:40:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<struct ArgListEntry> ArgListType;
|
|
|
|
|
|
|
|
struct ValueRefListEntry {
|
|
|
|
Value *Val;
|
2007-04-09 14:17:21 +08:00
|
|
|
uint16_t Attrs;
|
2006-12-31 13:40:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<ValueRefListEntry> ValueRefList;
|
|
|
|
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
#endif
|