AsmWriterEmitter.cpp revision 263508
1295016Sjkim//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===// 2280304Sjkim// 3280304Sjkim// The LLVM Compiler Infrastructure 4280304Sjkim// 568651Skris// This file is distributed under the University of Illinois Open Source 668651Skris// License. See LICENSE.TXT for details. 768651Skris// 868651Skris//===----------------------------------------------------------------------===// 968651Skris// 1068651Skris// This tablegen backend is emits an assembly printer for the current target. 1168651Skris// Note that this is currently fairly skeletal, but will grow over time. 1268651Skris// 1368651Skris//===----------------------------------------------------------------------===// 14280304Sjkim 1568651Skris#include "AsmWriterInst.h" 1668651Skris#include "CodeGenTarget.h" 1768651Skris#include "SequenceToOffsetTable.h" 1868651Skris#include "llvm/ADT/StringExtras.h" 1968651Skris#include "llvm/ADT/Twine.h" 2068651Skris#include "llvm/Support/Debug.h" 2168651Skris#include "llvm/Support/Format.h" 2268651Skris#include "llvm/Support/MathExtras.h" 2368651Skris#include "llvm/TableGen/Error.h" 2468651Skris#include "llvm/TableGen/Record.h" 2568651Skris#include "llvm/TableGen/TableGenBackend.h" 2668651Skris#include <algorithm> 2768651Skris#include <cassert> 2868651Skris#include <map> 2968651Skris#include <vector> 3068651Skrisusing namespace llvm; 3168651Skris 3268651Skrisnamespace { 3368651Skrisclass AsmWriterEmitter { 3468651Skris RecordKeeper &Records; 3568651Skris CodeGenTarget Target; 3668651Skris std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap; 3768651Skris std::vector<const CodeGenInstruction*> NumberedInstructions; 3868651Skris std::vector<AsmWriterInst> Instructions; 3968651Skrispublic: 4068651Skris AsmWriterEmitter(RecordKeeper &R); 4168651Skris 4268651Skris void run(raw_ostream &o); 4368651Skris 4468651Skrisprivate: 4568651Skris void EmitPrintInstruction(raw_ostream &o); 4668651Skris void EmitGetRegisterName(raw_ostream &o); 4768651Skris void EmitPrintAliasInstruction(raw_ostream &O); 4868651Skris 4968651Skris AsmWriterInst *getAsmWriterInstByID(unsigned ID) const { 5068651Skris assert(ID < NumberedInstructions.size()); 5168651Skris std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I = 5268651Skris CGIAWIMap.find(NumberedInstructions[ID]); 5368651Skris assert(I != CGIAWIMap.end() && "Didn't find inst!"); 5468651Skris return I->second; 5568651Skris } 5668651Skris void FindUniqueOperandCommands(std::vector<std::string> &UOC, 5768651Skris std::vector<unsigned> &InstIdxs, 5868651Skris std::vector<unsigned> &InstOpsUsed) const; 5968651Skris}; 6068651Skris} // end anonymous namespace 6168651Skris 6268651Skrisstatic void PrintCases(std::vector<std::pair<std::string, 6368651Skris AsmWriterOperand> > &OpsToPrint, raw_ostream &O) { 6468651Skris O << " case " << OpsToPrint.back().first << ": "; 6568651Skris AsmWriterOperand TheOp = OpsToPrint.back().second; 66280304Sjkim OpsToPrint.pop_back(); 67280304Sjkim 68280304Sjkim // Check to see if any other operands are identical in this list, and if so, 6968651Skris // emit a case label for them. 7068651Skris for (unsigned i = OpsToPrint.size(); i != 0; --i) 71280304Sjkim if (OpsToPrint[i-1].second == TheOp) { 7268651Skris O << "\n case " << OpsToPrint[i-1].first << ": "; 7368651Skris OpsToPrint.erase(OpsToPrint.begin()+i-1); 74280304Sjkim } 7568651Skris 76109998Smarkm // Finally, emit the code. 7768651Skris O << TheOp.getCode(); 7868651Skris O << "break;\n"; 7968651Skris} 80280304Sjkim 8168651Skris 8268651Skris/// EmitInstructions - Emit the last instruction in the vector and any other 8368651Skris/// instructions that are suitably similar to it. 8468651Skrisstatic void EmitInstructions(std::vector<AsmWriterInst> &Insts, 85109998Smarkm raw_ostream &O) { 86280304Sjkim AsmWriterInst FirstInst = Insts.back(); 87109998Smarkm Insts.pop_back(); 88280304Sjkim 89280304Sjkim std::vector<AsmWriterInst> SimilarInsts; 90280304Sjkim unsigned DifferingOperand = ~0; 91238405Sjkim for (unsigned i = Insts.size(); i != 0; --i) { 9268651Skris unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst); 9368651Skris if (DiffOp != ~1U) { 94280304Sjkim if (DifferingOperand == ~0U) // First match! 95280304Sjkim DifferingOperand = DiffOp; 96280304Sjkim 97280304Sjkim // If this differs in the same operand as the rest of the instructions in 98280304Sjkim // this class, move it to the SimilarInsts list. 9968651Skris if (DifferingOperand == DiffOp || DiffOp == ~0U) { 100280304Sjkim SimilarInsts.push_back(Insts[i-1]); 101280304Sjkim Insts.erase(Insts.begin()+i-1); 102280304Sjkim } 103280304Sjkim } 104280304Sjkim } 105280304Sjkim 106280304Sjkim O << " case " << FirstInst.CGI->Namespace << "::" 107280304Sjkim << FirstInst.CGI->TheDef->getName() << ":\n"; 108280304Sjkim for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i) 109280304Sjkim O << " case " << SimilarInsts[i].CGI->Namespace << "::" 110280304Sjkim << SimilarInsts[i].CGI->TheDef->getName() << ":\n"; 111280304Sjkim for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) { 11268651Skris if (i != DifferingOperand) { 11368651Skris // If the operand is the same for all instructions, just print it. 114280304Sjkim O << " " << FirstInst.Operands[i].getCode(); 115280304Sjkim } else { 116280304Sjkim // If this is the operand that varies between all of the instructions, 11768651Skris // emit a switch for just this operand now. 118280304Sjkim O << " switch (MI->getOpcode()) {\n"; 119280304Sjkim std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint; 120280304Sjkim OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" + 121280304Sjkim FirstInst.CGI->TheDef->getName(), 12268651Skris FirstInst.Operands[i])); 12368651Skris 124109998Smarkm for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) { 125280304Sjkim AsmWriterInst &AWI = SimilarInsts[si]; 126280304Sjkim OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+ 127280304Sjkim AWI.CGI->TheDef->getName(), 128280304Sjkim AWI.Operands[i])); 129280304Sjkim } 130280304Sjkim std::reverse(OpsToPrint.begin(), OpsToPrint.end()); 131280304Sjkim while (!OpsToPrint.empty()) 132280304Sjkim PrintCases(OpsToPrint, O); 13368651Skris O << " }"; 134280304Sjkim } 135280304Sjkim O << "\n"; 136280304Sjkim } 137280304Sjkim O << " break;\n"; 138280304Sjkim} 139280304Sjkim 140280304Sjkimvoid AsmWriterEmitter:: 141280304SjkimFindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands, 142280304Sjkim std::vector<unsigned> &InstIdxs, 143280304Sjkim std::vector<unsigned> &InstOpsUsed) const { 144280304Sjkim InstIdxs.assign(NumberedInstructions.size(), ~0U); 145280304Sjkim 146280304Sjkim // This vector parallels UniqueOperandCommands, keeping track of which 147280304Sjkim // instructions each case are used for. It is a comma separated string of 148280304Sjkim // enums. 149280304Sjkim std::vector<std::string> InstrsForCase; 150280304Sjkim InstrsForCase.resize(UniqueOperandCommands.size()); 151280304Sjkim InstOpsUsed.assign(UniqueOperandCommands.size(), 0); 152280304Sjkim 153280304Sjkim for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 154280304Sjkim const AsmWriterInst *Inst = getAsmWriterInstByID(i); 155280304Sjkim if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc. 156280304Sjkim 157280304Sjkim std::string Command; 158280304Sjkim if (Inst->Operands.empty()) 159280304Sjkim continue; // Instruction already done. 160280304Sjkim 161280304Sjkim Command = " " + Inst->Operands[0].getCode() + "\n"; 162280304Sjkim 163280304Sjkim // Check to see if we already have 'Command' in UniqueOperandCommands. 16468651Skris // If not, add it. 16568651Skris bool FoundIt = false; 166280304Sjkim for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx) 167280304Sjkim if (UniqueOperandCommands[idx] == Command) { 168280304Sjkim InstIdxs[i] = idx; 169280304Sjkim InstrsForCase[idx] += ", "; 170280304Sjkim InstrsForCase[idx] += Inst->CGI->TheDef->getName(); 171280304Sjkim FoundIt = true; 172280304Sjkim break; 173280304Sjkim } 174280304Sjkim if (!FoundIt) { 175280304Sjkim InstIdxs[i] = UniqueOperandCommands.size(); 176280304Sjkim UniqueOperandCommands.push_back(Command); 177280304Sjkim InstrsForCase.push_back(Inst->CGI->TheDef->getName()); 178280304Sjkim 179280304Sjkim // This command matches one operand so far. 180280304Sjkim InstOpsUsed.push_back(1); 181280304Sjkim } 182280304Sjkim } 183280304Sjkim 184280304Sjkim // For each entry of UniqueOperandCommands, there is a set of instructions 185280304Sjkim // that uses it. If the next command of all instructions in the set are 186280304Sjkim // identical, fold it into the command. 18768651Skris for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size(); 18868651Skris CommandIdx != e; ++CommandIdx) { 189280304Sjkim 190280304Sjkim for (unsigned Op = 1; ; ++Op) { 191280304Sjkim // Scan for the first instruction in the set. 19268651Skris std::vector<unsigned>::iterator NIT = 193280304Sjkim std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx); 194280304Sjkim if (NIT == InstIdxs.end()) break; // No commonality. 195280304Sjkim 196280304Sjkim // If this instruction has no more operands, we isn't anything to merge 197280304Sjkim // into this command. 198280304Sjkim const AsmWriterInst *FirstInst = 199280304Sjkim getAsmWriterInstByID(NIT-InstIdxs.begin()); 200280304Sjkim if (!FirstInst || FirstInst->Operands.size() == Op) 201280304Sjkim break; 202280304Sjkim 203280304Sjkim // Otherwise, scan to see if all of the other instructions in this command 204280304Sjkim // set share the operand. 205280304Sjkim bool AllSame = true; 206280304Sjkim // Keep track of the maximum, number of operands or any 207280304Sjkim // instruction we see in the group. 208280304Sjkim size_t MaxSize = FirstInst->Operands.size(); 209280304Sjkim 210280304Sjkim for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx); 211280304Sjkim NIT != InstIdxs.end(); 212280304Sjkim NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) { 21368651Skris // Okay, found another instruction in this command set. If the operand 21468651Skris // matches, we're ok, otherwise bail out. 215280304Sjkim const AsmWriterInst *OtherInst = 216280304Sjkim getAsmWriterInstByID(NIT-InstIdxs.begin()); 217280304Sjkim 21868651Skris if (OtherInst && 219280304Sjkim OtherInst->Operands.size() > FirstInst->Operands.size()) 220280304Sjkim MaxSize = std::max(MaxSize, OtherInst->Operands.size()); 221280304Sjkim 222280304Sjkim if (!OtherInst || OtherInst->Operands.size() == Op || 223280304Sjkim OtherInst->Operands[Op] != FirstInst->Operands[Op]) { 224280304Sjkim AllSame = false; 225280304Sjkim break; 226280304Sjkim } 227280304Sjkim } 228280304Sjkim if (!AllSame) break; 229280304Sjkim 230280304Sjkim // Okay, everything in this command set has the same next operand. Add it 231280304Sjkim // to UniqueOperandCommands and remember that it was consumed. 232280304Sjkim std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n"; 233280304Sjkim 234280304Sjkim UniqueOperandCommands[CommandIdx] += Command; 235280304Sjkim InstOpsUsed[CommandIdx]++; 236280304Sjkim } 237280304Sjkim } 238280304Sjkim 23968651Skris // Prepend some of the instructions each case is used for onto the case val. 240160814Ssimon for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) { 241280304Sjkim std::string Instrs = InstrsForCase[i]; 242280304Sjkim if (Instrs.size() > 70) { 243160814Ssimon Instrs.erase(Instrs.begin()+70, Instrs.end()); 244280304Sjkim Instrs += "..."; 245280304Sjkim } 246280304Sjkim 247280304Sjkim if (!Instrs.empty()) 248280304Sjkim UniqueOperandCommands[i] = " // " + Instrs + "\n" + 249280304Sjkim UniqueOperandCommands[i]; 250280304Sjkim } 251280304Sjkim} 252280304Sjkim 253280304Sjkim 254280304Sjkimstatic void UnescapeString(std::string &Str) { 255280304Sjkim for (unsigned i = 0; i != Str.size(); ++i) { 256280304Sjkim if (Str[i] == '\\' && i != Str.size()-1) { 257280304Sjkim switch (Str[i+1]) { 258280304Sjkim default: continue; // Don't execute the code after the switch. 259280304Sjkim case 'a': Str[i] = '\a'; break; 260280304Sjkim case 'b': Str[i] = '\b'; break; 261280304Sjkim case 'e': Str[i] = 27; break; 262280304Sjkim case 'f': Str[i] = '\f'; break; 263280304Sjkim case 'n': Str[i] = '\n'; break; 264280304Sjkim case 'r': Str[i] = '\r'; break; 265280304Sjkim case 't': Str[i] = '\t'; break; 266280304Sjkim case 'v': Str[i] = '\v'; break; 267280304Sjkim case '"': Str[i] = '\"'; break; 268280304Sjkim case '\'': Str[i] = '\''; break; 269280304Sjkim case '\\': Str[i] = '\\'; break; 270280304Sjkim } 271280304Sjkim // Nuke the second character. 272280304Sjkim Str.erase(Str.begin()+i+1); 273280304Sjkim } 274280304Sjkim } 275280304Sjkim} 276280304Sjkim 277280304Sjkim/// EmitPrintInstruction - Generate the code for the "printInstruction" method 278280304Sjkim/// implementation. Destroys all instances of AsmWriterInst information, by 279280304Sjkim/// clearing the Instructions vector. 280160814Ssimonvoid AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) { 281280304Sjkim Record *AsmWriter = Target.getAsmWriter(); 282280304Sjkim std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); 283160814Ssimon bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter"); 284280304Sjkim const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr"; 285280304Sjkim 286280304Sjkim O << 287280304Sjkim "/// printInstruction - This method is automatically generated by tablegen\n" 288280304Sjkim "/// from the instruction set description.\n" 289280304Sjkim "void " << Target.getName() << ClassName 290280304Sjkim << "::printInstruction(const " << MachineInstrClassName 291280304Sjkim << " *MI, raw_ostream &O) {\n"; 292280304Sjkim 293280304Sjkim // Build an aggregate string, and build a table of offsets into it. 294280304Sjkim SequenceToOffsetTable<std::string> StringTable; 295280304Sjkim 296280304Sjkim /// OpcodeInfo - This encodes the index of the string to use for the first 297280304Sjkim /// chunk of the output as well as indices used for operand printing. 298280304Sjkim /// To reduce the number of unhandled cases, we expand the size from 32-bit 299160814Ssimon /// to 32+16 = 48-bit. 300280304Sjkim std::vector<uint64_t> OpcodeInfo; 301280304Sjkim 302280304Sjkim // Add all strings to the string table upfront so it can generate an optimized 303280304Sjkim // representation. 304280304Sjkim for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 305280304Sjkim AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]]; 306280304Sjkim if (AWI != 0 && 307280304Sjkim AWI->Operands[0].OperandType == 308109998Smarkm AsmWriterOperand::isLiteralTextOperand && 309280304Sjkim !AWI->Operands[0].Str.empty()) { 310109998Smarkm std::string Str = AWI->Operands[0].Str; 311280304Sjkim UnescapeString(Str); 312109998Smarkm StringTable.add(Str); 313280304Sjkim } 314280304Sjkim } 315280304Sjkim 316109998Smarkm StringTable.layout(); 317280304Sjkim 318280304Sjkim unsigned MaxStringIdx = 0; 319280304Sjkim for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 320280304Sjkim AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]]; 321280304Sjkim unsigned Idx; 322280304Sjkim if (AWI == 0) { 323280304Sjkim // Something not handled by the asmwriter printer. 324280304Sjkim Idx = ~0U; 325280304Sjkim } else if (AWI->Operands[0].OperandType != 326280304Sjkim AsmWriterOperand::isLiteralTextOperand || 327280304Sjkim AWI->Operands[0].Str.empty()) { 328280304Sjkim // Something handled by the asmwriter printer, but with no leading string. 329280304Sjkim Idx = StringTable.get(""); 330280304Sjkim } else { 331280304Sjkim std::string Str = AWI->Operands[0].Str; 332280304Sjkim UnescapeString(Str); 333280304Sjkim Idx = StringTable.get(Str); 334280304Sjkim MaxStringIdx = std::max(MaxStringIdx, Idx); 335280304Sjkim 336280304Sjkim // Nuke the string from the operand list. It is now handled! 337280304Sjkim AWI->Operands.erase(AWI->Operands.begin()); 338280304Sjkim } 339280304Sjkim 34068651Skris // Bias offset by one since we want 0 as a sentinel. 341280304Sjkim OpcodeInfo.push_back(Idx+1); 342280304Sjkim } 343280304Sjkim 344280304Sjkim // Figure out how many bits we used for the string index. 345238405Sjkim unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2); 346280304Sjkim 347280304Sjkim // To reduce code size, we compactify common instructions into a few bits 348280304Sjkim // in the opcode-indexed table. 349280304Sjkim unsigned BitsLeft = 64-AsmStrBits; 350280304Sjkim 351280304Sjkim std::vector<std::vector<std::string> > TableDrivenOperandPrinters; 352280304Sjkim 353280304Sjkim while (1) { 354280304Sjkim std::vector<std::string> UniqueOperandCommands; 355238405Sjkim std::vector<unsigned> InstIdxs; 356280304Sjkim std::vector<unsigned> NumInstOpsHandled; 357280304Sjkim FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs, 358280304Sjkim NumInstOpsHandled); 359280304Sjkim 360280304Sjkim // If we ran out of operands to print, we're done. 361280304Sjkim if (UniqueOperandCommands.empty()) break; 362280304Sjkim 363280304Sjkim // Compute the number of bits we need to represent these cases, this is 364280304Sjkim // ceil(log2(numentries)). 365280304Sjkim unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size()); 366280304Sjkim 367280304Sjkim // If we don't have enough bits for this operand, don't include it. 368280304Sjkim if (NumBits > BitsLeft) { 369238405Sjkim DEBUG(errs() << "Not enough bits to densely encode " << NumBits 370280304Sjkim << " more bits\n"); 371280304Sjkim break; 372238405Sjkim } 373238405Sjkim 374280304Sjkim // Otherwise, we can include this in the initial lookup table. Add it in. 375280304Sjkim for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i) 376280304Sjkim if (InstIdxs[i] != ~0U) { 377238405Sjkim OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft); 378280304Sjkim } 379280304Sjkim BitsLeft -= NumBits; 380280304Sjkim 381 // Remove the info about this operand. 382 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 383 if (AsmWriterInst *Inst = getAsmWriterInstByID(i)) 384 if (!Inst->Operands.empty()) { 385 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]]; 386 assert(NumOps <= Inst->Operands.size() && 387 "Can't remove this many ops!"); 388 Inst->Operands.erase(Inst->Operands.begin(), 389 Inst->Operands.begin()+NumOps); 390 } 391 } 392 393 // Remember the handlers for this set of operands. 394 TableDrivenOperandPrinters.push_back(UniqueOperandCommands); 395 } 396 397 398 // We always emit at least one 32-bit table. A second table is emitted if 399 // more bits are needed. 400 O<<" static const uint32_t OpInfo[] = {\n"; 401 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 402 O << " " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// " 403 << NumberedInstructions[i]->TheDef->getName() << "\n"; 404 } 405 // Add a dummy entry so the array init doesn't end with a comma. 406 O << " 0U\n"; 407 O << " };\n\n"; 408 409 if (BitsLeft < 32) { 410 // Add a second OpInfo table only when it is necessary. 411 // Adjust the type of the second table based on the number of bits needed. 412 O << " static const uint" 413 << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8") 414 << "_t OpInfo2[] = {\n"; 415 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { 416 O << " " << (OpcodeInfo[i] >> 32) << "U,\t// " 417 << NumberedInstructions[i]->TheDef->getName() << "\n"; 418 } 419 // Add a dummy entry so the array init doesn't end with a comma. 420 O << " 0U\n"; 421 O << " };\n\n"; 422 } 423 424 // Emit the string itself. 425 O << " const char AsmStrs[] = {\n"; 426 StringTable.emit(O, printChar); 427 O << " };\n\n"; 428 429 O << " O << \"\\t\";\n\n"; 430 431 O << " // Emit the opcode for the instruction.\n"; 432 if (BitsLeft < 32) { 433 // If we have two tables then we need to perform two lookups and combine 434 // the results into a single 64-bit value. 435 O << " uint64_t Bits1 = OpInfo[MI->getOpcode()];\n" 436 << " uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n" 437 << " uint64_t Bits = (Bits2 << 32) | Bits1;\n"; 438 } else { 439 // If only one table is used we just need to perform a single lookup. 440 O << " uint32_t Bits = OpInfo[MI->getOpcode()];\n"; 441 } 442 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n" 443 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n"; 444 445 // Output the table driven operand information. 446 BitsLeft = 64-AsmStrBits; 447 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) { 448 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i]; 449 450 // Compute the number of bits we need to represent these cases, this is 451 // ceil(log2(numentries)). 452 unsigned NumBits = Log2_32_Ceil(Commands.size()); 453 assert(NumBits <= BitsLeft && "consistency error"); 454 455 // Emit code to extract this field from Bits. 456 O << "\n // Fragment " << i << " encoded into " << NumBits 457 << " bits for " << Commands.size() << " unique commands.\n"; 458 459 if (Commands.size() == 2) { 460 // Emit two possibilitys with if/else. 461 O << " if ((Bits >> " 462 << (64-BitsLeft) << ") & " 463 << ((1 << NumBits)-1) << ") {\n" 464 << Commands[1] 465 << " } else {\n" 466 << Commands[0] 467 << " }\n\n"; 468 } else if (Commands.size() == 1) { 469 // Emit a single possibility. 470 O << Commands[0] << "\n\n"; 471 } else { 472 O << " switch ((Bits >> " 473 << (64-BitsLeft) << ") & " 474 << ((1 << NumBits)-1) << ") {\n" 475 << " default: // unreachable.\n"; 476 477 // Print out all the cases. 478 for (unsigned i = 0, e = Commands.size(); i != e; ++i) { 479 O << " case " << i << ":\n"; 480 O << Commands[i]; 481 O << " break;\n"; 482 } 483 O << " }\n\n"; 484 } 485 BitsLeft -= NumBits; 486 } 487 488 // Okay, delete instructions with no operand info left. 489 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { 490 // Entire instruction has been emitted? 491 AsmWriterInst &Inst = Instructions[i]; 492 if (Inst.Operands.empty()) { 493 Instructions.erase(Instructions.begin()+i); 494 --i; --e; 495 } 496 } 497 498 499 // Because this is a vector, we want to emit from the end. Reverse all of the 500 // elements in the vector. 501 std::reverse(Instructions.begin(), Instructions.end()); 502 503 504 // Now that we've emitted all of the operand info that fit into 32 bits, emit 505 // information for those instructions that are left. This is a less dense 506 // encoding, but we expect the main 32-bit table to handle the majority of 507 // instructions. 508 if (!Instructions.empty()) { 509 // Find the opcode # of inline asm. 510 O << " switch (MI->getOpcode()) {\n"; 511 while (!Instructions.empty()) 512 EmitInstructions(Instructions, O); 513 514 O << " }\n"; 515 O << " return;\n"; 516 } 517 518 O << "}\n"; 519} 520 521static void 522emitRegisterNameString(raw_ostream &O, StringRef AltName, 523 const std::vector<CodeGenRegister*> &Registers) { 524 SequenceToOffsetTable<std::string> StringTable; 525 SmallVector<std::string, 4> AsmNames(Registers.size()); 526 for (unsigned i = 0, e = Registers.size(); i != e; ++i) { 527 const CodeGenRegister &Reg = *Registers[i]; 528 std::string &AsmName = AsmNames[i]; 529 530 // "NoRegAltName" is special. We don't need to do a lookup for that, 531 // as it's just a reference to the default register name. 532 if (AltName == "" || AltName == "NoRegAltName") { 533 AsmName = Reg.TheDef->getValueAsString("AsmName"); 534 if (AsmName.empty()) 535 AsmName = Reg.getName(); 536 } else { 537 // Make sure the register has an alternate name for this index. 538 std::vector<Record*> AltNameList = 539 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices"); 540 unsigned Idx = 0, e; 541 for (e = AltNameList.size(); 542 Idx < e && (AltNameList[Idx]->getName() != AltName); 543 ++Idx) 544 ; 545 // If the register has an alternate name for this index, use it. 546 // Otherwise, leave it empty as an error flag. 547 if (Idx < e) { 548 std::vector<std::string> AltNames = 549 Reg.TheDef->getValueAsListOfStrings("AltNames"); 550 if (AltNames.size() <= Idx) 551 PrintFatalError(Reg.TheDef->getLoc(), 552 (Twine("Register definition missing alt name for '") + 553 AltName + "'.").str()); 554 AsmName = AltNames[Idx]; 555 } 556 } 557 StringTable.add(AsmName); 558 } 559 560 StringTable.layout(); 561 O << " static const char AsmStrs" << AltName << "[] = {\n"; 562 StringTable.emit(O, printChar); 563 O << " };\n\n"; 564 565 O << " static const uint32_t RegAsmOffset" << AltName << "[] = {"; 566 for (unsigned i = 0, e = Registers.size(); i != e; ++i) { 567 if ((i % 14) == 0) 568 O << "\n "; 569 O << StringTable.get(AsmNames[i]) << ", "; 570 } 571 O << "\n };\n" 572 << "\n"; 573} 574 575void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) { 576 Record *AsmWriter = Target.getAsmWriter(); 577 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); 578 const std::vector<CodeGenRegister*> &Registers = 579 Target.getRegBank().getRegisters(); 580 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices(); 581 bool hasAltNames = AltNameIndices.size() > 1; 582 583 O << 584 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n" 585 "/// from the register set description. This returns the assembler name\n" 586 "/// for the specified register.\n" 587 "const char *" << Target.getName() << ClassName << "::"; 588 if (hasAltNames) 589 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n"; 590 else 591 O << "getRegisterName(unsigned RegNo) {\n"; 592 O << " assert(RegNo && RegNo < " << (Registers.size()+1) 593 << " && \"Invalid register number!\");\n" 594 << "\n"; 595 596 if (hasAltNames) { 597 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) 598 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers); 599 } else 600 emitRegisterNameString(O, "", Registers); 601 602 if (hasAltNames) { 603 O << " const uint32_t *RegAsmOffset;\n" 604 << " const char *AsmStrs;\n" 605 << " switch(AltIdx) {\n" 606 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n"; 607 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) { 608 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace"); 609 StringRef AltName(AltNameIndices[i]->getName()); 610 O << " case " << Namespace << "::" << AltName 611 << ":\n" 612 << " AsmStrs = AsmStrs" << AltName << ";\n" 613 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n" 614 << " break;\n"; 615 } 616 O << "}\n"; 617 } 618 619 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n" 620 << " \"Invalid alt name index for register!\");\n" 621 << " return AsmStrs+RegAsmOffset[RegNo-1];\n" 622 << "}\n"; 623} 624 625namespace { 626// IAPrinter - Holds information about an InstAlias. Two InstAliases match if 627// they both have the same conditionals. In which case, we cannot print out the 628// alias for that pattern. 629class IAPrinter { 630 std::vector<std::string> Conds; 631 std::map<StringRef, unsigned> OpMap; 632 std::string Result; 633 std::string AsmString; 634 SmallVector<Record*, 4> ReqFeatures; 635public: 636 IAPrinter(std::string R, std::string AS) 637 : Result(R), AsmString(AS) {} 638 639 void addCond(const std::string &C) { Conds.push_back(C); } 640 641 void addOperand(StringRef Op, unsigned Idx) { 642 assert(Idx < 0xFF && "Index too large!"); 643 OpMap[Op] = Idx; 644 } 645 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; } 646 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); } 647 648 void print(raw_ostream &O) { 649 if (Conds.empty() && ReqFeatures.empty()) { 650 O.indent(6) << "return true;\n"; 651 return; 652 } 653 654 O << "if ("; 655 656 for (std::vector<std::string>::iterator 657 I = Conds.begin(), E = Conds.end(); I != E; ++I) { 658 if (I != Conds.begin()) { 659 O << " &&\n"; 660 O.indent(8); 661 } 662 663 O << *I; 664 } 665 666 O << ") {\n"; 667 O.indent(6) << "// " << Result << "\n"; 668 669 // Directly mangle mapped operands into the string. Each operand is 670 // identified by a '$' sign followed by a byte identifying the number of the 671 // operand. We add one to the index to avoid zero bytes. 672 std::pair<StringRef, StringRef> ASM = StringRef(AsmString).split(' '); 673 SmallString<128> OutString = ASM.first; 674 if (!ASM.second.empty()) { 675 raw_svector_ostream OS(OutString); 676 OS << ' '; 677 for (StringRef::iterator I = ASM.second.begin(), E = ASM.second.end(); 678 I != E;) { 679 OS << *I; 680 if (*I == '$') { 681 StringRef::iterator Start = ++I; 682 while (I != E && 683 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') || 684 (*I >= '0' && *I <= '9') || *I == '_')) 685 ++I; 686 StringRef Name(Start, I - Start); 687 assert(isOpMapped(Name) && "Unmapped operand!"); 688 OS << format("\\x%02X", (unsigned char)getOpIndex(Name) + 1); 689 } else { 690 ++I; 691 } 692 } 693 } 694 695 // Emit the string. 696 O.indent(6) << "AsmString = \"" << OutString.str() << "\";\n"; 697 698 O.indent(6) << "break;\n"; 699 O.indent(4) << '}'; 700 } 701 702 bool operator==(const IAPrinter &RHS) { 703 if (Conds.size() != RHS.Conds.size()) 704 return false; 705 706 unsigned Idx = 0; 707 for (std::vector<std::string>::iterator 708 I = Conds.begin(), E = Conds.end(); I != E; ++I) 709 if (*I != RHS.Conds[Idx++]) 710 return false; 711 712 return true; 713 } 714 715 bool operator()(const IAPrinter &RHS) { 716 if (Conds.size() < RHS.Conds.size()) 717 return true; 718 719 unsigned Idx = 0; 720 for (std::vector<std::string>::iterator 721 I = Conds.begin(), E = Conds.end(); I != E; ++I) 722 if (*I != RHS.Conds[Idx++]) 723 return *I < RHS.Conds[Idx++]; 724 725 return false; 726 } 727}; 728 729} // end anonymous namespace 730 731static unsigned CountNumOperands(StringRef AsmString) { 732 unsigned NumOps = 0; 733 std::pair<StringRef, StringRef> ASM = AsmString.split(' '); 734 735 while (!ASM.second.empty()) { 736 ++NumOps; 737 ASM = ASM.second.split(' '); 738 } 739 740 return NumOps; 741} 742 743static unsigned CountResultNumOperands(StringRef AsmString) { 744 unsigned NumOps = 0; 745 std::pair<StringRef, StringRef> ASM = AsmString.split('\t'); 746 747 if (!ASM.second.empty()) { 748 size_t I = ASM.second.find('{'); 749 StringRef Str = ASM.second; 750 if (I != StringRef::npos) 751 Str = ASM.second.substr(I, ASM.second.find('|', I)); 752 753 ASM = Str.split(' '); 754 755 do { 756 ++NumOps; 757 ASM = ASM.second.split(' '); 758 } while (!ASM.second.empty()); 759 } 760 761 return NumOps; 762} 763 764void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) { 765 Record *AsmWriter = Target.getAsmWriter(); 766 767 if (!AsmWriter->getValueAsBit("isMCAsmWriter")) 768 return; 769 770 O << "\n#ifdef PRINT_ALIAS_INSTR\n"; 771 O << "#undef PRINT_ALIAS_INSTR\n\n"; 772 773 // Emit the method that prints the alias instruction. 774 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); 775 776 std::vector<Record*> AllInstAliases = 777 Records.getAllDerivedDefinitions("InstAlias"); 778 779 // Create a map from the qualified name to a list of potential matches. 780 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap; 781 for (std::vector<Record*>::iterator 782 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) { 783 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target); 784 const Record *R = *I; 785 if (!R->getValueAsBit("EmitAlias")) 786 continue; // We were told not to emit the alias, but to emit the aliasee. 787 const DagInit *DI = R->getValueAsDag("ResultInst"); 788 const DefInit *Op = cast<DefInit>(DI->getOperator()); 789 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias); 790 } 791 792 // A map of which conditions need to be met for each instruction operand 793 // before it can be matched to the mnemonic. 794 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap; 795 796 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator 797 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) { 798 std::vector<CodeGenInstAlias*> &Aliases = I->second; 799 800 for (std::vector<CodeGenInstAlias*>::iterator 801 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) { 802 const CodeGenInstAlias *CGA = *II; 803 unsigned LastOpNo = CGA->ResultInstOperandIndex.size(); 804 unsigned NumResultOps = 805 CountResultNumOperands(CGA->ResultInst->AsmString); 806 807 // Don't emit the alias if it has more operands than what it's aliasing. 808 if (NumResultOps < CountNumOperands(CGA->AsmString)) 809 continue; 810 811 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(), 812 CGA->AsmString); 813 814 std::string Cond; 815 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo); 816 IAP->addCond(Cond); 817 818 bool CantHandle = false; 819 820 for (unsigned i = 0, e = LastOpNo; i != e; ++i) { 821 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i]; 822 823 switch (RO.Kind) { 824 case CodeGenInstAlias::ResultOperand::K_Record: { 825 const Record *Rec = RO.getRecord(); 826 StringRef ROName = RO.getName(); 827 828 829 if (Rec->isSubClassOf("RegisterOperand")) 830 Rec = Rec->getValueAsDef("RegClass"); 831 if (Rec->isSubClassOf("RegisterClass")) { 832 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()"; 833 IAP->addCond(Cond); 834 835 if (!IAP->isOpMapped(ROName)) { 836 IAP->addOperand(ROName, i); 837 Record *R = CGA->ResultOperands[i].getRecord(); 838 if (R->isSubClassOf("RegisterOperand")) 839 R = R->getValueAsDef("RegClass"); 840 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" + 841 R->getName() + "RegClassID)" 842 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())"; 843 IAP->addCond(Cond); 844 } else { 845 Cond = std::string("MI->getOperand(") + 846 llvm::utostr(i) + ").getReg() == MI->getOperand(" + 847 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()"; 848 IAP->addCond(Cond); 849 } 850 } else { 851 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!"); 852 // FIXME: We may need to handle these situations. 853 delete IAP; 854 IAP = 0; 855 CantHandle = true; 856 break; 857 } 858 859 break; 860 } 861 case CodeGenInstAlias::ResultOperand::K_Imm: { 862 std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")"; 863 864 // Just because the alias has an immediate result, doesn't mean the 865 // MCInst will. An MCExpr could be present, for example. 866 IAP->addCond(Op + ".isImm()"); 867 868 Cond = Op + ".getImm() == " 869 + llvm::utostr(CGA->ResultOperands[i].getImm()); 870 IAP->addCond(Cond); 871 break; 872 } 873 case CodeGenInstAlias::ResultOperand::K_Reg: 874 // If this is zero_reg, something's playing tricks we're not 875 // equipped to handle. 876 if (!CGA->ResultOperands[i].getRegister()) { 877 CantHandle = true; 878 break; 879 } 880 881 Cond = std::string("MI->getOperand(") + 882 llvm::utostr(i) + ").getReg() == " + Target.getName() + 883 "::" + CGA->ResultOperands[i].getRegister()->getName(); 884 IAP->addCond(Cond); 885 break; 886 } 887 888 if (!IAP) break; 889 } 890 891 if (CantHandle) continue; 892 IAPrinterMap[I->first].push_back(IAP); 893 } 894 } 895 896 std::string Header; 897 raw_string_ostream HeaderO(Header); 898 899 HeaderO << "bool " << Target.getName() << ClassName 900 << "::printAliasInstr(const MCInst" 901 << " *MI, raw_ostream &OS) {\n"; 902 903 std::string Cases; 904 raw_string_ostream CasesO(Cases); 905 906 for (std::map<std::string, std::vector<IAPrinter*> >::iterator 907 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) { 908 std::vector<IAPrinter*> &IAPs = I->second; 909 std::vector<IAPrinter*> UniqueIAPs; 910 911 for (std::vector<IAPrinter*>::iterator 912 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) { 913 IAPrinter *LHS = *II; 914 bool IsDup = false; 915 for (std::vector<IAPrinter*>::iterator 916 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) { 917 IAPrinter *RHS = *III; 918 if (LHS != RHS && *LHS == *RHS) { 919 IsDup = true; 920 break; 921 } 922 } 923 924 if (!IsDup) UniqueIAPs.push_back(LHS); 925 } 926 927 if (UniqueIAPs.empty()) continue; 928 929 CasesO.indent(2) << "case " << I->first << ":\n"; 930 931 for (std::vector<IAPrinter*>::iterator 932 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) { 933 IAPrinter *IAP = *II; 934 CasesO.indent(4); 935 IAP->print(CasesO); 936 CasesO << '\n'; 937 } 938 939 CasesO.indent(4) << "return false;\n"; 940 } 941 942 if (CasesO.str().empty()) { 943 O << HeaderO.str(); 944 O << " return false;\n"; 945 O << "}\n\n"; 946 O << "#endif // PRINT_ALIAS_INSTR\n"; 947 return; 948 } 949 950 O << HeaderO.str(); 951 O.indent(2) << "const char *AsmString;\n"; 952 O.indent(2) << "switch (MI->getOpcode()) {\n"; 953 O.indent(2) << "default: return false;\n"; 954 O << CasesO.str(); 955 O.indent(2) << "}\n\n"; 956 957 // Code that prints the alias, replacing the operands with the ones from the 958 // MCInst. 959 O << " unsigned I = 0;\n"; 960 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\0')\n"; 961 O << " ++I;\n"; 962 O << " OS << '\\t' << StringRef(AsmString, I);\n"; 963 964 O << " if (AsmString[I] != '\\0') {\n"; 965 O << " OS << '\\t';\n"; 966 O << " do {\n"; 967 O << " if (AsmString[I] == '$') {\n"; 968 O << " ++I;\n"; 969 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, OS);\n"; 970 O << " } else {\n"; 971 O << " OS << AsmString[I++];\n"; 972 O << " }\n"; 973 O << " } while (AsmString[I] != '\\0');\n"; 974 O << " }\n\n"; 975 976 O << " return true;\n"; 977 O << "}\n\n"; 978 979 O << "#endif // PRINT_ALIAS_INSTR\n"; 980} 981 982AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) { 983 Record *AsmWriter = Target.getAsmWriter(); 984 for (CodeGenTarget::inst_iterator I = Target.inst_begin(), 985 E = Target.inst_end(); 986 I != E; ++I) 987 if (!(*I)->AsmString.empty() && (*I)->TheDef->getName() != "PHI") 988 Instructions.push_back( 989 AsmWriterInst(**I, AsmWriter->getValueAsInt("Variant"), 990 AsmWriter->getValueAsInt("FirstOperandColumn"), 991 AsmWriter->getValueAsInt("OperandSpacing"))); 992 993 // Get the instruction numbering. 994 NumberedInstructions = Target.getInstructionsByEnumValue(); 995 996 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not 997 // all machine instructions are necessarily being printed, so there may be 998 // target instructions not in this map. 999 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) 1000 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i])); 1001} 1002 1003void AsmWriterEmitter::run(raw_ostream &O) { 1004 EmitPrintInstruction(O); 1005 EmitGetRegisterName(O); 1006 EmitPrintAliasInstruction(O); 1007} 1008 1009 1010namespace llvm { 1011 1012void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) { 1013 emitSourceFileHeader("Assembly Writer Source Fragment", OS); 1014 AsmWriterEmitter(RK).run(OS); 1015} 1016 1017} // End llvm namespace 1018