Chris Lattner | 699f1eb | 2002-08-14 17:12:33 +0000 | [diff] [blame] | 1 | /*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements the flex scanner for LLVM assembly languages files. |
| 4 | // |
Chris Lattner | 699f1eb | 2002-08-14 17:12:33 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===*/ |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 6 | |
| 7 | %option prefix="llvmAsm" |
| 8 | %option yylineno |
| 9 | %option nostdinit |
| 10 | %option never-interactive |
| 11 | %option batch |
| 12 | %option noyywrap |
| 13 | %option nodefault |
| 14 | %option 8bit |
| 15 | %option outfile="Lexer.cpp" |
| 16 | %option ecs |
| 17 | %option noreject |
| 18 | %option noyymore |
| 19 | |
| 20 | %{ |
| 21 | #include "ParserInternals.h" |
Chris Lattner | b9bcbb5 | 2003-04-22 19:07:06 +0000 | [diff] [blame^] | 22 | #include "llvm/Module.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | #include <list> |
| 24 | #include "llvmAsmParser.h" |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
| 26 | #include <stdlib.h> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | |
| 28 | #define RET_TOK(type, Enum, sym) \ |
| 29 | llvmAsmlval.type = Instruction::Enum; return sym |
| 30 | |
| 31 | |
| 32 | // TODO: All of the static identifiers are figured out by the lexer, |
Chris Lattner | e1fe875 | 2001-09-07 16:32:43 +0000 | [diff] [blame] | 33 | // these should be hashed to reduce the lexer size |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | // atoull - Convert an ascii string of decimal digits into the unsigned long |
| 37 | // long representation... this does not have to do input error checking, |
| 38 | // because we know that the input will be matched by a suitable regex... |
| 39 | // |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 40 | static uint64_t atoull(const char *Buffer) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | uint64_t Result = 0; |
| 42 | for (; *Buffer; Buffer++) { |
| 43 | uint64_t OldRes = Result; |
| 44 | Result *= 10; |
| 45 | Result += *Buffer-'0'; |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 46 | if (Result < OldRes) // Uh, oh, overflow detected!!! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | ThrowException("constant bigger than 64 bits detected!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | } |
| 49 | return Result; |
| 50 | } |
| 51 | |
Chris Lattner | 3e8ba10 | 2003-04-17 22:17:32 +0000 | [diff] [blame] | 52 | static uint64_t HexIntToVal(const char *Buffer) { |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 53 | uint64_t Result = 0; |
| 54 | for (; *Buffer; ++Buffer) { |
| 55 | uint64_t OldRes = Result; |
| 56 | Result *= 16; |
| 57 | char C = *Buffer; |
| 58 | if (C >= '0' && C <= '9') |
| 59 | Result += C-'0'; |
| 60 | else if (C >= 'A' && C <= 'F') |
| 61 | Result += C-'A'+10; |
| 62 | else if (C >= 'a' && C <= 'f') |
| 63 | Result += C-'a'+10; |
| 64 | |
| 65 | if (Result < OldRes) // Uh, oh, overflow detected!!! |
| 66 | ThrowException("constant bigger than 64 bits detected!"); |
| 67 | } |
Chris Lattner | 3e8ba10 | 2003-04-17 22:17:32 +0000 | [diff] [blame] | 68 | return Result; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // HexToFP - Convert the ascii string in hexidecimal format to the floating |
| 73 | // point representation of it. |
| 74 | // |
| 75 | static double HexToFP(const char *Buffer) { |
| 76 | uint64_t Result = HexIntToVal(Buffer); |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 77 | |
| 78 | assert(sizeof(double) == sizeof(Result) && |
| 79 | "Data sizes incompatible on this target!"); |
Chris Lattner | b55679d | 2002-04-07 08:31:26 +0000 | [diff] [blame] | 80 | // Behave nicely in the face of C TBAA rules... see: |
| 81 | // http://www.nullstone.com/htmls/category/aliastyp.htm |
| 82 | // |
| 83 | char *ProxyPointer = (char*)&Result; |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 84 | return *(double*)ProxyPointer; // Cast Hex constant to double |
| 85 | } |
| 86 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 88 | // UnEscapeLexed - Run through the specified buffer and change \xx codes to the |
| 89 | // appropriate character. If AllowNull is set to false, a \00 value will cause |
| 90 | // an exception to be thrown. |
| 91 | // |
| 92 | // If AllowNull is set to true, the return value of the function points to the |
| 93 | // last character of the string in memory. |
| 94 | // |
Chris Lattner | bcafcce | 2002-07-25 06:17:42 +0000 | [diff] [blame] | 95 | char *UnEscapeLexed(char *Buffer, bool AllowNull) { |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 96 | char *BOut = Buffer; |
| 97 | for (char *BIn = Buffer; *BIn; ) { |
| 98 | if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) { |
| 99 | char Tmp = BIn[3]; BIn[3] = 0; // Terminate string |
| 100 | *BOut = strtol(BIn+1, 0, 16); // Convert to number |
| 101 | if (!AllowNull && !*BOut) |
| 102 | ThrowException("String literal cannot accept \\00 escape!"); |
| 103 | |
| 104 | BIn[3] = Tmp; // Restore character |
| 105 | BIn += 3; // Skip over handled chars |
| 106 | ++BOut; |
| 107 | } else { |
| 108 | *BOut++ = *BIn++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return BOut; |
| 113 | } |
| 114 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 115 | #define YY_NEVER_INTERACTIVE 1 |
| 116 | %} |
| 117 | |
| 118 | |
| 119 | |
| 120 | /* Comments start with a ; and go till end of line */ |
| 121 | Comment ;.* |
| 122 | |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 123 | /* Variable(Value) identifiers start with a % sign */ |
Chris Lattner | 9a88d27 | 2001-12-04 04:31:30 +0000 | [diff] [blame] | 124 | VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]* |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | |
| 126 | /* Label identifiers end with a colon */ |
Chris Lattner | 9a88d27 | 2001-12-04 04:31:30 +0000 | [diff] [blame] | 127 | Label [-a-zA-Z$._0-9]+: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 128 | |
| 129 | /* Quoted names can contain any character except " and \ */ |
| 130 | StringConstant \"[^\"]+\" |
| 131 | |
| 132 | |
| 133 | /* [PN]Integer: match positive and negative literal integer values that |
| 134 | * are preceeded by a '%' character. These represent unnamed variable slots. |
| 135 | */ |
| 136 | EPInteger %[0-9]+ |
| 137 | ENInteger %-[0-9]+ |
| 138 | |
| 139 | |
| 140 | /* E[PN]Integer: match positive and negative literal integer values */ |
| 141 | PInteger [0-9]+ |
| 142 | NInteger -[0-9]+ |
| 143 | |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 144 | /* FPConstant - A Floating point constant. |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 145 | */ |
Chris Lattner | 1e2c614 | 2001-11-01 22:06:08 +0000 | [diff] [blame] | 146 | FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 148 | /* HexFPConstant - Floating point constant represented in IEEE format as a |
| 149 | * hexadecimal number for when exponential notation is not precise enough. |
| 150 | */ |
| 151 | HexFPConstant 0x[0-9A-Fa-f]+ |
Chris Lattner | 3e8ba10 | 2003-04-17 22:17:32 +0000 | [diff] [blame] | 152 | |
| 153 | /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing |
| 154 | * it to deal with 64 bit numbers. |
| 155 | */ |
| 156 | HexIntConstant [us]0x[0-9A-Fa-f]+ |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | %% |
| 158 | |
| 159 | {Comment} { /* Ignore comments for now */ } |
| 160 | |
| 161 | begin { return BEGINTOK; } |
Chris Lattner | 9b02cc3 | 2002-05-03 18:23:48 +0000 | [diff] [blame] | 162 | end { return ENDTOK; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 163 | true { return TRUE; } |
| 164 | false { return FALSE; } |
| 165 | declare { return DECLARE; } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 166 | global { return GLOBAL; } |
Chris Lattner | 1781aca | 2001-09-18 04:00:54 +0000 | [diff] [blame] | 167 | constant { return CONSTANT; } |
Chris Lattner | df7306f | 2001-10-03 01:49:25 +0000 | [diff] [blame] | 168 | const { return CONST; } |
Chris Lattner | dda7196 | 2001-11-26 18:54:16 +0000 | [diff] [blame] | 169 | internal { return INTERNAL; } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 170 | linkonce { return LINKONCE; } |
| 171 | appending { return APPENDING; } |
Chris Lattner | 08c0e6a | 2002-10-06 22:45:09 +0000 | [diff] [blame] | 172 | uninitialized { return EXTERNAL; } /* Deprecated, turn into external */ |
| 173 | external { return EXTERNAL; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | implementation { return IMPLEMENTATION; } |
Chris Lattner | 8b81bf5 | 2001-07-25 22:47:46 +0000 | [diff] [blame] | 175 | \.\.\. { return DOTDOTDOT; } |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 176 | null { return NULL_TOK; } |
Chris Lattner | 5efbbc2 | 2001-10-13 06:37:14 +0000 | [diff] [blame] | 177 | to { return TO; } |
| 178 | except { return EXCEPT; } |
Chris Lattner | 699f1eb | 2002-08-14 17:12:33 +0000 | [diff] [blame] | 179 | not { return NOT; } /* Deprecated, turned into XOR */ |
Chris Lattner | b9bcbb5 | 2003-04-22 19:07:06 +0000 | [diff] [blame^] | 180 | target { return TARGET; } |
| 181 | endian { return ENDIAN; } |
| 182 | pointersize { return POINTERSIZE; } |
| 183 | little { return LITTLE; } |
| 184 | big { return BIG; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 185 | |
Chris Lattner | e1fe875 | 2001-09-07 16:32:43 +0000 | [diff] [blame] | 186 | void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; } |
| 187 | bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; } |
| 188 | sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; } |
| 189 | ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; } |
| 190 | short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; } |
| 191 | ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; } |
| 192 | int { llvmAsmlval.PrimType = Type::IntTy ; return INT; } |
| 193 | uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; } |
| 194 | long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; } |
| 195 | ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; } |
| 196 | float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; } |
| 197 | double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; } |
Chris Lattner | e1fe875 | 2001-09-07 16:32:43 +0000 | [diff] [blame] | 198 | type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; } |
Chris Lattner | e1fe875 | 2001-09-07 16:32:43 +0000 | [diff] [blame] | 199 | label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; } |
Chris Lattner | 4e4cae8 | 2002-04-04 19:22:17 +0000 | [diff] [blame] | 200 | opaque { return OPAQUE; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 202 | add { RET_TOK(BinaryOpVal, Add, ADD); } |
| 203 | sub { RET_TOK(BinaryOpVal, Sub, SUB); } |
| 204 | mul { RET_TOK(BinaryOpVal, Mul, MUL); } |
| 205 | div { RET_TOK(BinaryOpVal, Div, DIV); } |
| 206 | rem { RET_TOK(BinaryOpVal, Rem, REM); } |
Chris Lattner | 42c9e77 | 2001-10-20 09:32:59 +0000 | [diff] [blame] | 207 | and { RET_TOK(BinaryOpVal, And, AND); } |
| 208 | or { RET_TOK(BinaryOpVal, Or , OR ); } |
| 209 | xor { RET_TOK(BinaryOpVal, Xor, XOR); } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | setne { RET_TOK(BinaryOpVal, SetNE, SETNE); } |
| 211 | seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); } |
| 212 | setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); } |
| 213 | setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); } |
| 214 | setle { RET_TOK(BinaryOpVal, SetLE, SETLE); } |
| 215 | setge { RET_TOK(BinaryOpVal, SetGE, SETGE); } |
| 216 | |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 217 | phi { RET_TOK(OtherOpVal, PHINode, PHI); } |
| 218 | call { RET_TOK(OtherOpVal, Call, CALL); } |
| 219 | cast { RET_TOK(OtherOpVal, Cast, CAST); } |
| 220 | shl { RET_TOK(OtherOpVal, Shl, SHL); } |
| 221 | shr { RET_TOK(OtherOpVal, Shr, SHR); } |
| 222 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | ret { RET_TOK(TermOpVal, Ret, RET); } |
| 224 | br { RET_TOK(TermOpVal, Br, BR); } |
| 225 | switch { RET_TOK(TermOpVal, Switch, SWITCH); } |
Chris Lattner | 5efbbc2 | 2001-10-13 06:37:14 +0000 | [diff] [blame] | 226 | invoke { RET_TOK(TermOpVal, Invoke, INVOKE); } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | malloc { RET_TOK(MemOpVal, Malloc, MALLOC); } |
| 230 | alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); } |
| 231 | free { RET_TOK(MemOpVal, Free, FREE); } |
| 232 | load { RET_TOK(MemOpVal, Load, LOAD); } |
| 233 | store { RET_TOK(MemOpVal, Store, STORE); } |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 234 | getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | |
| 236 | |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 237 | {VarID} { |
| 238 | UnEscapeLexed(yytext+1); |
| 239 | llvmAsmlval.StrVal = strdup(yytext+1); // Skip % |
| 240 | return VAR_ID; |
| 241 | } |
| 242 | {Label} { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 243 | yytext[strlen(yytext)-1] = 0; // nuke colon |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 244 | UnEscapeLexed(yytext); |
| 245 | llvmAsmlval.StrVal = strdup(yytext); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 246 | return LABELSTR; |
| 247 | } |
| 248 | |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 249 | {StringConstant} { // Note that we cannot unescape a string constant here! The |
| 250 | // string constant might contain a \00 which would not be |
| 251 | // understood by the string stuff. It is valid to make a |
| 252 | // [sbyte] c"Hello World\00" constant, for example. |
| 253 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | yytext[strlen(yytext)-1] = 0; // nuke end quote |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 255 | llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 256 | return STRINGCONSTANT; |
Chris Lattner | 93750fa | 2001-07-28 17:48:55 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; } |
| 261 | {NInteger} { |
| 262 | uint64_t Val = atoull(yytext+1); |
| 263 | // +1: we have bigger negative range |
| 264 | if (Val > (uint64_t)INT64_MAX+1) |
| 265 | ThrowException("Constant too large for signed 64 bits!"); |
| 266 | llvmAsmlval.SInt64Val = -Val; |
| 267 | return ESINT64VAL; |
| 268 | } |
Chris Lattner | 3e8ba10 | 2003-04-17 22:17:32 +0000 | [diff] [blame] | 269 | {HexIntConstant} { |
| 270 | llvmAsmlval.UInt64Val = HexIntToVal(yytext+3); |
| 271 | return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL; |
| 272 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | |
| 274 | {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; } |
| 275 | {ENInteger} { |
| 276 | uint64_t Val = atoull(yytext+2); |
| 277 | // +1: we have bigger negative range |
| 278 | if (Val > (uint64_t)INT32_MAX+1) |
| 279 | ThrowException("Constant too large for signed 32 bits!"); |
| 280 | llvmAsmlval.SIntVal = -Val; |
| 281 | return SINTVAL; |
| 282 | } |
| 283 | |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 284 | {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; } |
Chris Lattner | 275da86 | 2002-04-07 08:10:41 +0000 | [diff] [blame] | 285 | {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 286 | |
| 287 | [ \t\n] { /* Ignore whitespace */ } |
Chris Lattner | e1fe875 | 2001-09-07 16:32:43 +0000 | [diff] [blame] | 288 | . { return yytext[0]; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 289 | |
| 290 | %% |