Is there BNF-like grammar to describe LLVM IR?

2020-06-02 09:57发布

I am wondering that whether there is a BNF-grammar to describe LLVM IR? like C BNF grammar describing C language, etc.

标签: llvm
7条回答
时光不老,我们不散
2楼-- · 2020-06-02 10:26

I just found LLIR ("Unofficial libraries for interacting with LLVM IR"), which includes a (WIP?) grammar repository ("BNF grammar for LLVM IR assembly").

查看更多
霸刀☆藐视天下
4楼-- · 2020-06-02 10:35

Yes. Go to the file "LLParser.cpp" in "LLVM/lib/AsmParser"; the EBNF is spread throughput the file, for instance, the function LLParser::ParseNamedType() around line 350 has the following function-comment:

/// toplevelentity
///   ::= LocalVar '=' 'type' type
bool LLParser::ParseNamedType() {
  /* impl. */
}

I'm sure a regular-expression would happily extract the EBNF. I'm doing so by hand as I go through the parser.

查看更多
欢心
5楼-- · 2020-06-02 10:39

My parser for LLVM assembly language, written for a YACC-like parser generator, can serve as a BNF definition for the language.

查看更多
一纸荒年 Trace。
6楼-- · 2020-06-02 10:40

In reply to @tschwinge

I just found LLIR ("Unofficial libraries for interacting with LLVM IR"), which includes a (WIP?) grammar repository ("BNF grammar for LLVM IR assembly").

One of the original authors here. The grammar repository is indeed a work in process, and the aim is to define a BNF grammar for the assembly language representation of LLVM IR. It's current progress may be tracked at https://github.com/llir/llvm/blob/master/current.md

In the future, my friends and I would wish to contribute this grammar back to the official LLVM project, and would love it if the lexer and parser were one day generated from a BNF grammar, as that would ensure an up-to-date specification of the language.

A problem we've encountered when writing the BNF grammar is that there is no single source of truth (well except the source code of course). We've compared the BNF comments of the source code against the LangRef documentation, and sometimes these were not in sync with the actual C++ code, which has lead to some confusion.

The current status of the BNF grammar is that it supports all instructions and constants expressions of LLVM IR. There are still some minor additions to be added to support all LLVM IR concepts, and currently we are working on supporting DWARF metadata. General metadata nodes are already supported.

Cheerful regards, /u & i

查看更多
再贱就再见
7楼-- · 2020-06-02 10:50

There isn't a published BNF grammar, but there is a polygen grammer file which is partially maintained, and used for exactly the purpose of randomly generating inputs to use to test the LLVM IR parser and lexer.

You can see the current version of the grammar here:

http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/llvm.grm?revision=112427&view=markup

查看更多
登录 后发表回答