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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* trans/monomorphise.hpp
* - MIR monomorphisation
*/
#include "monomorphise.hpp"
#include "hir_typeck/static.hpp"
#include <mir/mir.hpp>
#include <hir/hir.hpp>
#include <mir/operations.hpp> // Needed for post-monomorph checks and optimisations
#include <hir_conv/constant_evaluation.hpp>
namespace {
::MIR::LValue monomorph_LValue(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::LValue& tpl)
{
TU_MATCHA( (tpl), (e),
(Return, return e; ),
(Argument, return e; ),
(Local, return e; ),
(Static,
return box$(params.monomorph(resolve, *e));
),
(Field,
return ::MIR::LValue::make_Field({
box$(monomorph_LValue(resolve, params, *e.val)),
e.field_index
});
),
(Deref,
return ::MIR::LValue::make_Deref({
box$(monomorph_LValue(resolve, params, *e.val))
});
),
(Index,
return ::MIR::LValue::make_Index({
box$(monomorph_LValue(resolve, params, *e.val)),
box$(monomorph_LValue(resolve, params, *e.idx))
});
),
(Downcast,
return ::MIR::LValue::make_Downcast({
box$(monomorph_LValue(resolve, params, *e.val)),
e.variant_index
});
)
)
throw "";
}
::MIR::Constant monomorph_Constant(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::Constant& tpl)
{
TU_MATCHA( (tpl), (ce),
(Int,
return ::MIR::Constant::make_Int(ce);
),
(Uint,
return ::MIR::Constant::make_Uint(ce);
),
(Float,
return ::MIR::Constant::make_Float(ce);
),
(Bool,
return ::MIR::Constant::make_Bool(ce);
),
(Bytes,
return ::MIR::Constant(ce);
),
(StaticString,
return ::MIR::Constant(ce);
),
(Const,
return ::MIR::Constant::make_Const({
box$(params.monomorph(resolve, *ce.p))
});
),
(ItemAddr,
auto p = params.monomorph(resolve, *ce);
// TODO: If this is a pointer to a function on a trait object, replace with the address loaded from the vtable.
// - Requires creating a new temporary for the vtable pointer.
// - Also requires knowing what the receiver is.
return ::MIR::Constant( box$(p) );
)
)
throw "";
}
::MIR::Param monomorph_Param(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::Param& tpl)
{
TU_MATCHA( (tpl), (e),
(LValue,
return monomorph_LValue(resolve, params, e);
),
(Constant,
return monomorph_Constant(resolve, params, e);
)
)
throw "";
}
//::std::vector<::MIR::LValue> monomorph_LValue_list(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::std::vector<::MIR::LValue>& tpl)
//{
// ::std::vector<::MIR::LValue> rv;
// rv.reserve( tpl.size() );
// for(const auto& v : tpl)
// rv.push_back( monomorph_LValue(resolve, params, v) );
// return rv;
//}
::std::vector<::MIR::Param> monomorph_Param_list(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::std::vector<::MIR::Param>& tpl)
{
::std::vector<::MIR::Param> rv;
rv.reserve( tpl.size() );
for(const auto& v : tpl)
rv.push_back( monomorph_Param(resolve, params, v) );
return rv;
}
}
::MIR::FunctionPointer Trans_Monomorphise(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::FunctionPointer& tpl)
{
static Span sp;
TRACE_FUNCTION;
assert(tpl);
::MIR::Function output;
// 1. Monomorphise locals and temporaries
output.locals.reserve( tpl->locals.size() );
for(const auto& var : tpl->locals)
{
DEBUG("- _" << output.locals.size());
output.locals.push_back( params.monomorph(resolve, var) );
}
output.drop_flags = tpl->drop_flags;
// 2. Monomorphise all paths
output.blocks.reserve( tpl->blocks.size() );
for(const auto& block : tpl->blocks)
{
::std::vector< ::MIR::Statement> statements;
TRACE_FUNCTION_F("bb" << output.blocks.size());
statements.reserve( block.statements.size() );
for(const auto& stmt : block.statements)
{
switch( stmt.tag() )
{
case ::MIR::Statement::TAGDEAD: throw "";
case ::MIR::Statement::TAG_SetDropFlag:
statements.push_back( ::MIR::Statement( stmt.as_SetDropFlag() ) );
break;
case ::MIR::Statement::TAG_ScopeEnd:
statements.push_back( ::MIR::Statement( stmt.as_ScopeEnd() ) );
break;
case ::MIR::Statement::TAG_Drop: {
const auto& e = stmt.as_Drop();
DEBUG("- DROP " << e.slot);
statements.push_back( ::MIR::Statement::make_Drop({
e.kind,
monomorph_LValue(resolve, params, e.slot),
e.flag_idx
}) );
} break;
case ::MIR::Statement::TAG_Assign: {
const auto& e = stmt.as_Assign();
DEBUG("- " << e.dst << " = " << e.src);
::MIR::RValue rval;
TU_MATCHA( (e.src), (se),
(Use,
rval = ::MIR::RValue( monomorph_LValue(resolve, params, se) );
),
(Constant,
rval = monomorph_Constant(resolve, params, se);
),
(SizedArray,
rval = ::MIR::RValue::make_SizedArray({
monomorph_Param(resolve, params, se.val),
se.count
});
),
(Borrow,
rval = ::MIR::RValue::make_Borrow({
se.region, se.type,
monomorph_LValue(resolve, params, se.val)
});
),
(Cast,
rval = ::MIR::RValue::make_Cast({
monomorph_LValue(resolve, params, se.val),
params.monomorph(resolve, se.type)
});
),
(BinOp,
rval = ::MIR::RValue::make_BinOp({
monomorph_Param(resolve, params, se.val_l),
se.op,
monomorph_Param(resolve, params, se.val_r)
});
),
(UniOp,
rval = ::MIR::RValue::make_UniOp({
monomorph_LValue(resolve, params, se.val),
se.op
});
),
(DstMeta,
auto lv = monomorph_LValue(resolve, params, se.val);
// TODO: Get the type of this, and if it's an array - replace with the size
rval = ::MIR::RValue::make_DstMeta({ mv$(lv) });
),
(DstPtr,
rval = ::MIR::RValue::make_DstPtr({ monomorph_LValue(resolve, params, se.val) });
),
(MakeDst,
rval = ::MIR::RValue::make_MakeDst({
monomorph_Param(resolve, params, se.ptr_val),
monomorph_Param(resolve, params, se.meta_val)
});
),
(Tuple,
rval = ::MIR::RValue::make_Tuple({
monomorph_Param_list(resolve, params, se.vals)
});
),
(Array,
rval = ::MIR::RValue::make_Array({
monomorph_Param_list(resolve, params, se.vals)
});
),
// Create a new instance of a union (and eventually enum)
(Variant,
rval = ::MIR::RValue::make_Variant({
params.monomorph(resolve, se.path),
se.index,
monomorph_Param(resolve, params, se.val)
});
),
// Create a new instance of a struct (or enum)
(Struct,
rval = ::MIR::RValue::make_Struct({
params.monomorph(resolve, se.path),
monomorph_Param_list(resolve, params, se.vals)
});
)
)
statements.push_back( ::MIR::Statement::make_Assign({
monomorph_LValue(resolve, params, e.dst),
mv$(rval)
}) );
} break;
case ::MIR::Statement::TAG_Asm: {
const auto& e = stmt.as_Asm();
DEBUG("- asm! \"" << e.tpl << "\"");
::std::vector< ::std::pair<::std::string, ::MIR::LValue>> new_out, new_in;
new_out.reserve( e.outputs.size() );
for(auto& ent : e.outputs)
new_out.push_back(::std::make_pair( ent.first, monomorph_LValue(resolve, params, ent.second) ));
new_in.reserve( e.inputs.size() );
for(auto& ent : e.inputs)
new_in.push_back(::std::make_pair( ent.first, monomorph_LValue(resolve, params, ent.second) ));
statements.push_back( ::MIR::Statement::make_Asm({
e.tpl, mv$(new_out), mv$(new_in), e.clobbers, e.flags
}) );
} break;
}
}
::MIR::Terminator terminator;
DEBUG("> " << block.terminator);
TU_MATCHA( (block.terminator), (e),
(Incomplete,
//BUG(sp, "Incomplete block");
terminator = e;
),
(Return,
terminator = e;
),
(Diverge,
terminator = e;
),
(Goto,
terminator = e;
),
(Panic,
terminator = e;
),
(If,
terminator = ::MIR::Terminator::make_If({
monomorph_LValue(resolve, params, e.cond),
e.bb0, e.bb1
});
),
(Switch,
terminator = ::MIR::Terminator::make_Switch({
monomorph_LValue(resolve, params, e.val),
e.targets
});
),
(SwitchValue,
terminator = ::MIR::Terminator::make_SwitchValue({
monomorph_LValue(resolve, params, e.val),
e.def_target,
e.targets,
e.values.clone()
});
),
(Call,
struct H {
static ::MIR::CallTarget monomorph_calltarget(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::CallTarget& ct) {
TU_MATCHA( (ct), (e),
(Value,
return monomorph_LValue(resolve, params, e);
),
(Path,
return params.monomorph(resolve, e);
),
(Intrinsic,
return ::MIR::CallTarget::make_Intrinsic({ e.name, params.monomorph(resolve, e.params) });
)
)
throw "";
}
};
terminator = ::MIR::Terminator::make_Call({
e.ret_block, e.panic_block,
monomorph_LValue(resolve, params, e.ret_val),
H::monomorph_calltarget(resolve, params, e.fcn),
monomorph_Param_list(resolve, params, e.args)
});
)
)
output.blocks.push_back( ::MIR::BasicBlock { mv$(statements), mv$(terminator) } );
}
return ::MIR::FunctionPointer( box$(output).release() );
}
/// Monomorphise all functions in a TransList
void Trans_Monomorphise_List(const ::HIR::Crate& crate, TransList& list)
{
::StaticTraitResolve resolve { crate };
for(auto& fcn_ent : list.m_functions)
{
const auto& fcn = *fcn_ent.second->ptr;
// Trait methods (which are the only case where `Self` can exist in the argument list at this stage) always need to be monomorphised.
bool is_method = ( fcn.m_args.size() > 0 && visit_ty_with(fcn.m_args[0].second, [&](const auto& x){return x == ::HIR::TypeRef("Self",0xFFFF);}) );
if(fcn_ent.second->pp.has_types() || is_method)
{
const auto& path = fcn_ent.first;
const auto& pp = fcn_ent.second->pp;
TRACE_FUNCTION_FR(path, path);
ASSERT_BUG(Span(), fcn.m_code.m_mir, "No code for " << path);
auto mir = Trans_Monomorphise(resolve, fcn_ent.second->pp, fcn.m_code.m_mir);
// TODO: Should these be moved to their own pass? Potentially not, the extra pass should just be an inlining optimise pass
auto ret_type = pp.monomorph(resolve, fcn.m_return);
::HIR::Function::args_t args;
for(const auto& a : fcn.m_args)
args.push_back(::std::make_pair( ::HIR::Pattern{}, pp.monomorph(resolve, a.second) ));
//::std::string s = FMT(path);
::HIR::ItemPath ip(path);
MIR_Validate(resolve, ip, *mir, args, ret_type);
MIR_Cleanup(resolve, ip, *mir, args, ret_type);
MIR_Optimise(resolve, ip, *mir, args, ret_type);
MIR_Validate(resolve, ip, *mir, args, ret_type);
fcn_ent.second->monomorphised.ret_ty = ::std::move(ret_type);
fcn_ent.second->monomorphised.arg_tys = ::std::move(args);
fcn_ent.second->monomorphised.code = ::std::move(mir);
}
}
// Also do constants and statics (stored in where?)
// - NOTE: Done in reverse order, because consteval needs used constants to be evaluated
for(auto& ent : reverse(list.m_constants))
{
const auto& path = ent.first;
const auto& pp = ent.second->pp;
const auto& c = *ent.second->ptr;
TRACE_FUNCTION_FR(path, path);
auto ty = pp.monomorph(resolve, c.m_type);
// 1. Evaluate the constant
struct Nvs: public ::HIR::Evaluator::Newval
{
::HIR::Path new_static(::HIR::TypeRef type, ::HIR::Literal value) override {
TODO(Span(), "Create new static in monomorph pass - " << value << " : " << type);
}
} nvs;
auto eval = ::HIR::Evaluator { pp.sp, crate, nvs };
MonomorphState ms;
ms.self_ty = &pp.self_type;
ms.pp_impl = &pp.pp_impl;
ms.pp_method = &pp.pp_method;
auto new_lit = eval.evaluate_constant(path, c.m_value, ::std::move(ty), ::std::move(ms));
ASSERT_BUG(Span(), !new_lit.is_Defer(), "Result of evaluating " << path << " was still Defer");
// 2. Store evaluated HIR::Literal in c.m_monomorph_cache
c.m_monomorph_cache.insert(::std::make_pair( path.clone(), ::std::move(new_lit) ));
}
}
|