Browse Source

remove LLVM for now

master
Alison Watson 1 week ago
parent
commit
803b91bed2
8 changed files with 10 additions and 520 deletions
  1. +0
    -7
      Cargo.lock
  2. +2
    -5
      Cargo.toml
  3. +4
    -0
      fw/build.rs
  4. +3
    -53
      fw/vire/rt.rs
  5. +0
    -128
      fw/vire/rt/function.rs
  6. +0
    -23
      fw/vire/rt/nat.rs
  7. +0
    -117
      fw/vire/rt/types.rs
  8. +1
    -187
      ma/lib.rs

+ 0
- 7
Cargo.lock View File

@@ -45,7 +45,6 @@ dependencies = [
"ash",
"bitflags",
"blonkus-ma",
"cc",
"easy-cast",
"glam",
"half",
@@ -73,12 +72,6 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"

[[package]]
name = "cc"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d26a6ce4b6a484fa3edb70f7efa6fc430fd2b87285fe8b84304fd0936faa0dc0"

[[package]]
name = "cfg-if"
version = "1.0.0"


+ 2
- 5
Cargo.toml View File

@@ -55,11 +55,8 @@ half = "~1.7"
ash = "~0.32"
naga = { version = "~0.6", features = ["wgsl-in", "spv-out"] }

[build-dependencies]
cc = "1.0"

[profile.dev]
opt-level = 1
#[build-dependencies]
#cc = "1.0"

[profile.release]
codegen-units = 1


+ 4
- 0
fw/build.rs View File

@@ -1,3 +1,4 @@
/*
fn llvm_config(args: Vec<&str>) -> String {
std::process::Command::new("llvm-config")
.args(args)
@@ -22,8 +23,10 @@ fn with_components(mut v: Vec<&str>) -> Vec<&str> {
v.push("native");
v
}
*/

fn main() {
/*
let a_incdr = vec!["--includedir"];
let a_libdr = vec!["--libdir"];
let a_llibs = with_components(vec!["--libs", "--link-static"]);
@@ -56,6 +59,7 @@ fn main() {
.include(incdr)
.file("fw/vire/llvm.cxx")
.compile("helper");
*/
}

// EOF

+ 3
- 53
fw/vire/rt.rs View File

@@ -1,58 +1,8 @@
//pub mod types;

use super::llvm;

#[derive(thiserror::Error, Debug)]
pub enum Err {
#[error(transparent)]
Llvm(#[from] llvm::Err),
}

pub struct Rt {
build: *mut llvm::Builder,
modul: *mut llvm::Module,
contx: *mut llvm::Context,

jit_e: *mut llvm::Jit,
}

impl Rt {
pub fn new() -> Result<Self, Err> {
unsafe {
llvm::target_init();

let jit_e = jit_engine()?;

let contx = llvm::context_new();
let modul = llvm::module_new_with_name_in_context(
crate::c_str!("main"),
contx,
);
let build = llvm::builder_new_in_context(contx);

Ok(Self { build, modul, contx, jit_e })
}
}
}

impl Drop for Rt {
fn drop(&mut self) {
unsafe {
llvm::builder_drop(self.build);
llvm::module_drop(self.modul);
llvm::context_drop(self.contx);
pub enum Err {}

llvm::jit_drop(self.jit_e);
}
}
}
pub struct Rt {}

unsafe fn jit_engine() -> Result<*mut llvm::Jit, Err> {
let mut jit = std::ptr::null_mut();
unsafe {
llvm::Err::check(llvm::jit_new(&mut jit))?;
}
Ok(jit)
}
impl Rt {}

// EOF

+ 0
- 128
fw/vire/rt/function.rs View File

@@ -1,128 +0,0 @@
use super::*;
use std::{ffi::CString, io::Read};

pub struct IncompleteFunction<'a> {
value: *mut llvm::Value,
block: *mut llvm::BasicBlock,
contx: &'a Context,
in_context: bool,
}

pub struct Function {
value: *mut llvm::Value,
}

impl<'a> IncompleteFunction<'a> {
pub fn new(c: &'a Context, name: ffi::Nts, ftyp: &Type) -> Self {
const ENT: ffi::Nts = crate::c_str!("entry");
unsafe {
let value =
llvm::module_add_function(c.modul, name, ftyp.llvm_type());
let block = llvm::context_append_basic_block(c.contx, value, ENT);
Self { value, block, contx: c, in_context: false }
}
}

pub fn compile(
&mut self, datum: &Datum,
) -> Result<(*mut llvm::Type, *mut llvm::Value), Err> {
if !self.in_context {
return Err(Err::Context);
}

unsafe {
match datum {
| Datum::Null => {
let ty = BasicType::Void.llvm_type();
let vl = llvm::value_undef(ty);
Ok((ty, vl))
}
| Datum::Bool(val) => {
let ty = BasicType::Bool.llvm_type();
let vl = llvm::value_const_int(ty, *val as _, Bool::False);
Ok((ty, vl))
}
| Datum::Char(chr) => {
let ty = BasicType::Q32_0U.llvm_type();
let vl = llvm::value_const_int(ty, *chr as _, Bool::False);
Ok((ty, vl))
}
| Datum::Strn(_) => {
// TODO
Err(Err::Syntax)
}
| Datum::Symb(name) => {
// TODO: must support more than global function symbols
let name = CString::new(name.as_bytes()).unwrap().into_raw();
let func =
llvm::module_get_named_function(self.contx.modul, name);
CString::from_raw(name);
Ok((llvm::value_type(func), func))
}
| Datum::Numb(num) => {
let ty = BasicType::Q64_0.llvm_type();
let vl = llvm::value_const_int(ty, *num as _, Bool::False);
Ok((ty, vl))
}
| Datum::Cons { cdr, car } => {
// function call
let (ftyp, func) = self.compile(car)?;
let mut args = Vec::new();

let mut cdr = cdr.as_ref();
while let Datum::Cons { cdr: cdr_next, car: car_next } = cdr {
args.push(self.compile(car_next)?.1);
cdr = cdr_next.as_ref();
}

let call = llvm::builder_call(
self.contx.build,
ftyp,
func,
args.as_mut_ptr(),
args.len() as _,
crate::c_str!(""),
);

Ok((llvm::value_type(call), call))
}
}
}
}

pub fn begin_context(
&mut self, block: impl FnOnce(&mut Self) -> Result<(), Err>,
) -> Result<(), Err> {
unsafe {
llvm::builder_position_at_end(self.contx.build, self.block);
}
self.in_context = true;
let res = block(self);
self.in_context = false;
res
}

pub fn complete(self) -> Result<Function, Err> {
// insert ret void
unsafe {
llvm::builder_ret_void(self.contx.build);
}

// verify
let err_msg = unsafe { llvm::value_verify_func(self.value) };

if err_msg.is_null() {
Ok(Function::new(self.value))
} else {
Err(llvm::get_msg_and_drop(err_msg, llvm::message_drop))
}
}
}

impl Function {
pub fn new(value: *mut llvm::Value) -> Self {
Self { value }
}
}

// EOF

+ 0
- 23
fw/vire/rt/nat.rs View File

@@ -1,23 +0,0 @@
use super::*;

macro_rules! nat_binop {
( $id:literal , $name:ident , $op:tt ) => {
#[nat_call($id, |Q64_0, Q64_0| -> Q64_0)]
fn $name(x: i64, y: i64) -> i64 {
x $op y
}
};
}

#[nat_call("dbgi", |Q64_0| -> Void)]
fn dbgi(n: i64) {
eprintln!("{}", n);
}

nat_binop!("i+", addi, +);
nat_binop!("i-", subi, -);
nat_binop!("i*", muli, *);
nat_binop!("i/", divi, /);
nat_binop!("i%", remi, %);

// EOF

+ 0
- 117
fw/vire/rt/types.rs View File

@@ -1,117 +0,0 @@
use super::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum BasicType {
// 0 bit type
Void,

// 1 bit type
Bool,

// signed integers
Q8_0,
Q16_0,
Q32_0,
Q64_0,

// unsigned integers
Q8_0U,
Q16_0U,
Q32_0U,
Q64_0U,

// fixed point
Q24_8,
Q24_8U,

// angles
Q16,
Q16U,
}

#[derive(Clone)]
pub struct ArrayType {
pub size: usize,
pub memb: Box<Type>,
}

/*
#[derive(Clone)]
pub struct StrucType {
pub memb: Vec<Type>,
}
*/

#[derive(Clone)]
pub struct FunctType {
pub args: Vec<Type>,
pub retn: Box<Type>,
}

#[derive(Clone)]
pub enum Type {
Basic(BasicType),
Array(ArrayType),
//Struc(StrucType),
Funct(FunctType),
}

pub trait LlvmType {
unsafe fn llvm_type(&self) -> *mut llvm::Type;
}

impl BasicType {
pub fn iter() -> impl DoubleEndedIterator<Item = Self> {
(0u8..14u8).map(|it| unsafe { std::mem::transmute(it) })
}
}

impl LlvmType for BasicType {
unsafe fn llvm_type(&self) -> *mut llvm::Type {
match self {
| Self::Void => llvm::type_void(),
| Self::Bool => llvm::type_int1(),
| Self::Q8_0 | Self::Q8_0U => llvm::type_int8(),
| Self::Q16_0 | Self::Q16_0U | Self::Q16 | Self::Q16U => {
llvm::type_int16()
}
| Self::Q32_0 | Self::Q32_0U | Self::Q24_8 | Self::Q24_8U => {
llvm::type_int32()
}
| Self::Q64_0 | Self::Q64_0U => llvm::type_int64(),
}
}
}

impl LlvmType for ArrayType {
unsafe fn llvm_type(&self) -> *mut llvm::Type {
llvm::type_array(self.memb.llvm_type(), self.size.cast())
}
}

impl LlvmType for FunctType {
unsafe fn llvm_type(&self) -> *mut llvm::Type {
let mut args =
self.args.iter().map(|arg| arg.llvm_type()).collect::<Vec<_>>();
llvm::type_function(
self.retn.llvm_type(),
args.as_mut_ptr(),
args.len().cast(),
Bool::False,
)
}
}

impl LlvmType for Type {
unsafe fn llvm_type(&self) -> *mut llvm::Type {
match self {
Self::Basic(ty) => ty.llvm_type(),
Self::Array(ty) => ty.llvm_type(),
//Self::Struc(ty) => ty.llvm_type(),
Self::Funct(ty) => ty.llvm_type(),
}
}
}

// EOF

+ 1
- 187
ma/lib.rs View File

@@ -3,121 +3,12 @@ use proc_macro2 as pm2;

use pm2::Ident;
use quote::ToTokens;
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
Token,
};

#[derive(Clone)]
struct BasicType {
ty: Ident,
}

#[derive(Clone)]
struct ArrayType {
size: syn::LitInt,
memb: Box<Type>,
}

#[derive(Clone)]
struct FunctType {
args: Punctuated<Type, Token![,]>,
retn: Box<Type>,
}

#[derive(Clone)]
enum Type {
BasicType(BasicType),
ArrayType(ArrayType),
FunctType(FunctType),
}

struct NatCallArgs {
name: syn::LitStr,
funct_type: FunctType,
}

struct ComplTypeArgs {
contx: syn::ExprPath,
compl: Type,
}
use syn::parse::{Parse, ParseStream};

struct ReprArgs {
repr: Ident,
}

impl Parse for BasicType {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self { ty: input.parse()? })
}
}

impl Parse for ArrayType {
fn parse(input: ParseStream) -> syn::Result<Self> {
let inner;
syn::bracketed!(inner in input);
let memb = inner.parse()?;
input.parse::<Token![;]>()?;
let size = inner.parse()?;
Ok(Self { size, memb })
}
}

impl Parse for FunctType {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.parse::<Token![|]>()?;

let mut args = Punctuated::new();

if !input.peek(Token![|]) {
loop {
args.push_value(input.parse()?);
if input.peek(Token![|]) {
break;
}
args.push_punct(input.parse()?);
}
}

input.parse::<Token![|]>()?;
input.parse::<Token![->]>()?;

let retn = input.parse()?;
Ok(Self { args, retn })
}
}

impl Parse for Type {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(Token![|]) {
Ok(Self::FunctType(input.parse()?))
} else if input.peek(syn::token::Bracket) {
Ok(Self::ArrayType(input.parse()?))
} else {
Ok(Self::BasicType(input.parse()?))
}
}
}

impl Parse for NatCallArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name = input.parse()?;
input.parse::<Token![,]>()?;
let funct_type = input.parse()?;
Ok(Self { name, funct_type })
}
}

impl Parse for ComplTypeArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let contx = input.parse()?;
input.parse::<Token![,]>()?;
let compl = input.parse()?;
Ok(Self { contx, compl })
}
}

impl Parse for ReprArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let inner;
@@ -127,56 +18,6 @@ impl Parse for ReprArgs {
}
}

impl ToTokens for BasicType {
fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
let ty = self.ty.clone();
tokens.extend(quote::quote! {
Type::Basic(BasicType::#ty)
});
}
}

impl ToTokens for ArrayType {
fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
let size = self.size.clone();
let memb = self.memb.clone();
tokens.extend(quote::quote! {
Type::Array(ArrayType { size: #size, memb: Box::new(#memb) })
});
}
}

impl ToTokens for FunctType {
fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
let ret = self.retn.clone();
let arg = self.args.iter().collect::<Vec<_>>();
tokens.extend(quote::quote! {
Type::Funct(FunctType { args: vec![#(#arg),*], retn: Box::new(#ret) })
});
}
}

impl ToTokens for Type {
fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
match self {
| Self::BasicType(basic) => basic.to_tokens(tokens),
| Self::ArrayType(array) => array.to_tokens(tokens),
| Self::FunctType(funct) => funct.to_tokens(tokens),
}
}
}

#[proc_macro]
pub fn compl_type(input: pm::TokenStream) -> pm::TokenStream {
let ComplTypeArgs { contx, compl } = syn::parse(input).unwrap();
pm::TokenStream::from(quote::quote! {
{
let c = #contx;
#compl
}
})
}

#[proc_macro_attribute]
pub fn serialize_config(
_attr: pm::TokenStream, item: pm::TokenStream,
@@ -204,33 +45,6 @@ pub fn serialize_config(
})
}

#[proc_macro_attribute]
pub fn nat_call(
args: pm::TokenStream, item: pm::TokenStream,
) -> pm::TokenStream {
let NatCallArgs { name, funct_type } = syn::parse(args).unwrap();
let mut item = syn::parse::<syn::ItemFn>(item).unwrap();
item.sig.abi = Some(syn::parse_quote! { extern "C" });

let i_name = item.sig.ident.clone();
let c_name = quote::format_ident!("c_{}", i_name);

pm::TokenStream::from(quote::quote! {
#item
pub fn #c_name(c: &mut Context) {
let name = crate::c_str!(#name);
let ftyp = #funct_type;
let fdef = #i_name as _;
unsafe {
let func =
llvm::module_add_function(c.modul, name, ftyp.llvm_type());
llvm::engine_add_global_mapping(c.engin, func, fdef);
c.funcs.push(Function::new(func));
}
}
})
}

#[proc_macro_derive(FieldInfo)]
pub fn derive_field_info(item: pm::TokenStream) -> pm::TokenStream {
let item = syn::parse::<syn::ItemStruct>(item).unwrap();


Loading…
Cancel
Save