Skip to content

Commit

Permalink
Merge pull request #106 from NLnetLabs/strings
Browse files Browse the repository at this point in the history
Merge the strings to main now
  • Loading branch information
tertsdiepraam authored Jan 21, 2025
2 parents b7fdbb4 + 670347e commit f3d66e2
Show file tree
Hide file tree
Showing 16 changed files with 604 additions and 35 deletions.
13 changes: 11 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use std::{env::args, net::IpAddr};

use roto::{read_files, Runtime, Verdict};
use roto::{read_files, Context, Runtime, Verdict};

fn main() -> Result<(), roto::RotoReport> {
env_logger::init();

let runtime = Runtime::basic().unwrap();
let mut runtime = Runtime::basic().unwrap();

// Adding a context is not necessary but done here for testing purposes
#[derive(Context)]
struct Ctx {
/// This is the foo usize
pub foo: u32,
}

runtime.register_context_type::<Ctx>().unwrap();

let mut arguments = args();
let _program_name = arguments.next().unwrap();
Expand Down
30 changes: 19 additions & 11 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ pub fn roto_context(item: TokenStream) -> TokenStream {
let offset = quote!(std::mem::offset_of!(Self, #field_name));
let type_name = quote!(std::any::type_name::<#field_ty>());
let type_id = quote!(std::any::TypeId::of::<#field_ty>());
let docstring = gather_docstring(&f.attrs);

quote!(
roto::ContextField {
name: stringify!(#field_name),
offset: #offset,
type_name: #type_name,
type_id: #type_id
type_id: #type_id,
docstring: String::from(#docstring),
}
)
})
Expand Down Expand Up @@ -191,17 +193,10 @@ pub fn roto_static_method(
TokenStream::from(expanded)
}

fn generate_function(item: syn::ItemFn) -> Intermediate {
let syn::ItemFn {
attrs,
vis,
sig,
block: _,
} = item.clone();

fn gather_docstring(attrs: &[syn::Attribute]) -> String {
let mut docstring = String::new();

for attr in &attrs {
for attr in attrs {
if attr.path().is_ident("doc") {
let value = match &attr.meta {
syn::Meta::NameValue(name_value) => &name_value.value,
Expand All @@ -224,6 +219,19 @@ fn generate_function(item: syn::ItemFn) -> Intermediate {
}
}

docstring
}

fn generate_function(item: syn::ItemFn) -> Intermediate {
let syn::ItemFn {
attrs,
vis,
sig,
block: _,
} = item.clone();

let docstring = gather_docstring(&attrs);

assert!(sig.unsafety.is_none());
assert!(sig.variadic.is_none());

Expand Down Expand Up @@ -266,7 +274,7 @@ fn generate_function(item: syn::ItemFn) -> Intermediate {
#vis extern "C" fn #ident #generics ( out: *mut #ret, #(#inputs,)* ) {
#item

unsafe { *out = #ident(#(#args),*) };
unsafe { std::ptr::write(out, #ident(#(#args),*)) };
}
};

Expand Down
6 changes: 5 additions & 1 deletion src/codegen/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::{
types::{Primitive, Type},
},
};
use std::{any::TypeId, fmt::Display, mem::MaybeUninit, net::IpAddr};
use std::{
any::TypeId, fmt::Display, mem::MaybeUninit, net::IpAddr, sync::Arc,
};

#[derive(Debug)]
pub enum FunctionRetrievalError {
Expand Down Expand Up @@ -86,6 +88,7 @@ fn check_roto_type(
let ASN: TypeId = TypeId::of::<Asn>();
let IPADDR: TypeId = TypeId::of::<IpAddr>();
let PREFIX: TypeId = TypeId::of::<Prefix>();
let STRING: TypeId = TypeId::of::<Arc<str>>();

let Some(rust_ty) = registry.get(rust_ty) else {
return Err(TypeMismatch {
Expand Down Expand Up @@ -121,6 +124,7 @@ fn check_roto_type(
x if x == ASN => Type::Primitive(Primitive::Asn),
x if x == IPADDR => Type::Primitive(Primitive::IpAddr),
x if x == PREFIX => Type::Primitive(Primitive::Prefix),
x if x == STRING => Type::Primitive(Primitive::String),
_ => panic!(),
};
if expected_roto == roto_ty {
Expand Down
60 changes: 59 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ struct ModuleBuilder {
/// Signature to use for calls to `drop`
drop_signature: Signature,

/// Signature to use for calls to `init_string`
init_string_signature: Signature,

context_description: ContextDescription,
}

Expand All @@ -231,6 +234,9 @@ struct FuncGen<'c> {

/// Signature to use for calls to `drop`
drop_signature: SigRef,

/// Signature to use for calls to `init_string`
init_string_signature: SigRef,
}

// We use `with_aligned` to make sure that we notice if anything is
Expand Down Expand Up @@ -279,6 +285,17 @@ pub fn codegen(
.params
.push(AbiParam::new(isa.pointer_type()));

let mut init_string_signature = jit.make_signature();
init_string_signature
.params
.push(AbiParam::new(isa.pointer_type()));
init_string_signature
.params
.push(AbiParam::new(isa.pointer_type()));
init_string_signature
.params
.push(AbiParam::new(cranelift::codegen::ir::types::I32));

let mut module = ModuleBuilder {
functions: HashMap::new(),
runtime_functions: HashMap::new(),
Expand All @@ -289,6 +306,7 @@ pub fn codegen(
type_info,
drop_signature,
clone_signature,
init_string_signature,
context_description,
};

Expand Down Expand Up @@ -441,6 +459,8 @@ impl ModuleBuilder {
.import_signature(self.drop_signature.clone()),
clone_signature: builder
.import_signature(self.clone_signature.clone()),
init_string_signature: builder
.import_signature(self.init_string_signature.clone()),
module: self,
builder,
scope: *scope,
Expand Down Expand Up @@ -555,7 +575,7 @@ impl<'c> FuncGen<'c> {
args.next().unwrap(),
);

if dbg!(return_ptr) {
if return_ptr {
self.def(
self.module.variable_map[&Var {
scope: self.scope,
Expand Down Expand Up @@ -927,6 +947,44 @@ impl<'c> FuncGen<'c> {
let to = self.variable(to, ty);
self.def(to, val);
}
ir::Instruction::InitString {
to,
string,
init_func,
} => {
let data_id = self
.module
.inner
.declare_anonymous_data(false, false)
.unwrap();

let mut description = DataDescription::new();
description.define(string.clone().into_bytes().into());
self.module
.inner
.define_data(data_id, &description)
.unwrap();

let global_value = self
.module
.inner
.declare_data_in_func(data_id, self.builder.func);

let pointer_ty = self.module.isa.pointer_type();
let init_func = self.ins().iconst(
pointer_ty,
*init_func as *mut u8 as usize as i64,
);
let data = self.ins().global_value(pointer_ty, global_value);
let len = self.ins().iconst(I32, string.len() as u64 as i64);

let (to, _) = self.operand(&Operand::Place(to.clone()));
self.builder.ins().call_indirect(
self.init_string_signature,
init_func,
&[to, data, len],
);
}
}
}

Expand Down
Loading

0 comments on commit f3d66e2

Please sign in to comment.