Apache License 2.0

项目介绍

TheDan64/inkwell 是一个托管在 GitHub 上的开源项目,主要使用 Rust 编写,采用 Apache License 2.0 许可证,目前获得 3.0k 个 Star、263 次 Fork。

仓库全名
TheDan64/inkwell
开源许可证
Apache License 2.0
Star 数
3.0k
Fork 数
263
未关闭 Issue
108

README 摘要

Inkwell(s) It's a New Kind of Wrapper for Exposing LLVM (Safely) Inkwell aims to help you pen your own programming languages by safely wrapping llvm-sys. It provides a more strongly typed interface than the underlying LLVM C API so that certain types of errors can be caught at compile time instead of at LLVM's runtime. This means we are trying to replicate LLVM IR's strong typing as closely as possible. The ultimate…

完整内容见 GitHub 原始 README 或本页 。

常见问题

inkwell 用什么语言开发?

主要使用 Rust 编写,采用 Apache License 2.0 许可证,目前有 3.0k 个 Star。

inkwell 可以免费商用吗?

仓库采用 Apache License 2.0 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 inkwell 的源码?

可以直接克隆仓库:git clone https://github.com/TheDan64/inkwell.git,默认分支为 master;安装与运行步骤见仓库 README。

inkwell 最近还在维护吗?

仓库最近一次更新为 3 天前,当前有 108 个未关闭 Issue;Star 数为 3.0k,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

It's a New Kind of Wrapper for Exposing LLVM (Safely)

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
3586
价值分
1900
活跃状态
活跃

语言占比

Rust

Release

README

Inkwell(s)

Crates.io Build Status codecov lines of code Join the chat at https://gitter.im/inkwell-rs/Lobby Minimum rustc 1.85

It’s a New Kind of Wrapper for Exposing LLVM (Safely)

Inkwell aims to help you pen your own programming languages by safely wrapping llvm-sys. It provides a more strongly typed interface than the underlying LLVM C API so that certain types of errors can be caught at compile time instead of at LLVM’s runtime. This means we are trying to replicate LLVM IR’s strong typing as closely as possible. The ultimate goal is to make LLVM safer from the rust end and a bit easier to learn (via documentation) and use.

Requirements

  • Rust 1.85+
  • One of LLVM 12-23

Usage

You’ll need to point your Cargo.toml to use a single LLVM version feature flag corresponding to your LLVM version as such:

[dependencies]
inkwell = { version = "0.10.0", features = ["llvm23-1"] }

Supported versions: LLVM 12-23 mapping to a cargo feature flag llvmM-N where M and N correspond to the LLVM major and minor version, e.g. llvm18-1 for LLVM 18.1.x.

Please be aware that we may make breaking changes on master from time to time since we are pre-v1.0.0, in compliance with semver. Please prefer a crates.io release whenever possible!

Documentation

Documentation is automatically deployed here based on master. These docs are not yet 100% complete and only show the latest supported LLVM version due to a rustdoc issue. See #2 for more info.

Examples

Tari’s llvm-sys example written in safe code1 with Inkwell:

use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::execution_engine::{ExecutionEngine, JitFunction};
use inkwell::module::Module;
use inkwell::support::LLVMString;
use inkwell::OptimizationLevel;

use std::error::Error;

/// Convenience type alias for the `sum` function.
///
/// Calling this is innately `unsafe` because there's no guarantee it doesn't
/// do `unsafe` operations internally.
type SumFunc = unsafe extern "C" fn(u64, u64, u64) -> u64;

struct CodeGen<'ctx> {
    context: &'ctx Context,
    module: Module<'ctx>,
    builder: Builder<'ctx>,
}

impl<'ctx> CodeGen<'ctx> {
    fn compile_sum(&self) -> Option<()> {
        let i64_type = self.context.i64_type();
        let fn_type = i64_type.fn_type(&[i64_type.into(), i64_type.into(), i64_type.into()], false);
        let function = self.module.add_function("sum", fn_type, None);
        let basic_block = self.context.append_basic_block(function, "entry");

        self.builder.position_at_end(basic_block);

        let x = function.get_nth_param(0)?.into_int_value();
        let y = function.get_nth_param(1)?.into_int_value();
        let z = function.get_nth_param(2)?.into_int_value();

        let sum = self.builder.build_int_add(x, y, "sum").unwrap();
        let sum = self.builder.build_int_add(sum, z, "sum").unwrap();

        self.builder.build_return(Some(&sum)).unwrap();

        Some(())
    }

    /// Hands the finished module to a JIT engine, which owns it from now on.
    fn into_execution_engine(self) -> Result<ExecutionEngine<'ctx>, LLVMString> {
        self.module.create_jit_execution_engine(OptimizationLevel::None)
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let context = Context::create();
    let codegen = CodeGen {
        context: &context,
        module: context.create_module("sum"),
        builder: context.create_builder(),
    };

    codegen.compile_sum().ok_or("Unable to build `sum`")?;

    let execution_engine = codegen.into_execution_engine()?;
    let sum: JitFunction<SumFunc> = unsafe { execution_engine.get_function("sum")? };

    let x = 1u64;
    let y = 2u64;
    let z = 3u64;

    unsafe {
        println!("{} + {} + {} = {}", x, y, z, sum.call(x, y, z));
        assert_eq!(sum.call(x, y, z), x + y + z);
    }

    Ok(())
}

1 There are two uses of unsafe in this example because the actual act of compiling and executing code on the fly is innately unsafe. For one, there is no way of verifying we are calling get_function() with the right function signature. It is also unsafe to call the function we get because there’s no guarantee the code itself doesn’t do unsafe things internally (the same reason you need unsafe when calling into C).

LLVM’s Kaleidoscope Tutorial

Can be found in the examples directory.

Alternative Crate(s)

Contributing

Check out our Contributing Guide

评论

暂无评论