-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryExpression.cs
More file actions
351 lines (318 loc) · 15.1 KB
/
BinaryExpression.cs
File metadata and controls
351 lines (318 loc) · 15.1 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
namespace MiniSharpCompiler
{
using System;
using LLVMSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
public partial class LLVMIRGenerationVisitor
{
public override void VisitBinaryExpression(BinaryExpressionSyntax node)
{
SyntaxKind kind = node.Kind();
switch (kind)
{
case SyntaxKind.AddExpression:
this.AddExpression(node, node.Left, node.Right);
break;
case SyntaxKind.SubtractExpression:
this.SubExpression(node, node.Left, node.Right);
break;
case SyntaxKind.MultiplyExpression:
this.MulExpression(node, node.Left, node.Right);
break;
case SyntaxKind.DivideExpression:
this.DivExpression(node, node.Left, node.Right);
break;
case SyntaxKind.ModuloExpression:
this.ModExpression(node, node.Left, node.Right);
break;
case SyntaxKind.LeftShiftExpression:
this.ShlExpression(node, node.Left, node.Right);
break;
case SyntaxKind.RightShiftExpression:
this.ShrExpression(node, node.Left, node.Right);
break;
case SyntaxKind.BitwiseAndExpression:
this.BitAndExpression(node, node.Left, node.Right);
break;
case SyntaxKind.BitwiseOrExpression:
this.BitOrExpression(node, node.Left, node.Right);
break;
case SyntaxKind.ExclusiveOrExpression:
this.XorExpression(node, node.Left, node.Right);
break;
case SyntaxKind.LogicalAndExpression:
this.ConditionalAndExpression(node);
break;
case SyntaxKind.LogicalOrExpression:
this.ConditionalOrExpression(node);
break;
case SyntaxKind.EqualsExpression:
case SyntaxKind.NotEqualsExpression:
case SyntaxKind.LessThanExpression:
case SyntaxKind.LessThanOrEqualExpression:
case SyntaxKind.GreaterThanExpression:
case SyntaxKind.GreaterThanOrEqualExpression:
this.RelEqExpression(node);
break;
default:
throw new Exception("Unreachable");
}
}
private void AddExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMAdd, "add", visit);
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
this.BinOp(node, left, right, LLVMOpcode.LLVMFAdd, "fadd", visit);
break;
case SpecialType.System_String:
throw new NotImplementedException("String concatenation is not implemented");
}
}
private void SubExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMSub, "sub", visit);
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
this.BinOp(node, left, right, LLVMOpcode.LLVMFSub, "fsub", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void MulExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMMul, "mul", visit);
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
this.BinOp(node, left, right, LLVMOpcode.LLVMFMul, "fmul", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void DivExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_Int64:
this.BinOp(node, left, right, LLVMOpcode.LLVMSDiv, "sdiv", visit);
break;
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMUDiv, "udiv", visit);
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
this.BinOp(node, left, right, LLVMOpcode.LLVMFDiv, "fdiv", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void ModExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_Int64:
this.BinOp(node, left, right, LLVMOpcode.LLVMSRem, "srem", visit);
break;
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMURem, "urem", visit);
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
this.BinOp(node, left, right, LLVMOpcode.LLVMFRem, "frem", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void ShlExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMShl, "shl", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void ShrExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var leftNodeType = this.semanticModel.GetTypeInfo(left).Type;
switch (leftNodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_Int64:
this.BinOp(node, left, right, LLVMOpcode.LLVMAShr, "ashr", visit);
break;
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMLShr, "lshr", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void BitAndExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMAnd, "and", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void BitOrExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMOr, "or", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void XorExpression(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, bool visit = true)
{
var nodeType = this.semanticModel.GetTypeInfo(node).Type;
switch (nodeType.SpecialType)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
this.BinOp(node, left, right, LLVMOpcode.LLVMXor, "xor", visit);
break;
default:
throw new NotImplementedException("Operator overloading not yet implemented");
}
}
private void ConditionalAndExpression(BinaryExpressionSyntax node)
{
LLVMBasicBlockRef incoming = LLVM.GetInsertBlock(this.builder);
LLVMBasicBlockRef landRhs = LLVM.AppendBasicBlock(this.function, "lor.rhs");
LLVMBasicBlockRef landEnd = LLVM.AppendBasicBlock(this.function, "lor.end");
var lhs = this.Pop(node.Left);
LLVM.BuildCondBr(this.builder, lhs, landEnd, landRhs);
LLVM.PositionBuilderAtEnd(this.builder, landRhs);
var rhs = this.Pop(node.Right);
LLVM.BuildBr(this.builder, landEnd);
LLVM.PositionBuilderAtEnd(this.builder, landEnd);
var phi = LLVM.BuildPhi(this.builder, LLVM.Int1Type(), "phi");
var falseValue = LLVM.ConstInt(LLVM.Int1Type(), 1, False);
LLVM.AddIncoming(phi, out falseValue, out incoming, 1);
LLVM.AddIncoming(phi, out rhs, out landRhs, 1);
LLVM.PositionBuilderAtEnd(this.builder, landEnd);
this.Push(node, phi);
}
private void ConditionalOrExpression(BinaryExpressionSyntax node)
{
LLVMBasicBlockRef incoming = LLVM.GetInsertBlock(this.builder);
LLVMBasicBlockRef lorRhs = LLVM.AppendBasicBlock(this.function, "lor.rhs");
LLVMBasicBlockRef lorEnd = LLVM.AppendBasicBlock(this.function, "lor.end");
var lhs = this.Pop(node.Left);
LLVM.BuildCondBr(this.builder, lhs, lorEnd, lorRhs);
LLVM.PositionBuilderAtEnd(this.builder, lorRhs);
var rhs = this.Pop(node.Right);
LLVM.BuildBr(this.builder, lorEnd);
LLVM.PositionBuilderAtEnd(this.builder, lorEnd);
var phi = LLVM.BuildPhi(this.builder, LLVM.Int1Type(), "phi");
var trueValue = LLVM.ConstInt(LLVM.Int1Type(), 1, False);
LLVM.AddIncoming(phi, out trueValue, out incoming, 1);
LLVM.AddIncoming(phi, out rhs, out lorRhs, 1);
LLVM.PositionBuilderAtEnd(this.builder, lorEnd);
this.Push(node, phi);
}
/// <summary>
/// TODO: Pointers, Strings, IntPtr, Object type?
/// </summary>
private void RelEqExpression(BinaryExpressionSyntax node)
{
var left = this.semanticModel.GetTypeInfo(node.Left);
var right = this.semanticModel.GetTypeInfo(node.Right);
if (!left.ConvertedType.Equals(right.ConvertedType))
{
throw new Exception("Unreachable");
}
var type = left.ConvertedType.SpecialType;
LLVMValueRef operand;
switch (type)
{
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
operand = LLVM.BuildICmp(this.builder, TypeSystem.IntPredicate(type, node.Kind()), this.Pop(node.Left), this.Pop(node.Right), "cmp");
break;
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Decimal:
operand = LLVM.BuildFCmp(this.builder, TypeSystem.RealPredicate(node.Kind()), this.Pop(node.Left), this.Pop(node.Right), "fcmp");
break;
default:
throw new NotImplementedException("Operator overloading is not yet implemented");
}
this.Push(node, operand);
}
private void BinOp(ExpressionSyntax node, ExpressionSyntax left, ExpressionSyntax right, LLVMOpcode opcode, string name, bool visit = true)
{
var lhs = visit ? this.Pop(left) : this.OnlyPop(left);
var rhs = visit ? this.Pop(right) : this.OnlyPop(right);
this.Push(node, LLVM.BuildBinOp(builder, opcode, lhs, rhs, name));
}
}
}