Skip to content

Commit

Permalink
feat(codegen-x86,xlang-backend)!: Switch codegen-x86 to using xlang_b…
Browse files Browse the repository at this point in the history
…ackend::mc
  • Loading branch information
chorman0773 committed Aug 1, 2023
1 parent bc151ec commit f5e4d41
Show file tree
Hide file tree
Showing 18 changed files with 862 additions and 56 deletions.
20 changes: 13 additions & 7 deletions c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ impl XLangFrontend for CFrontend {
}

impl XLangPlugin for CFrontend {
#[allow(clippy::too_many_lines)]
#[allow(clippy::too_many_lines, unreachable_code)] // ray can fix passing properties.
fn accept_ir(&mut self, file: &mut ir::File) -> Result<(), Error> {
fn into_xir_type_real(
char_type: &ir::ScalarType,
int_type: &ir::ScalarType,
ty: &Type,
pointer: Option<&Pointer>,
properties: &TargetProperties,
) -> ir::Type {
// const is not currently supported by xir
let inner = match &ty.base {
Expand All @@ -74,12 +75,13 @@ impl XLangPlugin for CFrontend {
int_type,
&param.inner,
param.pointer.as_ref().into(),
properties,
));
}
ir::Type::FnType(Box::new(ir::FnType {
ret: into_xir_type_real(char_type, int_type, ret, None),
ret: into_xir_type_real(char_type, int_type, ret, None, properties),
params: param_types,
tag: ir::Abi::C,
tag: properties.default_tag_name.into(),
variadic: false,
}))
}
Expand All @@ -106,19 +108,21 @@ impl XLangPlugin for CFrontend {
int_type: &ir::ScalarType,
expr: &Expression,
block: &mut Vec<ir::BlockItem>,
properties: &TargetProperties,
) {
match expr {
Expression::FunctionCall { callee, args, .. } => {
codegen_expr_real(char_type, int_type, callee, block);
codegen_expr_real(char_type, int_type, callee, block, properties);
for arg in args {
codegen_expr_real(char_type, int_type, arg, block);
codegen_expr_real(char_type, int_type, arg, block, properties);
}
block.push(ir::BlockItem::Expr(ir::Expr::CallFunction(
match into_xir_type_real(
char_type,
int_type,
&callee.get_type().unwrap().inner,
callee.get_type().unwrap().pointer.as_ref().into(),
properties,
) {
ir::Type::FnType(x) => (*x).clone(),
x => todo!("{:?}", x),
Expand All @@ -136,6 +140,7 @@ impl XLangPlugin for CFrontend {
int_type,
&ty.inner,
ty.pointer.as_ref().into(),
properties,
),
item: ir::Path {
components: xlang::vec![ir::PathComponent::Text(String::from(&id))],
Expand All @@ -155,6 +160,7 @@ impl XLangPlugin for CFrontend {
int_type,
&ty.inner,
ty.pointer.as_ref().into(),
properties,
),
utf8: (&str).into(),
encoding: ir::StringEncoding::Utf8,
Expand Down Expand Up @@ -188,11 +194,11 @@ impl XLangPlugin for CFrontend {
};

let into_xir_type = |ty: &Type, pointer: Option<&Pointer>| {
into_xir_type_real(&char_type, &int_type, ty, pointer)
into_xir_type_real(&char_type, &int_type, ty, pointer, todo!())
};

let codegen_expr = |expr: &Expression, block: &mut Vec<ir::BlockItem>| {
codegen_expr_real(&char_type, &int_type, expr, block);
codegen_expr_real(&char_type, &int_type, expr, block, todo!());
};

for decl in &self.parsed {
Expand Down
8 changes: 4 additions & 4 deletions codegen-x86/src/callconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use xlang::{
};
use xlang_backend::{callconv::CallingConvention, ty::TypeInformation};
use xlang_struct::{
Abi, AggregateDefinition, FnType, ScalarType, ScalarTypeHeader, ScalarTypeKind, Type,
AggregateDefinition, FnType, ScalarType, ScalarTypeHeader, ScalarTypeKind, Type,
};

use crate::ValLocation;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub trait X86CallConv {
fn find_parameter(&self, off: u32, ty: &FnType, infn: bool) -> ValLocation;
fn find_return_val(&self, ty: &Type) -> Option<ValLocation>;
fn pass_return_place(&self, ty: &Type, frame_size: usize) -> Option<ValLocation>;
fn with_tag(&self, tag: Abi) -> Option<Box<dyn X86CallConv>>;
fn with_tag(&self, tag: &str) -> Option<Box<dyn X86CallConv>>;
fn callee_saved(&self) -> &[X86Register];
}

Expand Down Expand Up @@ -246,7 +246,7 @@ impl<S: std::hash::BuildHasher + Clone + 'static> X86CallConv for SysV64CC<S> {
None // For now
}

fn with_tag(&self, _: Abi) -> Option<Box<dyn X86CallConv>> {
fn with_tag(&self, _: &str) -> Option<Box<dyn X86CallConv>> {
Some(Box::new((*self).clone()))
}

Expand Down Expand Up @@ -281,7 +281,7 @@ impl<'a> CallingConvention for dyn X86CallConv + 'a {

#[allow(clippy::module_name_repetitions)]
pub fn get_callconv<S: std::hash::BuildHasher + Clone + 'static>(
_tag: Abi,
_tag: &str,
target: &'static TargetProperties<'static>,
features: HashSet<X86Feature, S>,
tys: Rc<TypeInformation>,
Expand Down
9 changes: 2 additions & 7 deletions codegen-x86/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ impl XLangPlugin for X86CodegenPlugin {
name: name.clone(),
strings: self.strings.clone(),
callconv: callconv::get_callconv(
ty.tag,
&ty.tag,
self.properties.unwrap(),
features.clone(),
tys.clone(),
Expand Down Expand Up @@ -1389,10 +1389,5 @@ impl XLangCodegen for X86CodegenPlugin {
xlang::host::rustcall! {
#[no_mangle]
pub extern "rustcall" fn xlang_backend_main() -> DynBox<dyn XLangCodegen> {
DynBox::unsize_box(Box::new(X86CodegenPlugin {
fns: Some(std::collections::HashMap::new()),
strings: Rc::new(RefCell::new(StringMap::new())),
properties: None,
features: HashSet::new()
}))
DynBox::unsize_box(Box::new(xlang_backend::mc::MCBackend::new(mc::new_writer())))
}}
Loading

0 comments on commit f5e4d41

Please sign in to comment.