Skip to content

Commit 20f2ad9

Browse files
authored
Usage clarifications and updates. (#288)
1 parent f0c0e36 commit 20f2ad9

File tree

15 files changed

+254
-202
lines changed

15 files changed

+254
-202
lines changed

src/Interop/Ubiquity.NET.Llvm.Interop/ABI/StringMarshaling/LLVMErrorRef.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public LLVMErrorRef( nint handle )
4848
/// <summary>Gets a value indicating whether this instance represents a failure</summary>
4949
public bool Failed => !Success;
5050

51-
/// <inheritdoc/>
51+
/// <summary>Gets a string representation of the error message</summary>
52+
/// <returns>String representation of the error message</returns>
5253
public override string ToString( )
5354
{
5455
return LazyMessage.Value?.ToString() ?? string.Empty;

src/Ubiquity.NET.Llvm.Tests/CallTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88

9+
using Ubiquity.NET.Llvm.DebugInfo;
910
using Ubiquity.NET.Llvm.Instructions;
1011
using Ubiquity.NET.Llvm.Values;
1112

@@ -43,5 +44,27 @@ public void Create_varargs_function_with_arbitrary_params_succeeds( )
4344
Assert.AreEqual( 1UL, callInstruction.Operands.GetOperand<ConstantInt>( 1 )!.ZeroExtendedValue );
4445
Assert.AreEqual( 2UL, callInstruction.Operands.GetOperand<ConstantInt>( 2 )!.ZeroExtendedValue );
4546
}
47+
48+
[TestMethod]
49+
[TestCategory( "Instruction" )]
50+
public void Create_indirect_function_call_succeeds( )
51+
{
52+
using var ctx = new Context();
53+
using var module = ctx.CreateBitcodeModule();
54+
var voidType = module.Context.VoidType;
55+
var voidPtrType = voidType.CreatePointerType();
56+
57+
// Func sig: void (*)(int32_t, double);
58+
var targetSig = ctx.GetFunctionType(returnType: voidType, ctx.Int32Type, ctx.DoubleType);
59+
var callerSig = ctx.GetFunctionType(returnType: voidType, voidPtrType);
60+
var caller = module.CreateFunction("CallIndirect"u8, callerSig);
61+
62+
using var bldr = new InstructionBuilder( caller.PrependBasicBlock( "entry" ) );
63+
64+
var callInstruction = bldr.Call( targetSig, caller.Parameters[0], ctx.CreateConstant(1), ctx.CreateConstant(2.0));
65+
bldr.Return();
66+
Assert.IsNotNull( callInstruction );
67+
Assert.IsTrue(module.Verify(out string? msg), msg);
68+
}
4669
}
4770
}

src/Ubiquity.NET.Llvm.Tests/ContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void SimpleConstructorDisposeTest( )
4141
public void GetPointerTypeForTest( )
4242
{
4343
using var context = new Context();
44-
var int8PtrType = context.GetPointerTypeFor( context.Int8Type );
44+
var int8PtrType = context.Int8Type.CreatePointerType();
4545
Assert.IsNotNull( int8PtrType );
4646
Assert.AreEqual( context, int8PtrType.Context );
4747

src/Ubiquity.NET.Llvm/Context.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ public Context( )
8181
/// <inheritdoc/>
8282
public void SetDiagnosticHandler( DiagnosticInfoCallbackAction handler ) => Impl.SetDiagnosticHandler( handler );
8383

84-
/// <inheritdoc/>
85-
public IPointerType GetPointerTypeFor( ITypeRef elementType ) => Impl.GetPointerTypeFor( elementType );
86-
8784
/// <inheritdoc/>
8885
public ITypeRef GetIntType( uint bitWidth ) => Impl.GetIntType( bitWidth );
8986

src/Ubiquity.NET.Llvm/ContextAlias.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ public void SetDiagnosticHandler( DiagnosticInfoCallbackAction handler )
155155
}
156156
}
157157

158-
public IPointerType GetPointerTypeFor( ITypeRef elementType )
159-
{
160-
ArgumentNullException.ThrowIfNull( elementType );
161-
162-
return !Equals( elementType.Context )
163-
? throw new ArgumentException( Resources.Cannot_mix_types_from_different_contexts, nameof( elementType ) )
164-
: (IPointerType)LLVMPointerType( elementType.GetTypeRef(), 0 ).CreateType();
165-
}
166-
167158
public ITypeRef GetIntType( uint bitWidth )
168159
{
169160
ArgumentOutOfRangeException.ThrowIfZero( bitWidth );

src/Ubiquity.NET.Llvm/ErrorInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public bool Success
3737
/// <summary>Gets a value indicating whether this instance is disposed</summary>
3838
public bool IsDisposed => Handle is not null && Handle.IsClosed;
3939

40-
/// <inheritdoc/>
40+
/// <summary>Gets a string representation of the error message</summary>
41+
/// <returns>String representation of the error message or an empty string if none</returns>
4142
public override string ToString( )
4243
{
4344
ObjectDisposedException.ThrowIf( IsDisposed, typeof( ErrorInfo ) );

src/Ubiquity.NET.Llvm/IContext.cs

Lines changed: 67 additions & 72 deletions
Large diffs are not rendered by default.

src/Ubiquity.NET.Llvm/IModule.cs

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public interface IModule
152152
/// <returns><see langword="true"/> if the function was found or <see langword="false"/> if not</returns>
153153
public bool TryGetNamedGlobalIFunc( LazyEncodedString name, [MaybeNullWhen( false )] out GlobalIFunc function );
154154

155-
/// <summary>Gets an existing function with the specified signature to the module or creates a new one if it doesn't exist</summary>
155+
/// <summary>Gets an existing function with the specified signature in the module or creates a new one if it doesn't exist</summary>
156156
/// <param name="name">Name of the function to add</param>
157157
/// <param name="signature">Signature of the function</param>
158158
/// <returns><see cref="Function"/>matching the specified signature and name</returns>
@@ -164,6 +164,51 @@ public interface IModule
164164
/// </remarks>
165165
public Function CreateFunction( LazyEncodedString name, IFunctionType signature );
166166

167+
/// <summary>Creates a Function definition with Debug information</summary>
168+
/// <param name="diBuilder">The debug info builder to use to create the function (must be associated with this module)</param>
169+
/// <param name="scope">Containing scope for the function</param>
170+
/// <param name="name">Name of the function in source language form</param>
171+
/// <param name="linkageName">Mangled linker visible name of the function (may be same as <paramref name="name"/> if mangling not required by source language</param>
172+
/// <param name="file">File containing the function definition</param>
173+
/// <param name="line">Line number of the function definition</param>
174+
/// <param name="signature">LLVM Function type for the signature of the function</param>
175+
/// <param name="isLocalToUnit">Flag to indicate if this function is local to the compilation unit</param>
176+
/// <param name="isDefinition">Flag to indicate if this is a definition</param>
177+
/// <param name="scopeLine">First line of the function's outermost scope, this may not be the same as the first line of the function definition due to source formatting</param>
178+
/// <param name="debugFlags">Additional flags describing this function</param>
179+
/// <param name="isOptimized">Flag to indicate if this function is optimized</param>
180+
/// <returns>Function described by the arguments</returns>
181+
public Function CreateFunction( IDIBuilder diBuilder
182+
, DIScope? scope
183+
, LazyEncodedString name
184+
, LazyEncodedString? linkageName
185+
, DIFile? file
186+
, uint line
187+
, DebugFunctionType signature
188+
, bool isLocalToUnit
189+
, bool isDefinition
190+
, uint scopeLine
191+
, DebugInfoFlags debugFlags
192+
, bool isOptimized
193+
);
194+
195+
/// <summary>Creates a function</summary>
196+
/// <param name="diBuilder"><see cref="DIBuilder"/> for creation of debug information</param>
197+
/// <param name="name">Name of the function</param>
198+
/// <param name="isVarArg">Flag indicating if the function supports a variadic argument list</param>
199+
/// <param name="returnType">Return type of the function</param>
200+
/// <param name="argumentTypes">Arguments for the function</param>
201+
/// <returns>
202+
/// Function, matching the signature specified. This may be a previously declared or defined
203+
/// function or a new function if none matching the name and signature is already present.
204+
/// </returns>
205+
public Function CreateFunction( IDIBuilder diBuilder
206+
, LazyEncodedString name
207+
, bool isVarArg
208+
, IDebugType<ITypeRef, DIType> returnType
209+
, params IEnumerable<IDebugType<ITypeRef, DIType>> argumentTypes
210+
);
211+
167212
/// <summary>Writes a bit-code module to a file</summary>
168213
/// <param name="path">Path to write the bit-code into</param>
169214
/// <remarks>
@@ -300,68 +345,6 @@ LazyEncodedString name
300345
/// <param name="version">version information to place in the llvm.ident metadata</param>
301346
public void AddVersionIdentMetadata( LazyEncodedString version );
302347

303-
/// <summary>Creates a Function definition with Debug information</summary>
304-
/// <param name="diBuilder">The debug info builder to use to create the function (must be associated with this module)</param>
305-
/// <param name="scope">Containing scope for the function</param>
306-
/// <param name="name">Name of the function in source language form</param>
307-
/// <param name="linkageName">Mangled linker visible name of the function (may be same as <paramref name="name"/> if mangling not required by source language</param>
308-
/// <param name="file">File containing the function definition</param>
309-
/// <param name="line">Line number of the function definition</param>
310-
/// <param name="signature">LLVM Function type for the signature of the function</param>
311-
/// <param name="isLocalToUnit">Flag to indicate if this function is local to the compilation unit</param>
312-
/// <param name="isDefinition">Flag to indicate if this is a definition</param>
313-
/// <param name="scopeLine">First line of the function's outermost scope, this may not be the same as the first line of the function definition due to source formatting</param>
314-
/// <param name="debugFlags">Additional flags describing this function</param>
315-
/// <param name="isOptimized">Flag to indicate if this function is optimized</param>
316-
/// <returns>Function described by the arguments</returns>
317-
public Function CreateFunction( IDIBuilder diBuilder
318-
, DIScope? scope
319-
, LazyEncodedString name
320-
, LazyEncodedString? linkageName
321-
, DIFile? file
322-
, uint line
323-
, DebugFunctionType signature
324-
, bool isLocalToUnit
325-
, bool isDefinition
326-
, uint scopeLine
327-
, DebugInfoFlags debugFlags
328-
, bool isOptimized
329-
);
330-
331-
/// <summary>Creates a function</summary>
332-
/// <param name="diBuilder"><see cref="DIBuilder"/> for creation of debug information</param>
333-
/// <param name="name">Name of the function</param>
334-
/// <param name="isVarArg">Flag indicating if the function supports a variadic argument list</param>
335-
/// <param name="returnType">Return type of the function</param>
336-
/// <param name="argumentTypes">Arguments for the function</param>
337-
/// <returns>
338-
/// Function, matching the signature specified. This may be a previously declared or defined
339-
/// function or a new function if none matching the name and signature is already present.
340-
/// </returns>
341-
public Function CreateFunction( IDIBuilder diBuilder
342-
, LazyEncodedString name
343-
, bool isVarArg
344-
, IDebugType<ITypeRef, DIType> returnType
345-
, IEnumerable<IDebugType<ITypeRef, DIType>> argumentTypes
346-
);
347-
348-
/// <summary>Creates a function</summary>
349-
/// <param name="diBuilder"><see cref="DIBuilder"/> for creation of debug information</param>
350-
/// <param name="name">Name of the function</param>
351-
/// <param name="isVarArg">Flag indicating if the function supports a variadic argument list</param>
352-
/// <param name="returnType">Return type of the function</param>
353-
/// <param name="argumentTypes">Arguments for the function</param>
354-
/// <returns>
355-
/// Function, matching the signature specified. This may be a previously declared or defined
356-
/// function or a new function if none matching the name and signature is already present.
357-
/// </returns>
358-
public Function CreateFunction( IDIBuilder diBuilder
359-
, LazyEncodedString name
360-
, bool isVarArg
361-
, IDebugType<ITypeRef, DIType> returnType
362-
, params IDebugType<ITypeRef, DIType>[] argumentTypes
363-
);
364-
365348
/// <summary>Gets a declaration for an LLVM intrinsic function</summary>
366349
/// <param name="name">Name of the intrinsic</param>
367350
/// <param name="args">Args for the intrinsic</param>

src/Ubiquity.NET.Llvm/Instructions/CallInstruction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class CallInstruction
1515
, IFunctionAttributeAccessor
1616
{
1717
/// <summary>Gets the target function of the call</summary>
18-
public Function TargetFunction
19-
=> FromHandle<Function>( LLVMGetCalledValue( Handle ).ThrowIfInvalid() )!;
18+
public Value TargetFunction
19+
=> FromHandle<Value>( LLVMGetCalledValue( Handle ).ThrowIfInvalid() )!;
2020

2121
/// <summary>Gets or sets a value indicating whether the call is a tail call</summary>
2222
public bool IsTailCall

0 commit comments

Comments
 (0)