blob: 711a20ba0c8f88ebce6f7b3790ce8f045e70f92c [file] [log] [blame]
Chris Lattner2e1f51b2004-08-01 05:59:33 +00001//===- 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"
Chris Lattner175580c2004-08-14 22:50:53 +000017#include "Record.h"
Chris Lattner2e1f51b2004-08-01 05:59:33 +000018#include <ostream>
19using namespace llvm;
20
Chris Lattner076efa72004-08-01 07:43:02 +000021static bool isIdentChar(char C) {
22 return (C >= 'a' && C <= 'z') ||
23 (C >= 'A' && C <= 'Z') ||
24 (C >= '0' && C <= '9') ||
25 C == '_';
26}
27
Chris Lattner2e1f51b2004-08-01 05:59:33 +000028void AsmWriterEmitter::run(std::ostream &O) {
29 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
30
31 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +000032 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +000033 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
34 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +000035
Chris Lattner2e1f51b2004-08-01 05:59:33 +000036 O <<
37 "/// printInstruction - This method is automatically generated by tablegen\n"
38 "/// from the instruction set description. This method returns true if the\n"
39 "/// machine instruction was sufficiently described to print it, otherwise\n"
40 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +000041 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +000042 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +000043 O << " switch (MI->getOpcode()) {\n"
44 " default: return false;\n";
45
46 std::string Namespace = Target.inst_begin()->second.Namespace;
47
Chris Lattner953c6fe2004-10-03 20:19:02 +000048 bool inVariant = false; // True if we are inside a {.|.|.} region.
49
Chris Lattner2e1f51b2004-08-01 05:59:33 +000050 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
51 E = Target.inst_end(); I != E; ++I)
52 if (!I->second.AsmString.empty()) {
53 const std::string &AsmString = I->second.AsmString;
Chris Lattner076efa72004-08-01 07:43:02 +000054 O << " case " << Namespace << "::" << I->first << ": O ";
55
56 std::string::size_type LastEmitted = 0;
57 while (LastEmitted != AsmString.size()) {
Chris Lattner953c6fe2004-10-03 20:19:02 +000058 std::string::size_type DollarPos =
59 AsmString.find_first_of("${|}", LastEmitted);
Chris Lattner076efa72004-08-01 07:43:02 +000060 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
61
62 // Emit a constant string fragment.
63 if (DollarPos != LastEmitted) {
64 // TODO: this should eventually handle escaping.
65 O << " << \"" << std::string(AsmString.begin()+LastEmitted,
66 AsmString.begin()+DollarPos) << "\"";
67 LastEmitted = DollarPos;
Chris Lattner953c6fe2004-10-03 20:19:02 +000068 } else if (AsmString[DollarPos] == '{') {
69 if (inVariant)
70 throw "Nested variants found for instruction '" + I->first + "'!";
71 LastEmitted = DollarPos+1;
72 inVariant = true; // We are now inside of the variant!
73 for (unsigned i = 0; i != Variant; ++i) {
74 // Skip over all of the text for an irrelevant variant here. The
75 // next variant starts at |, or there may not be text for this
76 // variant if we see a }.
77 std::string::size_type NP =
78 AsmString.find_first_of("|}", LastEmitted);
79 if (NP == std::string::npos)
80 throw "Incomplete variant for instruction '" + I->first + "'!";
81 LastEmitted = NP+1;
82 if (AsmString[NP] == '}') {
83 inVariant = false; // No text for this variant.
84 break;
85 }
86 }
87 } else if (AsmString[DollarPos] == '|') {
88 if (!inVariant)
89 throw "'|' character found outside of a variant in instruction '"
90 + I->first + "'!";
91 // Move to the end of variant list.
92 std::string::size_type NP = AsmString.find('}', LastEmitted);
93 if (NP == std::string::npos)
94 throw "Incomplete variant for instruction '" + I->first + "'!";
95 LastEmitted = NP+1;
96 inVariant = false;
97 } else if (AsmString[DollarPos] == '}') {
98 if (!inVariant)
99 throw "'}' character found outside of a variant in instruction '"
100 + I->first + "'!";
101 LastEmitted = DollarPos+1;
102 inVariant = false;
Chris Lattner076efa72004-08-01 07:43:02 +0000103 } else if (DollarPos+1 != AsmString.size() &&
104 AsmString[DollarPos+1] == '$') {
105 O << " << '$'"; // "$$" -> $
Andrew Lenharth2202bfa2005-01-22 00:35:22 +0000106 LastEmitted = DollarPos+2;
Chris Lattner076efa72004-08-01 07:43:02 +0000107 } else {
108 // Get the name of the variable.
109 // TODO: should eventually handle ${foo}bar as $foo
110 std::string::size_type VarEnd = DollarPos+1;
111 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
112 ++VarEnd;
113 std::string VarName(AsmString.begin()+DollarPos+1,
114 AsmString.begin()+VarEnd);
115 if (VarName.empty())
116 throw "Stray '$' in '" + I->first +
117 "' asm string, maybe you want $$?";
118 unsigned OpNo = I->second.getOperandNamed(VarName);
119
120 // If this is a two-address instruction and we are not accessing the
121 // 0th operand, remove an operand.
Chris Lattner9302ba42004-08-11 04:08:36 +0000122 unsigned MIOp = I->second.OperandList[OpNo].MIOperandNo;
123 if (I->second.isTwoAddress && MIOp != 0) {
124 if (MIOp == 1)
Chris Lattnerc3d5f3e2004-08-01 08:55:34 +0000125 throw "Should refer to operand #0 instead of #1 for two-address"
126 " instruction '" + I->first + "'!";
Chris Lattner9302ba42004-08-11 04:08:36 +0000127 --MIOp;
Chris Lattnerc3d5f3e2004-08-01 08:55:34 +0000128 }
Chris Lattner076efa72004-08-01 07:43:02 +0000129
Chris Lattner1caef2c2004-08-11 02:23:23 +0000130 O << "; " << I->second.OperandList[OpNo].PrinterMethodName
Chris Lattner9302ba42004-08-11 04:08:36 +0000131 << "(MI, " << MIOp << ", MVT::"
Chris Lattner076efa72004-08-01 07:43:02 +0000132 << getName(I->second.OperandList[OpNo].Ty) << "); O ";
133 LastEmitted = VarEnd;
134 }
135 }
136
137 O << " << '\\n'; break;\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000138 }
139
140 O << " }\n"
141 " return true;\n"
142 "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000143}