rev: eeadb6082f44956592d11d79c659cb265ac9d9d3 scopes/src/quote.cpp -rw-r--r-- 26.9 KiB View raw Log this file
eeadb6082f44 — Leonard Ritter * SPIR-V: fixed unstructured branch validation error for `if/elseif/else` construct 9 days ago
                                                                                
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "quote.hpp"
#include "value.hpp"
#include "types.hpp"
#include "error.hpp"
#include "prover.hpp"
//#include "closure.hpp"
#include "stream_expr.hpp"
//#include "hash.hpp"
//#include "timer.hpp"
//#include "gc.hpp"
//#include "builtin.hpp"
//#include "verify_tools.inc"
#include "dyn_cast.inc"
//#include "compiler_flags.hpp"
//#include "gen_llvm.hpp"
//#include "list.hpp"
//#include "expander.hpp"
#include "globals.hpp"
#include "anchor.hpp"
#include "scopes/scopes.h"



//#pragma GCC diagnostic ignored "-Wvla-extension"

namespace scopes {

#define REF(X) ref(_anchor, (X))

#define LOCALREF(X) \
    ref(Anchor::from(Symbol(String::from(__FILE__)), __LINE__, 1), (X))

static ValueRef canonicalize(const ExpressionRef &expr) {
    if (expr->body.empty())
        return expr->value;
    return expr;
}

ValueRef build_quoted_argument_list(const Anchor *_anchor, const Values &values) {
    auto result = REF(Expression::unscoped_from());
    auto numvals = (int)values.size();
    auto numelems = REF(ConstInt::from(TYPE_I32, numvals));
    auto buf = REF(CallTemplate::from(g_alloca_array, {
            REF(ConstPointer::type_from(TYPE_ValueRef)),
            numelems
        }));
    result->append(buf);
    for (int i = 0; i < numvals; ++i) {
        auto idx = REF(ConstInt::from(TYPE_I32, i));
        result->append(
            REF(CallTemplate::from(g_store, {
                REF(CallTemplate::from(g_sc_identity, { values[i] })),
                REF(CallTemplate::from(g_getelementptr, { buf, idx }))
            })));
    }
    result->append(REF(CallTemplate::from(g_sc_argument_list_new,
        { numelems, buf })));
    return canonicalize(result);
}

//------------------------------------------------------------------------------

struct Quoter {
    Quoter(const ASTContext  &_ctx) :
        ctx(_ctx) {}

    SCOPES_RESULT(ValueRef) quote_Expression(int level, const ExpressionRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_expression_new, {}));
        auto expr = REF(Expression::unscoped_from());
        if (node->scoped) {
            expr->append(REF(CallTemplate::from(g_sc_expression_set_scoped, { value })));
        }
        for (auto &&instr : node->body) {
            expr->append(REF(CallTemplate::from(g_sc_expression_append,
                { value, SCOPES_GET_RESULT(quote(level, instr)) })));
        }
        expr->append(REF(CallTemplate::from(g_sc_expression_append,
            { value, SCOPES_GET_RESULT(quote(level, node->value)) })));
        expr->append(value);
        return canonicalize(expr);
    }

    SCOPES_RESULT(ValueRef) quote_ArgumentListTemplate(int level, const ArgumentListTemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        /*if (node->values.size() == 1) {
            return quote(level, node->values[0]);
        } else*/ {
            Values newvalues;
            auto &&values = node->values();
            int count = (int)values.size();
            newvalues.reserve(count);
            for (int i = 0; i < count; ++i) {
                newvalues.push_back(SCOPES_GET_RESULT(quote(level, values[i])));
            }
            auto _anchor = node.anchor();
            return build_quoted_argument_list(_anchor, newvalues);
        }
    }

    SCOPES_RESULT(CallTemplateRef) quote_ExtractArgumentTemplate(int level, const ExtractArgumentTemplateRef &node) {
        SCOPES_RESULT_TYPE(CallTemplateRef);
        auto _anchor = node.anchor();
        if (node->vararg) {
            return REF(CallTemplate::from(g_sc_extract_argument_list_new, {
                    SCOPES_GET_RESULT(quote(level, node->value)),
                    REF(ConstInt::from(TYPE_I32, node->index)) }));
        } else {
            return REF(CallTemplate::from(g_sc_extract_argument_new, {
                    SCOPES_GET_RESULT(quote(level, node->value)),
                    REF(ConstInt::from(TYPE_I32, node->index)) }));
        }
    }

    SCOPES_RESULT(TypedValueRef) quote_param(const ParameterTemplateRef &node) {
        SCOPES_RESULT_TYPE(TypedValueRef);
        auto _anchor = node.anchor();
        auto newparam = REF(CallTemplate::from(g_sc_parameter_new, {
            REF(ConstInt::symbol_from(node->name)) }));
        auto typednewparam = SCOPES_GET_RESULT(prove(ctx, newparam));
        bind(node, typednewparam);
        ctx.frame->bind(node, typednewparam);
        return typednewparam;
    }

    SCOPES_RESULT(ValueRef) quote_Loop(int level, const LoopRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_loop_new, {
            SCOPES_GET_RESULT(quote(level, node->init))
        }));
        auto args = REF(CallTemplate::from(g_sc_loop_arguments, { value }));
        auto typedargs = SCOPES_GET_RESULT(prove(ctx, args));
        ctx.frame->bind(args, typedargs);
        bind(node->args, typedargs);
        auto expr = REF(Expression::unscoped_from());
        expr->append(value);
        expr->append(args);
        expr->append(REF(CallTemplate::from(g_sc_loop_set_body, { value,
            SCOPES_GET_RESULT(quote(level, node->value)) })));
        expr->append(value);
        return canonicalize(expr);
    }

    SCOPES_RESULT(ValueRef) quote_CompileStage(int level, const CompileStageRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        SCOPES_ERROR(QuoteUnsupportedValueKind, node->kind());
    }

    SCOPES_RESULT(CallTemplateRef) quote_KeyedTemplate(int level, const KeyedTemplateRef &node) {
        SCOPES_RESULT_TYPE(CallTemplateRef);
        auto value = SCOPES_GET_RESULT(quote(level, node->value));
        auto _anchor = node.anchor();
        return REF(CallTemplate::from(g_sc_keyed_new,
            { REF(ConstInt::symbol_from(node->key)), value }));
    }

    SCOPES_RESULT(ValueRef) quote_CallTemplate(int level, const CallTemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_call_new, {
            SCOPES_GET_RESULT(quote(level, node->callee))
        }));
        auto expr = REF(Expression::unscoped_from());
        if (node->is_rawcall()) {
            expr->append(REF(CallTemplate::from(g_sc_call_set_rawcall, { value,
                REF(ConstInt::from(TYPE_Bool, true)) })));
        }
        for (auto &&arg : node->args) {
            expr->append(REF(CallTemplate::from(g_sc_call_append_argument,
                { value, SCOPES_GET_RESULT(quote(level, arg)) })));
        }
        expr->append(value);
        return canonicalize(expr);
    }

    SCOPES_RESULT(TypedValueRef) quote_ParameterTemplate(int level, const ParameterTemplateRef &sym) {
        SCOPES_RESULT_TYPE(TypedValueRef);
        auto value = resolve(sym);
        if (!value) {
            SCOPES_ERROR(QuoteUnboundValue, sym);
        }
        return value;
    }

    SCOPES_RESULT(TypedValueRef) quote_LoopArguments(int level, const LoopArgumentsRef &sym) {
        SCOPES_RESULT_TYPE(TypedValueRef);
        auto value = resolve(sym);
        if (!value) {
            SCOPES_ERROR(QuoteUnboundValue, sym);
        }
        return value;
    }

    SCOPES_RESULT(ValueRef) quote_SwitchTemplate(int level, const SwitchTemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_switch_new, {
            SCOPES_GET_RESULT(quote(level, node->expr))
        }));
        auto expr = REF(Expression::unscoped_from());
        for (auto &&_case : node->cases) {
            //auto _case_anchor = _case.anchor;
            switch(_case.kind) {
            case CK_Case: {
                expr->append(REF(CallTemplate::from(g_sc_switch_append_case, { value,
                    SCOPES_GET_RESULT(quote(level, _case.literal)),
                    SCOPES_GET_RESULT(quote(level, _case.value)) })));
            } break;
            case CK_Pass: {
                expr->append(REF(CallTemplate::from(g_sc_switch_append_pass, { value,
                    SCOPES_GET_RESULT(quote(level, _case.literal)),
                    SCOPES_GET_RESULT(quote(level, _case.value)) })));
            } break;
            case CK_Default: {
                expr->append(REF(CallTemplate::from(g_sc_switch_append_default, { value,
                    SCOPES_GET_RESULT(quote(level, _case.value)) })));
            } break;
            default: assert(false);
            }
        }
        expr->append(value);
        return canonicalize(expr);
    }

    SCOPES_RESULT(ValueRef) quote_CondTemplate(int level, const CondTemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        return ValueRef(REF(CallTemplate::from(g_sc_cond_new, {
            SCOPES_GET_RESULT(quote(level, node->cond)),
            SCOPES_GET_RESULT(quote(level, node->then_value)),
            SCOPES_GET_RESULT(quote(level, node->else_value))
        })));
    }

    SCOPES_RESULT(ValueRef) quote_Template(int level, const TemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_template_new,
            { REF(ConstInt::symbol_from(node->name)) }));
        auto expr = REF(Expression::unscoped_from());
        if (node->is_inline()) {
            expr->append(REF(CallTemplate::from(g_sc_template_set_inline, { value })));
        }
        for (auto &&param : node->params) {
            expr->append(REF(CallTemplate::from(g_sc_template_append_parameter, {
                value, SCOPES_GET_RESULT(quote_param(param))
            })));
        }
        expr->append(REF(CallTemplate::from(g_sc_template_set_body, { value,
            SCOPES_GET_RESULT(quote(level, node->value)) })));
        expr->append(value);
        return canonicalize(expr);
    }

    SCOPES_RESULT(CallTemplateRef) quote_Quote(int level, const QuoteRef &node) {
        SCOPES_RESULT_TYPE(CallTemplateRef);
        auto _anchor = node.anchor();
        return REF(CallTemplate::from(g_sc_quote_new,
            { SCOPES_GET_RESULT(quote(level+1, node->value)) }));
    }

    ValueRef quote_typed_argument_list(const ArgumentListRef &node) {
        /*if (node->values.size() == 1) {
            return quote_typed(node->values[0]);
        } else*/ {
            Values newvalues;
            auto _anchor = node.anchor();
            int count = (int)node->values.size();
            newvalues.reserve(count);
            for (int i = 0; i < count; ++i) {
                newvalues.push_back(quote_typed(node->values[i]));
            }
            return build_quoted_argument_list(_anchor, newvalues);
        }
    }

    ValueRef quote_typed(const TypedValueRef &node) {
        if (node->get_type() == TYPE_ValueRef)
            return node;
        if (node.isa<ArgumentList>()) {
            return quote_typed_argument_list(node.cast<ArgumentList>());
        } else {
            auto result = wrap_value(node->get_type(), node);
            assert(result);
            return result;
        }
    }

    SCOPES_RESULT(ValueRef) quote_Unquote(int level, const UnquoteRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        assert(level >= 0);
        if (!level) {
            auto value = SCOPES_GET_RESULT(prove(ctx, node->value));
            auto T = value->get_type();
            if (is_arguments_type(T)) {
                auto at = cast<ArgumentsType>(T);
                auto _anchor = node.anchor();
                {
                    Values newvalues;
                    int count = (int)at->values.size();
                    newvalues.reserve(count);
                    for (int i = 0; i < count; ++i) {
                        newvalues.push_back(ExtractArgument::from(value, i));
                    }
                    return build_quoted_argument_list(_anchor, newvalues);
                }
            } else {
                return quote_typed(value);
            }
        } else {
            auto _anchor = node.anchor();
            return ValueRef(REF(CallTemplate::from(g_sc_unquote_new,
                { SCOPES_GET_RESULT(quote(level-1, node->value)) })));
        }
    }

    SCOPES_RESULT(CallTemplateRef) quote_MergeTemplate(int level, const MergeTemplateRef &node) {
        SCOPES_RESULT_TYPE(CallTemplateRef);
        auto _anchor = node.anchor();
        return REF(CallTemplate::from(g_sc_merge_new, {
            SCOPES_GET_RESULT(quote(level, node->label)),
            SCOPES_GET_RESULT(quote(level, node->value))
        }));
    }

    SCOPES_RESULT(ValueRef) quote_LabelTemplate(int level, const LabelTemplateRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        auto _anchor = node.anchor();
        auto value = REF(CallTemplate::from(g_sc_label_new, {
            REF(ConstInt::from(TYPE_I32, node->label_kind)),
            REF(ConstInt::symbol_from(node->name))
        }));
        auto typedvalue = SCOPES_GET_RESULT(prove(ctx, value));
        bind(node, typedvalue);
        ctx.frame->bind(node, typedvalue);
        auto expr = REF(Expression::unscoped_from());
        expr->append(REF(CallTemplate::from(g_sc_label_set_body, { typedvalue,
            SCOPES_GET_RESULT(quote(level, node->value)) })));
        expr->append(typedvalue);
        return canonicalize(expr);
    }

    SCOPES_RESULT(ValueRef) quote_new_node(int level, const ValueRef &node) {
        SCOPES_RESULT_TYPE(ValueRef);
        assert(node);
        ValueRef result;
        if (node.isa<TypedValue>()) {
            result = quote_typed(node.cast<TypedValue>());
        } else {
            // we shouldn't set an anchor here because sometimes the parent context
            // is more indicative than the node position
            //SCOPES_CHECK_RESULT(verify_stack());
            switch(node->kind()) {
    #define T(NAME, BNAME, CLASS) \
            case NAME: result = SCOPES_GET_RESULT(quote_ ## CLASS(level, node.cast<CLASS>())); break;
            SCOPES_UNTYPED_VALUE_KIND()
    #undef T
            default: assert(false);
            }
            assert(result);
        }
        return result;
    }

    SCOPES_RESULT(TypedValueRef) quote(int level, const ValueRef &node) {
        SCOPES_RESULT_TYPE(TypedValueRef);
        assert(node);
        assert(ctx.frame);
        {
            // check if node is already typed
            TypedValueRef result = SCOPES_GET_RESULT(ctx.frame->resolve(node, ctx.function));
            if (result) {
                // check if we have an existing wrap for the node
                TypedValueRef wrapped = resolve(result);
                if (!wrapped) {
                    // wrap it anew and type it
                    wrapped = SCOPES_GET_RESULT(prove(ctx, quote_typed(result)));
                    bind(result, wrapped);
                }
                return wrapped;
            }
        }
        // check if we have an existing quote for the node
        TypedValueRef result = resolve(node);
        if (result) return result;
        // node is untyped or unbound yet
        ValueRef untyped_result = SCOPES_GET_RESULT(quote_new_node(level, node));
        if (untyped_result.isa<TypedValue>()) {
            result = untyped_result.cast<TypedValue>();
        } else {
            result = SCOPES_GET_RESULT(prove(ctx, untyped_result));
        }
        bind(node, result);
        if (!node.isa<TypedValue>()) {
            #if 0
            StyledStream ss;
            ss << "binding ";
            stream_ast(ss, node, StreamASTFormat());
            ss << " to ";
            stream_ast(ss, result, StreamASTFormat());
            ss << std::endl;
            #endif
            // ensure that the unquoted context can access the typed result
            ctx.frame->bind(node, result);
        }
        return result;
    }

    void bind(const ValueRef &oldnode, const TypedValueRef &newnode) {
        auto it = map.insert({oldnode.unref(), newnode});
        if (!it.second) {
            it.first->second = newnode;
        }
    }

    TypedValueRef resolve(const ValueRef &node) const {
        auto it = map.find(node.unref());
        if (it == map.end())
            return TypedValueRef();
        return it->second;
    }

    #define T(NAME, BNAME, CLASS) \
        SCOPES_RESULT(ConstAggregateRef) quote_ ## CLASS(int level, const ValueRef &node) { \
            return ConstAggregate::ast_from(node); \
        }
    SCOPES_PURE_VALUE_KIND()
    #undef T

    std::unordered_map<Value *, TypedValueRef> map;
    const ASTContext &ctx;
};

ValueRef unwrap_value(const Type *T, const ValueRef &value) {
    auto _anchor = value.anchor();
    //T = strip_qualifiers(T);
    auto ST = storage_type(T).assert_ok();
    auto kind = ST->kind();
    switch(kind) {
    case TK_Pointer: {
        return REF(CallTemplate::from(g_bitcast, {
                REF(CallTemplate::from(g_sc_const_pointer_extract, { value })),
                REF(ConstPointer::type_from(T))
            }));
    } break;
    case TK_Integer: {
        auto ti = cast<IntegerType>(ST);
        if (ti->width <= 64ull) {
            return REF(CallTemplate::from(g_itrunc, {
                    REF(CallTemplate::from(g_sc_const_int_extract, { value })),
                    REF(ConstPointer::type_from(T))
                }));
        } else {
            // big integer
            auto result = REF(Expression::unscoped_from());
            size_t numwords = (ti->width + 63ull) / 64ull;
            auto mem = REF(CallTemplate::from(g_alloca_array, {
                    REF(ConstPointer::type_from(TYPE_U64)),
                    REF(ConstInt::from(TYPE_I32, numwords)),
                }));
            result->append(mem);
            for (size_t i = 0; i < numwords; ++i) {
                auto word = REF(CallTemplate::from(g_sc_const_int_extract_word, {
                    value,
                    REF(ConstInt::from(TYPE_I32, i)),
                }));
                result->append(word);
                auto ptr = REF(CallTemplate::from(g_getelementptr, { mem,
                    REF(ConstInt::from(TYPE_I32, i)) }));
                result->append(ptr);
                result->append(REF(CallTemplate::from(g_store, { word, ptr })));
            }
            auto castmem =
                REF(CallTemplate::from(g_bitcast, { mem,
                    REF(ConstPointer::type_from((local_ro_pointer_type(T)))) }));
            result->append(castmem);
            result->append(REF(CallTemplate::from(g_load, { castmem })));
            return result;
        }
    } break;
    case TK_Real: {
        return REF(CallTemplate::from(g_fptrunc, {
                REF(CallTemplate::from(g_sc_const_real_extract, { value })),
                REF(ConstPointer::type_from(T))
            }));
    } break;
    case TK_Vector: {
        auto vt = cast<VectorType>(ST);
        auto argT = vt->element_type;
        auto numvals = (int)vt->count();
        //auto numelems = ConstInt::from(anchor, TYPE_I32, numvals);
        ValueRef result = REF(Undef::from(T));
        for (int i = 0; i < numvals; ++i) {
            auto idx = REF(ConstInt::from(TYPE_I32, i));
            auto arg =
                REF(CallTemplate::from(g_sc_const_extract_at, { value, idx }));
            auto unwrapped_arg = unwrap_value(argT, arg);
            assert(unwrapped_arg);
            result = REF(CallTemplate::from(g_insertelement, { result, unwrapped_arg, idx }));
        }
        return result;
    } break;
    case TK_Array:
    case TK_Matrix: {
        auto at = cast<ArrayLikeType>(ST);
        auto argT = at->element_type;
        auto numvals = (int)at->count();
        //auto numelems = ConstInt::from(anchor, TYPE_I32, numvals);
        ValueRef result = REF(Undef::from(T));
        for (int i = 0; i < numvals; ++i) {
            auto idx = REF(ConstInt::from(TYPE_I32, i));
            auto arg =
                REF(CallTemplate::from(g_sc_const_extract_at, { value, idx }));
            auto unwrapped_arg = unwrap_value(argT, arg);
            assert(unwrapped_arg);
            result = REF(CallTemplate::from(g_insertvalue, { result, unwrapped_arg, idx }));
        }
        return result;
    } break;
    case TK_Tuple: {
        auto tt = cast<TupleType>(ST);
        //auto numelems = ConstInt::from(anchor, TYPE_I32, tt->values.size());
        ValueRef result = REF(Undef::from(T));
        for (int i = 0; i < tt->values.size(); ++i) {
            auto idx = REF(ConstInt::from(TYPE_I32, i));
            auto arg =
                REF(CallTemplate::from(g_sc_const_extract_at, { value, idx }));
            auto argT = tt->values[i];
            auto unwrapped_arg = unwrap_value(argT, arg);
            assert(unwrapped_arg);
            result = REF(CallTemplate::from(g_insertvalue, { result, unwrapped_arg, idx }));
        }
        //StyledStream ss;
        //stream_ast(ss, result, StreamASTFormat());
        return result;
    } break;
    default:
        break;
    }
    return ValueRef();
}

ValueRef wrap_value(const Type *T, const ValueRef &value) {
    auto _anchor = value.anchor();
    if (is_value_stage_constant(value)) {
        return ConstAggregate::ast_from(value);
    }
    if (!is_opaque(T)) {
        T = strip_qualifiers(T);
        auto ST = storage_type(T).assert_ok();
        auto kind = ST->kind();
        switch(kind) {
        case TK_Pointer: {
            return REF(CallTemplate::from(g_sc_const_pointer_new, {
                    REF(ConstPointer::type_from(T)),
                    REF(CallTemplate::from(g_bitcast, { value, g_voidstar })) }));
        } break;
        case TK_Integer: {
            auto ti = cast<IntegerType>(ST);
            if (ti->width <= 64ull) {
                return REF(CallTemplate::from(g_sc_const_int_new, {
                        REF(ConstPointer::type_from(T)),
                        REF(CallTemplate::from(ti->issigned?g_sext:g_zext, { value,
                        g_u64 })) }));
            } else {
                // big integer
                auto targettype = local_ro_pointer_type(TYPE_U64);
                auto result = REF(Expression::unscoped_from());
                auto mem = REF(CallTemplate::from(g_alloca, {
                        REF(ConstPointer::type_from(T))
                    }));
                result->append(mem);
                result->append(REF(CallTemplate::from(g_store, { value, mem })));
                auto castmem =
                    REF(CallTemplate::from(g_bitcast, { mem,
                        REF(ConstPointer::type_from(targettype)) }));
                result->append(castmem);
                size_t numwords = (ti->width + 63ull) / 64ull;
                result->append(REF(CallTemplate::from(g_sc_const_int_words_new, {
                        REF(ConstPointer::type_from(T)),
                        REF(ConstInt::from(TYPE_I32, numwords)),
                        castmem
                    })));
                return result;
            }
        } break;
        case TK_Real: {
            //auto ti = cast<RealType>(ST);
            return REF(CallTemplate::from(g_sc_const_real_new, {
                    REF(ConstPointer::type_from(T)),
                    REF(CallTemplate::from(g_fpext, { value,
                    g_f64 })) }));
        } break;
        case TK_Vector: {
            auto at = cast<VectorType>(ST);
            auto result = REF(Expression::unscoped_from());
            auto ET = at->element_type;
            auto numvals = (int)at->count();
            auto numelems = REF(ConstInt::from(TYPE_I32, numvals));
            auto buf = REF(CallTemplate::from(g_alloca_array, {
                    REF(ConstPointer::type_from(TYPE_ValueRef)),
                    numelems
                }));
            result->append(buf);
            for (int i = 0; i < numvals; ++i) {
                auto idx = REF(ConstInt::from(TYPE_I32, i));
                auto arg =
                    REF(CallTemplate::from(g_extractelement, { value, idx }));
                auto wrapped_arg = wrap_value(ET, arg);
                assert(wrapped_arg);
                result->append(
                    REF(CallTemplate::from(g_store, {
                        wrapped_arg,
                        REF(CallTemplate::from(g_getelementptr, { buf, idx }))
                    })));
            }
            result->append(REF(CallTemplate::from(g_sc_const_aggregate_new,
                { REF(ConstPointer::type_from(T)), numelems, buf })));
            return result;
        } break;
        case TK_Array:
        case TK_Matrix: {
            auto at = cast<ArrayLikeType>(ST);
            auto result = REF(Expression::unscoped_from());
            auto ET = at->element_type;
            auto numvals = (int)at->count();
            auto numelems = REF(ConstInt::from(TYPE_I32, numvals));
            auto buf = REF(CallTemplate::from(g_alloca_array, {
                    REF(ConstPointer::type_from(TYPE_ValueRef)),
                    numelems
                }));
            result->append(buf);
            for (int i = 0; i < numvals; ++i) {
                auto idx = REF(ConstInt::from(TYPE_I32, i));
                auto arg =
                    REF(CallTemplate::from(g_extractvalue, { value, idx }));
                auto wrapped_arg = wrap_value(ET, arg);
                assert(wrapped_arg);
                result->append(
                    REF(CallTemplate::from(g_store, {
                        wrapped_arg,
                        REF(CallTemplate::from(g_getelementptr, { buf, idx }))
                    })));
            }
            result->append(REF(CallTemplate::from(g_sc_const_aggregate_new,
                { REF(ConstPointer::type_from(T)), numelems, buf })));
            return result;
        } break;
        case TK_Tuple: {
            auto tt = cast<TupleType>(ST);
            auto result = REF(Expression::unscoped_from());
            auto numelems = REF(ConstInt::from(TYPE_I32, tt->values.size()));
            auto buf = REF(CallTemplate::from(g_alloca_array, {
                    REF(ConstPointer::type_from(TYPE_ValueRef)),
                    numelems
                }));
            result->append(buf);
            for (int i = 0; i < tt->values.size(); ++i) {
                auto idx = REF(ConstInt::from(TYPE_I32, i));
                auto arg =
                    REF(CallTemplate::from(g_extractvalue, { value, idx }));
                auto argT = tt->values[i];
                auto wrapped_arg = wrap_value(argT, arg);
                assert(wrapped_arg);
                result->append(
                    REF(CallTemplate::from(g_store, {
                        wrapped_arg,
                        REF(CallTemplate::from(g_getelementptr, { buf, idx }))
                    })));
            }
            result->append(REF(CallTemplate::from(g_sc_const_aggregate_new,
                { REF(ConstPointer::type_from(T)), numelems, buf })));
            return result;
        } break;
        default:
            break;
        }
    }
    return ValueRef();
}

SCOPES_RESULT(TypedValueRef) quote(const ASTContext &ctx, const ValueRef &node) {
    return Quoter(ctx).quote(0, node);
}

//------------------------------------------------------------------------------

} // namespace scopes