blob: 319fe4f1e98b9cfc2518523eefb114d5de234f97 [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"
17#include <ostream>
18using namespace llvm;
19
Chris Lattner076efa72004-08-01 07:43:02 +000020static bool isIdentChar(char C) {
21 return (C >= 'a' && C <= 'z') ||
22 (C >= 'A' && C <= 'Z') ||
23 (C >= '0' && C <= '9') ||
24 C == '_';
25}
26
Chris Lattner2e1f51b2004-08-01 05:59:33 +000027void AsmWriterEmitter::run(std::ostream &O) {
28 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
29
30 CodeGenTarget Target;
31 O <<
32 "/// printInstruction - This method is automatically generated by tablegen\n"
33 "/// from the instruction set description. This method returns true if the\n"
34 "/// machine instruction was sufficiently described to print it, otherwise\n"
35 "/// it returns false.\n"
36 "bool " << Target.getName()
37 << "AsmPrinter::printInstruction(const MachineInstr *MI) {\n";
38 O << " switch (MI->getOpcode()) {\n"
39 " default: return false;\n";
40
41 std::string Namespace = Target.inst_begin()->second.Namespace;
42
43 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
44 E = Target.inst_end(); I != E; ++I)
45 if (!I->second.AsmString.empty()) {
46 const std::string &AsmString = I->second.AsmString;
Chris Lattner076efa72004-08-01 07:43:02 +000047 O << " case " << Namespace << "::" << I->first << ": O ";
48
49 std::string::size_type LastEmitted = 0;
50 while (LastEmitted != AsmString.size()) {
51 std::string::size_type DollarPos = AsmString.find('$', LastEmitted);
52 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
53
54 // Emit a constant string fragment.
55 if (DollarPos != LastEmitted) {
56 // TODO: this should eventually handle escaping.
57 O << " << \"" << std::string(AsmString.begin()+LastEmitted,
58 AsmString.begin()+DollarPos) << "\"";
59 LastEmitted = DollarPos;
60 } else if (DollarPos+1 != AsmString.size() &&
61 AsmString[DollarPos+1] == '$') {
62 O << " << '$'"; // "$$" -> $
63 } else {
64 // Get the name of the variable.
65 // TODO: should eventually handle ${foo}bar as $foo
66 std::string::size_type VarEnd = DollarPos+1;
67 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
68 ++VarEnd;
69 std::string VarName(AsmString.begin()+DollarPos+1,
70 AsmString.begin()+VarEnd);
71 if (VarName.empty())
72 throw "Stray '$' in '" + I->first +
73 "' asm string, maybe you want $$?";
74 unsigned OpNo = I->second.getOperandNamed(VarName);
75
76 // If this is a two-address instruction and we are not accessing the
77 // 0th operand, remove an operand.
78 if (I->second.isTwoAddress && OpNo != 0)
79 --OpNo;
80
81 O << "; printOperand(MI->getOperand(" << OpNo << "), MVT::"
82 << getName(I->second.OperandList[OpNo].Ty) << "); O ";
83 LastEmitted = VarEnd;
84 }
85 }
86
87 O << " << '\\n'; break;\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +000088 }
89
90 O << " }\n"
91 " return true;\n"
92 "}\n";
93 EmitSourceFileTail(O);
94}