%-----------------------------------------------------------------------------% % Copyright (C) 1996-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. %-----------------------------------------------------------------------------% % This module defines predicates to produce the functions which are % exported to a foreign language via a `pragma export' declaration. % Note: any changes here might also require similar changes to the handling % of `pragma import' declarations, which are handled in make_hlds.m. % Main authors: dgj. %-----------------------------------------------------------------------------% :- module backend_libs__export. :- interface. :- import_module parse_tree__prog_data, hlds__hlds_module. :- import_module backend_libs__foreign. :- import_module io. % From the module_info, get a list of foreign_export_decls, % each of which holds information about the declaration % of a foreign function named in a `pragma export' declaration, % which is used to allow a call to be made to a Mercury % procedure from the foreign language. :- pred export__get_foreign_export_decls(module_info, foreign_export_decls). :- mode export__get_foreign_export_decls(in, out) is det. % From the module_info, get a list of foreign_export_defns, % each of which is a string containing the foreign code % for defining a foreign function named in a `pragma export' decl. :- pred export__get_foreign_export_defns(module_info, foreign_export_defns). :- mode export__get_foreign_export_defns(in, out) is det. % Produce an interface file containing declarations for the % exported foreign functions (if required in this foreign % language). :- pred export__produce_header_file(foreign_export_decls, module_name, io__state, io__state). :- mode export__produce_header_file(in, in, di, uo) is det. %-----------------------------------------------------------------------------% % Utilities for generating C code which interfaces with Mercury. % The {MLDS,LLDS}->C backends and fact tables use this code. % Generate C code to convert an rval (represented as a string), from % a C type to a mercury C type (ie. convert strings and floats to % words) and return the resulting C code as a string. :- pred convert_type_to_mercury(string, type, string). :- mode convert_type_to_mercury(in, in, out) is det. % Generate C code to convert an rval (represented as a string), from % a mercury C type to a C type. (ie. convert words to strings and % floats if required) and return the resulting C code as a string. :- pred convert_type_from_mercury(string, type, string). :- mode convert_type_from_mercury(in, in, out) is det. %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% :- implementation. :- import_module backend_libs__foreign. :- import_module parse_tree__modules. :- import_module hlds__hlds_pred, check_hlds__type_util. :- import_module hlds__error_util. :- import_module backend_libs__code_model. :- import_module ll_backend__code_gen, ll_backend__code_util. :- import_module ll_backend__llds_out, ll_backend__arg_info. :- import_module libs__globals, libs__options. :- import_module term, varset. :- import_module library, map, int, string, std_util, assoc_list, require. :- import_module list, bool. %-----------------------------------------------------------------------------% export__get_foreign_export_decls(HLDS, ForeignExportDecls) :- module_info_get_predicate_table(HLDS, PredicateTable), predicate_table_get_preds(PredicateTable, Preds), module_info_get_foreign_decl(HLDS, RevForeignDecls), ForeignDecls = list__reverse(RevForeignDecls), module_info_get_pragma_exported_procs(HLDS, ExportedProcs), module_info_globals(HLDS, Globals), export__get_foreign_export_decls_2(Preds, ExportedProcs, Globals, HLDS, C_ExportDecls), ForeignExportDecls = foreign_export_decls(ForeignDecls, C_ExportDecls). :- pred export__get_foreign_export_decls_2(pred_table, list(pragma_exported_proc), globals, module_info, list(foreign_export_decl)). :- mode export__get_foreign_export_decls_2(in, in, in, in, out) is det. export__get_foreign_export_decls_2(_Preds, [], _, _, []). export__get_foreign_export_decls_2(Preds, [E|ExportedProcs], Globals, Module, C_ExportDecls) :- E = pragma_exported_proc(PredId, ProcId, C_Function, _Ctxt), get_export_info(Preds, PredId, ProcId, Globals, Module, _HowToDeclare, C_RetType, _DeclareReturnVal, _FailureAction, _SuccessAction, HeadArgInfoTypes), get_argument_declarations(HeadArgInfoTypes, no, Module, ArgDecls), C_ExportDecl = foreign_export_decl(c, C_RetType, C_Function, ArgDecls), export__get_foreign_export_decls_2(Preds, ExportedProcs, Globals, Module, C_ExportDecls0), C_ExportDecls = [C_ExportDecl | C_ExportDecls0]. %-----------------------------------------------------------------------------% export__get_foreign_export_defns(Module, ExportedProcsCode) :- module_info_get_pragma_exported_procs(Module, ExportedProcs), module_info_get_predicate_table(Module, PredicateTable), predicate_table_get_preds(PredicateTable, Preds), export__to_c(Preds, ExportedProcs, Module, ExportedProcsCode). % For each exported procedure, produce a C function. % The code we generate is in the form % % MR_declare_entry(