%-----------------------------------------------------------------------------%) % Copyright (C) 1996-2005 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 hlds__hlds_module. :- import_module mdbcomp__prim_data. :- import_module parse_tree__prog_data. :- import_module parse_tree__prog_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::in, foreign_export_decls::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::in, foreign_export_defns::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::in, module_name::in, io::di, io::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::in, (type)::in, string::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::in, (type)::in, string::out) is det. % Succeeds iff the given C type is known by the compiler to be % an integer or pointer type the same size as MR_Word. % :- pred c_type_is_word_sized_int_or_ptr(string::in) is semidet. %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% :- implementation. :- import_module backend_libs__c_util. :- import_module backend_libs__foreign. :- import_module backend_libs__name_mangle. :- import_module backend_libs__proc_label. :- import_module check_hlds__type_util. :- import_module hlds__arg_info. :- import_module hlds__code_model. :- import_module hlds__hlds_pred. :- import_module libs__globals. :- import_module libs__options. :- import_module parse_tree__error_util. :- import_module parse_tree__modules. :- import_module parse_tree__prog_foreign. :- import_module parse_tree__prog_util. :- import_module assoc_list. :- import_module bool. :- import_module int. :- import_module library. :- import_module list. :- import_module map. :- import_module require. :- import_module std_util. :- import_module string. :- import_module term. :- import_module varset. %-----------------------------------------------------------------------------% 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::in, list(pragma_exported_proc)::in, globals::in, module_info::in, list(foreign_export_decl)::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(