Merge operand info and asmstr idx into a single 32-bit field. No other change.
[oota-llvm.git] / utils / TableGen / AsmWriterEmitter.cpp
1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AsmWriterEmitter.h"
16 #include "CodeGenTarget.h"
17 #include "Record.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/MathExtras.h"
21 #include <algorithm>
22 #include <ostream>
23 using namespace llvm;
24
25 static bool isIdentChar(char C) {
26   return (C >= 'a' && C <= 'z') ||
27          (C >= 'A' && C <= 'Z') ||
28          (C >= '0' && C <= '9') ||
29          C == '_';
30 }
31
32 namespace {
33   struct AsmWriterOperand {
34     enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
35
36     /// Str - For isLiteralTextOperand, this IS the literal text.  For
37     /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
38     std::string Str;
39
40     /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
41     /// machine instruction.
42     unsigned MIOpNo;
43     
44     /// MiModifier - For isMachineInstrOperand, this is the modifier string for
45     /// an operand, specified with syntax like ${opname:modifier}.
46     std::string MiModifier;
47
48     AsmWriterOperand(const std::string &LitStr)
49       : OperandType(isLiteralTextOperand), Str(LitStr) {}
50
51     AsmWriterOperand(const std::string &Printer, unsigned OpNo, 
52                      const std::string &Modifier) 
53       : OperandType(isMachineInstrOperand), Str(Printer), MIOpNo(OpNo),
54       MiModifier(Modifier) {}
55
56     bool operator!=(const AsmWriterOperand &Other) const {
57       if (OperandType != Other.OperandType || Str != Other.Str) return true;
58       if (OperandType == isMachineInstrOperand)
59         return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
60       return false;
61     }
62     bool operator==(const AsmWriterOperand &Other) const {
63       return !operator!=(Other);
64     }
65     
66     /// getCode - Return the code that prints this operand.
67     std::string getCode() const;
68   };
69 }
70
71 namespace llvm {
72   struct AsmWriterInst {
73     std::vector<AsmWriterOperand> Operands;
74     const CodeGenInstruction *CGI;
75
76     AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
77
78     /// MatchesAllButOneOp - If this instruction is exactly identical to the
79     /// specified instruction except for one differing operand, return the
80     /// differing operand number.  Otherwise return ~0.
81     unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
82
83   private:
84     void AddLiteralString(const std::string &Str) {
85       // If the last operand was already a literal text string, append this to
86       // it, otherwise add a new operand.
87       if (!Operands.empty() &&
88           Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
89         Operands.back().Str.append(Str);
90       else
91         Operands.push_back(AsmWriterOperand(Str));
92     }
93   };
94 }
95
96
97 std::string AsmWriterOperand::getCode() const {
98   if (OperandType == isLiteralTextOperand)
99     return "O << \"" + Str + "\"; ";
100
101   std::string Result = Str + "(MI, " + utostr(MIOpNo);
102   if (!MiModifier.empty())
103     Result += ", \"" + MiModifier + '"';
104   return Result + "); ";
105 }
106
107
108 /// ParseAsmString - Parse the specified Instruction's AsmString into this
109 /// AsmWriterInst.
110 ///
111 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
112   this->CGI = &CGI;
113   unsigned CurVariant = ~0U;  // ~0 if we are outside a {.|.|.} region, other #.
114
115   // NOTE: Any extensions to this code need to be mirrored in the 
116   // AsmPrinter::printInlineAsm code that executes as compile time (assuming
117   // that inline asm strings should also get the new feature)!
118   const std::string &AsmString = CGI.AsmString;
119   std::string::size_type LastEmitted = 0;
120   while (LastEmitted != AsmString.size()) {
121     std::string::size_type DollarPos =
122       AsmString.find_first_of("${|}", LastEmitted);
123     if (DollarPos == std::string::npos) DollarPos = AsmString.size();
124
125     // Emit a constant string fragment.
126     if (DollarPos != LastEmitted) {
127       // TODO: this should eventually handle escaping.
128       if (CurVariant == Variant || CurVariant == ~0U)
129         AddLiteralString(std::string(AsmString.begin()+LastEmitted,
130                                      AsmString.begin()+DollarPos));
131       LastEmitted = DollarPos;
132     } else if (AsmString[DollarPos] == '{') {
133       if (CurVariant != ~0U)
134         throw "Nested variants found for instruction '" +
135               CGI.TheDef->getName() + "'!";
136       LastEmitted = DollarPos+1;
137       CurVariant = 0;   // We are now inside of the variant!
138     } else if (AsmString[DollarPos] == '|') {
139       if (CurVariant == ~0U)
140         throw "'|' character found outside of a variant in instruction '"
141           + CGI.TheDef->getName() + "'!";
142       ++CurVariant;
143       ++LastEmitted;
144     } else if (AsmString[DollarPos] == '}') {
145       if (CurVariant == ~0U)
146         throw "'}' character found outside of a variant in instruction '"
147           + CGI.TheDef->getName() + "'!";
148       ++LastEmitted;
149       CurVariant = ~0U;
150     } else if (DollarPos+1 != AsmString.size() &&
151                AsmString[DollarPos+1] == '$') {
152       if (CurVariant == Variant || CurVariant == ~0U) 
153         AddLiteralString("$");  // "$$" -> $
154       LastEmitted = DollarPos+2;
155     } else {
156       // Get the name of the variable.
157       std::string::size_type VarEnd = DollarPos+1;
158
159       // handle ${foo}bar as $foo by detecting whether the character following
160       // the dollar sign is a curly brace.  If so, advance VarEnd and DollarPos
161       // so the variable name does not contain the leading curly brace.
162       bool hasCurlyBraces = false;
163       if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
164         hasCurlyBraces = true;
165         ++DollarPos;
166         ++VarEnd;
167       }
168
169       while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
170         ++VarEnd;
171       std::string VarName(AsmString.begin()+DollarPos+1,
172                           AsmString.begin()+VarEnd);
173
174       // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
175       // into printOperand.
176       std::string Modifier;
177       
178       // In order to avoid starting the next string at the terminating curly
179       // brace, advance the end position past it if we found an opening curly
180       // brace.
181       if (hasCurlyBraces) {
182         if (VarEnd >= AsmString.size())
183           throw "Reached end of string before terminating curly brace in '"
184                 + CGI.TheDef->getName() + "'";
185         
186         // Look for a modifier string.
187         if (AsmString[VarEnd] == ':') {
188           ++VarEnd;
189           if (VarEnd >= AsmString.size())
190             throw "Reached end of string before terminating curly brace in '"
191               + CGI.TheDef->getName() + "'";
192           
193           unsigned ModifierStart = VarEnd;
194           while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
195             ++VarEnd;
196           Modifier = std::string(AsmString.begin()+ModifierStart,
197                                  AsmString.begin()+VarEnd);
198           if (Modifier.empty())
199             throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
200         }
201         
202         if (AsmString[VarEnd] != '}')
203           throw "Variable name beginning with '{' did not end with '}' in '"
204                 + CGI.TheDef->getName() + "'";
205         ++VarEnd;
206       }
207       if (VarName.empty())
208         throw "Stray '$' in '" + CGI.TheDef->getName() +
209               "' asm string, maybe you want $$?";
210
211       unsigned OpNo = CGI.getOperandNamed(VarName);
212       CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
213
214       // If this is a two-address instruction and we are not accessing the
215       // 0th operand, remove an operand.
216       unsigned MIOp = OpInfo.MIOperandNo;
217       if (CGI.isTwoAddress && MIOp != 0) {
218         if (MIOp == 1)
219           throw "Should refer to operand #0 instead of #1 for two-address"
220             " instruction '" + CGI.TheDef->getName() + "'!";
221         --MIOp;
222       }
223
224       if (CurVariant == Variant || CurVariant == ~0U) 
225         Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
226                                             Modifier));
227       LastEmitted = VarEnd;
228     }
229   }
230
231   AddLiteralString("\\n");
232 }
233
234 /// MatchesAllButOneOp - If this instruction is exactly identical to the
235 /// specified instruction except for one differing operand, return the differing
236 /// operand number.  If more than one operand mismatches, return ~1, otherwise
237 /// if the instructions are identical return ~0.
238 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
239   if (Operands.size() != Other.Operands.size()) return ~1;
240
241   unsigned MismatchOperand = ~0U;
242   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
243     if (Operands[i] != Other.Operands[i])
244       if (MismatchOperand != ~0U)  // Already have one mismatch?
245         return ~1U;
246       else
247         MismatchOperand = i;
248   }
249   return MismatchOperand;
250 }
251
252 static void PrintCases(std::vector<std::pair<std::string,
253                        AsmWriterOperand> > &OpsToPrint, std::ostream &O) {
254   O << "    case " << OpsToPrint.back().first << ": ";
255   AsmWriterOperand TheOp = OpsToPrint.back().second;
256   OpsToPrint.pop_back();
257
258   // Check to see if any other operands are identical in this list, and if so,
259   // emit a case label for them.
260   for (unsigned i = OpsToPrint.size(); i != 0; --i)
261     if (OpsToPrint[i-1].second == TheOp) {
262       O << "\n    case " << OpsToPrint[i-1].first << ": ";
263       OpsToPrint.erase(OpsToPrint.begin()+i-1);
264     }
265
266   // Finally, emit the code.
267   O << TheOp.getCode();
268   O << "break;\n";
269 }
270
271
272 /// EmitInstructions - Emit the last instruction in the vector and any other
273 /// instructions that are suitably similar to it.
274 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
275                              std::ostream &O) {
276   AsmWriterInst FirstInst = Insts.back();
277   Insts.pop_back();
278
279   std::vector<AsmWriterInst> SimilarInsts;
280   unsigned DifferingOperand = ~0;
281   for (unsigned i = Insts.size(); i != 0; --i) {
282     unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
283     if (DiffOp != ~1U) {
284       if (DifferingOperand == ~0U)  // First match!
285         DifferingOperand = DiffOp;
286
287       // If this differs in the same operand as the rest of the instructions in
288       // this class, move it to the SimilarInsts list.
289       if (DifferingOperand == DiffOp || DiffOp == ~0U) {
290         SimilarInsts.push_back(Insts[i-1]);
291         Insts.erase(Insts.begin()+i-1);
292       }
293     }
294   }
295
296   O << "  case " << FirstInst.CGI->Namespace << "::"
297     << FirstInst.CGI->TheDef->getName() << ":\n";
298   for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
299     O << "  case " << SimilarInsts[i].CGI->Namespace << "::"
300       << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
301   for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
302     if (i != DifferingOperand) {
303       // If the operand is the same for all instructions, just print it.
304       O << "    " << FirstInst.Operands[i].getCode();
305     } else {
306       // If this is the operand that varies between all of the instructions,
307       // emit a switch for just this operand now.
308       O << "    switch (MI->getOpcode()) {\n";
309       std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
310       OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
311                                           FirstInst.CGI->TheDef->getName(),
312                                           FirstInst.Operands[i]));
313
314       for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
315         AsmWriterInst &AWI = SimilarInsts[si];
316         OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
317                                             AWI.CGI->TheDef->getName(),
318                                             AWI.Operands[i]));
319       }
320       std::reverse(OpsToPrint.begin(), OpsToPrint.end());
321       while (!OpsToPrint.empty())
322         PrintCases(OpsToPrint, O);
323       O << "    }";
324     }
325     O << "\n";
326   }
327
328   O << "    break;\n";
329 }
330
331 void AsmWriterEmitter::
332 FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands, 
333                           std::vector<unsigned> &InstIdxs, unsigned Op) const {
334   InstIdxs.clear();
335   InstIdxs.resize(NumberedInstructions.size());
336   
337   // This vector parallels UniqueOperandCommands, keeping track of which
338   // instructions each case are used for.  It is a comma separated string of
339   // enums.
340   std::vector<std::string> InstrsForCase;
341   InstrsForCase.resize(UniqueOperandCommands.size());
342   
343   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
344     const AsmWriterInst *Inst = getAsmWriterInstByID(i);
345     if (Inst == 0) continue;  // PHI, INLINEASM, etc.
346     
347     std::string Command;
348     if (Op > Inst->Operands.size())
349       continue;   // Instruction already done.
350     else if (Op == Inst->Operands.size())
351       Command = "    return true;\n";
352     else
353       Command = "    " + Inst->Operands[Op].getCode() + "\n";
354     
355     // Check to see if we already have 'Command' in UniqueOperandCommands.
356     // If not, add it.
357     bool FoundIt = false;
358     for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
359       if (UniqueOperandCommands[idx] == Command) {
360         InstIdxs[i] = idx;
361         InstrsForCase[idx] += ", ";
362         InstrsForCase[idx] += Inst->CGI->TheDef->getName();
363         FoundIt = true;
364         break;
365       }
366     if (!FoundIt) {
367       InstIdxs[i] = UniqueOperandCommands.size();
368       UniqueOperandCommands.push_back(Command);
369       InstrsForCase.push_back(Inst->CGI->TheDef->getName());
370     }
371   }
372   
373   // Prepend some of the instructions each case is used for onto the case val.
374   for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
375     std::string Instrs = InstrsForCase[i];
376     if (Instrs.size() > 70) {
377       Instrs.erase(Instrs.begin()+70, Instrs.end());
378       Instrs += "...";
379     }
380     
381     if (!Instrs.empty())
382       UniqueOperandCommands[i] = "    // " + Instrs + "\n" + 
383         UniqueOperandCommands[i];
384   }
385 }
386
387
388
389 void AsmWriterEmitter::run(std::ostream &O) {
390   EmitSourceFileHeader("Assembly Writer Source Fragment", O);
391
392   CodeGenTarget Target;
393   Record *AsmWriter = Target.getAsmWriter();
394   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
395   unsigned Variant = AsmWriter->getValueAsInt("Variant");
396
397   O <<
398   "/// printInstruction - This method is automatically generated by tablegen\n"
399   "/// from the instruction set description.  This method returns true if the\n"
400   "/// machine instruction was sufficiently described to print it, otherwise\n"
401   "/// it returns false.\n"
402     "bool " << Target.getName() << ClassName
403             << "::printInstruction(const MachineInstr *MI) {\n";
404
405   std::vector<AsmWriterInst> Instructions;
406
407   for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
408          E = Target.inst_end(); I != E; ++I)
409     if (!I->second.AsmString.empty())
410       Instructions.push_back(AsmWriterInst(I->second, Variant));
411
412   // Get the instruction numbering.
413   Target.getInstructionsByEnumValue(NumberedInstructions);
414   
415   // Compute the CodeGenInstruction -> AsmWriterInst mapping.  Note that not
416   // all machine instructions are necessarily being printed, so there may be
417   // target instructions not in this map.
418   for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
419     CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
420
421   // Build an aggregate string, and build a table of offsets into it.
422   std::map<std::string, unsigned> StringOffset;
423   std::string AggregateString;
424   AggregateString += '\0';
425   
426   /// OpcodeInfo - Theis encodes the index of the string to use for the first
427   /// chunk of the output as well as indices used for operand printing.
428   std::vector<unsigned> OpcodeInfo;
429   
430   unsigned MaxStringIdx = 0;
431   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
432     AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
433     unsigned Idx;
434     if (AWI == 0 || AWI->Operands[0].Str.empty()) {
435       // Something not handled by the asmwriter printer.
436       Idx = 0;
437     } else {
438       unsigned &Entry = StringOffset[AWI->Operands[0].Str];
439       if (Entry == 0) {
440         // Add the string to the aggregate if this is the first time found.
441         MaxStringIdx = Entry = AggregateString.size();
442         std::string Str = AWI->Operands[0].Str;
443         UnescapeString(Str);
444         AggregateString += Str;
445         AggregateString += '\0';
446       }
447       Idx = Entry;
448
449       // Nuke the string from the operand list.  It is now handled!
450       AWI->Operands.erase(AWI->Operands.begin());
451     }
452     OpcodeInfo.push_back(Idx);
453   }
454   
455   // Figure out how many bits we used for the string index.
456   unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx);
457   
458   // To reduce code size, we compactify common instructions into a few bits
459   // in the opcode-indexed table.
460   // 16 bits to play with.
461   unsigned BitsLeft = 16;
462
463   std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
464   
465   for (unsigned i = 0; ; ++i) {
466     std::vector<std::string> UniqueOperandCommands;
467
468     // For the first operand check, add a default value that unhandled
469     // instructions will use.
470     if (i == 0)
471       UniqueOperandCommands.push_back("    return false;\n");
472     
473     std::vector<unsigned> InstIdxs;
474     FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs, i);
475     
476     // If we ran out of operands to print, we're done.
477     if (UniqueOperandCommands.empty()) break;
478     
479     // FIXME: GROW THEM MAXIMALLY.
480
481     // Compute the number of bits we need to represent these cases, this is
482     // ceil(log2(numentries)).
483     unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
484     
485     // If we don't have enough bits for this operand, don't include it.
486     if (NumBits > BitsLeft) {
487       DEBUG(std::cerr << "Not enough bits to densely encode " << NumBits
488                       << " more bits\n");
489       break;
490     }
491     
492     // Otherwise, we can include this in the initial lookup table.  Add it in.
493     BitsLeft -= NumBits;
494     for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
495       OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
496     
497     TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
498   }
499   
500   
501   
502   O<<"  static const unsigned OpInfo[] = {\n";
503   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
504     O << "    " << OpcodeInfo[i] << ",\t// "
505       << NumberedInstructions[i]->TheDef->getName() << "\n";
506   }
507   // Add a dummy entry so the array init doesn't end with a comma.
508   O << "    0U\n";
509   O << "  };\n\n";
510   
511   // Emit the string itself.
512   O << "  const char *AsmStrs = \n    \"";
513   unsigned CharsPrinted = 0;
514   EscapeString(AggregateString);
515   for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
516     if (CharsPrinted > 70) {
517       O << "\"\n    \"";
518       CharsPrinted = 0;
519     }
520     O << AggregateString[i];
521     ++CharsPrinted;
522     
523     // Print escape sequences all together.
524     if (AggregateString[i] == '\\') {
525       assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
526       if (isdigit(AggregateString[i+1])) {
527         assert(isdigit(AggregateString[i+2]) && isdigit(AggregateString[i+3]) &&
528                "Expected 3 digit octal escape!");
529         O << AggregateString[++i];
530         O << AggregateString[++i];
531         O << AggregateString[++i];
532         CharsPrinted += 3;
533       } else {
534         O << AggregateString[++i];
535         ++CharsPrinted;
536       }
537     }
538   }
539   O << "\";\n\n";
540
541   O << "  if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
542     << "    printInlineAsm(MI);\n"
543     << "    return true;\n"
544     << "  }\n\n";
545   
546   O << "  // Emit the opcode for the instruction.\n"
547     << "  unsigned Bits = OpInfo[MI->getOpcode()];\n"
548     << "  O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n\n";
549
550   // Output the table driven operand information.
551   BitsLeft = 16;
552   for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
553     std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
554
555     // Compute the number of bits we need to represent these cases, this is
556     // ceil(log2(numentries)).
557     unsigned NumBits = Log2_32_Ceil(Commands.size());
558     assert(NumBits <= BitsLeft && "consistency error");
559     
560     // Emit code to extract this field from Bits.
561     BitsLeft -= NumBits;
562     
563     O << "\n  // Fragment " << i << " encoded into " << NumBits
564       << " bits for " << Commands.size() << " unique commands.\n"
565       << "  switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
566       << ((1 << NumBits)-1) << ") {\n"
567       << "  default:   // unreachable.\n";
568     
569     // Print out all the cases.
570     for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
571       O << "  case " << i << ":\n";
572       O << Commands[i];
573       O << "    break;\n";
574     }
575     O << "  }\n\n";
576   }
577   
578   // Okay, go through and strip out the operand information that we just
579   // emitted.
580   unsigned NumOpsToRemove = TableDrivenOperandPrinters.size();
581   for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
582     // Entire instruction has been emitted?
583     AsmWriterInst &Inst = Instructions[i];
584     if (Inst.Operands.size() <= NumOpsToRemove) {
585       Instructions.erase(Instructions.begin()+i);
586       --i; --e;      
587     } else {
588       Inst.Operands.erase(Inst.Operands.begin(),
589                           Inst.Operands.begin()+NumOpsToRemove);
590     }
591   }
592
593     
594   // Because this is a vector, we want to emit from the end.  Reverse all of the
595   // elements in the vector.
596   std::reverse(Instructions.begin(), Instructions.end());
597   
598   // Find the opcode # of inline asm
599   O << "  switch (MI->getOpcode()) {\n";
600   while (!Instructions.empty())
601     EmitInstructions(Instructions, O);
602
603   O << "  }\n"
604        "  return true;\n"
605        "}\n";
606 }