/ src / ARMeilleure / CodeGen / CompiledFunction.cs
CompiledFunction.cs
 1  using ARMeilleure.CodeGen.Linking;
 2  using ARMeilleure.CodeGen.Unwinding;
 3  using ARMeilleure.Translation.Cache;
 4  using System;
 5  using System.Runtime.InteropServices;
 6  
 7  namespace ARMeilleure.CodeGen
 8  {
 9      /// <summary>
10      /// Represents a compiled function.
11      /// </summary>
12      readonly struct CompiledFunction
13      {
14          /// <summary>
15          /// Gets the machine code of the <see cref="CompiledFunction"/>.
16          /// </summary>
17          public byte[] Code { get; }
18  
19          /// <summary>
20          /// Gets the <see cref="Unwinding.UnwindInfo"/> of the <see cref="CompiledFunction"/>.
21          /// </summary>
22          public UnwindInfo UnwindInfo { get; }
23  
24          /// <summary>
25          /// Gets the <see cref="Linking.RelocInfo"/> of the <see cref="CompiledFunction"/>.
26          /// </summary>
27          public RelocInfo RelocInfo { get; }
28  
29          /// <summary>
30          /// Initializes a new instance of the <see cref="CompiledFunction"/> struct with the specified machine code,
31          /// unwind info and relocation info.
32          /// </summary>
33          /// <param name="code">Machine code</param>
34          /// <param name="unwindInfo">Unwind info</param>
35          /// <param name="relocInfo">Relocation info</param>
36          internal CompiledFunction(byte[] code, UnwindInfo unwindInfo, RelocInfo relocInfo)
37          {
38              Code = code;
39              UnwindInfo = unwindInfo;
40              RelocInfo = relocInfo;
41          }
42  
43          /// <summary>
44          /// Maps the <see cref="CompiledFunction"/> onto the <see cref="JitCache"/> and returns a delegate of type
45          /// <typeparamref name="T"/> pointing to the mapped function.
46          /// </summary>
47          /// <typeparam name="T">Type of delegate</typeparam>
48          /// <returns>A delegate of type <typeparamref name="T"/> pointing to the mapped function</returns>
49          public T Map<T>()
50          {
51              return MapWithPointer<T>(out _);
52          }
53  
54          /// <summary>
55          /// Maps the <see cref="CompiledFunction"/> onto the <see cref="JitCache"/> and returns a delegate of type
56          /// <typeparamref name="T"/> pointing to the mapped function.
57          /// </summary>
58          /// <typeparam name="T">Type of delegate</typeparam>
59          /// <param name="codePointer">Pointer to the function code in memory</param>
60          /// <returns>A delegate of type <typeparamref name="T"/> pointing to the mapped function</returns>
61          public T MapWithPointer<T>(out IntPtr codePointer)
62          {
63              codePointer = JitCache.Map(this);
64  
65              return Marshal.GetDelegateForFunctionPointer<T>(codePointer);
66          }
67      }
68  }