Lines Matching defs:C
50 StringRef upto(Cursor C) const {
51 assert(C.Ptr >= Ptr && C.Ptr <= End);
52 return StringRef(Ptr, C.Ptr - Ptr);
85 static Cursor skipWhitespace(Cursor C) {
86 while (isblank(C.peek()))
87 C.advance();
88 return C;
91 static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
94 static Cursor skipComment(Cursor C) {
95 if (C.peek() != ';')
96 return C;
97 while (!isNewlineChar(C.peek()) && !C.isEOF())
98 C.advance();
99 return C;
104 static Cursor skipMachineOperandComment(Cursor C) {
105 if (C.peek() != '/' || C.peek(1) != '*')
106 return C;
108 while (C.peek() != '*' || C.peek(1) != '/')
109 C.advance();
111 C.advance();
112 C.advance();
113 return C;
118 static bool isIdentifierChar(char C) {
119 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
120 C == '$';
128 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
131 Str.reserve(C.remaining().size());
132 while (!C.isEOF()) {
133 char Char = C.peek();
135 if (C.peek(1) == '\\') {
138 C.advance(2);
141 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
142 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
143 C.advance(3);
148 C.advance();
154 static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
155 assert(C.peek() == '"');
156 for (C.advance(); C.peek() != '"'; C.advance()) {
157 if (C.isEOF() || isNewlineChar(C.peek())) {
159 C.location(),
164 C.advance();
165 return C;
168 static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
170 auto Range = C;
171 C.advance(PrefixLength);
172 if (C.peek() == '"') {
173 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
183 while (isIdentifierChar(C.peek()))
184 C.advance();
185 Token.reset(Type, Range.upto(C))
186 .setStringValue(Range.upto(C).drop_front(PrefixLength));
187 return C;
293 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
294 if (!isalpha(C.peek()) && C.peek() != '_')
296 auto Range = C;
297 while (isIdentifierChar(C.peek()))
298 C.advance();
299 auto Identifier = Range.upto(C);
302 return C;
305 static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
307 bool IsReference = C.remaining().starts_with("%bb.");
308 if (!IsReference && !C.remaining().starts_with("bb."))
310 auto Range = C;
312 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
313 if (!isdigit(C.peek())) {
314 Token.reset(MIToken::Error, C.remaining());
315 ErrorCallback(C.location(), "expected a number after '%bb.'");
316 return C;
318 auto NumberRange = C;
319 while (isdigit(C.peek()))
320 C.advance();
321 StringRef Number = NumberRange.upto(C);
326 if (C.peek() == '.') {
327 C.advance(); // Skip '.'
329 while (isIdentifierChar(C.peek()))
330 C.advance();
334 Range.upto(C))
336 .setStringValue(Range.upto(C).drop_front(StringOffset));
337 return C;
340 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
342 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
344 auto Range = C;
345 C.advance(Rule.size());
346 auto NumberRange = C;
347 while (isdigit(C.peek()))
348 C.advance();
349 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
350 return C;
353 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
355 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
357 auto Range = C;
358 C.advance(Rule.size());
359 auto NumberRange = C;
360 while (isdigit(C.peek()))
361 C.advance();
362 StringRef Number = NumberRange.upto(C);
364 if (C.peek() == '.') {
365 C.advance();
367 while (isIdentifierChar(C.peek()))
368 C.advance();
370 Token.reset(Kind, Range.upto(C))
372 .setStringValue(Range.upto(C).drop_front(StringOffset));
373 return C;
376 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
377 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
380 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
381 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
384 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
385 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
388 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
389 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
392 static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
395 if (!C.remaining().starts_with(Rule))
397 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
401 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
404 if (!C.remaining().starts_with(Rule))
406 if (isdigit(C.peek(Rule.size())))
407 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
408 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
411 static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
414 if (!C.remaining().starts_with(Rule))
416 if (isdigit(C.peek(Rule.size())))
417 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
418 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
421 static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
423 if (C.peek() != '"')
425 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
429 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
430 auto Range = C;
431 C.advance(); // Skip '%'
432 auto NumberRange = C;
433 while (isdigit(C.peek()))
434 C.advance();
435 Token.reset(MIToken::VirtualRegister, Range.upto(C))
436 .setIntegerValue(APSInt(NumberRange.upto(C)));
437 return C;
441 static bool isRegisterChar(char C) {
442 return isIdentifierChar(C) && C != '.';
445 static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
446 Cursor Range = C;
447 C.advance(); // Skip '%'
448 while (isRegisterChar(C.peek()))
449 C.advance();
450 Token.reset(MIToken::NamedVirtualRegister, Range.upto(C))
451 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
452 return C;
455 static Cursor maybeLexRegister(Cursor C, MIToken &Token,
457 if (C.peek() != '%' && C.peek() != '$')
460 if (C.peek() == '%') {
461 if (isdigit(C.peek(1)))
462 return lexVirtualRegister(C, Token);
464 if (isRegisterChar(C.peek(1)))
465 return lexNamedVirtualRegister(C, Token);
470 assert(C.peek() == '$');
471 auto Range = C;
472 C.advance(); // Skip '$'
473 while (isRegisterChar(C.peek()))
474 C.advance();
475 Token.reset(MIToken::NamedRegister, Range.upto(C))
476 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
477 return C;
480 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
482 if (C.peek() != '@')
484 if (!isdigit(C.peek(1)))
485 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
487 auto Range = C;
488 C.advance(1); // Skip the '@'
489 auto NumberRange = C;
490 while (isdigit(C.peek()))
491 C.advance();
492 Token.reset(MIToken::GlobalValue, Range.upto(C))
493 .setIntegerValue(APSInt(NumberRange.upto(C)));
494 return C;
497 static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
499 if (C.peek() != '&')
501 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
505 static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
508 if (!C.remaining().starts_with(Rule))
510 auto Start = C;
511 C.advance(Rule.size());
514 if (C.peek() != '"') {
515 while (isIdentifierChar(C.peek()))
516 C.advance();
517 StringRef String = Start.upto(C).drop_front(Rule.size());
518 if (C.peek() != '>') {
519 ErrorCallback(C.location(),
524 C.advance();
526 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
527 return C;
531 Cursor R = lexStringConstant(C, ErrorCallback);
533 ErrorCallback(C.location(),
552 static bool isValidHexFloatingPointPrefix(char C) {
553 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
556 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
557 C.advance();
559 while (isdigit(C.peek()))
560 C.advance();
561 if ((C.peek() == 'e' || C.peek() == 'E') &&
562 (isdigit(C.peek(1)) ||
563 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
564 C.advance(2);
565 while (isdigit(C.peek()))
566 C.advance();
568 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
569 return C;
572 static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
573 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
575 Cursor Range = C;
576 C.advance(2);
578 if (isValidHexFloatingPointPrefix(C.peek())) {
579 C.advance();
582 while (isxdigit(C.peek()))
583 C.advance();
584 StringRef StrVal = Range.upto(C);
588 Token.reset(MIToken::HexLiteral, Range.upto(C));
590 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
591 return C;
594 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
595 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
597 auto Range = C;
598 C.advance();
599 while (isdigit(C.peek()))
600 C.advance();
601 if (C.peek() == '.')
602 return lexFloatingPointLiteral(Range, C, Token);
603 StringRef StrVal = Range.upto(C);
605 return C;
619 static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
621 if (C.peek() != '!')
623 auto Range = C;
624 C.advance(1);
625 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
626 Token.reset(MIToken::exclaim, Range.upto(C));
627 return C;
629 while (isIdentifierChar(C.peek()))
630 C.advance();
631 StringRef StrVal = Range.upto(C);
636 return C;
639 static MIToken::TokenKind symbolToken(char C) {
640 switch (C) {
670 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
673 if (C.peek() == ':' && C.peek(1) == ':') {
677 Kind = symbolToken(C.peek());
680 auto Range = C;
681 C.advance(Length);
682 Token.reset(Kind, Range.upto(C));
683 return C;
686 static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
687 if (!isNewlineChar(C.peek()))
689 auto Range = C;
690 C.advance();
691 Token.reset(MIToken::Newline, Range.upto(C));
692 return C;
695 static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
697 if (C.peek() != '`')
699 auto Range = C;
700 C.advance();
701 auto StrRange = C;
702 while (C.peek() != '`') {
703 if (C.isEOF() || isNewlineChar(C.peek())) {
705 C.location(),
708 return C;
710 C.advance();
712 StringRef Value = StrRange.upto(C);
713 C.advance();
714 Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value);
715 return C;
720 auto C = skipComment(skipWhitespace(Cursor(Source)));
721 if (C.isEOF()) {
722 Token.reset(MIToken::Eof, C.remaining());
723 return C.remaining();
726 C = skipMachineOperandComment(C);
728 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
730 if (Cursor R = maybeLexIdentifier(C, Token))
732 if (Cursor R = maybeLexJumpTableIndex(C, Token))
734 if (Cursor R = maybeLexStackObject(C, Token))
736 if (Cursor R = maybeLexFixedStackObject(C, Token))
738 if (Cursor R = maybeLexConstantPoolItem(C, Token))
740 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
742 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
744 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
746 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
748 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
750 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
752 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
754 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
756 if (Cursor R = maybeLexNumericalLiteral(C, Token))
758 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
760 if (Cursor R = maybeLexSymbol(C, Token))
762 if (Cursor R = maybeLexNewline(C, Token))
764 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
766 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
769 Token.reset(MIToken::Error, C.remaining());
770 ErrorCallback(C.location(),
771 Twine("unexpected character '") + Twine(C.peek()) + "'");
772 return C.remaining();