aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-03-30 22:38:45 +0100
committerMichael Williamson <mike@zwobble.org>2026-03-30 22:38:45 +0100
commit066a035b586b3e3fb8be9d608e0ef4ba16461cae (patch)
treef9e60f260625b0d9291d1d36c94e3e592679e1b5
parent20fc2b92d7e04a35fdce0eb7de909710e132c39a (diff)
Run cargo fmt
-rw-r--r--patio-to-cranelift/src/context.rs44
-rw-r--r--patio-to-cranelift/src/errors.rs2
-rw-r--r--patio-to-cranelift/src/lib.rs131
-rw-r--r--patio-to-cranelift/src/main.rs2
4 files changed, 110 insertions, 69 deletions
diff --git a/patio-to-cranelift/src/context.rs b/patio-to-cranelift/src/context.rs
index 38aedf60..ae377c1f 100644
--- a/patio-to-cranelift/src/context.rs
+++ b/patio-to-cranelift/src/context.rs
@@ -22,24 +22,28 @@ impl CompilerContext {
let object_builder = cranelift_object::ObjectBuilder::new(
isa,
source_path.as_os_str().as_encoded_bytes(),
- cranelift_module::default_libcall_names()
- ).map_err(CompileError::ObjectBuilderNewFailed)?;
+ cranelift_module::default_libcall_names(),
+ )
+ .map_err(CompileError::ObjectBuilderNewFailed)?;
let module = cranelift_object::ObjectModule::new(object_builder);
Ok(Self::new(module, target_triple))
}
- fn isa(target: target_lexicon::Triple) -> Result<std::sync::Arc<dyn cranelift::prelude::isa::TargetIsa>, CompileError> {
+ fn isa(
+ target: target_lexicon::Triple,
+ ) -> Result<std::sync::Arc<dyn cranelift::prelude::isa::TargetIsa>, CompileError> {
let mut flags_builder = cranelift::prelude::settings::builder();
- flags_builder.enable("is_pic").map_err(CompileError::EnablePicFailed)?;
+ flags_builder
+ .enable("is_pic")
+ .map_err(CompileError::EnablePicFailed)?;
cranelift::prelude::isa::lookup(target)
.map_err(CompileError::LookupIsa)?
.finish(cranelift::prelude::settings::Flags::new(flags_builder))
.map_err(CompileError::BuildIsa)
}
-
fn new(module: cranelift_object::ObjectModule, target_triple: target_lexicon::Triple) -> Self {
CompilerContext {
codegen_context: module.make_context(),
@@ -53,8 +57,15 @@ impl CompilerContext {
self.module.finish()
}
- pub(crate) fn declare_function(&mut self, name: &str, signature: &cranelift::prelude::Signature) {
- let func_id = self.module.declare_function(name, cranelift_module::Linkage::Import, signature).unwrap();
+ pub(crate) fn declare_function(
+ &mut self,
+ name: &str,
+ signature: &cranelift::prelude::Signature,
+ ) {
+ let func_id = self
+ .module
+ .declare_function(name, cranelift_module::Linkage::Import, signature)
+ .unwrap();
self.func_name_to_func_id.insert(name.to_string(), func_id);
}
@@ -68,12 +79,18 @@ impl CompilerContext {
// TODO: clear context at start or end of function?
self.codegen_context.clear();
- let func_id = self.module.declare_function(name, linkage, &signature).unwrap();
+ let func_id = self
+ .module
+ .declare_function(name, linkage, &signature)
+ .unwrap();
self.func_name_to_func_id.insert(name.to_string(), func_id);
// TODO: reuse function builder context
let mut func_builder_context = cranelift::prelude::FunctionBuilderContext::new();
- let mut function_builder = cranelift::prelude::FunctionBuilder::new(&mut self.codegen_context.func, &mut func_builder_context);
+ let mut function_builder = cranelift::prelude::FunctionBuilder::new(
+ &mut self.codegen_context.func,
+ &mut func_builder_context,
+ );
function_builder.func.signature = signature;
let entry_block = function_builder.create_block();
@@ -86,7 +103,9 @@ impl CompilerContext {
module: &mut self.module,
});
- self.module.define_function(func_id, &mut self.codegen_context).unwrap();
+ self.module
+ .define_function(func_id, &mut self.codegen_context)
+ .unwrap();
}
}
@@ -96,9 +115,10 @@ pub(crate) struct CompilerFunctionContext<'a> {
module: &'a mut cranelift_object::ObjectModule,
}
-impl <'a> CompilerFunctionContext<'a> {
+impl<'a> CompilerFunctionContext<'a> {
pub(crate) fn func_id(&mut self, func_name: &str) -> cranelift_codegen::ir::FuncRef {
let func_id = self.func_name_to_func_id[func_name];
- self.module.declare_func_in_func(func_id, self.function_builder.func)
+ self.module
+ .declare_func_in_func(func_id, self.function_builder.func)
}
}
diff --git a/patio-to-cranelift/src/errors.rs b/patio-to-cranelift/src/errors.rs
index 3a71da71..09e9a2a4 100644
--- a/patio-to-cranelift/src/errors.rs
+++ b/patio-to-cranelift/src/errors.rs
@@ -1,5 +1,3 @@
-
-
#[derive(Debug)]
pub enum CompileError {
/// Reading the bytes of the patio source file failed.
diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs
index 6ceb4968..cfb2467f 100644
--- a/patio-to-cranelift/src/lib.rs
+++ b/patio-to-cranelift/src/lib.rs
@@ -1,8 +1,17 @@
-use std::{path::{Path}, str::FromStr};
+use std::{path::Path, str::FromStr};
use cranelift::prelude::InstBuilder;
-use crate::{context::{CompilerContext, CompilerFunctionContext}, errors::CompileError, patio_generated::patio::{PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioInstruction, PatioInstructionValue, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility}};
+use crate::{
+ context::{CompilerContext, CompilerFunctionContext},
+ errors::CompileError,
+ patio_generated::patio::{
+ PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue,
+ PatioFunctionDefinition, PatioInstruction, PatioInstructionValue, PatioIntValue,
+ PatioIntValueValue, PatioLocal, PatioLocalType, PatioOperand, PatioOperandValue,
+ PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility,
+ },
+};
#[allow(dead_code, unused_imports)]
#[path = "./gen/patio_generated.rs"]
@@ -21,14 +30,18 @@ pub fn compile(source_path: &Path, target_str: &str) -> Result<(), CompileError>
let compilation_unit = patio_generated::patio::root_as_patio_compilation_unit(&source_bytes)
.map_err(CompileError::InvalidFlatbuffer)?;
- let target_triple = target_lexicon::Triple::from_str(target_str)
- .map_err(CompileError::ParseTargetTriple)?;
+ let target_triple =
+ target_lexicon::Triple::from_str(target_str).map_err(CompileError::ParseTargetTriple)?;
let mut context = CompilerContext::for_object(source_path, target_triple)?;
for element in compilation_unit.elements() {
- if element.value_compilation_unit_element_value_type() == PatioCompilationUnitElementValue::PatioFunctionDeclaration {
- let function_declaration = element.value_compilation_unit_element_value_as_patio_function_declaration().unwrap();
+ if element.value_compilation_unit_element_value_type()
+ == PatioCompilationUnitElementValue::PatioFunctionDeclaration
+ {
+ let function_declaration = element
+ .value_compilation_unit_element_value_as_patio_function_declaration()
+ .unwrap();
compile_function_declaration(function_declaration, &mut context);
}
}
@@ -73,7 +86,7 @@ fn compile_compilation_unit_element(
fn compile_function_declaration(
function_declaration: patio_generated::patio::PatioFunctionDeclaration<'_>,
- context: &mut CompilerContext
+ context: &mut CompilerContext,
) {
let cranelift_signature = cranelift_signature(
function_declaration.params().iter(),
@@ -81,10 +94,7 @@ fn compile_function_declaration(
context,
);
- context.declare_function(
- function_declaration.name(),
- &cranelift_signature,
- );
+ context.declare_function(function_declaration.name(), &cranelift_signature);
}
fn compile_function_definition(
@@ -107,7 +117,7 @@ fn compile_function_definition(
for instruction in function_definition.body() {
compile_instruction(instruction, function_context)
}
- }
+ },
);
}
@@ -122,35 +132,40 @@ fn compile_instruction(
}
PatioInstructionValue::PatioReturn => {
- let return_ = instruction.value_instruction_value_as_patio_return().unwrap();
+ let return_ = instruction
+ .value_instruction_value_as_patio_return()
+ .unwrap();
compile_return(return_, context);
}
PatioInstructionValue::PatioReturnVoid => {
- let return_void = instruction.value_instruction_value_as_patio_return_void().unwrap();
+ let return_void = instruction
+ .value_instruction_value_as_patio_return_void()
+ .unwrap();
compile_return_void(return_void, context);
}
instruction_type => {
- todo!("Unhandled instruction type: {:?} {}", instruction_type.variant_name(), instruction_type.0)
+ todo!(
+ "Unhandled instruction type: {:?} {}",
+ instruction_type.variant_name(),
+ instruction_type.0
+ )
}
}
}
-fn compile_call(
- call: PatioCall<'_>,
- context: &mut CompilerFunctionContext<'_>,
-) {
+fn compile_call(call: PatioCall<'_>, context: &mut CompilerFunctionContext<'_>) {
let cranelift_args = compile_operands(call.args().iter(), context);
let func_ref = context.func_id(call.function_name());
- context.function_builder.ins().call(func_ref, &cranelift_args);
+ context
+ .function_builder
+ .ins()
+ .call(func_ref, &cranelift_args);
}
-fn compile_return(
- return_: PatioReturn<'_>,
- context: &mut CompilerFunctionContext<'_>,
-) {
+fn compile_return(return_: PatioReturn<'_>, context: &mut CompilerFunctionContext<'_>) {
let cranelift_value = compile_operand(return_.value(), context);
context.function_builder.ins().return_(&[cranelift_value]);
@@ -192,7 +207,11 @@ fn compile_operand(
}
operand_type => {
- todo!("Unhandled operand type: {:?} {}", operand_type.variant_name(), operand_type.0)
+ todo!(
+ "Unhandled operand type: {:?} {}",
+ operand_type.variant_name(),
+ operand_type.0
+ )
}
}
}
@@ -204,13 +223,22 @@ fn compile_int_value(
) -> cranelift::prelude::Value {
match int_value.value_int_value_type() {
PatioIntValueValue::PatioIntValueLiteral => {
- let literal = int_value.value_int_value_as_patio_int_value_literal().unwrap();
+ let literal = int_value
+ .value_int_value_as_patio_int_value_literal()
+ .unwrap();
- context.function_builder.ins().iconst(cranelift_type, i64::from(literal.value()))
+ context
+ .function_builder
+ .ins()
+ .iconst(cranelift_type, i64::from(literal.value()))
}
int_value_type => {
- todo!("Unhandled int value type: {:?} {}", int_value_type.variant_name(), int_value_type.0)
+ todo!(
+ "Unhandled int value type: {:?} {}",
+ int_value_type.variant_name(),
+ int_value_type.0
+ )
}
}
}
@@ -220,7 +248,8 @@ fn cranelift_signature<'a>(
return_type: PatioType,
context: &CompilerContext,
) -> cranelift::prelude::Signature {
- let mut signature = cranelift::prelude::Signature::new(cranelift_codegen::isa::CallConv::SystemV);
+ let mut signature =
+ cranelift::prelude::Signature::new(cranelift_codegen::isa::CallConv::SystemV);
for local in locals {
if local.local_type() == PatioLocalType::PARAM {
@@ -241,14 +270,11 @@ fn cranelift_signature<'a>(
fn patio_visibility_to_cranelift_linkage(visibility: PatioVisibility) -> cranelift_module::Linkage {
match visibility {
- PatioVisibility::PRIVATE =>
- cranelift_module::Linkage::Local,
+ PatioVisibility::PRIVATE => cranelift_module::Linkage::Local,
- PatioVisibility::PUBLIC =>
- cranelift_module::Linkage::Export,
+ PatioVisibility::PUBLIC => cranelift_module::Linkage::Export,
- _ =>
- panic!("Unsupported visibility: {}", visibility.0)
+ _ => panic!("Unsupported visibility: {}", visibility.0),
}
}
@@ -257,31 +283,30 @@ fn patio_type_to_cranelift_type(
context: &CompilerContext,
) -> cranelift::prelude::Type {
match patio_type.value_patio_type_type() {
- PatioTypeValue::PatioBoolType =>
- cranelift::prelude::Type::int(1).unwrap(),
+ PatioTypeValue::PatioBoolType => cranelift::prelude::Type::int(1).unwrap(),
- PatioTypeValue::PatioByteType =>
- cranelift::prelude::Type::int(8).unwrap(),
+ PatioTypeValue::PatioByteType => cranelift::prelude::Type::int(8).unwrap(),
- PatioTypeValue::PatioDataPtrType =>
- cranelift::prelude::Type::triple_pointer_type(&context.target_triple),
+ PatioTypeValue::PatioDataPtrType => {
+ cranelift::prelude::Type::triple_pointer_type(&context.target_triple)
+ }
- PatioTypeValue::PatioFunctionPtrType =>
- cranelift::prelude::Type::triple_pointer_type(&context.target_triple),
+ PatioTypeValue::PatioFunctionPtrType => {
+ cranelift::prelude::Type::triple_pointer_type(&context.target_triple)
+ }
PatioTypeValue::PatioIntType =>
- // TODO: this assumes that pointers are at least as wide as i32s:
- // probably safe, but we should check.
- // TODO: this relies on cranelift pointer types just being integers.
- cranelift::prelude::Type::triple_pointer_type(&context.target_triple),
+ // TODO: this assumes that pointers are at least as wide as i32s:
+ // probably safe, but we should check.
+ // TODO: this relies on cranelift pointer types just being integers.
+ {
+ cranelift::prelude::Type::triple_pointer_type(&context.target_triple)
+ }
- PatioTypeValue::PatioInt32Type =>
- cranelift::prelude::Type::int(32).unwrap(),
+ PatioTypeValue::PatioInt32Type => cranelift::prelude::Type::int(32).unwrap(),
- PatioTypeValue::PatioVoidType =>
- todo!(),
+ PatioTypeValue::PatioVoidType => todo!(),
- _ =>
- todo!(),
+ _ => todo!(),
}
}
diff --git a/patio-to-cranelift/src/main.rs b/patio-to-cranelift/src/main.rs
index 6bb533a1..77409d1f 100644
--- a/patio-to-cranelift/src/main.rs
+++ b/patio-to-cranelift/src/main.rs
@@ -20,5 +20,3 @@ fn main() {
patio_to_cranelift::compile(&source_path, &args.target).unwrap();
}
}
-
-