Skip to content

Commit

Permalink
[MONO] Move marshal-ilgen into a component (dotnet#71203)
Browse files Browse the repository at this point in the history
* Move marshal-ilgen into a component.
  • Loading branch information
Nathan Ricci authored Aug 10, 2022
1 parent fa75057 commit de32c44
Show file tree
Hide file tree
Showing 27 changed files with 3,366 additions and 3,047 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@
<PlatformManifestFileEntry Include="libmono-component-debugger-stub-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-debugger-static.lib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-debugger-stub-static.lib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen.dll" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen.so" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen-stub-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen-static.lib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-marshal-ilgen-stub-static.lib" IsNative="true" />
<!-- Mono WASM-specific files -->
<PlatformManifestFileEntry Include="libmono-ee-interp.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<PropertyGroup>
<RuntimeComponents Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetOS)' == 'Android'">diagnostics_tracing</RuntimeComponents>
<RuntimeComponents Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetOS)' == 'Android'">diagnostics_tracing;marshal-ilgen</RuntimeComponents>
</PropertyGroup>
<!-- Windows only files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
Expand Down
3 changes: 2 additions & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@
<MonoAOTCMakeArgs Include="-DENABLE_ICALL_SYMBOL_MAP=1" />
<MonoAOTCMakeArgs Include="-DDISABLE_SHARED_LIBS=1" />
<MonoAOTCMakeArgs Include="-DDISABLE_LIBS=1" />
<MonoAOTCMakeArgs Include="-DDISABLE_COMPONENTS=1" />
<!-- Link in only the components neeeded for AOT compilation -->
<MonoAOTCMakeArgs Include="-DAOT_COMPONENTS=1 -DSTATIC_COMPONENTS=1;" />
<MonoAOTCMakeArgs Condition="'$(MonoAotOffsetsFile)' != ''" Include="-DAOT_OFFSETS_FILE=&quot;$(MonoAotOffsetsFile)&quot;" />
<MonoAOTCMakeArgs Condition="'$(MonoAOTEnableLLVM)' == 'true'" Include="-DLLVM_PREFIX=$(MonoAOTLLVMDir.TrimEnd('\/'))" />
<MonoAOTCMakeArgs Include="$(_MonoAOTCFLAGSOption)" />
Expand Down
53 changes: 46 additions & 7 deletions src/mono/mono/component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ set(MONO_EVENTPIPE_GEN_INCLUDE_PATH "${CMAKE_CURRENT_BINARY_DIR}/eventpipe")
set(MONO_HOT_RELOAD_COMPONENT_NAME "hot_reload")
set(MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME "diagnostics_tracing")
set(MONO_DEBUGGER_COMPONENT_NAME "debugger")
set(MONO_MARSHAL_ILGEN_COMPONENT_NAME "marshal-ilgen")

# a list of every component.
set(components "")
# a list of components needed by the AOT compiler
set(components_for_aot "")

# the sources for each individiable component define a new
# component_name-sources list for each component, and a
Expand Down Expand Up @@ -78,17 +81,53 @@ set(${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-dependencies
${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-sources
)

# marshal-ilgen
list(APPEND components
${MONO_MARSHAL_ILGEN_COMPONENT_NAME}
)
list(APPEND components_for_aot
${MONO_MARSHAL_ILGEN_COMPONENT_NAME}
)

set(${MONO_MARSHAL_ILGEN_COMPONENT_NAME}-sources
${MONO_COMPONENT_PATH}/marshal-ilgen.c
${MONO_COMPONENT_PATH}/marshal-ilgen.h
${MONO_COMPONENT_PATH}/marshal-ilgen-noilgen.c
${MONO_COMPONENT_PATH}/marshal-ilgen-noilgen.h
)

# For every component not build into the AOT compiler, build the stub instead
set(stubs_for_aot "")
foreach (component IN LISTS components)
if (NOT (component IN_LIST components_for_aot))
list(APPEND stubs_for_aot "${component}")
endif()
endforeach()


set(${MONO_MARSHAL_ILGEN_COMPONENT_NAME}-stub-sources
${MONO_COMPONENT_PATH}/marshal-ilgen-stub.c
)

if (AOT_COMPONENTS)
set(components_to_build ${components_for_aot})
set(stubs_to_build ${stubs_for_aot})
else()
set(components_to_build ${components})
set(stubs_to_build ${components})
endif()

# from here down, all the components are treated in the same way

#define a library for each component and component stub
function(define_component_libs)
# NOTE: keep library naming pattern in sync with RuntimeComponentManifest.targets
if (NOT DISABLE_LIBS)
foreach(component IN LISTS components)
if (AOT_COMPONENTS OR NOT DISABLE_LIBS )
foreach(component IN LISTS components_to_build)
add_library("mono-component-${component}-static" STATIC $<TARGET_OBJECTS:${component}-objects>)
install(TARGETS "mono-component-${component}-static" LIBRARY)
endforeach()
foreach(component IN LISTS components)
foreach(component IN LISTS stubs_to_build)
add_library("mono-component-${component}-stub-static" STATIC $<TARGET_OBJECTS:${component}-stub-objects>)
install(TARGETS "mono-component-${component}-stub-static" LIBRARY)
endforeach()
Expand All @@ -102,7 +141,7 @@ target_sources(component_base INTERFACE
)
target_link_libraries(component_base INTERFACE monoapi)

if(DISABLE_COMPONENTS OR DISABLE_LIBS)
if(NOT AOT_COMPONENTS AND (DISABLE_COMPONENTS OR DISABLE_LIBS))
set(DISABLE_COMPONENT_OBJECTS 1)
endif()

Expand All @@ -122,7 +161,7 @@ endforeach()

if(NOT DISABLE_COMPONENTS AND NOT STATIC_COMPONENTS)
# define a shared library for each component
foreach(component IN LISTS components)
foreach(component IN LISTS components_to_build)
# NOTE: keep library naming pattern in sync with RuntimeComponentManifest.targets
if(HOST_WIN32)
add_library("mono-component-${component}" SHARED "${${component}-sources}")
Expand Down Expand Up @@ -154,14 +193,14 @@ if(NOT DISABLE_COMPONENTS AND NOT STATIC_COMPONENTS)
#define a library for each component and component stub
define_component_libs()

elseif(NOT DISABLE_COMPONENTS AND STATIC_COMPONENTS)
elseif(AOT_COMPONENTS OR (NOT DISABLE_COMPONENTS AND STATIC_COMPONENTS))

#define a library for each component and component stub
define_component_libs()

# define a list of mono-components objects for mini if building a shared libmono with static-linked components
set(mono-components-objects "")
foreach(component IN LISTS components)
foreach(component IN LISTS components_to_build)
list(APPEND mono-components-objects $<TARGET_OBJECTS:${component}-objects>)
endforeach()

Expand Down
186 changes: 186 additions & 0 deletions src/mono/mono/component/marshal-ilgen-noilgen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include "mono/component/marshal-ilgen.h"
#include "mono/component/marshal-ilgen-noilgen.h"

#ifndef ENABLE_ILGEN
static int
emit_marshal_array_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
MonoType *object_type = mono_get_object_type ();
switch (action) {
case MARSHAL_ACTION_CONV_IN:
*conv_arg_type = object_type;
break;
case MARSHAL_ACTION_MANAGED_CONV_IN:
*conv_arg_type = int_type;
break;
}
return conv_arg;
}

static int
emit_marshal_ptr_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
return conv_arg;
}
#endif

#if !defined(ENABLE_ILGEN) || defined(DISABLE_NONBLITTABLE)
static int
emit_marshal_vtype_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
return conv_arg;
}

static int
emit_marshal_string_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
switch (action) {
case MARSHAL_ACTION_CONV_IN:
*conv_arg_type = int_type;
break;
case MARSHAL_ACTION_MANAGED_CONV_IN:
*conv_arg_type = int_type;
break;
}
return conv_arg;
}

static int
emit_marshal_safehandle_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
if (action == MARSHAL_ACTION_CONV_IN)
*conv_arg_type = int_type;
return conv_arg;
}

static int
emit_marshal_handleref_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
if (action == MARSHAL_ACTION_CONV_IN)
*conv_arg_type = int_type;
return conv_arg;
}

static int
emit_marshal_object_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
if (action == MARSHAL_ACTION_CONV_IN)
*conv_arg_type = int_type;
return conv_arg;
}

static int
emit_marshal_variant_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
g_assert_not_reached ();
}

static int
emit_marshal_asany_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
return conv_arg;
}

static int
emit_marshal_boolean_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
switch (action) {
case MARSHAL_ACTION_CONV_IN:
if (m_type_is_byref (t))
*conv_arg_type = int_type;
else
*conv_arg_type = mono_marshal_boolean_conv_in_get_local_type (spec, NULL);
break;

case MARSHAL_ACTION_MANAGED_CONV_IN: {
MonoClass* conv_arg_class = mono_marshal_boolean_managed_conv_in_get_conv_arg_class (spec, NULL);
if (m_type_is_byref (t))
*conv_arg_type = m_class_get_this_arg (conv_arg_class);
else
*conv_arg_type = m_class_get_byval_arg (conv_arg_class);
break;
}

}
return conv_arg;
}

static int
emit_marshal_char_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
return conv_arg;
}

static int
emit_marshal_custom_noilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoType *int_type = mono_get_int_type ();
if (action == MARSHAL_ACTION_CONV_IN && t->type == MONO_TYPE_VALUETYPE)
*conv_arg_type = int_type;
return conv_arg;
}
#endif

#ifndef ENABLE_ILGEN

void
mono_marshal_noilgen_init_heavyweight (void)
{
MonoMarshalILgenCallbacks ilgen_cb;

ilgen_cb.version = MONO_MARSHAL_CALLBACKS_VERSION;
ilgen_cb.emit_marshal_array = emit_marshal_array_noilgen;
ilgen_cb.emit_marshal_vtype = emit_marshal_vtype_noilgen;
ilgen_cb.emit_marshal_string = emit_marshal_string_noilgen;
ilgen_cb.emit_marshal_safehandle = emit_marshal_safehandle_noilgen;
ilgen_cb.emit_marshal_handleref = emit_marshal_handleref_noilgen;
ilgen_cb.emit_marshal_object = emit_marshal_object_noilgen;
ilgen_cb.emit_marshal_variant = emit_marshal_variant_noilgen;
ilgen_cb.emit_marshal_asany = emit_marshal_asany_noilgen;
ilgen_cb.emit_marshal_boolean = emit_marshal_boolean_noilgen;
ilgen_cb.emit_marshal_custom = emit_marshal_custom_noilgen;
ilgen_cb.emit_marshal_ptr = emit_marshal_ptr_noilgen;

ilgen_cb.emit_marshal_char = emit_marshal_char_noilgen;
mono_install_marshal_callbacks_ilgen(&ilgen_cb);
}

#endif
11 changes: 11 additions & 0 deletions src/mono/mono/component/marshal-ilgen-noilgen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* \file
* Copyright 2022 Microsoft
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#ifndef __MARSHAL_ILGEN_NOILGEN_H__
#define __MARSHAL_ILGEN_NOILGEN_H__

void mono_marshal_noilgen_init_heavyweight (void);

#endif // __MARSHAL_ILGEN_NOILGEN_H__
41 changes: 41 additions & 0 deletions src/mono/mono/component/marshal-ilgen-stub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <mono/component/component.h>
#include <mono/component/marshal-ilgen.h>
#include <mono/metadata/marshal.h>

static bool
marshal_ilgen_available (void)
{
return false;
}

static int
stub_emit_marshal_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action, MonoMarshalLightweightCallbacks* lightweigth_cb)
{
return 0;
}

static void
mono_component_marshal_ilgen_stub_init(void)
{
}

static void
stub_mono_marshal_ilgen_install_callbacks_mono (IlgenCallbacksToMono *callbacks)
{
}

static MonoComponentMarshalILgen component_func_table = {
{ MONO_COMPONENT_ITF_VERSION, &marshal_ilgen_available },
mono_component_marshal_ilgen_stub_init,
stub_emit_marshal_ilgen,
stub_mono_marshal_ilgen_install_callbacks_mono
};

MonoComponentMarshalILgen*
mono_component_marshal_ilgen_init (void)
{
return &component_func_table;
}
Loading

0 comments on commit de32c44

Please sign in to comment.