6 releases

0.3.0 Aug 14, 2023
0.2.1 Aug 14, 2023
0.1.3 Aug 1, 2023

#997 in Programming languages

Download history 152/week @ 2024-01-02 126/week @ 2024-01-09 118/week @ 2024-01-16 85/week @ 2024-01-23 67/week @ 2024-01-30 91/week @ 2024-02-06 327/week @ 2024-02-13 380/week @ 2024-02-20 194/week @ 2024-02-27 229/week @ 2024-03-05 343/week @ 2024-03-12 340/week @ 2024-03-19 238/week @ 2024-03-26 234/week @ 2024-04-02 253/week @ 2024-04-09 190/week @ 2024-04-16

1,021 downloads per month
Used in 2 crates (via melior-macro)

MIT/Apache

85KB
2K SLoC

Rust 1.5K SLoC // 0.0% comments C++ 458 SLoC // 0.1% comments

tblgen

This crate provides raw bindings and a safe wrapper for TableGen, a domain-specific language used by the LLVM project.

The goal of this crate is to enable users to develop custom TableGen backends in Rust. Hence the primary use case of this crate are procedural macros that generate Rust code from TableGen description files.

Documentation

Read the documentation at https://danacus.gitlab.io/tblgen-rs/tblgen/.

Supported LLVM Versions

An installation of LLVM is required to use this crate. This crate only aims to support the latest version of LLVM. The version of LLVM currently supported is 17.x.x.

The 1TABLEGEN_170_PREFIX` environment variable can be used to specify a custom directory of the LLVM installation.


lib.rs:

This crate provides raw bindings and a safe wrapper for TableGen, a domain-specific language used by the LLVM project.

The goal of this crate is to enable users to develop custom TableGen backends in Rust. Hence the primary use case of this crate are procedural macros that generate Rust code from TableGen description files.

Safety

This crate aims to be completely safe.

Supported LLVM Versions

An installation of LLVM is required to use this crate. The versions of LLVM currently supported are 16.x.x (default) and 17.x.x. Different LLVM version can be selected using features flags (llvm16-0 or llvm17-0).

The TABLEGEN_<version>_PREFIX environment variable can be used to specify a custom directory of the LLVM installation.

Examples

The following example parse simple TableGen code provided as a &str and iterates over classes and defs defined in this file.

use tblgen::{TableGenParser, RecordKeeper};

let keeper: RecordKeeper = TableGenParser::new()
    .add_source(
        r#"
        class A;
        def D: A;
        "#,
    )?
    .parse()?;
assert_eq!(keeper.classes().next().unwrap().0, Ok("A"));
assert_eq!(keeper.defs().next().unwrap().0, Ok("D"));
assert_eq!(keeper.all_derived_definitions("A").next().unwrap().name(), Ok("D"));

By adding include paths, external TableGen files can be included.

use tblgen::{TableGenParser, RecordKeeper};
use std::path::Path;

let keeper: RecordKeeper = TableGenParser::new()
    .add_source(r#"include "mlir/IR/OpBase.td""#)?
    .add_include_path(&format!("{}/include", std::env::var("TABLEGEN_170_PREFIX")?))
    .parse()?;
let i32_def = keeper.def("I32").expect("has I32 def");
assert!(i32_def.subclass_of("I"));
assert_eq!(i32_def.int_value("bitwidth"), Ok(32));

API Stability

LLVM does not provide a stable C API for TableGen, and the C API provided by this crate is not stable. Furthermore, the safe wrapper does not provide a stable interface either, since this crate is still in early development.

Dependencies

~0.3–2.7MB
~56K SLoC