ILUtil.cs 1.59 KB
Newer Older
pcy's avatar
pcy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace PoroCYon.FNAGLSL {
	public static class ILUtil {
		// TODO: other useful insns?
		static ILUtil() {
			var cpblk = new DynamicMethod("_Cpblk_IL",
					MethodAttributes.Public|MethodAttributes.Static,
					CallingConventions.Standard,
				null, new [] { typeof(IntPtr), typeof(IntPtr), typeof(IntPtr) },
				typeof(ILUtil), true);

			var gen = cpblk.GetILGenerator();
			gen.Emit(OpCodes.Ldarg_0);
			gen.Emit(OpCodes.Ldarg_1);
			gen.Emit(OpCodes.Ldarg_2);
			gen.Emit(OpCodes.Cpblk);
			gen.Emit(OpCodes.Ret);

			_Cpblk = (Action<IntPtr, IntPtr, IntPtr>)cpblk.CreateDelegate(
				typeof(Action<IntPtr, IntPtr, IntPtr>));

			var initblk = new DynamicMethod("_Initblk_IL",
					MethodAttributes.Public|MethodAttributes.Static,
					CallingConventions.Standard,
				null, new [] { typeof(IntPtr), typeof(byte), typeof(IntPtr) },
				typeof(ILUtil), true);

			gen = initblk.GetILGenerator();
			gen.Emit(OpCodes.Ldarg_0);
			gen.Emit(OpCodes.Ldarg_1);
			gen.Emit(OpCodes.Ldarg_2);
			gen.Emit(OpCodes.Initblk);
			gen.Emit(OpCodes.Ret);

			_Initblk = (Action<IntPtr, byte, IntPtr>)initblk.CreateDelegate(
				typeof(Action<IntPtr, byte, IntPtr>));
		}

		static Action<IntPtr, IntPtr, IntPtr> _Cpblk;
		public static void Cpblk(IntPtr dst, IntPtr src, IntPtr size) { // memcpy args order
			_Cpblk(dst, src, size);
		}

		static Action<IntPtr, byte, IntPtr> _Initblk;
		public static void Initblk(IntPtr ptr, byte val, IntPtr size) { // memset args order
			_Initblk(ptr, val, size);
		}
	}
}