Files
mercury/compiler/mlds_to_ilasm.m
Peter Ross 26e14009d2 Output Managed C++ code using the same mechanisms as we do for C#
Estimated hours taken: 40
Branches: main

Output Managed C++ code using the same mechanisms as we do for C#
code, rather than using pragma_c_gen to generate MC++ code.  This
fixes the problem that functions couldn't be defined in MC++, and also
should make the code more maintainable in the future as MC++ is much
more similar to C# than to C.

compiler/mlds.m:
	Add a new field to outline_foreign_proc. This field has
	information which links the mlds variables with the variable
	names used in the foreign code.

compiler/ml_code_gen.m:
	Generate a foreign proc for MC++ the same way we generate a
	foreign proc for C#.
	Generate the data for the new field in outline_foreign_proc.
	Use the context of the string which contains the pragma foreign
	proc body for the context of the foreign proc.  This ensures that
	error messages refer to the correct line number.

compiler/mlds_to_il.m:
	Add to the list of foreign languages defined in the module to
	include those which don't have a foreign_proc defined in them.
	Move the relevant code from atomic_statement_to_il to
	generate_method so that we handle external procedures correctly.

compiler/mlds_to_ilasm.m:
	Call mlds_to_managed.

compiler/ml_backend.m:
	Add the new module and remove mlds_to_mcpp and mlds_to_csharp.

compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_java.m:
	Minor changes to handling the new field in outline_foreign_proc.

compiler/mlds_to_csharp.m:
compiler/mlds_to_mcpp.m:
	Removed files whose functionality has been subsumed by
	mlds_to_managed.

library/construct.m:
library/float.m:
library/io.m:
library/std_util.m:
library/type_desc.m:
	Changes required to get the library to be able compile in the
	ilc grade.
2002-06-21 13:26:50 +00:00

119 lines
4.1 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1999-2002 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 ml_backend__mlds_to_ilasm.
:- interface.
:- import_module ml_backend__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 libs__globals, libs__options, hlds__passes_aux.
:- import_module backend_libs__builtin_ops, backend_libs__c_util.
:- import_module parse_tree__modules, libs__tree.
:- import_module hlds__hlds_pred. % for `pred_proc_id'.
:- import_module parse_tree__prog_data, parse_tree__prog_out.
:- import_module backend_libs__rtti, check_hlds__type_util, hlds__error_util.
:- import_module ml_backend__ilds, ml_backend__ilasm, ml_backend__il_peephole.
:- import_module ml_backend__ml_util, ml_backend__ml_code_util.
:- import_module ml_backend__mlds_to_managed.
:- import_module bool, int, map, string, set, list, assoc_list, term, std_util.
:- import_module library, require, counter.
:- import_module ml_backend__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),
( { Result = yes(ForeignLangs) } ->
% Output any outline foreign_code to the appropriate foreign
% language file.
list__foldl(output_foreign_file(MLDS),
set__to_sorted_list(ForeignLangs))
;
% An I/O error occurred; output_to_file has already reported
% an error message, so we don't need to do anything here.
[]
).
:- pred output_foreign_file(mlds::in, foreign_language::in,
io__state::di, io__state::uo) is det.
output_foreign_file(MLDS, ForeignLang) -->
{ ModuleName = mlds__get_module_name(MLDS) },
{ handle_foreign_lang(ForeignLang, Extension, CodeGenerator) },
module_name_to_file_name(ModuleName, Extension, yes, File),
output_to_file(File, (pred(di, uo) is det --> CodeGenerator(MLDS))).
:- pred handle_foreign_lang(foreign_language::in, string::out,
pred(mlds, io__state, io__state)::out(pred(in, di, uo) is det))
is det.
handle_foreign_lang(managed_cplusplus, "__cpp_code.cpp",
output_managed_code(managed_cplusplus)).
handle_foreign_lang(csharp, "__csharp_code.cs", output_managed_code(csharp)).
handle_foreign_lang(c, _, _) :-
sorry(this_file, "language C foreign code not supported").
handle_foreign_lang(il, _, _) :-
sorry(this_file, "language IL foreign code not supported").
%
% Generate the `.il' file.
% Returns the set of foreign language
%
:- pred output_assembler(mlds, set(foreign_language), io__state, io__state).
:- mode output_assembler(in, out, di, uo) is det.
output_assembler(MLDS, ForeignLangs) -->
{ MLDS = mlds(ModuleName, _ForeignCode, _Imports, _Defns) },
output_src_start(ModuleName),
io__nl,
generate_il(MLDS, ILAsm0, ForeignLangs),
% 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.