summaryrefslogtreecommitdiffstats
path: root/mlir/lib/Parser
diff options
context:
space:
mode:
authorChris Lattner <clattner@google.com>2018-06-28 20:45:33 -0700
committerjpienaar <jpienaar@google.com>2019-03-29 12:26:53 -0700
commit1734d78f8802fca30da5ed20780ae591b3a2b4e0 (patch)
tree9b81a515722d1139903935c8eb0edf0871637f68 /mlir/lib/Parser
parent3609599af69c9c091b75d0caefadfb5a0479c913 (diff)
downloadbcm5719-llvm-1734d78f8802fca30da5ed20780ae591b3a2b4e0.tar.gz
bcm5719-llvm-1734d78f8802fca30da5ed20780ae591b3a2b4e0.zip
Sketch out parser/IR support for OperationInst, and a new Instruction base
class. Introduce an Identifier class to MLIRContext to represent uniqued identifiers, introduce string literal support to the lexer, introducing parser and printer support etc. PiperOrigin-RevId: 202592007
Diffstat (limited to 'mlir/lib/Parser')
-rw-r--r--mlir/lib/Parser/Lexer.cpp30
-rw-r--r--mlir/lib/Parser/Lexer.h1
-rw-r--r--mlir/lib/Parser/Parser.cpp77
-rw-r--r--mlir/lib/Parser/Token.cpp11
-rw-r--r--mlir/lib/Parser/Token.h7
5 files changed, 104 insertions, 22 deletions
diff --git a/mlir/lib/Parser/Lexer.cpp b/mlir/lib/Parser/Lexer.cpp
index 209f9881468..b6473f523eb 100644
--- a/mlir/lib/Parser/Lexer.cpp
+++ b/mlir/lib/Parser/Lexer.cpp
@@ -99,6 +99,7 @@ Token Lexer::lexToken() {
case ';': return lexComment();
case '@': return lexAtIdentifier(tokStart);
case '#': return lexAffineMapId(tokStart);
+ case '"': return lexString(tokStart);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
@@ -231,3 +232,32 @@ Token Lexer::lexNumber(const char *tokStart) {
return formToken(Token::integer, tokStart);
}
+
+/// Lex a string literal.
+///
+/// string-literal ::= '"' [^"\n\f\v\r]* '"'
+///
+/// TODO: define escaping rules.
+Token Lexer::lexString(const char *tokStart) {
+ assert(curPtr[-1] == '"');
+
+ while (1) {
+ switch (*curPtr++) {
+ case '"':
+ return formToken(Token::string, tokStart);
+ case '0':
+ // If this is a random nul character in the middle of a string, just
+ // include it. If it is the end of file, then it is an error.
+ if (curPtr-1 != curBuffer.end())
+ continue;
+ LLVM_FALLTHROUGH;
+ case '\n':
+ case '\v':
+ case '\f':
+ return emitError(curPtr-1, "expected '\"' in string literal");
+
+ default:
+ continue;
+ }
+ }
+}
diff --git a/mlir/lib/Parser/Lexer.h b/mlir/lib/Parser/Lexer.h
index 0301a35bbda..f0274fe128f 100644
--- a/mlir/lib/Parser/Lexer.h
+++ b/mlir/lib/Parser/Lexer.h
@@ -62,6 +62,7 @@ private:
Token lexAtIdentifier(const char *tokStart);
Token lexAffineMapId(const char *tokStart);
Token lexNumber(const char *tokStart);
+ Token lexString(const char *tokStart);
};
} // end namespace mlir
diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp
index df952f95ea9..c36d3b9cfcc 100644
--- a/mlir/lib/Parser/Parser.cpp
+++ b/mlir/lib/Parser/Parser.cpp
@@ -43,7 +43,7 @@ enum ParseResult {
/// Main parser implementation.
class Parser {
- public:
+public:
Parser(llvm::SourceMgr &sourceMgr, MLIRContext *context,
const SMDiagnosticHandlerTy &errorReporter)
: context(context),
@@ -137,10 +137,13 @@ private:
ParseResult parseCFGFunc();
ParseResult parseMLFunc();
ParseResult parseBasicBlock(CFGFunctionParserState &functionState);
- TerminatorInst *parseTerminator(BasicBlock *currentBB,
- CFGFunctionParserState &functionState);
MLStatement *parseMLStatement(MLFunction *currentFunction);
+ ParseResult parseCFGOperation(BasicBlock *currentBB,
+ CFGFunctionParserState &functionState);
+ ParseResult parseTerminator(BasicBlock *currentBB,
+ CFGFunctionParserState &functionState);
+
};
} // end anonymous namespace
@@ -490,7 +493,7 @@ ParseResult Parser::parseAffineMapDef() {
// Check that 'affineMapId' is unique.
// TODO(andydavis) Add a unit test for this case.
if (affineMaps.count(affineMapId) > 0)
- return emitError("encountered non-unique affine map id");
+ return emitError("redefinition of affine map id '" + affineMapId + "'");
consumeToken(Token::affine_map_id);
@@ -660,22 +663,54 @@ ParseResult Parser::parseBasicBlock(CFGFunctionParserState &functionState) {
if (!consumeIf(Token::colon))
return emitError("expected ':' after basic block name");
+ // Parse the list of operations that make up the body of the block.
+ while (curToken.isNot(Token::kw_return, Token::kw_br)) {
+ if (parseCFGOperation(block, functionState))
+ return ParseFailure;
+ }
- // TODO(clattner): Verify block hasn't already been parsed (this would be a
- // redefinition of the same name) once we have a body implementation.
+ if (parseTerminator(block, functionState))
+ return ParseFailure;
- // TODO(clattner): Move block to the end of the list, once we have a proper
- // block list representation in CFGFunction.
+ return ParseSuccess;
+}
- // TODO: parse instruction list.
- // TODO: Generalize this once instruction list parsing is built out.
+/// Parse the CFG operation.
+///
+/// TODO(clattner): This is a change from the MLIR spec as written, it is an
+/// experiment that will eliminate "builtin" instructions as a thing.
+///
+/// cfg-operation ::=
+/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
+/// `:` function-type
+///
+ParseResult Parser::
+parseCFGOperation(BasicBlock *currentBB,
+ CFGFunctionParserState &functionState) {
- auto *termInst = parseTerminator(block, functionState);
- if (!termInst)
- return ParseFailure;
- block->setTerminator(termInst);
+ // TODO: parse ssa-id.
+
+ if (curToken.isNot(Token::string))
+ return emitError("expected operation name in quotes");
+
+ auto name = curToken.getStringValue();
+ if (name.empty())
+ return emitError("empty operation name is invalid");
+
+ consumeToken(Token::string);
+
+ if (!consumeIf(Token::l_paren))
+ return emitError("expected '(' in operation");
+
+ // TODO: Parse operands.
+ if (!consumeIf(Token::r_paren))
+ return emitError("expected '(' in operation");
+
+ auto nameId = Identifier::get(name, context);
+ new OperationInst(nameId, currentBB);
+ // TODO: add instruction the per-function symbol table.
return ParseSuccess;
}
@@ -688,23 +723,25 @@ ParseResult Parser::parseBasicBlock(CFGFunctionParserState &functionState) {
/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
/// terminator-stmt ::= `return` ssa-use-and-type-list?
///
-TerminatorInst *Parser::parseTerminator(BasicBlock *currentBB,
- CFGFunctionParserState &functionState) {
+ParseResult Parser::parseTerminator(BasicBlock *currentBB,
+ CFGFunctionParserState &functionState) {
switch (curToken.getKind()) {
default:
- return (emitError("expected terminator at end of basic block"), nullptr);
+ return emitError("expected terminator at end of basic block");
case Token::kw_return:
consumeToken(Token::kw_return);
- return new ReturnInst(currentBB);
+ new ReturnInst(currentBB);
+ return ParseSuccess;
case Token::kw_br: {
consumeToken(Token::kw_br);
auto destBB = functionState.getBlockNamed(curToken.getSpelling(),
curToken.getLoc());
if (!consumeIf(Token::bare_identifier))
- return (emitError("expected basic block name"), nullptr);
- return new BranchInst(destBB, currentBB);
+ return emitError("expected basic block name");
+ new BranchInst(destBB, currentBB);
+ return ParseSuccess;
}
}
}
diff --git a/mlir/lib/Parser/Token.cpp b/mlir/lib/Parser/Token.cpp
index c721cf1d625..a8affc7f504 100644
--- a/mlir/lib/Parser/Token.cpp
+++ b/mlir/lib/Parser/Token.cpp
@@ -39,7 +39,7 @@ SMRange Token::getLocRange() const {
/// For an integer token, return its value as an unsigned. If it doesn't fit,
/// return None.
-Optional<unsigned> Token::getUnsignedIntegerValue() {
+Optional<unsigned> Token::getUnsignedIntegerValue() const {
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
unsigned result = 0;
@@ -47,3 +47,12 @@ Optional<unsigned> Token::getUnsignedIntegerValue() {
return None;
return result;
}
+
+/// Given a 'string' token, return its value, including removing the quote
+/// characters and unescaping the contents of the string.
+std::string Token::getStringValue() const {
+ // TODO: Handle escaping.
+
+ // Just drop the quotes off for now.
+ return getSpelling().drop_front().drop_back().str();
+}
diff --git a/mlir/lib/Parser/Token.h b/mlir/lib/Parser/Token.h
index 8a654a17ada..15ce0150255 100644
--- a/mlir/lib/Parser/Token.h
+++ b/mlir/lib/Parser/Token.h
@@ -38,6 +38,7 @@ public:
// TODO: @@foo, etc.
integer, // 42
+ string, // "foo"
// Punctuation.
arrow, // ->
@@ -105,7 +106,11 @@ public:
/// For an integer token, return its value as an unsigned. If it doesn't fit,
/// return None.
- Optional<unsigned> getUnsignedIntegerValue();
+ Optional<unsigned> getUnsignedIntegerValue() const;
+
+ /// Given a 'string' token, return its value, including removing the quote
+ /// characters and unescaping the contents of the string.
+ std::string getStringValue() const;
// Location processing.
llvm::SMLoc getLoc() const;
OpenPOWER on IntegriCloud