aboutsummaryrefslogtreecommitdiff
path: root/patio-to-cranelift/src/lib.rs
blob: 2c7e8d8f644a5e7070a0217b399adcbdb02b1c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::{io::Cursor, path::Path, str::FromStr};

use crate::errors::CompileError;

mod codegen;
mod errors;
mod patio;

mod sources {
    include!("./gen/sources.rs");

    #[allow(unused)]
    pub(crate) mod transient_0 {
        include!("./gen/sources/transient_0.rs");
    }
}

mod sourcetext {
    include!("./gen/sourcetext.rs");

    #[derive(Clone, Debug, Hash, PartialEq)]
    pub struct SourceText {
        name: String,
        contents: String,
    }

    #[allow(unused)]
    pub(crate) mod transient_0 {
        use std::{any::Any, collections::HashMap, sync::Arc};

        use crate::sourcetext::SourceText;

        include!("./gen/sourcetext/transient_0.rs");

        fn encode_source_text(
            value: &SourceText,
            writer: &mut impl std::io::Write,
            shared_values: &mut HashMap<usize, i64>,
        ) -> std::io::Result<()> {
            todo!();
        }

        fn decode_source_text(
            reader: &mut impl std::io::Read,
            shared_values: &mut Vec<Arc<dyn Any + Sync + Send>>,
        ) -> std::io::Result<SourceText> {
            let name = crate::transient_0::decode_string(reader, shared_values)?;
            let contents = crate::transient_0::decode_string(reader, shared_values)?;
            Ok(SourceText { name, contents })
        }
    }
}

#[allow(unused)]
mod transient_0 {
    include!("./gen/transient_0.rs");
}

pub fn compile(
    source_path: &Path,
    target_str: &str,
    main: Option<&str>,
) -> Result<(), CompileError> {
    let source_bytes = std::fs::read(source_path).map_err(CompileError::ReadSourceBytes)?;

    let compilation_unit = patio::transient_0::decode_patio_compilation_unit(
        &mut Cursor::new(source_bytes),
        &mut Vec::new(),
    )
    .map_err(CompileError::InvalidPatioSource)?;

    let target_triple =
        target_lexicon::Triple::from_str(target_str).map_err(CompileError::ParseTargetTriple)?;

    let object_bytes =
        codegen::compile_compilation_unit(compilation_unit, main, source_path, target_triple)?;

    let destination_path = source_path.with_extension("o");
    std::fs::write(destination_path, object_bytes).map_err(CompileError::WriteObjectFile)?;

    Ok(())
}