Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Working fibonacci!
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh committed Apr 1, 2024
1 parent 1bca615 commit 3ca4bf5
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 99 deletions.
1 change: 1 addition & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ e2e-test-brig:
COPY (+e2e-test-sailrs/arm-v9.4-a.rkyv) arm-v9.4-a.rkyv
COPY (+build-borealis/borealis) borealis

RUN mkdir target
RUN ./borealis --standalone arm-v9.4-a.rkyv aarch64.rs
RUN rustc --edition 2021 aarch64.rs
# RUN ./aarch64 91500421
26 changes: 20 additions & 6 deletions borealis/src/brig/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ pub fn codegen(rudder: Context, entrypoint: InternedString) -> TokenStream {

fn decode_execute<T: Tracer>(value: u32, state: &mut State, tracer: &mut T) -> ExecuteResult {
// reset SEE
// todo: for the love of god make this not break if registers are regenerated
unsafe {
*(state.data.as_mut_ptr().byte_offset(0x38f0) as *mut u64) = 0
*(state.data.as_mut_ptr().byte_offset(0x3920) as *mut u64) = 0
};

#entrypoint
Expand Down Expand Up @@ -199,9 +200,14 @@ pub fn codegen_type(typ: Rc<Type>) -> TokenStream {
element_count,
element_type,
} => {
let count = quote!(#element_count);
let element_type = codegen_type(element_type.clone());
quote!([#element_type; #count])

if *element_count == 0 {
quote!(Vec<#element_type>)
} else {
let count = quote!(#element_count);
quote!([#element_type; #count])
}
}
Type::Bundled { value, len } => {
let value_type = codegen_type(value.clone());
Expand Down Expand Up @@ -456,6 +462,8 @@ fn codegen_stmt(stmt: Statement) -> TokenStream {
quote! {
((#ident) != 0)
}
} else if target.is_unknown_length_vector() {
quote!(Vec::from(#ident))
} else {
match kind {
CastOperationKind::ZeroExtend => {
Expand Down Expand Up @@ -606,10 +614,16 @@ fn codegen_stmt(stmt: Statement) -> TokenStream {
}
}
StatementKind::ReadElement { vector, index } => {
let index_typ = index.typ();

let vector = get_ident(&vector);
let index = get_ident(&index);
// todo remove this cast, need "machine word" size in rudder?
quote!(#vector[(#index) as usize])

if let Type::Bundled { .. } = &*index_typ {
quote!(#vector[(#index.value) as usize])
} else {
quote!(#vector[(#index) as usize])
}
}
StatementKind::MutateElement {
vector,
Expand All @@ -619,7 +633,7 @@ fn codegen_stmt(stmt: Statement) -> TokenStream {
let vector = get_ident(&vector);
let index = get_ident(&index);
let value = get_ident(&value);
// todo remove this cast, need "machine word" size in rudder?
// todo: support bundle indexes
quote! {
{
let mut local = #vector.clone();
Expand Down
98 changes: 92 additions & 6 deletions borealis/src/brig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ const FN_ALLOWLIST: &[&str] = &[
"__DecodeA64_BranchExcSys",
"decode_b_cond_aarch64_instrs_branch_conditional_cond",
"execute_aarch64_instrs_branch_conditional_cond",
"ConditionHolds",
"BranchNotTaken",
"HaveStatisticalProfiling",
"IsFeatureImplemented",
"num_of_Feature",
"decode_add_addsub_shift_aarch64_instrs_integer_arithmetic_add_sub_shiftedreg",
"decode_orr_log_shift_aarch64_instrs_integer_logical_shiftedreg",
"execute_aarch64_instrs_integer_logical_shiftedreg",
"decode_b_uncond_aarch64_instrs_branch_unconditional_immediate",
"execute_aarch64_instrs_branch_unconditional_immediate",
"PC_read",
"BranchTo",
"Hint_Branch",
"UsingAArch32",
"HaveAArch32",
"HaveAArch64",
"AArch64_BranchAddr",
"AddrTop",
"HaveEL",
"S1TranslationRegime",
"ELUsingAArch32",
"IsSecureBelowEL3",
"SCR_GEN_read",
"Mk_SCRType",
"_get_SCRType_NS",
"ELStateUsingAArch32",
"ELStateUsingAArch32K",
"HaveAArch32EL",
"HaveVirtHostExt",
"EffectiveTBI",
"_get_TCR_EL1_Type_TBI0",
"HavePACExt",
"HaveBRBExt",
"decode_svc_aarch64_instrs_system_exceptions_runtime_svc",
"execute_aarch64_instrs_system_exceptions_runtime_svc",
"AArch64_CheckForSVCTrap",
"HaveFGTExt",
"AArch64_CallSupervisor",
"SSAdvance",
"decode_hvc_aarch64_instrs_system_exceptions_runtime_hvc",
"DebugTarget",
"CurrentSecurityState",
];

mod codegen;
Expand Down Expand Up @@ -155,15 +197,48 @@ pub fn sail_to_brig<I: Iterator<Item = jib_ast::Definition>>(

let mut state = State::default();

text.chunks(4).map(|c| {
let mut buf = [0u8;4];
buf.copy_from_slice(c);
u32::from_le_bytes(buf)
}).for_each(|insr| {
// set enabled features
unsafe {
*(state.data.as_mut_ptr().byte_offset(0x18ed0) as *mut [bool; 259]) = [
false, false, false, false, true, true, true, false, false, true, true, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false,
]
};


loop {
let pc = unsafe { (state.data.as_mut_ptr().byte_offset(12704) as *mut u64) };
let insr = unsafe { *(text.as_ptr().offset(dbg!(unsafe {*pc}) as isize) as *mut u32) };
dbg!(decode_execute(insr, &mut state, &mut ConsoleTracer));
})

let branch_taken = unsafe { (state.data.as_mut_ptr().byte_offset(14856) as *mut bool) };

if !unsafe {*branch_taken} {
unsafe { *pc += 4};
}

unsafe {*branch_taken = false};
}
}
}
} else {
Expand Down Expand Up @@ -264,6 +339,17 @@ pub fn sail_to_brig<I: Iterator<Item = jib_ast::Definition>>(
}
}

impl<V: core::ops::BitXor<Output = V>, L> core::ops::BitXor for Bundle<V, L> {
type Output = Self;

fn bitxor(self, rhs: Self) -> Self::Output {
Self {
value: self.value ^ rhs.value,
length: self.length
}
}
}

impl<V: core::ops::Shl<Output = V>, L> core::ops::Shl for Bundle<V, L> {
type Output = Self;

Expand Down
Loading

0 comments on commit 3ca4bf5

Please sign in to comment.