blob: 64debe27f80420611160632877879065bd775f70 [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
34 Record *AsmWriter = Target.getAsmWriter();
35
36 std::string AsmWriterClassName =
37 AsmWriter->getValueAsString("AsmWriterClassName");
38
Chris Lattner2e1f51b2004-08-01 05:59:33 +000039 O <<
40 "/// printInstruction - This method is automatically generated by tablegen\n"
41 "/// from the instruction set description. This method returns true if the\n"
42 "/// machine instruction was sufficiently described to print it, otherwise\n"
43 "/// it returns false.\n"
Chris Lattner175580c2004-08-14 22:50:53 +000044 "bool " << Target.getName() << AsmWriterClassName
45 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +000046 O << " switch (MI->getOpcode()) {\n"
47 " default: return false;\n";
48
49 std::string Namespace = Target.inst_begin()->second.Namespace;
50
51 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()) {
59 std::string::size_type DollarPos = AsmString.find('$', LastEmitted);
60 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;
68 } else if (DollarPos+1 != AsmString.size() &&
69 AsmString[DollarPos+1] == '$') {
70 O << " << '$'"; // "$$" -> $
71 } else {
72 // Get the name of the variable.
73 // TODO: should eventually handle ${foo}bar as $foo
74 std::string::size_type VarEnd = DollarPos+1;
75 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
76 ++VarEnd;
77 std::string VarName(AsmString.begin()+DollarPos+1,
78 AsmString.begin()+VarEnd);
79 if (VarName.empty())
80 throw "Stray '$' in '" + I->first +
81 "' asm string, maybe you want $$?";
82 unsigned OpNo = I->second.getOperandNamed(VarName);
83
84 // If this is a two-address instruction and we are not accessing the
85 // 0th operand, remove an operand.
Chris Lattner9302ba42004-08-11 04:08:36 +000086 unsigned MIOp = I->second.OperandList[OpNo].MIOperandNo;
87 if (I->second.isTwoAddress && MIOp != 0) {
88 if (MIOp == 1)
Chris Lattnerc3d5f3e2004-08-01 08:55:34 +000089 throw "Should refer to operand #0 instead of #1 for two-address"
90 " instruction '" + I->first + "'!";
Chris Lattner9302ba42004-08-11 04:08:36 +000091 --MIOp;
Chris Lattnerc3d5f3e2004-08-01 08:55:34 +000092 }
Chris Lattner076efa72004-08-01 07:43:02 +000093
Chris Lattner1caef2c2004-08-11 02:23:23 +000094 O << "; " << I->second.OperandList[OpNo].PrinterMethodName
Chris Lattner9302ba42004-08-11 04:08:36 +000095 << "(MI, " << MIOp << ", MVT::"
Chris Lattner076efa72004-08-01 07:43:02 +000096 << getName(I->second.OperandList[OpNo].Ty) << "); O ";
97 LastEmitted = VarEnd;
98 }
99 }
100
101 O << " << '\\n'; break;\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000102 }
103
104 O << " }\n"
105 " return true;\n"
106 "}\n";
Chris Lattner2c384132004-08-17 03:08:28 +0000107 O << "} // End llvm namespace \n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000108}