mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-17 14:57:03 +00:00
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.
107 lines
3.3 KiB
Mathematica
107 lines
3.3 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1999-2001 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% mlds_to_ilasm - Convert MLDS to IL assembler code.
|
|
% Main author: trd.
|
|
%
|
|
% This code converts the MLDS representation into IL assembler.
|
|
% This module takes care of creating the appropriate files and
|
|
% generating output, while mlds_to_il takes care of generated IL from
|
|
% MLDS.
|
|
|
|
:- module mlds_to_ilasm.
|
|
:- interface.
|
|
|
|
:- import_module mlds.
|
|
:- import_module io.
|
|
|
|
% Convert the MLDS to IL and write it to a file.
|
|
|
|
:- pred mlds_to_ilasm__output_mlds(mlds, io__state, io__state).
|
|
:- mode mlds_to_ilasm__output_mlds(in, di, uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module globals, options, passes_aux.
|
|
:- import_module builtin_ops, c_util, modules, tree.
|
|
:- import_module hlds_pred. % for `pred_proc_id'.
|
|
:- import_module prog_data, prog_out, llds_out.
|
|
:- import_module rtti, type_util, error_util.
|
|
|
|
:- import_module ilds, ilasm, il_peephole.
|
|
:- import_module ml_util, ml_code_util.
|
|
:- import_module mlds_to_csharp. /* to output C sharp code */
|
|
:- import_module mlds_to_mcpp. /* to output MC++ code */
|
|
:- use_module llds. /* for user_c_code */
|
|
|
|
:- import_module bool, int, map, string, set, list, assoc_list, term, std_util.
|
|
:- import_module library, require, counter.
|
|
|
|
:- import_module mlds_to_il.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
output_mlds(MLDS) -->
|
|
{ ModuleName = mlds__get_module_name(MLDS) },
|
|
module_name_to_file_name(ModuleName, ".il", yes, ILAsmFile),
|
|
output_to_file(ILAsmFile, output_assembler(MLDS), Result),
|
|
|
|
% Put the pragma C code into a C++ file.
|
|
% This is temporary, when we have pragma foreign
|
|
% we should just put managed C++ foreign code into
|
|
% this file.
|
|
( { Result = yes(ForeignLangs) } ->
|
|
( { set__member(managed_cplusplus, ForeignLangs) } ->
|
|
module_name_to_file_name(ModuleName,
|
|
"__c_code.cpp", yes, CPPFile),
|
|
output_to_file(CPPFile, output_mcpp_code(MLDS))
|
|
; { set__member(csharp, ForeignLangs) } ->
|
|
module_name_to_file_name(ModuleName,
|
|
"__csharp_code.cs", yes, CSFile),
|
|
output_to_file(CSFile, output_csharp_code(MLDS))
|
|
;
|
|
[]
|
|
)
|
|
;
|
|
[]
|
|
).
|
|
|
|
%
|
|
% Generate the `.il' file
|
|
%
|
|
:- pred output_assembler(mlds, set(foreign_language), io__state, io__state).
|
|
:- mode output_assembler(in, out, di, uo) is det.
|
|
|
|
output_assembler(MLDS, ContainsCCode) -->
|
|
{ MLDS = mlds(ModuleName, _ForeignCode, _Imports, _Defns) },
|
|
output_src_start(ModuleName),
|
|
io__nl,
|
|
|
|
generate_il(MLDS, ILAsm0, ContainsCCode),
|
|
|
|
% Perform peephole optimization if requested.
|
|
globals__io_lookup_bool_option(optimize_peep, Peephole),
|
|
{ Peephole = yes ->
|
|
il_peephole__optimize(ILAsm0, ILAsm)
|
|
;
|
|
ILAsm0 = ILAsm
|
|
},
|
|
% Output the assembly.
|
|
ilasm__output(ILAsm),
|
|
|
|
output_src_end(ModuleName).
|
|
|
|
:- func this_file = string.
|
|
this_file = "mlds_to_ilasm.m".
|
|
|
|
:- end_module mlds_to_ilasm.
|