using System.Globalization; using CommunityToolkit.HighPerformance; using Backend32 = System.Int32; using Backend64 = System.Int64; using Backend8 = System.Byte; namespace Brutal; /// An argument meant to be passed to a C++ variadic function, /// in va_list flavor, eg vprintf. public readonly struct VariadicArgument { public readonly TypeCode Type; public readonly Backend64 Value64; public readonly string? ValueString; private VariadicArgument(TypeCode type, Backend64 value) { Type = type; Value64 = value; } public VariadicArgument(string? value) { Type = TypeCode.String; ValueString = value; } public static implicit operator VariadicArgument(Char16 value) => new(TypeCode.Char, (Char8)value); public static implicit operator VariadicArgument(int value) => new(TypeCode.Int32, As(value)); public static implicit operator VariadicArgument(uint value) => new(TypeCode.UInt32, As(value)); public static implicit operator VariadicArgument(long value) => new(TypeCode.Int64, As(value)); public static implicit operator VariadicArgument(ulong value) => new(TypeCode.UInt64, As(value)); public static implicit operator VariadicArgument(float value) => new(TypeCode.Single, As(value)); public static implicit operator VariadicArgument(double value) => new(TypeCode.Double, As(value)); public static implicit operator VariadicArgument(string? arg) => new(arg); public static VariadicArgument Convert(object o) => o switch { Char16 v => v, int v => v, uint v => v, long v => v, ulong v => v, float v => v, double v => v, string v => v, _ => throw new ArgumentException($"type {o.GetType()} not supported"), }; #region debugging Backend8 Value8 => (Backend8)Value64; Backend32 Value32 => (Backend32)Value64; public Char16 AsChar16 => (Char16)Value8; public int AsInt => As(Value32); public uint AsUint => As(Value32); public long AsLong => As(Value64); public ulong AsUlong => As(Value64); public float AsFloat => (float)As(Value64); public double AsDouble => As(Value64); public string? AsString => ValueString; int BackendSize => Type switch { TypeCode.Char => sizeof(Backend8), TypeCode.Int32 => sizeof(Backend32), TypeCode.UInt32 => sizeof(Backend32), TypeCode.Int64 => sizeof(Backend64), TypeCode.UInt64 => sizeof(Backend64), TypeCode.Single => sizeof(Backend64), TypeCode.Double => sizeof(Backend64), TypeCode.String => sizeof(Backend64), _ => throw new ArgumentOutOfRangeException(), }; private int StringSize => ValueString != null ? Utf8.GetByteCount(ValueString) + 1 : 0; public int Size => sizeof(Backend64) + StringSize; public string ValueToString => Type switch { TypeCode.Char => AsChar16.ToString(), TypeCode.Int32 => AsInt.ToString(), TypeCode.UInt32 => AsUint.ToString(), TypeCode.Int64 => AsLong.ToString(), TypeCode.UInt64 => AsUlong.ToString(), TypeCode.Single => AsFloat.ToString(CultureInfo.InvariantCulture), TypeCode.Double => AsDouble.ToString(CultureInfo.InvariantCulture), TypeCode.String => AsString ?? "", _ => throw new ArgumentOutOfRangeException(), }; public override string ToString() { return $"{Type} ({BackendSize}): {ValueToString}"; } #endregion public void CopyTo(ref Backend64 vaList, ref Span stringBuffer) { switch (Type) { case TypeCode.Char: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: { vaList = Value64; return; } case TypeCode.String: { if (ValueString == null) return; // add the pointer to the string buffer vaList = (Backend64)stringBuffer.AsPtr(); // add the string var stringSize = Utf8.GetBytes(ValueString, stringBuffer); stringBuffer[stringSize] = 0; // null-terminator stringBuffer = stringBuffer[(stringSize + 1)..]; return; } default: throw new ArgumentOutOfRangeException(nameof(Type), Type, null); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] static TTo As(TFrom source) where TFrom : allows ref struct where TTo : allows ref struct { return Unsafe.As(ref source); } } /// Utility to interop variadic arguments to a C++ va_list, eg vprintf. /// Usage: GetSize(), stackalloc, Pack(). public static class VaList { /* * va_list ABI layout: * - [VaListHeader, if Linux / System V ABI] * (contains a pointer to the argument list below) * - 64 bits-aligned span of arguments (ints, floats, string pointers, ...) * (this is the old school C ABI format) * - [string buffers (if any), each null-terminated] */ private static readonly int headerSize = Platform.IsLinux ? Unsafe.SizeOf() : 0; /// Compute the size of a va_list for this set of arguments. /// stackalloc a byte array of the returned size, and pass it to . /// The arguments to interop / compute size for. public static int GetSize(ReadOnlySpan args) { var argsSize = headerSize; foreach (var arg in args) argsSize += arg.Size; return argsSize; } /// Pack the given arguments to a va_list that can be interop'd to C++. /// A buffer you stackalloc'd use the size returned by /// The arguments to interop / pack. /// The buffer you passed in, with arguments packed in. public static Span Pack(Span buffer, ReadOnlySpan args) { // argument buffer, 64-bits aligned var vaListSize = args.Length * sizeof(Backend64); var vaList = buffer.Slice(headerSize, vaListSize).Cast(); // string buffers, if any var stringBuffer = buffer[(headerSize + vaListSize)..]; if (headerSize > 0) { unsafe { // va_list header, if the platform ABI requires it ref var header = ref buffer.Cast()[0]; header = new VaListHeader(vaList.AsPointer()); } } for (var i = 0; i < args.Length; i++) { args[i].CopyTo(ref vaList[i], ref stringBuffer); } return buffer; } /// The va_list structure of System V ABI (Linux x64). [StructLayout(LayoutKind.Sequential, Pack = 4)] private readonly unsafe struct VaListHeader { private readonly uint gp_offset; private readonly uint fp_offset; private readonly void* overflow_arg_area; private readonly void* reg_save_area; /// Wrap the given arguments in a header structure. /// A pointer to the arguments already packed in C ABI format. public VaListHeader(void* va_list) { /* * This ABI tries to put arguments in registers if any are left, then pushes the rest on the stack. * We bypass the register mechanism by maxing out the offsets, then put everything in the overflow area. * * Source: https://stackoverflow.com/questions/11695237/creating-va-list-dynamically-in-gcc-can-it-be-done/30632712#30632712 * The va_args() macro will read from the overflow area: * - if the gp_offset is greater than or equal to 48 (6 gp registers * 8 bytes/register) * - and the fp_offset is greater than or equal to 304 (gp_offset +16 fp registers * 16 bytes/register) */ gp_offset = 48; fp_offset = 304; reg_save_area = null; overflow_arg_area = va_list; } } }