-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.cpp
More file actions
1267 lines (1076 loc) · 35.7 KB
/
ast.cpp
File metadata and controls
1267 lines (1076 loc) · 35.7 KB
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
#include "llvm/Config/config.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/CodeGen/MachineCodeInfo.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JIT.h"
#if ((LLVM_VERSION_MAJOR > 3) || ((LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 3)))
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#else
#include "llvm/DataLayout.h"
#include "llvm/DerivedTypes.h"
#include "llvm/IRBuilder.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#endif
#include "llvm/PassManager.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Transforms/Scalar.h"
#include <cstdio>
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
using namespace std;
#include "typeinfo.h"
#include "ast.h"
#include "ast-internal.h"
using namespace nanjit;
/* Base types, from lowest to highest precedence */
static const TypeInfo::BaseTypeEnum BASE_TYPES[] = {
TypeInfo::TYPE_USHORT,
TypeInfo::TYPE_SHORT,
TypeInfo::TYPE_UINT,
TypeInfo::TYPE_INT,
TypeInfo::TYPE_FLOAT,
TypeInfo::TYPE_VOID
};
llvm::Type *typeinfo_get_llvm_type(nanjit::TypeInfo type)
{
if (type.getBaseType() == TypeInfo::TYPE_VOID)
throw SyntaxErrorException("Could not get LLVM type for " + type.toStr());
Type *llvm_type = type.getLLVMType();
if (!llvm_type)
throw SyntaxErrorException("Could not get LLVM type for " + type.toStr());
return llvm_type;
}
void cast_value(ScopeContext *scope, nanjit::TypeInfo to_type, nanjit::TypeInfo from_type, Value **value)
{
IRBuilder<> *Builder = scope->Builder;
if (to_type.getWidth() != from_type.getWidth())
throw SyntaxErrorException("Could not convert " + from_type.toStr() + " to " + to_type.toStr() + ", types differ in width");
TypeInfo::BaseTypeEnum to_type_base = to_type.getBaseType();
TypeInfo::BaseTypeEnum from_type_base = from_type.getBaseType();
if (to_type == from_type)
{
return;
}
else if ((to_type_base == TypeInfo::TYPE_UINT && from_type_base == TypeInfo::TYPE_INT) ||
(to_type_base == TypeInfo::TYPE_INT && from_type_base == TypeInfo::TYPE_UINT))
{
return;
}
else if ((to_type_base == TypeInfo::TYPE_UINT && from_type_base == TypeInfo::TYPE_USHORT) ||
(to_type_base == TypeInfo::TYPE_INT && from_type_base == TypeInfo::TYPE_USHORT) ||
(to_type_base == TypeInfo::TYPE_UINT && from_type_base == TypeInfo::TYPE_SHORT))
{
*value = Builder->CreateZExt(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_USHORT && from_type_base == TypeInfo::TYPE_UINT) ||
(to_type_base == TypeInfo::TYPE_SHORT && from_type_base == TypeInfo::TYPE_UINT) ||
(to_type_base == TypeInfo::TYPE_USHORT && from_type_base == TypeInfo::TYPE_INT))
{
*value = Builder->CreateTrunc(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_INT && from_type_base == TypeInfo::TYPE_SHORT))
{
*value = Builder->CreateSExt(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_SHORT && from_type_base == TypeInfo::TYPE_INT))
{
*value = Builder->CreateTrunc(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_FLOAT && from_type_base == TypeInfo::TYPE_INT) ||
(to_type_base == TypeInfo::TYPE_FLOAT && from_type_base == TypeInfo::TYPE_SHORT))
{
*value = Builder->CreateSIToFP(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_FLOAT && from_type_base == TypeInfo::TYPE_UINT) ||
(to_type_base == TypeInfo::TYPE_FLOAT && from_type_base == TypeInfo::TYPE_USHORT))
{
*value = Builder->CreateUIToFP(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_INT && from_type_base == TypeInfo::TYPE_FLOAT) ||
(to_type_base == TypeInfo::TYPE_SHORT && from_type_base == TypeInfo::TYPE_FLOAT))
{
*value = Builder->CreateFPToSI(*value, typeinfo_get_llvm_type(to_type));
}
else if ((to_type_base == TypeInfo::TYPE_UINT && from_type_base == TypeInfo::TYPE_FLOAT) ||
(to_type_base == TypeInfo::TYPE_USHORT && from_type_base == TypeInfo::TYPE_FLOAT))
{
*value = Builder->CreateFPToUI(*value, typeinfo_get_llvm_type(to_type));
}
else
throw SyntaxErrorException("Could not convert " + from_type.toStr() + " to " + to_type.toStr());
}
Function *define_llvm_intrinisic(ScopeContext *scope, string name, TypeInfo type)
{
if (!type.isFloatType())
throw SyntaxErrorException("Intrinsic \"" + name + "\" type must be a float");
std::stringstream full_name;
if (type.getWidth() != 1)
full_name << name << ".v" << type.getWidth() << "f32";
else
full_name << name << ".f32";
Function *func = scope->Module->getFunction(full_name.str());
if (!func)
{
Type *result_type = type.getLLVMType();
vector<Type *> arg_types;
arg_types.push_back(result_type);
/* create the function type */
FunctionType *func_type = FunctionType::get(result_type, arg_types, false);
func = Function::Create(func_type, Function::ExternalLinkage, full_name.str(), scope->Module);
}
return func;
}
void ScopeContext::setVariable(std::string name, llvm::Value *value)
{
/* Set an existing value in this contex's or a parent's scope */
std::map<std::string, llvm::Value *>::iterator it;
it = variables.find(name);
if (it == variables.end())
{
if (parent)
parent->setVariable(name, value);
else
throw SyntaxErrorException("Assignment to undefined variable \"" + name + "\"");
}
else
{
Builder->CreateStore(value, it->second);
}
}
void ScopeContext::setVariable(TypeInfo type, std::string name, llvm::Value *value)
{
/* When a type is given, create a new variable in this contex's scope */
std::map<std::string, llvm::Value *>::iterator it;
it = variables.find(name);
if (it == variables.end())
{
Function *parent_function = Builder->GetInsertBlock()->getParent();
IRBuilder<> temp_builder(&parent_function->getEntryBlock(), parent_function->getEntryBlock().begin());
variables[name] = temp_builder.CreateAlloca(value->getType());
types[name] = type;
Builder->CreateStore(value, variables[name]);
}
else
{
throw SyntaxErrorException("Redefinition of variable \"" + name + "\"");
}
}
Value *ScopeContext::getVariable(std::string name)
{
std::map<std::string, llvm::Value *>::iterator it;
it = variables.find(name);
if (it != variables.end())
return Builder->CreateLoad(it->second, name);
else if (parent)
return parent->getVariable(name);
throw SyntaxErrorException ("Unknown variable: " + name);
}
nanjit::TypeInfo ScopeContext::getTypeInfo(std::string name)
{
std::map<std::string, nanjit::TypeInfo>::iterator it;
it = types.find(name);
if (it != types.end())
return it->second;
else if (parent)
return parent->getTypeInfo(name);
throw SyntaxErrorException ("Unknown variable: " + name);
}
ScopeContext *ScopeContext::createChild()
{
ScopeContext *scope = new ScopeContext();
scope->Builder = Builder;
scope->Module = Module;
scope->return_type = return_type;
scope->parent = this;
return scope;
}
ASTNode::ASTNode()
{
line_number = -1;
column_number = -1;
}
void ASTNode::setLocation(int line, int col)
{
line_number = line;
/* FIXME: Bison considers the first column to be 0 */
column_number = col + 1;
}
SyntaxErrorException ASTNode::genSyntaxError(std::string e)
{
std::stringstream error_string;
error_string << "(" << line_number << ":" << column_number << "): " << e;
return SyntaxErrorException(error_string.str());
}
Value *ExprAST::codegen(ScopeContext *scope)
{
throw SyntaxErrorException ("Codegen not implemented");
}
nanjit::TypeInfo ExprAST::getResultType(ScopeContext *scope)
{
throw SyntaxErrorException("getResultType not implemented");
}
ostream& ExprAST::print(ostream& os)
{
return os << "?" << this << "?";
}
DoubleExprAST::DoubleExprAST(std::string val)
{
str_value = val;
}
Value *DoubleExprAST::codegen(ScopeContext *scope)
{
float v = atof(str_value.c_str());
cout << "Warning: Double value " << str_value << " will be truncated to float" << endl;
return ConstantFP::get(getGlobalContext(), APFloat(v));
}
nanjit::TypeInfo DoubleExprAST::getResultType(ScopeContext *scope)
{
return nanjit::TypeInfo(TypeInfo::TYPE_FLOAT);
}
ostream& DoubleExprAST::print(ostream& os)
{
return os << str_value;
}
FloatExprAST::FloatExprAST(std::string val)
{
str_value = val;
}
Value *FloatExprAST::codegen(ScopeContext *scope)
{
float v = atof(str_value.c_str());
return ConstantFP::get(getGlobalContext(), APFloat(v));
}
nanjit::TypeInfo FloatExprAST::getResultType(ScopeContext *scope)
{
return nanjit::TypeInfo(TypeInfo::TYPE_FLOAT);
}
ostream& FloatExprAST::print(ostream& os)
{
return os << str_value;
}
IntExprAST::IntExprAST(std::string val)
{
str_value = val;
}
Value *IntExprAST::codegen(ScopeContext *scope)
{
double dval = atof(str_value.c_str());
if (dval <= numeric_limits<int32_t>::max())
{
if (dval < numeric_limits<int32_t>::min())
cout << "Warning: Integer underflow, " << str_value << " can not be represented by a 32-bit value" << endl;
int32_t ival = dval;
return scope->Builder->getInt32(ival);
}
else
{
if (dval > numeric_limits<uint32_t>::max())
cout << "Warning: Integer overflow, " << str_value << " can not be represented by a 32-bit value" << endl;
uint32_t ival = dval;
return scope->Builder->getInt32(ival);
}
}
nanjit::TypeInfo IntExprAST::getResultType(ScopeContext *scope)
{
double dval = atof(str_value.c_str());
if (dval <= numeric_limits<int32_t>::max())
return nanjit::TypeInfo(TypeInfo::TYPE_INT);
return nanjit::TypeInfo(TypeInfo::TYPE_UINT);
}
ostream& IntExprAST::print(ostream& os)
{
return os << str_value;
}
Value *IdentifierExprAST::codegen(ScopeContext *scope)
{
return scope->getVariable(Name);
}
nanjit::TypeInfo IdentifierExprAST::getResultType(ScopeContext *scope)
{
return nanjit::TypeInfo(scope->getTypeInfo(Name));
}
ostream& IdentifierExprAST::print(ostream& os)
{
return os << "Identifier(" << Name << ")";
}
TypeInfo promote_types(const TypeInfo &lhs_type, const TypeInfo &rhs_type)
{
/* From C99 TC3, Section 6.3.1.1 "Conversions", paragraph 2
* The following may be used in an expression wherever an int or
* unsigned int may be used:
* - An object or expression with an integer type whose integer conversion
* rank is less than or equal to the rank of int and unsigned int.
* — A bit-field of type _Bool, int, signed int, or unsigned int.
* If an int can represent all values of the original type, the value is
* converted to an int; otherwise, it is converted to an unsigned int.
*/
TypeInfo promoted_type;
unsigned int type_width = lhs_type.getWidth();
if (lhs_type.isFloatType() || rhs_type.isFloatType())
{
promoted_type = TypeInfo(TypeInfo::TYPE_FLOAT, type_width);
}
else if (lhs_type.getBaseType() == TypeInfo::TYPE_UINT ||
lhs_type.getBaseType() == TypeInfo::TYPE_UINT)
{
promoted_type = TypeInfo(TypeInfo::TYPE_UINT, type_width);
}
else
{
promoted_type = TypeInfo(TypeInfo::TYPE_INT, type_width);
}
return promoted_type;
}
Value *BinaryExprAST::codegen(ScopeContext *scope)
{
IRBuilder<> *Builder = scope->Builder;
Value *lhs = LHS->codegen(scope);
Value *rhs = RHS->codegen(scope);
TypeInfo lhs_type = LHS->getResultType(scope);
TypeInfo rhs_type = RHS->getResultType(scope);
TypeInfo type_for_operation = promote_types(lhs_type, rhs_type);
cast_value(scope, type_for_operation, lhs_type, &lhs);
cast_value(scope, type_for_operation, rhs_type, &rhs);
if (lhs->getType() != rhs->getType())
{
std::string error;
llvm::raw_string_ostream rso(error);
rso << "Type mismatch for BinaryExprAST: ";
lhs->getType()->print(rso);
rso << " " << Op << " ";
rhs->getType()->print(rso);
throw SyntaxErrorException(rso.str());
}
if (type_for_operation.isFloatType())
{
if (Op == '+')
return Builder->CreateFAdd(lhs, rhs);
else if (Op == '-')
return Builder->CreateFSub(lhs, rhs);
else if (Op == '*')
return Builder->CreateFMul(lhs, rhs);
else if (Op == '/')
return Builder->CreateFDiv(lhs, rhs);
else
throw SyntaxErrorException ("Invalid operator for BinaryExprAST");
}
else if (type_for_operation.isIntegerType())
{
if (Op == '+')
return Builder->CreateAdd(lhs, rhs);
else if (Op == '-')
return Builder->CreateSub(lhs, rhs);
else if (Op == '*')
return Builder->CreateMul(lhs, rhs);
else if (Op == '/')
{
if (type_for_operation.isUnsignedType())
return Builder->CreateUDiv(lhs, rhs);
else
return Builder->CreateSDiv(lhs, rhs);
}
else
throw SyntaxErrorException ("Invalid operator for BinaryExprAST");
}
else
{
throw SyntaxErrorException ("Invalid type for BinaryExprAST" + type_for_operation.toStr());
}
}
nanjit::TypeInfo BinaryExprAST::getResultType(ScopeContext *scope)
{
return promote_types(LHS->getResultType(scope), RHS->getResultType(scope));
}
ostream& BinaryExprAST::print(ostream& os)
{
os << "BinaryExprAST(" << Op << " ";
LHS->print(os);
os << " ";
RHS->print(os);
os << ")";
return os;
}
Value *AssignmentExprAST::codegen(ScopeContext *scope)
{
IRBuilder<> *Builder = scope->Builder;
std::string name = LHS->getName();
TypeInfo lhs_type(TypeInfo::TYPE_VOID);
TypeInfo rhs_type = RHS->getResultType(scope);
if (LHSType.get())
lhs_type = TypeInfo(LHSType->getName());
else
lhs_type = scope->getTypeInfo(name);
Value *result = RHS->codegen(scope);
cast_value(scope, lhs_type, rhs_type, &result);
if (LHSType.get())
scope->setVariable(lhs_type, name, result);
else
scope->setVariable(name, result);
return result;
}
ostream& AssignmentExprAST::print(ostream& os)
{
os << "AssignmentExprAST(= ";
LHS->print(os);
os << " ";
RHS->print(os);
os << ")";
return os;
}
Value *ShuffleSelfAST::codegen(ScopeContext *scope)
{
IRBuilder<> *Builder = scope->Builder;
Value *lhs = Target->codegen(scope);
VectorType *lhs_type = dyn_cast<VectorType>(lhs->getType());
if (!lhs_type)
throw genSyntaxError("LHS of ShuffleSelfAST is not a vector");
std::string access = Access->getName();
if (access[0] == 's' && access.size() == 5)
{
int a, b, c, d;
int max_index = lhs_type->getNumElements() - 1;
if (4 != sscanf(access.c_str(), "s%1d%1d%1d%1d", &a, &b, &c, &d))
throw genSyntaxError("ShuffleSelfAST index invalid");
if (a > max_index || b > max_index || c > max_index || d > max_index)
throw genSyntaxError("ShuffleSelfAST index out of range");
Value *undef = UndefValue::get(lhs->getType());
Value *mask = UndefValue::get(VectorType::get(Builder->getInt32Ty(), 4));
mask = Builder->CreateInsertElement(mask, Builder->getInt32(a), Builder->getInt32(0));
mask = Builder->CreateInsertElement(mask, Builder->getInt32(b), Builder->getInt32(1));
mask = Builder->CreateInsertElement(mask, Builder->getInt32(c), Builder->getInt32(2));
mask = Builder->CreateInsertElement(mask, Builder->getInt32(d), Builder->getInt32(3));
return Builder->CreateShuffleVector(lhs, undef, mask);
throw genSyntaxError("ShuffleSelfAST");
}
else if (access[0] == 's' && access.size() == 2)
{
int a;
int max_index = lhs_type->getNumElements() - 1;
if (1 != sscanf(access.c_str(), "s%1d", &a))
throw genSyntaxError("ShuffleSelfAST index invalid");
if (a > max_index)
throw genSyntaxError("ShuffleSelfAST index out of range");
return Builder->CreateExtractElement(lhs, Builder->getInt32(a));
}
throw genSyntaxError(std::string("Invalid indexes for ShuffleSelfAST: ") + access);
}
nanjit::TypeInfo ShuffleSelfAST::getResultType(ScopeContext *scope)
{
/* FIXME: Validate */
TypeInfo in_type = Target->getResultType(scope);
int width = Access->getName().size() - 1;
return nanjit::TypeInfo(in_type.getBaseType(), width);
}
ostream& ShuffleSelfAST::print(ostream& os)
{
os << "Shuffle(";
Target->print(os);
os << " ";
Access->print(os);
os << ")";
return os;
}
CallArgListAST::CallArgListAST()
{
}
CallArgListAST::CallArgListAST(ExprAST *expr)
{
Args.insert(Args.begin(), expr);
}
void CallArgListAST::prependArg(ExprAST *expr)
{
Args.insert(Args.begin(), expr);
}
CallArgListAST::~CallArgListAST()
{
for (std::vector<ExprAST *>::iterator it=Args.begin(); it!=Args.end(); ++it)
{
delete (*it);
}
}
ostream& CallArgListAST::print(ostream& os)
{
os << "(";
bool first = true;
for (std::vector<ExprAST *>::iterator it=Args.begin(); it!=Args.end(); ++it)
{
if (!first)
os << ", ";
else
first = false;
(*it)->print(os);
}
os << ")";
return os;
}
Value *VectorConstructorAST::codegen(ScopeContext *scope)
{
IRBuilder<> *Builder = scope->Builder;
TypeInfo vector_type = TypeInfo(Type->getName());
std::vector<ExprAST *> &args = ArgList->getArgsList();
int width = vector_type.getWidth();
if (width < 2)
throw genSyntaxError(std::string("Invalid vector type: ") + Type->getName());
if (args.size() != width)
throw genSyntaxError(std::string("Invalid arguments to vector constructor for ") + Type->getName());
Value *out_value = UndefValue::get(typeinfo_get_llvm_type(vector_type));
TypeInfo element_type = TypeInfo(vector_type.getBaseType());
for (int element_index = 0; element_index < width; ++element_index)
{
ExprAST *elmement_ast = args[element_index];
Value *elmement_value = elmement_ast->codegen(scope);
cast_value(scope, element_type, elmement_ast->getResultType(scope), &elmement_value);
out_value = Builder->CreateInsertElement(out_value, elmement_value, Builder->getInt32(element_index));
}
return out_value;
}
nanjit::TypeInfo VectorConstructorAST::getResultType(ScopeContext *scope)
{
TypeInfo type = TypeInfo(Type->getName());
if (type.getWidth() < 2)
throw genSyntaxError(std::string("Invalid vector type: ") + Type->getName());
return type;
}
ostream& VectorConstructorAST::print(ostream& os)
{
os << "VectorConstructorAST(";
Type->print(os);
os << " ";
ArgList->print(os);
os << ")";
return os;
}
Value *CallAST::codegen(ScopeContext *scope)
{
std::vector<ExprAST *> &args = ArgList->getArgsList();
if (std::string("shuffle2") == Target->getName())
{
const int num_args = 3;
if (args.size() != num_args)
{
std::stringstream error_string;
error_string << "Called \"" << Target->getName() << "\" with ";
error_string << args.size() << " arguments, expected " << num_args;
throw SyntaxErrorException(error_string.str());
}
IRBuilder<> *Builder = scope->Builder;
ExprAST *vecA = args[0];
ExprAST *vecB = args[1];
std::auto_ptr<Value> mask(args[2]->codegen(scope));
/* FIXME: Validate mask size and type */
return Builder->CreateShuffleVector(vecA->codegen(scope), vecB->codegen(scope), mask.release());
}
else if (std::string("select") == Target->getName())
{
const int num_args = 3;
if (args.size() != num_args)
{
std::stringstream error_string;
error_string << "Called \"" << Target->getName() << "\" with ";
error_string << args.size() << " arguments, expected " << num_args;
throw SyntaxErrorException(error_string.str());
}
IRBuilder<> *Builder = scope->Builder;
ExprAST *vecA = args[0];
ExprAST *vecB = args[1];
ExprAST *cmp = args[2];
/* FIXME: Validate arguments */
return Builder->CreateSelect(cmp->codegen(scope), vecA->codegen(scope), vecB->codegen(scope));
}
else if (std::string("clamp") == Target->getName())
{
const int num_args = 3;
if (args.size() != num_args)
{
std::stringstream error_string;
error_string << "Called \"" << Target->getName() << "\" with ";
error_string << args.size() << " arguments, expected " << num_args;
throw SyntaxErrorException(error_string.str());
}
IRBuilder<> *Builder = scope->Builder;
Value *value = args[0]->codegen(scope);
Value *min = args[1]->codegen(scope);
Value *max = args[2]->codegen(scope);
TypeInfo value_type = args[0]->getResultType(scope);
TypeInfo min_type = args[1]->getResultType(scope);
TypeInfo max_type = args[2]->getResultType(scope);
if ((value_type.getWidth() != min_type.getWidth()) ||
(min_type.getWidth() != max_type.getWidth()))
{
std::string error;
llvm::raw_string_ostream rso(error);
rso << "Type mismatch for " << Target->getName() << " : ";
rso << value_type.toStr();
rso << " vs ";
rso << min_type.toStr();
rso << " vs ";
rso << max_type.toStr();
throw SyntaxErrorException(rso.str());
}
TypeInfo target_type = TypeInfo(TypeInfo::TYPE_FLOAT, value_type.getWidth());
cast_value(scope, target_type, value_type, &value);
cast_value(scope, target_type, min_type, &min);
cast_value(scope, target_type, max_type, &max);
Value *mask;
mask = Builder->CreateFCmpUGE(value, min);
value = Builder->CreateSelect(mask, value, min);
mask = Builder->CreateFCmpULE(value, max);
value = Builder->CreateSelect(mask, value, max);
return value;
}
else if ((std::string("min") == Target->getName()) || (std::string("max") == Target->getName()))
{
const int num_args = 2;
if (args.size() != num_args)
{
std::stringstream error_string;
error_string << "Called \"" << Target->getName() << "\" with ";
error_string << args.size() << " arguments, expected " << num_args;
throw SyntaxErrorException(error_string.str());
}
IRBuilder<> *Builder = scope->Builder;
Value *value_a = args[0]->codegen(scope);
Value *value_b = args[1]->codegen(scope);
TypeInfo a_type = args[0]->getResultType(scope);
TypeInfo b_type = args[1]->getResultType(scope);
if (a_type.getWidth() != b_type.getWidth())
{
std::string error;
llvm::raw_string_ostream rso(error);
rso << "Type mismatch for " << Target->getName() << " : ";
rso << a_type.toStr();
rso << " vs ";
rso << b_type.toStr();
throw SyntaxErrorException(rso.str());
}
TypeInfo target_type = TypeInfo(TypeInfo::TYPE_FLOAT, a_type.getWidth());
cast_value(scope, target_type, a_type, &value_a);
cast_value(scope, target_type, b_type, &value_b);
Value *mask = NULL;
if (std::string("min") == Target->getName())
mask = Builder->CreateFCmpULE(value_a, value_b);
else
mask = Builder->CreateFCmpUGE(value_a, value_b);
Value *value = Builder->CreateSelect(mask, value_a, value_b);
return value;
}
else if (std::string("sqrt") == Target->getName())
{
const int num_args = 1;
if (args.size() != num_args)
{
std::stringstream error_string;
error_string << "Called \"" << Target->getName() << "\" with ";
error_string << args.size() << " arguments, expected " << num_args;
throw SyntaxErrorException(error_string.str());
}
IRBuilder<> *Builder = scope->Builder;
Value *arg_value = args[0]->codegen(scope);
TypeInfo arg_type = args[0]->getResultType(scope);
TypeInfo call_type = TypeInfo(TypeInfo::TYPE_FLOAT, arg_type.getWidth());
cast_value(scope, call_type, arg_type, &arg_value);
vector<Value *>call_parameters;
call_parameters.push_back(arg_value);
Function *target_func = define_llvm_intrinisic(scope, "llvm.sqrt", call_type);
Value *call_result = Builder->CreateCall(target_func, call_parameters);
return call_result;
}
throw SyntaxErrorException("Call \"" + Target->getName() + "\" not implemented");
}
nanjit::TypeInfo CallAST::getResultType(ScopeContext *scope)
{
if (std::string("shuffle2") == Target->getName())
{
return ArgList->getArgsList()[0]->getResultType(scope);
}
else if (std::string("select") == Target->getName())
{
return ArgList->getArgsList()[0]->getResultType(scope);
}
else if (std::string("clamp") == Target->getName())
{
TypeInfo arg_type = ArgList->getArgsList()[0]->getResultType(scope);
return TypeInfo(TypeInfo::TYPE_FLOAT, arg_type.getWidth());
}
else if ((std::string("min") == Target->getName()) ||
(std::string("max") == Target->getName()))
{
TypeInfo arg_type = ArgList->getArgsList()[0]->getResultType(scope);
return TypeInfo(TypeInfo::TYPE_FLOAT, arg_type.getWidth());
}
else if (std::string("sqrt") == Target->getName())
{
TypeInfo arg_type = ArgList->getArgsList()[0]->getResultType(scope);
return TypeInfo(TypeInfo::TYPE_FLOAT, arg_type.getWidth());
}
throw SyntaxErrorException("Call \"" + Target->getName() + "\" result type not implemented");
}
ostream& CallAST::print(ostream& os)
{
os << "CallAST( ";
Target->print(os);
os << " ";
ArgList->print(os);
os << " )";
return os;
}
BlockAST::BlockAST(ExprAST *expr)
{
Expressions.push_front(expr);
}
void BlockAST::PrependExpr(ExprAST *expr)
{
Expressions.push_front(expr);
}
BlockAST::~BlockAST()
{
for (std::list<ExprAST *>::iterator it=Expressions.begin(); it!=Expressions.end(); ++it)
{
delete (*it);
}
}
Value *BlockAST::codegen(ScopeContext *scope)
{
Value *last = NULL;
for (std::list<ExprAST *>::iterator it = Expressions.begin();
it != Expressions.end();
++it)
{
last = (*it)->codegen(scope);
}
return last;
}
ostream& BlockAST::print(ostream& os)
{
for (std::list<ExprAST *>::iterator it=Expressions.begin(); it!=Expressions.end(); ++it)
{
os << " ";
(*it)->print(os);
os << endl;
}
return os;
}
Value *ComparisonAST::codegen(ScopeContext *scope)
{
IRBuilder<> *Builder = scope->Builder;
if ((LHS->getResultType(scope).getBaseType() != TypeInfo::TYPE_FLOAT) ||
(RHS->getResultType(scope).getBaseType() != TypeInfo::TYPE_FLOAT))
{
throw SyntaxErrorException ("Invalid type for ComparisonAST");
}
switch (Op)
{
case EqualTo:
return Builder->CreateFCmpUEQ(LHS->codegen(scope), RHS->codegen(scope));
case NotEqualTo:
return Builder->CreateFCmpUNE(LHS->codegen(scope), RHS->codegen(scope));
case GreaterThan:
return Builder->CreateFCmpUGT(LHS->codegen(scope), RHS->codegen(scope));
case GreaterThanOrEqual:
return Builder->CreateFCmpUGE(LHS->codegen(scope), RHS->codegen(scope));
case LessThan:
return Builder->CreateFCmpULT(LHS->codegen(scope), RHS->codegen(scope));
case LessThanOrEqual:
return Builder->CreateFCmpULE(LHS->codegen(scope), RHS->codegen(scope));
}
throw SyntaxErrorException("Compare op not implemented");
}
nanjit::TypeInfo ComparisonAST::getResultType(ScopeContext *scope)
{
nanjit::TypeInfo lhs_type = LHS->getResultType(scope);
return nanjit::TypeInfo(TypeInfo::TYPE_BOOL, lhs_type.getWidth());
}
ostream& ComparisonAST::print(ostream& os)
{
os << "ComparisonAST( ";
LHS->print(os);
switch (Op)
{
case EqualTo:
os << " == ";
break;
case NotEqualTo:
os << " != ";
break;
case GreaterThan:
os << " > ";
break;
case GreaterThanOrEqual:
os << " >= ";
break;
case LessThan:
os << " < ";
break;
case LessThanOrEqual:
os << " <= ";
break;
}
RHS->print(os);
os << " )";
return os;
}
Value *IfElseAST::codegen(ScopeContext *scope)
{
IRBuilder<> *builder = scope->Builder;
Function *parent_function = builder->GetInsertBlock()->getParent();
Value *comparison = Comparison->codegen(scope);
if (ElseBlock.get())
{
BasicBlock *if_block = BasicBlock::Create(builder->getContext(), "if", parent_function);
BasicBlock *else_block = BasicBlock::Create(builder->getContext(), "else", parent_function);
BasicBlock *merge_block = BasicBlock::Create(builder->getContext(), "ifcont");
BasicBlock *active_block = NULL;
builder->CreateCondBr(comparison, if_block, else_block);
builder->SetInsertPoint(if_block);
auto_ptr<ScopeContext> child_scope(scope->createChild());
IfBlock->codegen(child_scope.get());
/* Use GetInsertBlock() here because IfBlock->codegen may change the active block */
active_block = builder->GetInsertBlock();
if (active_block->empty() ||
!(isa<ReturnInst>(builder->GetInsertBlock()->back())))
{
/* Create a merge jump if the block doesn't cause a return */
builder->CreateBr(merge_block);
}
builder->SetInsertPoint(else_block);
child_scope.reset(scope->createChild());
ElseBlock->codegen(child_scope.get());
/* Use GetInsertBlock() here because ElseBlock->codegen may change the active block */
active_block = builder->GetInsertBlock();
if (active_block->empty() ||
!(isa<ReturnInst>(builder->GetInsertBlock()->back())))
{
/* Create a merge jump if the block doesn't cause a return */