1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is emits an assembly printer for the current target.
11 // Note that this is currently fairly skeletal, but will grow over time.
13 //===----------------------------------------------------------------------===//
15 #include "AsmWriterInst.h"
16 #include "CodeGenTarget.h"
17 #include "SequenceToOffsetTable.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Record.h"
25 #include "llvm/TableGen/TableGenBackend.h"
33 class AsmWriterEmitter {
34 RecordKeeper &Records;
36 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
37 const std::vector<const CodeGenInstruction*> *NumberedInstructions;
38 std::vector<AsmWriterInst> Instructions;
40 AsmWriterEmitter(RecordKeeper &R);
42 void run(raw_ostream &o);
45 void EmitPrintInstruction(raw_ostream &o);
46 void EmitGetRegisterName(raw_ostream &o);
47 void EmitPrintAliasInstruction(raw_ostream &O);
49 AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
50 assert(ID < NumberedInstructions->size());
51 std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
52 CGIAWIMap.find(NumberedInstructions->at(ID));
53 assert(I != CGIAWIMap.end() && "Didn't find inst!");
56 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
57 std::vector<unsigned> &InstIdxs,
58 std::vector<unsigned> &InstOpsUsed) const;
60 } // end anonymous namespace
62 static void PrintCases(std::vector<std::pair<std::string,
63 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
64 O << " case " << OpsToPrint.back().first << ": ";
65 AsmWriterOperand TheOp = OpsToPrint.back().second;
66 OpsToPrint.pop_back();
68 // Check to see if any other operands are identical in this list, and if so,
69 // emit a case label for them.
70 for (unsigned i = OpsToPrint.size(); i != 0; --i)
71 if (OpsToPrint[i-1].second == TheOp) {
72 O << "\n case " << OpsToPrint[i-1].first << ": ";
73 OpsToPrint.erase(OpsToPrint.begin()+i-1);
76 // Finally, emit the code.
82 /// EmitInstructions - Emit the last instruction in the vector and any other
83 /// instructions that are suitably similar to it.
84 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
86 AsmWriterInst FirstInst = Insts.back();
89 std::vector<AsmWriterInst> SimilarInsts;
90 unsigned DifferingOperand = ~0;
91 for (unsigned i = Insts.size(); i != 0; --i) {
92 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
94 if (DifferingOperand == ~0U) // First match!
95 DifferingOperand = DiffOp;
97 // If this differs in the same operand as the rest of the instructions in
98 // this class, move it to the SimilarInsts list.
99 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
100 SimilarInsts.push_back(Insts[i-1]);
101 Insts.erase(Insts.begin()+i-1);
106 O << " case " << FirstInst.CGI->Namespace << "::"
107 << FirstInst.CGI->TheDef->getName() << ":\n";
108 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
109 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
110 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
111 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
112 if (i != DifferingOperand) {
113 // If the operand is the same for all instructions, just print it.
114 O << " " << FirstInst.Operands[i].getCode();
116 // If this is the operand that varies between all of the instructions,
117 // emit a switch for just this operand now.
118 O << " switch (MI->getOpcode()) {\n";
119 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
120 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
121 FirstInst.CGI->TheDef->getName(),
122 FirstInst.Operands[i]));
124 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
125 AsmWriterInst &AWI = SimilarInsts[si];
126 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
127 AWI.CGI->TheDef->getName(),
130 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
131 while (!OpsToPrint.empty())
132 PrintCases(OpsToPrint, O);
140 void AsmWriterEmitter::
141 FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
142 std::vector<unsigned> &InstIdxs,
143 std::vector<unsigned> &InstOpsUsed) const {
144 InstIdxs.assign(NumberedInstructions->size(), ~0U);
146 // This vector parallels UniqueOperandCommands, keeping track of which
147 // instructions each case are used for. It is a comma separated string of
149 std::vector<std::string> InstrsForCase;
150 InstrsForCase.resize(UniqueOperandCommands.size());
151 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
153 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
154 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
156 continue; // PHI, INLINEASM, CFI_INSTRUCTION, etc.
159 if (Inst->Operands.empty())
160 continue; // Instruction already done.
162 Command = " " + Inst->Operands[0].getCode() + "\n";
164 // Check to see if we already have 'Command' in UniqueOperandCommands.
166 bool FoundIt = false;
167 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
168 if (UniqueOperandCommands[idx] == Command) {
170 InstrsForCase[idx] += ", ";
171 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
176 InstIdxs[i] = UniqueOperandCommands.size();
177 UniqueOperandCommands.push_back(Command);
178 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
180 // This command matches one operand so far.
181 InstOpsUsed.push_back(1);
185 // For each entry of UniqueOperandCommands, there is a set of instructions
186 // that uses it. If the next command of all instructions in the set are
187 // identical, fold it into the command.
188 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
189 CommandIdx != e; ++CommandIdx) {
191 for (unsigned Op = 1; ; ++Op) {
192 // Scan for the first instruction in the set.
193 std::vector<unsigned>::iterator NIT =
194 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
195 if (NIT == InstIdxs.end()) break; // No commonality.
197 // If this instruction has no more operands, we isn't anything to merge
198 // into this command.
199 const AsmWriterInst *FirstInst =
200 getAsmWriterInstByID(NIT-InstIdxs.begin());
201 if (!FirstInst || FirstInst->Operands.size() == Op)
204 // Otherwise, scan to see if all of the other instructions in this command
205 // set share the operand.
207 // Keep track of the maximum, number of operands or any
208 // instruction we see in the group.
209 size_t MaxSize = FirstInst->Operands.size();
211 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
212 NIT != InstIdxs.end();
213 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
214 // Okay, found another instruction in this command set. If the operand
215 // matches, we're ok, otherwise bail out.
216 const AsmWriterInst *OtherInst =
217 getAsmWriterInstByID(NIT-InstIdxs.begin());
220 OtherInst->Operands.size() > FirstInst->Operands.size())
221 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
223 if (!OtherInst || OtherInst->Operands.size() == Op ||
224 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
231 // Okay, everything in this command set has the same next operand. Add it
232 // to UniqueOperandCommands and remember that it was consumed.
233 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
235 UniqueOperandCommands[CommandIdx] += Command;
236 InstOpsUsed[CommandIdx]++;
240 // Prepend some of the instructions each case is used for onto the case val.
241 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
242 std::string Instrs = InstrsForCase[i];
243 if (Instrs.size() > 70) {
244 Instrs.erase(Instrs.begin()+70, Instrs.end());
249 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
250 UniqueOperandCommands[i];
255 static void UnescapeString(std::string &Str) {
256 for (unsigned i = 0; i != Str.size(); ++i) {
257 if (Str[i] == '\\' && i != Str.size()-1) {
259 default: continue; // Don't execute the code after the switch.
260 case 'a': Str[i] = '\a'; break;
261 case 'b': Str[i] = '\b'; break;
262 case 'e': Str[i] = 27; break;
263 case 'f': Str[i] = '\f'; break;
264 case 'n': Str[i] = '\n'; break;
265 case 'r': Str[i] = '\r'; break;
266 case 't': Str[i] = '\t'; break;
267 case 'v': Str[i] = '\v'; break;
268 case '"': Str[i] = '\"'; break;
269 case '\'': Str[i] = '\''; break;
270 case '\\': Str[i] = '\\'; break;
272 // Nuke the second character.
273 Str.erase(Str.begin()+i+1);
278 /// EmitPrintInstruction - Generate the code for the "printInstruction" method
279 /// implementation. Destroys all instances of AsmWriterInst information, by
280 /// clearing the Instructions vector.
281 void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
282 Record *AsmWriter = Target.getAsmWriter();
283 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
286 "/// printInstruction - This method is automatically generated by tablegen\n"
287 "/// from the instruction set description.\n"
288 "void " << Target.getName() << ClassName
289 << "::printInstruction(const MCInst *MI, raw_ostream &O) {\n";
291 // Build an aggregate string, and build a table of offsets into it.
292 SequenceToOffsetTable<std::string> StringTable;
294 /// OpcodeInfo - This encodes the index of the string to use for the first
295 /// chunk of the output as well as indices used for operand printing.
296 /// To reduce the number of unhandled cases, we expand the size from 32-bit
297 /// to 32+16 = 48-bit.
298 std::vector<uint64_t> OpcodeInfo;
300 // Add all strings to the string table upfront so it can generate an optimized
302 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
303 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
305 AWI->Operands[0].OperandType ==
306 AsmWriterOperand::isLiteralTextOperand &&
307 !AWI->Operands[0].Str.empty()) {
308 std::string Str = AWI->Operands[0].Str;
310 StringTable.add(Str);
314 StringTable.layout();
316 unsigned MaxStringIdx = 0;
317 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
318 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
321 // Something not handled by the asmwriter printer.
323 } else if (AWI->Operands[0].OperandType !=
324 AsmWriterOperand::isLiteralTextOperand ||
325 AWI->Operands[0].Str.empty()) {
326 // Something handled by the asmwriter printer, but with no leading string.
327 Idx = StringTable.get("");
329 std::string Str = AWI->Operands[0].Str;
331 Idx = StringTable.get(Str);
332 MaxStringIdx = std::max(MaxStringIdx, Idx);
334 // Nuke the string from the operand list. It is now handled!
335 AWI->Operands.erase(AWI->Operands.begin());
338 // Bias offset by one since we want 0 as a sentinel.
339 OpcodeInfo.push_back(Idx+1);
342 // Figure out how many bits we used for the string index.
343 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
345 // To reduce code size, we compactify common instructions into a few bits
346 // in the opcode-indexed table.
347 unsigned BitsLeft = 64-AsmStrBits;
349 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
352 std::vector<std::string> UniqueOperandCommands;
353 std::vector<unsigned> InstIdxs;
354 std::vector<unsigned> NumInstOpsHandled;
355 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
358 // If we ran out of operands to print, we're done.
359 if (UniqueOperandCommands.empty()) break;
361 // Compute the number of bits we need to represent these cases, this is
362 // ceil(log2(numentries)).
363 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
365 // If we don't have enough bits for this operand, don't include it.
366 if (NumBits > BitsLeft) {
367 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
372 // Otherwise, we can include this in the initial lookup table. Add it in.
373 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
374 if (InstIdxs[i] != ~0U) {
375 OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft);
379 // Remove the info about this operand.
380 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
381 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
382 if (!Inst->Operands.empty()) {
383 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
384 assert(NumOps <= Inst->Operands.size() &&
385 "Can't remove this many ops!");
386 Inst->Operands.erase(Inst->Operands.begin(),
387 Inst->Operands.begin()+NumOps);
391 // Remember the handlers for this set of operands.
392 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
396 // We always emit at least one 32-bit table. A second table is emitted if
397 // more bits are needed.
398 O<<" static const uint32_t OpInfo[] = {\n";
399 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
400 O << " " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// "
401 << NumberedInstructions->at(i)->TheDef->getName() << "\n";
403 // Add a dummy entry so the array init doesn't end with a comma.
408 // Add a second OpInfo table only when it is necessary.
409 // Adjust the type of the second table based on the number of bits needed.
410 O << " static const uint"
411 << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8")
412 << "_t OpInfo2[] = {\n";
413 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
414 O << " " << (OpcodeInfo[i] >> 32) << "U,\t// "
415 << NumberedInstructions->at(i)->TheDef->getName() << "\n";
417 // Add a dummy entry so the array init doesn't end with a comma.
422 // Emit the string itself.
423 O << " const char AsmStrs[] = {\n";
424 StringTable.emit(O, printChar);
427 O << " O << \"\\t\";\n\n";
429 O << " // Emit the opcode for the instruction.\n";
431 // If we have two tables then we need to perform two lookups and combine
432 // the results into a single 64-bit value.
433 O << " uint64_t Bits1 = OpInfo[MI->getOpcode()];\n"
434 << " uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n"
435 << " uint64_t Bits = (Bits2 << 32) | Bits1;\n";
437 // If only one table is used we just need to perform a single lookup.
438 O << " uint32_t Bits = OpInfo[MI->getOpcode()];\n";
440 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
441 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
443 // Output the table driven operand information.
444 BitsLeft = 64-AsmStrBits;
445 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
446 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
448 // Compute the number of bits we need to represent these cases, this is
449 // ceil(log2(numentries)).
450 unsigned NumBits = Log2_32_Ceil(Commands.size());
451 assert(NumBits <= BitsLeft && "consistency error");
453 // Emit code to extract this field from Bits.
454 O << "\n // Fragment " << i << " encoded into " << NumBits
455 << " bits for " << Commands.size() << " unique commands.\n";
457 if (Commands.size() == 2) {
458 // Emit two possibilitys with if/else.
459 O << " if ((Bits >> "
460 << (64-BitsLeft) << ") & "
461 << ((1 << NumBits)-1) << ") {\n"
466 } else if (Commands.size() == 1) {
467 // Emit a single possibility.
468 O << Commands[0] << "\n\n";
470 O << " switch ((Bits >> "
471 << (64-BitsLeft) << ") & "
472 << ((1 << NumBits)-1) << ") {\n"
473 << " default: // unreachable.\n";
475 // Print out all the cases.
476 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
477 O << " case " << i << ":\n";
486 // Okay, delete instructions with no operand info left.
487 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
488 // Entire instruction has been emitted?
489 AsmWriterInst &Inst = Instructions[i];
490 if (Inst.Operands.empty()) {
491 Instructions.erase(Instructions.begin()+i);
497 // Because this is a vector, we want to emit from the end. Reverse all of the
498 // elements in the vector.
499 std::reverse(Instructions.begin(), Instructions.end());
502 // Now that we've emitted all of the operand info that fit into 32 bits, emit
503 // information for those instructions that are left. This is a less dense
504 // encoding, but we expect the main 32-bit table to handle the majority of
506 if (!Instructions.empty()) {
507 // Find the opcode # of inline asm.
508 O << " switch (MI->getOpcode()) {\n";
509 while (!Instructions.empty())
510 EmitInstructions(Instructions, O);
520 emitRegisterNameString(raw_ostream &O, StringRef AltName,
521 const std::vector<CodeGenRegister*> &Registers) {
522 SequenceToOffsetTable<std::string> StringTable;
523 SmallVector<std::string, 4> AsmNames(Registers.size());
524 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
525 const CodeGenRegister &Reg = *Registers[i];
526 std::string &AsmName = AsmNames[i];
528 // "NoRegAltName" is special. We don't need to do a lookup for that,
529 // as it's just a reference to the default register name.
530 if (AltName == "" || AltName == "NoRegAltName") {
531 AsmName = Reg.TheDef->getValueAsString("AsmName");
533 AsmName = Reg.getName();
535 // Make sure the register has an alternate name for this index.
536 std::vector<Record*> AltNameList =
537 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
539 for (e = AltNameList.size();
540 Idx < e && (AltNameList[Idx]->getName() != AltName);
543 // If the register has an alternate name for this index, use it.
544 // Otherwise, leave it empty as an error flag.
546 std::vector<std::string> AltNames =
547 Reg.TheDef->getValueAsListOfStrings("AltNames");
548 if (AltNames.size() <= Idx)
549 PrintFatalError(Reg.TheDef->getLoc(),
550 (Twine("Register definition missing alt name for '") +
551 AltName + "'.").str());
552 AsmName = AltNames[Idx];
555 StringTable.add(AsmName);
558 StringTable.layout();
559 O << " static const char AsmStrs" << AltName << "[] = {\n";
560 StringTable.emit(O, printChar);
563 O << " static const uint32_t RegAsmOffset" << AltName << "[] = {";
564 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
567 O << StringTable.get(AsmNames[i]) << ", ";
573 void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
574 Record *AsmWriter = Target.getAsmWriter();
575 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
576 const std::vector<CodeGenRegister*> &Registers =
577 Target.getRegBank().getRegisters();
578 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
579 bool hasAltNames = AltNameIndices.size() > 1;
582 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
583 "/// from the register set description. This returns the assembler name\n"
584 "/// for the specified register.\n"
585 "const char *" << Target.getName() << ClassName << "::";
587 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
589 O << "getRegisterName(unsigned RegNo) {\n";
590 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
591 << " && \"Invalid register number!\");\n"
595 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
596 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
598 emitRegisterNameString(O, "", Registers);
601 O << " const uint32_t *RegAsmOffset;\n"
602 << " const char *AsmStrs;\n"
603 << " switch(AltIdx) {\n"
604 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
605 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
606 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
607 StringRef AltName(AltNameIndices[i]->getName());
608 O << " case " << Namespace << "::" << AltName
610 << " AsmStrs = AsmStrs" << AltName << ";\n"
611 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
617 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
618 << " \"Invalid alt name index for register!\");\n"
619 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
624 // IAPrinter - Holds information about an InstAlias. Two InstAliases match if
625 // they both have the same conditionals. In which case, we cannot print out the
626 // alias for that pattern.
628 std::vector<std::string> Conds;
629 std::map<StringRef, unsigned> OpMap;
631 std::string AsmString;
632 SmallVector<Record*, 4> ReqFeatures;
634 IAPrinter(std::string R, std::string AS)
635 : Result(R), AsmString(AS) {}
637 void addCond(const std::string &C) { Conds.push_back(C); }
639 void addOperand(StringRef Op, unsigned Idx) {
640 assert(Idx < 0xFF && "Index too large!");
643 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
644 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
646 void print(raw_ostream &O) {
647 if (Conds.empty() && ReqFeatures.empty()) {
648 O.indent(6) << "return true;\n";
654 for (std::vector<std::string>::iterator
655 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
656 if (I != Conds.begin()) {
665 O.indent(6) << "// " << Result << "\n";
667 // Directly mangle mapped operands into the string. Each operand is
668 // identified by a '$' sign followed by a byte identifying the number of the
669 // operand. We add one to the index to avoid zero bytes.
670 std::pair<StringRef, StringRef> ASM = StringRef(AsmString).split(' ');
671 SmallString<128> OutString = ASM.first;
672 if (!ASM.second.empty()) {
673 raw_svector_ostream OS(OutString);
675 for (StringRef::iterator I = ASM.second.begin(), E = ASM.second.end();
679 StringRef::iterator Start = ++I;
681 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
682 (*I >= '0' && *I <= '9') || *I == '_'))
684 StringRef Name(Start, I - Start);
685 assert(isOpMapped(Name) && "Unmapped operand!");
686 OS << format("\\x%02X", (unsigned char)getOpIndex(Name) + 1);
694 O.indent(6) << "AsmString = \"" << OutString.str() << "\";\n";
696 O.indent(6) << "break;\n";
700 bool operator==(const IAPrinter &RHS) {
701 if (Conds.size() != RHS.Conds.size())
705 for (std::vector<std::string>::iterator
706 I = Conds.begin(), E = Conds.end(); I != E; ++I)
707 if (*I != RHS.Conds[Idx++])
713 bool operator()(const IAPrinter &RHS) {
714 if (Conds.size() < RHS.Conds.size())
718 for (std::vector<std::string>::iterator
719 I = Conds.begin(), E = Conds.end(); I != E; ++I)
720 if (*I != RHS.Conds[Idx++])
721 return *I < RHS.Conds[Idx++];
727 } // end anonymous namespace
729 static unsigned CountNumOperands(StringRef AsmString) {
731 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
733 while (!ASM.second.empty()) {
735 ASM = ASM.second.split(' ');
741 static unsigned CountResultNumOperands(StringRef AsmString) {
743 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
745 if (!ASM.second.empty()) {
746 size_t I = ASM.second.find('{');
747 StringRef Str = ASM.second;
748 if (I != StringRef::npos)
749 Str = ASM.second.substr(I, ASM.second.find('|', I));
751 ASM = Str.split(' ');
755 ASM = ASM.second.split(' ');
756 } while (!ASM.second.empty());
762 void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
763 Record *AsmWriter = Target.getAsmWriter();
765 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
766 O << "#undef PRINT_ALIAS_INSTR\n\n";
768 // Emit the method that prints the alias instruction.
769 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
771 std::vector<Record*> AllInstAliases =
772 Records.getAllDerivedDefinitions("InstAlias");
774 // Create a map from the qualified name to a list of potential matches.
775 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
776 for (std::vector<Record*>::iterator
777 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
778 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
779 const Record *R = *I;
780 if (!R->getValueAsBit("EmitAlias"))
781 continue; // We were told not to emit the alias, but to emit the aliasee.
782 const DagInit *DI = R->getValueAsDag("ResultInst");
783 const DefInit *Op = cast<DefInit>(DI->getOperator());
784 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
787 // A map of which conditions need to be met for each instruction operand
788 // before it can be matched to the mnemonic.
789 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
791 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
792 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
793 std::vector<CodeGenInstAlias*> &Aliases = I->second;
795 for (std::vector<CodeGenInstAlias*>::iterator
796 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
797 const CodeGenInstAlias *CGA = *II;
798 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
799 unsigned NumResultOps =
800 CountResultNumOperands(CGA->ResultInst->AsmString);
802 // Don't emit the alias if it has more operands than what it's aliasing.
803 if (NumResultOps < CountNumOperands(CGA->AsmString))
806 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
810 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
813 bool CantHandle = false;
815 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
816 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
819 case CodeGenInstAlias::ResultOperand::K_Record: {
820 const Record *Rec = RO.getRecord();
821 StringRef ROName = RO.getName();
824 if (Rec->isSubClassOf("RegisterOperand"))
825 Rec = Rec->getValueAsDef("RegClass");
826 if (Rec->isSubClassOf("RegisterClass")) {
827 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
830 if (!IAP->isOpMapped(ROName)) {
831 IAP->addOperand(ROName, i);
832 Record *R = CGA->ResultOperands[i].getRecord();
833 if (R->isSubClassOf("RegisterOperand"))
834 R = R->getValueAsDef("RegClass");
835 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
836 R->getName() + "RegClassID)"
837 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
840 Cond = std::string("MI->getOperand(") +
841 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
842 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
846 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
847 // FIXME: We may need to handle these situations.
856 case CodeGenInstAlias::ResultOperand::K_Imm: {
857 std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")";
859 // Just because the alias has an immediate result, doesn't mean the
860 // MCInst will. An MCExpr could be present, for example.
861 IAP->addCond(Op + ".isImm()");
863 Cond = Op + ".getImm() == "
864 + llvm::utostr(CGA->ResultOperands[i].getImm());
868 case CodeGenInstAlias::ResultOperand::K_Reg:
869 // If this is zero_reg, something's playing tricks we're not
870 // equipped to handle.
871 if (!CGA->ResultOperands[i].getRegister()) {
876 Cond = std::string("MI->getOperand(") +
877 llvm::utostr(i) + ").getReg() == " + Target.getName() +
878 "::" + CGA->ResultOperands[i].getRegister()->getName();
886 if (CantHandle) continue;
887 IAPrinterMap[I->first].push_back(IAP);
892 raw_string_ostream HeaderO(Header);
894 HeaderO << "bool " << Target.getName() << ClassName
895 << "::printAliasInstr(const MCInst"
896 << " *MI, raw_ostream &OS) {\n";
899 raw_string_ostream CasesO(Cases);
901 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
902 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
903 std::vector<IAPrinter*> &IAPs = I->second;
904 std::vector<IAPrinter*> UniqueIAPs;
906 for (std::vector<IAPrinter*>::iterator
907 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
908 IAPrinter *LHS = *II;
910 for (std::vector<IAPrinter*>::iterator
911 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
912 IAPrinter *RHS = *III;
913 if (LHS != RHS && *LHS == *RHS) {
919 if (!IsDup) UniqueIAPs.push_back(LHS);
922 if (UniqueIAPs.empty()) continue;
924 CasesO.indent(2) << "case " << I->first << ":\n";
926 for (std::vector<IAPrinter*>::iterator
927 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
928 IAPrinter *IAP = *II;
934 CasesO.indent(4) << "return false;\n";
937 if (CasesO.str().empty()) {
939 O << " return false;\n";
941 O << "#endif // PRINT_ALIAS_INSTR\n";
946 O.indent(2) << "const char *AsmString;\n";
947 O.indent(2) << "switch (MI->getOpcode()) {\n";
948 O.indent(2) << "default: return false;\n";
950 O.indent(2) << "}\n\n";
952 // Code that prints the alias, replacing the operands with the ones from the
954 O << " unsigned I = 0;\n";
955 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\0')\n";
957 O << " OS << '\\t' << StringRef(AsmString, I);\n";
959 O << " if (AsmString[I] != '\\0') {\n";
960 O << " OS << '\\t';\n";
962 O << " if (AsmString[I] == '$') {\n";
964 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, OS);\n";
966 O << " OS << AsmString[I++];\n";
968 O << " } while (AsmString[I] != '\\0');\n";
971 O << " return true;\n";
974 O << "#endif // PRINT_ALIAS_INSTR\n";
977 AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
978 Record *AsmWriter = Target.getAsmWriter();
979 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
980 E = Target.inst_end();
982 if (!(*I)->AsmString.empty() && (*I)->TheDef->getName() != "PHI")
983 Instructions.push_back(
984 AsmWriterInst(**I, AsmWriter->getValueAsInt("Variant"),
985 AsmWriter->getValueAsInt("OperandSpacing")));
987 // Get the instruction numbering.
988 NumberedInstructions = &Target.getInstructionsByEnumValue();
990 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
991 // all machine instructions are necessarily being printed, so there may be
992 // target instructions not in this map.
993 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
994 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
997 void AsmWriterEmitter::run(raw_ostream &O) {
998 EmitPrintInstruction(O);
999 EmitGetRegisterName(O);
1000 EmitPrintAliasInstruction(O);
1006 void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1007 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1008 AsmWriterEmitter(RK).run(OS);
1011 } // End llvm namespace