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
use super::ast;
use super::types;
use super::Type;
use ast::PrimitiveType;
use ast::TypeSignature;

pub fn type_to_cranelift<T: cranelift_module::Backend>(
    type_: &Option<TypeSignature>,
    module: &super::Module<T>,
) -> Type {
    if let Some(t) = type_ {
        match t {
            TypeSignature::Primitive(p) => match p {
                PrimitiveType::Nil => types::I32,
                PrimitiveType::Bool => types::B1,
                PrimitiveType::I8 => types::I8,
                PrimitiveType::I16 => types::I16,
                PrimitiveType::I32 => types::I32,
                PrimitiveType::I64 => types::I64,
                PrimitiveType::U8 => types::I8,
                PrimitiveType::U16 => types::I16,
                PrimitiveType::U32 => types::I32,
                PrimitiveType::U64 => types::I64,
                PrimitiveType::F32 => types::F32,
                PrimitiveType::F64 => types::F64,
            },
            TypeSignature::Function(_sig) => module.target_config().pointer_type(),
            TypeSignature::Custom(name) => match &**name {
                "String" => module.target_config().pointer_type(),
                _ => types::I32,
            },
        }
    } else {
        type_to_cranelift(&Some(TypeSignature::Primitive(PrimitiveType::Nil)), module)
    }
}

pub fn type_ref_to_cranelift<T: cranelift_module::Backend>(
    type_: &TypeSignature,
    module: &super::Module<T>,
) -> Type {
    match type_ {
        TypeSignature::Primitive(p) => match p {
            PrimitiveType::Nil => types::I32,
            PrimitiveType::Bool => types::B1,
            PrimitiveType::I8 => types::I8,
            PrimitiveType::I16 => types::I16,
            PrimitiveType::I32 => types::I32,
            PrimitiveType::I64 => types::I64,
            PrimitiveType::U8 => types::I8,
            PrimitiveType::U16 => types::I16,
            PrimitiveType::U32 => types::I32,
            PrimitiveType::U64 => types::I64,
            PrimitiveType::F32 => types::F32,
            PrimitiveType::F64 => types::F64,
        },
        TypeSignature::Function(_sig) => module.target_config().pointer_type(),
        TypeSignature::Custom(name) => match &**name {
            "String" => module.target_config().pointer_type(),
            _ => types::I32,
        },
    }
}