From cd6f2ddc4c39c2991e519ed22372002ea26e7f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Thu, 3 Aug 2017 20:14:57 +0100 Subject: [PATCH] g3 --- .../ForceIncludeAttribute.cs | 14 ++++ source/Cosmos.IL2CPU/ILScanner.cs | 34 ++++++++++ source/Kernel-X86/00-CPU/Cosmos.CPU/INTs.cs | 67 +------------------ 3 files changed, 51 insertions(+), 64 deletions(-) create mode 100644 source/Cosmos.IL2CPU.API/ForceIncludeAttribute.cs diff --git a/source/Cosmos.IL2CPU.API/ForceIncludeAttribute.cs b/source/Cosmos.IL2CPU.API/ForceIncludeAttribute.cs new file mode 100644 index 000000000..f2656f9d0 --- /dev/null +++ b/source/Cosmos.IL2CPU.API/ForceIncludeAttribute.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Cosmos.IL2CPU.API +{ + /// + /// Use this attribute to force the inclusion of a class, struct or a method in the compiled code. + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method, AllowMultiple = false)] + public class ForceIncludeAttribute : Attribute + { + } +} diff --git a/source/Cosmos.IL2CPU/ILScanner.cs b/source/Cosmos.IL2CPU/ILScanner.cs index 2d73b573e..b0a9ad528 100644 --- a/source/Cosmos.IL2CPU/ILScanner.cs +++ b/source/Cosmos.IL2CPU/ILScanner.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.Loader; using Cosmos.Assembler; using Cosmos.IL2CPU.Extensions; @@ -231,6 +232,39 @@ namespace Cosmos.IL2CPU { Queue(typeof(Array).GetTypeInfo().GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(), null, "Explicit Entry"); Queue(typeof(MulticastDelegate).GetTypeInfo().GetMethod("GetInvocationList"), null, "Explicit Entry"); Queue(ExceptionHelperRefs.CurrentExceptionRef, null, "Explicit Entry"); + + // ForceInclude + foreach (var xAssembly in AssemblyLoadContext.Default.GetLoadedAssemblies()) + { + foreach (var xType in xAssembly.GetTypes()) + { + var xTypeInfo = xType.GetTypeInfo(); + + if (xTypeInfo.GetCustomAttribute() != null) + { + Queue(xTypeInfo, null, "Explicit Entry"); + continue; + } + + foreach (var xMethod in xTypeInfo.GetMethods( + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) + { + if (xMethod.GetCustomAttribute() != null) + { + Queue(xMethod, null, "Explicit Entry"); + } + } + + foreach (var xMethod in xTypeInfo.GetMethods( + BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) + { + if (xMethod.GetCustomAttribute() != null) + { + Queue(xMethod, null, "Explicit Entry"); + } + } + } + } // Start from entry point of this program Queue(aStartMethod, null, "Entry Point"); diff --git a/source/Kernel-X86/00-CPU/Cosmos.CPU/INTs.cs b/source/Kernel-X86/00-CPU/Cosmos.CPU/INTs.cs index bb41d6237..88429c66d 100644 --- a/source/Kernel-X86/00-CPU/Cosmos.CPU/INTs.cs +++ b/source/Kernel-X86/00-CPU/Cosmos.CPU/INTs.cs @@ -1,7 +1,10 @@ using System.Runtime.InteropServices; + using Cosmos.CPU; +using Cosmos.IL2CPU.API; namespace Cosmos.Core { + [ForceInclude] public class INTs { #region Enums // TODO: Protect IRQs like memory and ports are @@ -482,69 +485,5 @@ namespace Cosmos.Core { PutErrorChar(line, startCol + i, error[i]); } } - - // This is to trick IL2CPU to compile it in - //TODO: Make a new attribute that IL2CPU sees when scanning to force inclusion so we dont have to do this. - // We dont actually need to cal this method - public static void Dummy() { - // Compiler magic - bool xTest = false; - if (xTest) { - unsafe - { - var xCtx = new IRQContext(); - HandleInterrupt_Default(ref xCtx); - HandleInterrupt_00(ref xCtx); - HandleInterrupt_01(ref xCtx); - HandleInterrupt_02(ref xCtx); - HandleInterrupt_03(ref xCtx); - HandleInterrupt_04(ref xCtx); - HandleInterrupt_05(ref xCtx); - HandleInterrupt_06(ref xCtx); - HandleInterrupt_07(ref xCtx); - HandleInterrupt_08(ref xCtx); - HandleInterrupt_09(ref xCtx); - HandleInterrupt_0A(ref xCtx); - HandleInterrupt_0B(ref xCtx); - HandleInterrupt_0C(ref xCtx); - HandleInterrupt_0D(ref xCtx); - HandleInterrupt_0E(ref xCtx); - HandleInterrupt_0F(ref xCtx); - HandleInterrupt_10(ref xCtx); - HandleInterrupt_11(ref xCtx); - HandleInterrupt_12(ref xCtx); - HandleInterrupt_13(ref xCtx); - HandleInterrupt_20(ref xCtx); - HandleInterrupt_21(ref xCtx); - HandleInterrupt_22(ref xCtx); - HandleInterrupt_23(ref xCtx); - HandleInterrupt_24(ref xCtx); - HandleInterrupt_25(ref xCtx); - HandleInterrupt_26(ref xCtx); - HandleInterrupt_27(ref xCtx); - HandleInterrupt_28(ref xCtx); - HandleInterrupt_29(ref xCtx); - HandleInterrupt_2A(ref xCtx); - HandleInterrupt_2B(ref xCtx); - HandleInterrupt_2C(ref xCtx); - HandleInterrupt_2D(ref xCtx); - HandleInterrupt_2E(ref xCtx); - HandleInterrupt_2F(ref xCtx); - HandleInterrupt_30(ref xCtx); - HandleInterrupt_35(ref xCtx); - HandleInterrupt_40(ref xCtx); - HandleInterrupt_41(ref xCtx); - HandleInterrupt_42(ref xCtx); - HandleInterrupt_43(ref xCtx); - HandleInterrupt_44(ref xCtx); - HandleInterrupt_45(ref xCtx); - HandleInterrupt_46(ref xCtx); - HandleInterrupt_47(ref xCtx); - HandleInterrupt_48(ref xCtx); - HandleInterrupt_49(ref xCtx); - } - } - } - } }