blob: 535299c32de0b4626c0472a800bf4fa5dda3a315 [file] [log] [blame]
Owen Andersond8c87882011-02-18 21:51:29 +00001//===------------ FixedLenDecoderEmitter.h - Decoder Generator --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// It contains the tablegen backend that emits the decoder functions for
11// targets with fixed length instruction set.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef FixedLenDECODEREMITTER_H
16#define FixedLenDECODEREMITTER_H
17
18#include "CodeGenTarget.h"
19#include "TableGenBackend.h"
20
21#include "llvm/Support/DataTypes.h"
22
23namespace llvm {
24
Owen Andersond1e38df2011-07-28 21:54:31 +000025struct EncodingField {
26 unsigned Base, Width, Offset;
27 EncodingField(unsigned B, unsigned W, unsigned O)
28 : Base(B), Width(W), Offset(O) { }
29};
30
Owen Andersond8c87882011-02-18 21:51:29 +000031struct OperandInfo {
Owen Andersond1e38df2011-07-28 21:54:31 +000032 std::vector<EncodingField> Fields;
Owen Andersond8c87882011-02-18 21:51:29 +000033 std::string Decoder;
34
Owen Andersond1e38df2011-07-28 21:54:31 +000035 OperandInfo(std::string D)
36 : Decoder(D) { }
37
38 void addField(unsigned Base, unsigned Width, unsigned Offset) {
39 Fields.push_back(EncodingField(Base, Width, Offset));
40 }
41
42 unsigned numFields() { return Fields.size(); }
43
44 typedef std::vector<EncodingField>::iterator iterator;
45
46 iterator begin() { return Fields.begin(); }
47 iterator end() { return Fields.end(); }
Owen Andersond8c87882011-02-18 21:51:29 +000048};
49
50class FixedLenDecoderEmitter : public TableGenBackend {
51public:
Owen Anderson83e3f672011-08-17 17:44:15 +000052 FixedLenDecoderEmitter(RecordKeeper &R,
53 std::string GPrefix = "if (",
54 std::string GPostfix = " == MCDisassembler::Fail) return MCDisassembler::Fail;",
55 std::string ROK = "MCDisassembler::Success",
56 std::string RFail = "MCDisassembler::Fail",
57 std::string L = "") :
Owen Andersond8c87882011-02-18 21:51:29 +000058 Records(R), Target(R),
Owen Anderson83e3f672011-08-17 17:44:15 +000059 NumberedInstructions(Target.getInstructionsByEnumValue()),
60 GuardPrefix(GPrefix), GuardPostfix(GPostfix),
61 ReturnOK(ROK), ReturnFail(RFail), Locals(L) {}
Owen Andersond8c87882011-02-18 21:51:29 +000062
63 // run - Output the code emitter
64 void run(raw_ostream &o);
65
66private:
67 RecordKeeper &Records;
68 CodeGenTarget Target;
69 std::vector<const CodeGenInstruction*> NumberedInstructions;
70 std::vector<unsigned> Opcodes;
71 std::map<unsigned, std::vector<OperandInfo> > Operands;
Owen Anderson83e3f672011-08-17 17:44:15 +000072public:
73 std::string GuardPrefix, GuardPostfix;
74 std::string ReturnOK, ReturnFail;
75 std::string Locals;
Owen Andersond8c87882011-02-18 21:51:29 +000076};
77
78} // end llvm namespace
79
80#endif