-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodDeclaration.cs
More file actions
73 lines (62 loc) · 2.68 KB
/
MethodDeclaration.cs
File metadata and controls
73 lines (62 loc) · 2.68 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
namespace MiniSharpCompiler
{
using System;
using LLVMSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
public partial class LLVMIRGenerationVisitor
{
public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
{
var parameters = node.ParameterList.Parameters;
var paramCount = (uint)parameters.Count;
LLVMTypeRef[] paramTypesArr = new LLVMTypeRef[Math.Max(paramCount, 1)]; // always need 1 for deref
{
uint index = 0;
foreach (var parameter in parameters)
{
paramTypesArr[index] = this.semanticModel.GetTypeInfo(parameter.Type).LLVMTypeRef();
}
}
var returnType = this.semanticModel.GetTypeInfo(node.ReturnType).LLVMTypeRef();
var functionType = LLVM.FunctionType(returnType, out paramTypesArr[0], paramCount, False);
this.function = LLVM.AddFunction(this.module, node.Identifier.Text, functionType);
this.symbolTable.Add(node, this.function); // add to symbol table
var body = node.Body;
// extern methods
if (body == null)
{
return;
}
LLVM.PositionBuilderAtEnd(this.builder, LLVM.AppendBasicBlock(this.function, "entry"));
{
uint index = 0;
foreach (var parameter in parameters)
{
var llvmParam = LLVM.GetParam(this.function, index);
var alloca = LLVM.BuildAlloca(this.builder, paramTypesArr[index], parameter.Identifier.Text);
LLVM.BuildStore(this.builder, llvmParam, alloca);
this.symbolTable.Add(parameter, llvmParam);
// TODO: this.MarkGCRoot(llvmParam, tuple.Item2);
}
}
var localVariableVisitor = new VariableDeclarationVisitor(this.semanticModel, this.builder);
localVariableVisitor.Visit(node);
var variables = localVariableVisitor.Variables;
foreach (var entry in variables)
{
this.symbolTable.Add(entry.Key, entry.Value);
// TODO: this.MarkGCRoot(entry.Value, this.semanticModel.GetTypeInfo(parent.Type));
}
this.Visit(body);
if (this.semanticModel.GetTypeInfo(node.ReturnType).Type.SpecialType == SpecialType.System_Void)
{
LLVM.BuildRetVoid(this.builder);
}
else
{
LLVM.BuildRet(this.builder, LLVM.ConstNull(returnType));
}
}
}
}