mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 14:25:56 +00:00
Implement a C# interface for the .NET backend.
Estimated hours taken: 45 Branches: main Implement a C# interface for the .NET backend. To use it, you currently need to set --backend-foreign-language csharp --use-foreign-language csharp in your MCFLAGS. The C# foreign language interface works by introducing a new sort of MLDS statement called outline_foreign_proc. outline_foreign_proc is expected to be turned into a separate procedure in a separate file. This is quite different to normal foreign code which has been renamed as inline target code, as it is really intended to be generated inline, inside the generated code. Because outline_foreign_proc is expected to be generated outside the normal code, we don't need to generate variable renamings, initializations, casts and other complicated interfacing code. Any marshalling is done by the backend, which knows how to marshall arguments across the boundary into the outline code and back. In the case of marshalling to C# from the .NET backend, we currently don't do anything special (part of the point of .NET is that data representation don't have to change very often just because you are using different languages, so this is a property we should try to preserve). The actual implementation of the foreign code is therefore very simple. Simply generate an appropriate procedure, and insert the user's code in the middle. The bulk of this change to delay the mangling of MLDS var names, so we can still use the original user's var name when we output the outline procedure (since the user's foreign code will refer to these var names, it's important to keep them around). compiler/foreign.m: Handle the csharp foreign language. compiler/globals.m: Fix an XXX about converting to lowercase to do language name comparisons. Add new predicates to make conversion of foreign languages to strings more uniform. compiler/handle_options.m: Don't set backend_foreign_language to the default if it has already been set by hand. compiler/ml_call_gen.m: compiler/ml_code_gen.m: compiler/ml_code_util.m: Delay the mangling of MLDS var names by keeping the variable number around until the output phase. Slightly generalize the handling of foreign language interfacing. Handle C# foreign language interfacing. Add value_output_vars to the ml_gen_info, which are the variables returned rather than passed by reference. We need to know these variables for C# interfacing so that we can handle the return value of the forwarding function. Mark the beginning and end of the MLDS foreign language processing as a "sub-module" (in comments at least). Later I may put this code into a separate module. Rename some predicates from c_code to foreign_code. compiler/ml_elim_nested.m: compiler/ml_optimize.m: compiler/ml_string_switch.m: compiler/ml_type_gen.m: compiler/ml_unify_gen.m: compiler/ml_util.m: compiler/rtti_to_mlds.m: Handle the new var_name type, and the new target_code constructors. compiler/mlds.m: Add outline_foreign_proc which is handled differently to the old target_code (which has been renamed inline_target_code). Change the definiton for mlds__var_name. compiler/mlds_to_c.m: Factor out mlds_output_to_file. Handle the new var_name type, and the new target_code constructors. compiler/mlds_to_csharp.m: A new module to generate C# code suitable for foreign language interfacing. This is largely lifted from the MC++ code, with a few changes to the output syntax. compiler/mlds_to_il.m: Return the set of foreign languages processed instead of a bool saying wither MC++ was present. This is so we can generate the appropriate output .cs or .cpp files, and because we need to keep track of all the external assembly references we need to put in the .il file. Handle the inline_target_code and mlds__var_name changes. compiler/mlds_to_ilasm.m: Output .cpp and .cs files conditionally. Factor out output_to_file. Move MC++ output code to mlds_to_mcpp.m compiler/mlds_to_java.m: Factor out output_to_file. Handle the new var_name type, and the new target_code constructors. compiler/mlds_to_mcpp.m: New file to handle generating MC++ code suitable for foreign language interfacing. compiler/options.m: Add a way of setting the backend-foreign-language option. compiler/passes_aux.m: Add output_to_file which is used by the MLDS backend to generate output files. compiler/prog_data.m: Uncomment csharp as a foreign language.
This commit is contained in:
@@ -54,13 +54,15 @@ ml_string_switch__generate(Cases, Var, CodeModel, _CanFail, Context,
|
||||
% MR_String str;
|
||||
%
|
||||
ml_gen_info_new_cond_var(SlotVarSeq),
|
||||
{ SlotVarName = string__format("slot_%d", [i(SlotVarSeq)]) },
|
||||
{ SlotVarName = mlds__var_name(
|
||||
string__format("slot_%d", [i(SlotVarSeq)]), no) },
|
||||
{ SlotVarDefn = ml_gen_mlds_var_decl(var(SlotVarName),
|
||||
mlds__native_int_type, MLDS_Context) },
|
||||
ml_gen_var_lval(SlotVarName, mlds__native_int_type, SlotVarLval),
|
||||
|
||||
ml_gen_info_new_cond_var(StringVarSeq),
|
||||
{ StringVarName = string__format("str_%d", [i(StringVarSeq)]) },
|
||||
{ StringVarName = mlds__var_name(
|
||||
string__format("str_%d", [i(StringVarSeq)]), no) },
|
||||
{ StringVarDefn = ml_gen_mlds_var_decl(var(StringVarName),
|
||||
ml_string_type, MLDS_Context) },
|
||||
ml_gen_var_lval(StringVarName, ml_string_type, StringVarLval),
|
||||
|
||||
Reference in New Issue
Block a user