public void Generate(AssemblyLoader assemblyLoader, CodeGenerator codeGenerator) { MethodBodyLoader bodyLoader = new MethodBodyLoader(assemblyLoader, codeGenerator); BlockRegion baseRegion = bodyLoader.LoadBody(this, _body); LLVMTypeRef type = GetMethodType(codeGenerator); LLVMValueRef function = codeGenerator.GetFunction(MangledName, type); LLVM.SetLinkage(function, LLVMLinkage.LLVMExternalLinkage); FunctionGenerator functionGenerator = new FunctionGenerator(codeGenerator, this, function); functionGenerator.CreateBlock("entry", null); functionGenerator.Locals = new LLVMValueRef[_body.Variables.Count]; foreach (VariableDefinition variable in _body.Variables) { string name = "local_" + (string.IsNullOrEmpty(variable.Name) ? variable.Index.ToString() : variable.Name); LLVMTypeRef localType = assemblyLoader.ResolveType(variable.VariableType).GetStackType() .GetType(codeGenerator); functionGenerator.Locals[variable.Index] = LLVM.BuildAlloca(codeGenerator.Builder, localType, name); } foreach (Block block in bodyLoader.Blocks) { functionGenerator.CreateBlock(block, codeGenerator); } baseRegion.Generate(codeGenerator, functionGenerator); functionGenerator.SelectBlock("entry"); functionGenerator.Branch(bodyLoader.GetFirstBlock(), null); }
public void Generate(CodeGenerator codeGenerator, FunctionGenerator functionGenerator) { functionGenerator.SelectBlock(this); Node node = FirstNode; while (node != null) { node.Generate(codeGenerator, functionGenerator); node = node.NextNode?.Node; } }