[m-dev.] for review: document pragma import & nondet pragma c_code

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Nov 5 18:08:07 AEDT 1998


On 05-Nov-1998, Zoltan Somogyi <zs at cs.mu.OZ.AU> wrote:
> 
> > The compiler actually allows either `shared_code' or `duplicate_code'
> > as alternatives to `common_code' -- `shared_code' results in smaller
> > code, `duplicate_code' results in faster but larger code, while with
> > `common_code' the compiler does its best to make the appropriate
> > time-space trade-off.  Is it worth documenting this?
> 
> It must be documented, because it affects the semantics of any "static"
> declarations in the C code; you don't want such declarations to be duplicated
> by the compiler without the user's consent.

The documentation for `pragma c_code' already says the following:

   The C code fragment may declare local variables, but it should not
   declare any static variables unless there is also a Mercury `pragma
   no_inline' declaration (*note Inlining::.) for the procedure.  The
   reason for this is that otherwise the Mercury implementation may inline
   the procedure by duplicating the C code fragment for each call, which
   would result in the program having multiple instances of the static
   variable, rather than a single shared instance.

So I propose the following fix to pragma_c_gen.m:

--------------------

Estimated hours taken: 0.25

compiler/pragma_c_gen.m:
	Change the semantics of `common_code' in nondet pragma c_code
	to match the documentation: don't duplicate it if
	`pragma no_inline' was specified.

Index: compiler/pragma_c_gen.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/pragma_c_gen.m,v
retrieving revision 1.20
diff -u -r1.20 pragma_c_gen.m
--- pragma_c_gen.m	1998/08/12 02:39:56	1.20
+++ pragma_c_gen.m	1998/11/05 07:00:46
@@ -524,9 +524,10 @@
 	{ output_descs_from_arg_info(OutArgs, OutputDescs) },
 
 	code_info__get_module_info(ModuleInfo),
-	{ predicate_module(ModuleInfo, PredId, ModuleName) },
-	{ predicate_name(ModuleInfo, PredId, PredName) },
-	{ predicate_arity(ModuleInfo, PredId, Arity) },
+	{ module_info_pred_info(ModuleInfo, PredId, PredInfo) },
+	{ pred_info_module(PredInfo, ModuleName) },
+	{ pred_info_name(PredInfo, PredName) },
+	{ pred_info_arity(PredInfo, Arity) },
 	{ pragma_c_gen__struct_name(ModuleName, PredName, Arity, ProcId,
 		StructName) },
 	{ SaveStructDecl = pragma_c_struct_ptr_decl(StructName, "LOCALS") },
@@ -606,6 +607,11 @@
 			% is small enough for its duplication not to have
 			% a significant effect on code size. (This form
 			% generates slightly faster code.)
+			% However, if `pragma no_inline' is specified,
+			% then we don't duplicate the code unless the
+			% programmer asked for it -- the code may contain
+			% static variable declarations, so duplicating it
+			% could change the semantics.
 
 			% We use the number of semicolons in the code
 			% as an indication how many C statements it has
@@ -614,6 +620,7 @@
 			Treat = duplicate
 		;
 			Treat = automatic,
+			\+ pred_info_requested_no_inlining(PredInfo),
 			CountSemis = lambda([Char::in, Count0::in, Count::out]
 				is det,
 				( Char = (;) ->
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list