-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteralExpression.cs
More file actions
69 lines (66 loc) · 3.15 KB
/
LiteralExpression.cs
File metadata and controls
69 lines (66 loc) · 3.15 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
namespace MiniSharpCompiler
{
using System;
using LLVMSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
public partial class LLVMIRGenerationVisitor
{
/// <summary>
/// Section 2.4.4 Literals
/// </summary>
public override void VisitLiteralExpression(LiteralExpressionSyntax node)
{
SyntaxKind kind = node.Kind();
TypeInfo typeInfo = this.semanticModel.GetTypeInfo(node);
LLVMTypeRef llvmTypeRef = typeInfo.LLVMTypeRef();
LLVMValueRef operand;
switch (kind)
{
case SyntaxKind.TrueLiteralExpression:
operand = LLVM.ConstInt(TypeSystem.Int1Type, N: 1, SignExtend: False);
break;
case SyntaxKind.FalseLiteralExpression:
operand = LLVM.ConstInt(TypeSystem.Int1Type, N: 0, SignExtend: False);
break;
case SyntaxKind.CharacterLiteralExpression:
operand = LLVM.ConstInt(TypeSystem.Int16Type, (char) node.Token.Value, SignExtend: False);
break;
case SyntaxKind.NullLiteralExpression:
operand = LLVM.ConstNull(TypeSystem.NullType);
break;
case SyntaxKind.NumericLiteralExpression:
switch (typeInfo.Type.SpecialType)
{
case SpecialType.System_Int32: // var x = 1;
operand = LLVM.ConstInt(llvmTypeRef, (ulong) ((int) node.Token.Value), SignExtend: True);
break;
case SpecialType.System_UInt32: // var x = 1U;
operand = LLVM.ConstInt(llvmTypeRef, (uint) node.Token.Value, SignExtend: False);
break;
case SpecialType.System_Int64: // var x = 1L;
operand = LLVM.ConstInt(llvmTypeRef, (ulong) ((long) node.Token.Value), SignExtend: True);
break;
case SpecialType.System_UInt64: // var x = 1UL;
operand = LLVM.ConstInt(llvmTypeRef, (ulong) node.Token.Value, SignExtend: False);
break;
case SpecialType.System_Single: // var x = 1F;
operand = LLVM.ConstReal(llvmTypeRef, (float) node.Token.Value);
break;
case SpecialType.System_Double: // var x = 1D;
operand = LLVM.ConstReal(llvmTypeRef, (double) node.Token.Value);
break;
default:
throw new Exception("Unreachable");
}
break;
case SyntaxKind.StringLiteralExpression:
throw new NotImplementedException("Strings are not yet supported");
default:
throw new Exception("Unreachable");
}
this.Push(node, operand);
}
}
}