Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the bison parser for LLVM assembly languages files. |
| 4 | // |
| 5 | //===------------------------------------------------------------------------=// |
| 6 | |
| 7 | // |
| 8 | // TODO: Parse comments and add them to an internal node... so that they may |
| 9 | // be saved in the bytecode format as well as everything else. Very important |
| 10 | // for a general IR format. |
| 11 | // |
| 12 | |
| 13 | %{ |
| 14 | #include "ParserInternals.h" |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Method.h" |
| 17 | #include "llvm/SymbolTable.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Assembly/Parser.h" |
| 22 | #include "llvm/ConstantPool.h" |
| 23 | #include "llvm/iTerminators.h" |
| 24 | #include "llvm/iMemory.h" |
| 25 | #include <list> |
| 26 | #include <utility> // Get definition of pair class |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 27 | #include <algorithm> // Get definition of find_if |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | #include <stdio.h> // This embarasment is due to our flex lexer... |
| 29 | |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 30 | int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit |
| 31 | int yylex(); // declaration" of xxx warnings. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | int yyparse(); |
| 33 | |
| 34 | static Module *ParserResult; |
| 35 | const ToolCommandLine *CurOptions = 0; |
| 36 | |
| 37 | // This contains info used when building the body of a method. It is destroyed |
| 38 | // when the method is completed. |
| 39 | // |
| 40 | typedef vector<Value *> ValueList; // Numbered defs |
| 41 | static void ResolveDefinitions(vector<ValueList> &LateResolvers); |
| 42 | |
| 43 | static struct PerModuleInfo { |
| 44 | Module *CurrentModule; |
| 45 | vector<ValueList> Values; // Module level numbered definitions |
| 46 | vector<ValueList> LateResolveValues; |
| 47 | |
| 48 | void ModuleDone() { |
| 49 | // If we could not resolve some blocks at parsing time (forward branches) |
| 50 | // resolve the branches now... |
| 51 | ResolveDefinitions(LateResolveValues); |
| 52 | |
| 53 | Values.clear(); // Clear out method local definitions |
| 54 | CurrentModule = 0; |
| 55 | } |
| 56 | } CurModule; |
| 57 | |
| 58 | static struct PerMethodInfo { |
| 59 | Method *CurrentMethod; // Pointer to current method being created |
| 60 | |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 61 | vector<ValueList> Values; // Keep track of numbered definitions |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | vector<ValueList> LateResolveValues; |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 63 | bool isDeclare; // Is this method a forward declararation? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | |
| 65 | inline PerMethodInfo() { |
| 66 | CurrentMethod = 0; |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 67 | isDeclare = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | inline ~PerMethodInfo() {} |
| 71 | |
| 72 | inline void MethodStart(Method *M) { |
| 73 | CurrentMethod = M; |
| 74 | } |
| 75 | |
| 76 | void MethodDone() { |
| 77 | // If we could not resolve some blocks at parsing time (forward branches) |
| 78 | // resolve the branches now... |
| 79 | ResolveDefinitions(LateResolveValues); |
| 80 | |
| 81 | Values.clear(); // Clear out method local definitions |
| 82 | CurrentMethod = 0; |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 83 | isDeclare = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 84 | } |
| 85 | } CurMeth; // Info for the current method... |
| 86 | |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | // Code to handle definitions of all the types |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | |
| 92 | static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) { |
| 93 | if (!D->hasName()) { // Is this a numbered definition? |
| 94 | unsigned type = D->getType()->getUniqueID(); |
| 95 | if (ValueTab.size() <= type) |
| 96 | ValueTab.resize(type+1, ValueList()); |
| 97 | //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D); |
| 98 | ValueTab[type].push_back(D); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static Value *getVal(const Type *Type, ValID &D, |
| 103 | bool DoNotImprovise = false) { |
| 104 | switch (D.Type) { |
| 105 | case 0: { // Is it a numbered definition? |
| 106 | unsigned type = Type->getUniqueID(); |
| 107 | unsigned Num = (unsigned)D.Num; |
| 108 | |
| 109 | // Module constants occupy the lowest numbered slots... |
| 110 | if (type < CurModule.Values.size()) { |
| 111 | if (Num < CurModule.Values[type].size()) |
| 112 | return CurModule.Values[type][Num]; |
| 113 | |
| 114 | Num -= CurModule.Values[type].size(); |
| 115 | } |
| 116 | |
| 117 | // Make sure that our type is within bounds |
| 118 | if (CurMeth.Values.size() <= type) |
| 119 | break; |
| 120 | |
| 121 | // Check that the number is within bounds... |
| 122 | if (CurMeth.Values[type].size() <= Num) |
| 123 | break; |
| 124 | |
| 125 | return CurMeth.Values[type][Num]; |
| 126 | } |
| 127 | case 1: { // Is it a named definition? |
| 128 | string Name(D.Name); |
| 129 | SymbolTable *SymTab = 0; |
| 130 | if (CurMeth.CurrentMethod) |
| 131 | SymTab = CurMeth.CurrentMethod->getSymbolTable(); |
| 132 | Value *N = SymTab ? SymTab->lookup(Type, Name) : 0; |
| 133 | |
| 134 | if (N == 0) { |
| 135 | SymTab = CurModule.CurrentModule->getSymbolTable(); |
| 136 | if (SymTab) |
| 137 | N = SymTab->lookup(Type, Name); |
| 138 | if (N == 0) break; |
| 139 | } |
| 140 | |
| 141 | D.destroy(); // Free old strdup'd memory... |
| 142 | return N; |
| 143 | } |
| 144 | |
| 145 | case 2: // Is it a constant pool reference?? |
| 146 | case 3: // Is it an unsigned const pool reference? |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 147 | case 4: // Is it a string const pool reference? |
| 148 | case 5:{ // Is it a floating point const pool reference? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 149 | ConstPoolVal *CPV = 0; |
| 150 | |
| 151 | // Check to make sure that "Type" is an integral type, and that our |
| 152 | // value will fit into the specified type... |
| 153 | switch (D.Type) { |
| 154 | case 2: |
| 155 | if (Type == Type::BoolTy) { // Special handling for boolean data |
| 156 | CPV = new ConstPoolBool(D.ConstPool64 != 0); |
| 157 | } else { |
| 158 | if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 159 | ThrowException("Symbolic constant pool value '" + |
| 160 | itostr(D.ConstPool64) + "' is invalid for type '" + |
| 161 | Type->getName() + "'!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | CPV = new ConstPoolSInt(Type, D.ConstPool64); |
| 163 | } |
| 164 | break; |
| 165 | case 3: |
| 166 | if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) { |
| 167 | if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) { |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 168 | ThrowException("Integral constant pool reference is invalid!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 169 | } else { // This is really a signed reference. Transmogrify. |
| 170 | CPV = new ConstPoolSInt(Type, D.ConstPool64); |
| 171 | } |
| 172 | } else { |
| 173 | CPV = new ConstPoolUInt(Type, D.UConstPool64); |
| 174 | } |
| 175 | break; |
| 176 | case 4: |
| 177 | cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; |
| 178 | abort(); |
| 179 | //CPV = new ConstPoolString(D.Name); |
| 180 | D.destroy(); // Free the string memory |
| 181 | break; |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 182 | case 5: |
| 183 | if (!ConstPoolFP::isValueValidForType(Type, D.ConstPoolFP)) |
| 184 | ThrowException("FP constant invalid for type!!"); |
| 185 | else |
| 186 | CPV = new ConstPoolFP(Type, D.ConstPoolFP); |
| 187 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 188 | } |
| 189 | assert(CPV && "How did we escape creating a constant??"); |
| 190 | |
| 191 | // Scan through the constant table and see if we already have loaded this |
| 192 | // constant. |
| 193 | // |
| 194 | ConstantPool &CP = CurMeth.CurrentMethod ? |
| 195 | CurMeth.CurrentMethod->getConstantPool() : |
| 196 | CurModule.CurrentModule->getConstantPool(); |
| 197 | ConstPoolVal *C = CP.find(CPV); // Already have this constant? |
| 198 | if (C) { |
| 199 | delete CPV; // Didn't need this after all, oh well. |
| 200 | return C; // Yup, we already have one, recycle it! |
| 201 | } |
| 202 | CP.insert(CPV); |
| 203 | |
| 204 | // Success, everything is kosher. Lets go! |
| 205 | return CPV; |
| 206 | } // End of case 2,3,4 |
| 207 | } // End of switch |
| 208 | |
| 209 | |
| 210 | // If we reached here, we referenced either a symbol that we don't know about |
| 211 | // or an id number that hasn't been read yet. We may be referencing something |
| 212 | // forward, so just create an entry to be resolved later and get to it... |
| 213 | // |
| 214 | if (DoNotImprovise) return 0; // Do we just want a null to be returned? |
| 215 | |
| 216 | // TODO: Attempt to coallecse nodes that are the same with previous ones. |
| 217 | Value *d = 0; |
| 218 | switch (Type->getPrimitiveID()) { |
| 219 | case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break; |
| 220 | case Type::MethodTyID: |
| 221 | d = new MethPlaceHolder(Type, D); |
| 222 | InsertValue(d, CurModule.LateResolveValues); |
| 223 | return d; |
| 224 | //case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break; |
| 225 | default: d = new DefPlaceHolder(Type, D); break; |
| 226 | } |
| 227 | |
| 228 | assert(d != 0 && "How did we not make something?"); |
| 229 | InsertValue(d, CurMeth.LateResolveValues); |
| 230 | return d; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | //===----------------------------------------------------------------------===// |
| 235 | // Code to handle forward references in instructions |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // |
| 238 | // This code handles the late binding needed with statements that reference |
| 239 | // values not defined yet... for example, a forward branch, or the PHI node for |
| 240 | // a loop body. |
| 241 | // |
| 242 | // This keeps a table (CurMeth.LateResolveValues) of all such forward references |
| 243 | // and back patchs after we are done. |
| 244 | // |
| 245 | |
| 246 | // ResolveDefinitions - If we could not resolve some defs at parsing |
| 247 | // time (forward branches, phi functions for loops, etc...) resolve the |
| 248 | // defs now... |
| 249 | // |
| 250 | static void ResolveDefinitions(vector<ValueList> &LateResolvers) { |
| 251 | // Loop over LateResolveDefs fixing up stuff that couldn't be resolved |
| 252 | for (unsigned ty = 0; ty < LateResolvers.size(); ty++) { |
| 253 | while (!LateResolvers[ty].empty()) { |
| 254 | Value *V = LateResolvers[ty].back(); |
| 255 | LateResolvers[ty].pop_back(); |
| 256 | ValID &DID = getValIDFromPlaceHolder(V); |
| 257 | |
| 258 | Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true); |
| 259 | |
| 260 | if (TheRealValue == 0 && DID.Type == 1) |
| 261 | ThrowException("Reference to an invalid definition: '" +DID.getName() + |
| 262 | "' of type '" + V->getType()->getName() + "'"); |
| 263 | else if (TheRealValue == 0) |
| 264 | ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+ |
| 265 | " of type '" + V->getType()->getName() + "'"); |
| 266 | |
| 267 | V->replaceAllUsesWith(TheRealValue); |
| 268 | assert(V->use_empty()); |
| 269 | delete V; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | LateResolvers.clear(); |
| 274 | } |
| 275 | |
| 276 | // addConstValToConstantPool - This code is used to insert a constant into the |
| 277 | // current constant pool. This is designed to make maximal (but not more than |
| 278 | // possible) reuse (merging) of constants in the constant pool. This means that |
| 279 | // multiple references to %4, for example will all get merged. |
| 280 | // |
| 281 | static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { |
| 282 | vector<ValueList> &ValTab = CurMeth.CurrentMethod ? |
| 283 | CurMeth.Values : CurModule.Values; |
| 284 | ConstantPool &CP = CurMeth.CurrentMethod ? |
| 285 | CurMeth.CurrentMethod->getConstantPool() : |
| 286 | CurModule.CurrentModule->getConstantPool(); |
| 287 | |
| 288 | if (ConstPoolVal *CPV = CP.find(C)) { |
| 289 | // Constant already in constant pool. Try to merge the two constants |
| 290 | if (CPV->hasName() && !C->hasName()) { |
| 291 | // Merge the two values, we inherit the existing CPV's name. |
| 292 | // InsertValue requires that the value have no name to insert correctly |
| 293 | // (because we want to fill the slot this constant would have filled) |
| 294 | // |
| 295 | string Name = CPV->getName(); |
| 296 | CPV->setName(""); |
| 297 | InsertValue(CPV, ValTab); |
| 298 | CPV->setName(Name); |
| 299 | delete C; |
| 300 | return CPV; |
| 301 | } else if (!CPV->hasName() && C->hasName()) { |
| 302 | // If we have a name on this value and there isn't one in the const |
| 303 | // pool val already, propogate it. |
| 304 | // |
| 305 | CPV->setName(C->getName()); |
| 306 | delete C; // Sorry, you're toast |
| 307 | return CPV; |
| 308 | } else if (CPV->hasName() && C->hasName()) { |
| 309 | // Both values have distinct names. We cannot merge them. |
| 310 | CP.insert(C); |
| 311 | InsertValue(C, ValTab); |
| 312 | return C; |
| 313 | } else if (!CPV->hasName() && !C->hasName()) { |
| 314 | // Neither value has a name, trivially merge them. |
| 315 | InsertValue(CPV, ValTab); |
| 316 | delete C; |
| 317 | return CPV; |
| 318 | } |
| 319 | |
| 320 | assert(0 && "Not reached!"); |
| 321 | return 0; |
| 322 | } else { // No duplication of value. |
| 323 | CP.insert(C); |
| 324 | InsertValue(C, ValTab); |
| 325 | return C; |
| 326 | } |
| 327 | } |
| 328 | |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 329 | |
| 330 | struct EqualsType { |
| 331 | const Type *T; |
| 332 | inline EqualsType(const Type *t) { T = t; } |
| 333 | inline bool operator()(const ConstPoolVal *CPV) const { |
| 334 | return static_cast<const ConstPoolType*>(CPV)->getValue() == T; |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | |
| 339 | // checkNewType - We have to be careful to add all types referenced by the |
| 340 | // program to the constant pool of the method or module. Because of this, we |
| 341 | // often want to check to make sure that types used are in the constant pool, |
| 342 | // and add them if they aren't. That's what this function does. |
| 343 | // |
| 344 | static const Type *checkNewType(const Type *Ty) { |
| 345 | ConstantPool &CP = CurMeth.CurrentMethod ? |
| 346 | CurMeth.CurrentMethod->getConstantPool() : |
| 347 | CurModule.CurrentModule->getConstantPool(); |
| 348 | |
Chris Lattner | 698b56e | 2001-07-20 19:15:08 +0000 | [diff] [blame^] | 349 | // TODO: This should use ConstantPool::ensureTypeAvailable |
| 350 | |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 351 | // Get the type type plane... |
| 352 | ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy); |
| 353 | ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(), |
| 354 | EqualsType(Ty)); |
| 355 | if (PI == P.end()) { |
| 356 | vector<ValueList> &ValTab = CurMeth.CurrentMethod ? |
| 357 | CurMeth.Values : CurModule.Values; |
| 358 | ConstPoolVal *CPT = new ConstPoolType(Ty); |
| 359 | CP.insert(CPT); |
| 360 | InsertValue(CPT, ValTab); |
| 361 | } |
| 362 | return Ty; |
| 363 | } |
| 364 | |
| 365 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // RunVMAsmParser - Define an interface to this parser |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | // |
| 370 | Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) { |
| 371 | llvmAsmin = F; |
| 372 | CurOptions = &Opts; |
| 373 | llvmAsmlineno = 1; // Reset the current line number... |
| 374 | |
| 375 | CurModule.CurrentModule = new Module(); // Allocate a new module to read |
| 376 | yyparse(); // Parse the file. |
| 377 | Module *Result = ParserResult; |
| 378 | CurOptions = 0; |
| 379 | llvmAsmin = stdin; // F is about to go away, don't use it anymore... |
| 380 | ParserResult = 0; |
| 381 | |
| 382 | return Result; |
| 383 | } |
| 384 | |
| 385 | %} |
| 386 | |
| 387 | %union { |
| 388 | Module *ModuleVal; |
| 389 | Method *MethodVal; |
| 390 | MethodArgument *MethArgVal; |
| 391 | BasicBlock *BasicBlockVal; |
| 392 | TerminatorInst *TermInstVal; |
| 393 | Instruction *InstVal; |
| 394 | ConstPoolVal *ConstVal; |
| 395 | const Type *TypeVal; |
| 396 | |
| 397 | list<MethodArgument*> *MethodArgList; |
| 398 | list<Value*> *ValueList; |
| 399 | list<const Type*> *TypeList; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 400 | list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 401 | list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable; |
| 402 | vector<ConstPoolVal*> *ConstVector; |
| 403 | |
| 404 | int64_t SInt64Val; |
| 405 | uint64_t UInt64Val; |
| 406 | int SIntVal; |
| 407 | unsigned UIntVal; |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 408 | double FPVal; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 409 | |
| 410 | char *StrVal; // This memory is allocated by strdup! |
| 411 | ValID ValIDVal; // May contain memory allocated by strdup |
| 412 | |
| 413 | Instruction::UnaryOps UnaryOpVal; |
| 414 | Instruction::BinaryOps BinaryOpVal; |
| 415 | Instruction::TermOps TermOpVal; |
| 416 | Instruction::MemoryOps MemOpVal; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 417 | Instruction::OtherOps OtherOpVal; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | %type <ModuleVal> Module MethodList |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 421 | %type <MethodVal> Method MethodProto MethodHeader BasicBlockList |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 422 | %type <BasicBlockVal> BasicBlock InstructionList |
| 423 | %type <TermInstVal> BBTerminatorInst |
| 424 | %type <InstVal> Inst InstVal MemoryInst |
| 425 | %type <ConstVal> ConstVal |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 426 | %type <ConstVector> ConstVector UByteList |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 427 | %type <MethodArgList> ArgList ArgListH |
| 428 | %type <MethArgVal> ArgVal |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 429 | %type <PHIList> PHIList |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 430 | %type <ValueList> ValueRefList ValueRefListE // For call param lists |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 431 | %type <TypeList> TypeList |
| 432 | %type <JumpTable> JumpTable |
| 433 | |
| 434 | %type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB |
| 435 | |
| 436 | // Tokens and types for handling constant integer values |
| 437 | // |
| 438 | // ESINT64VAL - A negative number within long long range |
| 439 | %token <SInt64Val> ESINT64VAL |
| 440 | |
| 441 | // EUINT64VAL - A positive number within uns. long long range |
| 442 | %token <UInt64Val> EUINT64VAL |
| 443 | %type <SInt64Val> EINT64VAL |
| 444 | |
| 445 | %token <SIntVal> SINTVAL // Signed 32 bit ints... |
| 446 | %token <UIntVal> UINTVAL // Unsigned 32 bit ints... |
| 447 | %type <SIntVal> INTVAL |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 448 | %token <FPVal> FPVAL // Float or Double constant |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 449 | |
| 450 | // Built in types... |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 451 | %type <TypeVal> Types TypesV SIntType UIntType IntType FPType |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 452 | %token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG |
| 453 | %token <TypeVal> FLOAT DOUBLE STRING TYPE LABEL |
| 454 | |
| 455 | %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT |
| 456 | %type <StrVal> OptVAR_ID OptAssign |
| 457 | |
| 458 | |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 459 | %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 460 | |
| 461 | // Basic Block Terminating Operators |
| 462 | %token <TermOpVal> RET BR SWITCH |
| 463 | |
| 464 | // Unary Operators |
| 465 | %type <UnaryOpVal> UnaryOps // all the unary operators |
Chris Lattner | 71496b3 | 2001-07-08 19:03:27 +0000 | [diff] [blame] | 466 | %token <UnaryOpVal> NOT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 467 | |
| 468 | // Binary Operators |
| 469 | %type <BinaryOpVal> BinaryOps // all the binary operators |
| 470 | %token <BinaryOpVal> ADD SUB MUL DIV REM |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 471 | %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 472 | |
| 473 | // Memory Instructions |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 474 | %token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 476 | // Other Operators |
| 477 | %type <OtherOpVal> ShiftOps |
| 478 | %token <OtherOpVal> PHI CALL CAST SHL SHR |
| 479 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 480 | %start Module |
| 481 | %% |
| 482 | |
| 483 | // Handle constant integer size restriction and conversion... |
| 484 | // |
| 485 | |
| 486 | INTVAL : SINTVAL |
| 487 | INTVAL : UINTVAL { |
| 488 | if ($1 > (uint32_t)INT32_MAX) // Outside of my range! |
| 489 | ThrowException("Value too large for type!"); |
| 490 | $$ = (int32_t)$1; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | EINT64VAL : ESINT64VAL // These have same type and can't cause problems... |
| 495 | EINT64VAL : EUINT64VAL { |
| 496 | if ($1 > (uint64_t)INT64_MAX) // Outside of my range! |
| 497 | ThrowException("Value too large for type!"); |
| 498 | $$ = (int64_t)$1; |
| 499 | } |
| 500 | |
| 501 | // Types includes all predefined types... except void, because you can't do |
| 502 | // anything with it except for certain specific things... |
| 503 | // |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 504 | // User defined types are added later... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 505 | // |
| 506 | Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT |
| 507 | Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL |
| 508 | |
| 509 | // TypesV includes all of 'Types', but it also includes the void type. |
| 510 | TypesV : Types | VOID |
| 511 | |
| 512 | // Operations that are notably excluded from this list include: |
| 513 | // RET, BR, & SWITCH because they end basic blocks and are treated specially. |
| 514 | // |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 515 | UnaryOps : NOT |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 516 | BinaryOps : ADD | SUB | MUL | DIV | REM |
| 517 | BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 518 | ShiftOps : SHL | SHR |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 519 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 520 | // These are some types that allow classification if we only want a particular |
| 521 | // thing... for example, only a signed, unsigned, or integral type. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 522 | SIntType : LONG | INT | SHORT | SBYTE |
| 523 | UIntType : ULONG | UINT | USHORT | UBYTE |
| 524 | IntType : SIntType | UIntType |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 525 | FPType : FLOAT | DOUBLE |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 526 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 527 | // OptAssign - Value producing statements have an optional assignment component |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 528 | OptAssign : VAR_ID '=' { |
| 529 | $$ = $1; |
| 530 | } |
| 531 | | /*empty*/ { |
| 532 | $$ = 0; |
| 533 | } |
| 534 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 535 | // ConstVal - The various declarations that go into the constant pool. This |
| 536 | // includes all forward declarations of types, constants, and functions. |
| 537 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 538 | ConstVal : SIntType EINT64VAL { // integral constants |
| 539 | if (!ConstPoolSInt::isValueValidForType($1, $2)) |
| 540 | ThrowException("Constant value doesn't fit in type!"); |
| 541 | $$ = new ConstPoolSInt($1, $2); |
| 542 | } |
| 543 | | UIntType EUINT64VAL { // integral constants |
| 544 | if (!ConstPoolUInt::isValueValidForType($1, $2)) |
| 545 | ThrowException("Constant value doesn't fit in type!"); |
| 546 | $$ = new ConstPoolUInt($1, $2); |
| 547 | } |
| 548 | | BOOL TRUE { // Boolean constants |
| 549 | $$ = new ConstPoolBool(true); |
| 550 | } |
| 551 | | BOOL FALSE { // Boolean constants |
| 552 | $$ = new ConstPoolBool(false); |
| 553 | } |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 554 | | FPType FPVAL { // Float & Double constants |
| 555 | $$ = new ConstPoolFP($1, $2); |
| 556 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 557 | | STRING STRINGCONSTANT { // String constants |
| 558 | cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; |
| 559 | abort(); |
| 560 | //$$ = new ConstPoolString($2); |
| 561 | free($2); |
| 562 | } |
| 563 | | TYPE Types { // Type constants |
| 564 | $$ = new ConstPoolType($2); |
| 565 | } |
| 566 | | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant |
| 567 | // Verify all elements are correct type! |
| 568 | const ArrayType *AT = ArrayType::getArrayType($2); |
| 569 | for (unsigned i = 0; i < $5->size(); i++) { |
| 570 | if ($2 != (*$5)[i]->getType()) |
| 571 | ThrowException("Element #" + utostr(i) + " is not of type '" + |
| 572 | $2->getName() + "' as required!\nIt is of type '" + |
| 573 | (*$5)[i]->getType()->getName() + "'."); |
| 574 | } |
| 575 | |
| 576 | $$ = new ConstPoolArray(AT, *$5); |
| 577 | delete $5; |
| 578 | } |
| 579 | | '[' Types ']' '[' ']' { // Empty array constant |
| 580 | vector<ConstPoolVal*> Empty; |
| 581 | $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty); |
| 582 | } |
| 583 | | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' { |
| 584 | // Verify all elements are correct type! |
| 585 | const ArrayType *AT = ArrayType::getArrayType($4, (int)$2); |
| 586 | if ($2 != $7->size()) |
| 587 | ThrowException("Type mismatch: constant sized array initialized with " + |
| 588 | utostr($7->size()) + " arguments, but has size of " + |
| 589 | itostr((int)$2) + "!"); |
| 590 | |
| 591 | for (unsigned i = 0; i < $7->size(); i++) { |
| 592 | if ($4 != (*$7)[i]->getType()) |
| 593 | ThrowException("Element #" + utostr(i) + " is not of type '" + |
| 594 | $4->getName() + "' as required!\nIt is of type '" + |
| 595 | (*$7)[i]->getType()->getName() + "'."); |
| 596 | } |
| 597 | |
| 598 | $$ = new ConstPoolArray(AT, *$7); |
| 599 | delete $7; |
| 600 | } |
| 601 | | '[' EUINT64VAL 'x' Types ']' '[' ']' { |
| 602 | if ($2 != 0) |
| 603 | ThrowException("Type mismatch: constant sized array initialized with 0" |
| 604 | " arguments, but has size of " + itostr((int)$2) + "!"); |
| 605 | vector<ConstPoolVal*> Empty; |
| 606 | $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty); |
| 607 | } |
| 608 | | '{' TypeList '}' '{' ConstVector '}' { |
| 609 | StructType::ElementTypes Types($2->begin(), $2->end()); |
| 610 | delete $2; |
| 611 | |
| 612 | const StructType *St = StructType::getStructType(Types); |
| 613 | $$ = new ConstPoolStruct(St, *$5); |
| 614 | delete $5; |
| 615 | } |
| 616 | | '{' '}' '{' '}' { |
| 617 | const StructType *St = |
| 618 | StructType::getStructType(StructType::ElementTypes()); |
| 619 | vector<ConstPoolVal*> Empty; |
| 620 | $$ = new ConstPoolStruct(St, Empty); |
| 621 | } |
| 622 | /* |
| 623 | | Types '*' ConstVal { |
| 624 | assert(0); |
| 625 | $$ = 0; |
| 626 | } |
| 627 | */ |
| 628 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 629 | // ConstVector - A list of comma seperated constants. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 630 | ConstVector : ConstVector ',' ConstVal { |
| 631 | ($$ = $1)->push_back(addConstValToConstantPool($3)); |
| 632 | } |
| 633 | | ConstVal { |
| 634 | $$ = new vector<ConstPoolVal*>(); |
| 635 | $$->push_back(addConstValToConstantPool($1)); |
| 636 | } |
| 637 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 638 | //ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' { |
| 639 | // } |
| 640 | //ExternVarDecl : |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 641 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 642 | // ConstPool - Constants with optional names assigned to them. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | ConstPool : ConstPool OptAssign ConstVal { |
| 644 | if ($2) { |
| 645 | $3->setName($2); |
| 646 | free($2); |
| 647 | } |
| 648 | |
| 649 | addConstValToConstantPool($3); |
| 650 | } |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 651 | /* |
| 652 | | ConstPool OptAssign GlobalDecl { // Global declarations appear in CP |
| 653 | if ($2) { |
| 654 | $3->setName($2); |
| 655 | free($2); |
| 656 | } |
| 657 | //CurModule.CurrentModule-> |
| 658 | } |
| 659 | */ |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 660 | | /* empty: end of list */ { |
| 661 | } |
| 662 | |
| 663 | |
| 664 | //===----------------------------------------------------------------------===// |
| 665 | // Rules to match Modules |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
| 668 | // Module rule: Capture the result of parsing the whole file into a result |
| 669 | // variable... |
| 670 | // |
| 671 | Module : MethodList { |
| 672 | $$ = ParserResult = $1; |
| 673 | CurModule.ModuleDone(); |
| 674 | } |
| 675 | |
Chris Lattner | e98dda6 | 2001-07-14 06:10:16 +0000 | [diff] [blame] | 676 | // MethodList - A list of methods, preceeded by a constant pool. |
| 677 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 678 | MethodList : MethodList Method { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 679 | $$ = $1; |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 680 | if (!$2->getParent()) |
| 681 | $1->getMethodList().push_back($2); |
| 682 | CurMeth.MethodDone(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 683 | } |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 684 | | MethodList MethodProto { |
| 685 | $$ = $1; |
| 686 | if (!$2->getParent()) |
| 687 | $1->getMethodList().push_back($2); |
| 688 | CurMeth.MethodDone(); |
| 689 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 690 | | ConstPool IMPLEMENTATION { |
| 691 | $$ = CurModule.CurrentModule; |
| 692 | } |
| 693 | |
| 694 | |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | // Rules to match Method Headers |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | |
| 699 | OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; } |
| 700 | |
| 701 | ArgVal : Types OptVAR_ID { |
| 702 | $$ = new MethodArgument($1); |
| 703 | if ($2) { // Was the argument named? |
| 704 | $$->setName($2); |
| 705 | free($2); // The string was strdup'd, so free it now. |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | ArgListH : ArgVal ',' ArgListH { |
| 710 | $$ = $3; |
| 711 | $3->push_front($1); |
| 712 | } |
| 713 | | ArgVal { |
| 714 | $$ = new list<MethodArgument*>(); |
| 715 | $$->push_front($1); |
| 716 | } |
| 717 | |
| 718 | ArgList : ArgListH { |
| 719 | $$ = $1; |
| 720 | } |
| 721 | | /* empty */ { |
| 722 | $$ = 0; |
| 723 | } |
| 724 | |
| 725 | MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' { |
| 726 | MethodType::ParamTypes ParamTypeList; |
| 727 | if ($4) |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 728 | for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 729 | ParamTypeList.push_back((*I)->getType()); |
| 730 | |
| 731 | const MethodType *MT = MethodType::getMethodType($1, ParamTypeList); |
| 732 | |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 733 | Method *M = 0; |
| 734 | if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) { |
| 735 | if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab? |
| 736 | M = V->castMethodAsserting(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 737 | |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 738 | // Yes it is. If this is the case, either we need to be a forward decl, |
| 739 | // or it needs to be. |
| 740 | if (!CurMeth.isDeclare && !M->isExternal()) |
| 741 | ThrowException("Redefinition of method '" + string($2) + "'!"); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | if (M == 0) { // Not already defined? |
| 746 | M = new Method(MT, $2); |
| 747 | InsertValue(M, CurModule.Values); |
| 748 | } |
| 749 | |
| 750 | free($2); // Free strdup'd memory! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 751 | |
| 752 | CurMeth.MethodStart(M); |
| 753 | |
| 754 | // Add all of the arguments we parsed to the method... |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 755 | if ($4 && !CurMeth.isDeclare) { // Is null if empty... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 756 | Method::ArgumentListType &ArgList = M->getArgumentList(); |
| 757 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 758 | for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 759 | InsertValue(*I); |
| 760 | ArgList.push_back(*I); |
| 761 | } |
| 762 | delete $4; // We're now done with the argument list |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | MethodHeader : MethodHeaderH ConstPool BEGINTOK { |
| 767 | $$ = CurMeth.CurrentMethod; |
| 768 | } |
| 769 | |
| 770 | Method : BasicBlockList END { |
| 771 | $$ = $1; |
| 772 | } |
| 773 | |
Chris Lattner | e181564 | 2001-07-15 06:35:53 +0000 | [diff] [blame] | 774 | MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH { |
| 775 | $$ = CurMeth.CurrentMethod; |
| 776 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 777 | |
| 778 | //===----------------------------------------------------------------------===// |
| 779 | // Rules to match Basic Blocks |
| 780 | //===----------------------------------------------------------------------===// |
| 781 | |
| 782 | ConstValueRef : ESINT64VAL { // A reference to a direct constant |
| 783 | $$ = ValID::create($1); |
| 784 | } |
| 785 | | EUINT64VAL { |
| 786 | $$ = ValID::create($1); |
| 787 | } |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 788 | | FPVAL { // Perhaps it's an FP constant? |
| 789 | $$ = ValID::create($1); |
| 790 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 791 | | TRUE { |
| 792 | $$ = ValID::create((int64_t)1); |
| 793 | } |
| 794 | | FALSE { |
| 795 | $$ = ValID::create((int64_t)0); |
| 796 | } |
| 797 | | STRINGCONSTANT { // Quoted strings work too... especially for methods |
| 798 | $$ = ValID::create_conststr($1); |
| 799 | } |
| 800 | |
| 801 | // ValueRef - A reference to a definition... |
| 802 | ValueRef : INTVAL { // Is it an integer reference...? |
| 803 | $$ = ValID::create($1); |
| 804 | } |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 805 | | VAR_ID { // Is it a named reference...? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 806 | $$ = ValID::create($1); |
| 807 | } |
| 808 | | ConstValueRef { |
| 809 | $$ = $1; |
| 810 | } |
| 811 | |
| 812 | // The user may refer to a user defined type by its typeplane... check for this |
| 813 | // now... |
| 814 | // |
| 815 | Types : ValueRef { |
| 816 | Value *D = getVal(Type::TypeTy, $1, true); |
| 817 | if (D == 0) ThrowException("Invalid user defined type: " + $1.getName()); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 818 | |
| 819 | // User defined type not in const pool! |
| 820 | ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 821 | $$ = CPT->getValue(); |
| 822 | } |
| 823 | | TypesV '(' TypeList ')' { // Method derived type? |
| 824 | MethodType::ParamTypes Params($3->begin(), $3->end()); |
| 825 | delete $3; |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 826 | $$ = checkNewType(MethodType::getMethodType($1, Params)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 827 | } |
| 828 | | TypesV '(' ')' { // Method derived type? |
| 829 | MethodType::ParamTypes Params; // Empty list |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 830 | $$ = checkNewType(MethodType::getMethodType($1, Params)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 831 | } |
| 832 | | '[' Types ']' { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 833 | $$ = checkNewType(ArrayType::getArrayType($2)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 834 | } |
| 835 | | '[' EUINT64VAL 'x' Types ']' { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 836 | $$ = checkNewType(ArrayType::getArrayType($4, (int)$2)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 837 | } |
| 838 | | '{' TypeList '}' { |
| 839 | StructType::ElementTypes Elements($2->begin(), $2->end()); |
| 840 | delete $2; |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 841 | $$ = checkNewType(StructType::getStructType(Elements)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 842 | } |
| 843 | | '{' '}' { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 844 | $$ = checkNewType(StructType::getStructType(StructType::ElementTypes())); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 845 | } |
| 846 | | Types '*' { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 847 | $$ = checkNewType(PointerType::getPointerType($1)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | |
| 851 | TypeList : Types { |
| 852 | $$ = new list<const Type*>(); |
| 853 | $$->push_back($1); |
| 854 | } |
| 855 | | TypeList ',' Types { |
| 856 | ($$=$1)->push_back($3); |
| 857 | } |
| 858 | |
| 859 | |
| 860 | BasicBlockList : BasicBlockList BasicBlock { |
| 861 | $1->getBasicBlocks().push_back($2); |
| 862 | $$ = $1; |
| 863 | } |
| 864 | | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks |
| 865 | $$ = $1; // in them... |
| 866 | $1->getBasicBlocks().push_back($2); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | // Basic blocks are terminated by branching instructions: |
| 871 | // br, br/cc, switch, ret |
| 872 | // |
| 873 | BasicBlock : InstructionList BBTerminatorInst { |
| 874 | $1->getInstList().push_back($2); |
| 875 | InsertValue($1); |
| 876 | $$ = $1; |
| 877 | } |
| 878 | | LABELSTR InstructionList BBTerminatorInst { |
| 879 | $2->getInstList().push_back($3); |
| 880 | $2->setName($1); |
| 881 | free($1); // Free the strdup'd memory... |
| 882 | |
| 883 | InsertValue($2); |
| 884 | $$ = $2; |
| 885 | } |
| 886 | |
| 887 | InstructionList : InstructionList Inst { |
| 888 | $1->getInstList().push_back($2); |
| 889 | $$ = $1; |
| 890 | } |
| 891 | | /* empty */ { |
| 892 | $$ = new BasicBlock(); |
| 893 | } |
| 894 | |
| 895 | BBTerminatorInst : RET Types ValueRef { // Return with a result... |
| 896 | $$ = new ReturnInst(getVal($2, $3)); |
| 897 | } |
| 898 | | RET VOID { // Return with no result... |
| 899 | $$ = new ReturnInst(); |
| 900 | } |
| 901 | | BR LABEL ValueRef { // Unconditional Branch... |
| 902 | $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3)); |
| 903 | } // Conditional Branch... |
| 904 | | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef { |
| 905 | $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6), |
| 906 | (BasicBlock*)getVal(Type::LabelTy, $9), |
| 907 | getVal(Type::BoolTy, $3)); |
| 908 | } |
| 909 | | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' { |
| 910 | SwitchInst *S = new SwitchInst(getVal($2, $3), |
| 911 | (BasicBlock*)getVal(Type::LabelTy, $6)); |
| 912 | $$ = S; |
| 913 | |
| 914 | list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(), |
| 915 | end = $8->end(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 916 | for (; I != end; ++I) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 917 | S->dest_push_back(I->first, I->second); |
| 918 | } |
| 919 | |
| 920 | JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef { |
| 921 | $$ = $1; |
| 922 | ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true); |
| 923 | if (V == 0) |
| 924 | ThrowException("May only switch on a constant pool value!"); |
| 925 | |
| 926 | $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6))); |
| 927 | } |
| 928 | | IntType ConstValueRef ',' LABEL ValueRef { |
| 929 | $$ = new list<pair<ConstPoolVal*, BasicBlock*> >(); |
| 930 | ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true); |
| 931 | |
| 932 | if (V == 0) |
| 933 | ThrowException("May only switch on a constant pool value!"); |
| 934 | |
| 935 | $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5))); |
| 936 | } |
| 937 | |
| 938 | Inst : OptAssign InstVal { |
| 939 | if ($1) // Is this definition named?? |
| 940 | $2->setName($1); // if so, assign the name... |
| 941 | |
| 942 | InsertValue($2); |
| 943 | $$ = $2; |
| 944 | } |
| 945 | |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 946 | PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes |
| 947 | $$ = new list<pair<Value*, BasicBlock*> >(); |
| 948 | $$->push_back(make_pair(getVal($1, $3), |
| 949 | (BasicBlock*)getVal(Type::LabelTy, $5))); |
| 950 | } |
| 951 | | PHIList ',' '[' ValueRef ',' ValueRef ']' { |
| 952 | $$ = $1; |
| 953 | $1->push_back(make_pair(getVal($1->front().first->getType(), $4), |
| 954 | (BasicBlock*)getVal(Type::LabelTy, $6))); |
| 955 | } |
| 956 | |
| 957 | |
| 958 | ValueRefList : Types ValueRef { // Used for call statements... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 959 | $$ = new list<Value*>(); |
| 960 | $$->push_back(getVal($1, $2)); |
| 961 | } |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 962 | | ValueRefList ',' Types ValueRef { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 963 | $$ = $1; |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 964 | $1->push_back(getVal($3, $4)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | // ValueRefListE - Just like ValueRefList, except that it may also be empty! |
| 968 | ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; } |
| 969 | |
| 970 | InstVal : BinaryOps Types ValueRef ',' ValueRef { |
Chris Lattner | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 971 | $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 972 | if ($$ == 0) |
| 973 | ThrowException("binary operator returned null!"); |
| 974 | } |
| 975 | | UnaryOps Types ValueRef { |
Chris Lattner | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 976 | $$ = UnaryOperator::create($1, getVal($2, $3)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 977 | if ($$ == 0) |
| 978 | ThrowException("unary operator returned null!"); |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 979 | } |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 980 | | ShiftOps Types ValueRef ',' Types ValueRef { |
| 981 | if ($5 != Type::UByteTy) ThrowException("Shift amount must be ubyte!"); |
| 982 | $$ = new ShiftInst($1, getVal($2, $3), getVal($5, $6)); |
| 983 | } |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 984 | | CAST Types ValueRef TO Types { |
Chris Lattner | 71496b3 | 2001-07-08 19:03:27 +0000 | [diff] [blame] | 985 | $$ = new CastInst(getVal($2, $3), $5); |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 986 | } |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 987 | | PHI PHIList { |
| 988 | const Type *Ty = $2->front().first->getType(); |
| 989 | $$ = new PHINode(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 990 | while ($2->begin() != $2->end()) { |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 991 | if ($2->front().first->getType() != Ty) |
| 992 | ThrowException("All elements of a PHI node must be of the same type!"); |
| 993 | ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 994 | $2->pop_front(); |
| 995 | } |
| 996 | delete $2; // Free the list... |
| 997 | } |
| 998 | | CALL Types ValueRef '(' ValueRefListE ')' { |
| 999 | if (!$2->isMethodType()) |
| 1000 | ThrowException("Can only call methods: invalid type '" + |
| 1001 | $2->getName() + "'!"); |
| 1002 | |
| 1003 | const MethodType *Ty = (const MethodType*)$2; |
| 1004 | |
| 1005 | Value *V = getVal(Ty, $3); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 1006 | if (!V->isMethod() || V->getType() != Ty) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1007 | ThrowException("Cannot call: " + $3.getName() + "!"); |
| 1008 | |
| 1009 | // Create or access a new type that corresponds to the function call... |
| 1010 | vector<Value *> Params; |
| 1011 | |
| 1012 | if ($5) { |
| 1013 | // Pull out just the arguments... |
| 1014 | Params.insert(Params.begin(), $5->begin(), $5->end()); |
| 1015 | delete $5; |
| 1016 | |
| 1017 | // Loop through MethodType's arguments and ensure they are specified |
| 1018 | // correctly! |
| 1019 | // |
| 1020 | MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin(); |
| 1021 | unsigned i; |
| 1022 | for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){ |
| 1023 | if (Params[i]->getType() != *I) |
| 1024 | ThrowException("Parameter " + utostr(i) + " is not of type '" + |
| 1025 | (*I)->getName() + "'!"); |
| 1026 | } |
| 1027 | |
| 1028 | if (i != Params.size() || I != Ty->getParamTypes().end()) |
| 1029 | ThrowException("Invalid number of parameters detected!"); |
| 1030 | } |
| 1031 | |
| 1032 | // Create the call node... |
| 1033 | $$ = new CallInst((Method*)V, Params); |
| 1034 | } |
| 1035 | | MemoryInst { |
| 1036 | $$ = $1; |
| 1037 | } |
| 1038 | |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 1039 | // UByteList - List of ubyte values for load and store instructions |
| 1040 | UByteList : ',' ConstVector { |
| 1041 | $$ = $2; |
| 1042 | } | /* empty */ { |
| 1043 | $$ = new vector<ConstPoolVal*>(); |
| 1044 | } |
| 1045 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1046 | MemoryInst : MALLOC Types { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 1047 | $$ = new MallocInst(checkNewType(PointerType::getPointerType($2))); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1048 | } |
| 1049 | | MALLOC Types ',' UINT ValueRef { |
| 1050 | if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) |
| 1051 | ThrowException("Trying to allocate " + $2->getName() + |
| 1052 | " as unsized array!"); |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 1053 | const Type *Ty = checkNewType(PointerType::getPointerType($2)); |
| 1054 | $$ = new MallocInst(Ty, getVal($4, $5)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1055 | } |
| 1056 | | ALLOCA Types { |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 1057 | $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2))); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1058 | } |
| 1059 | | ALLOCA Types ',' UINT ValueRef { |
| 1060 | if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) |
| 1061 | ThrowException("Trying to allocate " + $2->getName() + |
| 1062 | " as unsized array!"); |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 1063 | const Type *Ty = checkNewType(PointerType::getPointerType($2)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1064 | Value *ArrSize = getVal($4, $5); |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame] | 1065 | $$ = new AllocaInst(Ty, ArrSize); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1066 | } |
| 1067 | | FREE Types ValueRef { |
| 1068 | if (!$2->isPointerType()) |
| 1069 | ThrowException("Trying to free nonpointer type " + $2->getName() + "!"); |
| 1070 | $$ = new FreeInst(getVal($2, $3)); |
| 1071 | } |
| 1072 | |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 1073 | | LOAD Types ValueRef UByteList { |
| 1074 | if (!$2->isPointerType()) |
| 1075 | ThrowException("Can't load from nonpointer type: " + $2->getName()); |
| 1076 | if (LoadInst::getIndexedType($2, *$4) == 0) |
| 1077 | ThrowException("Invalid indices for load instruction!"); |
| 1078 | |
| 1079 | $$ = new LoadInst(getVal($2, $3), *$4); |
| 1080 | delete $4; // Free the vector... |
| 1081 | } |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 1082 | | STORE Types ValueRef ',' Types ValueRef UByteList { |
| 1083 | if (!$5->isPointerType()) |
| 1084 | ThrowException("Can't store to a nonpointer type: " + $5->getName()); |
| 1085 | const Type *ElTy = StoreInst::getIndexedType($5, *$7); |
| 1086 | if (ElTy == 0) |
| 1087 | ThrowException("Can't store into that field list!"); |
| 1088 | if (ElTy != $2) |
| 1089 | ThrowException("Can't store '" + $2->getName() + "' into space of type '"+ |
| 1090 | ElTy->getName() + "'!"); |
| 1091 | $$ = new StoreInst(getVal($2, $3), getVal($5, $6), *$7); |
| 1092 | delete $7; |
| 1093 | } |
| 1094 | | GETELEMENTPTR Types ValueRef UByteList { |
| 1095 | if (!$2->isPointerType()) |
| 1096 | ThrowException("getelementptr insn requires pointer operand!"); |
| 1097 | if (!GetElementPtrInst::getIndexedType($2, *$4, true)) |
| 1098 | ThrowException("Can't get element ptr '" + $2->getName() + "'!"); |
| 1099 | $$ = new GetElementPtrInst(getVal($2, $3), *$4); |
| 1100 | delete $4; |
Chris Lattner | 8896eda | 2001-07-09 19:38:36 +0000 | [diff] [blame] | 1101 | checkNewType($$->getType()); |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 1102 | } |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1104 | %% |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 1105 | int yyerror(const char *ErrorMsg) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1106 | ThrowException(string("Parse error: ") + ErrorMsg); |
| 1107 | return 0; |
| 1108 | } |