blob: a2cc21ded5de8535939ffcead488adc152213665 [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);
Chris Lattner2c384132004-08-17 03:08:28 +000030 O << "namespace llvm {\n\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +000031
32 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +000033 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +000034 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
35 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +000036
Chris Lattner2e1f51b2004-08-01 05:59:33 +000037 O <<
38 "/// printInstruction - This method is automatically generated by tablegen\n"
39 "/// from the instruction set description. This method returns true if the\n"
40 "/// machine instruction was sufficiently described to print it, otherwise\n"
41 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +000042 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +000043 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +000044 O << " switch (MI->getOpcode()) {\n"
45 " default: return false;\n";
46
47 std::string Namespace = Target.inst_begin()->second.Namespace;
48
Chris Lattner953c6fe2004-10-03 20:19:02 +000049 bool inVariant = false; // True if we are inside a {.|.|.} region.
50
Chris Lattner2e1f51b2004-08-01 05:59:33 +000051 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
52 E = Target.inst_end(); I != E; ++I)
53 if (!I->second.AsmString.empty()) {
54 const std::string &AsmString = I->second.AsmString;
Chris Lattner076efa72004-08-01 07:43:02 +000055 O << " case " << Namespace << "::" << I->first << ": O ";
56
57 std::string::size_type LastEmitted = 0;
58 while (LastEmitted != AsmString.size()) {
Chris Lattner953c6fe2004-10-03 20:19:02 +000059 std::string::size_type DollarPos =
60 AsmString.find_first_of("${|}", LastEmitted);
Chris Lattner076efa72004-08-01 07:43:02 +000061 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
62
63 // Emit a constant string fragment.
64 if (DollarPos != LastEmitted) {
65 // TODO: this should eventually handle escaping.
66 O << " << \"" << std::string(AsmString.begin()+LastEmitted,
67 AsmString.begin()+DollarPos) << "\"";
68 LastEmitted = DollarPos;
Chris Lattner953c6fe2004-10-03 20:19:02 +000069 } else if (AsmString[DollarPos] == '{') {
70 if (inVariant)
71 throw "Nested variants found for instruction '" + I->first + "'!";
72 LastEmitted = DollarPos+1;
73 inVariant = true; // We are now inside of the variant!
74 for (unsigned i = 0; i != Variant; ++i) {
75 // Skip over all of the text for an irrelevant variant here. The
76 // next variant starts at |, or there may not be text for this
77 // variant if we see a }.
78 std::string::size_type NP =
79 AsmString.find_first_of("|}", LastEmitted);
80 if (NP == std::string::npos)
81 throw "Incomplete variant for instruction '" + I->first + "'!";
82 LastEmitted = NP+1;
83 if (AsmString[NP] == '}') {
84 inVariant = false; // No text for this variant.
85 break;
86 }
87 }
88 } else if (AsmString[DollarPos] == '|') {
89 if (!inVariant)
90 throw "'|' character found outside of a variant in instruction '"
91 + I->first + "'!";
92 // Move to the end of variant list.
93 std::string::size_type NP = AsmString.find('}', LastEmitted);
94 if (NP == std::string::npos)
95 throw "Incomplete variant for instruction '" + I->first + "'!";
96 LastEmitted = NP+1;
97 inVariant = false;
98 } else if (AsmString[DollarPos] == '}') {
99 if (!inVariant)
100 throw "'}' character found outside of a variant in instruction '"
101 + I->first + "'!";
102 LastEmitted = DollarPos+1;
103 inVariant = false;
Chris Lattner076efa72004-08-01 07:43:02 +0000104 } else if (DollarPos+1 != AsmString.size() &&
105 AsmString[DollarPos+1] == '$') {
106 O << " << '$'"; // "$$" -> $
107 } 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 Lattner2c384132004-08-17 03:08:28 +0000143 O << "} // End llvm namespace \n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000144}