-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinker.cpp
1438 lines (1217 loc) · 51.8 KB
/
Linker.cpp
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=====================================================================
Linker.cpp
----------
Copyright Glare Technologies Limited 2016 -
=====================================================================*/
#include "Linker.h"
#include "BuiltInFunctionImpl.h"
#include "utils/StringUtils.h"
#include "utils/PlatformUtils.h"
#include "wnt_ExternalFunction.h"
#include "wnt_RefCounting.h"
#include <limits>
using std::vector;
namespace Winter
{
Linker::Linker(bool try_coerce_int_to_double_first_, bool emit_in_bound_asserts_,
bool real_is_double_, bool optimise_for_opencl_)
: try_coerce_int_to_double_first(try_coerce_int_to_double_first_),
emit_in_bound_asserts(emit_in_bound_asserts_),
real_is_double(real_is_double_),
optimise_for_opencl(optimise_for_opencl_)
{}
Linker::~Linker()
{}
void Linker::addFunctions(const vector<FunctionDefinitionRef>& new_func_defs)
{
for(unsigned int i=0; i<new_func_defs.size(); ++i)
addFunction(new_func_defs[i]);
}
void Linker::addFunction(const FunctionDefinitionRef& def)
{
if(this->sig_to_function_map.find(def->sig) != this->sig_to_function_map.end())
throw ExceptionWithPosition("Function " + def->sig.toString() + " already defined: " + errorContextString(def.getPointer()) + "\nalready defined here: ",
errorContext(this->sig_to_function_map[def->sig].getPointer()));
this->name_to_functions_map[def->sig.name].push_back(def);
this->sig_to_function_map.insert(std::make_pair(def->sig, def));
top_level_defs.push_back(def);
}
void Linker::addTopLevelDefs(const vector<ASTNodeRef>& defs)
{
for(unsigned int i=0; i<defs.size(); ++i)
{
if(defs[i]->nodeType() == ASTNode::FunctionDefinitionType)
addFunction(defs[i].downcast<FunctionDefinition>());
else if(defs[i]->nodeType() == ASTNode::NamedConstantType)
{
const NamedConstantRef named_constant = defs[i].downcast<NamedConstant>();
if(named_constant_map.find(named_constant->name) != named_constant_map.end())
throw ExceptionWithPosition("Named constant with name '" + named_constant->name + "' already defined." + errorContextString(*named_constant) +
"\nalready defined here: ", errorContext(named_constant_map[named_constant->name].getPointer()));
named_constant_map[named_constant->name] = named_constant;
top_level_defs.push_back(named_constant);
}
else
{
assert(0);
}
}
}
void Linker::addExternalFunctions(vector<ExternalFunctionRef>& funcs)
{
for(unsigned int i=0; i<funcs.size(); ++i)
{
ExternalFunctionRef& f = funcs[i];
vector<FunctionDefinition::FunctionArg> args;
for(size_t z=0; z<f->sig.param_types.size(); ++z)
args.push_back(FunctionDefinition::FunctionArg(f->sig.param_types[z], "arg_" + ::toString((uint64)z)));
Reference<FunctionDefinition> def(new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
f->sig.name,
args,
ASTNodeRef(NULL), // body
f->return_type, // declared return type
NULL
));
def->external_function = f;
//this->external_functions.insert(f[i].sig);
addFunction(def);
//this->functions.insert(std::make_pair(funcs[i].sig, def));*/
//this->external_functions.insert(std::make_pair(funcs[i]->sig, funcs[i]));
}
}
void Linker::buildLLVMCode(llvm::Module* module, const llvm::DataLayout* target_data, const CommonFunctions& common_functions, ProgramStats& stats, bool emit_trace_code)
{
WinterCPUInfo cpu_info;
#if defined(__x86_64__) || defined(_M_X64)
cpu_info.arch = WinterCPUInfo::Arch_x64;
PlatformUtils::getCPUInfo(cpu_info.cpu_info);
#else
cpu_info.arch = WinterCPUInfo::Arch_ARM64;
#endif
std::set<VRef<const Type>, ConstTypeVRefLessThan> destructors_called_types;
for(Linker::SigToFuncMapType::iterator it = sig_to_function_map.begin(); it != sig_to_function_map.end(); ++it)
{
FunctionDefinition& f = *(*it).second;
if(!f.isGenericFunction() && !f.isExternalFunction())
{
if(!f.is_anon_func)
f.buildLLVMFunction(module, cpu_info, target_data, common_functions, destructors_called_types, stats, emit_trace_code, false);
if(f.is_anon_func || f.need_to_emit_captured_var_struct_version)
f.buildLLVMFunction(module, cpu_info, target_data, common_functions, destructors_called_types, stats, emit_trace_code, true);
}
}
// Emit code for anonymous functions
for(size_t i=0; i<anon_functions_to_codegen.size(); ++i)
{
anon_functions_to_codegen[i]->buildLLVMFunction(module, cpu_info, target_data, common_functions, destructors_called_types, stats, emit_trace_code,
true // with_captured_var_struct_ptr
);
}
// Build concrete funcs
/*for(unsigned int i=0; i<concrete_funcs.size(); ++i)
{
assert(!concrete_funcs[i]->isGenericFunction());
concrete_funcs[i]->buildLLVMFunction(module, cpu_info, target_data, common_functions);
}*/
// Build 'unique' functions (like shuffle())
for(unsigned int i=0; i<unique_functions.size(); ++i)
{
unique_functions[i]->buildLLVMFunction(module, cpu_info, target_data, common_functions, destructors_called_types, stats, emit_trace_code, false);
}
// Emit destructors
for(auto i = destructors_called_types.begin(); i != destructors_called_types.end(); ++i)
{
if((*i)->hasDestructor())
{
RefCounting::emitDecrementorForType(module, target_data, common_functions, *i);
RefCounting::emitDestructorForType(module, target_data, common_functions, *i);
}
}
}
const std::string Linker::buildOpenCLCode()
{
//NOTE: not called right now
assert(0);
std::string s;
EmitOpenCLCodeParams params;
params.uid = 0;
params.emit_comments = true;
params.emit_in_bound_asserts = emit_in_bound_asserts;
for(Linker::SigToFuncMapType::iterator it = sig_to_function_map.begin(); it != sig_to_function_map.end(); ++it)
{
FunctionDefinition& f = *(*it).second;
if(!f.isGenericFunction() && !f.isExternalFunction() && f.built_in_func_impl.isNull())
{
s += f.emitOpenCLC(params) + "\n";
}
}
// Build concrete funcs
/*for(unsigned int i=0; i<concrete_funcs.size(); ++i)
{
assert(!concrete_funcs[i]->isGenericFunction());
s += concrete_funcs[i]->emitOpenCLC(params) + "\n";
}*/
// Build 'unique' functions (like shuffle())
//for(unsigned int i=0; i<unique_functions.size(); ++i)
//{
// s += unique_functions[i]->emitOpenCLC() + "\n";
//}
return params.file_scope_code + "\n\n" + s;
}
/*ExternalFunctionRef Linker::findMatchingExternalFunction(const FunctionSignature& sig)
{
ExternalFuncMapType::iterator res = external_functions.find(sig);
if(res != external_functions.end())
{
return res->second;
}
return ExternalFunctionRef();
}*/
template <class BuiltInFuncType>
static FunctionDefinitionRef makeBuiltInFuncDef(const std::string& name, const TypeVRef& type, const TypeVRef& return_type)
{
vector<FunctionDefinition::FunctionArg> args;
args.push_back(FunctionDefinition::FunctionArg(type, "x"));
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
name, // name
args, // args
NULL, // body expr
return_type, // return type
new BuiltInFuncType(type) // built in impl.
);
return def;
}
FunctionDefinitionRef Linker::findMatchingFunctionSimple(const FunctionSignature& sig)
{
SigToFuncMapType::iterator sig_lookup_res = sig_to_function_map.find(sig);
if(sig_lookup_res != sig_to_function_map.end())
return sig_lookup_res->second;
else
return NULL;
}
static vector<FunctionDefinition::FunctionArg> makeFunctionArgPair(const std::string& arg0_name, const TypeVRef& type0, const std::string& arg1_name, const TypeVRef& type1)
{
vector<FunctionDefinition::FunctionArg> args;
args.reserve(2);
args.push_back(FunctionDefinition::FunctionArg(type0, arg0_name));
args.push_back(FunctionDefinition::FunctionArg(type1, arg1_name));
return args;
}
Reference<FunctionDefinition> Linker::findMatchingFunction(const FunctionSignature& sig, const SrcLocation& call_src_location, int effective_callsite_order_num) // , const std::vector<FunctionDefinition*>* func_def_stack)
{
/*
if sig.name matches eN
create or insert eN function
For each function f
If f.name == sig.name
If it takes the correct number of args
new empty association
for each arg type in f T_i
if T_i is a generic type
if T_i is already associated with a type
if T_i associated_type != sig.T_i, fail match
else let T_i = sig.T_i
else if T_i is a concrete type
if T_i associated_type != sig.T_i, fail match
if T_i has children, then, for each child C_i
if sig.T_i is concrete type
*/
// If the function matching this signature is in the map, return it
SigToFuncMapType::iterator sig_lookup_res = sig_to_function_map.find(sig);
if(sig_lookup_res != sig_to_function_map.end())
{
if(sig_lookup_res->second->order_num >= effective_callsite_order_num && effective_callsite_order_num != -1)
throw ExceptionWithPosition("Tried to refer to a function defined later: " + sig.toString() + errorContextString(call_src_location) + "\ntried to call function defined later: ", errorContext(*sig_lookup_res->second));
//if(sig_lookup_res->second->order_num < effective_callsite_order_num || effective_callsite_order_num == -1) // !func_def_stack || isTargetDefinedBeforeAllInStack(*func_def_stack, sig_lookup_res->second->order_num))
return sig_lookup_res->second;
}
if(sig.param_types.size() == 0)
{
if(sig.name == "floatNaN")
{
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
sig.name, // name
std::vector<FunctionDefinition::FunctionArg>(), // args
NULL, // body expr
new Float(), // return type
new NaNBuiltInFunc(new Float()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.name == "doubleNaN")
{
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
sig.name, // name
std::vector<FunctionDefinition::FunctionArg>(), // args
NULL, // body expr
new Double(), // return type
new NaNBuiltInFunc(new Double()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.name == "realNaN")
{
if(real_is_double)
{
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
sig.name, // name
std::vector<FunctionDefinition::FunctionArg>(), // args
NULL, // body expr
new Double(), // return type
new NaNBuiltInFunc(new Double()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else
{
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
sig.name, // name
std::vector<FunctionDefinition::FunctionArg>(), // args
NULL, // body expr
new Float(), // return type
new NaNBuiltInFunc(new Float()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
}
else if(sig.param_types.size() == 1)
{
// Handle float->float, or vector<float, N> -> vector<float, N> functions
if(sig.param_types[0]->getType() == Type::FloatType || sig.param_types[0]->getType() == Type::DoubleType || // if float or double
(sig.param_types[0]->getType() == Type::VectorTypeType && static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::FloatType) || // or vector of floats
(sig.param_types[0]->getType() == Type::VectorTypeType && static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::DoubleType) // or vector of doubles
)
{
if(sig.name == "floor")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<FloorBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "ceil")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<CeilBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "sqrt")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<SqrtBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "sin")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<SinBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "cos")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<CosBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "exp")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<ExpBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "log")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<LogBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "abs")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<AbsBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "truncateToInt")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<TruncateToIntBuiltInFunc>(sig.name, sig.param_types[0], TruncateToIntBuiltInFunc::getReturnType(sig.param_types[0]));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "sign")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<SignBuiltInFunc>(sig.name, sig.param_types[0], sig.param_types[0]);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
else if(
(sig.param_types[0]->getType() == Type::IntType || // If Int
(sig.param_types[0]->getType() == Type::VectorTypeType && static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::IntType))) // or vector of ints
{
if(sig.name == "toFloat")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<ToFloatBuiltInFunc>(sig.name, sig.param_types[0], ToFloatBuiltInFunc::getReturnType(sig.param_types[0]));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.name == "toDouble")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<ToDoubleBuiltInFunc>(sig.name, sig.param_types[0], ToDoubleBuiltInFunc::getReturnType(sig.param_types[0]));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.name == "toReal")
{
FunctionDefinitionRef def;
if(real_is_double)
def = makeBuiltInFuncDef<ToDoubleBuiltInFunc>(sig.name, sig.param_types[0], ToDoubleBuiltInFunc::getReturnType(sig.param_types[0]));
else
def = makeBuiltInFuncDef<ToFloatBuiltInFunc>(sig.name, sig.param_types[0], ToFloatBuiltInFunc::getReturnType(sig.param_types[0]));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.param_types[0]->getType() == Type::IntType && sig.param_types[0].downcastToPtr<const Int>()->numBits() == 32 && sig.name == "toInt64")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<ToInt64BuiltInFunc>(sig.name, sig.param_types[0], new Int(64));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
if(sig.param_types[0]->getType() == Type::IntType && sig.param_types[0].downcastToPtr<const Int>()->numBits() == 64 && sig.name == "toInt32")
{
FunctionDefinitionRef def = makeBuiltInFuncDef<ToInt32BuiltInFunc>(sig.name, sig.param_types[0], new Int(32));
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
else if(sig.param_types[0]->getType() == Type::OpaqueTypeType)
{
if(sig.name == "toInt")
{
TypeVRef ret_type = new Int(64);
FunctionDefinitionRef def = makeBuiltInFuncDef<VoidPtrToInt64BuiltInFunc>(sig.name, sig.param_types[0], ret_type);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(sig.name == "length")
{
if(sig.param_types[0]->getType() == Type::ArrayTypeType || sig.param_types[0]->getType() == Type::VArrayTypeType ||
sig.param_types[0]->getType() == Type::TupleTypeType || sig.param_types[0]->getType() == Type::VectorTypeType)
{
TypeVRef ret_type = new Int(64);
FunctionDefinitionRef def = makeBuiltInFuncDef<LengthBuiltInFunc>(sig.name, sig.param_types[0], ret_type);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
}
else if(sig.param_types.size() == 2)
{
if(sig.name == "__compare_equal")
{
if(sig.param_types[0]->requiresCompareEqualFunction())
{
//if(type->getType() == Type::FunctionType)
// continue; // TODO: implement function comparison
vector<FunctionDefinition::FunctionArg> compare_args;
compare_args.push_back(FunctionDefinition::FunctionArg(sig.param_types[0], "a"));
compare_args.push_back(FunctionDefinition::FunctionArg(sig.param_types[0], "b"));
FunctionDefinitionRef compare_eq_func = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"__compare_equal", // name
compare_args, // arguments
ASTNodeRef(), // body expr
new Bool(), // declard return type
new CompareEqualBuiltInFunc(sig.param_types[0], /*is_compare_not_equal=*/false) // built in func impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, compare_eq_func));
this->top_level_defs.push_back(compare_eq_func); // Add to top_level_defs so binding is done on this function as well.
return compare_eq_func;
}
}
else if(sig.name == "__compare_not_equal")
{
if(sig.param_types[0]->requiresCompareEqualFunction())
{
//if(type->getType() == Type::FunctionType)
// continue; // TODO: implement function comparison
vector<FunctionDefinition::FunctionArg> compare_args;
compare_args.push_back(FunctionDefinition::FunctionArg(sig.param_types[0], "a"));
compare_args.push_back(FunctionDefinition::FunctionArg(sig.param_types[0], "b"));
FunctionDefinitionRef compare_neq_func = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"__compare_not_equal", // name
compare_args, // arguments
ASTNodeRef(), // body expr
new Bool(), // declard return type
new CompareEqualBuiltInFunc(sig.param_types[0], /*is_compare_not_equal=*/true) // built in func impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, compare_neq_func));
this->top_level_defs.push_back(compare_neq_func); // Add to top_level_defs so binding is done on this function as well.
return compare_neq_func;
}
}
if(sig.param_types[0]->getType() == Type::FunctionType && sig.param_types[1]->getType() == Type::ArrayTypeType)
{
if(sig.name == "map")
{
const VRef<ArrayType> array_type = sig.param_types[1].downcast<ArrayType>();
const VRef<Type> array_elem_type = array_type->elem_type;
const VRef<Function> func_type = sig.param_types[0].downcast<Function>();
const VRef<Type> R(func_type->return_type);
// map(function<T, R>, array<T, N>) array<R, N>
if(func_type->arg_types.size() != 1)
throw ExceptionWithPosition("Function argument to map must take one argument.", errorContext(call_src_location));
if(*func_type->arg_types[0] != *array_elem_type)
{
throw ExceptionWithPosition(std::string("Function argument to map must take same argument type as array element.\n") +
"Function type: " + func_type->toString() + ",\n array_elem_type: " + array_elem_type->toString(), errorContext(call_src_location));
}
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("f", func_type, "array", array_type);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"map",
args,
ASTNodeRef(NULL), // body expr
new ArrayType(R, array_type->num_elems), // return type
new ArrayMapBuiltInFunc(
array_type, // from array type
func_type // func type
)
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(sig.param_types[0]->getType() == Type::ArrayTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "elem")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("array", sig.param_types[0], "index", sig.param_types[1]);
VRef<Type> ret_type = sig.param_types[0].downcast<ArrayType>()->elem_type;
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"elem", // name
args, // args
NULL, // body expr
ret_type, // return type
new ArraySubscriptBuiltInFunc(sig.param_types[0].downcast<ArrayType>(), sig.param_types[1]) // built in impl.
);
assert(this->sig_to_function_map.find(sig) == this->sig_to_function_map.end()); // Check not already inserted
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(sig.param_types[0]->getType() == Type::VArrayTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "elem")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("varray", sig.param_types[0], "index", sig.param_types[1]);
TypeVRef ret_type = sig.param_types[0].downcast<const VArrayType>()->elem_type;
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"elem", // name
args, // args
NULL, // body expr
ret_type, // return type
new VArraySubscriptBuiltInFunc(sig.param_types[0].downcast<VArrayType>(), sig.param_types[1]) // built in impl.
);
assert(this->sig_to_function_map.find(sig) == this->sig_to_function_map.end()); // Check not already inserted
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
// inBounds(array, index)
if(sig.param_types[0]->getType() == Type::ArrayTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "inBounds")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("array", sig.param_types[0], "index", sig.param_types[1]);
TypeRef ret_type = new Bool();
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"inBounds", // name
args, // args
NULL, // body expr
ret_type, // return type
new ArrayInBoundsBuiltInFunc(sig.param_types[0].downcast<ArrayType>(), sig.param_types[1]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
// inBounds(vector, index)
if(sig.param_types[0]->getType() == Type::VectorTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "inBounds")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("vector", sig.param_types[0], "index", sig.param_types[1]);
TypeRef ret_type = new Bool();
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"inBounds", // name
args, // args
NULL, // body expr
ret_type, // return type
new VectorInBoundsBuiltInFunc(sig.param_types[0].downcast<VectorType>(), sig.param_types[1]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(sig.param_types[0]->getType() == Type::VectorTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "elem")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("vector", sig.param_types[0], "index", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"elem", // name
args, // args
NULL, // body expr
sig.param_types[0].downcast<VectorType>()->elem_type, // return type
new VectorSubscriptBuiltInFunc(sig.param_types[0].downcast<VectorType>(), sig.param_types[1]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(sig.param_types[0]->getType() == Type::TupleTypeType && sig.param_types[1]->getType() == Type::IntType)
{
if(sig.name == "elem")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("tuple", sig.param_types[0], "index", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
call_src_location,
-1, // order number - Consider Before everything else
"elem", // name
args, // args
NULL, // body expr
NULL, // sig.param_types[0].downcast<TupleType>()->component_types, // return type
new GetTupleElementBuiltInFunc(sig.param_types[0].downcast<TupleType>(), std::numeric_limits<unsigned int>::max()) // built in impl.
);
// This isn't really a proper function, and cannot be, because the return type depends on the index.
// So it will just be special cased in the FunctionExpression node code emission, and no actual func should be generated for it.
unique_functions_no_codegen.push_back(def);
return def;
}
}
// Gather elem : elem(array<T, n>, vector<int, m>) -> vector<T, m>
if(sig.param_types[0]->getType() == Type::ArrayTypeType && sig.param_types[1]->getType() == Type::VectorTypeType && sig.param_types[1].downcast<VectorType>()->elem_type->getType() == Type::IntType)
{
if(sig.name == "elem")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("array", sig.param_types[0], "index_vector", sig.param_types[1]);
VRef<ArrayType> array_type = sig.param_types[0].downcast<ArrayType>();
TypeRef return_type = new VectorType(array_type->elem_type, sig.param_types[1].downcast<VectorType>()->num);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"elem", // name
args, // args
NULL, // body expr
return_type, // return type
new ArraySubscriptBuiltInFunc(array_type, sig.param_types[1]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if( (sig.param_types[0]->getType() == Type::FloatType && sig.param_types[1]->getType() == Type::FloatType) ||
(sig.param_types[0]->getType() == Type::DoubleType && sig.param_types[1]->getType() == Type::DoubleType))
{
if(sig.name == "pow")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"pow", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new PowBuiltInFunc(sig.param_types[0]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "_frem_")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"_frem_", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new FRemBuiltInFunc(sig.param_types[0]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if(
sig.param_types[0]->getType() == Type::VectorTypeType && // vector
sig.param_types[1]->getType() == Type::VectorTypeType) // and vector
{
// Shuffle(vector<T, m>, vector<int, n) -> vector<T, n>
if(sig.param_types[1].downcast<VectorType>()->elem_type->getType() == Type::IntType)
{
if(sig.name == "shuffle")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
call_src_location,
-1, // order number
"shuffle_" + toString(unique_functions.size()) + "_", // name
args, // args
NULL, // body expr
new VectorType(sig.param_types[0].downcast<VectorType>()->elem_type, sig.param_types[1].downcast<VectorType>()->num), // return type
new ShuffleBuiltInFunc(sig.param_types[0].downcast<VectorType>(), sig.param_types[1].downcast<VectorType>()) // built in impl.
);
// NOTE: because shuffle is unusual in that it has the shuffle mask 'baked into it', we need a unique ShuffleBuiltInFunc impl each time.
// So don't add to function map, so that it isn't reused.
// However, we need to add it to unique_functions to prevent it from being deleted, as calling function expr doesn't hold a ref to it.
unique_functions.push_back(def);
return def;
}
}
if((
(static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::FloatType) || // if vector of floats
(static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::DoubleType) || // or vector of doubles
(static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::IntType) // or vector of ints
) && (*sig.param_types[0] == *sig.param_types[1])) // and argument types are the same
{
assert(*sig.param_types[0] == *sig.param_types[1]);
if(sig.name == "min")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"min", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new VectorMinBuiltInFunc(sig.param_types[0].downcast<VectorType>()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "max")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
const TypeRef ret_type = sig.param_types[0];
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"max", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new VectorMaxBuiltInFunc(sig.param_types[0].downcast<VectorType>()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
}
if((static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::FloatType || // vector of floats
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type->getType() == Type::DoubleType) && // or vector of doubles
(*sig.param_types[0] == *sig.param_types[1])) // and argument types are the same
{
if(sig.name == "pow")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"pow", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new PowBuiltInFunc(sig.param_types[0]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "_frem_")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"_frem_", // name
args, // args
NULL, // body expr
sig.param_types[0], // return type
new FRemBuiltInFunc(sig.param_types[0]) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "dot")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"dot", // name
args, // args
NULL, // body expr
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type, // return type
new DotProductBuiltInFunc(sig.param_types[0].downcast<VectorType>()) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "dot1")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"dot1", // name
args, // args
NULL, // body expr
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type, // return type
new DotProductBuiltInFunc(sig.param_types[0].downcast<VectorType>(), /*num_components=*/1) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "dot2")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"dot2", // name
args, // args
NULL, // body expr
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type, // return type
new DotProductBuiltInFunc(sig.param_types[0].downcast<VectorType>(), /*num_components=*/2) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "dot3")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"dot3", // name
args, // args
NULL, // body expr
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type, // return type
new DotProductBuiltInFunc(sig.param_types[0].downcast<VectorType>(), /*num_components=*/3) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
else if(sig.name == "dot4")
{
const vector<FunctionDefinition::FunctionArg> args = makeFunctionArgPair("x", sig.param_types[0], "y", sig.param_types[1]);
FunctionDefinitionRef def = new FunctionDefinition(
SrcLocation::invalidLocation(),
-1, // order number
"dot4", // name
args, // args
NULL, // body expr
static_cast<const VectorType*>(sig.param_types[0].getPointer())->elem_type, // return type
new DotProductBuiltInFunc(sig.param_types[0].downcast<VectorType>(), /*num_components=*/4) // built in impl.
);
this->sig_to_function_map.insert(std::make_pair(sig, def));
return def;
}
} // End if (vector of floats, vector of floats)
} // End if (vector, vector) params
if(sig.name == "makeVArray" && sig.param_types[1]->getType() == Type::IntType)
{