mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 22:35:41 +00:00
Estimated hours taken: 40
Implement nondet pragma C codes.
runtime/mercury_stacks.h:
Define a new macro, mkpragmaframe, for use in the implementation
of nondet pragma C codes. This new macro includes space for a
struct with a given sruct tag in the nondet stack frame being created.
compiler/{prog_data.m,hlds_goal.m}:
Revise the representation of pragma C codes, both as the item and
in the HLDS.
compiler/prog_io_pragma.m:
Parse nondet pragma C declarations.
Fix the indentation in some places.
compiler/llds.m:
Include an extra argument in mkframe instructions. This extra argument
gives the details of the C structure (if any) to be included in the
nondet stack frame to be created.
Generalize the LLDS representation of pragma C codes. Instead of a
fixed sequence of <assign from inputs, user c code, assign to outputs>,
let the sequence contain these elements, as well as arbitrary
compiler-generated C code, in any order and possibly with repetitions.
This flexibility is needed for nondet pragma C codes.
Add a field to pragma C codes to say whether they can call Mercury.
Some optimizations can do a better job if they know that a pragma C
code cannot call Mercury.
Add another field to pragma C codes to give the name of the label
they refer to (if any). This is needed to prevent labelopt from
incorrectly optimizing away the label definition.
Add a new alternative to the type pragma_c_decl, to describe the
declaration of the local variable that points to the save struct.
compiler/llds_out.m:
Output mkframe instructions that specify a struct as invoking the new
mkpragmaframe macro, and make sure that the struct is declared just
before the procedure that uses it.
Other minor changes to keep up with the changes to the representation
of pragma C code in the LLDS, and to make the output look a bit nicer.
compiler/pragma_c_gen.m:
Add code to generate code for nondet pragma C codes. Revise the utility
predicates and their data structures a bit to make this possible.
compiler/code_gen.m:
Add code for the necessary special handling of prologs and epilogs
of procedures defined by nondet pragma C codes. The prologs need
to be modified to include a programmer-defined C structure in the
nondet stack frame and to communicate the location of this structure
to the pragma C code, whereas the functionality of the epilog is
taken care of by the pragma C code itself.
compiler/make_hlds.m:
When creating a proc_info for a procedure defined by a pragma C code,
we used to insert unifications between the headvars and the vars of
the pragma C code into the body goal. We now perform substitutions
instead. This removes a factor that would complicate the generation
of code for nondet pragma C codes.
Pass a moduleinfo down the procedures that warn about singletons
(and other basic scope errors). When checking whether to warn about
an argument of a pragma C code not being mentioned in the C code
fragment, we need to know whether the argument is input or output,
since input variables should appear in some code fragments in a
nondet pragma C code and must not appear in others. The
mode_is_{in,out}put checks need the moduleinfo.
(We do not need to check for any variables being mentioned where
they shouldn't be. The C compiler will fail in the presence of any
errors of that type, and since those variables could be referred
to via macros whose definitions we do not see, we couldn't implement
a reliable test anyway.)
compiler/opt_util.m:
Recognize that some sorts of pragma_c codes cannot affect the data
structures that control backtracking. This allows peepholing to
do a better job on code sequences produced for nondet pragma C codes.
Recognize that the C code strings inside some pragma_c codes refer to
other labels in the procedure. This prevents labelopt from incorrectly
optimizing away these labels.
compiler/dupelim.m:
If a label is referred to from within a C code string, then do not
attempt to optimize it away.
compiler/det_analysis.m:
Remove a now incorrect part of an error message.
compiler/*.m:
Minor changes to conform to changes to the HLDS and LLDS data
structures.
209 lines
6.6 KiB
Mathematica
209 lines
6.6 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1995-1998 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.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% vn_type.m - types for value numbering.
|
|
|
|
% Author: zs.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module vn_type.
|
|
|
|
:- interface.
|
|
:- import_module llds, livemap, options.
|
|
:- import_module bool, getopt, map, set, list, std_util.
|
|
|
|
:- type vn == int.
|
|
|
|
:- type vnlval ---> vn_reg(reg_type, int)
|
|
; vn_temp(reg_type, int)
|
|
; vn_stackvar(int)
|
|
; vn_framevar(int)
|
|
; vn_succip
|
|
; vn_maxfr
|
|
; vn_curfr
|
|
; vn_succfr(vn)
|
|
; vn_prevfr(vn)
|
|
; vn_redoip(vn)
|
|
; vn_succip(vn)
|
|
; vn_hp
|
|
; vn_sp
|
|
; vn_field(maybe(tag), vn, vn)
|
|
; vn_mem_ref(vn).
|
|
|
|
% these lvals do not have vnlval parallels
|
|
% lvar(var)
|
|
|
|
:- type vnrval ---> vn_origlval(vnlval)
|
|
; vn_mkword(tag, vn)
|
|
; vn_const(rval_const)
|
|
; vn_create(tag, list(maybe(rval)),
|
|
bool, int, string)
|
|
; vn_unop(unary_op, vn)
|
|
; vn_binop(binary_op, vn, vn)
|
|
; vn_stackvar_addr(int)
|
|
; vn_framevar_addr(int)
|
|
; vn_heap_addr(vn, int, int).
|
|
|
|
% these rvals do not have vnrval parallels
|
|
% var(var)
|
|
|
|
% given a vnlval, figure out its type
|
|
:- pred vn_type__vnlval_type(vnlval::in, llds_type::out) is det.
|
|
|
|
% given a vnrval, figure out its type
|
|
:- pred vn_type__vnrval_type(vnrval::in, llds_type::out) is det.
|
|
|
|
:- type vn_src ---> src_ctrl(int)
|
|
; src_liveval(vnlval)
|
|
; src_access(vnlval)
|
|
; src_vn(int).
|
|
|
|
:- type vn_node ---> node_shared(vn)
|
|
; node_lval(vnlval)
|
|
; node_origlval(vnlval)
|
|
; node_ctrl(int).
|
|
|
|
:- type vn_instr ---> vn_livevals(lvalset)
|
|
; vn_call(code_addr, code_addr,
|
|
list(liveinfo), call_model)
|
|
; vn_mkframe(string, int, maybe(pragma_c_struct),
|
|
code_addr)
|
|
; vn_label(label)
|
|
; vn_goto(code_addr)
|
|
; vn_computed_goto(vn, list(label))
|
|
; vn_if_val(vn, code_addr)
|
|
; vn_mark_hp(vnlval)
|
|
; vn_restore_hp(vn)
|
|
; vn_store_ticket(vnlval)
|
|
; vn_reset_ticket(vn, reset_trail_reason)
|
|
; vn_discard_ticket
|
|
; vn_mark_ticket_stack(vnlval)
|
|
; vn_discard_tickets_to(vn)
|
|
; vn_incr_sp(int, string)
|
|
; vn_decr_sp(int).
|
|
|
|
:- type parentry == pair(lval, list(rval)).
|
|
:- type parallel ---> parallel(label, label, list(parentry)).
|
|
|
|
:- type vnlvalset == set(vnlval).
|
|
|
|
:- type ctrlmap == map(int, vn_instr).
|
|
:- type flushmap == map(int, flushmapentry).
|
|
:- type flushmapentry == map(vnlval, vn).
|
|
:- type parmap == map(int, list(parallel)).
|
|
|
|
:- type vn_ctrl_tuple ---> tuple(int, ctrlmap, flushmap, int, parmap).
|
|
|
|
:- type vn_params.
|
|
|
|
:- pred vn_type__init_params(option_table(option), vn_params).
|
|
:- mode vn_type__init_params(in, out) is det.
|
|
|
|
:- pred vn_type__bytes_per_word(vn_params, int).
|
|
:- mode vn_type__bytes_per_word(in, out) is det.
|
|
|
|
:- pred vn_type__real_r_regs(vn_params, int).
|
|
:- mode vn_type__real_r_regs(in, out) is det.
|
|
|
|
:- pred vn_type__real_f_regs(vn_params, int).
|
|
:- mode vn_type__real_f_regs(in, out) is det.
|
|
|
|
:- pred vn_type__real_r_temps(vn_params, int).
|
|
:- mode vn_type__real_r_temps(in, out) is det.
|
|
|
|
:- pred vn_type__real_f_temps(vn_params, int).
|
|
:- mode vn_type__real_f_temps(in, out) is det.
|
|
|
|
:- pred vn_type__costof_assign(vn_params, int).
|
|
:- mode vn_type__costof_assign(in, out) is det.
|
|
|
|
:- pred vn_type__costof_intops(vn_params, int).
|
|
:- mode vn_type__costof_intops(in, out) is det.
|
|
|
|
:- pred vn_type__costof_stackref(vn_params, int).
|
|
:- mode vn_type__costof_stackref(in, out) is det.
|
|
|
|
:- pred vn_type__costof_heapref(vn_params, int).
|
|
:- mode vn_type__costof_heapref(in, out) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module int.
|
|
|
|
:- type vn_params ---> vn_params(
|
|
int, % word size in bytes
|
|
% needed for incr_hp; incr_hp
|
|
int, % number of real r regs
|
|
int, % number of real f regs
|
|
int, % number of real r temps
|
|
int, % number of real f temps
|
|
int, % cost of assign
|
|
int, % cost of int operation
|
|
int, % cost of stack reference
|
|
int % cost of heap reference
|
|
).
|
|
|
|
vn_type__init_params(OptionTable, VnParams) :-
|
|
getopt__lookup_int_option(OptionTable, num_real_r_regs, RealRRegs),
|
|
getopt__lookup_int_option(OptionTable, num_real_f_regs, RealFRegs),
|
|
getopt__lookup_int_option(OptionTable, num_real_r_temps, RealRTemps),
|
|
getopt__lookup_int_option(OptionTable, num_real_f_temps, RealFTemps),
|
|
getopt__lookup_int_option(OptionTable, bytes_per_word, WordBytes),
|
|
VnParams = vn_params(WordBytes, RealRRegs, RealFRegs,
|
|
RealRTemps, RealFTemps, 1, 1, 2, 2).
|
|
|
|
vn_type__bytes_per_word(vn_params(BytesPerWord, _, _, _, _, _, _, _, _),
|
|
BytesPerWord).
|
|
vn_type__real_r_regs(vn_params(_, RealRRegs, _, _, _, _, _, _, _),
|
|
RealRRegs).
|
|
vn_type__real_f_regs(vn_params(_, _, RealFRegs, _, _, _, _, _, _),
|
|
RealFRegs).
|
|
vn_type__real_r_temps(vn_params(_, _, _, RealRTemps, _, _, _, _, _),
|
|
RealRTemps).
|
|
vn_type__real_f_temps(vn_params(_, _, _, _, RealFTemps, _, _, _, _),
|
|
RealFTemps).
|
|
vn_type__costof_assign(vn_params(_, _, _, _, _, AssignCost, _, _, _),
|
|
AssignCost).
|
|
vn_type__costof_intops(vn_params(_, _, _, _, _, _, IntOpCost, _, _),
|
|
IntOpCost).
|
|
vn_type__costof_stackref(vn_params(_, _, _, _, _, _, _, StackCost, _),
|
|
StackCost).
|
|
vn_type__costof_heapref(vn_params(_, _, _, _, _, _, _, _, HeapCost),
|
|
HeapCost).
|
|
|
|
vn_type__vnrval_type(vn_origlval(Lval), Type) :-
|
|
vn_type__vnlval_type(Lval, Type).
|
|
vn_type__vnrval_type(vn_create(_, _, _, _, _), data_ptr).
|
|
vn_type__vnrval_type(vn_mkword(_, _), data_ptr). % see comment in llds.m
|
|
vn_type__vnrval_type(vn_const(Const), Type) :-
|
|
llds__const_type(Const, Type).
|
|
vn_type__vnrval_type(vn_unop(UnOp, _), Type) :-
|
|
llds__unop_return_type(UnOp, Type).
|
|
vn_type__vnrval_type(vn_binop(BinOp, _, _), Type) :-
|
|
llds__binop_return_type(BinOp, Type).
|
|
vn_type__vnrval_type(vn_stackvar_addr(_), data_ptr).
|
|
vn_type__vnrval_type(vn_framevar_addr(_), data_ptr).
|
|
vn_type__vnrval_type(vn_heap_addr(_, _, _), data_ptr).
|
|
|
|
vn_type__vnlval_type(vn_reg(RegType, _), Type) :-
|
|
llds__register_type(RegType, Type).
|
|
vn_type__vnlval_type(vn_succip, code_ptr).
|
|
vn_type__vnlval_type(vn_maxfr, data_ptr).
|
|
vn_type__vnlval_type(vn_curfr, data_ptr).
|
|
vn_type__vnlval_type(vn_hp, data_ptr).
|
|
vn_type__vnlval_type(vn_sp, data_ptr).
|
|
vn_type__vnlval_type(vn_temp(RegType, _), Type) :-
|
|
llds__register_type(RegType, Type).
|
|
vn_type__vnlval_type(vn_stackvar(_), word).
|
|
vn_type__vnlval_type(vn_framevar(_), word).
|
|
vn_type__vnlval_type(vn_succip(_), code_ptr).
|
|
vn_type__vnlval_type(vn_redoip(_), code_ptr).
|
|
vn_type__vnlval_type(vn_succfr(_), data_ptr).
|
|
vn_type__vnlval_type(vn_prevfr(_), data_ptr).
|
|
vn_type__vnlval_type(vn_field(_, _, _), word).
|
|
vn_type__vnlval_type(vn_mem_ref(_), word).
|