blob: 667a4df8b88a2f7f106a749cfc9a32fa82481c7a [file] [log] [blame]
Chris Lattner33ccf7e2003-08-03 17:24:10 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
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 Lattner803a5f62004-08-01 04:04:35 +000016#include "CodeGenTarget.h"
Chris Lattner33ccf7e2003-08-03 17:24:10 +000017#include "Record.h"
Chris Lattner2082ebe2004-08-01 03:55:39 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattner33ccf7e2003-08-03 17:24:10 +000020// runEnums - Print out enum values for all of the instructions.
21void InstrInfoEmitter::runEnums(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000022 EmitSourceFileHeader("Target Instruction Enum Values", OS);
Chris Lattner2c384132004-08-17 03:08:28 +000023 OS << "namespace llvm {\n\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000024
Chris Lattner7884b752003-08-07 05:39:09 +000025 CodeGenTarget Target;
26
Chris Lattnera3ae6142003-08-03 21:57:51 +000027 // We must emit the PHI opcode first...
Chris Lattner7884b752003-08-07 05:39:09 +000028 Record *InstrInfo = Target.getInstructionSet();
Chris Lattner33ccf7e2003-08-03 17:24:10 +000029
Chris Lattnerec352402004-08-01 05:04:00 +000030 std::string Namespace = Target.inst_begin()->second.Namespace;
31
32 if (!Namespace.empty())
33 OS << "namespace " << Namespace << " {\n";
34 OS << " enum {\n";
35
Chris Lattnerd6488672005-01-22 18:58:51 +000036 std::vector<const CodeGenInstruction*> NumberedInstructions;
37 Target.getInstructionsByEnumValue(NumberedInstructions);
38
39 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
40 OS << " " << NumberedInstructions[i]->TheDef->getName()
41 << ", \t// " << i << "\n";
42 }
Chris Lattner33ccf7e2003-08-03 17:24:10 +000043 OS << " };\n";
44 if (!Namespace.empty())
45 OS << "}\n";
Chris Lattner2c384132004-08-17 03:08:28 +000046 OS << "} // End llvm namespace \n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000047}
Chris Lattnera3ae6142003-08-03 21:57:51 +000048
Chris Lattnera3ac88d2005-08-18 21:36:47 +000049static std::vector<Record*> GetDefList(ListInit *LI, const std::string &Name) {
50 std::vector<Record*> Result;
51 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
52 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
53 Result.push_back(DI->getDef());
Chris Lattnera3ae6142003-08-03 21:57:51 +000054 else
55 throw "Illegal value in '" + Name + "' list!";
Chris Lattnera3ac88d2005-08-18 21:36:47 +000056 return Result;
57}
58
59void InstrInfoEmitter::printDefList(const std::vector<Record*> &Uses,
60 unsigned Num, std::ostream &OS) const {
61 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
62 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
63 OS << getQualifiedName(Uses[i]) << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +000064 OS << "0 };\n";
65}
66
67
68// run - Emit the main instruction description records for the target...
69void InstrInfoEmitter::run(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000070 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner2c384132004-08-17 03:08:28 +000071 OS << "namespace llvm {\n\n";
72
Chris Lattner7884b752003-08-07 05:39:09 +000073 CodeGenTarget Target;
74 const std::string &TargetName = Target.getName();
75 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +000076 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
77
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000078 // Emit empty implicit uses and defs lists
Chris Lattnera3ac88d2005-08-18 21:36:47 +000079 OS << "static const unsigned EmptyImpList[] = { 0 };\n";
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000080
Chris Lattnera3ac88d2005-08-18 21:36:47 +000081 // Keep track of all of the def lists we have emitted already.
82 std::map<std::vector<Record*>, unsigned> EmittedLists;
83 std::map<ListInit*, unsigned> ListNumbers;
84 unsigned ListNumber = 0;
85
86 // Emit all of the instruction's implicit uses and defs.
Chris Lattnerec352402004-08-01 05:04:00 +000087 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
88 E = Target.inst_end(); II != E; ++II) {
89 Record *Inst = II->second.TheDef;
Chris Lattnera3ae6142003-08-03 21:57:51 +000090 ListInit *LI = Inst->getValueAsListInit("Uses");
Chris Lattnera3ac88d2005-08-18 21:36:47 +000091 if (LI->getSize()) {
92 std::vector<Record*> Uses = GetDefList(LI, Inst->getName());
93 unsigned &IL = EmittedLists[Uses];
94 if (!IL) printDefList(Uses, IL = ++ListNumber, OS);
95 ListNumbers[LI] = IL;
96 }
Chris Lattnera3ae6142003-08-03 21:57:51 +000097 LI = Inst->getValueAsListInit("Defs");
Chris Lattnera3ac88d2005-08-18 21:36:47 +000098 if (LI->getSize()) {
99 std::vector<Record*> Uses = GetDefList(LI, Inst->getName());
100 unsigned &IL = EmittedLists[Uses];
101 if (!IL) printDefList(Uses, IL = ++ListNumber, OS);
102 ListNumbers[LI] = IL;
103 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000104 }
105
Chris Lattner0e384b62005-08-19 16:57:28 +0000106 // Emit all of the operand info records.
107 OS << "\n";
108 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
109 E = Target.inst_end(); II != E; ++II) {
110 const CodeGenInstruction &Inst = II->second;
111 if (!Inst.hasVariableNumberOfOperands) {
112 OS << "static const TargetOperandInfo " << Inst.TheDef->getName()
113 << "_Operands[] = {";
114 // FIXME: Emit operand info.
115 OS << "};\n";
116 }
117 }
118
119 // Emit all of the TargetInstrDescriptor records.
120 //
Chris Lattnera3ae6142003-08-03 21:57:51 +0000121 OS << "\nstatic const TargetInstrDescriptor " << TargetName
122 << "Insts[] = {\n";
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000123 emitRecord(Target.getPHIInstruction(), 0, InstrInfo, ListNumbers, OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000124
Chris Lattnerec352402004-08-01 05:04:00 +0000125 unsigned i = 0;
126 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
127 E = Target.inst_end(); II != E; ++II)
128 if (II->second.TheDef != PHI)
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000129 emitRecord(II->second, ++i, InstrInfo, ListNumbers, OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000130 OS << "};\n";
Chris Lattner2c384132004-08-17 03:08:28 +0000131 OS << "} // End llvm namespace \n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000132}
133
Chris Lattnerec352402004-08-01 05:04:00 +0000134void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000135 Record *InstrInfo,
136 std::map<ListInit*, unsigned> &ListNumbers,
137 std::ostream &OS) {
Chris Lattnerd98958f2005-08-19 00:59:49 +0000138 int NumOperands;
139 if (Inst.hasVariableNumberOfOperands)
140 NumOperands = -1;
141 else if (!Inst.OperandList.empty())
142 // Each logical operand can be multiple MI operands.
143 NumOperands = Inst.OperandList.back().MIOperandNo +
144 Inst.OperandList.back().MINumOperands;
145 else
146 NumOperands = 0;
147
Chris Lattner2d12b2c2004-08-01 08:38:17 +0000148 OS << " { \"";
149 if (Inst.Name.empty())
150 OS << Inst.TheDef->getName();
151 else
152 OS << Inst.Name;
Chris Lattnerd98958f2005-08-19 00:59:49 +0000153 OS << "\",\t" << NumOperands << ", -1, 0, false, 0, 0, 0, 0";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000154
155 // Emit all of the target indepedent flags...
Chris Lattnerec352402004-08-01 05:04:00 +0000156 if (Inst.isReturn) OS << "|M_RET_FLAG";
157 if (Inst.isBranch) OS << "|M_BRANCH_FLAG";
158 if (Inst.isBarrier) OS << "|M_BARRIER_FLAG";
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000159 if (Inst.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000160 if (Inst.isCall) OS << "|M_CALL_FLAG";
Nate Begemancdd66b52004-09-28 21:01:45 +0000161 if (Inst.isLoad) OS << "|M_LOAD_FLAG";
162 if (Inst.isStore) OS << "|M_STORE_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000163 if (Inst.isTwoAddress) OS << "|M_2_ADDR_FLAG";
Chris Lattneraad75aa2005-01-02 02:29:04 +0000164 if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR";
165 if (Inst.isCommutable) OS << "|M_COMMUTABLE";
Chris Lattnerec352402004-08-01 05:04:00 +0000166 if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000167 OS << ", 0";
168
169 // Emit all of the target-specific flags...
170 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
171 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
172 if (LI->getSize() != Shift->getSize())
173 throw "Lengths of " + InstrInfo->getName() +
174 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
175
176 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Chris Lattnerec352402004-08-01 05:04:00 +0000177 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
Chris Lattnera3ae6142003-08-03 21:57:51 +0000178 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
179
180 OS << ", ";
181
182 // Emit the implicit uses and defs lists...
Chris Lattnerec352402004-08-01 05:04:00 +0000183 LI = Inst.TheDef->getValueAsListInit("Uses");
Chris Lattnera3ae6142003-08-03 21:57:51 +0000184 if (!LI->getSize())
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000185 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000186 else
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000187 OS << "ImplicitList" << ListNumbers[LI] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000188
Chris Lattnerec352402004-08-01 05:04:00 +0000189 LI = Inst.TheDef->getValueAsListInit("Defs");
Chris Lattnera3ae6142003-08-03 21:57:51 +0000190 if (!LI->getSize())
Chris Lattner0e384b62005-08-19 16:57:28 +0000191 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000192 else
Chris Lattner0e384b62005-08-19 16:57:28 +0000193 OS << "ImplicitList" << ListNumbers[LI] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000194
Chris Lattner0e384b62005-08-19 16:57:28 +0000195 // Emit the operand info.
196 if (NumOperands == -1)
197 OS << "0 ";
198 else
199 OS << Inst.TheDef->getName() << "_Operands ";
200
Chris Lattnerec352402004-08-01 05:04:00 +0000201 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000202}
203
204void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
205 IntInit *ShiftInt, std::ostream &OS) {
206 if (Val == 0 || ShiftInt == 0)
207 throw std::string("Illegal value or shift amount in TargetInfo*!");
208 RecordVal *RV = R->getValue(Val->getValue());
209 int Shift = ShiftInt->getValue();
210
211 if (RV == 0 || RV->getValue() == 0)
212 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
213
214 Init *Value = RV->getValue();
215 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
216 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
217 return;
218 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
219 // Convert the Bits to an integer to print...
220 Init *I = BI->convertInitializerTo(new IntRecTy());
221 if (I)
222 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
223 if (II->getValue())
224 OS << "|(" << II->getValue() << "<<" << Shift << ")";
225 return;
226 }
227
228 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
229 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
230 return;
231 }
232
233 std::cerr << "Unhandled initializer: " << *Val << "\n";
234 throw "In record '" + R->getName() + "' for TSFlag emission.";
235}
Brian Gaeked0fde302003-11-11 22:41:34 +0000236