aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--utils/TableGen/AsmWriterEmitter.cpp234
1 files changed, 213 insertions, 21 deletions
diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
index f34feef0c2..069578056c 100644
--- a/utils/TableGen/AsmWriterEmitter.cpp
+++ b/utils/TableGen/AsmWriterEmitter.cpp
@@ -19,6 +19,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
+#include <sstream>
#include <iostream>
using namespace llvm;
@@ -32,7 +33,11 @@ static bool isIdentChar(char C) {
// This should be an anon namespace, this works around a GCC warning.
namespace llvm {
struct AsmWriterOperand {
- enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
+ enum OpType {
+ isLiteralTextOperand,
+ isMachineInstrOperand,
+ isLiteralStatementOperand
+ } OperandType;
/// Str - For isLiteralTextOperand, this IS the literal text. For
/// isMachineInstrOperand, this is the PrinterMethodName for the operand.
@@ -47,14 +52,16 @@ namespace llvm {
std::string MiModifier;
// To make VS STL happy
- AsmWriterOperand():OperandType(isLiteralTextOperand) {}
+ AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
- explicit AsmWriterOperand(const std::string &LitStr)
- : OperandType(isLiteralTextOperand), Str(LitStr) {}
+ AsmWriterOperand(const std::string &LitStr,
+ OpType op = isLiteralTextOperand)
+ : OperandType(op), Str(LitStr) {}
AsmWriterOperand(const std::string &Printer, unsigned OpNo,
- const std::string &Modifier)
- : OperandType(isMachineInstrOperand), Str(Printer), MIOpNo(OpNo),
+ const std::string &Modifier,
+ OpType op = isMachineInstrOperand)
+ : OperandType(op), Str(Printer), MIOpNo(OpNo),
MiModifier(Modifier) {}
bool operator!=(const AsmWriterOperand &Other) const {
@@ -78,6 +85,29 @@ namespace llvm {
std::vector<AsmWriterOperand> Operands;
const CodeGenInstruction *CGI;
+ /// MAX_GROUP_NESTING_LEVEL - The maximum number of group nesting
+ /// levels we ever expect to see in an asm operand.
+ static const int MAX_GROUP_NESTING_LEVEL = 10;
+
+ /// GroupLevel - The level of nesting of the current operand
+ /// group, such as [reg + (reg + offset)]. -1 means we are not in
+ /// a group.
+ int GroupLevel;
+
+ /// GroupDelim - Remember the delimeter for a group operand.
+ char GroupDelim[MAX_GROUP_NESTING_LEVEL];
+
+ /// ReadingWhitespace - Tell whether we just read some whitespace.
+ bool ReadingWhitespace;
+
+ /// InGroup - Determine whether we are in the middle of an
+ /// operand group.
+ bool InGroup() const { return GroupLevel != -1; }
+
+ /// InWhitespace - Determine whether we are in the middle of
+ /// emitting whitespace.
+ bool InWhitespace() const { return ReadingWhitespace; }
+
AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
/// MatchesAllButOneOp - If this instruction is exactly identical to the
@@ -103,6 +133,18 @@ std::string AsmWriterOperand::getCode() const {
if (OperandType == isLiteralTextOperand)
return "O << \"" + Str + "\"; ";
+ if (OperandType == isLiteralStatementOperand) {
+ return Str;
+ }
+
+ if (OperandType == isLiteralStatementOperand) {
+ return Str;
+ }
+
+ if (OperandType == isLiteralStatementOperand) {
+ return Str;
+ }
+
std::string Result = Str + "(MI";
if (MIOpNo != ~0U)
Result += ", " + utostr(MIOpNo);
@@ -115,7 +157,8 @@ std::string AsmWriterOperand::getCode() const {
/// ParseAsmString - Parse the specified Instruction's AsmString into this
/// AsmWriterInst.
///
-AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
+AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant)
+ : GroupLevel(-1), ReadingWhitespace(false) {
this->CGI = &CGI;
unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
@@ -130,15 +173,92 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
if (DollarPos == std::string::npos) DollarPos = AsmString.size();
// Emit a constant string fragment.
+
+ // TODO: Recognize an operand separator to determine when to pad
+ // to the next operator.
if (DollarPos != LastEmitted) {
if (CurVariant == Variant || CurVariant == ~0U) {
for (; LastEmitted != DollarPos; ++LastEmitted)
switch (AsmString[LastEmitted]) {
- case '\n': AddLiteralString("\\n"); break;
- case '\t': AddLiteralString("\\t"); break;
- case '"': AddLiteralString("\\\""); break;
- case '\\': AddLiteralString("\\\\"); break;
+ case '\n':
+ assert(!InGroup() && "Missing matching group delimeter");
+ ReadingWhitespace = false;
+ AddLiteralString("\\n");
+ break;
+ case '\t':
+ if (!InGroup()) {
+ ReadingWhitespace = true;
+ }
+ AddLiteralString("\\t");
+ break;
+ case '"':
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+ AddLiteralString("\\\"");
+ break;
+ case '\\':
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+ AddLiteralString("\\\\");
+ break;
+
+ case '(': // Fallthrough
+ case '[':
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+
+ ++GroupLevel;
+ assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
+ && "Exceeded maximum operand group nesting level");
+ GroupDelim[GroupLevel] = AsmString[LastEmitted];
+ AddLiteralString(std::string(1, AsmString[LastEmitted]));
+ break;
+
+ case ')': // Fallthrough
+ case ']':
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+
+ if (AsmString[LastEmitted] == ')')
+ assert(GroupDelim[GroupLevel] == '(' && "Mismatched delimeters");
+ else
+ assert(GroupDelim[GroupLevel] == '[' && "Mismatched delimeters");
+
+ --GroupLevel;
+ assert(GroupLevel > -2 && "Too many end delimeters!");
+ AddLiteralString(std::string(1, AsmString[LastEmitted]));
+ break;
+
default:
+ if (AsmString[LastEmitted] != ' ' &&
+ AsmString[LastEmitted] != '\t') {
+ if (!InGroup() && InWhitespace())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+ }
+ else
+ if (!InGroup())
+ ReadingWhitespace = true;
+
AddLiteralString(std::string(1, AsmString[LastEmitted]));
break;
}
@@ -149,11 +269,33 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
if (DollarPos+1 != AsmString.size() &&
(CurVariant == Variant || CurVariant == ~0U)) {
if (AsmString[DollarPos+1] == 'n') {
+ assert(!InGroup() && "Missing matching group delimeter");
+ ReadingWhitespace = false;
AddLiteralString("\\n");
} else if (AsmString[DollarPos+1] == 't') {
+ if (!InGroup()) {
+ ReadingWhitespace = true;
+ }
AddLiteralString("\\t");
} else if (std::string("${|}\\").find(AsmString[DollarPos+1])
!= std::string::npos) {
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+
+ if (AsmString[DollarPos+1] == '{') {
+ ++GroupLevel;
+ assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
+ && "Exceeded maximum operand group nesting level");
+ GroupDelim[GroupLevel] = AsmString[DollarPos+1];
+ } else if (AsmString[DollarPos+1] == '}') {
+ assert(GroupDelim[GroupLevel] == '{' && "Mismatched delimeters");
+ --GroupLevel;
+ assert(GroupLevel > -2 && "Too many end delimeters!");
+ }
AddLiteralString(std::string(1, AsmString[DollarPos+1]));
} else {
throw "Non-supported escaped character found in instruction '" +
@@ -182,13 +324,27 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
CurVariant = ~0U;
} else if (DollarPos+1 != AsmString.size() &&
AsmString[DollarPos+1] == '$') {
- if (CurVariant == Variant || CurVariant == ~0U)
+ if (CurVariant == Variant || CurVariant == ~0U) {
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
AddLiteralString("$"); // "$$" -> $
+ }
LastEmitted = DollarPos+2;
} else {
+ if (InWhitespace() && !InGroup())
+ Operands.push_back(
+ AsmWriterOperand(
+ "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
+ AsmWriterOperand::isLiteralStatementOperand));
+ ReadingWhitespace = false;
+
// Get the name of the variable.
std::string::size_type VarEnd = DollarPos+1;
-
+
// handle ${foo}bar as $foo by detecting whether the character following
// the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
// so the variable name does not contain the leading curly brace.
@@ -260,6 +416,9 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
}
}
+ Operands.push_back(
+ AsmWriterOperand("EmitComments(*MI);\n",
+ AsmWriterOperand::isLiteralStatementOperand));
AddLiteralString("\\n");
}
@@ -357,7 +516,6 @@ static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
}
O << "\n";
}
-
O << " break;\n";
}
@@ -385,8 +543,9 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Command = " " + Inst->Operands[0].getCode() + "\n";
// If this is the last operand, emit a return.
- if (Inst->Operands.size() == 1)
+ if (Inst->Operands.size() == 1) {
Command += " return true;\n";
+ }
// Check to see if we already have 'Command' in UniqueOperandCommands.
// If not, add it.
@@ -431,7 +590,10 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
// Otherwise, scan to see if all of the other instructions in this command
// set share the operand.
bool AllSame = true;
-
+ // Keep track of the maximum, number of operands or any
+ // instruction we see in the group.
+ size_t MaxSize = FirstInst->Operands.size();
+
for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
NIT != InstIdxs.end();
NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
@@ -439,6 +601,11 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
// matches, we're ok, otherwise bail out.
const AsmWriterInst *OtherInst =
getAsmWriterInstByID(NIT-InstIdxs.begin());
+
+ if (OtherInst &&
+ OtherInst->Operands.size() > FirstInst->Operands.size())
+ MaxSize = std::max(MaxSize, OtherInst->Operands.size());
+
if (!OtherInst || OtherInst->Operands.size() == Op ||
OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
AllSame = false;
@@ -452,8 +619,12 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
// If this is the last operand, emit a return after the code.
- if (FirstInst->Operands.size() == Op+1)
+ if (FirstInst->Operands.size() == Op+1 &&
+ // Don't early-out too soon. Other instructions in this
+ // group may have more operands.
+ FirstInst->Operands.size() == MaxSize) {
Command += " return true;\n";
+ }
UniqueOperandCommands[CommandIdx] += Command;
InstOpsUsed[CommandIdx]++;
@@ -567,7 +738,7 @@ void AsmWriterEmitter::run(raw_ostream &O) {
UniqueOperandCommands.push_back(" return true;\n");
isFirst = false;
}
-
+
std::vector<unsigned> InstIdxs;
std::vector<unsigned> NumInstOpsHandled;
FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
@@ -675,8 +846,28 @@ void AsmWriterEmitter::run(raw_ostream &O) {
O << " // Emit the opcode for the instruction.\n"
<< " unsigned Bits = OpInfo[MI->getOpcode()];\n"
- << " if (Bits == 0) return false;\n"
- << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n\n";
+ << " if (Bits == 0) return false;\n\n";
+
+ O << " unsigned OperandColumn = 1;\n\n"
+ << " if (TAI->getOperandColumn(1) > 0) {\n"
+ << " // Don't emit trailing whitespace, let the column padding do it. This\n"
+ << " // guarantees that a stray long opcode + tab won't upset the alignment.\n"
+ << " unsigned OpLength = std::strlen(AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "));\n"
+ << " if (OpLength > 0 &&\n"
+ << " ((AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == ' ' ||\n"
+ << " (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == '\\t')) {\n"
+ << " do {\n"
+ << " --OpLength;\n"
+ << " } while ((AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == ' ' ||\n"
+ << " (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == '\\t');\n"
+ << " for (unsigned Idx = 0; Idx < OpLength; ++Idx)\n"
+ << " O << (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[Idx];\n"
+ << " O.PadToColumn(TAI->getOperandColumn(OperandColumn++), 1);\n"
+ << " }\n"
+ << " } else {\n"
+ << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n"
+ << " }\n\n";
+
// Output the table driven operand information.
BitsLeft = 32-AsmStrBits;
@@ -741,6 +932,7 @@ void AsmWriterEmitter::run(raw_ostream &O) {
O << " }\n";
O << " return true;\n";
}
-
+
+ O << " return true;\n";
O << "}\n";
}