Record.h revision a558d91cedd4fd5c2d3327a8a18e12c35530efa8
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines the main TableGen data structures, including the TableGen 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// types, values, and high-level data structures. 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_TABLEGEN_RECORD_H 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_TABLEGEN_RECORD_H 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/ArrayRef.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/FoldingSet.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Allocator.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/SourceMgr.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/DataTypes.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class raw_ostream; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RecTy subclasses. 31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class BitRecTy; 32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class BitsRecTy; 33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class IntRecTy; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringRecTy; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListRecTy; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DagRecTy; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RecordRecTy; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Init subclasses. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Init; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UnsetInit; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BitInit; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BitsInit; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IntInit; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringInit; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListInit; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UnOpInit; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BinOpInit; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TernOpInit; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DefInit; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DagInit; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TypedInit; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VarInit; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FieldInit; 55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class VarBitInit; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VarListElementInit; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Other classes. 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Record; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RecordVal; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct MultiClass; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RecordKeeper; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Type Classes 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RecTy { 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ListRecTy *ListTy; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void anchor(); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RecTy() : ListTy(0) {} 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~RecTy() {} 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual std::string getAsString() const = 0; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void print(raw_ostream &OS) const { OS << getAsString(); } 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void dump() const; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// typeIsConvertibleTo - Return true if all values of 'this' type can be 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// converted to the specified type. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0; 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// getListTy - Returns the type representing list<this>. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ListRecTy *getListTy(); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public: // These methods should only be called from subclasses of Init 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnsetInit *UI) { return 0; } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitInit *BI) { return 0; } 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitsInit *BI) { return 0; } 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( IntInit *II) { return 0; } 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(StringInit *SI) { return 0; } 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( ListInit *LI) { return 0; } 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnOpInit *UI) { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return convertValue((TypedInit*)UI); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BinOpInit *UI) { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return convertValue((TypedInit*)UI); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual Init *convertValue( TernOpInit *UI) { 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return convertValue((TypedInit*)UI); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(VarBitInit *VB) { return 0; } 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DefInit *DI) { return 0; } 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DagInit *DI) { return 0; } 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( TypedInit *TI) { return 0; } 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual Init *convertValue( VarInit *VI) { 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return convertValue((TypedInit*)VI); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( FieldInit *FI) { 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return convertValue((TypedInit*)FI); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: // These methods should only be called by subclasses of RecTy. 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // baseClassOf - These virtual methods should be overloaded to return true iff 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // all values of type 'RHS' can be converted to the 'this' type. 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) { 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Ty.print(OS); 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return OS; 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// BitRecTy - 'bit' - Represent a single bit 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BitRecTy : public RecTy { 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static BitRecTy Shared; 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BitRecTy() {} 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static BitRecTy *get() { return &Shared; } 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitInit *BI) { return (Init*)BI; } 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitsInit *BI); 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( IntInit *II); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(StringInit *SI) { return 0; } 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( ListInit *LI) { return 0; } 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; } 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DefInit *DI) { return 0; } 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DagInit *DI) { return 0; } 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( TypedInit *TI); 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual std::string getAsString() const { return "bit"; } 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return RHS->baseClassOf(this); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const BitsRecTy *RHS) const; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BitsRecTy - 'bits<n>' - Represent a fixed number of bits 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// BitsRecTy - 'bits<n>' - Represent a fixed number of bits 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BitsRecTy : public RecTy { 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Size; 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit BitsRecTy(unsigned Sz) : Size(Sz) {} 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static BitsRecTy *get(unsigned Sz); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getNumBits() const { return Size; } 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnsetInit *UI); 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitInit *UI); 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BitsInit *BI); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( IntInit *II); 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(StringInit *SI) { return 0; } 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( ListInit *LI) { return 0; } 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue(VarBitInit *VB) { return 0; } 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DefInit *DI) { return 0; } 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( DagInit *DI) { return 0; } 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual Init *convertValue( TypedInit *TI); 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual std::string getAsString() const; 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return RHS->baseClassOf(this); 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; } 204 virtual bool baseClassOf(const BitsRecTy *RHS) const { 205 return RHS->Size == Size; 206 } 207 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 208 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 209 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 210 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 211 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 212 213}; 214 215 216/// IntRecTy - 'int' - Represent an integer value of no particular size 217/// 218class IntRecTy : public RecTy { 219 static IntRecTy Shared; 220 IntRecTy() {} 221public: 222 static IntRecTy *get() { return &Shared; } 223 224 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 225 virtual Init *convertValue( BitInit *BI); 226 virtual Init *convertValue( BitsInit *BI); 227 virtual Init *convertValue( IntInit *II) { return (Init*)II; } 228 virtual Init *convertValue(StringInit *SI) { return 0; } 229 virtual Init *convertValue( ListInit *LI) { return 0; } 230 virtual Init *convertValue(VarBitInit *VB) { return 0; } 231 virtual Init *convertValue( DefInit *DI) { return 0; } 232 virtual Init *convertValue( DagInit *DI) { return 0; } 233 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 234 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 235 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 236 virtual Init *convertValue( TypedInit *TI); 237 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 238 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 239 240 virtual std::string getAsString() const { return "int"; } 241 242 virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 243 return RHS->baseClassOf(this); 244 } 245 246 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } 247 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; } 248 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 249 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 250 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 251 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 252 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 253 254}; 255 256/// StringRecTy - 'string' - Represent an string value 257/// 258class StringRecTy : public RecTy { 259 static StringRecTy Shared; 260 StringRecTy() {} 261public: 262 static StringRecTy *get() { return &Shared; } 263 264 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 265 virtual Init *convertValue( BitInit *BI) { return 0; } 266 virtual Init *convertValue( BitsInit *BI) { return 0; } 267 virtual Init *convertValue( IntInit *II) { return 0; } 268 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; } 269 virtual Init *convertValue( ListInit *LI) { return 0; } 270 virtual Init *convertValue( UnOpInit *BO); 271 virtual Init *convertValue( BinOpInit *BO); 272 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);} 273 274 virtual Init *convertValue(VarBitInit *VB) { return 0; } 275 virtual Init *convertValue( DefInit *DI) { return 0; } 276 virtual Init *convertValue( DagInit *DI) { return 0; } 277 virtual Init *convertValue( TypedInit *TI); 278 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 279 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 280 281 virtual std::string getAsString() const { return "string"; } 282 283 virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 284 return RHS->baseClassOf(this); 285 } 286 287 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 288 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 289 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 290 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; } 291 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 292 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 293 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 294}; 295 296// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of 297// the specified type. 298/// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must 299/// be of the specified type. 300/// 301class ListRecTy : public RecTy { 302 RecTy *Ty; 303 explicit ListRecTy(RecTy *T) : Ty(T) {} 304 friend ListRecTy *RecTy::getListTy(); 305public: 306 static ListRecTy *get(RecTy *T) { return T->getListTy(); } 307 RecTy *getElementType() const { return Ty; } 308 309 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 310 virtual Init *convertValue( BitInit *BI) { return 0; } 311 virtual Init *convertValue( BitsInit *BI) { return 0; } 312 virtual Init *convertValue( IntInit *II) { return 0; } 313 virtual Init *convertValue(StringInit *SI) { return 0; } 314 virtual Init *convertValue( ListInit *LI); 315 virtual Init *convertValue(VarBitInit *VB) { return 0; } 316 virtual Init *convertValue( DefInit *DI) { return 0; } 317 virtual Init *convertValue( DagInit *DI) { return 0; } 318 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 319 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 320 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 321 virtual Init *convertValue( TypedInit *TI); 322 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 323 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 324 325 virtual std::string getAsString() const; 326 327 virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 328 return RHS->baseClassOf(this); 329 } 330 331 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 332 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 333 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 334 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 335 virtual bool baseClassOf(const ListRecTy *RHS) const { 336 return RHS->getElementType()->typeIsConvertibleTo(Ty); 337 } 338 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 339 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 340}; 341 342/// DagRecTy - 'dag' - Represent a dag fragment 343/// 344class DagRecTy : public RecTy { 345 static DagRecTy Shared; 346 DagRecTy() {} 347public: 348 static DagRecTy *get() { return &Shared; } 349 350 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 351 virtual Init *convertValue( BitInit *BI) { return 0; } 352 virtual Init *convertValue( BitsInit *BI) { return 0; } 353 virtual Init *convertValue( IntInit *II) { return 0; } 354 virtual Init *convertValue(StringInit *SI) { return 0; } 355 virtual Init *convertValue( ListInit *LI) { return 0; } 356 virtual Init *convertValue(VarBitInit *VB) { return 0; } 357 virtual Init *convertValue( DefInit *DI) { return 0; } 358 virtual Init *convertValue( UnOpInit *BO); 359 virtual Init *convertValue( BinOpInit *BO); 360 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);} 361 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; } 362 virtual Init *convertValue( TypedInit *TI); 363 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 364 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 365 366 virtual std::string getAsString() const { return "dag"; } 367 368 virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 369 return RHS->baseClassOf(this); 370 } 371 372 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 373 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 374 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 375 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 376 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 377 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; } 378 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 379}; 380 381 382/// RecordRecTy - '[classname]' - Represent an instance of a class, such as: 383/// (R32 X = EAX). 384/// 385class RecordRecTy : public RecTy { 386 Record *Rec; 387 explicit RecordRecTy(Record *R) : Rec(R) {} 388 friend class Record; 389public: 390 static RecordRecTy *get(Record *R); 391 392 Record *getRecord() const { return Rec; } 393 394 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 395 virtual Init *convertValue( BitInit *BI) { return 0; } 396 virtual Init *convertValue( BitsInit *BI) { return 0; } 397 virtual Init *convertValue( IntInit *II) { return 0; } 398 virtual Init *convertValue(StringInit *SI) { return 0; } 399 virtual Init *convertValue( ListInit *LI) { return 0; } 400 virtual Init *convertValue(VarBitInit *VB) { return 0; } 401 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 402 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 403 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 404 virtual Init *convertValue( DefInit *DI); 405 virtual Init *convertValue( DagInit *DI) { return 0; } 406 virtual Init *convertValue( TypedInit *VI); 407 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 408 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 409 410 virtual std::string getAsString() const; 411 412 virtual bool typeIsConvertibleTo(const RecTy *RHS) const { 413 return RHS->baseClassOf(this); 414 } 415 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 416 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 417 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 418 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 419 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 420 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 421 virtual bool baseClassOf(const RecordRecTy *RHS) const; 422}; 423 424/// resolveTypes - Find a common type that T1 and T2 convert to. 425/// Return 0 if no such type exists. 426/// 427RecTy *resolveTypes(RecTy *T1, RecTy *T2); 428 429//===----------------------------------------------------------------------===// 430// Initializer Classes 431//===----------------------------------------------------------------------===// 432 433class Init { 434 Init(const Init &) LLVM_DELETED_FUNCTION; 435 Init &operator=(const Init &) LLVM_DELETED_FUNCTION; 436 virtual void anchor(); 437 438protected: 439 Init(void) {} 440 441public: 442 virtual ~Init() {} 443 444 /// isComplete - This virtual method should be overridden by values that may 445 /// not be completely specified yet. 446 virtual bool isComplete() const { return true; } 447 448 /// print - Print out this value. 449 void print(raw_ostream &OS) const { OS << getAsString(); } 450 451 /// getAsString - Convert this value to a string form. 452 virtual std::string getAsString() const = 0; 453 /// getAsUnquotedString - Convert this value to a string form, 454 /// without adding quote markers. This primaruly affects 455 /// StringInits where we will not surround the string value with 456 /// quotes. 457 virtual std::string getAsUnquotedString() const { return getAsString(); } 458 459 /// dump - Debugging method that may be called through a debugger, just 460 /// invokes print on stderr. 461 void dump() const; 462 463 /// convertInitializerTo - This virtual function is a simple call-back 464 /// function that should be overridden to call the appropriate 465 /// RecTy::convertValue method. 466 /// 467 virtual Init *convertInitializerTo(RecTy *Ty) const = 0; 468 469 /// convertInitializerBitRange - This method is used to implement the bitrange 470 /// selection operator. Given an initializer, it selects the specified bits 471 /// out, returning them as a new init of bits type. If it is not legal to use 472 /// the bit subscript operator on this initializer, return null. 473 /// 474 virtual Init * 475 convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 476 return 0; 477 } 478 479 /// convertInitListSlice - This method is used to implement the list slice 480 /// selection operator. Given an initializer, it selects the specified list 481 /// elements, returning them as a new init of list type. If it is not legal 482 /// to take a slice of this, return null. 483 /// 484 virtual Init * 485 convertInitListSlice(const std::vector<unsigned> &Elements) const { 486 return 0; 487 } 488 489 /// getFieldType - This method is used to implement the FieldInit class. 490 /// Implementors of this method should return the type of the named field if 491 /// they are of record type. 492 /// 493 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; } 494 495 /// getFieldInit - This method complements getFieldType to return the 496 /// initializer for the specified field. If getFieldType returns non-null 497 /// this method should return non-null, otherwise it returns null. 498 /// 499 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 500 const std::string &FieldName) const { 501 return 0; 502 } 503 504 /// resolveReferences - This method is used by classes that refer to other 505 /// variables which may not be defined at the time the expression is formed. 506 /// If a value is set for the variable later, this method will be called on 507 /// users of the value to allow the value to propagate out. 508 /// 509 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const { 510 return const_cast<Init *>(this); 511 } 512 513 /// getBit - This method is used to return the initializer for the specified 514 /// bit. 515 virtual Init *getBit(unsigned Bit) const = 0; 516 517 /// getBitVar - This method is used to retrieve the initializer for bit 518 /// reference. For non-VarBitInit, it simply returns itself. 519 virtual Init *getBitVar() const { return const_cast<Init*>(this); } 520 521 /// getBitNum - This method is used to retrieve the bit number of a bit 522 /// reference. For non-VarBitInit, it simply returns 0. 523 virtual unsigned getBitNum() const { return 0; } 524}; 525 526inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) { 527 I.print(OS); return OS; 528} 529 530/// TypedInit - This is the common super-class of types that have a specific, 531/// explicit, type. 532/// 533class TypedInit : public Init { 534 RecTy *Ty; 535 536 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION; 537 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION; 538 539protected: 540 explicit TypedInit(RecTy *T) : Ty(T) {} 541 542public: 543 RecTy *getType() const { return Ty; } 544 545 virtual Init * 546 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 547 virtual Init * 548 convertInitListSlice(const std::vector<unsigned> &Elements) const; 549 550 /// getFieldType - This method is used to implement the FieldInit class. 551 /// Implementors of this method should return the type of the named field if 552 /// they are of record type. 553 /// 554 virtual RecTy *getFieldType(const std::string &FieldName) const; 555 556 /// resolveListElementReference - This method is used to implement 557 /// VarListElementInit::resolveReferences. If the list element is resolvable 558 /// now, we return the resolved value, otherwise we return null. 559 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 560 unsigned Elt) const = 0; 561}; 562 563 564/// UnsetInit - ? - Represents an uninitialized value 565/// 566class UnsetInit : public Init { 567 UnsetInit() : Init() {} 568 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION; 569 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION; 570 virtual void anchor(); 571 572public: 573 static UnsetInit *get(); 574 575 virtual Init *convertInitializerTo(RecTy *Ty) const { 576 return Ty->convertValue(const_cast<UnsetInit *>(this)); 577 } 578 579 virtual Init *getBit(unsigned Bit) const { 580 return const_cast<UnsetInit*>(this); 581 } 582 583 virtual bool isComplete() const { return false; } 584 virtual std::string getAsString() const { return "?"; } 585}; 586 587 588/// BitInit - true/false - Represent a concrete initializer for a bit. 589/// 590class BitInit : public Init { 591 bool Value; 592 593 explicit BitInit(bool V) : Value(V) {} 594 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION; 595 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION; 596 virtual void anchor(); 597 598public: 599 static BitInit *get(bool V); 600 601 bool getValue() const { return Value; } 602 603 virtual Init *convertInitializerTo(RecTy *Ty) const { 604 return Ty->convertValue(const_cast<BitInit *>(this)); 605 } 606 607 virtual Init *getBit(unsigned Bit) const { 608 assert(Bit < 1 && "Bit index out of range!"); 609 return const_cast<BitInit*>(this); 610 } 611 612 virtual std::string getAsString() const { return Value ? "1" : "0"; } 613}; 614 615/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value. 616/// It contains a vector of bits, whose size is determined by the type. 617/// 618class BitsInit : public Init, public FoldingSetNode { 619 std::vector<Init*> Bits; 620 621 BitsInit(ArrayRef<Init *> Range) : Bits(Range.begin(), Range.end()) {} 622 623 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION; 624 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION; 625 626public: 627 static BitsInit *get(ArrayRef<Init *> Range); 628 629 void Profile(FoldingSetNodeID &ID) const; 630 631 unsigned getNumBits() const { return Bits.size(); } 632 633 virtual Init *convertInitializerTo(RecTy *Ty) const { 634 return Ty->convertValue(const_cast<BitsInit *>(this)); 635 } 636 virtual Init * 637 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 638 639 virtual bool isComplete() const { 640 for (unsigned i = 0; i != getNumBits(); ++i) 641 if (!getBit(i)->isComplete()) return false; 642 return true; 643 } 644 bool allInComplete() const { 645 for (unsigned i = 0; i != getNumBits(); ++i) 646 if (getBit(i)->isComplete()) return false; 647 return true; 648 } 649 virtual std::string getAsString() const; 650 651 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 652 653 virtual Init *getBit(unsigned Bit) const { 654 assert(Bit < Bits.size() && "Bit index out of range!"); 655 return Bits[Bit]; 656 } 657}; 658 659 660/// IntInit - 7 - Represent an initalization by a literal integer value. 661/// 662class IntInit : public TypedInit { 663 int64_t Value; 664 665 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {} 666 667 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION; 668 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION; 669 670public: 671 static IntInit *get(int64_t V); 672 673 int64_t getValue() const { return Value; } 674 675 virtual Init *convertInitializerTo(RecTy *Ty) const { 676 return Ty->convertValue(const_cast<IntInit *>(this)); 677 } 678 virtual Init * 679 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 680 681 virtual std::string getAsString() const; 682 683 /// resolveListElementReference - This method is used to implement 684 /// VarListElementInit::resolveReferences. If the list element is resolvable 685 /// now, we return the resolved value, otherwise we return null. 686 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 687 unsigned Elt) const { 688 llvm_unreachable("Illegal element reference off int"); 689 } 690 691 virtual Init *getBit(unsigned Bit) const { 692 return BitInit::get((Value & (1ULL << Bit)) != 0); 693 } 694}; 695 696 697/// StringInit - "foo" - Represent an initialization by a string value. 698/// 699class StringInit : public TypedInit { 700 std::string Value; 701 702 explicit StringInit(const std::string &V) 703 : TypedInit(StringRecTy::get()), Value(V) {} 704 705 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION; 706 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION; 707 virtual void anchor(); 708 709public: 710 static StringInit *get(StringRef); 711 712 const std::string &getValue() const { return Value; } 713 714 virtual Init *convertInitializerTo(RecTy *Ty) const { 715 return Ty->convertValue(const_cast<StringInit *>(this)); 716 } 717 718 virtual std::string getAsString() const { return "\"" + Value + "\""; } 719 virtual std::string getAsUnquotedString() const { return Value; } 720 721 /// resolveListElementReference - This method is used to implement 722 /// VarListElementInit::resolveReferences. If the list element is resolvable 723 /// now, we return the resolved value, otherwise we return null. 724 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 725 unsigned Elt) const { 726 llvm_unreachable("Illegal element reference off string"); 727 } 728 729 virtual Init *getBit(unsigned Bit) const { 730 llvm_unreachable("Illegal bit reference off string"); 731 } 732}; 733 734/// ListInit - [AL, AH, CL] - Represent a list of defs 735/// 736class ListInit : public TypedInit, public FoldingSetNode { 737 std::vector<Init*> Values; 738public: 739 typedef std::vector<Init*>::const_iterator const_iterator; 740 741private: 742 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy) 743 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {} 744 745 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION; 746 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION; 747 748public: 749 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy); 750 751 void Profile(FoldingSetNodeID &ID) const; 752 753 unsigned getSize() const { return Values.size(); } 754 Init *getElement(unsigned i) const { 755 assert(i < Values.size() && "List element index out of range!"); 756 return Values[i]; 757 } 758 759 Record *getElementAsRecord(unsigned i) const; 760 761 virtual Init * 762 convertInitListSlice(const std::vector<unsigned> &Elements) const; 763 764 virtual Init *convertInitializerTo(RecTy *Ty) const { 765 return Ty->convertValue(const_cast<ListInit *>(this)); 766 } 767 768 /// resolveReferences - This method is used by classes that refer to other 769 /// variables which may not be defined at the time they expression is formed. 770 /// If a value is set for the variable later, this method will be called on 771 /// users of the value to allow the value to propagate out. 772 /// 773 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 774 775 virtual std::string getAsString() const; 776 777 ArrayRef<Init*> getValues() const { return Values; } 778 779 inline const_iterator begin() const { return Values.begin(); } 780 inline const_iterator end () const { return Values.end(); } 781 782 inline size_t size () const { return Values.size(); } 783 inline bool empty() const { return Values.empty(); } 784 785 /// resolveListElementReference - This method is used to implement 786 /// VarListElementInit::resolveReferences. If the list element is resolvable 787 /// now, we return the resolved value, otherwise we return null. 788 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 789 unsigned Elt) const; 790 791 virtual Init *getBit(unsigned Bit) const { 792 llvm_unreachable("Illegal bit reference off list"); 793 } 794}; 795 796 797/// OpInit - Base class for operators 798/// 799class OpInit : public TypedInit { 800 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION; 801 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION; 802 803protected: 804 explicit OpInit(RecTy *Type) : TypedInit(Type) {} 805 806public: 807 // Clone - Clone this operator, replacing arguments with the new list 808 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0; 809 810 virtual int getNumOperands() const = 0; 811 virtual Init *getOperand(int i) const = 0; 812 813 // Fold - If possible, fold this to a simpler init. Return this if not 814 // possible to fold. 815 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0; 816 817 virtual Init *convertInitializerTo(RecTy *Ty) const { 818 return Ty->convertValue(const_cast<OpInit *>(this)); 819 } 820 821 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 822 unsigned Elt) const; 823 824 virtual Init *getBit(unsigned Bit) const; 825}; 826 827 828/// UnOpInit - !op (X) - Transform an init. 829/// 830class UnOpInit : public OpInit { 831public: 832 enum UnaryOp { CAST, HEAD, TAIL, EMPTY }; 833private: 834 UnaryOp Opc; 835 Init *LHS; 836 837 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) 838 : OpInit(Type), Opc(opc), LHS(lhs) {} 839 840 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION; 841 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION; 842 843public: 844 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type); 845 846 // Clone - Clone this operator, replacing arguments with the new list 847 virtual OpInit *clone(std::vector<Init *> &Operands) const { 848 assert(Operands.size() == 1 && 849 "Wrong number of operands for unary operation"); 850 return UnOpInit::get(getOpcode(), *Operands.begin(), getType()); 851 } 852 853 virtual int getNumOperands() const { return 1; } 854 virtual Init *getOperand(int i) const { 855 assert(i == 0 && "Invalid operand id for unary operator"); 856 return getOperand(); 857 } 858 859 UnaryOp getOpcode() const { return Opc; } 860 Init *getOperand() const { return LHS; } 861 862 // Fold - If possible, fold this to a simpler init. Return this if not 863 // possible to fold. 864 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 865 866 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 867 868 virtual std::string getAsString() const; 869}; 870 871/// BinOpInit - !op (X, Y) - Combine two inits. 872/// 873class BinOpInit : public OpInit { 874public: 875 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ }; 876private: 877 BinaryOp Opc; 878 Init *LHS, *RHS; 879 880 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) : 881 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {} 882 883 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION; 884 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION; 885 886public: 887 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, 888 RecTy *Type); 889 890 // Clone - Clone this operator, replacing arguments with the new list 891 virtual OpInit *clone(std::vector<Init *> &Operands) const { 892 assert(Operands.size() == 2 && 893 "Wrong number of operands for binary operation"); 894 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType()); 895 } 896 897 virtual int getNumOperands() const { return 2; } 898 virtual Init *getOperand(int i) const { 899 assert((i == 0 || i == 1) && "Invalid operand id for binary operator"); 900 if (i == 0) { 901 return getLHS(); 902 } else { 903 return getRHS(); 904 } 905 } 906 907 BinaryOp getOpcode() const { return Opc; } 908 Init *getLHS() const { return LHS; } 909 Init *getRHS() const { return RHS; } 910 911 // Fold - If possible, fold this to a simpler init. Return this if not 912 // possible to fold. 913 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 914 915 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 916 917 virtual std::string getAsString() const; 918}; 919 920/// TernOpInit - !op (X, Y, Z) - Combine two inits. 921/// 922class TernOpInit : public OpInit { 923public: 924 enum TernaryOp { SUBST, FOREACH, IF }; 925private: 926 TernaryOp Opc; 927 Init *LHS, *MHS, *RHS; 928 929 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, 930 RecTy *Type) : 931 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {} 932 933 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION; 934 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION; 935 936public: 937 static TernOpInit *get(TernaryOp opc, Init *lhs, 938 Init *mhs, Init *rhs, 939 RecTy *Type); 940 941 // Clone - Clone this operator, replacing arguments with the new list 942 virtual OpInit *clone(std::vector<Init *> &Operands) const { 943 assert(Operands.size() == 3 && 944 "Wrong number of operands for ternary operation"); 945 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2], 946 getType()); 947 } 948 949 virtual int getNumOperands() const { return 3; } 950 virtual Init *getOperand(int i) const { 951 assert((i == 0 || i == 1 || i == 2) && 952 "Invalid operand id for ternary operator"); 953 if (i == 0) { 954 return getLHS(); 955 } else if (i == 1) { 956 return getMHS(); 957 } else { 958 return getRHS(); 959 } 960 } 961 962 TernaryOp getOpcode() const { return Opc; } 963 Init *getLHS() const { return LHS; } 964 Init *getMHS() const { return MHS; } 965 Init *getRHS() const { return RHS; } 966 967 // Fold - If possible, fold this to a simpler init. Return this if not 968 // possible to fold. 969 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 970 971 virtual bool isComplete() const { return false; } 972 973 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 974 975 virtual std::string getAsString() const; 976}; 977 978 979/// VarInit - 'Opcode' - Represent a reference to an entire variable object. 980/// 981class VarInit : public TypedInit { 982 Init *VarName; 983 984 explicit VarInit(const std::string &VN, RecTy *T) 985 : TypedInit(T), VarName(StringInit::get(VN)) {} 986 explicit VarInit(Init *VN, RecTy *T) 987 : TypedInit(T), VarName(VN) {} 988 989 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION; 990 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION; 991 992public: 993 static VarInit *get(const std::string &VN, RecTy *T); 994 static VarInit *get(Init *VN, RecTy *T); 995 996 virtual Init *convertInitializerTo(RecTy *Ty) const { 997 return Ty->convertValue(const_cast<VarInit *>(this)); 998 } 999 1000 const std::string &getName() const; 1001 Init *getNameInit() const { return VarName; } 1002 std::string getNameInitAsString() const { 1003 return getNameInit()->getAsUnquotedString(); 1004 } 1005 1006 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1007 unsigned Elt) const; 1008 1009 virtual RecTy *getFieldType(const std::string &FieldName) const; 1010 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 1011 const std::string &FieldName) const; 1012 1013 /// resolveReferences - This method is used by classes that refer to other 1014 /// variables which may not be defined at the time they expression is formed. 1015 /// If a value is set for the variable later, this method will be called on 1016 /// users of the value to allow the value to propagate out. 1017 /// 1018 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1019 1020 virtual Init *getBit(unsigned Bit) const; 1021 1022 virtual std::string getAsString() const { return getName(); } 1023}; 1024 1025 1026/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field. 1027/// 1028class VarBitInit : public Init { 1029 TypedInit *TI; 1030 unsigned Bit; 1031 1032 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) { 1033 assert(T->getType() && 1034 (dynamic_cast<IntRecTy*>(T->getType()) || 1035 (dynamic_cast<BitsRecTy*>(T->getType()) && 1036 dynamic_cast<BitsRecTy*>(T->getType())->getNumBits() > B)) && 1037 "Illegal VarBitInit expression!"); 1038 } 1039 1040 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION; 1041 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION; 1042 1043public: 1044 static VarBitInit *get(TypedInit *T, unsigned B); 1045 1046 virtual Init *convertInitializerTo(RecTy *Ty) const { 1047 return Ty->convertValue(const_cast<VarBitInit *>(this)); 1048 } 1049 1050 virtual Init *getBitVar() const { return TI; } 1051 virtual unsigned getBitNum() const { return Bit; } 1052 1053 virtual std::string getAsString() const; 1054 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1055 1056 virtual Init *getBit(unsigned B) const { 1057 assert(B < 1 && "Bit index out of range!"); 1058 return const_cast<VarBitInit*>(this); 1059 } 1060}; 1061 1062/// VarListElementInit - List[4] - Represent access to one element of a var or 1063/// field. 1064class VarListElementInit : public TypedInit { 1065 TypedInit *TI; 1066 unsigned Element; 1067 1068 VarListElementInit(TypedInit *T, unsigned E) 1069 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()), 1070 TI(T), Element(E) { 1071 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) && 1072 "Illegal VarBitInit expression!"); 1073 } 1074 1075 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION; 1076 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION; 1077 1078public: 1079 static VarListElementInit *get(TypedInit *T, unsigned E); 1080 1081 virtual Init *convertInitializerTo(RecTy *Ty) const { 1082 return Ty->convertValue(const_cast<VarListElementInit *>(this)); 1083 } 1084 1085 TypedInit *getVariable() const { return TI; } 1086 unsigned getElementNum() const { return Element; } 1087 1088 /// resolveListElementReference - This method is used to implement 1089 /// VarListElementInit::resolveReferences. If the list element is resolvable 1090 /// now, we return the resolved value, otherwise we return null. 1091 virtual Init *resolveListElementReference(Record &R, 1092 const RecordVal *RV, 1093 unsigned Elt) const; 1094 1095 virtual std::string getAsString() const; 1096 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1097 1098 virtual Init *getBit(unsigned Bit) const; 1099}; 1100 1101/// DefInit - AL - Represent a reference to a 'def' in the description 1102/// 1103class DefInit : public TypedInit { 1104 Record *Def; 1105 1106 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {} 1107 friend class Record; 1108 1109 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION; 1110 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION; 1111 1112public: 1113 static DefInit *get(Record*); 1114 1115 virtual Init *convertInitializerTo(RecTy *Ty) const { 1116 return Ty->convertValue(const_cast<DefInit *>(this)); 1117 } 1118 1119 Record *getDef() const { return Def; } 1120 1121 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); 1122 1123 virtual RecTy *getFieldType(const std::string &FieldName) const; 1124 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 1125 const std::string &FieldName) const; 1126 1127 virtual std::string getAsString() const; 1128 1129 virtual Init *getBit(unsigned Bit) const { 1130 llvm_unreachable("Illegal bit reference off def"); 1131 } 1132 1133 /// resolveListElementReference - This method is used to implement 1134 /// VarListElementInit::resolveReferences. If the list element is resolvable 1135 /// now, we return the resolved value, otherwise we return null. 1136 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1137 unsigned Elt) const { 1138 llvm_unreachable("Illegal element reference off def"); 1139 } 1140}; 1141 1142 1143/// FieldInit - X.Y - Represent a reference to a subfield of a variable 1144/// 1145class FieldInit : public TypedInit { 1146 Init *Rec; // Record we are referring to 1147 std::string FieldName; // Field we are accessing 1148 1149 FieldInit(Init *R, const std::string &FN) 1150 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { 1151 assert(getType() && "FieldInit with non-record type!"); 1152 } 1153 1154 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION; 1155 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION; 1156 1157public: 1158 static FieldInit *get(Init *R, const std::string &FN); 1159 static FieldInit *get(Init *R, const Init *FN); 1160 1161 virtual Init *convertInitializerTo(RecTy *Ty) const { 1162 return Ty->convertValue(const_cast<FieldInit *>(this)); 1163 } 1164 1165 virtual Init *getBit(unsigned Bit) const; 1166 1167 virtual Init *resolveListElementReference(Record &R, 1168 const RecordVal *RV, 1169 unsigned Elt) const; 1170 1171 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1172 1173 virtual std::string getAsString() const { 1174 return Rec->getAsString() + "." + FieldName; 1175 } 1176}; 1177 1178/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required 1179/// to have at least one value then a (possibly empty) list of arguments. Each 1180/// argument can have a name associated with it. 1181/// 1182class DagInit : public TypedInit, public FoldingSetNode { 1183 Init *Val; 1184 std::string ValName; 1185 std::vector<Init*> Args; 1186 std::vector<std::string> ArgNames; 1187 1188 DagInit(Init *V, const std::string &VN, 1189 ArrayRef<Init *> ArgRange, 1190 ArrayRef<std::string> NameRange) 1191 : TypedInit(DagRecTy::get()), Val(V), ValName(VN), 1192 Args(ArgRange.begin(), ArgRange.end()), 1193 ArgNames(NameRange.begin(), NameRange.end()) {} 1194 1195 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION; 1196 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION; 1197 1198public: 1199 static DagInit *get(Init *V, const std::string &VN, 1200 ArrayRef<Init *> ArgRange, 1201 ArrayRef<std::string> NameRange); 1202 static DagInit *get(Init *V, const std::string &VN, 1203 const std::vector< 1204 std::pair<Init*, std::string> > &args); 1205 1206 void Profile(FoldingSetNodeID &ID) const; 1207 1208 virtual Init *convertInitializerTo(RecTy *Ty) const { 1209 return Ty->convertValue(const_cast<DagInit *>(this)); 1210 } 1211 1212 Init *getOperator() const { return Val; } 1213 1214 const std::string &getName() const { return ValName; } 1215 1216 unsigned getNumArgs() const { return Args.size(); } 1217 Init *getArg(unsigned Num) const { 1218 assert(Num < Args.size() && "Arg number out of range!"); 1219 return Args[Num]; 1220 } 1221 const std::string &getArgName(unsigned Num) const { 1222 assert(Num < ArgNames.size() && "Arg number out of range!"); 1223 return ArgNames[Num]; 1224 } 1225 1226 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1227 1228 virtual std::string getAsString() const; 1229 1230 typedef std::vector<Init*>::const_iterator const_arg_iterator; 1231 typedef std::vector<std::string>::const_iterator const_name_iterator; 1232 1233 inline const_arg_iterator arg_begin() const { return Args.begin(); } 1234 inline const_arg_iterator arg_end () const { return Args.end(); } 1235 1236 inline size_t arg_size () const { return Args.size(); } 1237 inline bool arg_empty() const { return Args.empty(); } 1238 1239 inline const_name_iterator name_begin() const { return ArgNames.begin(); } 1240 inline const_name_iterator name_end () const { return ArgNames.end(); } 1241 1242 inline size_t name_size () const { return ArgNames.size(); } 1243 inline bool name_empty() const { return ArgNames.empty(); } 1244 1245 virtual Init *getBit(unsigned Bit) const { 1246 llvm_unreachable("Illegal bit reference off dag"); 1247 } 1248 1249 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1250 unsigned Elt) const { 1251 llvm_unreachable("Illegal element reference off dag"); 1252 } 1253}; 1254 1255//===----------------------------------------------------------------------===// 1256// High-Level Classes 1257//===----------------------------------------------------------------------===// 1258 1259class RecordVal { 1260 Init *Name; 1261 RecTy *Ty; 1262 unsigned Prefix; 1263 Init *Value; 1264public: 1265 RecordVal(Init *N, RecTy *T, unsigned P); 1266 RecordVal(const std::string &N, RecTy *T, unsigned P); 1267 1268 const std::string &getName() const; 1269 const Init *getNameInit() const { return Name; } 1270 std::string getNameInitAsString() const { 1271 return getNameInit()->getAsUnquotedString(); 1272 } 1273 1274 unsigned getPrefix() const { return Prefix; } 1275 RecTy *getType() const { return Ty; } 1276 Init *getValue() const { return Value; } 1277 1278 bool setValue(Init *V) { 1279 if (V) { 1280 Value = V->convertInitializerTo(Ty); 1281 return Value == 0; 1282 } 1283 Value = 0; 1284 return false; 1285 } 1286 1287 void dump() const; 1288 void print(raw_ostream &OS, bool PrintSem = true) const; 1289}; 1290 1291inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) { 1292 RV.print(OS << " "); 1293 return OS; 1294} 1295 1296class Record { 1297 static unsigned LastID; 1298 1299 // Unique record ID. 1300 unsigned ID; 1301 Init *Name; 1302 // Location where record was instantiated, followed by the location of 1303 // multiclass prototypes used. 1304 SmallVector<SMLoc, 4> Locs; 1305 std::vector<Init *> TemplateArgs; 1306 std::vector<RecordVal> Values; 1307 std::vector<Record*> SuperClasses; 1308 1309 // Tracks Record instances. Not owned by Record. 1310 RecordKeeper &TrackedRecords; 1311 1312 DefInit *TheInit; 1313 1314 void init(); 1315 void checkName(); 1316 1317public: 1318 1319 // Constructs a record. 1320 explicit Record(const std::string &N, ArrayRef<SMLoc> locs, 1321 RecordKeeper &records) : 1322 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()), 1323 TrackedRecords(records), TheInit(0) { 1324 init(); 1325 } 1326 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records) : 1327 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()), 1328 TrackedRecords(records), TheInit(0) { 1329 init(); 1330 } 1331 ~Record() {} 1332 1333 1334 static unsigned getNewUID() { return LastID++; } 1335 1336 1337 unsigned getID() const { return ID; } 1338 1339 const std::string &getName() const; 1340 Init *getNameInit() const { 1341 return Name; 1342 } 1343 const std::string getNameInitAsString() const { 1344 return getNameInit()->getAsUnquotedString(); 1345 } 1346 1347 void setName(Init *Name); // Also updates RecordKeeper. 1348 void setName(const std::string &Name); // Also updates RecordKeeper. 1349 1350 ArrayRef<SMLoc> getLoc() const { return Locs; } 1351 1352 /// get the corresponding DefInit. 1353 DefInit *getDefInit(); 1354 1355 const std::vector<Init *> &getTemplateArgs() const { 1356 return TemplateArgs; 1357 } 1358 const std::vector<RecordVal> &getValues() const { return Values; } 1359 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; } 1360 1361 bool isTemplateArg(Init *Name) const { 1362 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i) 1363 if (TemplateArgs[i] == Name) return true; 1364 return false; 1365 } 1366 bool isTemplateArg(StringRef Name) const { 1367 return isTemplateArg(StringInit::get(Name.str())); 1368 } 1369 1370 const RecordVal *getValue(const Init *Name) const { 1371 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1372 if (Values[i].getNameInit() == Name) return &Values[i]; 1373 return 0; 1374 } 1375 const RecordVal *getValue(StringRef Name) const { 1376 return getValue(StringInit::get(Name)); 1377 } 1378 RecordVal *getValue(const Init *Name) { 1379 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1380 if (Values[i].getNameInit() == Name) return &Values[i]; 1381 return 0; 1382 } 1383 RecordVal *getValue(StringRef Name) { 1384 return getValue(StringInit::get(Name)); 1385 } 1386 1387 void addTemplateArg(Init *Name) { 1388 assert(!isTemplateArg(Name) && "Template arg already defined!"); 1389 TemplateArgs.push_back(Name); 1390 } 1391 void addTemplateArg(StringRef Name) { 1392 addTemplateArg(StringInit::get(Name.str())); 1393 } 1394 1395 void addValue(const RecordVal &RV) { 1396 assert(getValue(RV.getNameInit()) == 0 && "Value already added!"); 1397 Values.push_back(RV); 1398 if (Values.size() > 1) 1399 // Keep NAME at the end of the list. It makes record dumps a 1400 // bit prettier and allows TableGen tests to be written more 1401 // naturally. Tests can use CHECK-NEXT to look for Record 1402 // fields they expect to see after a def. They can't do that if 1403 // NAME is the first Record field. 1404 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]); 1405 } 1406 1407 void removeValue(Init *Name) { 1408 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1409 if (Values[i].getNameInit() == Name) { 1410 Values.erase(Values.begin()+i); 1411 return; 1412 } 1413 llvm_unreachable("Cannot remove an entry that does not exist!"); 1414 } 1415 1416 void removeValue(StringRef Name) { 1417 removeValue(StringInit::get(Name.str())); 1418 } 1419 1420 bool isSubClassOf(const Record *R) const { 1421 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) 1422 if (SuperClasses[i] == R) 1423 return true; 1424 return false; 1425 } 1426 1427 bool isSubClassOf(StringRef Name) const { 1428 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) 1429 if (SuperClasses[i]->getNameInitAsString() == Name) 1430 return true; 1431 return false; 1432 } 1433 1434 void addSuperClass(Record *R) { 1435 assert(!isSubClassOf(R) && "Already subclassing record!"); 1436 SuperClasses.push_back(R); 1437 } 1438 1439 /// resolveReferences - If there are any field references that refer to fields 1440 /// that have been filled in, we can propagate the values now. 1441 /// 1442 void resolveReferences() { resolveReferencesTo(0); } 1443 1444 /// resolveReferencesTo - If anything in this record refers to RV, replace the 1445 /// reference to RV with the RHS of RV. If RV is null, we resolve all 1446 /// possible references. 1447 void resolveReferencesTo(const RecordVal *RV); 1448 1449 RecordKeeper &getRecords() const { 1450 return TrackedRecords; 1451 } 1452 1453 void dump() const; 1454 1455 //===--------------------------------------------------------------------===// 1456 // High-level methods useful to tablegen back-ends 1457 // 1458 1459 /// getValueInit - Return the initializer for a value with the specified name, 1460 /// or throw an exception if the field does not exist. 1461 /// 1462 Init *getValueInit(StringRef FieldName) const; 1463 1464 /// getValueAsString - This method looks up the specified field and returns 1465 /// its value as a string, throwing an exception if the field does not exist 1466 /// or if the value is not a string. 1467 /// 1468 std::string getValueAsString(StringRef FieldName) const; 1469 1470 /// getValueAsBitsInit - This method looks up the specified field and returns 1471 /// its value as a BitsInit, throwing an exception if the field does not exist 1472 /// or if the value is not the right type. 1473 /// 1474 BitsInit *getValueAsBitsInit(StringRef FieldName) const; 1475 1476 /// getValueAsListInit - This method looks up the specified field and returns 1477 /// its value as a ListInit, throwing an exception if the field does not exist 1478 /// or if the value is not the right type. 1479 /// 1480 ListInit *getValueAsListInit(StringRef FieldName) const; 1481 1482 /// getValueAsListOfDefs - This method looks up the specified field and 1483 /// returns its value as a vector of records, throwing an exception if the 1484 /// field does not exist or if the value is not the right type. 1485 /// 1486 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const; 1487 1488 /// getValueAsListOfInts - This method looks up the specified field and 1489 /// returns its value as a vector of integers, throwing an exception if the 1490 /// field does not exist or if the value is not the right type. 1491 /// 1492 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const; 1493 1494 /// getValueAsListOfStrings - This method looks up the specified field and 1495 /// returns its value as a vector of strings, throwing an exception if the 1496 /// field does not exist or if the value is not the right type. 1497 /// 1498 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const; 1499 1500 /// getValueAsDef - This method looks up the specified field and returns its 1501 /// value as a Record, throwing an exception if the field does not exist or if 1502 /// the value is not the right type. 1503 /// 1504 Record *getValueAsDef(StringRef FieldName) const; 1505 1506 /// getValueAsBit - This method looks up the specified field and returns its 1507 /// value as a bit, throwing an exception if the field does not exist or if 1508 /// the value is not the right type. 1509 /// 1510 bool getValueAsBit(StringRef FieldName) const; 1511 1512 /// getValueAsBitOrUnset - This method looks up the specified field and 1513 /// returns its value as a bit. If the field is unset, sets Unset to true and 1514 /// retunrs false. 1515 /// 1516 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const; 1517 1518 /// getValueAsInt - This method looks up the specified field and returns its 1519 /// value as an int64_t, throwing an exception if the field does not exist or 1520 /// if the value is not the right type. 1521 /// 1522 int64_t getValueAsInt(StringRef FieldName) const; 1523 1524 /// getValueAsDag - This method looks up the specified field and returns its 1525 /// value as an Dag, throwing an exception if the field does not exist or if 1526 /// the value is not the right type. 1527 /// 1528 DagInit *getValueAsDag(StringRef FieldName) const; 1529}; 1530 1531raw_ostream &operator<<(raw_ostream &OS, const Record &R); 1532 1533struct MultiClass { 1534 Record Rec; // Placeholder for template args and Name. 1535 typedef std::vector<Record*> RecordVector; 1536 RecordVector DefPrototypes; 1537 1538 void dump() const; 1539 1540 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) : 1541 Rec(Name, Loc, Records) {} 1542}; 1543 1544class RecordKeeper { 1545 std::map<std::string, Record*> Classes, Defs; 1546 1547public: 1548 ~RecordKeeper() { 1549 for (std::map<std::string, Record*>::iterator I = Classes.begin(), 1550 E = Classes.end(); I != E; ++I) 1551 delete I->second; 1552 for (std::map<std::string, Record*>::iterator I = Defs.begin(), 1553 E = Defs.end(); I != E; ++I) 1554 delete I->second; 1555 } 1556 1557 const std::map<std::string, Record*> &getClasses() const { return Classes; } 1558 const std::map<std::string, Record*> &getDefs() const { return Defs; } 1559 1560 Record *getClass(const std::string &Name) const { 1561 std::map<std::string, Record*>::const_iterator I = Classes.find(Name); 1562 return I == Classes.end() ? 0 : I->second; 1563 } 1564 Record *getDef(const std::string &Name) const { 1565 std::map<std::string, Record*>::const_iterator I = Defs.find(Name); 1566 return I == Defs.end() ? 0 : I->second; 1567 } 1568 void addClass(Record *R) { 1569 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second; 1570 (void)Ins; 1571 assert(Ins && "Class already exists"); 1572 } 1573 void addDef(Record *R) { 1574 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second; 1575 (void)Ins; 1576 assert(Ins && "Record already exists"); 1577 } 1578 1579 /// removeClass - Remove, but do not delete, the specified record. 1580 /// 1581 void removeClass(const std::string &Name) { 1582 assert(Classes.count(Name) && "Class does not exist!"); 1583 Classes.erase(Name); 1584 } 1585 /// removeDef - Remove, but do not delete, the specified record. 1586 /// 1587 void removeDef(const std::string &Name) { 1588 assert(Defs.count(Name) && "Def does not exist!"); 1589 Defs.erase(Name); 1590 } 1591 1592 //===--------------------------------------------------------------------===// 1593 // High-level helper methods, useful for tablegen backends... 1594 1595 /// getAllDerivedDefinitions - This method returns all concrete definitions 1596 /// that derive from the specified class name. If a class with the specified 1597 /// name does not exist, an exception is thrown. 1598 std::vector<Record*> 1599 getAllDerivedDefinitions(const std::string &ClassName) const; 1600 1601 void dump() const; 1602}; 1603 1604/// LessRecord - Sorting predicate to sort record pointers by name. 1605/// 1606struct LessRecord { 1607 bool operator()(const Record *Rec1, const Record *Rec2) const { 1608 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0; 1609 } 1610}; 1611 1612/// LessRecordFieldName - Sorting predicate to sort record pointers by their 1613/// name field. 1614/// 1615struct LessRecordFieldName { 1616 bool operator()(const Record *Rec1, const Record *Rec2) const { 1617 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); 1618 } 1619}; 1620 1621raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK); 1622 1623/// QualifyName - Return an Init with a qualifier prefix referring 1624/// to CurRec's name. 1625Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1626 Init *Name, const std::string &Scoper); 1627 1628/// QualifyName - Return an Init with a qualifier prefix referring 1629/// to CurRec's name. 1630Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1631 const std::string &Name, const std::string &Scoper); 1632 1633} // End llvm namespace 1634 1635#endif 1636