Skip to content

Commit

Permalink
wip: running onlinechangexmpl almost works! crashes on signal1()
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Jun 26, 2024
1 parent f306874 commit a501648
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 21 deletions.
13 changes: 9 additions & 4 deletions compiler/section_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ pub const RUSTY_PREFIX: &str = "$RUSTY$";

// TODO: How to encode variadics?
fn mangle_function(FunctionMangler { name, parameters, return_type }: FunctionMangler) -> String {
let mangled = parameters
.into_iter()
/* FIXME: Is that correct? */
.fold(return_type.unwrap_or(Type::Void).to_string(), |mangled, arg| format!("{mangled}[{arg}]"));
/* FIXME: Is that correct? */
let return_type = return_type.unwrap_or(Type::Void);

let mangled = match parameters.as_slice() {
[] => format!("{return_type}[]"),
parameters => {
parameters.iter().fold(return_type.to_string(), |mangled, arg| format!("{mangled}[{arg}]"))
}
};

format!("{name}:{mangled}")
}
Expand Down
89 changes: 87 additions & 2 deletions compiler/section_mangler/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FunctionArgument, SectionMangler, Type};
use crate::{FunctionArgument, SectionMangler, StringEncoding, Type};

use std::str;

Expand Down Expand Up @@ -75,8 +75,29 @@ fn type_enum(input: &str) -> ParseResult<Type> {
.parse(input)
}

fn string_encoding(input: &str) -> ParseResult<StringEncoding> {
let utf8 = tag("8u").map(|_| StringEncoding::Utf8);
let utf16 = tag("16u").map(|_| StringEncoding::Utf16);

alt((utf8, utf16))(input)
}

fn type_string(input: &str) -> ParseResult<Type> {
char('s')
.and(string_encoding)
.and(number::<usize>)
.map(|((_, encoding), size)| Type::String { size, encoding })
.parse(input)
}

fn type_array(input: &str) -> ParseResult<Type> {
char('a').and(parse_type).map(|(_, inner_ty)| Type::Array { inner: Box::new(inner_ty) }).parse(input)
}

fn parse_type(input: &str) -> ParseResult<Type> {
alt((type_void, type_integer, type_float, type_pointer, type_struct, type_enum))(input)
alt((type_void, type_integer, type_float, type_pointer, type_struct, type_enum, type_string, type_array))(
input,
)
}

fn parse_var_content<'i>(input: &'i str, name: &str) -> ParseResult<'i, SectionMangler> {
Expand Down Expand Up @@ -188,6 +209,7 @@ mod tests {
// this needs to be handled by the toplevel parse function
assert!(type_struct("r0u8u8").is_ok());
assert!(type_struct("r1u8u8").is_ok());
assert!(type_struct("r5s8u1025s8u2049s8u3u64s8u3").is_ok());

// invalid number of elements
assert!(type_struct("r15u8").is_err());
Expand Down Expand Up @@ -235,4 +257,67 @@ mod tests {

assert_eq!(mangled.name(), "Color.red");
}

#[test]
fn parse_complex1() {
let mangled = SectionMangler::from("$RUSTY$var-__File__init:r5s8u1025s8u2049s8u3u64s8u3");

assert_eq!(mangled.name(), "__File__init");
}

#[test]
fn parse_complex2() {
let inputs = [
"$RUSTY$var-__CosineSignal__init:r4f64i32f64f64",
"$RUSTY$var-__File__init:r5s8u1025s8u2049s8u3u64s8u3",
"$RUSTY$var-__SineSignal__init:r4f64i32f64f64",
"$RUSTY$var-__mainProg_state.Init:e3i32",
"$RUSTY$var-__mainProg_state.Running:e3i32",
"$RUSTY$var-__mainProg_state.Stopped:e3i32",
"$RUSTY$var-__SR__init:r3u8u8u8",
"$RUSTY$var-__RS__init:r3u8u8u8",
"$RUSTY$var-__CTU__init:r6u8u8i16u8i16u8",
"$RUSTY$var-__CTU_INT__init:r6u8u8i16u8i16u8",
"$RUSTY$var-__CTU_DINT__init:r6u8u8i32u8i32u8",
"$RUSTY$var-__CTU_UDINT__init:r6u8u8u32u8u32u8",
"$RUSTY$var-__CTU_LINT__init:r6u8u8i64u8i64u8",
"$RUSTY$var-__CTU_ULINT__init:r6u8u8u64u8u64u8",
"$RUSTY$var-__CTD__init:r6u8u8i16u8i16u8",
"$RUSTY$var-__CTD_INT__init:r6u8u8i16u8i16u8",
"$RUSTY$var-__CTD_DINT__init:r6u8u8i32u8i32u8",
"$RUSTY$var-__CTD_UDINT__init:r6u8u8u32u8u32u8",
"$RUSTY$var-__CTD_LINT__init:r6u8u8i64u8i64u8",
"$RUSTY$var-__CTD_ULINT__init:r6u8u8u64u8u64u8",
"$RUSTY$var-__CTUD__init:r10u8u8u8u8i16u8u8i16u8u8",
"$RUSTY$var-__CTUD_INT__init:r10u8u8u8u8i16u8u8i16u8u8",
"$RUSTY$var-__CTUD_DINT__init:r10u8u8u8u8i32u8u8i32u8u8",
"$RUSTY$var-__CTUD_UDINT__init:r10u8u8u8u8u32u8u8u32u8u8",
"$RUSTY$var-__CTUD_LINT__init:r10u8u8u8u8i64u8u8i64u8u8",
"$RUSTY$var-__CTUD_ULINT__init:r10u8u8u8u8u64u8u8u64u8u8",
"$RUSTY$var-__R_TRIG__init:r3u8u8u8",
"$RUSTY$var-__F_TRIG__init:r3u8u8u8",
"$RUSTY$var-__TP__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TP_TIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TP_LTIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TON__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TON_TIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TON_LTIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TOF__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TOF_TIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$var-__TOF_LTIME__init:r7u8i64u8i64u8u8au8",
"$RUSTY$fn-CosineSignal:v[f64][i32][f64]",
"$RUSTY$fn-File:v[s8u1025][s8u2049][s8u3]",
"$RUSTY$fn-File.Open:v",
"$RUSTY$fn-File.Write:v",
"$RUSTY$fn-File.Close:v",
"$RUSTY$fn-File.Clear:v",
"$RUSTY$fn-SineSignal:v[f64][i32][f64]",
"$RUSTY$fn-mainProg:v",
"$RUSTY$var-mainProg:r7i32r4f64i32f64f64r4f64i32f64f64e3i32r5s8u1025s8u2049s8u3u64s8u3r5s8u1025s8u2049s8u3u64s8u3r5s8u1025s8u2049s8u3u64s8u3"
];

inputs.into_iter().for_each(|input| {
let _ = SectionMangler::from(dbg!(input));
});
}
}
28 changes: 13 additions & 15 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,24 @@ impl<'ink> CodeGen<'ink> {
// write_got_layout(new_got_entries, location.as_str(), *format)?;

// Construct our GOT as a new global array. We initialise this array in the loader code.
let got_size = new_got.keys().max().map_or(0, |m| *m + 1);
let got_size: u32 = new_got
.keys()
.max()
.map_or(0, |m| *m + 1)
.try_into()
.expect("the computed custom GOT size is too large");
eprintln!("creating __custom_got array");

let custom_got_ty = BasicTypeEnum::ArrayType(Llvm::get_array_type(
BasicTypeEnum::PointerType(llvm.context.i8_type().ptr_type(0.into())),
got_size.try_into().expect("the computed custom GOT size is too large"),
));

let ptr_ty = llvm.context.i8_type().ptr_type(AddressSpace::default());
let init = ptr_ty.const_array(vec![ptr_ty.const_null(); got_size.try_into().unwrap()].as_slice());

// let initial_value = unsafe {
// inkwell::values::ArrayValue::new_const_array(
// BasicTypeEnum::PointerType(llvm.context.i8_type().ptr_type(AddressSpace::default())),
// vec![llvm.create_null_ptr().unwrap(); got_size.try_into().unwrap()].as_slice(),
// )
// };
let empty_got = ptr_ty
.const_array(vec![ptr_ty.const_null(); got_size as usize].as_slice())
.as_basic_value_enum();
let custom_got_ty =
BasicTypeEnum::ArrayType(Llvm::get_array_type(BasicTypeEnum::PointerType(ptr_ty), got_size));

let custom_got = llvm.create_global_variable(&self.module, "__custom_got", custom_got_ty);
custom_got.set_initial_value(Some(init.as_basic_value_enum()), custom_got_ty);
custom_got.set_linkage(inkwell::module::Linkage::WeakODR);
custom_got.set_initial_value(Some(empty_got), custom_got_ty);

*got_entries = new_got_entries;
}
Expand Down

0 comments on commit a501648

Please sign in to comment.