Skip to content

Commit

Permalink
Regenerate LLVMSharp from the 6.0.0 sources, based on the ClangSharp …
Browse files Browse the repository at this point in the history
…rewrite (#93)

* Regenerate LLVMSharp from the 6.0.0 sources, based on the ClangSharp rewrite

* Manual fixups to the generated LLVMSharp code

* Rerun the generator excluding LLVMInitializeCore
  • Loading branch information
tannergooding committed Apr 29, 2019
1 parent 0534bd4 commit d9fd646
Show file tree
Hide file tree
Showing 20 changed files with 2,117 additions and 1,940 deletions.
52 changes: 10 additions & 42 deletions src/Api/DisasmContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,15 @@
using System;
using System.Text;
using Utilities;
using OpInfoCallback = System.Func<System.IntPtr, ulong, ulong, ulong, int, System.IntPtr, int>;
using SymbolLookupCallback = System.Func<System.IntPtr, ulong, ulong, System.Tuple<System.IntPtr, ulong, System.IntPtr>>;

public sealed class DisasmContext : IDisposableWrapper<LLVMDisasmContextRef>, IDisposable
{
LLVMDisasmContextRef IWrapper<LLVMDisasmContextRef>.ToHandleType => this._instance;
void IDisposableWrapper<LLVMDisasmContextRef>.MakeHandleOwner() => this._owner = true;

private class SymbolLookupClosure
{
private readonly SymbolLookupCallback _callback;

public SymbolLookupClosure(SymbolLookupCallback c)
{
this._callback = c;
}

public IntPtr Invoke(IntPtr @DisInfo, ulong @ReferenceValue, out ulong @ReferenceType, ulong @ReferencePC, out IntPtr @ReferenceName)
{
var r = this._callback(DisInfo, ReferenceValue, ReferencePC);
ReferenceType = r.Item2;
ReferenceName = r.Item3;
return r.Item1;
}
}

public static DisasmContext CreateDisasm(string tripleName, IntPtr disInfo, int tagType, OpInfoCallback getOpInfo,
SymbolLookupCallback symbolLookUp)
public static DisasmContext CreateDisasm(string tripleName, IntPtr disInfo, int tagType, LLVMOpInfoCallback opInfoCallback,
LLVMSymbolLookupCallback symbolCallback)
{
var opInfoCallback = new LLVMOpInfoCallback(getOpInfo);
var symbolCallback = new LLVMSymbolLookupCallback(new SymbolLookupClosure(symbolLookUp).Invoke);
var disasmContext =
LLVM.CreateDisasm(tripleName, disInfo, tagType, opInfoCallback, symbolCallback)
.Wrap()
Expand All @@ -42,11 +20,9 @@ public static DisasmContext CreateDisasm(string tripleName, IntPtr disInfo, int
return disasmContext;
}

public static DisasmContext CreateDisasmCPU(string triple, string cpu, IntPtr disInfo, int tagType, OpInfoCallback getOpInfo,
SymbolLookupCallback symbolLookUp)
public static DisasmContext CreateDisasmCPU(string triple, string cpu, IntPtr disInfo, int tagType, LLVMOpInfoCallback opInfoCallback,
LLVMSymbolLookupCallback symbolCallback)
{
var opInfoCallback = new LLVMOpInfoCallback(getOpInfo);
var symbolCallback = new LLVMSymbolLookupCallback(new SymbolLookupClosure(symbolLookUp).Invoke);
var disasmContext =
LLVM.CreateDisasmCPU(triple, cpu, disInfo, tagType, opInfoCallback, symbolCallback)
.Wrap()
Expand All @@ -57,11 +33,9 @@ public static DisasmContext CreateDisasmCPU(string triple, string cpu, IntPtr di
}

public static DisasmContext CreateDisasmCPUFeatures(string triple, string cpu, string features, IntPtr disInfo,
int tagType, OpInfoCallback getOpInfo,
SymbolLookupCallback symbolLookUp)
int tagType, LLVMOpInfoCallback opInfoCallback,
LLVMSymbolLookupCallback symbolCallback)
{
var opInfoCallback = new LLVMOpInfoCallback(getOpInfo);
var symbolCallback = new LLVMSymbolLookupCallback(new SymbolLookupClosure(symbolLookUp).Invoke);
var disasmContext = LLVM.CreateDisasmCPUFeatures(triple, cpu, features, disInfo, tagType, opInfoCallback, symbolCallback)
.Wrap()
.MakeHandleOwner<DisasmContext, LLVMDisasmContextRef>();
Expand Down Expand Up @@ -111,16 +85,10 @@ private void Dispose(bool disposing)

public unsafe Tuple<string, int> DisasmInstruction(byte[] instructionBytes, ulong programCounter)
{
fixed(byte* iptr = &instructionBytes[0])
{
var outputBuffer = new byte[1024];
fixed(byte* optr = &outputBuffer[0])
{
var count = LLVM.DisasmInstruction(this.Unwrap(), new IntPtr(iptr), (ulong)instructionBytes.Length, programCounter, new IntPtr(optr), new size_t(outputBuffer.Length));
var text = new UTF8Encoding().GetString(outputBuffer, 0, outputBuffer.Length);
return new Tuple<string, int>(text, count.Pointer.ToInt32());
}
}
var outputBuffer = new byte[1024];
var count = LLVM.DisasmInstruction(this.Unwrap(), out instructionBytes[0], (ulong)instructionBytes.Length, programCounter, out outputBuffer[0], (IntPtr)(outputBuffer.Length));
var text = new UTF8Encoding().GetString(outputBuffer, 0, outputBuffer.Length);
return new Tuple<string, int>(text, count.ToInt32());
}
}
}
15 changes: 10 additions & 5 deletions src/Api/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public static ExecutionEngine CreateJITCompiler(Module m, uint optLevel)
return instance.Wrap().MakeHandleOwner<ExecutionEngine, LLVMExecutionEngineRef>().AssociateWithModule(m);
}

public static ExecutionEngine CreateMCJITCompilerForModule(Module module) => CreateMCJITCompiler(module, new size_t(new IntPtr(Marshal.SizeOf(typeof(LLVMMCJITCompilerOptions)))));
public static ExecutionEngine CreateMCJITCompilerForModule(Module module) => CreateMCJITCompiler(module, Marshal.SizeOf(typeof(LLVMMCJITCompilerOptions)));

public unsafe static ExecutionEngine CreateMCJITCompiler(Module module, int optionsSize)
{
LLVMMCJITCompilerOptions options;
var size = new size_t(new IntPtr(optionsSize));
LLVM.InitializeMCJITCompilerOptions(&options, size);
if (LLVM.CreateMCJITCompilerForModule(out LLVMExecutionEngineRef instance, module.Unwrap(), &options, size, out IntPtr error).Failed())
var size = (IntPtr)(optionsSize);
LLVM.InitializeMCJITCompilerOptions(out options, size);
if (LLVM.CreateMCJITCompilerForModule(out LLVMExecutionEngineRef instance, module.Unwrap(), out options, size, out IntPtr error).Failed())
{
TextUtilities.Throw(error);
}
Expand All @@ -111,7 +111,12 @@ internal ExecutionEngine(LLVMExecutionEngineRef ee)
public void RunStaticDestructors() => LLVM.RunStaticDestructors(this.Unwrap());

public GenericValue Run(Function f, params GenericValue[] args) => LLVM.RunFunction(this.Unwrap(), f.Unwrap(), args.Unwrap()).Wrap();
public int RunAsMain(Function f, uint argC, string[] argV, string[] envP) => LLVM.RunFunctionAsMain(this.Unwrap(), f.Unwrap(), argC, argV, envP);
public int RunAsMain(Function f, uint argC, string[] argV, string[] envP)
{
var marshaledArgv = StringMarshaler.MarshalArray(argV);
var marshaledEnvp = StringMarshaler.MarshalArray(envP);
return LLVM.RunFunctionAsMain(this.Unwrap(), f.Unwrap(), argC, out marshaledArgv[0], out marshaledEnvp[0]);
}
public int RunAsMain(Function f, params string[] argV) => this.RunAsMain(f, (uint)argV.Length, argV, new string[0]);
public void FreeMachineCode(Function f) => LLVM.FreeMachineCodeForFunction(this.Unwrap(), f.Unwrap());

Expand Down
26 changes: 1 addition & 25 deletions src/Api/MCJITMemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,14 @@
{
using System;
using Utilities;
using AllocCodeSectionCallback = global::System.Func<global::System.IntPtr, global::System.IntPtr, uint, uint, string, global::System.IntPtr>;
using AllocDataSectionCallback = global::System.Func<global::System.IntPtr, global::System.IntPtr, uint, uint, string, bool, global::System.IntPtr>;
using FinalizeMemoryCallback = global::System.Func<global::System.IntPtr, global::System.Tuple<int, global::System.IntPtr>>;

public sealed class MCJITMemoryManager : IDisposableWrapper<LLVMMCJITMemoryManagerRef>, IDisposable
{
LLVMMCJITMemoryManagerRef IWrapper<LLVMMCJITMemoryManagerRef>.ToHandleType => this._instance;
void IDisposableWrapper<LLVMMCJITMemoryManagerRef>.MakeHandleOwner() => this._owner = true;

private class MemoryManagerFinalizeMemoryClosure
public static MCJITMemoryManager Create(IntPtr opaque, LLVMMemoryManagerAllocateCodeSectionCallback allocCodeSectionCallback, LLVMMemoryManagerAllocateDataSectionCallback allocDataSectionCallback, LLVMMemoryManagerFinalizeMemoryCallback finalizeMemoryCallback, LLVMMemoryManagerDestroyCallback destroyCallback)
{
private readonly FinalizeMemoryCallback _callback;

public MemoryManagerFinalizeMemoryClosure(FinalizeMemoryCallback callback)
{
this._callback = callback;
}

public int Invoke(IntPtr opaque, out IntPtr errMsg)
{
var r = _callback(opaque);
errMsg = r.Item2;
return r.Item1;
}
}

public static MCJITMemoryManager Create(IntPtr opaque, AllocCodeSectionCallback allocateCodeSection, AllocDataSectionCallback allocateDataSection, FinalizeMemoryCallback finalizeMemory, Action<IntPtr> destroy)
{
var allocCodeSectionCallback = new LLVMMemoryManagerAllocateCodeSectionCallback(allocateCodeSection);
var allocDataSectionCallback = new LLVMMemoryManagerAllocateDataSectionCallback((a, b, c, d, e, f) => allocateDataSection(a, b, c, d, e, f));
var finalizeMemoryCallback = new LLVMMemoryManagerFinalizeMemoryCallback(new MemoryManagerFinalizeMemoryClosure(finalizeMemory).Invoke);
var destroyCallback = new LLVMMemoryManagerDestroyCallback(destroy);
var memoryManager = LLVM.CreateSimpleMCJITMemoryManager(opaque, allocCodeSectionCallback, allocDataSectionCallback, finalizeMemoryCallback, destroyCallback)
.Wrap()
.MakeHandleOwner<MCJITMemoryManager, LLVMMCJITMemoryManagerRef>();
Expand Down
12 changes: 3 additions & 9 deletions src/Api/MemoryBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ public static MemoryBuffer CreateMemoryBufferWithSTDIN()

public unsafe static MemoryBuffer CreateMemoryBufferWithMemoryRange(string inputData, string bufferLength, bool requiresNullTerminator)
{
fixed(char* c = inputData)
{
return LLVM.CreateMemoryBufferWithMemoryRange(new IntPtr(c), new size_t(inputData.Length), bufferLength, requiresNullTerminator).Wrap().MakeHandleOwner<MemoryBuffer, LLVMMemoryBufferRef>();
}
return LLVM.CreateMemoryBufferWithMemoryRange(inputData, (IntPtr)(inputData.Length), bufferLength, requiresNullTerminator).Wrap().MakeHandleOwner<MemoryBuffer, LLVMMemoryBufferRef>();
}

public unsafe static MemoryBuffer CreateMemoryBufferWithMemoryRangeCopy(string inputData, string bufferName)
{
fixed(char* c = inputData)
{
return LLVM.CreateMemoryBufferWithMemoryRangeCopy(new IntPtr(c), new size_t(inputData.Length), bufferName).Wrap().MakeHandleOwner<MemoryBuffer, LLVMMemoryBufferRef>();
}
return LLVM.CreateMemoryBufferWithMemoryRangeCopy(inputData, (IntPtr)(inputData.Length), bufferName).Wrap().MakeHandleOwner<MemoryBuffer, LLVMMemoryBufferRef>();
}

private readonly LLVMMemoryBufferRef _instance;
Expand All @@ -59,7 +53,7 @@ internal MemoryBuffer(LLVMMemoryBufferRef instance)
}

public IntPtr BufferStart => LLVM.GetBufferStart(this.Unwrap());
public size_t BufferSize => LLVM.GetBufferSize(this.Unwrap());
public IntPtr BufferSize => LLVM.GetBufferSize(this.Unwrap());

public void Dispose()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Api/Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
public static class Support
{
public static bool LoadLibraryPermanently(string filename) => LLVM.LoadLibraryPermanently(filename);
public static void ParseCommandLineOptions(int argc, string[] argv, string overview) => LLVM.ParseCommandLineOptions(argc, argv, overview);
public static void ParseCommandLineOptions(int argc, string[] argv, string overview)
{
var marshaledArgv = StringMarshaler.MarshalArray(argv);
LLVM.ParseCommandLineOptions(argc, out marshaledArgv[0], overview);
}
}
}
2 changes: 1 addition & 1 deletion src/Api/Values/Constants/ConstantDataSequential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal ConstantDataSequential(LLVMValueRef instance)

public bool IsString => LLVM.IsConstantString(this.Unwrap());

public string GetAsString() => this.IsString ? LLVM.GetAsString(this.Unwrap(), out size_t @out) : throw new InvalidOperationException();
public string GetAsString() => this.IsString ? LLVM.GetAsString(this.Unwrap(), out IntPtr @out) : throw new InvalidOperationException();

public Value GetElementAsConstant(uint idx) => LLVM.GetElementAsConstant(this.Unwrap(), idx).Wrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Values/Constants/ConstantInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class ConstantInt : Constant
{
public static ConstantInt Get(IntegerType type, ulong value) => Get(type, value, false);
public static ConstantInt Get(IntegerType type, ulong value, bool signExtend) => LLVM.ConstInt(type.Unwrap(), value, signExtend).WrapAs<ConstantInt>();
public static ConstantInt Get(IntegerType type, ulong[] words) => LLVM.ConstIntOfArbitraryPrecision(type.Unwrap(), (uint)words.Length, words).WrapAs<ConstantInt>();
public static ConstantInt Get(IntegerType type, ulong[] words) => LLVM.ConstIntOfArbitraryPrecision(type.Unwrap(), (uint)words.Length, out words[0]).WrapAs<ConstantInt>();
public static ConstantInt Get(IntegerType type, string str, byte radix) => LLVM.ConstIntOfString(type.Unwrap(), str, radix).WrapAs<ConstantInt>();

internal ConstantInt(LLVMValueRef instance)
Expand Down
23 changes: 0 additions & 23 deletions src/Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,4 @@ public static implicit operator LLVMBasicBlockRef(LLVMValueRef v)
return LLVM.ValueAsBasicBlock(v);
}
}

partial struct size_t
{
public static implicit operator size_t(int b)
{
return new size_t(new IntPtr(b));
}

public static implicit operator size_t(long b)
{
return new size_t(new IntPtr(b));
}

public static implicit operator int(size_t v)
{
return v.Pointer.ToInt32();
}

public static implicit operator long(size_t b)
{
return b.Pointer.ToInt64();
}
}
}
4 changes: 3 additions & 1 deletion src/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public void RunStaticDestructors()

public int RunFunctionAsMain(LLVMValueRef @F, uint @ArgC, string[] @ArgV, string[] @EnvP)
{
return LLVM.RunFunctionAsMain(this.instance, @F, @ArgC, @ArgV, @EnvP);
var marshaledArgv = StringMarshaler.MarshalArray(ArgV);
var marshaledEnvp = StringMarshaler.MarshalArray(EnvP);
return LLVM.RunFunctionAsMain(this.instance, @F, @ArgC, out marshaledArgv[0], out marshaledEnvp[0]);
}

public LLVMGenericValueRef RunFunction(LLVMValueRef @F, LLVMGenericValueRef[] @Args)
Expand Down
56 changes: 52 additions & 4 deletions src/Generated.Custom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace LLVMSharp
using System;
using System.Runtime.InteropServices;

public partial struct size_t
public partial struct off_t
{
public size_t(IntPtr Pointer)
public off_t(int value)
{
this.Pointer = Pointer;
this.Value = value;
}

public IntPtr Pointer;
public int Value;
}

public static partial class LLVM
Expand Down Expand Up @@ -131,5 +131,53 @@ public static partial class LLVM

[DllImport(libraryPath, EntryPoint = "LLVMLinkInGC", CallingConvention = CallingConvention.Cdecl)]
public static extern void LinkInGC();

[DllImport(libraryPath, EntryPoint = "LLVMDisasmInstruction", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr DisasmInstruction(LLVMDisasmContextRef DC, out byte Bytes, ulong BytesSize, ulong PC, out byte OutString, IntPtr OutStringSize);

[DllImport(libraryPath, EntryPoint = "LLVMGetBufferStart", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetBufferStart(LLVMMemoryBufferRef MemBuf);

[DllImport(libraryPath, EntryPoint = "LLVMGetDiagInfoDescription", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetDiagInfoDescription(LLVMDiagnosticInfoRef DI);

[DllImport(libraryPath, EntryPoint = "LLVMGetDefaultTargetTriple", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetDefaultTargetTriple();

[DllImport(libraryPath, EntryPoint = "LLVMCopyStringRepOfTargetData", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CopyStringRepOfTargetData(LLVMTargetDataRef TD);

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetMachineTriple", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetMachineTriple(LLVMTargetMachineRef T);

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetMachineCPU", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetMachineCPU(LLVMTargetMachineRef T);

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetMachineFeatureString", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetMachineFeatureString(LLVMTargetMachineRef T);

[DllImport(libraryPath, EntryPoint = "LLVMPrintTypeToString", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr PrintTypeToString(LLVMTypeRef Val);

[DllImport(libraryPath, EntryPoint = "LLVMCreateMessage", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateMessage([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StringMarshaler))] string Message);

[DllImport(libraryPath, EntryPoint = "LLVMDisposeMessage", CallingConvention = CallingConvention.Cdecl)]
public static extern void DisposeMessage(IntPtr Message);

[DllImport(libraryPath, EntryPoint = "LLVMPrintModuleToString", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr PrintModuleToString(LLVMModuleRef M);

[DllImport(libraryPath, EntryPoint = "LLVMPrintValueToString", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr PrintValueToString(LLVMValueRef Val);

[DllImport(libraryPath, EntryPoint = "LLVMOrcDisposeMangledSymbol", CallingConvention = CallingConvention.Cdecl)]
public static extern void OrcDisposeMangledSymbol(IntPtr MangledSymbol);

[DllImport(libraryPath, EntryPoint = "LLVMTargetMachineEmitToFile", CallingConvention = CallingConvention.Cdecl)]
public static extern LLVMBool TargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, IntPtr Filename, LLVMCodeGenFileType codegen, out IntPtr ErrorMessage);

[DllImport(libraryPath, EntryPoint = "LLVMInitializeCore", CallingConvention = CallingConvention.Cdecl)]
public static extern void InitializeCore(LLVMPassRegistryRef R);
}
}
Loading

0 comments on commit d9fd646

Please sign in to comment.