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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use arret_syntax::span::Span;

use arret_runtime::abitype;
use arret_runtime::boxed;
use arret_runtime::boxed::prelude::*;

use crate::mir::builder::{Builder, BuiltReg};
use crate::mir::eval_hir::EvalHirCtx;
use crate::mir::ops::*;
use crate::mir::tagset::TypeTagSet;
use crate::mir::value::build_reg::value_to_reg;
use crate::mir::value::from_reg::reg_to_value;
use crate::mir::value::types::TypeHint;
use crate::mir::value::Value;
use crate::ty;
use crate::ty::record;
use crate::ty::Ty;

/// Returns a set of type tags that would satisfy the type predicate
fn type_tags_for_test_ty(test_ty: &ty::pred::TestTy) -> TypeTagSet {
    use crate::ty::pred::TestTy;

    match test_ty {
        TestTy::Str => boxed::TypeTag::Str.into(),
        TestTy::Sym => boxed::TypeTag::Sym.into(),
        TestTy::Int => boxed::TypeTag::Int.into(),
        TestTy::Float => boxed::TypeTag::Float.into(),
        TestTy::Char => boxed::TypeTag::Char.into(),
        TestTy::Nil => boxed::TypeTag::Nil.into(),
        TestTy::Fun => boxed::TypeTag::FunThunk.into(),
        TestTy::Bool => [boxed::TypeTag::True, boxed::TypeTag::False]
            .iter()
            .collect(),
        TestTy::Num => [boxed::TypeTag::Int, boxed::TypeTag::Float]
            .iter()
            .collect(),
        TestTy::List => [boxed::TypeTag::Pair, boxed::TypeTag::Nil].iter().collect(),
        TestTy::Vector => boxed::TypeTag::Vector.into(),
        TestTy::Set => boxed::TypeTag::Set.into(),
        TestTy::Map => boxed::TypeTag::Map.into(),
        TestTy::TopRecord => boxed::TypeTag::Record.into(),
        TestTy::RecordClass(_) => {
            todo!("record classes");
        }
    }
}

fn build_load_type_tag(
    ehx: &mut EvalHirCtx,
    b: &mut Builder,
    span: Span,
    value: &Value,
    possible_type_tags: TypeTagSet,
) -> BuiltReg {
    let subject_reg = value_to_reg(ehx, b, span, value, &abitype::BoxedAbiType::Any.into()).into();

    b.push_reg(
        span,
        OpKind::LoadBoxedTypeTag,
        LoadBoxedTypeTagOp {
            subject_reg,
            possible_type_tags,
        },
    )
}

fn build_is_type_tag(
    b: &mut Builder,
    span: Span,
    subject_type_tag_reg: BuiltReg,
    test_tag: boxed::TypeTag,
) -> BuiltReg {
    let test_tag_reg = b.push_reg(span, OpKind::ConstTypeTag, test_tag);

    b.push_reg(
        span,
        OpKind::TypeTagEqual,
        BinaryOp {
            lhs_reg: subject_type_tag_reg.into(),
            rhs_reg: test_tag_reg.into(),
        },
    )
}

fn eval_tagged_ty_pred(
    ehx: &mut EvalHirCtx,
    b: &mut Option<Builder>,
    span: Span,
    subject_value: &Value,
    qualifying_type_tags: TypeTagSet,
) -> Value {
    use crate::mir::value::types::possible_type_tags_for_value;
    let possible_type_tags = possible_type_tags_for_value(subject_value);

    if possible_type_tags.is_subset(qualifying_type_tags) {
        // Statically true
        return boxed::TRUE_INSTANCE.as_any_ref().into();
    } else if qualifying_type_tags.is_disjoint(possible_type_tags) {
        // Statically false
        return boxed::FALSE_INSTANCE.as_any_ref().into();
    }

    let b = if let Some(some_b) = b {
        some_b
    } else {
        panic!(
            "runtime tagged type predicate without builder: {:?} is in type tag set {:?}",
            subject_value, qualifying_type_tags
        );
    };

    let subject_type_tag_reg = build_load_type_tag(ehx, b, span, subject_value, possible_type_tags);
    let result_reg = (qualifying_type_tags & possible_type_tags)
        .into_iter()
        .fold(None, |tail_result_reg: Option<BuiltReg>, test_tag| {
            let is_test_tag = build_is_type_tag(b, span, subject_type_tag_reg, test_tag);

            if let Some(tail_result_reg) = tail_result_reg {
                // Logically or this with our tail result
                let or_result_reg = b.alloc_local();

                let cond_op_kind = OpKind::Cond(CondOp {
                    reg_phi: Some(RegPhi {
                        output_reg: or_result_reg.into(),
                        true_result_reg: is_test_tag.into(),
                        false_result_reg: tail_result_reg.into(),
                    }),
                    test_reg: is_test_tag.into(),
                    true_ops: Box::new([]),
                    false_ops: Box::new([]),
                });
                b.push(span, cond_op_kind);

                Some(or_result_reg)
            } else {
                // We are the first result
                Some(is_test_tag)
            }
        })
        .unwrap();

    reg_to_value(
        ehx,
        result_reg,
        &abitype::AbiType::Bool,
        &Ty::<ty::Mono>::Bool.into(),
    )
}

fn eval_record_ty_pred(
    ehx: &mut EvalHirCtx,
    b: &mut Option<Builder>,
    span: Span,
    subject_value: &Value,
    test_cons: &record::ConsId,
) -> Value {
    use crate::mir::value::types::{possible_type_tags_for_value, type_hint_for_value};

    let possible_type_tags = possible_type_tags_for_value(subject_value);

    if !possible_type_tags.contains(boxed::TypeTag::Record) {
        // Cannot be a record
        return boxed::FALSE_INSTANCE.as_any_ref().into();
    }

    let is_definite_record = possible_type_tags == boxed::TypeTag::Record.into();
    let definite_matching_cons =
        if let TypeHint::KnownRecordCons(subject_cons) = type_hint_for_value(ehx, subject_value) {
            let known_matching_cons = test_cons == &subject_cons;
            if !known_matching_cons {
                // This cannot possibly match regardless of if it's record
                return boxed::FALSE_INSTANCE.as_any_ref().into();
            }

            known_matching_cons
        } else {
            false
        };

    if is_definite_record && definite_matching_cons {
        // This is a record with a matching cons
        return boxed::TRUE_INSTANCE.as_any_ref().into();
    }

    let b = if let Some(some_b) = b {
        some_b
    } else {
        panic!("runtime record type predicate without builder");
    };

    let is_record_reg = if is_definite_record {
        None
    } else {
        // If this isn't guaranteed to be a record we need to test its type tag first
        let subject_type_tag_reg =
            build_load_type_tag(ehx, b, span, subject_value, possible_type_tags);

        Some(build_is_type_tag(
            b,
            span,
            subject_type_tag_reg,
            boxed::TypeTag::Record,
        ))
    };

    let is_record_class_b_and_reg = if definite_matching_cons {
        None
    } else {
        // Create a builder for testing the record class
        let mut is_record_class_b = Builder::new();

        let record_reg = value_to_reg(
            ehx,
            &mut is_record_class_b,
            span,
            subject_value,
            &abitype::BoxedAbiType::UniqueTagged(boxed::TypeTag::Record).into(),
        )
        .into();

        // Load our subject record class ID
        let subject_record_class_id_reg =
            is_record_class_b.push_reg(span, OpKind::LoadBoxedRecordClassId, record_reg);

        // Create our test record class ID
        let test_record_class_id_reg = is_record_class_b.push_reg(
            span,
            OpKind::ConstRecordClassId,
            ehx.evaled_record_class_for_cons(test_cons)
                .record_struct
                .clone(),
        );

        // Compare them for equality
        let is_record_class_reg = is_record_class_b.push_reg(
            span,
            OpKind::RecordClassIdEqual,
            BinaryOp {
                lhs_reg: subject_record_class_id_reg.into(),
                rhs_reg: test_record_class_id_reg.into(),
            },
        );

        Some((is_record_class_b, is_record_class_reg))
    };

    let result_reg = match (is_record_reg, is_record_class_b_and_reg) {
        (Some(is_record_reg), Some((is_record_class_b, is_record_class_reg))) => {
            // Need to merge the type tag test with the record class test
            let and_result_reg = b.alloc_local();

            let cond_op_kind = OpKind::Cond(CondOp {
                reg_phi: Some(RegPhi {
                    output_reg: and_result_reg.into(),
                    true_result_reg: is_record_class_reg.into(),
                    false_result_reg: is_record_reg.into(),
                }),
                test_reg: is_record_reg.into(),
                true_ops: is_record_class_b.into_ops(),
                false_ops: Box::new([]),
            });

            b.push(span, cond_op_kind);
            and_result_reg
        }
        (Some(is_record_reg), None) => is_record_reg,
        (None, Some((is_record_class_b, is_record_class_reg))) => {
            b.append(is_record_class_b.into_ops().into_vec());
            is_record_class_reg
        }
        (None, None) => {
            // This is unreachable but has a sane answer anyway
            return boxed::TRUE_INSTANCE.as_any_ref().into();
        }
    };

    reg_to_value(
        ehx,
        result_reg,
        &abitype::AbiType::Bool,
        &Ty::<ty::Mono>::Bool.into(),
    )
}

pub fn eval_ty_pred(
    ehx: &mut EvalHirCtx,
    b: &mut Option<Builder>,
    span: Span,
    subject_value: &Value,
    test_ty: &ty::pred::TestTy,
) -> Value {
    match test_ty {
        ty::pred::TestTy::RecordClass(record_cons) => {
            eval_record_ty_pred(ehx, b, span, subject_value, record_cons)
        }
        tagged_ty => {
            let qualifying_type_tags = type_tags_for_test_ty(tagged_ty);
            eval_tagged_ty_pred(ehx, b, span, subject_value, qualifying_type_tags)
        }
    }
}