mirror of
https://github.com/Colton1skees/Dna
synced 2026-06-21 13:42:09 +00:00
.NET structured control flow bindings
This commit is contained in:
+13
-1
@@ -691,7 +691,19 @@ LlvmUtilities.LLVMParseCommandLineOptions(new string[] { "-memdep-block-scan-lim
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
|
||||
LLVMInteropApi.Test(unicornTraceModule, ptr);
|
||||
var result = LLVMInteropApi.Test(unicornTraceModule, ptr);
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine(result.Kind);
|
||||
|
||||
var idk2 = result.ChildCount;
|
||||
var idk3 = result.EntryRegion;
|
||||
var idk4 = result.Id;
|
||||
var idk5 = result.HeadBasicBlock;
|
||||
var idk6 = result.EntryRegion;
|
||||
var idk7 = result.Owner;
|
||||
var idk1 = result.BasicBlock;
|
||||
var idk8 = result.PredecessorCount;
|
||||
var idk9 = result.SuccessorCount;
|
||||
LlvmUtilities.LLVMParseCommandLineOptions(new string[] {
|
||||
"test",
|
||||
"-memdep-block-scan-limit=10000000",
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace Dna::API {
|
||||
#define DNA_EXPORT extern "C" __declspec(dllexport)
|
||||
}
|
||||
@@ -47,6 +47,8 @@
|
||||
|
||||
#include "Utilities/magic_enum.hpp"
|
||||
|
||||
#include <API/RegionAPI/RegionAPI.h>
|
||||
|
||||
using namespace llvm::sl;
|
||||
|
||||
void DumpRegion(llvm::sl::Region* region)
|
||||
@@ -90,7 +92,7 @@ void DumpStructuredFunction(const llvm::sl::StructuredFunction& sfunc)
|
||||
DumpRegion(rootRegion);
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) void OptimizeModule(llvm::Module * module, Dna::Passes::tReadBinaryContents readBinaryContents)
|
||||
extern "C" __declspec(dllexport) llvm::sl::Region* OptimizeModule(llvm::Module * module, Dna::Passes::tReadBinaryContents readBinaryContents)
|
||||
{
|
||||
printf("received llvm module.");
|
||||
|
||||
@@ -200,4 +202,23 @@ extern "C" __declspec(dllexport) void OptimizeModule(llvm::Module * module, Dna:
|
||||
|
||||
printf("got structured function.");
|
||||
DumpStructuredFunction(*structuredFunction);
|
||||
|
||||
auto root = structuredFunction->getBody();
|
||||
|
||||
auto kind = root->get_kind();
|
||||
|
||||
auto str = magic_enum::enum_name(kind);
|
||||
|
||||
printf("foo \n");
|
||||
std::printf(
|
||||
"%.*s",
|
||||
static_cast<int>(str.length()),
|
||||
str.data());
|
||||
|
||||
|
||||
auto childCt = root->getChildSize();
|
||||
|
||||
printf("The value of a : %zu", childCt);
|
||||
|
||||
return root;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "Passes/generator_jit_sl_function.h"
|
||||
#include "API/ExportDef.h"
|
||||
#include "Utilities/magic_enum.hpp"
|
||||
using namespace llvm::sl;
|
||||
|
||||
namespace Dna::API {
|
||||
extern "C" __declspec(dllexport) uint32_t RegionGetKind(Region* region)
|
||||
{
|
||||
return region->get_kind();
|
||||
}
|
||||
|
||||
DNA_EXPORT Region* RegionGetOwnerRegion(Region* region)
|
||||
{
|
||||
return region->getOwnerRegion();
|
||||
}
|
||||
|
||||
DNA_EXPORT const Region* RegionGetEntryRegionBlock(Region* region)
|
||||
{
|
||||
return region->getEntryRegionBlock();
|
||||
}
|
||||
|
||||
DNA_EXPORT size_t RegionGetId(Region* region)
|
||||
{
|
||||
return region->get_id();
|
||||
}
|
||||
|
||||
DNA_EXPORT size_t RegionGetPredCount(Region* region)
|
||||
{
|
||||
return region->pred_size();
|
||||
}
|
||||
|
||||
DNA_EXPORT Region* RegionGetPred(Region* region, size_t index)
|
||||
{
|
||||
return region->get_pred(index);
|
||||
}
|
||||
|
||||
DNA_EXPORT size_t RegionGetSuccCount(Region* region)
|
||||
{
|
||||
return region->succ_size();
|
||||
}
|
||||
|
||||
DNA_EXPORT Region* RegionGetSucc(Region* region, size_t index)
|
||||
{
|
||||
return region->get_succ(index);
|
||||
}
|
||||
|
||||
DNA_EXPORT llvm::BasicBlock* RegionGetLLVMBasicBlock(Region* region)
|
||||
{
|
||||
return region->get_bb();
|
||||
}
|
||||
|
||||
DNA_EXPORT llvm::BasicBlock* RegionGetHeadLLVMBasicBlock(Region* region)
|
||||
{
|
||||
return region->get_head_bb();
|
||||
}
|
||||
|
||||
DNA_EXPORT size_t RegionGetChildCount(Region* region)
|
||||
{
|
||||
region->getChildSize();
|
||||
}
|
||||
|
||||
DNA_EXPORT Region* RegionGetChild(Region* region, size_t index)
|
||||
{
|
||||
return region->getChild(index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Passes/generator_jit_sl_function.h"
|
||||
#include "API/ExportDef.h"
|
||||
using namespace llvm::sl;
|
||||
|
||||
namespace Dna::API {
|
||||
DNA_EXPORT Region* ComplexRegionGetHead(RegionComplex* region)
|
||||
{
|
||||
return region->getHead();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Passes/generator_jit_sl_function.h"
|
||||
#include "API/ExportDef.h"
|
||||
using namespace llvm::sl;
|
||||
|
||||
namespace Dna::API {
|
||||
DNA_EXPORT Region* IfThenElseRegionGetElse(RegionIfThenElse* region)
|
||||
{
|
||||
return region->getElse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "Passes/generator_jit_sl_function.h"
|
||||
#include "API/ExportDef.h"
|
||||
using namespace llvm::sl;
|
||||
|
||||
namespace Dna::API {
|
||||
DNA_EXPORT Region* IfThenRegionGetThen(RegionIfThen* region)
|
||||
{
|
||||
return region->getThen();
|
||||
}
|
||||
|
||||
DNA_EXPORT bool IfThenRegionGetIsNegated(RegionIfThen* region)
|
||||
{
|
||||
return region->isNegated();
|
||||
}
|
||||
|
||||
DNA_EXPORT llvm::Instruction* IfThenRegionGetLLVMTerminatorInstruction(RegionIfThen* region)
|
||||
{
|
||||
return region->get_terminator_inst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once`
|
||||
|
||||
#include "BaseRegionAPI.h"
|
||||
#include "ComplexRegionAPI.h"
|
||||
#include "IfThenRegionAPI.h"
|
||||
#include "IfThenElseRegionAPI.h"
|
||||
#include "ReturnRegionAPI.h"
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Passes/generator_jit_sl_function.h"
|
||||
#include "API/ExportDef.h"
|
||||
using namespace llvm::sl;
|
||||
|
||||
namespace Dna::API {
|
||||
DNA_EXPORT llvm::ReturnInst* ReturnRegionGetLLVMReturnInstruction(RegionReturn* region)
|
||||
{
|
||||
return region->get_return_inst();
|
||||
}
|
||||
}
|
||||
@@ -157,8 +157,16 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="API\ExportDef.h" />
|
||||
<ClInclude Include="API\ExportedAPI.h" />
|
||||
<ClInclude Include="API\OptimizationAPI\OptimizationAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\BaseRegionAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\ComplexRegionAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\IfThenElseRegionAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\IfThenRegionAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\RegionAPI.h" />
|
||||
<ClInclude Include="API\RegionAPI\ReturnRegionAPI.h" />
|
||||
<ClInclude Include="Arch\X86\X86Registers.h" />
|
||||
<ClInclude Include="ExportedApi.h" />
|
||||
<ClInclude Include="Passes\ClassifyingAliasAnalysisPass.h" />
|
||||
<ClInclude Include="Passes\compilercore_assert.h" />
|
||||
<ClInclude Include="Passes\ConstantConcretizationPass.h" />
|
||||
|
||||
@@ -16,9 +16,6 @@
|
||||
<ClInclude Include="Arch\X86\X86Registers.h">
|
||||
<Filter>Arch\X86</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ExportedApi.h">
|
||||
<Filter>API</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Passes\ConstantConcretizationPass.h">
|
||||
<Filter>Passes</Filter>
|
||||
</ClInclude>
|
||||
@@ -52,6 +49,33 @@
|
||||
<ClInclude Include="Utilities\magic_enum_switch.hpp">
|
||||
<Filter>Utilities</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\ExportedAPI.h">
|
||||
<Filter>API</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\BaseRegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\ComplexRegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\IfThenElseRegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\IfThenRegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\ReturnRegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\OptimizationAPI\OptimizationAPI.h">
|
||||
<Filter>API\OptimizationAPI</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\ExportDef.h">
|
||||
<Filter>API</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="API\RegionAPI\RegionAPI.h">
|
||||
<Filter>API\RegionAPI</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dllmain.cpp" />
|
||||
@@ -93,6 +117,12 @@
|
||||
<Filter Include="Utilities">
|
||||
<UniqueIdentifier>{dc5b4ba6-84b8-49f6-b289-87600b5945b1}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="API\RegionAPI">
|
||||
<UniqueIdentifier>{06c71f36-bfee-4518-822e-152c6ef0275b}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="API\OptimizationAPI">
|
||||
<UniqueIdentifier>{a9052d1b-7687-4796-96e4-3b6df6841c81}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\llvm-15.0.3-win64\bin\LLVM-C.dll" />
|
||||
|
||||
@@ -744,6 +744,8 @@ StructuredFunction::StructuredFunction(
|
||||
// Destructor.
|
||||
StructuredFunction::~StructuredFunction()
|
||||
{
|
||||
printf("destructor.");
|
||||
return;
|
||||
while (!m_region_list.empty()) {
|
||||
Region *n = m_region_list.front();
|
||||
m_region_list.pop_front();
|
||||
@@ -1507,6 +1509,7 @@ StructuredControlFlowPass::StructuredControlFlowPass()
|
||||
// Destructor.
|
||||
StructuredControlFlowPass::~StructuredControlFlowPass()
|
||||
{
|
||||
return;
|
||||
for (auto it : m_structured_function_map) {
|
||||
delete it.second;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,10 @@ protected:
|
||||
}
|
||||
|
||||
/// Destructor.
|
||||
virtual ~Region() {}
|
||||
virtual ~Region()
|
||||
{
|
||||
printf("destroyed.");
|
||||
}
|
||||
|
||||
public:
|
||||
/// Get the kind of the region.
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
#include <llvm/Transforms/Scalar.h>
|
||||
#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
|
||||
#include "llvm/Transforms/Utils/LowerSwitch.h"
|
||||
#include <ExportedApi.h>
|
||||
#include <API/ExportedApi.h>
|
||||
#include "Passes/ClassifyingAliasAnalysisPass.h"
|
||||
#include <Windows.h>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using LLVMSharp.Interop;
|
||||
using Dna.LLVMInterop.Native;
|
||||
using Dna.LLVMInterop.Wrapper;
|
||||
using LLVMSharp.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -11,9 +13,11 @@ namespace Dna.LLVMInterop
|
||||
{
|
||||
public static class LLVMInteropApi
|
||||
{
|
||||
public unsafe static void Test(LLVMModuleRef module, IntPtr readBinaryContents)
|
||||
public unsafe static Region Test(LLVMModuleRef module, IntPtr readBinaryContents)
|
||||
{
|
||||
NativeLLVMInterop.VerifyModule(module, readBinaryContents);
|
||||
nint ptr = NativeLLVMInterop.OptimizeModule(module, readBinaryContents);
|
||||
|
||||
return Region.CreateRegion(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using ELFSharp.MachO;
|
||||
using LLVMSharp.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Native
|
||||
{
|
||||
public class NativeComplexRegionApi
|
||||
{
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ComplexRegionGetHead")]
|
||||
public unsafe static extern nint ComplexRegionGetHead(nint region);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using ELFSharp.MachO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Native
|
||||
{
|
||||
public static class NativeIfThenElseRegionApi
|
||||
{
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "IfThenElseRegionGetElse")]
|
||||
public unsafe static extern nint IfThenElseRegionGetElse(nint region);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using ELFSharp.MachO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Native
|
||||
{
|
||||
public class NativeIfThenRegionApi
|
||||
{
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "IfThenRegionGetThen")]
|
||||
public unsafe static extern nint IfThenRegionGetThen(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "IfThenRegionGetIsNegated")]
|
||||
public unsafe static extern bool IfThenRegionGetIsNegated(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "IfThenRegionGetLLVMTerminatorInstruction")]
|
||||
public unsafe static extern nint IfThenRegionGetLLVMTerminatorInstruction(nint region);
|
||||
}
|
||||
}
|
||||
@@ -72,36 +72,36 @@ namespace Dna.LLVMInterop.Native
|
||||
public unsafe static extern RegionKind RegionGetKind(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetOwnerRegion")]
|
||||
public unsafe static extern bool GetOwnerRegion(nint region, out nint ownerRegion);
|
||||
public unsafe static extern nint RegionGetOwnerRegion(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetEntryRegionBlock")]
|
||||
public unsafe static extern nint RegionGetEntryRegionBlock(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetId")]
|
||||
public unsafe static extern ulong RegionGetId(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetPredCount")]
|
||||
public unsafe static extern ulong RegionGetPredCount(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
public unsafe static extern nint RegionGetPred(nint region);
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetPred")]
|
||||
public unsafe static extern nint RegionGetPred(nint region, ulong index);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetSuccCount")]
|
||||
public unsafe static extern ulong RegionGetSuccCount(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
public unsafe static extern nint RegionGetSucc(nint region);
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetSucc")]
|
||||
public unsafe static extern nint RegionGetSucc(nint region, ulong index);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetLLVMBasicBlock")]
|
||||
public unsafe static extern nint RegionGetLLVMBasicBlock(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetHeadLLVMBasicBlock")]
|
||||
public unsafe static extern nint RegionGetHeadLLVMBasicBlock(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetChildCount")]
|
||||
public unsafe static extern ulong RegionGetChildCount(nint region);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetKind")]
|
||||
public unsafe static extern nint RegionGetChild(nint region);
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "RegionGetChild")]
|
||||
public unsafe static extern nint RegionGetChild(nint region, ulong id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using ELFSharp.MachO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Native
|
||||
{
|
||||
public class NativeReturnRegionApi
|
||||
{
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReturnRegionGetLLVMReturnInstruction")]
|
||||
public unsafe static extern nint ReturnRegionGetLLVMReturnInstruction(nint region);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace Dna.LLVMInterop
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate ulong dgReadBinaryContents(ulong rva, uint size);
|
||||
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl, EntryPoint = "OptimizeModule")]
|
||||
public unsafe static extern void VerifyModule(LLVMOpaqueModule* M, IntPtr readBinaryContents);
|
||||
[DllImport("Dna.LLVMInterop", CallingConvention = CallingConvention.Cdecl)]
|
||||
public unsafe static extern IntPtr OptimizeModule(LLVMOpaqueModule* M, IntPtr readBinaryContents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class BlockRegion : Region
|
||||
{
|
||||
public BlockRegion(nint handle) : base(handle)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class BreakRegion : ComplexRegion
|
||||
{
|
||||
public BreakRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Dna.LLVMInterop.Native;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public abstract unsafe class ComplexRegion : Region
|
||||
{
|
||||
public Region Head => NativeComplexRegionApi.ComplexRegionGetHead(handle);
|
||||
|
||||
public ComplexRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static implicit operator ComplexRegion(nint value) => (ComplexRegion)(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class ContinueRegion : Region
|
||||
{
|
||||
public ContinueRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Dna.LLVMInterop.Native;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class IfThenElseRegion : IfThenRegion
|
||||
{
|
||||
public Region Else => NativeIfThenElseRegionApi.IfThenElseRegionGetElse(handle);
|
||||
|
||||
public IfThenElseRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Dna.LLVMInterop.Native;
|
||||
using LLVMSharp.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class IfThenRegion : ComplexRegion
|
||||
{
|
||||
public Region Then => NativeIfThenRegionApi.IfThenRegionGetThen(handle);
|
||||
|
||||
public bool IsNegated => NativeIfThenRegionApi.IfThenRegionGetIsNegated(handle);
|
||||
|
||||
public LLVMValueRef TerminatorInstruction => new LLVMValueRef(NativeIfThenRegionApi.IfThenRegionGetLLVMTerminatorInstruction(handle));
|
||||
|
||||
public IfThenRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class NaturalLoopRegion : ComplexRegion
|
||||
{
|
||||
public NaturalLoopRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using Dna.LLVMInterop.Native;
|
||||
using LLVMSharp.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public unsafe abstract class Region : IEquatable<Region>
|
||||
{
|
||||
protected readonly nint handle;
|
||||
|
||||
public RegionKind Kind => NativeRegionApi.RegionGetKind(handle);
|
||||
|
||||
public ComplexRegion? Owner
|
||||
{
|
||||
get
|
||||
{
|
||||
var regionPtr = NativeRegionApi.RegionGetOwnerRegion(handle);
|
||||
return regionPtr == nint.Zero ? null : (ComplexRegion)CreateRegion(regionPtr);
|
||||
}
|
||||
}
|
||||
|
||||
public Region EntryRegion => NativeRegionApi.RegionGetEntryRegionBlock(handle);
|
||||
|
||||
public ulong Id => NativeRegionApi.RegionGetId(handle);
|
||||
|
||||
public ulong PredecessorCount => NativeRegionApi.RegionGetPredCount(handle);
|
||||
|
||||
public IEnumerable<Region> Predecessors => GetPredecessors();
|
||||
|
||||
public ulong SuccessorCount => NativeRegionApi.RegionGetPredCount(handle);
|
||||
|
||||
public IEnumerable<Region> Successors => GetSuccessors();
|
||||
|
||||
public LLVMBasicBlockRef BasicBlock => new LLVMBasicBlockRef(NativeRegionApi.RegionGetLLVMBasicBlock(handle));
|
||||
|
||||
public LLVMBasicBlockRef HeadBasicBlock => new LLVMBasicBlockRef(NativeRegionApi.RegionGetHeadLLVMBasicBlock(handle));
|
||||
|
||||
public ulong ChildCount => NativeRegionApi.RegionGetChildCount(handle);
|
||||
|
||||
public Region(nint handle)
|
||||
{
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
private IEnumerable<Region> GetPredecessors()
|
||||
{
|
||||
for (ulong i = 0; i < PredecessorCount; i++)
|
||||
{
|
||||
yield return GetPredecessor(i);
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public Region GetPredecessor(ulong index)
|
||||
{
|
||||
return NativeRegionApi.RegionGetPred(handle, index);
|
||||
}
|
||||
|
||||
private IEnumerable<Region> GetSuccessors()
|
||||
{
|
||||
for (ulong i = 0; i < SuccessorCount; i++)
|
||||
{
|
||||
yield return GetSuccessor(i);
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public Region GetSuccessor(ulong index)
|
||||
{
|
||||
return NativeRegionApi.RegionGetSucc(handle, index);
|
||||
}
|
||||
|
||||
private IEnumerable<Region> GetChildren()
|
||||
{
|
||||
for (ulong i = 0; i < ChildCount; i++)
|
||||
{
|
||||
yield return GetChild(i);
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public Region GetChild(ulong index)
|
||||
{
|
||||
return NativeRegionApi.RegionGetChild(handle, index);
|
||||
}
|
||||
|
||||
public static Region CreateRegion(nint ptr)
|
||||
{
|
||||
var kind = NativeRegionApi.RegionGetKind(ptr);
|
||||
|
||||
return kind switch
|
||||
{
|
||||
RegionKind.Block => new BlockRegion(ptr),
|
||||
RegionKind.Sequence => new SequenceRegion(ptr),
|
||||
RegionKind.IfThen => new IfThenRegion(ptr),
|
||||
RegionKind.IfThenElse => new IfThenElseRegion(ptr),
|
||||
RegionKind.NaturalLoop => new NaturalLoopRegion(ptr),
|
||||
RegionKind.Break => new BreakRegion(ptr),
|
||||
RegionKind.Continue => new ContinueRegion(ptr),
|
||||
RegionKind.Return => new ReturnRegion(ptr),
|
||||
RegionKind.Switch => new SwitchRegion(ptr),
|
||||
_ => throw new InvalidOperationException($"Region kind {kind} is not supported."),
|
||||
};
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj) => (obj is Region other) && Equals(other);
|
||||
|
||||
public bool Equals(Region other) => this == other;
|
||||
|
||||
public override int GetHashCode() => handle.GetHashCode();
|
||||
|
||||
public static bool operator ==(Region left, Region right) => left.handle == right.handle;
|
||||
|
||||
public static bool operator !=(Region left, Region right) => !(left == right);
|
||||
|
||||
public static implicit operator Region(nint value) => CreateRegion(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Dna.LLVMInterop.Native;
|
||||
using LLVMSharp.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public unsafe class ReturnRegion : ComplexRegion
|
||||
{
|
||||
public LLVMValueRef ReturnInstruction => new LLVMValueRef(NativeReturnRegionApi.ReturnRegionGetLLVMReturnInstruction(handle));
|
||||
|
||||
public ReturnRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
internal class SequenceRegion : ComplexRegion
|
||||
{
|
||||
public SequenceRegion(nint handle) : base(handle)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dna.LLVMInterop.Wrapper
|
||||
{
|
||||
public class SwitchRegion : ComplexRegion
|
||||
{
|
||||
public SwitchRegion(nint handle) : base(handle)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user