llvm.org GIT mirror llvm / df98617
Reimplement the old and horrible bison parser for .ll files with a nice and clean recursive descent parser. This change has a couple of ramifications: 1. The parser code is about 400 lines shorter (in what we maintain, not including what is autogenerated). 2. The code should be significantly faster than the old code because we don't have to work around bison's poor handling of datatypes with ctors/dtors. This also makes the code much more resistant to memory leaks. 3. We now get caret diagnostics from the .ll parser, woo. 4. The actual diagnostics emited from the parser are completely different so a bunch of testcases had to be updated. 5. I now disallow "%ty = type opaque %ty = type i32". There was no good reason to support this, it was just an accident of the old implementation. I have no reason to think that anyone is actually using this. 6. The syntax for sticking a global variable has changed to make it unambiguous. I don't think anyone is depending on this since only clang supports this and it is not solid yet, so I'm not worried about anything breaking. 7. This gets rid of the last use of bison, and along with it the .cvs files. I'll prune this from the makefiles as a subsequent commit. There are a few minor cleanups that can be done after this commit (suggestions welcome!) but this passes dejagnu testing and is ready for its time in the limelight. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61558 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
37 changed file(s) with 3976 addition(s) and 15799 deletion(s). Raw diff Collapse all Expand all
1919
2020 class Module;
2121 class ParseError;
22 class raw_ostream;
2223
2324 /// This function is the main interface to the LLVM Assembly Parser. It parses
2425 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
2829 /// @brief Parse LLVM Assembly from a file
2930 Module *ParseAssemblyFile(
3031 const std::string &Filename, ///< The name of the file to parse
31 ParseError* Error = 0 ///< If not null, an object to return errors in.
32 ParseError &Error ///< If not null, an object to return errors in.
3233 );
3334
3435 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
3839 /// run the verifier after parsing the file to check that it is okay.
3940 /// @brief Parse LLVM Assembly from a string
4041 Module *ParseAssemblyString(
41 const char * AsmString, ///< The string containing assembly
42 Module * M, ///< A module to add the assembly too.
43 ParseError* Error = 0 ///< If not null, an object to return errors in.
42 const char *AsmString, ///< The string containing assembly
43 Module *M, ///< A module to add the assembly too.
44 ParseError &Error ///< If not null, an object to return errors in.
4445 );
4546
4647 //===------------------------------------------------------------------------===
5758 ParseError() : Filename("unknown"), Message("none"), LineNo(0), ColumnNo(0) {}
5859 ParseError(const ParseError &E);
5960
61 void setFilename(const std::string &F) { Filename = F; }
62
6063 // getMessage - Return the message passed in at construction time plus extra
6164 // information extracted from the options used to parse with...
6265 //
7073 return Filename;
7174 }
7275
73 void setError(const std::string &filename, const std::string &message,
74 int LineNo = -1, int ColNo = -1);
76 void setError(const std::string &message, int lineNo = -1, int ColNo = -1,
77 const std::string &FileContents = "") {
78 Message = message;
79 LineNo = lineNo; ColumnNo = ColNo;
80 LineContents = FileContents;
81 }
7582
7683 // getErrorLocation - Return the line and column number of the error in the
7784 // input source file. The source filename can be derived from the
8188 inline void getErrorLocation(int &Line, int &Column) const {
8289 Line = LineNo; Column = ColumnNo;
8390 }
91
92 void PrintError(const char *ProgName, raw_ostream &S);
8493
8594 private :
8695 std::string Filename;
8796 std::string Message;
8897 int LineNo, ColumnNo; // -1 if not relevant
98 std::string LineContents;
8999
90 ParseError &operator=(const ParseError &E); // objects by reference
100 void operator=(const ParseError &E); // DO NOT IMPLEMENT
91101 };
92102
93103 } // End llvm namespace
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "LLLexer.h"
14 #include "ParserInternals.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Instruction.h"
1516 #include "llvm/Support/MemoryBuffer.h"
1617 #include "llvm/Support/MathExtras.h"
17
18 #include
19 #include "llvmAsmParser.h"
20
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Assembly/Parser.h"
2120 #include
2221 using namespace llvm;
22
23 bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
24 // Scan backward to find the start of the line.
25 const char *LineStart = ErrorLoc;
26 while (LineStart != CurBuf->getBufferStart() &&
27 LineStart[-1] != '\n' && LineStart[-1] != '\r')
28 --LineStart;
29 // Get the end of the line.
30 const char *LineEnd = ErrorLoc;
31 while (LineEnd != CurBuf->getBufferEnd() &&
32 LineEnd[0] != '\n' && LineEnd[0] != '\r')
33 ++LineEnd;
34
35 unsigned LineNo = 1;
36 for (const char *FP = CurBuf->getBufferStart(); FP != ErrorLoc; ++FP)
37 if (*FP == '\n') ++LineNo;
38
39 std::string LineContents(LineStart, LineEnd);
40 ErrorInfo.setError(Msg, LineNo, ErrorLoc-LineStart, LineContents);
41 return true;
42 }
2343
2444 //===----------------------------------------------------------------------===//
2545 // Helper functions.
2949 // long representation... this does not have to do input error checking,
3050 // because we know that the input will be matched by a suitable regex...
3151 //
32 static uint64_t atoull(const char *Buffer, const char *End) {
52 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
3353 uint64_t Result = 0;
3454 for (; Buffer != End; Buffer++) {
3555 uint64_t OldRes = Result;
3656 Result *= 10;
3757 Result += *Buffer-'0';
3858 if (Result < OldRes) { // Uh, oh, overflow detected!!!
39 GenerateError("constant bigger than 64 bits detected!");
59 Error("constant bigger than 64 bits detected!");
4060 return 0;
4161 }
4262 }
4363 return Result;
4464 }
4565
46 static uint64_t HexIntToVal(const char *Buffer, const char *End) {
66 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
4767 uint64_t Result = 0;
4868 for (; Buffer != End; ++Buffer) {
4969 uint64_t OldRes = Result;
5777 Result += C-'a'+10;
5878
5979 if (Result < OldRes) { // Uh, oh, overflow detected!!!
60 GenerateError("constant bigger than 64 bits detected!");
80 Error("constant bigger than 64 bits detected!");
6181 return 0;
6282 }
6383 }
6484 return Result;
6585 }
6686
67 // HexToFP - Convert the ascii string in hexadecimal format to the floating
68 // point representation of it.
69 //
70 static double HexToFP(const char *Buffer, const char *End) {
71 return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
72 }
73
74 static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
87 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
88 uint64_t Pair[2]) {
7589 Pair[0] = 0;
7690 for (int i=0; i<16; i++, Buffer++) {
7791 assert(Buffer != End);
96110 Pair[1] += C-'a'+10;
97111 }
98112 if (Buffer != End)
99 GenerateError("constant bigger than 128 bits detected!");
113 Error("constant bigger than 128 bits detected!");
100114 }
101115
102116 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
148162 // Lexer definition.
149163 //===----------------------------------------------------------------------===//
150164
151 // FIXME: REMOVE THIS.
152 #define YYEOF 0
153 #define YYERROR -2
154
155 LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
165 LLLexer::LLLexer(MemoryBuffer *StartBuf, ParseError &Err)
166 : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
156167 CurPtr = CurBuf->getBufferStart();
157168 }
158169
173184 // Otherwise, return end of file.
174185 --CurPtr; // Another call to lex will return EOF again.
175186 return EOF;
176 case '\n':
177 case '\r':
178 // Handle the newline character by ignoring it and incrementing the line
179 // count. However, be careful about 'dos style' files with \n\r in them.
180 // Only treat a \n\r or \r\n as a single line.
181 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
182 *CurPtr != CurChar)
183 ++CurPtr; // Eat the two char newline sequence.
184
185 ++CurLineNo;
186 return '\n';
187 }
188 }
189
190
191 int LLLexer::LexToken() {
187 }
188 }
189
190
191 lltok::Kind LLLexer::LexToken() {
192192 TokStart = CurPtr;
193193
194194 int CurChar = getNextChar();
195
196195 switch (CurChar) {
197196 default:
198197 // Handle letters: [a-zA-Z_]
199198 if (isalpha(CurChar) || CurChar == '_')
200199 return LexIdentifier();
201200
202 return CurChar;
203 case EOF: return YYEOF;
201 return lltok::Error;
202 case EOF: return lltok::Eof;
204203 case 0:
205204 case ' ':
206205 case '\t':
215214 case '.':
216215 if (const char *Ptr = isLabelTail(CurPtr)) {
217216 CurPtr = Ptr;
218 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
219 return LABELSTR;
217 StrVal.assign(TokStart, CurPtr-1);
218 return lltok::LabelStr;
220219 }
221220 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
222221 CurPtr += 2;
223 return DOTDOTDOT;
224 }
225 return '.';
222 return lltok::dotdotdot;
223 }
224 return lltok::Error;
226225 case '$':
227226 if (const char *Ptr = isLabelTail(CurPtr)) {
228227 CurPtr = Ptr;
229 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
230 return LABELSTR;
231 }
232 return '$';
228 StrVal.assign(TokStart, CurPtr-1);
229 return lltok::LabelStr;
230 }
231 return lltok::Error;
233232 case ';':
234233 SkipLineComment();
235234 return LexToken();
237236 case '5': case '6': case '7': case '8': case '9':
238237 case '-':
239238 return LexDigitOrNegative();
239 case '=': return lltok::equal;
240 case '[': return lltok::lsquare;
241 case ']': return lltok::rsquare;
242 case '{': return lltok::lbrace;
243 case '}': return lltok::rbrace;
244 case '<': return lltok::less;
245 case '>': return lltok::greater;
246 case '(': return lltok::lparen;
247 case ')': return lltok::rparen;
248 case ',': return lltok::comma;
249 case '*': return lltok::star;
250 case '\\': return lltok::backslash;
240251 }
241252 }
242253
248259 }
249260
250261 /// LexAt - Lex all tokens that start with an @ character:
251 /// AtStringConstant @\"[^\"]*\"
252 /// GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
253 /// GlobalVarID @[0-9]+
254 int LLLexer::LexAt() {
262 /// GlobalVar @\"[^\"]*\"
263 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
264 /// GlobalVarID @[0-9]+
265 lltok::Kind LLLexer::LexAt() {
255266 // Handle AtStringConstant: @\"[^\"]*\"
256267 if (CurPtr[0] == '"') {
257268 ++CurPtr;
260271 int CurChar = getNextChar();
261272
262273 if (CurChar == EOF) {
263 GenerateError("End of file in global variable name");
264 return YYERROR;
274 Error("end of file in global variable name");
275 return lltok::Error;
265276 }
266277 if (CurChar == '"') {
267 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
268 UnEscapeLexed(*llvmAsmlval.StrVal);
269 return ATSTRINGCONSTANT;
278 StrVal.assign(TokStart+2, CurPtr-1);
279 UnEscapeLexed(StrVal);
280 return lltok::GlobalVar;
270281 }
271282 }
272283 }
279290 CurPtr[0] == '.' || CurPtr[0] == '_')
280291 ++CurPtr;
281292
282 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip @
283 return GLOBALVAR;
293 StrVal.assign(TokStart+1, CurPtr); // Skip @
294 return lltok::GlobalVar;
284295 }
285296
286297 // Handle GlobalVarID: @[0-9]+
290301
291302 uint64_t Val = atoull(TokStart+1, CurPtr);
292303 if ((unsigned)Val != Val)
293 GenerateError("Invalid value number (too large)!");
294 llvmAsmlval.UIntVal = unsigned(Val);
295 return GLOBALVAL_ID;
296 }
297
298 return '@';
304 Error("invalid value number (too large)!");
305 UIntVal = unsigned(Val);
306 return lltok::GlobalID;
307 }
308
309 return lltok::Error;
299310 }
300311
301312
302313 /// LexPercent - Lex all tokens that start with a % character:
303 /// PctStringConstant %\"[^\"]*\"
304 /// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
305 /// LocalVarID %[0-9]+
306 int LLLexer::LexPercent() {
307 // Handle PctStringConstant: %\"[^\"]*\"
314 /// LocalVar ::= %\"[^\"]*\"
315 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
316 /// LocalVarID ::= %[0-9]+
317 lltok::Kind LLLexer::LexPercent() {
318 // Handle LocalVarName: %\"[^\"]*\"
308319 if (CurPtr[0] == '"') {
309320 ++CurPtr;
310321
312323 int CurChar = getNextChar();
313324
314325 if (CurChar == EOF) {
315 GenerateError("End of file in local variable name");
316 return YYERROR;
326 Error("end of file in string constant");
327 return lltok::Error;
317328 }
318329 if (CurChar == '"') {
319 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
320 UnEscapeLexed(*llvmAsmlval.StrVal);
321 return PCTSTRINGCONSTANT;
330 StrVal.assign(TokStart+2, CurPtr-1);
331 UnEscapeLexed(StrVal);
332 return lltok::LocalVar;
322333 }
323334 }
324335 }
331342 CurPtr[0] == '.' || CurPtr[0] == '_')
332343 ++CurPtr;
333344
334 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
335 return LOCALVAR;
345 StrVal.assign(TokStart+1, CurPtr); // Skip %
346 return lltok::LocalVar;
336347 }
337348
338349 // Handle LocalVarID: %[0-9]+
342353
343354 uint64_t Val = atoull(TokStart+1, CurPtr);
344355 if ((unsigned)Val != Val)
345 GenerateError("Invalid value number (too large)!");
346 llvmAsmlval.UIntVal = unsigned(Val);
347 return LOCALVAL_ID;
348 }
349
350 return '%';
356 Error("invalid value number (too large)!");
357 UIntVal = unsigned(Val);
358 return lltok::LocalVarID;
359 }
360
361 return lltok::Error;
351362 }
352363
353364 /// LexQuote - Lex all tokens that start with a " character:
354365 /// QuoteLabel "[^"]+":
355366 /// StringConstant "[^"]*"
356 int LLLexer::LexQuote() {
367 lltok::Kind LLLexer::LexQuote() {
357368 while (1) {
358369 int CurChar = getNextChar();
359370
360371 if (CurChar == EOF) {
361 GenerateError("End of file in quoted string");
362 return YYERROR;
372 Error("end of file in quoted string");
373 return lltok::Error;
363374 }
364375
365376 if (CurChar != '"') continue;
366377
367378 if (CurPtr[0] != ':') {
368 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
369 UnEscapeLexed(*llvmAsmlval.StrVal);
370 return STRINGCONSTANT;
379 StrVal.assign(TokStart+1, CurPtr-1);
380 UnEscapeLexed(StrVal);
381 return lltok::StringConstant;
371382 }
372383
373384 ++CurPtr;
374 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
375 UnEscapeLexed(*llvmAsmlval.StrVal);
376 return LABELSTR;
385 StrVal.assign(TokStart+1, CurPtr-2);
386 UnEscapeLexed(StrVal);
387 return lltok::LabelStr;
377388 }
378389 }
379390
394405 /// IntegerType i[0-9]+
395406 /// Keyword sdiv, float, ...
396407 /// HexIntConstant [us]0x[0-9A-Fa-f]+
397 int LLLexer::LexIdentifier() {
408 lltok::Kind LLLexer::LexIdentifier() {
398409 const char *StartChar = CurPtr;
399410 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
400411 const char *KeywordEnd = 0;
407418
408419 // If we stopped due to a colon, this really is a label.
409420 if (*CurPtr == ':') {
410 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
411 return LABELSTR;
421 StrVal.assign(StartChar-1, CurPtr++);
422 return lltok::LabelStr;
412423 }
413424
414425 // Otherwise, this wasn't a label. If this was valid as an integer type,
419430 uint64_t NumBits = atoull(StartChar, CurPtr);
420431 if (NumBits < IntegerType::MIN_INT_BITS ||
421432 NumBits > IntegerType::MAX_INT_BITS) {
422 GenerateError("Bitwidth for integer type out of range!");
423 return YYERROR;
424 }
425 const Type* Ty = IntegerType::get(NumBits);
426 llvmAsmlval.PrimType = Ty;
427 return INTTYPE;
433 Error("bitwidth for integer type out of range!");
434 return lltok::Error;
435 }
436 TyVal = IntegerType::get(NumBits);
437 return lltok::Type;
428438 }
429439
430440 // Otherwise, this was a letter sequence. See which keyword this is.
432442 CurPtr = KeywordEnd;
433443 --StartChar;
434444 unsigned Len = CurPtr-StartChar;
435 #define KEYWORD(STR, TOK) \
436 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
437
438 KEYWORD("begin", BEGINTOK);
439 KEYWORD("end", ENDTOK);
440 KEYWORD("true", TRUETOK);
441 KEYWORD("false", FALSETOK);
442 KEYWORD("declare", DECLARE);
443 KEYWORD("define", DEFINE);
444 KEYWORD("global", GLOBAL);
445 KEYWORD("constant", CONSTANT);
446
447 KEYWORD("internal", INTERNAL);
448 KEYWORD("linkonce", LINKONCE);
449 KEYWORD("weak", WEAK);
450 KEYWORD("appending", APPENDING);
451 KEYWORD("dllimport", DLLIMPORT);
452 KEYWORD("dllexport", DLLEXPORT);
453 KEYWORD("common", COMMON);
454 KEYWORD("default", DEFAULT);
455 KEYWORD("hidden", HIDDEN);
456 KEYWORD("protected", PROTECTED);
457 KEYWORD("extern_weak", EXTERN_WEAK);
458 KEYWORD("external", EXTERNAL);
459 KEYWORD("thread_local", THREAD_LOCAL);
460 KEYWORD("zeroinitializer", ZEROINITIALIZER);
461 KEYWORD("undef", UNDEF);
462 KEYWORD("null", NULL_TOK);
463 KEYWORD("to", TO);
464 KEYWORD("tail", TAIL);
465 KEYWORD("target", TARGET);
466 KEYWORD("triple", TRIPLE);
467 KEYWORD("deplibs", DEPLIBS);
468 KEYWORD("datalayout", DATALAYOUT);
469 KEYWORD("volatile", VOLATILE);
470 KEYWORD("align", ALIGN);
471 KEYWORD("addrspace", ADDRSPACE);
472 KEYWORD("section", SECTION);
473 KEYWORD("alias", ALIAS);
474 KEYWORD("module", MODULE);
475 KEYWORD("asm", ASM_TOK);
476 KEYWORD("sideeffect", SIDEEFFECT);
477 KEYWORD("gc", GC);
478
479 KEYWORD("cc", CC_TOK);
480 KEYWORD("ccc", CCC_TOK);
481 KEYWORD("fastcc", FASTCC_TOK);
482 KEYWORD("coldcc", COLDCC_TOK);
483 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
484 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
485
486 KEYWORD("signext", SIGNEXT);
487 KEYWORD("zeroext", ZEROEXT);
488 KEYWORD("inreg", INREG);
489 KEYWORD("sret", SRET);
490 KEYWORD("nounwind", NOUNWIND);
491 KEYWORD("noreturn", NORETURN);
492 KEYWORD("noalias", NOALIAS);
493 KEYWORD("nocapture", NOCAPTURE);
494 KEYWORD("byval", BYVAL);
495 KEYWORD("nest", NEST);
496 KEYWORD("readnone", READNONE);
497 KEYWORD("readonly", READONLY);
498
499 KEYWORD("noinline", NOINLINE);
500 KEYWORD("alwaysinline", ALWAYSINLINE);
501 KEYWORD("optsize", OPTSIZE);
502 KEYWORD("ssp", SSP);
503 KEYWORD("sspreq", SSPREQ);
504
505 KEYWORD("type", TYPE);
506 KEYWORD("opaque", OPAQUE);
507
508 KEYWORD("eq" , EQ);
509 KEYWORD("ne" , NE);
510 KEYWORD("slt", SLT);
511 KEYWORD("sgt", SGT);
512 KEYWORD("sle", SLE);
513 KEYWORD("sge", SGE);
514 KEYWORD("ult", ULT);
515 KEYWORD("ugt", UGT);
516 KEYWORD("ule", ULE);
517 KEYWORD("uge", UGE);
518 KEYWORD("oeq", OEQ);
519 KEYWORD("one", ONE);
520 KEYWORD("olt", OLT);
521 KEYWORD("ogt", OGT);
522 KEYWORD("ole", OLE);
523 KEYWORD("oge", OGE);
524 KEYWORD("ord", ORD);
525 KEYWORD("uno", UNO);
526 KEYWORD("ueq", UEQ);
527 KEYWORD("une", UNE);
445 #define KEYWORD(STR) \
446 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
447 return lltok::kw_##STR;
448
449 KEYWORD(begin); KEYWORD(end);
450 KEYWORD(true); KEYWORD(false);
451 KEYWORD(declare); KEYWORD(define);
452 KEYWORD(global); KEYWORD(constant);
453
454 KEYWORD(internal);
455 KEYWORD(linkonce);
456 KEYWORD(weak);
457 KEYWORD(appending);
458 KEYWORD(dllimport);
459 KEYWORD(dllexport);
460 KEYWORD(common);
461 KEYWORD(default);
462 KEYWORD(hidden);
463 KEYWORD(protected);
464 KEYWORD(extern_weak);
465 KEYWORD(external);
466 KEYWORD(thread_local);
467 KEYWORD(zeroinitializer);
468 KEYWORD(undef);
469 KEYWORD(null);
470 KEYWORD(to);
471 KEYWORD(tail);
472 KEYWORD(target);
473 KEYWORD(triple);
474 KEYWORD(deplibs);
475 KEYWORD(datalayout);
476 KEYWORD(volatile);
477 KEYWORD(align);
478 KEYWORD(addrspace);
479 KEYWORD(section);
480 KEYWORD(alias);
481 KEYWORD(module);
482 KEYWORD(asm);
483 KEYWORD(sideeffect);
484 KEYWORD(gc);
485
486 KEYWORD(ccc);
487 KEYWORD(fastcc);
488 KEYWORD(coldcc);
489 KEYWORD(x86_stdcallcc);
490 KEYWORD(x86_fastcallcc);
491 KEYWORD(cc);
492 KEYWORD(c);
493
494 KEYWORD(signext);
495 KEYWORD(zeroext);
496 KEYWORD(inreg);
497 KEYWORD(sret);
498 KEYWORD(nounwind);
499 KEYWORD(noreturn);
500 KEYWORD(noalias);
501 KEYWORD(nocapture);
502 KEYWORD(byval);
503 KEYWORD(nest);
504 KEYWORD(readnone);
505 KEYWORD(readonly);
506
507 KEYWORD(noinline);
508 KEYWORD(alwaysinline);
509 KEYWORD(optsize);
510 KEYWORD(ssp);
511 KEYWORD(sspreq);
512
513 KEYWORD(type);
514 KEYWORD(opaque);
515
516 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
517 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
518 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
519 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
520
521 KEYWORD(x);
528522 #undef KEYWORD
529523
530524 // Keywords for types.
531 #define TYPEKEYWORD(STR, LLVMTY, TOK) \
525 #define TYPEKEYWORD(STR, LLVMTY) \
532526 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
533 llvmAsmlval.PrimType = LLVMTY; return TOK; }
534 TYPEKEYWORD("void", Type::VoidTy, VOID);
535 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
536 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
537 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
538 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
539 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
540 TYPEKEYWORD("label", Type::LabelTy, LABEL);
527 TyVal = LLVMTY; return lltok::Type; }
528 TYPEKEYWORD("void", Type::VoidTy);
529 TYPEKEYWORD("float", Type::FloatTy);
530 TYPEKEYWORD("double", Type::DoubleTy);
531 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
532 TYPEKEYWORD("fp128", Type::FP128Ty);
533 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
534 TYPEKEYWORD("label", Type::LabelTy);
541535 #undef TYPEKEYWORD
542536
543537 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
545539 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
546540 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
547541 if (JustWhitespaceNewLine(CurPtr))
548 return SIGNEXT;
542 return lltok::kw_signext;
549543 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
550544 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
551545 if (JustWhitespaceNewLine(CurPtr))
552 return ZEROEXT;
546 return lltok::kw_zeroext;
553547 }
554548
555549 // Keywords for instructions.
556 #define INSTKEYWORD(STR, type, Enum, TOK) \
557 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
558 llvmAsmlval.type = Instruction::Enum; return TOK; }
559
560 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
561 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
562 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
563 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
564 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
565 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
566 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
567 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
568 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
569 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
570 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
571 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
572 INSTKEYWORD("and", BinaryOpVal, And, AND);
573 INSTKEYWORD("or", BinaryOpVal, Or , OR );
574 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
575 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
576 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
577 INSTKEYWORD("vicmp", OtherOpVal, VICmp, VICMP);
578 INSTKEYWORD("vfcmp", OtherOpVal, VFCmp, VFCMP);
579
580 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
581 INSTKEYWORD("call", OtherOpVal, Call, CALL);
582 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
583 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
584 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
585 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
586 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
587 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
588 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
589 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
590 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
591 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
592 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
593 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
594 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
595 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
596 INSTKEYWORD("ret", TermOpVal, Ret, RET);
597 INSTKEYWORD("br", TermOpVal, Br, BR);
598 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
599 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
600 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
601 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
602
603 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
604 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
605 INSTKEYWORD("free", MemOpVal, Free, FREE);
606 INSTKEYWORD("load", MemOpVal, Load, LOAD);
607 INSTKEYWORD("store", MemOpVal, Store, STORE);
608 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
609
610 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
611 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
612 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
613 INSTKEYWORD("getresult", OtherOpVal, ExtractValue, GETRESULT);
614 INSTKEYWORD("extractvalue", OtherOpVal, ExtractValue, EXTRACTVALUE);
615 INSTKEYWORD("insertvalue", OtherOpVal, InsertValue, INSERTVALUE);
550 #define INSTKEYWORD(STR, Enum) \
551 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
552 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
553
554 INSTKEYWORD(add, Add); INSTKEYWORD(sub, Sub); INSTKEYWORD(mul, Mul);
555 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
556 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
557 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
558 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
559 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
560 INSTKEYWORD(vicmp, VICmp); INSTKEYWORD(vfcmp, VFCmp);
561
562 INSTKEYWORD(phi, PHI);
563 INSTKEYWORD(call, Call);
564 INSTKEYWORD(trunc, Trunc);
565 INSTKEYWORD(zext, ZExt);
566 INSTKEYWORD(sext, SExt);
567 INSTKEYWORD(fptrunc, FPTrunc);
568 INSTKEYWORD(fpext, FPExt);
569 INSTKEYWORD(uitofp, UIToFP);
570 INSTKEYWORD(sitofp, SIToFP);
571 INSTKEYWORD(fptoui, FPToUI);
572 INSTKEYWORD(fptosi, FPToSI);
573 INSTKEYWORD(inttoptr, IntToPtr);
574 INSTKEYWORD(ptrtoint, PtrToInt);
575 INSTKEYWORD(bitcast, BitCast);
576 INSTKEYWORD(select, Select);
577 INSTKEYWORD(va_arg, VAArg);
578 INSTKEYWORD(ret, Ret);
579 INSTKEYWORD(br, Br);
580 INSTKEYWORD(switch, Switch);
581 INSTKEYWORD(invoke, Invoke);
582 INSTKEYWORD(unwind, Unwind);
583 INSTKEYWORD(unreachable, Unreachable);
584
585 INSTKEYWORD(malloc, Malloc);
586 INSTKEYWORD(alloca, Alloca);
587 INSTKEYWORD(free, Free);
588 INSTKEYWORD(load, Load);
589 INSTKEYWORD(store, Store);
590 INSTKEYWORD(getelementptr, GetElementPtr);
591
592 INSTKEYWORD(extractelement, ExtractElement);
593 INSTKEYWORD(insertelement, InsertElement);
594 INSTKEYWORD(shufflevector, ShuffleVector);
595 INSTKEYWORD(getresult, ExtractValue);
596 INSTKEYWORD(extractvalue, ExtractValue);
597 INSTKEYWORD(insertvalue, InsertValue);
616598 #undef INSTKEYWORD
617599
618600 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
625607 uint32_t activeBits = Tmp.getActiveBits();
626608 if (activeBits > 0 && activeBits < bits)
627609 Tmp.trunc(activeBits);
628 if (Tmp.getBitWidth() > 64) {
629 llvmAsmlval.APIntVal = new APInt(Tmp);
630 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
631 } else if (TokStart[0] == 's') {
632 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
633 return ESINT64VAL;
634 } else {
635 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
636 return EUINT64VAL;
637 }
610 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
611 return lltok::APSInt;
638612 }
639613
640614 // If this is "cc1234", return this as just "cc".
641615 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
642616 CurPtr = TokStart+2;
643 return CC_TOK;
617 return lltok::kw_cc;
644618 }
645619
646620 // If this starts with "call", return it as CALL. This is to support old
647621 // broken .ll files. FIXME: remove this with LLVM 3.0.
648622 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
649623 CurPtr = TokStart+4;
650 llvmAsmlval.OtherOpVal = Instruction::Call;
651 return CALL;
652 }
653
654 // Finally, if this isn't known, return just a single character.
624 UIntVal = Instruction::Call;
625 return lltok::kw_call;
626 }
627
628 // Finally, if this isn't known, return an error.
655629 CurPtr = TokStart+1;
656 return TokStart[0];
630 return lltok::Error;
657631 }
658632
659633
663637 /// HexFP80Constant 0xK[0-9A-Fa-f]+
664638 /// HexFP128Constant 0xL[0-9A-Fa-f]+
665639 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
666 int LLLexer::Lex0x() {
640 lltok::Kind LLLexer::Lex0x() {
667641 CurPtr = TokStart + 2;
668642
669643 char Kind;
674648 }
675649
676650 if (!isxdigit(CurPtr[0])) {
677 // Bad token, return it as just zero.
651 // Bad token, return it as an error.
678652 CurPtr = TokStart+1;
679 return '0';
653 return lltok::Error;
680654 }
681655
682656 while (isxdigit(CurPtr[0]))
686660 // HexFPConstant - Floating point constant represented in IEEE format as a
687661 // hexadecimal number for when exponential notation is not precise enough.
688662 // Float and double only.
689 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
690 return FPVAL;
663 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
664 return lltok::APFloat;
691665 }
692666
693667 uint64_t Pair[2];
696670 default: assert(0 && "Unknown kind!");
697671 case 'K':
698672 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
699 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
700 return FPVAL;
673 APFloatVal = APFloat(APInt(80, 2, Pair));
674 return lltok::APFloat;
701675 case 'L':
702676 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
703 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
704 return FPVAL;
677 APFloatVal = APFloat(APInt(128, 2, Pair), true);
678 return lltok::APFloat;
705679 case 'M':
706680 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
707 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
708 return FPVAL;
681 APFloatVal = APFloat(APInt(128, 2, Pair));
682 return lltok::APFloat;
709683 }
710684 }
711685
718692 /// HexFP80Constant 0xK[0-9A-Fa-f]+
719693 /// HexFP128Constant 0xL[0-9A-Fa-f]+
720694 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
721 int LLLexer::LexDigitOrNegative() {
695 lltok::Kind LLLexer::LexDigitOrNegative() {
722696 // If the letter after the negative is a number, this is probably a label.
723697 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
724698 // Okay, this is not a number after the -, it's probably a label.
725699 if (const char *End = isLabelTail(CurPtr)) {
726 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
700 StrVal.assign(TokStart, End-1);
727701 CurPtr = End;
728 return LABELSTR;
729 }
730
731 return CurPtr[-1];
702 return lltok::LabelStr;
703 }
704
705 return lltok::Error;
732706 }
733707
734708 // At this point, it is either a label, int or fp constant.
740714 // Check to see if this really is a label afterall, e.g. "-1:".
741715 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
742716 if (const char *End = isLabelTail(CurPtr)) {
743 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
717 StrVal.assign(TokStart, End-1);
744718 CurPtr = End;
745 return LABELSTR;
719 return lltok::LabelStr;
746720 }
747721 }
748722
758732 uint32_t minBits = Tmp.getMinSignedBits();
759733 if (minBits > 0 && minBits < numBits)
760734 Tmp.trunc(minBits);
761 if (Tmp.getBitWidth() > 64) {
762 llvmAsmlval.APIntVal = new APInt(Tmp);
763 return ESAPINTVAL;
764 } else {
765 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
766 return ESINT64VAL;
767 }
735 APSIntVal = APSInt(Tmp, false);
768736 } else {
769737 uint32_t activeBits = Tmp.getActiveBits();
770738 if (activeBits > 0 && activeBits < numBits)
771739 Tmp.trunc(activeBits);
772 if (Tmp.getBitWidth() > 64) {
773 llvmAsmlval.APIntVal = new APInt(Tmp);
774 return EUAPINTVAL;
775 } else {
776 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
777 return EUINT64VAL;
778 }
779 }
740 APSIntVal = APSInt(Tmp, true);
741 }
742 return lltok::APSInt;
780743 }
781744
782745 ++CurPtr;
792755 }
793756 }
794757
795 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
796 return FPVAL;
758 APFloatVal = APFloat(atof(TokStart));
759 return lltok::APFloat;
797760 }
798761
799762 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
800 int LLLexer::LexPositive() {
763 lltok::Kind LLLexer::LexPositive() {
801764 // If the letter after the negative is a number, this is probably not a
802765 // label.
803766 if (!isdigit(CurPtr[0]))
804 return CurPtr[-1];
767 return lltok::Error;
805768
806769 // Skip digits.
807770 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
810773 // At this point, we need a '.'.
811774 if (CurPtr[0] != '.') {
812775 CurPtr = TokStart+1;
813 return TokStart[0];
776 return lltok::Error;
814777 }
815778
816779 ++CurPtr;
826789 }
827790 }
828791
829 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
830 return FPVAL;
831 }
832
833
834 //===----------------------------------------------------------------------===//
835 // Define the interface to this file.
836 //===----------------------------------------------------------------------===//
837
838 static LLLexer *TheLexer;
839
840 void InitLLLexer(llvm::MemoryBuffer *MB) {
841 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
842 TheLexer = new LLLexer(MB);
843 }
844
845 int llvmAsmlex() {
846 return TheLexer->LexToken();
847 }
848 const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
849 unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
850 std::string LLLgetFilename() { return TheLexer->getFilename(); }
851 unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }
852
853 void FreeLexer() {
854 delete TheLexer;
855 TheLexer = 0;
856 }
792 APFloatVal = APFloat(atof(TokStart));
793 return lltok::APFloat;
794 }
1313 #ifndef LIB_ASMPARSER_LLLEXER_H
1414 #define LIB_ASMPARSER_LLLEXER_H
1515
16 #include "LLToken.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/APFloat.h"
19
1620 #include
1721 #include
1822 #include
1923
2024 namespace llvm {
2125 class MemoryBuffer;
26 class Type;
27 class ParseError;
2228
2329 class LLLexer {
2430 const char *CurPtr;
25 unsigned CurLineNo;
2631 MemoryBuffer *CurBuf;
27
32 ParseError &ErrorInfo;
33
34 // Information about the current token.
2835 const char *TokStart;
36 lltok::Kind CurKind;
37 std::string StrVal;
38 unsigned UIntVal;
39 const Type *TyVal;
40 APFloat APFloatVal;
41 APSInt APSIntVal;
2942
3043 std::string TheError;
3144 public:
32 explicit LLLexer(MemoryBuffer *StartBuf);
45 explicit LLLexer(MemoryBuffer *StartBuf, ParseError &);
3346 ~LLLexer() {}
3447
35 const char *getTokStart() const { return TokStart; }
36 unsigned getTokLength() const { return CurPtr-TokStart; }
37 unsigned getLineNo() const { return CurLineNo; }
48 lltok::Kind Lex() {
49 return CurKind = LexToken();
50 }
51
52 typedef const char* LocTy;
53 LocTy getLoc() const { return TokStart; }
54 lltok::Kind getKind() const { return CurKind; }
55 const std::string getStrVal() const { return StrVal; }
56 const Type *getTyVal() const { return TyVal; }
57 unsigned getUIntVal() const { return UIntVal; }
58 const APSInt &getAPSIntVal() const { return APSIntVal; }
59 const APFloat &getAPFloatVal() const { return APFloatVal; }
60
61
62 bool Error(LocTy L, const std::string &Msg) const;
63 bool Error(const std::string &Msg) const { return Error(CurPtr, Msg); }
3864 std::string getFilename() const;
39 int LexToken();
40
41 const std::string getError() const { return TheError; }
42
65
4366 private:
67 lltok::Kind LexToken();
68
4469 int getNextChar();
4570 void SkipLineComment();
46 int LexIdentifier();
47 int LexDigitOrNegative();
48 int LexPositive();
49 int LexAt();
50 int LexPercent();
51 int LexQuote();
52 int Lex0x();
71 lltok::Kind LexIdentifier();
72 lltok::Kind LexDigitOrNegative();
73 lltok::Kind LexPositive();
74 lltok::Kind LexAt();
75 lltok::Kind LexPercent();
76 lltok::Kind LexQuote();
77 lltok::Kind Lex0x();
78
79 uint64_t atoull(const char *Buffer, const char *End);
80 uint64_t HexIntToVal(const char *Buffer, const char *End);
81 void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
5382 };
5483 } // end namespace llvm
5584
0 //===-- LLParser.cpp - Parser Class ---------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the parser class for .ll files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "LLParser.h"
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/CallingConv.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/ValueSymbolTable.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26
27 // ValID - Represents a reference of a definition of some sort with no type.
28 // There are several cases where we have to parse the value but where the type
29 // can depend on later context. This may either
30 // be a numeric reference or a symbolic (%var) reference. This is just a
31 // discriminated union.
32 //
33 // Note that I can't implement this class in a straight forward manner with
34 // constructors and stuff because it goes in a union.
35 //
36 namespace llvm {
37 struct ValID {
38 enum {
39 t_LocalID, t_GlobalID, // ID in UIntVal.
40 t_LocalName, t_GlobalName, // Name in StrVal.
41 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
42 t_Null, t_Undef, t_Zero, // No value.
43 t_Constant, // Value in ConstantVal.
44 t_InlineAsm // Value in StrVal/StrVal2/UIntVal.
45 } Kind;
46
47 LLParser::LocTy Loc;
48 unsigned UIntVal;
49 std::string StrVal, StrVal2;
50 APSInt APSIntVal;
51 APFloat APFloatVal;
52 Constant *ConstantVal;
53 ValID() : APFloatVal(0.0) {}
54 };
55 }
56
57 /// Parse: module ::= toplevelentity*
58 Module *LLParser::Run() {
59 M = new Module(Lex.getFilename());
60
61 if (ParseTopLevelEntities() ||
62 ValidateEndOfModule()) {
63 delete M;
64 return 0;
65 }
66
67 return M;
68 }
69
70 /// ValidateEndOfModule - Do final validity and sanity checks at the end of the
71 /// module.
72 bool LLParser::ValidateEndOfModule() {
73 if (!ForwardRefTypes.empty())
74 return Error(ForwardRefTypes.begin()->second.second,
75 "use of undefined type named '" +
76 ForwardRefTypes.begin()->first + "'");
77 if (!ForwardRefTypeIDs.empty())
78 return Error(ForwardRefTypeIDs.begin()->second.second,
79 "use of undefined type '%" +
80 utostr(ForwardRefTypeIDs.begin()->first) + "'");
81
82 if (!ForwardRefVals.empty())
83 return Error(ForwardRefVals.begin()->second.second,
84 "use of undefined value '@" + ForwardRefVals.begin()->first +
85 "'");
86
87 if (!ForwardRefValIDs.empty())
88 return Error(ForwardRefValIDs.begin()->second.second,
89 "use of undefined value '@" +
90 utostr(ForwardRefValIDs.begin()->first) + "'");
91
92 // Look for intrinsic functions and CallInst that need to be upgraded
93 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
94 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
95
96 return false;
97 }
98
99 //===----------------------------------------------------------------------===//
100 // Top-Level Entities
101 //===----------------------------------------------------------------------===//
102
103 bool LLParser::ParseTopLevelEntities() {
104 Lex.Lex();
105 while (1) {
106 switch (Lex.getKind()) {
107 default: return TokError("expected top-level entity");
108 case lltok::Eof: return false;
109 //case lltok::kw_define:
110 case lltok::kw_declare: if (ParseDeclare()) return true; break;
111 case lltok::kw_define: if (ParseDefine()) return true; break;
112 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
113 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
114 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
115 case lltok::kw_type: if (ParseUnnamedType()) return true; break;
116 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
117 case lltok::LocalVar: if (ParseNamedType()) return true; break;
118 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
119
120 // The Global variable production with no name can have many different
121 // optional leading prefixes, the production is:
122 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
123 // OptionalAddrSpace ('constant'|'global') ...
124 case lltok::kw_internal: // OptionalLinkage
125 case lltok::kw_weak: // OptionalLinkage
126 case lltok::kw_linkonce: // OptionalLinkage
127 case lltok::kw_appending: // OptionalLinkage
128 case lltok::kw_dllexport: // OptionalLinkage
129 case lltok::kw_common: // OptionalLinkage
130 case lltok::kw_dllimport: // OptionalLinkage
131 case lltok::kw_extern_weak: // OptionalLinkage
132 case lltok::kw_external: { // OptionalLinkage
133 unsigned Linkage, Visibility;
134 if (ParseOptionalLinkage(Linkage) ||
135 ParseOptionalVisibility(Visibility) ||
136 ParseGlobal("", 0, Linkage, true, Visibility))
137 return true;
138 break;
139 }
140 case lltok::kw_default: // OptionalVisibility
141 case lltok::kw_hidden: // OptionalVisibility
142 case lltok::kw_protected: { // OptionalVisibility
143 unsigned Visibility;
144 if (ParseOptionalVisibility(Visibility) ||
145 ParseGlobal("", 0, 0, false, Visibility))
146 return true;
147 break;
148 }
149
150 case lltok::kw_thread_local: // OptionalThreadLocal
151 case lltok::kw_addrspace: // OptionalAddrSpace
152 case lltok::kw_constant: // GlobalType
153 case lltok::kw_global: // GlobalType
154 if (ParseGlobal("", 0, 0, false, 0)) return true;
155 break;
156 }
157 }
158 }
159
160
161 /// toplevelentity
162 /// ::= 'module' 'asm' STRINGCONSTANT
163 bool LLParser::ParseModuleAsm() {
164 assert(Lex.getKind() == lltok::kw_module);
165 Lex.Lex();
166
167 if (ParseToken(lltok::kw_asm, "expected 'module asm'")) return true;
168
169 if (Lex.getKind() != lltok::StringConstant)
170 return TokError("expected 'module asm \"foo\"'");
171
172 const std::string &AsmSoFar = M->getModuleInlineAsm();
173 if (AsmSoFar.empty())
174 M->setModuleInlineAsm(Lex.getStrVal());
175 else
176 M->setModuleInlineAsm(AsmSoFar+"\n"+Lex.getStrVal());
177 Lex.Lex();
178 return false;
179 }
180
181 /// toplevelentity
182 /// ::= 'target' 'triple' '=' STRINGCONSTANT
183 /// ::= 'target' 'datalayout' '=' STRINGCONSTANT
184 bool LLParser::ParseTargetDefinition() {
185 assert(Lex.getKind() == lltok::kw_target);
186 switch (Lex.Lex()) {
187 default: return TokError("unknown target property");
188 case lltok::kw_triple:
189 Lex.Lex();
190 if (ParseToken(lltok::equal, "expected '=' after target triple"))
191 return true;
192 if (Lex.getKind() != lltok::StringConstant)
193 return TokError("expected string after target triple '='");
194 M->setTargetTriple(Lex.getStrVal());
195 Lex.Lex();
196 return false;
197 case lltok::kw_datalayout:
198 Lex.Lex();
199 if (ParseToken(lltok::equal, "expected '=' after target datalayout"))
200 return true;
201 if (Lex.getKind() != lltok::StringConstant)
202 return TokError("expected string after target datalayout '='");
203 M->setDataLayout(Lex.getStrVal());
204 Lex.Lex();
205 return false;
206 }
207 }
208
209 /// toplevelentity
210 /// ::= 'deplibs' '=' '[' ']'
211 /// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
212 bool LLParser::ParseDepLibs() {
213 assert(Lex.getKind() == lltok::kw_deplibs);
214 if (Lex.Lex() != lltok::equal)
215 return TokError("expected '=' after deplibs");
216
217 if (Lex.Lex() != lltok::lsquare)
218 return TokError("expected '=' after deplibs");
219
220 if (Lex.Lex() == lltok::rsquare) {
221 Lex.Lex();
222 return false;
223 }
224
225 if (Lex.getKind() != lltok::StringConstant)
226 return TokError("expected string in deplib list");
227
228 M->addLibrary(Lex.getStrVal());
229
230 while (Lex.Lex() == lltok::comma) {
231 if (Lex.Lex() != lltok::StringConstant)
232 return TokError("expected string in deplibs list");
233 M->addLibrary(Lex.getStrVal());
234 }
235
236 if (Lex.getKind() != lltok::rsquare)
237 return TokError("expected ']' at end of list");
238 Lex.Lex();
239 return false;
240 }
241
242 /// toplevelentity
243 /// ::= 'type' type
244 bool LLParser::ParseUnnamedType() {
245 assert(Lex.getKind() == lltok::kw_type);
246 LocTy TypeLoc = Lex.getLoc();
247 Lex.Lex(); // eat kw_type
248
249 PATypeHolder Ty(Type::VoidTy);
250 if (ParseType(Ty)) return true;
251
252 unsigned TypeID = NumberedTypes.size();
253
254 // We don't allow assigning names to void type
255 if (Ty == Type::VoidTy)
256 return Error(TypeLoc, "can't assign name to the void type");
257
258 // See if this type was previously referenced.
259 std::map >::iterator
260 FI = ForwardRefTypeIDs.find(TypeID);
261 if (FI != ForwardRefTypeIDs.end()) {
262 cast(FI->second.first.get())->refineAbstractTypeTo(Ty);
263 Ty = FI->second.first.get();
264 ForwardRefTypeIDs.erase(FI);
265 }
266
267 NumberedTypes.push_back(Ty);
268
269 return false;
270 }
271
272 /// toplevelentity
273 /// ::= LocalVar '=' 'type' type
274 bool LLParser::ParseNamedType() {
275 std::string Name = Lex.getStrVal();
276 LocTy NameLoc = Lex.getLoc();
277
278 if (Lex.Lex() != lltok::equal)
279 return TokError("expected '=' after name");
280 if (Lex.Lex() != lltok::kw_type)
281 return TokError("expected 'type' after name");
282 Lex.Lex(); // consume 'type'.
283
284 PATypeHolder Ty(Type::VoidTy);
285 if (ParseType(Ty)) return true;
286
287 // We don't allow assigning names to void type
288 if (Ty == Type::VoidTy)
289 return Error(NameLoc, "can't assign name '" + Name + "' to the void type");
290
291 // Set the type name, checking for conflicts as we do so.
292 bool AlreadyExists = M->addTypeName(Name, Ty);
293 if (!AlreadyExists) return false;
294
295 // See if this type is a forward reference. We need to eagerly resolve
296 // types to allow recursive type redefinitions below.
297 std::map >::iterator
298 FI = ForwardRefTypes.find(Name);
299 if (FI != ForwardRefTypes.end()) {
300 cast(FI->second.first.get())->refineAbstractTypeTo(Ty);
301 Ty = FI->second.first.get();
302 ForwardRefTypes.erase(FI);
303 }
304
305 // Inserting a name that is already defined, get the existing name.
306 const Type *Existing = M->getTypeByName(Name);
307 assert(Existing && "Conflict but no matching type?!");
308
309 // Otherwise, this is an attempt to redefine a type. That's okay if
310 // the redefinition is identical to the original.
311 // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0
312 if (Existing == Ty) return false;
313
314 // Any other kind of (non-equivalent) redefinition is an error.
315 return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" +
316 Ty->getDescription() + "'");
317 }
318
319
320 /// toplevelentity
321 /// ::= 'declare' FunctionHeader
322 bool LLParser::ParseDeclare() {
323 assert(Lex.getKind() == lltok::kw_declare);
324 Lex.Lex();
325
326 Function *F;
327 return ParseFunctionHeader(F, false);
328 }
329
330 /// toplevelentity
331 /// ::= 'define' FunctionHeader '{' ...
332 bool LLParser::ParseDefine() {
333 assert(Lex.getKind() == lltok::kw_define);
334 Lex.Lex();
335
336 Function *F;
337 if (ParseFunctionHeader(F, true)) return true;
338
339 return ParseFunctionBody(*F);
340 }
341
342 bool LLParser::ParseGlobalType(bool &IsConstant) {
343 if (Lex.getKind() == lltok::kw_constant)
344 IsConstant = true;
345 else if (Lex.getKind() == lltok::kw_global)
346 IsConstant = false;
347 else
348 return TokError("expected 'global' or 'constant'");
349 Lex.Lex();
350 return false;
351 }
352
353 /// ParseNamedGlobal:
354 /// GlobalVar '=' OptionalVisibility ALIAS ...
355 /// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable
356 bool LLParser::ParseNamedGlobal() {
357 assert(Lex.getKind() == lltok::GlobalVar);
358 LocTy NameLoc = Lex.getLoc();
359 std::string Name = Lex.getStrVal();
360 Lex.Lex();
361
362 bool HasLinkage;
363 unsigned Linkage, Visibility;
364 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
365 ParseOptionalLinkage(Linkage, HasLinkage) ||
366 ParseOptionalVisibility(Visibility))
367 return true;
368
369 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
370 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
371 return ParseAlias(Name, NameLoc, Visibility);
372 }
373
374 /// ParseAlias:
375 /// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
376 /// Aliasee
377 /// ::= TypeAndValue | 'bitcast' '(' TypeAndValue 'to' Type ')'
378 ///
379 /// Everything through visibility has already been parsed.
380 ///
381 bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
382 unsigned Visibility) {
383 assert(Lex.getKind() == lltok::kw_alias);
384 Lex.Lex();
385 unsigned Linkage;
386 LocTy LinkageLoc = Lex.getLoc();
387 if (ParseOptionalLinkage(Linkage))
388 return true;
389
390 if (Linkage != GlobalValue::ExternalLinkage &&
391 Linkage != GlobalValue::WeakLinkage &&
392 Linkage != GlobalValue::InternalLinkage)
393 return Error(LinkageLoc, "invalid linkage type for alias");
394
395 Constant *Aliasee;
396 LocTy AliaseeLoc = Lex.getLoc();
397 if (Lex.getKind() != lltok::kw_bitcast) {
398 if (ParseGlobalTypeAndValue(Aliasee)) return true;
399 } else {
400 // The bitcast dest type is not present, it is implied by the dest type.
401 ValID ID;
402 if (ParseValID(ID)) return true;
403 if (ID.Kind != ValID::t_Constant)
404 return Error(AliaseeLoc, "invalid aliasee");
405 Aliasee = ID.ConstantVal;
406 }
407
408 if (!isa(Aliasee->getType()))
409 return Error(AliaseeLoc, "alias must have pointer type");
410
411 // Okay, create the alias but do not insert it into the module yet.
412 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
413 (GlobalValue::LinkageTypes)Linkage, Name,
414 Aliasee);
415 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
416
417 // See if this value already exists in the symbol table. If so, it is either
418 // a redefinition or a definition of a forward reference.
419 if (GlobalValue *Val =
420 cast_or_null(M->getValueSymbolTable().lookup(Name))) {
421 // See if this was a redefinition. If so, there is no entry in
422 // ForwardRefVals.
423 std::map >::iterator
424 I = ForwardRefVals.find(Name);
425 if (I == ForwardRefVals.end())
426 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
427
428 // Otherwise, this was a definition of forward ref. Verify that types
429 // agree.
430 if (Val->getType() != GA->getType())
431 return Error(NameLoc,
432 "forward reference and definition of alias have different types");
433
434 // If they agree, just RAUW the old value with the alias and remove the
435 // forward ref info.
436 Val->replaceAllUsesWith(GA);
437 Val->eraseFromParent();
438 ForwardRefVals.erase(I);
439 }
440
441 // Insert into the module, we know its name won't collide now.
442 M->getAliasList().push_back(GA);
443 assert(GA->getNameStr() == Name && "Should not be a name conflict!");
444
445 return false;
446 }
447
448 /// ParseGlobal
449 /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
450 /// OptionalAddrSpace GlobalType Type Const
451 /// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
452 /// OptionalAddrSpace GlobalType Type Const
453 ///
454 /// Everything through visibility has been parsed already.
455 ///
456 bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
457 unsigned Linkage, bool HasLinkage,
458 unsigned Visibility) {
459 unsigned AddrSpace;
460 bool ThreadLocal, IsConstant;
461 LocTy TyLoc;
462
463 PATypeHolder Ty(Type::VoidTy);
464 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
465 ParseOptionalAddrSpace(AddrSpace) ||
466 ParseGlobalType(IsConstant) ||
467 ParseType(Ty, TyLoc))
468 return true;
469
470 // If the linkage is specified and is external, then no initializer is
471 // present.
472 Constant *Init = 0;
473 if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage &&
474 Linkage != GlobalValue::ExternalWeakLinkage &&
475 Linkage != GlobalValue::ExternalLinkage)) {
476 if (ParseGlobalValue(Ty, Init))
477 return true;
478 }
479
480 if (isa(Ty) || Ty == Type::LabelTy)
481 return Error(TyLoc, "invald type for global variable");
482
483 GlobalVariable *GV = 0;
484
485 // See if the global was forward referenced, if so, use the global.
486 if (!Name.empty() && (GV = M->getGlobalVariable(Name, true))) {
487 if (!ForwardRefVals.erase(Name))
488 return Error(NameLoc, "redefinition of global '@" + Name + "'");
489 } else {
490 std::map >::iterator
491 I = ForwardRefValIDs.find(NumberedVals.size());
492 if (I != ForwardRefValIDs.end()) {
493 GV = cast(I->second.first);
494 ForwardRefValIDs.erase(I);
495 }
496 }
497
498 if (GV == 0) {
499 GV = new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage, 0, Name,
500 M, false, AddrSpace);
501 } else {
502 if (GV->getType()->getElementType() != Ty)
503 return Error(TyLoc,
504 "forward reference and definition of global have different types");
505
506 // Move the forward-reference to the correct spot in the module.
507 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
508 }
509
510 if (Name.empty())
511 NumberedVals.push_back(GV);
512
513 // Set the parsed properties on the global.
514 if (Init)
515 GV->setInitializer(Init);
516 GV->setConstant(IsConstant);
517 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
518 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
519 GV->setThreadLocal(ThreadLocal);
520
521 // Parse attributes on the global.
522 while (Lex.getKind() == lltok::comma) {
523 Lex.Lex();
524
525 if (Lex.getKind() == lltok::kw_section) {
526 Lex.Lex();
527 GV->setSection(Lex.getStrVal());
528 if (ParseToken(lltok::StringConstant, "expected global section string"))
529 return true;
530 } else if (Lex.getKind() == lltok::kw_align) {
531 unsigned Alignment;
532 if (ParseOptionalAlignment(Alignment)) return true;
533 GV->setAlignment(Alignment);
534 } else {
535 TokError("unknown global variable property!");
536 }
537 }
538
539 return false;
540 }
541
542
543 //===----------------------------------------------------------------------===//
544 // GlobalValue Reference/Resolution Routines.
545 //===----------------------------------------------------------------------===//
546
547 /// GetGlobalVal - Get a value with the specified name or ID, creating a
548 /// forward reference record if needed. This can return null if the value
549 /// exists but does not have the right type.
550 GlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty,
551 LocTy Loc) {
552 const PointerType *PTy = dyn_cast(Ty);
553 if (PTy == 0) {
554 Error(Loc, "global variable reference must have pointer type");
555 return 0;
556 }
557
558 // Look this name up in the normal function symbol table.
559 GlobalValue *Val =
560 cast_or_null(M->getValueSymbolTable().lookup(Name));
561
562 // If this is a forward reference for the value, see if we already created a
563 // forward ref record.
564 if (Val == 0) {
565 std::map >::iterator
566 I = ForwardRefVals.find(Name);
567 if (I != ForwardRefVals.end())
568 Val = I->second.first;
569 }
570
571 // If we have the value in the symbol table or fwd-ref table, return it.
572 if (Val) {
573 if (Val->getType() == Ty) return Val;
574 Error(Loc, "'@" + Name + "' defined with type '" +
575 Val->getType()->getDescription() + "'");
576 return 0;
577 }
578
579 // Otherwise, create a new forward reference for this value and remember it.
580 GlobalValue *FwdVal;
581 if (const FunctionType *FT = dyn_cast(PTy->getElementType()))
582 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
583 else
584 FwdVal = new GlobalVariable(PTy->getElementType(), false,
585 GlobalValue::ExternalWeakLinkage, 0, Name, M);
586
587 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
588 return FwdVal;
589 }
590
591 GlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) {
592 const PointerType *PTy = dyn_cast(Ty);
593 if (PTy == 0) {
594 Error(Loc, "global variable reference must have pointer type");
595 return 0;
596 }
597
598 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
599
600 // If this is a forward reference for the value, see if we already created a
601 // forward ref record.
602 if (Val == 0) {
603 std::map >::iterator
604 I = ForwardRefValIDs.find(ID);
605 if (I != ForwardRefValIDs.end())
606 Val = I->second.first;
607 }
608
609 // If we have the value in the symbol table or fwd-ref table, return it.
610 if (Val) {
611 if (Val->getType() == Ty) return Val;
612 Error(Loc, "'@" + utostr(ID) + "' defined with type '" +
613 Val->getType()->getDescription() + "'");
614 return 0;
615 }
616
617 // Otherwise, create a new forward reference for this value and remember it.
618 GlobalValue *FwdVal;
619 if (const FunctionType *FT = dyn_cast(PTy->getElementType()))
620 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
621 else
622 FwdVal = new GlobalVariable(PTy->getElementType(), false,
623 GlobalValue::ExternalWeakLinkage, 0, "", M);
624
625 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
626 return FwdVal;
627 }
628
629
630 //===----------------------------------------------------------------------===//
631 // Helper Routines.
632 //===----------------------------------------------------------------------===//
633
634 /// ParseToken - If the current token has the specified kind, eat it and return
635 /// success. Otherwise, emit the specified error and return failure.
636 bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
637 if (Lex.getKind() != T)
638 return TokError(ErrMsg);
639 Lex.Lex();
640 return false;
641 }
642
643 bool LLParser::ParseUnsigned(unsigned &Val) {
644 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
645 return TokError("expected integer");
646 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
647 if (Val64 != unsigned(Val64))
648 return TokError("expected 32-bit integer (too large)");
649 Val = Val64;
650 Lex.Lex();
651 return false;
652 }
653
654
655 /// ParseOptionalAddrSpace
656 /// := /*empty*/
657 /// := 'addrspace' '(' uint32 ')'
658 bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
659 AddrSpace = 0;
660 bool HasAddrSpace;
661 ParseOptionalToken(lltok::kw_addrspace, HasAddrSpace);
662 if (!HasAddrSpace)
663 return false;
664
665 return ParseToken(lltok::lparen, "expected '(' in address space") ||
666 ParseUnsigned(AddrSpace) ||
667 ParseToken(lltok::rparen, "expected ')' in address space");
668 }
669
670 /// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind
671 /// indicates what kind of attribute list this is: 0: function arg, 1: result,
672 /// 2: function attr.
673 bool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) {
674 Attrs = Attribute::None;
675 LocTy AttrLoc = Lex.getLoc();
676
677 while (1) {
678 switch (Lex.getKind()) {
679 case lltok::kw_sext:
680 case lltok::kw_zext:
681 // Treat these as signext/zeroext unless they are function attrs.
682 // FIXME: REMOVE THIS IN LLVM 3.0
683 if (AttrKind != 2) {
684 if (Lex.getKind() == lltok::kw_sext)
685 Attrs |= Attribute::SExt;
686 else
687 Attrs |= Attribute::ZExt;
688 break;
689 }
690 // FALL THROUGH.
691 default: // End of attributes.
692 if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly))
693 return Error(AttrLoc, "invalid use of function-only attribute");
694
695 if (AttrKind != 0 && (Attrs & Attribute::ParameterOnly))
696 return Error(AttrLoc, "invalid use of parameter-only attribute");
697
698 return false;
699 case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break;
700 case lltok::kw_signext: Attrs |= Attribute::SExt; break;
701 case lltok::kw_inreg: Attrs |= Attribute::InReg; break;
702 case lltok::kw_sret: Attrs |= Attribute::StructRet; break;
703 case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break;
704 case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break;
705 case lltok::kw_byval: Attrs |= Attribute::ByVal; break;
706 case lltok::kw_nest: Attrs |= Attribute::Nest; break;
707
708 case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break;
709 case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break;
710 case lltok::kw_noinline: Attrs |= Attribute::NoInline; break;
711 case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break;
712 case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break;
713 case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break;
714 case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break;
715 case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break;
716 case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break;
717
718
719 case lltok::kw_align: {
720 unsigned Alignment;
721 if (ParseOptionalAlignment(Alignment))
722 return true;
723 Attrs |= Attribute::constructAlignmentFromInt(Alignment);
724 continue;
725 }
726 }
727 Lex.Lex();
728 }
729 }
730
731 /// ParseOptionalLinkage
732 /// ::= /*empty*/
733 /// ::= 'internal'
734 /// ::= 'weak'
735 /// ::= 'linkonce'
736 /// ::= 'appending'
737 /// ::= 'dllexport'
738 /// ::= 'common'
739 /// ::= 'dllimport'
740 /// ::= 'extern_weak'
741 /// ::= 'external'
742 bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
743 HasLinkage = false;
744 switch (Lex.getKind()) {
745 default: Res = GlobalValue::ExternalLinkage; return false;
746 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
747 case lltok::kw_weak: Res = GlobalValue::WeakLinkage; break;
748 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceLinkage; break;
749 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
750 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
751 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
752 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
753 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
754 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
755 }
756 Lex.Lex();
757 HasLinkage = true;
758 return false;
759 }
760
761 /// ParseOptionalVisibility
762 /// ::= /*empty*/
763 /// ::= 'default'
764 /// ::= 'hidden'
765 /// ::= 'protected'
766 ///
767 bool LLParser::ParseOptionalVisibility(unsigned &Res) {
768 switch (Lex.getKind()) {
769 default: Res = GlobalValue::DefaultVisibility; return false;
770 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
771 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
772 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
773 }
774 Lex.Lex();
775 return false;
776 }
777
778 /// ParseOptionalCallingConv
779 /// ::= /*empty*/
780 /// ::= 'ccc'
781 /// ::= 'fastcc'
782 /// ::= 'coldcc'
783 /// ::= 'x86_stdcallcc'
784 /// ::= 'x86_fastcallcc'
785 /// ::= 'cc' UINT
786 ///
787 bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
788 switch (Lex.getKind()) {
789 default: CC = CallingConv::C; return false;
790 case lltok::kw_ccc: CC = CallingConv::C; break;
791 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
792 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
793 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
794 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
795 case lltok::kw_cc: Lex.Lex(); return ParseUnsigned(CC);
796 }
797 Lex.Lex();
798 return false;
799 }
800
801 /// ParseOptionalAlignment
802 /// ::= /* empty */
803 /// ::= 'align' 4
804 bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
805 Alignment = 0;
806 bool HasAlignment;
807 if (ParseOptionalToken(lltok::kw_align, HasAlignment)) return true;
808
809 return HasAlignment && ParseUnsigned(Alignment);
810 }
811
812 /// ParseOptionalCommaAlignment
813 /// ::= /* empty */
814 /// ::= ',' 'align' 4
815 bool LLParser::ParseOptionalCommaAlignment(unsigned &Alignment) {
816 Alignment = 0;
817 bool HasComma;
818 ParseOptionalToken(lltok::comma, HasComma);
819 if (!HasComma)
820 return false;
821
822 return ParseToken(lltok::kw_align, "expected 'align'") ||
823 ParseUnsigned(Alignment);
824 }
825
826 /// ParseIndexList
827 /// ::= (',' uint32)+
828 bool LLParser::ParseIndexList(SmallVectorImpl &Indices) {
829 if (Lex.getKind() != lltok::comma)
830 return TokError("expected ',' as start of index list");
831
832 while (Lex.getKind() == lltok::comma) {
833 Lex.Lex();
834 unsigned Idx;
835 if (ParseUnsigned(Idx)) return true;
836 Indices.push_back(Idx);
837 }
838
839 return false;
840 }
841
842 //===----------------------------------------------------------------------===//
843 // Type Parsing.
844 //===----------------------------------------------------------------------===//
845
846 /// ParseType - Parse and resolve a full type.
847 bool LLParser::ParseType(PATypeHolder &Result) {
848 if (ParseTypeRec(Result)) return true;
849
850 // Verify no unresolved uprefs.
851 if (!UpRefs.empty())
852 return Error(UpRefs.back().Loc, "invalid unresolved type up reference");
853 // GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
854
855 return false;
856 }
857
858 /// HandleUpRefs - Every time we finish a new layer of types, this function is
859 /// called. It loops through the UpRefs vector, which is a list of the
860 /// currently active types. For each type, if the up-reference is contained in
861 /// the newly completed type, we decrement the level count. When the level
862 /// count reaches zero, the up-referenced type is the type that is passed in:
863 /// thus we can complete the cycle.
864 ///
865 PATypeHolder LLParser::HandleUpRefs(const Type *ty) {
866 // If Ty isn't abstract, or if there are no up-references in it, then there is
867 // nothing to resolve here.
868 if (!ty->isAbstract() || UpRefs.empty()) return ty;
869
870 PATypeHolder Ty(ty);
871 #if 0
872 errs() << "Type '" << Ty->getDescription()
873 << "' newly formed. Resolving upreferences.\n"
874 << UpRefs.size() << " upreferences active!\n";
875 #endif
876
877 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
878 // to zero), we resolve them all together before we resolve them to Ty. At
879 // the end of the loop, if there is anything to resolve to Ty, it will be in
880 // this variable.
881 OpaqueType *TypeToResolve = 0;
882
883 for (unsigned i = 0; i != UpRefs.size(); ++i) {
884 // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
885 bool ContainsType =
886 std::find(Ty->subtype_begin(), Ty->subtype_end(),
887 UpRefs[i].LastContainedTy) != Ty->subtype_end();
888
889 #if 0
890 errs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
891 << UpRefs[i].LastContainedTy->getDescription() << ") = "
892 << (ContainsType ? "true" : "false")
893 << " level=" << UpRefs[i].NestingLevel << "\n";
894 #endif
895 if (!ContainsType)
896 continue;
897
898 // Decrement level of upreference
899 unsigned Level = --UpRefs[i].NestingLevel;
900 UpRefs[i].LastContainedTy = Ty;
901
902 // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
903 if (Level != 0)
904 continue;
905
906 #if 0
907 errs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
908 #endif
909 if (!TypeToResolve)
910 TypeToResolve = UpRefs[i].UpRefTy;
911 else
912 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
913 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list.
914 --i; // Do not skip the next element.
915 }
916
917 if (TypeToResolve)
918 TypeToResolve->refineAbstractTypeTo(Ty);
919
920 return Ty;
921 }
922
923
924 /// ParseTypeRec - The recursive function used to process the internal
925 /// implementation details of types.
926 bool LLParser::ParseTypeRec(PATypeHolder &Result) {
927 switch (Lex.getKind()) {
928 default:
929 return TokError("expected type");
930 case lltok::Type:
931 // TypeRec ::= 'float' | 'void' (etc)
932 Result = Lex.getTyVal();
933 Lex.Lex();
934 break;
935 case lltok::kw_opaque:
936 // TypeRec ::= 'opaque'
937 Result = OpaqueType::get();
938 Lex.Lex();
939 break;
940 case lltok::lbrace:
941 // TypeRec ::= '{' ... '}'
942 if (ParseStructType(Result, false))
943 return true;
944 break;
945 case lltok::lsquare:
946 // TypeRec ::= '[' ... ']'
947 Lex.Lex(); // eat the lsquare.
948 if (ParseArrayVectorType(Result, false))
949 return true;
950 break;
951 case lltok::less: // Either vector or packed struct.
952 // TypeRec ::= '<' ... '>'
953 if (Lex.Lex() == lltok::lbrace) {
954 if (ParseStructType(Result, true))
955 return true;
956 if (Lex.getKind() != lltok::greater)
957 return TokError("expected '>' at end of packed struct");
958 Lex.Lex();
959 } else if (ParseArrayVectorType(Result, true))
960 return true;
961 break;
962 case lltok::LocalVar:
963 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
964 // TypeRec ::= %foo
965 if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
966 Result = T;
967 } else {
968 Result = OpaqueType::get();
969 ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
970 std::make_pair(Result,
971 Lex.getLoc())));
972 M->addTypeName(Lex.getStrVal(), Result.get());
973 }
974 Lex.Lex();
975 break;
976
977 case lltok::LocalVarID:
978 // TypeRec ::= %4
979 if (Lex.getUIntVal() < NumberedTypes.size())
980 Result = NumberedTypes[Lex.getUIntVal()];
981 else {
982 std::map >::iterator
983 I = ForwardRefTypeIDs.find(Lex.getUIntVal());
984 if (I != ForwardRefTypeIDs.end())
985 Result = I->second.first;
986 else {
987 Result = OpaqueType::get();
988 ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
989 std::make_pair(Result,
990 Lex.getLoc())));
991 }
992 }
993 Lex.Lex();
994 break;
995 case lltok::backslash: {
996 // TypeRec ::= '\' 4
997 unsigned Val;
998 Lex.Lex();
999 if (ParseUnsigned(Val)) return true;
1000 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder.
1001 UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
1002 Result = OT;
1003 break;
1004 }
1005 }
1006
1007 // Parse the type suffixes.
1008 while (1) {
1009 switch (Lex.getKind()) {
1010 // End of type.
1011 default: return false;
1012
1013 // TypeRec ::= TypeRec '*'
1014 case lltok::star:
1015 if (Result.get() == Type::LabelTy)
1016 return TokError("basic block pointers are invalid");
1017 Result = HandleUpRefs(PointerType::getUnqual(Result.get()));
1018 Lex.Lex();
1019 break;
1020
1021 // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
1022 case lltok::kw_addrspace: {
1023 if (Result.get() == Type::LabelTy)
1024 return TokError("basic block pointers are invalid");
1025 unsigned AddrSpace;
1026 if (ParseOptionalAddrSpace(AddrSpace) ||
1027 ParseToken(lltok::star, "expected '*' in address space"))
1028 return true;
1029
1030 Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace));
1031 break;
1032 }
1033
1034 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1035 case lltok::lparen:
1036 if (ParseFunctionType(Result))
1037 return true;
1038 break;
1039 }
1040 }
1041 }
1042
1043 /// ParseParameterList
1044 /// ::= '(' ')'
1045 /// ::= '(' Arg (',' Arg)* ')'
1046 /// Arg
1047 /// ::= Type OptionalAttributes Value OptionalAttributes
1048 bool LLParser::ParseParameterList(SmallVectorImpl &ArgList,
1049 PerFunctionState &PFS) {
1050 if (ParseToken(lltok::lparen, "expected '(' in call"))
1051 return true;
1052
1053 while (Lex.getKind() != lltok::rparen) {
1054 // If this isn't the first argument, we need a comma.
1055 if (!ArgList.empty() &&
1056 ParseToken(lltok::comma, "expected ',' in argument list"))
1057 return true;
1058
1059 // Parse the argument.
1060 LocTy ArgLoc;
1061 PATypeHolder ArgTy(Type::VoidTy);
1062 unsigned ArgAttrs1, ArgAttrs2;
1063 Value *V;
1064 if (ParseType(ArgTy, ArgLoc) ||
1065 ParseOptionalAttrs(ArgAttrs1, 0) ||
1066 ParseValue(ArgTy, V, PFS) ||
1067 // FIXME: Should not allow attributes after the argument, remove this in
1068 // LLVM 3.0.
1069 ParseOptionalAttrs(ArgAttrs2, 0))
1070 return true;
1071 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1072 }
1073
1074 Lex.Lex(); // Lex the ')'.
1075 return false;
1076 }
1077
1078
1079
1080 /// ParseArgumentList
1081 /// ::= '(' ArgTypeListI ')'
1082 /// ArgTypeListI
1083 /// ::= /*empty*/
1084 /// ::= '...'
1085 /// ::= ArgTypeList ',' '...'
1086 /// ::= ArgType (',' ArgType)*
1087 bool LLParser::ParseArgumentList(std::vector &ArgList,
1088 bool &isVarArg) {
1089 isVarArg = false;
1090 assert(Lex.getKind() == lltok::lparen);
1091 Lex.Lex(); // eat the (.
1092
1093 if (Lex.getKind() == lltok::rparen) {
1094 // empty
1095 } else if (Lex.getKind() == lltok::dotdotdot) {
1096 isVarArg = true;
1097 Lex.Lex();
1098 } else {
1099 LocTy TypeLoc = Lex.getLoc();
1100 PATypeHolder ArgTy(Type::VoidTy);
1101
1102 if (ParseTypeRec(ArgTy)) return true;
1103
1104 if (!ArgTy->isFirstClassType() && !isa(ArgTy))
1105 return Error(TypeLoc, "invalid type for function argument");
1106
1107 unsigned Attrs;
1108 if (ParseOptionalAttrs(Attrs, 0)) return true;
1109
1110 std::string Name;
1111 if (Lex.getKind() == lltok::LocalVar ||
1112 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1113 Name = Lex.getStrVal();
1114 Lex.Lex();
1115 }
1116
1117 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1118
1119 while (Lex.getKind() == lltok::comma) {
1120 Lex.Lex(); // eat the comma.
1121
1122 // Handle ... at end of arg list.
1123 if (Lex.getKind() == lltok::dotdotdot) {
1124 isVarArg = true;
1125 Lex.Lex();
1126 break;
1127 }
1128
1129 // Otherwise must be an argument type.
1130 TypeLoc = Lex.getLoc();
1131 if (ParseTypeRec(ArgTy)) return true;
1132 if (!ArgTy->isFirstClassType() && !isa(ArgTy))
1133 return Error(TypeLoc, "invalid type for function argument");
1134
1135 if (ParseOptionalAttrs(Attrs, 0)) return true;
1136
1137 if (Lex.getKind() == lltok::LocalVar ||
1138 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1139 Name = Lex.getStrVal();
1140 Lex.Lex();
1141 } else {
1142 Name = "";
1143 }
1144
1145 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1146 }
1147 }
1148
1149 if (Lex.getKind() != lltok::rparen)
1150 return TokError("expected ')' at end of function argument list");
1151 Lex.Lex();
1152 return false;
1153 }
1154
1155 /// ParseFunctionType
1156 /// ::= Type ArgumentList OptionalAttrs
1157 bool LLParser::ParseFunctionType(PATypeHolder &Result) {
1158 assert(Lex.getKind() == lltok::lparen);
1159
1160 std::vector ArgList;
1161 bool isVarArg;
1162 unsigned Attrs;
1163 if (ParseArgumentList(ArgList, isVarArg) ||
1164 // FIXME: Allow, but ignore attributes on function types!
1165 // FIXME: Remove in LLVM 3.0
1166 ParseOptionalAttrs(Attrs, 2))
1167 return true;
1168
1169 // Reject names on the arguments lists.
1170 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1171 if (!ArgList[i].Name.empty())
1172 return Error(ArgList[i].Loc, "argument name invalid in function type");
1173 if (!ArgList[i].Attrs != 0) {
1174 // Allow but ignore attributes on function types; this permits
1175 // auto-upgrade.
1176 // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0
1177 }
1178 }
1179
1180 std::vector ArgListTy;
1181 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
1182 ArgListTy.push_back(ArgList[i].Type);
1183
1184 Result = HandleUpRefs(FunctionType::get(Result.get(), ArgListTy, isVarArg));
1185 return false;
1186 }
1187
1188 /// ParseStructType: Handles packed and unpacked types. parsed elsewhere.
1189 /// TypeRec
1190 /// ::= '{' '}'
1191 /// ::= '{' TypeRec (',' TypeRec)* '}'
1192 /// ::= '<' '{' '}' '>'
1193 /// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>'
1194 bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
1195 assert(Lex.getKind() == lltok::lbrace);
1196 Lex.Lex(); // Consume the '{'
1197
1198 if (Lex.getKind() == lltok::rbrace) {
1199 Result = StructType::get(std::vector(), Packed);
1200 Lex.Lex();
1201 return false;
1202 }
1203
1204 std::vector ParamsList;
1205 if (ParseTypeRec(Result)) return true;
1206 ParamsList.push_back(Result);
1207
1208 while (Lex.getKind() == lltok::comma) {
1209 Lex.Lex(); // eat the comma.
1210
1211 if (ParseTypeRec(Result)) return true;
1212 ParamsList.push_back(Result);
1213 }
1214
1215 if (Lex.getKind() != lltok::rbrace)
1216 return TokError("expected '}' at end of struct");
1217 Lex.Lex(); // Consume the '}'
1218
1219 std::vector ParamsListTy;
1220 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1221 ParamsListTy.push_back(ParamsList[i].get());
1222 Result = HandleUpRefs(StructType::get(ParamsListTy, Packed));
1223 return false;
1224 }
1225
1226 /// ParseArrayVectorType - Parse an array or vector type, assuming the first
1227 /// token has already been consumed.
1228 /// TypeRec
1229 /// ::= '[' APSINTVAL 'x' Types ']'
1230 /// ::= '<' APSINTVAL 'x' Types '>'
1231 bool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) {
1232 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1233 Lex.getAPSIntVal().getBitWidth() > 64)
1234 return TokError("expected number in address space");
1235
1236 LocTy SizeLoc = Lex.getLoc();
1237 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
1238 if (Lex.Lex() != lltok::kw_x)
1239 return TokError("expected 'x' after element count");
1240 Lex.Lex(); // eat the 'x'.
1241
1242 LocTy TypeLoc = Lex.getLoc();
1243 PATypeHolder EltTy(Type::VoidTy);
1244 if (ParseTypeRec(EltTy)) return true;
1245
1246 if (Lex.getKind() != (isVector ? lltok::greater : lltok::rsquare))
1247 return TokError("expected end of sequential type");
1248 Lex.Lex();
1249
1250 if (isVector) {
1251 if ((unsigned)Size != Size)
1252 return Error(SizeLoc, "size too large for vector");
1253 if (!EltTy->isFloatingPoint() && !EltTy->isInteger())
1254 return Error(TypeLoc, "vector element type must be fp or integer");
1255 Result = VectorType::get(EltTy, unsigned(Size));
1256 } else {
1257 if (!EltTy->isFirstClassType() && !isa(EltTy))
1258 return Error(TypeLoc, "invalid array element type");
1259 Result = HandleUpRefs(ArrayType::get(EltTy, Size));
1260 }
1261 return false;
1262 }
1263
1264 //===----------------------------------------------------------------------===//
1265 // Function Semantic Analysis.
1266 //===----------------------------------------------------------------------===//
1267
1268 LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f)
1269 : P(p), F(f) {
1270
1271 // Insert unnamed arguments into the NumberedVals list.
1272 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1273 AI != E; ++AI)
1274 if (!AI->hasName())
1275 NumberedVals.push_back(AI);
1276 }
1277
1278 LLParser::PerFunctionState::~PerFunctionState() {
1279 // If there were any forward referenced non-basicblock values, delete them.
1280 for (std::map >::iterator
1281 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1282 if (!isa(I->second.first)) {
1283 I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first
1284 ->getType()));
1285 delete I->second.first;
1286 I->second.first = 0;
1287 }
1288
1289 for (std::map >::iterator
1290 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1291 if (!isa(I->second.first)) {
1292 I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first
1293 ->getType()));
1294 delete I->second.first;
1295 I->second.first = 0;
1296 }
1297 }
1298
1299 bool LLParser::PerFunctionState::VerifyFunctionComplete() {
1300 if (!ForwardRefVals.empty())
1301 return P.Error(ForwardRefVals.begin()->second.second,
1302 "use of undefined value '%" + ForwardRefVals.begin()->first +
1303 "'");
1304 if (!ForwardRefValIDs.empty())
1305 return P.Error(ForwardRefValIDs.begin()->second.second,
1306 "use of undefined value '%" +
1307 utostr(ForwardRefValIDs.begin()->first) + "'");
1308 return false;
1309 }
1310
1311
1312 /// GetVal - Get a value with the specified name or ID, creating a
1313 /// forward reference record if needed. This can return null if the value
1314 /// exists but does not have the right type.
1315 Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
1316 const Type *Ty, LocTy Loc) {
1317 // Look this name up in the normal function symbol table.
1318 Value *Val = F.getValueSymbolTable().lookup(Name);
1319
1320 // If this is a forward reference for the value, see if we already created a
1321 // forward ref record.
1322 if (Val == 0) {
1323 std::map >::iterator
1324 I = ForwardRefVals.find(Name);
1325 if (I != ForwardRefVals.end())
1326 Val = I->second.first;
1327 }
1328
1329 // If we have the value in the symbol table or fwd-ref table, return it.
1330 if (Val) {
1331 if (Val->getType() == Ty) return Val;
1332 if (Ty == Type::LabelTy)
1333 P.Error(Loc, "'%" + Name + "' is not a basic block");
1334 else
1335 P.Error(Loc, "'%" + Name + "' defined with type '" +
1336 Val->getType()->getDescription() + "'");
1337 return 0;
1338 }
1339
1340 // Don't make placeholders with invalid type.
1341 if (!Ty->isFirstClassType() && !isa(Ty) && Ty != Type::LabelTy) {
1342 P.Error(Loc, "invalid use of a non-first-class type");
1343 return 0;
1344 }
1345
1346 // Otherwise, create a new forward reference for this value and remember it.
1347 Value *FwdVal;
1348 if (Ty == Type::LabelTy)
1349 FwdVal = BasicBlock::Create(Name, &F);
1350 else
1351 FwdVal = new Argument(Ty, Name);
1352
1353 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1354 return FwdVal;
1355 }
1356
1357 Value *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty,
1358 LocTy Loc) {
1359 // Look this name up in the normal function symbol table.
1360 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
1361
1362 // If this is a forward reference for the value, see if we already created a
1363 // forward ref record.
1364 if (Val == 0) {
1365 std::map >::iterator
1366 I = ForwardRefValIDs.find(ID);
1367 if (I != ForwardRefValIDs.end())
1368 Val = I->second.first;
1369 }
1370
1371 // If we have the value in the symbol table or fwd-ref table, return it.
1372 if (Val) {
1373 if (Val->getType() == Ty) return Val;
1374 if (Ty == Type::LabelTy)
1375 P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block");
1376 else
1377 P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" +
1378 Val->getType()->getDescription() + "'");
1379 return 0;
1380 }
1381
1382 if (!Ty->isFirstClassType() && !isa(Ty) && Ty != Type::LabelTy) {
1383 P.Error(Loc, "invalid use of a non-first-class type");
1384 return 0;
1385 }
1386
1387 // Otherwise, create a new forward reference for this value and remember it.
1388 Value *FwdVal;
1389 if (Ty == Type::LabelTy)
1390 FwdVal = BasicBlock::Create("", &F);
1391 else
1392 FwdVal = new Argument(Ty);
1393
1394 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1395 return FwdVal;
1396 }
1397
1398 /// SetInstName - After an instruction is parsed and inserted into its
1399 /// basic block, this installs its name.
1400 bool LLParser::PerFunctionState::SetInstName(int NameID,
1401 const std::string &NameStr,
1402 LocTy NameLoc, Instruction *Inst) {
1403 // If this instruction has void type, it cannot have a name or ID specified.
1404 if (Inst->getType() == Type::VoidTy) {
1405 if (NameID != -1 || !NameStr.empty())
1406 return P.Error(NameLoc, "instructions returning void cannot have a name");
1407 return false;
1408 }
1409
1410 // If this was a numbered instruction, verify that the instruction is the
1411 // expected value and resolve any forward references.
1412 if (NameStr.empty()) {
1413 // If neither a name nor an ID was specified, just use the next ID.
1414 if (NameID == -1)
1415 NameID = NumberedVals.size();
1416
1417 if (unsigned(NameID) != NumberedVals.size())
1418 return P.Error(NameLoc, "instruction expected to be numbered '%" +
1419 utostr(NumberedVals.size()) + "'");
1420
1421 std::map >::iterator FI =
1422 ForwardRefValIDs.find(NameID);
1423 if (FI != ForwardRefValIDs.end()) {
1424 if (FI->second.first->getType() != Inst->getType())
1425 return P.Error(NameLoc, "instruction forward referenced with type '" +
1426 FI->second.first->getType()->getDescription() + "'");
1427 FI->second.first->replaceAllUsesWith(Inst);
1428 ForwardRefValIDs.erase(FI);
1429 }
1430
1431 NumberedVals.push_back(Inst);
1432 return false;
1433 }
1434
1435 // Otherwise, the instruction had a name. Resolve forward refs and set it.
1436 std::map >::iterator
1437 FI = ForwardRefVals.find(NameStr);
1438 if (FI != ForwardRefVals.end()) {
1439 if (FI->second.first->getType() != Inst->getType())
1440 return P.Error(NameLoc, "instruction forward referenced with type '" +
1441 FI->second.first->getType()->getDescription() + "'");
1442 FI->second.first->replaceAllUsesWith(Inst);
1443 ForwardRefVals.erase(FI);
1444 }
1445
1446 // Set the name on the instruction.
1447 Inst->setName(NameStr);
1448
1449 if (Inst->getNameStr() != NameStr)
1450 return P.Error(NameLoc, "multiple definition of local value named '" +
1451 NameStr + "'");
1452 return false;
1453 }
1454
1455 /// GetBB - Get a basic block with the specified name or ID, creating a
1456 /// forward reference record if needed.
1457 BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
1458 LocTy Loc) {
1459 return cast_or_null(GetVal(Name, Type::LabelTy, Loc));
1460 }
1461
1462 BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
1463 return cast_or_null(GetVal(ID, Type::LabelTy, Loc));
1464 }
1465
1466 /// DefineBB - Define the specified basic block, which is either named or
1467 /// unnamed. If there is an error, this returns null otherwise it returns
1468 /// the block being defined.
1469 BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
1470 LocTy Loc) {
1471 BasicBlock *BB;
1472 if (Name.empty())
1473 BB = GetBB(NumberedVals.size(), Loc);
1474 else
1475 BB = GetBB(Name, Loc);
1476 if (BB == 0) return 0; // Already diagnosed error.
1477
1478 // Move the block to the end of the function. Forward ref'd blocks are
1479 // inserted wherever they happen to be referenced.
1480 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
1481
1482 // Remove the block from forward ref sets.
1483 if (Name.empty()) {
1484 ForwardRefValIDs.erase(NumberedVals.size());
1485 NumberedVals.push_back(BB);
1486 } else {
1487 // BB forward references are already in the function symbol table.
1488 ForwardRefVals.erase(Name);
1489 }
1490
1491 return BB;
1492 }
1493
1494 //===----------------------------------------------------------------------===//
1495 // Constants.
1496 //===----------------------------------------------------------------------===//
1497
1498 /// ParseValID - Parse an abstract value that doesn't necessarily have a
1499 /// type implied. For example, if we parse "4" we don't know what integer type
1500 /// it has. The value will later be combined with its type and checked for
1501 /// sanity.
1502 bool LLParser::ParseValID(ValID &ID) {
1503 ID.Loc = Lex.getLoc();
1504 switch (Lex.getKind()) {
1505 default: return TokError("expected value token");
1506 case lltok::GlobalID: // @42
1507 ID.UIntVal = Lex.getUIntVal();
1508 ID.Kind = ValID::t_GlobalID;
1509 break;
1510 case lltok::GlobalVar: // @foo
1511 ID.StrVal = Lex.getStrVal();
1512 ID.Kind = ValID::t_GlobalName;
1513 break;
1514 case lltok::LocalVarID: // %42
1515 ID.UIntVal = Lex.getUIntVal();
1516 ID.Kind = ValID::t_LocalID;
1517 break;
1518 case lltok::LocalVar: // %foo
1519 case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0
1520 ID.StrVal = Lex.getStrVal();
1521 ID.Kind = ValID::t_LocalName;
1522 break;
1523 case lltok::APSInt:
1524 ID.APSIntVal = Lex.getAPSIntVal();
1525 ID.Kind = ValID::t_APSInt;
1526 break;
1527 case lltok::APFloat:
1528 ID.APFloatVal = Lex.getAPFloatVal();
1529 ID.Kind = ValID::t_APFloat;
1530 break;
1531 case lltok::kw_true:
1532 ID.ConstantVal = ConstantInt::getTrue();
1533 ID.Kind = ValID::t_Constant;
1534 break;
1535 case lltok::kw_false:
1536 ID.ConstantVal = ConstantInt::getFalse();
1537 ID.Kind = ValID::t_Constant;
1538 break;
1539 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
1540 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
1541 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
1542
1543 case lltok::lbrace: {
1544 // ValID ::= '{' ConstVector '}'
1545 Lex.Lex();
1546 SmallVector Elts;
1547 if (ParseGlobalValueVector(Elts) ||
1548 ParseToken(lltok::rbrace, "expected end of struct constant"))
1549 return true;
1550
1551 ID.ConstantVal = ConstantStruct::get(&Elts[0], Elts.size(), false);
1552 ID.Kind = ValID::t_Constant;
1553 return false;
1554 }
1555 case lltok::less: {
1556 // ValID ::= '<' ConstVector '>' --> Vector.
1557 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
1558 Lex.Lex();
1559 bool isPackedStruct;
1560 ParseOptionalToken(lltok::lbrace, isPackedStruct);
1561
1562 SmallVector Elts;
1563 LocTy FirstEltLoc = Lex.getLoc();
1564 if (ParseGlobalValueVector(Elts) ||
1565 (isPackedStruct &&
1566 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
1567 ParseToken(lltok::greater, "expected end of constant"))
1568 return true;
1569
1570 if (isPackedStruct) {
1571 ID.ConstantVal = ConstantStruct::get(&Elts[0], Elts.size(), true);
1572 ID.Kind = ValID::t_Constant;
1573 return false;
1574 }
1575
1576 if (Elts.empty())
1577 return Error(ID.Loc, "constant vector must not be empty");
1578
1579 if (!Elts[0]->getType()->isInteger() &&
1580 !Elts[0]->getType()->isFloatingPoint())
1581 return Error(FirstEltLoc,
1582 "vector elements must have integer or floating point type");
1583
1584 // Verify that all the vector elements have the same type.
1585 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
1586 if (Elts[i]->getType() != Elts[0]->getType())
1587 return Error(FirstEltLoc,
1588 "vector element #" + utostr(i) +
1589 " is not of type '" + Elts[0]->getType()->getDescription());
1590
1591 ID.ConstantVal = ConstantVector::get(&Elts[0], Elts.size());
1592 ID.Kind = ValID::t_Constant;
1593 return false;
1594 }
1595 case lltok::lsquare: { // Array Constant
1596 Lex.Lex();
1597 SmallVector Elts;
1598 LocTy FirstEltLoc = Lex.getLoc();
1599 if (ParseGlobalValueVector(Elts) ||
1600 ParseToken(lltok::rsquare, "expected end of array constant"))
1601 return true;
1602
1603 // Handle empty element.
1604 if (Elts.empty()) {
1605 // Use undef instead of an array because it's inconvenient to determine
1606 // the element type at this point, there being no elements to examine.
1607 ID.Kind = ValID::t_Undef;
1608 return false;
1609 }
1610
1611 if (!Elts[0]->getType()->isFirstClassType())
1612 return Error(FirstEltLoc, "invalid array element type: " +
1613 Elts[0]->getType()->getDescription());
1614
1615 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
1616
1617 // Verify all elements are correct type!
1618 for (unsigned i = i, e = Elts.size() ; i != e; ++i) {
1619 if (Elts[i]->getType() != Elts[0]->getType())
1620 return Error(FirstEltLoc,
1621 "array element #" + utostr(i) +
1622 " is not of type '" +Elts[0]->getType()->getDescription());
1623 }
1624
1625 ID.ConstantVal = ConstantArray::get(ATy, &Elts[0], Elts.size());
1626 ID.Kind = ValID::t_Constant;
1627 return false;
1628 }
1629 case lltok::kw_c: // c "foo"
1630 Lex.Lex();
1631 ID.ConstantVal = ConstantArray::get(Lex.getStrVal(), false);
1632 if (ParseToken(lltok::StringConstant, "expected string")) return true;
1633 ID.Kind = ValID::t_Constant;
1634 return false;
1635
1636 case lltok::kw_asm: {
1637 // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT
1638 bool HasSideEffect;
1639 Lex.Lex();
1640 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
1641 ParseToken(lltok::StringConstant, "expected asm string"))
1642 return true;
1643 ID.StrVal = Lex.getStrVal();
1644
1645 if (ParseToken(lltok::comma, "expected comma in inline asm expression") ||
1646 ParseToken(lltok::StringConstant, "expected constraint string"))
1647 return true;
1648 ID.StrVal2 = Lex.getStrVal();
1649 ID.UIntVal = HasSideEffect;
1650 ID.Kind = ValID::t_InlineAsm;
1651 return false;
1652 }
1653
1654 case lltok::kw_trunc:
1655 case lltok::kw_zext:
1656 case lltok::kw_sext:
1657 case lltok::kw_fptrunc:
1658 case lltok::kw_fpext:
1659 case lltok::kw_bitcast:
1660 case lltok::kw_uitofp:
1661 case lltok::kw_sitofp:
1662 case lltok::kw_fptoui:
1663 case lltok::kw_fptosi:
1664 case lltok::kw_inttoptr:
1665 case lltok::kw_ptrtoint: {
1666 unsigned Opc = Lex.getUIntVal();
1667 PATypeHolder DestTy(Type::VoidTy);
1668 Constant *SrcVal;
1669 Lex.Lex();
1670 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
1671 ParseGlobalTypeAndValue(SrcVal) ||
1672 ParseToken(lltok::kw_to, "expected 'to' int constantexpr cast") ||
1673 ParseType(DestTy) ||
1674 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
1675 return true;
1676 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
1677 return Error(ID.Loc, "invalid cast opcode for cast from '" +
1678 SrcVal->getType()->getDescription() + "' to '" +
1679 DestTy->getDescription() + "'");
1680 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, SrcVal,
1681 DestTy);
1682 ID.Kind = ValID::t_Constant;
1683 return false;
1684 }
1685 case lltok::kw_extractvalue: {
1686 Lex.Lex();
1687 Constant *Val;
1688 SmallVector Indices;
1689 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
1690 ParseGlobalTypeAndValue(Val) ||
1691 ParseIndexList(Indices) ||
1692 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
1693 return true;
1694 if (!isa(Val->getType()) && !isa(Val->getType()))
1695 return Error(ID.Loc, "extractvalue operand must be array or struct");
1696 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
1697 Indices.end()))
1698 return Error(ID.Loc, "invalid indices for extractvalue");
1699 ID.ConstantVal = ConstantExpr::getExtractValue(Val,
1700 &Indices[0], Indices.size());
1701 ID.Kind = ValID::t_Constant;
1702 return false;
1703 }
1704 case lltok::kw_insertvalue: {
1705 Lex.Lex();
1706 Constant *Val0, *Val1;
1707 SmallVector Indices;
1708 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
1709 ParseGlobalTypeAndValue(Val0) ||
1710 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
1711 ParseGlobalTypeAndValue(Val1) ||
1712 ParseIndexList(Indices) ||
1713 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
1714 return true;
1715 if (!isa(Val0->getType()) && !isa(Val0->getType()))
1716 return Error(ID.Loc, "extractvalue operand must be array or struct");
1717 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
1718 Indices.end()))
1719 return Error(ID.Loc, "invalid indices for insertvalue");
1720 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
1721 &Indices[0], Indices.size());
1722 ID.Kind = ValID::t_Constant;
1723 return false;
1724 }
1725 case lltok::kw_icmp:
1726 case lltok::kw_fcmp:
1727 case lltok::kw_vicmp:
1728 case lltok::kw_vfcmp: {
1729 unsigned PredVal, Opc = Lex.getUIntVal();
1730 Constant *Val0, *Val1;
1731 Lex.Lex();
1732 if (ParseCmpPredicate(PredVal, Opc) ||
1733 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
1734 ParseGlobalTypeAndValue(Val0) ||
1735 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
1736 ParseGlobalTypeAndValue(Val1) ||
1737 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
1738 return true;
1739
1740 if (Val0->getType() != Val1->getType())
1741 return Error(ID.Loc, "compare operands must have the same type");
1742
1743 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
1744
1745 if (Opc == Instruction::FCmp) {
1746 if (!Val0->getType()->isFPOrFPVector())
1747 return Error(ID.Loc, "fcmp requires floating point operands");
1748 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
1749 } else if (Opc == Instruction::ICmp) {
1750 if (!Val0->getType()->isIntOrIntVector() &&
1751 !isa(Val0->getType()))
1752 return Error(ID.Loc, "icmp requires pointer or integer operands");
1753 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
1754 } else if (Opc == Instruction::VFCmp) {
1755 // FIXME: REMOVE VFCMP Support
1756 ID.ConstantVal = ConstantExpr::getVFCmp(Pred, Val0, Val1);
1757 } else if (Opc == Instruction::VICmp) {
1758 // FIXME: REMOVE VFCMP Support
1759 ID.ConstantVal = ConstantExpr::getVICmp(Pred, Val0, Val1);
1760 }
1761 ID.Kind = ValID::t_Constant;
1762 return false;
1763 }
1764
1765 // Binary Operators.
1766 case lltok::kw_add:
1767 case lltok::kw_sub:
1768 case lltok::kw_mul:
1769 case lltok::kw_udiv:
1770 case lltok::kw_sdiv:
1771 case lltok::kw_fdiv:
1772 case lltok::kw_urem:
1773 case lltok::kw_srem:
1774 case lltok::kw_frem: {
1775 unsigned Opc = Lex.getUIntVal();
1776 Constant *Val0, *Val1;
1777 Lex.Lex();
1778 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
1779 ParseGlobalTypeAndValue(Val0) ||
1780 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
1781 ParseGlobalTypeAndValue(Val1) ||
1782 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
1783 return true;
1784 if (Val0->getType() != Val1->getType())
1785 return Error(ID.Loc, "operands of constexpr must have same type");
1786 if (!Val0->getType()->isIntOrIntVector() &&
1787 !Val0->getType()->isFPOrFPVector())
1788 return Error(ID.Loc,"constexpr requires integer, fp, or vector operands");
1789 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
1790 ID.Kind = ValID::t_Constant;
1791 return false;
1792 }
1793
1794 // Logical Operations
1795 case lltok::kw_shl:
1796 case lltok::kw_lshr:
1797 case lltok::kw_ashr:
1798 case lltok::kw_and:
1799 case lltok::kw_or:
1800 case lltok::kw_xor: {
1801 unsigned Opc = Lex.getUIntVal();
1802 Constant *Val0, *Val1;
1803 Lex.Lex();
1804 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
1805 ParseGlobalTypeAndValue(Val0) ||
1806 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
1807 ParseGlobalTypeAndValue(Val1) ||
1808 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
1809 return true;
1810 if (Val0->getType() != Val1->getType())
1811 return Error(ID.Loc, "operands of constexpr must have same type");
1812 if (!Val0->getType()->isIntOrIntVector())
1813 return Error(ID.Loc,
1814 "constexpr requires integer or integer vector operands");
1815 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
1816 ID.Kind = ValID::t_Constant;
1817 return false;
1818 }
1819
1820 case lltok::kw_getelementptr:
1821 case lltok::kw_shufflevector:
1822 case lltok::kw_insertelement:
1823 case lltok::kw_extractelement:
1824 case lltok::kw_select: {
1825 unsigned Opc = Lex.getUIntVal();
1826 SmallVector Elts;
1827 Lex.Lex();
1828 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
1829 ParseGlobalValueVector(Elts) ||
1830 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
1831 return true;
1832
1833 if (Opc == Instruction::GetElementPtr) {
1834 if (Elts.size() == 0 || !isa(Elts[0]->getType()))
1835 return Error(ID.Loc, "getelementptr requires pointer operand");
1836
1837 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
1838 (Value**)&Elts[1], Elts.size()-1))
1839 return Error(ID.Loc, "invalid indices for getelementptr");
1840 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0],
1841 &Elts[1], Elts.size()-1);
1842 } else if (Opc == Instruction::Select) {
1843 if (Elts.size() != 3)
1844 return Error(ID.Loc, "expected three operands to select");
1845 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
1846 Elts[2]))
1847 return Error(ID.Loc, Reason);
1848 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
1849 } else if (Opc == Instruction::ShuffleVector) {
1850 if (Elts.size() != 3)
1851 return Error(ID.Loc, "expected three operands to shufflevector");
1852 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
1853 return Error(ID.Loc, "invalid operands to shufflevector");
1854 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
1855 } else if (Opc == Instruction::ExtractElement) {
1856 if (Elts.size() != 2)
1857 return Error(ID.Loc, "expected two operands to extractelement");
1858 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
1859 return Error(ID.Loc, "invalid extractelement operands");
1860 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
1861 } else {
1862 assert(Opc == Instruction::InsertElement && "Unknown opcode");
1863 if (Elts.size() != 3)
1864 return Error(ID.Loc, "expected three operands to insertelement");
1865 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
1866 return Error(ID.Loc, "invalid insertelement operands");
1867 ID.ConstantVal = ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
1868 }
1869
1870 ID.Kind = ValID::t_Constant;
1871 return false;
1872 }
1873 }
1874
1875 Lex.Lex();
1876 return false;
1877 }
1878
1879 /// ParseGlobalValue - Parse a global value with the specified type.
1880 bool LLParser::ParseGlobalValue(const Type *Ty, Constant *&V) {
1881 V = 0;
1882 ValID ID;
1883 if (ParseValID(ID) ||
1884 ConvertGlobalValIDToValue(Ty, ID, V))
1885 return true;
1886 return false;
1887 }
1888
1889 /// ConvertGlobalValIDToValue - Apply a type to a ValID to get a fully resolved
1890 /// constant.
1891 bool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID,
1892 Constant *&V) {
1893 if (isa(Ty))
1894 return Error(ID.Loc, "functions are not values, refer to them as pointers");
1895
1896 switch (ID.Kind) {
1897 default: assert(0 && "Unknown ValID!");
1898 case ValID::t_LocalID:
1899 case ValID::t_LocalName:
1900 return Error(ID.Loc, "invalid use of function-local name");
1901 case ValID::t_InlineAsm:
1902 return Error(ID.Loc, "inline asm can only be an operand of call/invoke");
1903 case ValID::t_GlobalName:
1904 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
1905 return V == 0;
1906 case ValID::t_GlobalID:
1907 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
1908 return V == 0;
1909 case ValID::t_APSInt:
1910 if (!isa(Ty))
1911 return Error(ID.Loc, "integer constant must have integer type");
1912 ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
1913 V = ConstantInt::get(ID.APSIntVal);
1914 return false;
1915 case ValID::t_APFloat:
1916 if (!Ty->isFloatingPoint() ||
1917 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
1918 return Error(ID.Loc, "floating point constant invalid for type");
1919
1920 // The lexer has no type info, so builds all float and double FP constants
1921 // as double. Fix this here. Long double does not need this.
1922 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
1923 Ty == Type::FloatTy) {
1924 bool Ignored;
1925 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1926 &Ignored);
1927 }
1928 V = ConstantFP::get(ID.APFloatVal);
1929 return false;
1930 case ValID::t_Null:
1931 if (!isa(Ty))
1932 return Error(ID.Loc, "null must be a pointer type");
1933 V = ConstantPointerNull::get(cast(Ty));
1934 return false;
1935 case ValID::t_Undef:
1936 V = UndefValue::get(Ty);
1937 return false;
1938 case ValID::t_Zero:
1939 if (!Ty->isFirstClassType())
1940 return Error(ID.Loc, "invalid type for null constant");
1941 V = Constant::getNullValue(Ty);
1942 return false;
1943 case ValID::t_Constant:
1944 if (ID.ConstantVal->getType() != Ty)
1945 return Error(ID.Loc, "constant expression type mismatch");
1946 V = ID.ConstantVal;
1947 return false;
1948 }
1949 }
1950
1951 bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
1952 PATypeHolder Type(Type::VoidTy);
1953 return ParseType(Type) ||
1954 ParseGlobalValue(Type, V);
1955 }
1956
1957 bool LLParser::ParseGlobalValueVector(SmallVectorImpl &Elts) {
1958 // Empty list.
1959 if (Lex.getKind() == lltok::rbrace ||
1960 Lex.getKind() == lltok::rsquare ||
1961 Lex.getKind() == lltok::greater ||
1962 Lex.getKind() == lltok::rparen)
1963 return false;
1964
1965 Constant *C;
1966 if (ParseGlobalTypeAndValue(C)) return true;
1967 Elts.push_back(C);
1968
1969 while (Lex.getKind() == lltok::comma) {
1970 Lex.Lex();
1971 if (ParseGlobalTypeAndValue(C)) return true;
1972 Elts.push_back(C);
1973 }
1974
1975 return false;
1976 }
1977
1978
1979 //===----------------------------------------------------------------------===//
1980 // Function Parsing.
1981 //===----------------------------------------------------------------------===//
1982
1983 bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
1984 PerFunctionState &PFS) {
1985 if (ID.Kind == ValID::t_LocalID)
1986 V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc);
1987 else if (ID.Kind == ValID::t_LocalName)
1988 V = PFS.GetVal(ID.StrVal, Ty, ID.Loc);
1989 else if (ID.Kind == ValID::ValID::t_InlineAsm) {
1990 const PointerType *PTy = dyn_cast(Ty);
1991 const FunctionType *FTy =
1992 PTy ? dyn_cast(PTy->getElementType()) : 0;
1993 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
1994 return Error(ID.Loc, "invalid type for inline asm constraint string");
1995 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal);
1996 return false;
1997 } else {
1998 Constant *C;
1999 if (ConvertGlobalValIDToValue(Ty, ID, C)) return true;
2000 V = C;
2001 return false;
2002 }
2003
2004 return V == 0;
2005 }
2006
2007 bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2008 V = 0;
2009 ValID ID;
2010 if (ParseValID(ID) ||
2011 ConvertValIDToValue(Ty, ID, V, PFS))
2012 return true;
2013 return false;
2014 }
2015
2016 bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
2017 PATypeHolder T(Type::VoidTy);
2018 if (ParseType(T)) return true;
2019 return ParseValue(T, V, PFS);
2020 }
2021
2022 /// FunctionHeader
2023 /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
2024 /// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
2025 /// OptionalAlign OptGC
2026 bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2027 // Parse the linkage.
2028 LocTy LinkageLoc = Lex.getLoc();
2029 unsigned Linkage;
2030
2031 unsigned Visibility, CC, RetAttrs;
2032 PATypeHolder RetType(Type::VoidTy);
2033 LocTy RetTypeLoc = Lex.getLoc();
2034 if (ParseOptionalLinkage(Linkage) ||
2035 ParseOptionalVisibility(Visibility) ||
2036 ParseOptionalCallingConv(CC) ||
2037 ParseOptionalAttrs(RetAttrs, 1) ||
2038 ParseType(RetType, RetTypeLoc))
2039 return true;
2040
2041 // Verify that the linkage is ok.
2042 switch ((GlobalValue::LinkageTypes)Linkage) {
2043 case GlobalValue::ExternalLinkage:
2044 break; // always ok.
2045 case GlobalValue::DLLImportLinkage:
2046 case GlobalValue::ExternalWeakLinkage:
2047 if (isDefine)
2048 return Error(LinkageLoc, "invalid linkage for function definition");
2049 break;
2050 case GlobalValue::InternalLinkage:
2051 case GlobalValue::LinkOnceLinkage:
2052 case GlobalValue::WeakLinkage:
2053 case GlobalValue::DLLExportLinkage:
2054 if (!isDefine)
2055 return Error(LinkageLoc, "invalid linkage for function declaration");
2056 break;
2057 case GlobalValue::AppendingLinkage:
2058 case GlobalValue::GhostLinkage:
2059 case GlobalValue::CommonLinkage:
2060 return Error(LinkageLoc, "invalid function linkage type");
2061 }
2062
2063 if (!FunctionType::isValidReturnType(RetType))
2064 return Error(RetTypeLoc, "invalid function return type");
2065
2066 if (Lex.getKind() != lltok::GlobalVar)
2067 return TokError("expected function name");
2068
2069 LocTy NameLoc = Lex.getLoc();
2070 std::string FunctionName = Lex.getStrVal();
2071
2072 if (Lex.Lex() != lltok::lparen)
2073 return TokError("expected '(' in function argument list");
2074
2075 std::vector ArgList;
2076 bool isVarArg;
2077 if (ParseArgumentList(ArgList, isVarArg)) return true;
2078
2079 unsigned FuncAttrs;
2080 if (ParseOptionalAttrs(FuncAttrs, 2)) return true;
2081
2082 // Section string.
2083 std::string Section;
2084 if (Lex.getKind() == lltok::kw_section) {
2085 if (Lex.Lex() != lltok::StringConstant)
2086 return TokError("expected section name");
2087 Section = Lex.getStrVal();
2088 Lex.Lex();
2089 }
2090
2091 unsigned Alignment;
2092 if (ParseOptionalAlignment(Alignment)) return true;
2093
2094 // If the alignment was parsed as an attribute, move to the alignment field.
2095 if (FuncAttrs & Attribute::Alignment) {
2096 Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2097 FuncAttrs &= ~Attribute::Alignment;
2098 }
2099
2100 // Optional GC setting.
2101 std::string GC;
2102 if (Lex.getKind() == lltok::kw_gc) {
2103 if (Lex.Lex() != lltok::StringConstant)
2104 return TokError("expected gc name");
2105 GC = Lex.getStrVal();
2106 Lex.Lex();
2107 }
2108
2109 // Okay, if we got here, the function is syntactically valid. Convert types
2110 // and do semantic checks.
2111 std::vector ParamTypeList;
2112 SmallVector Attrs;
2113 // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2114 // attributes.
2115 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
2116 if (FuncAttrs & ObsoleteFuncAttrs) {
2117 RetAttrs |= FuncAttrs & ObsoleteFuncAttrs;
2118 FuncAttrs &= ~ObsoleteFuncAttrs;
2119 }
2120
2121 if (RetAttrs != Attribute::None)
2122 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2123
2124 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2125 ParamTypeList.push_back(ArgList[i].Type);
2126 if (ArgList[i].Attrs != Attribute::None)
2127 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2128 }
2129
2130 if (FuncAttrs != Attribute::None)
2131 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2132
2133 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
2134
2135 const FunctionType *FT = FunctionType::get(RetType, ParamTypeList, isVarArg);
2136 const PointerType *PFT = PointerType::getUnqual(FT);
2137
2138 Fn = 0;
2139 if (!FunctionName.empty()) {
2140 // If this was a definition of a forward reference, remove the definition
2141 // from the forward reference table and fill in the forward ref.
2142 std::map >::iterator FRVI =
2143 ForwardRefVals.find(FunctionName);
2144 if (FRVI != ForwardRefVals.end()) {
2145 Fn = M->getFunction(FunctionName);
2146 ForwardRefVals.erase(FRVI);
2147 } else if ((Fn = M->getFunction(FunctionName))) {
2148 // If this function already exists in the symbol table, then it is
2149 // multiply defined. We accept a few cases for old backwards compat.
2150 // FIXME: Remove this stuff for LLVM 3.0.
2151 if (Fn->getType() != PFT || Fn->getAttributes() != PAL ||
2152 (!Fn->isDeclaration() && isDefine)) {
2153 // If the redefinition has different type or different attributes,
2154 // reject it. If both have bodies, reject it.
2155 return Error(NameLoc, "invalid redefinition of function '" +
2156 FunctionName + "'");
2157 } else if (Fn->isDeclaration()) {
2158 // Make sure to strip off any argument names so we can't get conflicts.
2159 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2160 AI != AE; ++AI)
2161 AI->setName("");
2162 }
2163 }
2164
2165 } else if (FunctionName.empty()) {
2166 // If this is a definition of a forward referenced function, make sure the
2167 // types agree.
2168 std::map >::iterator I
2169 = ForwardRefValIDs.find(NumberedVals.size());
2170 if (I != ForwardRefValIDs.end()) {
2171 Fn = cast(I->second.first);
2172 if (Fn->getType() != PFT)
2173 return Error(NameLoc, "type of definition and forward reference of '@" +
2174 utostr(NumberedVals.size()) +"' disagree");
2175 ForwardRefValIDs.erase(I);
2176 }
2177 }
2178
2179 if (Fn == 0)
2180 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2181 else // Move the forward-reference to the correct spot in the module.
2182 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2183
2184 if (FunctionName.empty())
2185 NumberedVals.push_back(Fn);
2186
2187 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2188 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2189 Fn->setCallingConv(CC);
2190 Fn->setAttributes(PAL);
2191 Fn->setAlignment(Alignment);
2192 Fn->setSection(Section);
2193 if (!GC.empty()) Fn->setGC(GC.c_str());
2194
2195 // Add all of the arguments we parsed to the function.
2196 Function::arg_iterator ArgIt = Fn->arg_begin();
2197 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
2198 // If the argument has a name, insert it into the argument symbol table.
2199 if (ArgList[i].Name.empty()) continue;
2200
2201 // Set the name, if it conflicted, it will be auto-renamed.
2202 ArgIt->setName(ArgList[i].Name);
2203
2204 if (ArgIt->getNameStr() != ArgList[i].Name)
2205 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2206 ArgList[i].Name + "'");
2207 }
2208
2209 return false;
2210 }
2211
2212
2213 /// ParseFunctionBody
2214 /// ::= '{' BasicBlock+ '}'
2215 /// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0
2216 ///
2217 bool LLParser::ParseFunctionBody(Function &Fn) {
2218 if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin)
2219 return TokError("expected '{' in function body");
2220 Lex.Lex(); // eat the {.
2221
2222 PerFunctionState PFS(*this, Fn);
2223
2224 while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end)
2225 if (ParseBasicBlock(PFS)) return true;
2226
2227 // Eat the }.
2228 Lex.Lex();
2229
2230 // Verify function is ok.
2231 return PFS.VerifyFunctionComplete();
2232 }
2233
2234 /// ParseBasicBlock
2235 /// ::= LabelStr? Instruction*
2236 bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2237 // If this basic block starts out with a name, remember it.
2238 std::string Name;
2239 LocTy NameLoc = Lex.getLoc();
2240 if (Lex.getKind() == lltok::LabelStr) {
2241 Name = Lex.getStrVal();
2242 Lex.Lex();
2243 }
2244
2245 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2246 if (BB == 0) return true;
2247
2248 std::string NameStr;
2249
2250 // Parse the instructions in this block until we get a terminator.
2251 Instruction *Inst;
2252 do {
2253 // This instruction may have three possibilities for a name: a) none
2254 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2255 LocTy NameLoc = Lex.getLoc();
2256 int NameID = -1;
2257 NameStr = "";
2258
2259 if (Lex.getKind() == lltok::LocalVarID) {
2260 NameID = Lex.getUIntVal();
2261 Lex.Lex();
2262 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2263 return true;
2264 } else if (Lex.getKind() == lltok::LocalVar ||
2265 // FIXME: REMOVE IN LLVM 3.0
2266 Lex.getKind() == lltok::StringConstant) {
2267 NameStr = Lex.getStrVal();
2268 Lex.Lex();
2269 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2270 return true;
2271 }
2272
2273 if (ParseInstruction(Inst, BB, PFS)) return true;
2274
2275 BB->getInstList().push_back(Inst);
2276
2277 // Set the name on the instruction.
2278 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2279 } while (!isa(Inst));
2280
2281 return false;
2282 }
2283
2284 //===----------------------------------------------------------------------===//
2285 // Instruction Parsing.
2286 //===----------------------------------------------------------------------===//
2287
2288 /// ParseInstruction - Parse one of the many different instructions.
2289 ///
2290 bool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2291 PerFunctionState &PFS) {
2292 lltok::Kind Token = Lex.getKind();
2293 if (Token == lltok::Eof)
2294 return TokError("found end of file when expecting more instructions");
2295 LocTy Loc = Lex.getLoc();
2296 Lex.Lex(); // Eat the keyword.
2297
2298 switch (Token) {
2299 default: return Error(Loc, "expected instruction opcode");
2300 // Terminator Instructions.
2301 case lltok::kw_unwind: Inst = new UnwindInst(); return false;
2302 case lltok::kw_unreachable: Inst = new UnreachableInst(); return false;
2303 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
2304 case lltok::kw_br: return ParseBr(Inst, PFS);
2305 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
2306 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
2307 // Binary Operators.
2308 case lltok::kw_add:
2309 case lltok::kw_sub:
2310 case lltok::kw_mul:
2311 case lltok::kw_udiv:
2312 case lltok::kw_sdiv:
2313 case lltok::kw_fdiv:
2314 case lltok::kw_urem:
2315 case lltok::kw_srem:
2316 case lltok::kw_frem: return ParseArithmetic(Inst, PFS, Lex.getUIntVal());
2317 case lltok::kw_shl:
2318 case lltok::kw_lshr:
2319 case lltok::kw_ashr:
2320 case lltok::kw_and:
2321 case lltok::kw_or:
2322 case lltok::kw_xor: return ParseLogical(Inst, PFS, Lex.getUIntVal());
2323 case lltok::kw_icmp:
2324 case lltok::kw_fcmp:
2325 case lltok::kw_vicmp:
2326 case lltok::kw_vfcmp: return ParseCompare(Inst, PFS, Lex.getUIntVal());
2327 // Casts.
2328 case lltok::kw_trunc:
2329 case lltok::kw_zext:
2330 case lltok::kw_sext:
2331 case lltok::kw_fptrunc:
2332 case lltok::kw_fpext:
2333 case lltok::kw_bitcast:
2334 case lltok::kw_uitofp:
2335 case lltok::kw_sitofp:
2336 case lltok::kw_fptoui:
2337 case lltok::kw_fptosi:
2338 case lltok::kw_inttoptr:
2339 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, Lex.getUIntVal());
2340 // Other.
2341 case lltok::kw_select: return ParseSelect(Inst, PFS);
2342 case lltok::kw_va_arg: return ParseVAArg(Inst, PFS);
2343 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
2344 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
2345 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
2346 case lltok::kw_phi: return ParsePHI(Inst, PFS);
2347 case lltok::kw_call: return ParseCall(Inst, PFS, false);
2348 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
2349 // Memory.
2350 case lltok::kw_alloca:
2351 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, Lex.getUIntVal());
2352 case lltok::kw_free: return ParseFree(Inst, PFS);
2353 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
2354 case lltok::kw_store: return ParseStore(Inst, PFS, false);
2355 case lltok::kw_volatile:
2356 if (Lex.getKind() == lltok::kw_load) {
2357 Lex.Lex();
2358 return ParseLoad(Inst, PFS, true);
2359 } else if (Lex.getKind() == lltok::kw_store) {
2360 Lex.Lex();
2361 return ParseStore(Inst, PFS, true);
2362 } else {
2363 return TokError("expected 'load' or 'store'");
2364 }
2365 case lltok::kw_getresult: return ParseGetResult(Inst, PFS);
2366 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
2367 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
2368 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
2369 }
2370 }
2371
2372 /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
2