summaryrefslogtreecommitdiff
path: root/utils/TableGen/AsmWriterEmitter.cpp
blob: a2cc21ded5de8535939ffcead488adc152213665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// This tablegen backend is emits an assembly printer for the current target.
// Note that this is currently fairly skeletal, but will grow over time.
//
//===----------------------------------------------------------------------===//

#include "AsmWriterEmitter.h"
#include "CodeGenTarget.h"
#include "Record.h"
#include <ostream>
using namespace llvm;

static bool isIdentChar(char C) {
  return (C >= 'a' && C <= 'z') ||
         (C >= 'A' && C <= 'Z') ||
         (C >= '0' && C <= '9') ||
         C == '_';
}

void AsmWriterEmitter::run(std::ostream &O) {
  EmitSourceFileHeader("Assembly Writer Source Fragment", O);
  O << "namespace llvm {\n\n";

  CodeGenTarget Target;
  Record *AsmWriter = Target.getAsmWriter();
  std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
  unsigned Variant = AsmWriter->getValueAsInt("Variant");

  O <<
  "/// printInstruction - This method is automatically generated by tablegen\n"
  "/// from the instruction set description.  This method returns true if the\n"
  "/// machine instruction was sufficiently described to print it, otherwise\n"
  "/// it returns false.\n"
    "bool " << Target.getName() << ClassName
            << "::printInstruction(const MachineInstr *MI) {\n";
  O << "  switch (MI->getOpcode()) {\n"
       "  default: return false;\n";

  std::string Namespace = Target.inst_begin()->second.Namespace;

  bool inVariant = false;  // True if we are inside a {.|.|.} region.

  for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
         E = Target.inst_end(); I != E; ++I)
    if (!I->second.AsmString.empty()) {
      const std::string &AsmString = I->second.AsmString;
      O << "  case " << Namespace << "::" << I->first << ": O ";

      std::string::size_type LastEmitted = 0;
      while (LastEmitted != AsmString.size()) {
        std::string::size_type DollarPos =
          AsmString.find_first_of("${|}", LastEmitted);
        if (DollarPos == std::string::npos) DollarPos = AsmString.size();

        // Emit a constant string fragment.
        if (DollarPos != LastEmitted) {
          // TODO: this should eventually handle escaping.
          O << " << \"" << std::string(AsmString.begin()+LastEmitted,
                                       AsmString.begin()+DollarPos) << "\"";
          LastEmitted = DollarPos;
        } else if (AsmString[DollarPos] == '{') {
          if (inVariant)
            throw "Nested variants found for instruction '" + I->first + "'!";
          LastEmitted = DollarPos+1;
          inVariant = true;   // We are now inside of the variant!
          for (unsigned i = 0; i != Variant; ++i) {
            // Skip over all of the text for an irrelevant variant here.  The
            // next variant starts at |, or there may not be text for this
            // variant if we see a }.
            std::string::size_type NP =
              AsmString.find_first_of("|}", LastEmitted);
            if (NP == std::string::npos)
              throw "Incomplete variant for instruction '" + I->first + "'!";
            LastEmitted = NP+1;
            if (AsmString[NP] == '}') {
              inVariant = false;        // No text for this variant.
              break;
            }
          }
        } else if (AsmString[DollarPos] == '|') {
          if (!inVariant)
            throw "'|' character found outside of a variant in instruction '"
                  + I->first + "'!";
          // Move to the end of variant list.
          std::string::size_type NP = AsmString.find('}', LastEmitted);
          if (NP == std::string::npos)
            throw "Incomplete variant for instruction '" + I->first + "'!";
          LastEmitted = NP+1;
          inVariant = false;
        } else if (AsmString[DollarPos] == '}') {
          if (!inVariant)
            throw "'}' character found outside of a variant in instruction '"
                  + I->first + "'!";
          LastEmitted = DollarPos+1;
          inVariant = false;
        } else if (DollarPos+1 != AsmString.size() &&
                   AsmString[DollarPos+1] == '$') {
          O << " << '$'";         // "$$" -> $
        } else {
          // Get the name of the variable.
          // TODO: should eventually handle ${foo}bar as $foo
          std::string::size_type VarEnd = DollarPos+1;
          while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
            ++VarEnd;
          std::string VarName(AsmString.begin()+DollarPos+1,
                              AsmString.begin()+VarEnd);
          if (VarName.empty())
            throw "Stray '$' in '" + I->first +
                  "' asm string, maybe you want $$?";
          unsigned OpNo = I->second.getOperandNamed(VarName);

          // If this is a two-address instruction and we are not accessing the
          // 0th operand, remove an operand.
          unsigned MIOp = I->second.OperandList[OpNo].MIOperandNo;
          if (I->second.isTwoAddress && MIOp != 0) {
            if (MIOp == 1)
              throw "Should refer to operand #0 instead of #1 for two-address"
                    " instruction '" + I->first + "'!";
            --MIOp;
          }

          O << ";  " << I->second.OperandList[OpNo].PrinterMethodName 
            << "(MI, " << MIOp << ", MVT::"
            << getName(I->second.OperandList[OpNo].Ty) << "); O ";
          LastEmitted = VarEnd;
        }
      }

      O << " << '\\n'; break;\n";
    }

  O << "  }\n"
       "  return true;\n"
       "}\n";
  O << "} // End llvm namespace \n";
}