blob: 6f9440a6076f4d2721e68998a6d10b6b45d19983 [file] [log] [blame]
Chris Lattner33ccf7e2003-08-03 17:24:10 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
John Criswell01d45822003-10-20 20:20:30 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner33ccf7e2003-08-03 17:24:10 +00009//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
15#include "InstrInfoEmitter.h"
Chris Lattner7884b752003-08-07 05:39:09 +000016#include "CodeGenWrappers.h"
Chris Lattner33ccf7e2003-08-03 17:24:10 +000017#include "Record.h"
18
Brian Gaeked0fde302003-11-11 22:41:34 +000019namespace llvm {
20
Chris Lattner33ccf7e2003-08-03 17:24:10 +000021// runEnums - Print out enum values for all of the instructions.
22void InstrInfoEmitter::runEnums(std::ostream &OS) {
23 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
24
25 if (Insts.size() == 0)
26 throw std::string("No 'Instruction' subclasses defined!");
27
28 std::string Namespace = Insts[0]->getValueAsString("Namespace");
29
Chris Lattnerbc017232003-08-06 04:32:07 +000030 EmitSourceFileHeader("Target Instruction Enum Values", OS);
Chris Lattner33ccf7e2003-08-03 17:24:10 +000031
32 if (!Namespace.empty())
33 OS << "namespace " << Namespace << " {\n";
34 OS << " enum {\n";
35
Chris Lattner7884b752003-08-07 05:39:09 +000036 CodeGenTarget Target;
37
Chris Lattnera3ae6142003-08-03 21:57:51 +000038 // We must emit the PHI opcode first...
Chris Lattner7884b752003-08-07 05:39:09 +000039 Record *InstrInfo = Target.getInstructionSet();
Chris Lattner33ccf7e2003-08-03 17:24:10 +000040 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
Chris Lattner33ccf7e2003-08-03 17:24:10 +000041
Chris Lattnera3ae6142003-08-03 21:57:51 +000042 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000043
44 // Print out the rest of the instructions now...
45 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
Chris Lattnera3ae6142003-08-03 21:57:51 +000046 if (Insts[i] != PHI)
47 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000048
49 OS << " };\n";
50 if (!Namespace.empty())
51 OS << "}\n";
Brian Gaeked0fde302003-11-11 22:41:34 +000052 EmitSourceFileTail(OS);
Chris Lattner33ccf7e2003-08-03 17:24:10 +000053}
Chris Lattnera3ae6142003-08-03 21:57:51 +000054
Chris Lattnerbc017232003-08-06 04:32:07 +000055void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
56 std::ostream &OS) const {
Chris Lattnera3ae6142003-08-03 21:57:51 +000057 OS << "static const unsigned " << Name << "[] = { ";
58 for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
59 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
60 OS << getQualifiedName(DI->getDef()) << ", ";
61 else
62 throw "Illegal value in '" + Name + "' list!";
63 OS << "0 };\n";
64}
65
66
67// run - Emit the main instruction description records for the target...
68void InstrInfoEmitter::run(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000069 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner7884b752003-08-07 05:39:09 +000070 CodeGenTarget Target;
71 const std::string &TargetName = Target.getName();
72 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +000073 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
74
75 std::vector<Record*> Instructions =
76 Records.getAllDerivedDefinitions("Instruction");
77
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000078 // Emit empty implicit uses and defs lists
79 OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
80 << "static const unsigned EmptyImpDefs[] = { 0 };\n";
81
Chris Lattnera3ae6142003-08-03 21:57:51 +000082 // Emit all of the instruction's implicit uses and defs...
83 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
84 Record *Inst = Instructions[i];
85 ListInit *LI = Inst->getValueAsListInit("Uses");
86 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
87 LI = Inst->getValueAsListInit("Defs");
88 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
89 }
90
91 OS << "\nstatic const TargetInstrDescriptor " << TargetName
92 << "Insts[] = {\n";
93 emitRecord(PHI, 0, InstrInfo, OS);
94
95 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
96 if (Instructions[i] != PHI)
97 emitRecord(Instructions[i], i+1, InstrInfo, OS);
98 OS << "};\n";
Brian Gaeked0fde302003-11-11 22:41:34 +000099 EmitSourceFileTail(OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000100}
101
102void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
103 std::ostream &OS) {
104 OS << " { \"" << R->getValueAsString("Name")
105 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
106
107 // Emit all of the target indepedent flags...
108 if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG";
109 if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG";
Chris Lattner58505992004-07-31 02:07:26 +0000110 if (R->getValueAsBit("isBarrier")) OS << "|M_BARRIER_FLAG";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000111 if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG";
112 if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
113 if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
114 OS << ", 0";
115
116 // Emit all of the target-specific flags...
117 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
118 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
119 if (LI->getSize() != Shift->getSize())
120 throw "Lengths of " + InstrInfo->getName() +
121 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
122
123 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
124 emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
125 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
126
127 OS << ", ";
128
129 // Emit the implicit uses and defs lists...
130 LI = R->getValueAsListInit("Uses");
131 if (!LI->getSize())
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000132 OS << "EmptyImpUses, ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000133 else
134 OS << R->getName() << "ImpUses, ";
135
136 LI = R->getValueAsListInit("Defs");
137 if (!LI->getSize())
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000138 OS << "EmptyImpDefs ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000139 else
140 OS << R->getName() << "ImpDefs ";
141
142 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
143}
144
145void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
146 IntInit *ShiftInt, std::ostream &OS) {
147 if (Val == 0 || ShiftInt == 0)
148 throw std::string("Illegal value or shift amount in TargetInfo*!");
149 RecordVal *RV = R->getValue(Val->getValue());
150 int Shift = ShiftInt->getValue();
151
152 if (RV == 0 || RV->getValue() == 0)
153 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
154
155 Init *Value = RV->getValue();
156 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
157 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
158 return;
159 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
160 // Convert the Bits to an integer to print...
161 Init *I = BI->convertInitializerTo(new IntRecTy());
162 if (I)
163 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
164 if (II->getValue())
165 OS << "|(" << II->getValue() << "<<" << Shift << ")";
166 return;
167 }
168
169 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
170 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
171 return;
172 }
173
174 std::cerr << "Unhandled initializer: " << *Val << "\n";
175 throw "In record '" + R->getName() + "' for TSFlag emission.";
176}
Brian Gaeked0fde302003-11-11 22:41:34 +0000177
178} // End llvm namespace