1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-20 19:42:54 +02:00

Add support for global constants, and for initializers for constants

Clean up parser somewhat by factoring out freeing of ID's into setname function

llvm-svn: 597
This commit is contained in:
Chris Lattner 2001-09-18 04:00:54 +00:00
parent f8c425fc43
commit 0dfd40e910
2 changed files with 52 additions and 32 deletions

View File

@ -123,6 +123,8 @@ true { return TRUE; }
false { return FALSE; } false { return FALSE; }
declare { return DECLARE; } declare { return DECLARE; }
global { return GLOBAL; } global { return GLOBAL; }
constant { return CONSTANT; }
uninitialized { return UNINIT; }
implementation { return IMPLEMENTATION; } implementation { return IMPLEMENTATION; }
\.\.\. { return DOTDOTDOT; } \.\.\. { return DOTDOTDOT; }
string { return STRING; } string { return STRING; }

View File

@ -375,7 +375,15 @@ static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
} }
} }
static void setValueName(Value *V, const string &Name) { // setValueName - Set the specified value to the name given. The name may be
// null potentially, in which case this is a noop. The string passed in is
// assumed to be a malloc'd string buffer, and is freed by this function.
//
static void setValueName(Value *V, char *NameStr) {
if (NameStr == 0) return;
string Name(NameStr); // Copy string
free(NameStr); // Free old string
SymbolTable *ST = CurMeth.CurrentMethod ? SymbolTable *ST = CurMeth.CurrentMethod ?
CurMeth.CurrentMethod->getSymbolTableSure() : CurMeth.CurrentMethod->getSymbolTableSure() :
CurModule.CurrentModule->getSymbolTableSure(); CurModule.CurrentModule->getSymbolTableSure();
@ -514,6 +522,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
int SIntVal; int SIntVal;
unsigned UIntVal; unsigned UIntVal;
double FPVal; double FPVal;
bool BoolVal;
char *StrVal; // This memory is strdup'd! char *StrVal; // This memory is strdup'd!
ValID ValIDVal; // strdup'd memory maybe! ValID ValIDVal; // strdup'd memory maybe!
@ -538,6 +547,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
%type <ValueList> ValueRefList ValueRefListE // For call param lists %type <ValueList> ValueRefList ValueRefListE // For call param lists
%type <TypeList> TypeListI ArgTypeListI %type <TypeList> TypeListI ArgTypeListI
%type <JumpTable> JumpTable %type <JumpTable> JumpTable
%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB %type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
%type <ValueVal> ResolvedVal // <type> <valref> pair %type <ValueVal> ResolvedVal // <type> <valref> pair
@ -568,7 +578,8 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
%type <StrVal> OptVAR_ID OptAssign %type <StrVal> OptVAR_ID OptAssign
%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL TO DOTDOTDOT STRING %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
%token TO DOTDOTDOT STRING
// Basic Block Terminating Operators // Basic Block Terminating Operators
%token <TermOpVal> RET BR SWITCH %token <TermOpVal> RET BR SWITCH
@ -846,24 +857,20 @@ ConstVector : ConstVector ',' ConstVal {
} }
//ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' { // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
// } GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
//ExternVarDecl :
// ConstPool - Constants with optional names assigned to them. // ConstPool - Constants with optional names assigned to them.
ConstPool : ConstPool OptAssign ConstVal { ConstPool : ConstPool OptAssign ConstVal {
if ($2) { setValueName($3, $2);
setValueName($3, $2);
free($2);
}
InsertValue($3); InsertValue($3);
} }
| ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
if ($2) { // TODO: FIXME when Type are not const
// TODO: FIXME when Type are not const setValueName(const_cast<Type*>($4->get()), $2);
setValueName(const_cast<Type*>($4->get()), $2);
free($2); if (!$2) {
} else {
InsertType($4->get(), InsertType($4->get(),
CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types); CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
} }
@ -871,20 +878,36 @@ ConstPool : ConstPool OptAssign ConstVal {
} }
| ConstPool MethodProto { // Method prototypes can be in const pool | ConstPool MethodProto { // Method prototypes can be in const pool
} }
| ConstPool GLOBAL OptAssign Types { // Global declarations appear in CP | ConstPool OptAssign GlobalType ResolvedVal {
if (!$4->get()->isPointerType() || const Type *Ty = $4->getType();
(((PointerType*)$4->get())->isArrayType() && // Global declarations appear in Constant Pool
((PointerType*)$4->get())->castArrayType()->isUnsized())) { if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
ThrowException("Type '" + $4->get()->getDescription() + ThrowException("Type '" + Ty->getDescription() +
"' is not a pointer to a sized type!"); "' is not a sized type!");
} }
GlobalVariable *GV = new GlobalVariable(*$4); ConstPoolVal *Initializer = $4->castConstant();
delete $4; if (Initializer == 0)
if ($3) { ThrowException("Global value initializer is not a constant!");
setValueName(GV, $3);
free($3); GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
Initializer);
setValueName(GV, $2);
CurModule.CurrentModule->getGlobalList().push_back(GV);
InsertValue(GV, CurModule.Values);
}
| ConstPool OptAssign UNINIT GlobalType Types {
const Type *Ty = *$5;
// Global declarations appear in Constant Pool
if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
ThrowException("Type '" + Ty->getDescription() +
"' is not a sized type!");
} }
GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
setValueName(GV, $2);
CurModule.CurrentModule->getGlobalList().push_back(GV); CurModule.CurrentModule->getGlobalList().push_back(GV);
InsertValue(GV, CurModule.Values); InsertValue(GV, CurModule.Values);
} }
@ -930,10 +953,7 @@ OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
ArgVal : Types OptVAR_ID { ArgVal : Types OptVAR_ID {
$$ = new MethodArgument(*$1); delete $1; $$ = new MethodArgument(*$1); delete $1;
if ($2) { // Was the argument named? setValueName($$, $2);
setValueName($$, $2);
free($2); // The string was strdup'd, so free it now.
}
} }
ArgListH : ArgVal ',' ArgListH { ArgListH : ArgVal ',' ArgListH {
@ -1085,7 +1105,6 @@ BasicBlock : InstructionList BBTerminatorInst {
| LABELSTR InstructionList BBTerminatorInst { | LABELSTR InstructionList BBTerminatorInst {
$2->getInstList().push_back($3); $2->getInstList().push_back($3);
setValueName($2, $1); setValueName($2, $1);
free($1); // Free the strdup'd memory...
InsertValue($2); InsertValue($2);
$$ = $2; $$ = $2;
@ -1143,8 +1162,7 @@ JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
} }
Inst : OptAssign InstVal { Inst : OptAssign InstVal {
if ($1) // Is this definition named?? setValueName($2, $1); // Is this definition named?? if so, assign the name...
setValueName($2, $1); // if so, assign the name...
InsertValue($2); InsertValue($2);
$$ = $2; $$ = $2;