+// If the VarBitInit at position 'bit' matches the specified variable then
+// return the variable bit position. Otherwise return -1.
+int CodeEmitterGen::getVariableBit(const std::string &VarName,
+ BitsInit *BI, int bit) {
+ if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
+ TypedInit *TI = VBI->getVariable();
+
+ if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
+ if (VI->getName() == VarName) return VBI->getBitNum();
+ }
+ }
+
+ return -1;
+}
+
+
+void CodeEmitterGen::run(std::ostream &o) {
+ CodeGenTarget Target;
+ std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
+
+ // For little-endian instruction bit encodings, reverse the bit order
+ if (Target.isLittleEndianEncoding()) reverseBits(Insts);
+
+ EmitSourceFileHeader("Machine Code Emitter", o);
+ std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
+
+ std::vector<const CodeGenInstruction*> NumberedInstructions;
+ Target.getInstructionsByEnumValue(NumberedInstructions);
+
+ // Emit function declaration
+ o << "unsigned " << Target.getName() << "CodeEmitter::"
+ << "getBinaryCodeForInstr(const MachineInstr &MI) {\n";
+
+ // Emit instruction base values
+ o << " static const unsigned InstBits[] = {\n";
+ for (std::vector<const CodeGenInstruction*>::iterator
+ IN = NumberedInstructions.begin(),
+ EN = NumberedInstructions.end();
+ IN != EN; ++IN) {
+ const CodeGenInstruction *CGI = *IN;
+ Record *R = CGI->TheDef;
+
+ if (R->getName() == "PHI" ||
+ R->getName() == "INLINEASM" ||
+ R->getName() == "DBG_LABEL" ||
+ R->getName() == "EH_LABEL" ||
+ R->getName() == "GC_LABEL" ||
+ R->getName() == "DECLARE" ||
+ R->getName() == "EXTRACT_SUBREG" ||
+ R->getName() == "INSERT_SUBREG" ||
+ R->getName() == "IMPLICIT_DEF" ||
+ R->getName() == "SUBREG_TO_REG") {
+ o << " 0U,\n";
+ continue;
+ }
+
+ BitsInit *BI = R->getValueAsBitsInit("Inst");