Patchwork [v2,7/9] compile: New 'compile print'

login
register
mail settings
Submitter Jan Kratochvil
Date April 6, 2015, 5:27 p.m.
Message ID <20150406172716.31404.8551.stgit@host1.jankratochvil.net>
Download mbox | patch
Permalink /patch/6023/
State New
Headers show

Comments

Jan Kratochvil - April 6, 2015, 5:27 p.m.
It is planned the existing GDB command 'print' will be able to evaluate its
expressions using the compiler.  There will be some option to choose between
the existing GDB evaluation and the compiler evaluation.  But as an
intermediate step this patch provides the expression printing feature as a new
command.

I can imagine it could be also called 'maintenance compile print' as in the
future one should be able to use its functionality by the normal 'print'
command.

There was a discussion with Eli about the command name:
	https://sourceware.org/ml/gdb-patches/2015-03/msg00880.html
As there were no other comments yet I haven't renamed it yet, before there is
some confirmation about settlement on the final name.

Support for the GDB '@' operator to create arrays has been submitted for GCC:
	[gcc patch] libcc1: '@' GDB array operator
	https://gcc.gnu.org/ml/gcc-patches/2015-03/msg01451.html


gdb/ChangeLog
2015-03-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Phil Muldoon  <pmuldoon@redhat.com>

	* NEWS (Changes since GDB 7.9): Add compile print.
	* compile/compile-c-support.c (add_code_header, add_code_footer)
	(c_compute_program): Add COMPILE_I_PRINT_ADDRESS_SCOPE and
	COMPILE_I_PRINT_VALUE_SCOPE.
	* compile/compile-internal.h (COMPILE_I_PRINT_OUT_ARG_TYPE)
	(COMPILE_I_PRINT_OUT_ARG, COMPILE_I_EXPR_VAL, COMPILE_I_EXPR_PTR_TYPE):
	New.
	* compile/compile-object-load.c: Include block.h.
	(get_out_value_type): New function.
	(compile_object_load): Handle COMPILE_I_PRINT_ADDRESS_SCOPE and
	COMPILE_I_PRINT_VALUE_SCOPE.  Set compile_module's OUT_VALUE_ADDR and
	OUT_VALUE_TYPE.
	* compile/compile-object-load.h (struct compile_module): Add fields
	out_value_addr and out_value_type.
	* compile/compile-object-run.c: Include valprint.h and compile.h.
	(struct do_module_cleanup): Add fields out_value_addr and
	out_value_type.
	(do_module_cleanup): Handle COMPILE_I_PRINT_ADDRESS_SCOPE and
	COMPILE_I_PRINT_VALUE_SCOPE.
	(compile_object_run): Propagate out_value_addr and out_value_type.
	Pass OUT_VALUE_ADDR.
	* compile/compile.c: Include valprint.h.
	(compile_print_value, compile_print_command): New functions.
	(eval_compile_command): Handle failed COMPILE_I_PRINT_ADDRESS_SCOPE.
	(_initialize_compile): Install compile_print_command.
	* compile/compile.h (compile_print_value): New prototype.
	* defs.h (enum compile_i_scope_types): Add
	COMPILE_I_PRINT_ADDRESS_SCOPE and COMPILE_I_PRINT_VALUE_SCOPE.

gdb/doc/ChangeLog
2015-03-26  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Compiling and Injecting Code): Add compile print.

gdb/testsuite/ChangeLog
2015-03-26  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.compile/compile-print.c: New file.
	* gdb.compile/compile-print.exp: New file.
---
 gdb/NEWS                                    |    3 +
 gdb/compile/compile-c-support.c             |   43 ++++++++
 gdb/compile/compile-internal.h              |    4 +
 gdb/compile/compile-object-load.c           |  141 ++++++++++++++++++++++++++-
 gdb/compile/compile-object-load.h           |    8 ++
 gdb/compile/compile-object-run.c            |   31 ++++++
 gdb/compile/compile.c                       |   73 ++++++++++++++
 gdb/compile/compile.h                       |    2 
 gdb/defs.h                                  |    9 ++
 gdb/doc/gdb.texinfo                         |   19 ++++
 gdb/testsuite/gdb.compile/compile-print.c   |   32 ++++++
 gdb/testsuite/gdb.compile/compile-print.exp |   58 +++++++++++
 12 files changed, 417 insertions(+), 6 deletions(-)
 create mode 100644 gdb/testsuite/gdb.compile/compile-print.c
 create mode 100644 gdb/testsuite/gdb.compile/compile-print.exp
Eli Zaretskii - April 6, 2015, 5:41 p.m.
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Cc: Phil Muldoon <pmuldoon@redhat.com>
> Date: Mon, 06 Apr 2015 19:27:16 +0200
> 
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -53,6 +53,9 @@ record btrace bts
>  record bts
>    Start branch trace recording using Branch Trace Store (BTS) format.
>  
> +compile print
> +  Evaluate expression with the compiler and print result.

Perhaps "by using the compiler" or "by compiling and running it"?
Saying "with the compiler" is slightly incorrect here, and might even
be confusing, if some non-native speaker interprets the "with" as
referring to "expression" and not to "evaluate".

> +  add_cmd ("print", class_obscure, compile_print_command,
> +	   _("\
> +Evaluate EXPR with the compiler and print result.\n\

Same here.

> +Alternatively, you can type the expression interactively.\n\
> +You can invoke this mode when no argument is given to the command\n\
> +(i.e., \"compile print\" is typed with nothing after it).  An\n\
> +interactive prompt will be shown allowing you to enter multiple\n\
> +lines of source code.  Type a line containing \"end\" to indicate\n\
> +the end of the source code.\n\

I suggest to rephrase:

  Alternatively, you can type a multiline expression by invoking
  this command with no argument.  GDB will then prompt for the
  expression interactively; type a line containing "end" to
  indicate the end of the expression.

> +@table @code
> +@item compile print @var{expr}
> +@itemx compile print /@var{f} @var{expr}
> +Compile @var{expr} with the compiler language found as the current

"Compile and execute", I would think.

The documentation parts are OK with those changes.
Paul_Koning@Dell.com - April 6, 2015, 5:49 p.m.
> On Apr 6, 2015, at 1:41 PM, Eli Zaretskii <eliz@gnu.org> wrote:

> 

>> From: Jan Kratochvil <jan.kratochvil@redhat.com>

>> Cc: Phil Muldoon <pmuldoon@redhat.com>

>> Date: Mon, 06 Apr 2015 19:27:16 +0200

>> 

>> --- a/gdb/NEWS

>> +++ b/gdb/NEWS

>> @@ -53,6 +53,9 @@ record btrace bts

>> record bts

>>   Start branch trace recording using Branch Trace Store (BTS) format.

>> 

>> +compile print

>> +  Evaluate expression with the compiler and print result.

> 

> Perhaps "by using the compiler" or "by compiling and running it"?

> Saying "with the compiler" is slightly incorrect here, and might even

> be confusing, if some non-native speaker interprets the "with" as

> referring to "expression" and not to "evaluate”.


That’s still not fully clear.  If it actually is compile-and-run (as opposed to using the compiler to evaluate a compile-time expression), where does it run?  Presumably on the target.  Does it only work for native gdb?  Does it work with gdbserver?  It would be good to state the limitations and assumptions.

Also, “the compiler” begs the question: which one, i.e., how is “the compiler” found?

	paul
Jan Kratochvil - April 6, 2015, 5:52 p.m.
On Mon, 06 Apr 2015 19:49:28 +0200, Paul_Koning@dell.com wrote:
> where does it run?  Presumably on the target.

Yes.

> Does it only work for native gdb?

Yes.

> Does it work with gdbserver?

Yes.

> It would be good to state the limitations and assumptions.

I am not aware of any, there may be needed some target-specific support like
for s390 gcc -m31 but in general it should work.

I could pick some 10 years old discussion that NEWS file should not contain
limitations, that those are just bugs for future ChangeLog entries.


> Also, “the compiler” begs the question: which one, i.e., how is “the
> compiler” found?

A complicated way, for non-native targets an appropriate cross-gcc should be
found.


Jan
Paul_Koning@Dell.com - April 6, 2015, 5:58 p.m.
> On Apr 6, 2015, at 1:52 PM, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> 
> On Mon, 06 Apr 2015 19:49:28 +0200, Paul_Koning@dell.com wrote:
>> where does it run?  Presumably on the target.
> 
> Yes.
> 
>> Does it only work for native gdb?
> 
> Yes.
> 
>> Does it work with gdbserver?
> 
> Yes.
> 
>> It would be good to state the limitations and assumptions.
> 
> I am not aware of any, there may be needed some target-specific support like
> for s390 gcc -m31 but in general it should work.
> 
> I could pick some 10 years old discussion that NEWS file should not contain
> limitations, that those are just bugs for future ChangeLog entries.

I meant limitations such as “native only”.   But given that it works across the board (give or take bugs) I agree that NEWS doesn’t need to say anything further.
> 
> 
>> Also, “the compiler” begs the question: which one, i.e., how is “the
>> compiler” found?
> 
> A complicated way, for non-native targets an appropriate cross-gcc should be
> found.

Ok.  I would not expect that to be documented in NEWS, but it seems like it should be in gdb.texinfo or some similar spot.  I have a whole pile of cross-compilers in various places; I would need to understand what the algorithm looks like to be able to ensure that the correct one is found.  

	paul
Jan Kratochvil - April 6, 2015, 6:46 p.m.
On Mon, 06 Apr 2015 19:52:50 +0200, Jan Kratochvil wrote:
> On Mon, 06 Apr 2015 19:49:28 +0200, Paul_Koning@dell.com wrote:
> > Does it only work for native gdb?
> 
> Yes.

No; that was a mistake but that was probably clear from the text below.


Jan
Jan Kratochvil - April 8, 2015, 8:03 p.m.
On Mon, 06 Apr 2015 19:58:31 +0200, Paul_Koning@dell.com wrote:
> On Apr 6, 2015, at 1:52 PM, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> > On Mon, 06 Apr 2015 19:49:28 +0200, Paul_Koning@dell.com wrote:
> > > Also, “the compiler” begs the question: which one, i.e., how is “the
> > > compiler” found?
> > 
> > A complicated way, for non-native targets an appropriate cross-gcc should be
> > found.
> 
> Ok.  I would not expect that to be documented in NEWS, but it seems like it
> should be in gdb.texinfo or some similar spot.  I have a whole pile of
> cross-compilers in various places; I would need to understand what the
> algorithm looks like to be able to ensure that the correct one is found.  

I find it should "just work":
gdb:
	compile_to_object():
	ARCH(-[^-]*)?-OS
	ARCH=gdbarch_gnu_triplet_regexp
		i386/x86_64: (x86_64|i.86)
		s390/s390x: s390x?
		*: bfd_arch_info::arch_name
	OS=gdb_osabi_names.regexp
		GNU/Linux: linux(-gnu)?
		*: ""
gcc:
	libcc1_set_arguments()->find_compiler():
		$PATH

Just make your cross-compiler accessible by $PATH and cross your fingers.
If it does not work it is a bug that should be filed+fixed.

It is questionable whether it should be in GDB manual or GCC manual.
So far the GCC manual does not mention GCC's libcc1 GDB interface at all.


Jan
Paul_Koning@Dell.com - April 8, 2015, 8:11 p.m.
> On Apr 6, 2015, at 1:58 PM, paul_koning@dell.com wrote:
> 
> 
>> On Apr 6, 2015, at 1:52 PM, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
>> ...
>>> Also, “the compiler” begs the question: which one, i.e., how is “the
>>> compiler” found?
>> 
>> A complicated way, for non-native targets an appropriate cross-gcc should be
>> found.
> 
> Ok.  I would not expect that to be documented in NEWS, but it seems like it should be in gdb.texinfo or some similar spot.  I have a whole pile of cross-compilers in various places; I would need to understand what the algorithm looks like to be able to ensure that the correct one is found.  

A related question: what controls the compiler switches sent to the compiler for this operation?  In some cases, specific switches may be needed to produce the correct flavor of code generation, say -msoft-float or -mabi=xyz.

	paul
Jan Kratochvil - April 8, 2015, 8:33 p.m.
On Wed, 08 Apr 2015 22:11:58 +0200, Paul_Koning@dell.com wrote:
> A related question: what controls the compiler switches sent to the compiler
> for this operation?  In some cases, specific switches may be needed to
> produce the correct flavor of code generation, say -msoft-float or
> -mabi=xyz.

gdbarch_gcc_target_options()
DW_AT_producer - -grecord-gcc-switches - gcc default if DWARF is present
Some GDB overrides like -O0 -gdwarf-4 -fPIE etc.

The specific switched you mention should get applied by DW_AT_producer.
Some basic ones may be provided already by gdbarch_gcc_target_options().


Jan
Eli Zaretskii - April 9, 2015, 7:39 a.m.
> Date: Wed, 8 Apr 2015 22:03:15 +0200
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Cc: eliz@gnu.org, gdb-patches@sourceware.org, pmuldoon@redhat.com
> 
> Just make your cross-compiler accessible by $PATH and cross your fingers.
> If it does not work it is a bug that should be filed+fixed.
> 
> It is questionable whether it should be in GDB manual or GCC manual.
> So far the GCC manual does not mention GCC's libcc1 GDB interface at all.

"Cross your fingers" is not a good proactive policy for someone who
has several different compilers installed.  They need the information
requested by Paul to adapt this feature to their complex setups, which
we here will never be able to second-guess.

So I think we should tell the story in the manual, yes.
Jan Kratochvil - April 11, 2015, 7:20 p.m.
On Mon, 06 Apr 2015 19:41:03 +0200, Eli Zaretskii wrote:
> Perhaps "by using the compiler" or "by compiling and running it"?

"by compiling" is not clear to me it uses gcc.  Used the first one.


> > +Alternatively, you can type the expression interactively.\n\
> > +You can invoke this mode when no argument is given to the command\n\
> > +(i.e., \"compile print\" is typed with nothing after it).  An\n\
> > +interactive prompt will be shown allowing you to enter multiple\n\
> > +lines of source code.  Type a line containing \"end\" to indicate\n\
> > +the end of the source code.\n\
> 
> I suggest to rephrase:
> 
>   Alternatively, you can type a multiline expression by invoking
>   this command with no argument.  GDB will then prompt for the
>   expression interactively; type a line containing "end" to
>   indicate the end of the expression.

OK; although the same text was also at the "compile code" command so I have
also updated it in this patch.


Thanks,
Jan

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 884c381..8dd3767 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -53,6 +53,9 @@  record btrace bts
 record bts
   Start branch trace recording using Branch Trace Store (BTS) format.
 
+compile print
+  Evaluate expression with the compiler and print result.
+
 * New options
 
 set max-completions
diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c
index 48a17e2..86b02cf 100644
--- a/gdb/compile/compile-c-support.c
+++ b/gdb/compile/compile-c-support.c
@@ -190,6 +190,23 @@  add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
 			") {\n",
 			buf);
       break;
+    case COMPILE_I_PRINT_ADDRESS_SCOPE:
+    case COMPILE_I_PRINT_VALUE_SCOPE:
+      fputs_unfiltered ("#include <string.h>\n"
+			"void "
+			GCC_FE_WRAPPER_FUNCTION
+			" (struct "
+			COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
+			" *"
+			COMPILE_I_SIMPLE_REGISTER_ARG_NAME
+			", "
+			COMPILE_I_PRINT_OUT_ARG_TYPE
+			" "
+			COMPILE_I_PRINT_OUT_ARG
+			") {\n",
+			buf);
+      break;
+
     case COMPILE_I_RAW_SCOPE:
       break;
     default:
@@ -207,6 +224,8 @@  add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
   switch (type)
     {
     case COMPILE_I_SIMPLE_SCOPE:
+    case COMPILE_I_PRINT_ADDRESS_SCOPE:
+    case COMPILE_I_PRINT_VALUE_SCOPE:
       fputs_unfiltered ("}\n", buf);
       break;
     case COMPILE_I_RAW_SCOPE:
@@ -369,7 +388,9 @@  c_compute_program (struct compile_instance *inst,
 
   add_code_header (inst->scope, buf);
 
-  if (inst->scope == COMPILE_I_SIMPLE_SCOPE)
+  if (inst->scope == COMPILE_I_SIMPLE_SCOPE
+      || inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
+      || inst->scope == COMPILE_I_PRINT_VALUE_SCOPE)
     {
       ui_file_put (var_stream, ui_file_write_for_put, buf);
       fputs_unfiltered ("#pragma GCC user_expression\n", buf);
@@ -383,7 +404,25 @@  c_compute_program (struct compile_instance *inst,
     fputs_unfiltered ("{\n", buf);
 
   fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf);
-  fputs_unfiltered (input, buf);
+
+  switch (inst->scope)
+    {
+    case COMPILE_I_PRINT_ADDRESS_SCOPE:
+    case COMPILE_I_PRINT_VALUE_SCOPE:
+      fprintf_unfiltered (buf,
+"__auto_type " COMPILE_I_EXPR_VAL " = %s;\n"
+"typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n"
+"memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" COMPILE_I_EXPR_VAL ",\n"
+	 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
+			  , input, input,
+			  (inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
+			   ? "&" : ""));
+      break;
+    default:
+      fputs_unfiltered (input, buf);
+      break;
+    }
+
   fputs_unfiltered ("\n", buf);
 
   /* For larger user expressions the automatic semicolons may be
diff --git a/gdb/compile/compile-internal.h b/gdb/compile/compile-internal.h
index c369d46..b1a5a88 100644
--- a/gdb/compile/compile-internal.h
+++ b/gdb/compile/compile-internal.h
@@ -84,6 +84,10 @@  struct compile_c_instance
 #define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG "__gdb_regs"
 #define COMPILE_I_SIMPLE_REGISTER_ARG_NAME "__regs"
 #define COMPILE_I_SIMPLE_REGISTER_DUMMY "_dummy"
+#define COMPILE_I_PRINT_OUT_ARG_TYPE "void *"
+#define COMPILE_I_PRINT_OUT_ARG "__gdb_out_param"
+#define COMPILE_I_EXPR_VAL "__gdb_expr_val"
+#define COMPILE_I_EXPR_PTR_TYPE "__gdb_expr_ptr_type"
 
 /* Call gdbarch_register_name (GDBARCH, REGNUM) and convert its result
    to a form suitable for the compiler source.  The register names
diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index 3c07840..e67e5ba 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -29,6 +29,7 @@ 
 #include "regcache.h"
 #include "inferior.h"
 #include "compile.h"
+#include "block.h"
 #include "arch-utils.h"
 
 /* Helper data for setup_sections.  */
@@ -354,6 +355,114 @@  copy_sections (bfd *abfd, asection *sect, void *data)
   do_cleanups (cleanups);
 }
 
+/* Fetch the type of COMPILE_I_EXPR_PTR_TYPE and COMPILE_I_EXPR_VAL
+   symbols in OBJFILE so we can calculate how much memory to allocate
+   for the out parameter.  This avoids needing a malloc in the generated
+   code.  Throw an error if anything fails.
+   Set *OUT_VALUE_TAKE_ADDRESSP depending whether inferior code should
+   copy COMPILE_I_EXPR_VAL or its address - this depends on __auto_type
+   array-to-pointer type conversion of COMPILE_I_EXPR_VAL, as detected
+   by COMPILE_I_EXPR_PTR_TYPE preserving the array type.  */
+
+static struct type *
+get_out_value_type (struct symbol *func_sym, struct objfile *objfile,
+		    enum compile_i_scope_types scope)
+{
+  struct symbol *gdb_ptr_type_sym, *gdb_val_sym;
+  struct type *gdb_ptr_type, *gdb_type_from_ptr, *gdb_type, *retval;
+  const struct block *block;
+  const struct blockvector *bv;
+  int nblocks = 0;
+  int block_loop = 0;
+
+  bv = SYMTAB_BLOCKVECTOR (func_sym->owner.symtab);
+  nblocks = BLOCKVECTOR_NBLOCKS (bv);
+
+  gdb_ptr_type_sym = NULL;
+  for (block_loop = 0; block_loop < nblocks; block_loop++)
+    {
+      struct symbol *function;
+      const struct block *function_block;
+
+      block = BLOCKVECTOR_BLOCK (bv, block_loop);
+      if (BLOCK_FUNCTION (block) != NULL)
+	continue;
+      gdb_val_sym = block_lookup_symbol (block, COMPILE_I_EXPR_VAL, VAR_DOMAIN);
+      if (gdb_val_sym == NULL)
+	continue;
+
+      function_block = block;
+      while (function_block != BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)
+	     && function_block != BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
+	{
+	  function_block = BLOCK_SUPERBLOCK (function_block);
+	  function = BLOCK_FUNCTION (function_block);
+	  if (function != NULL)
+	    break;
+	}
+      if (function != NULL
+	  && (BLOCK_SUPERBLOCK (function_block)
+	      == BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))
+	  && (strcmp (SYMBOL_LINKAGE_NAME (function), GCC_FE_WRAPPER_FUNCTION)
+	      == 0))
+	break;
+    }
+  if (block_loop == nblocks)
+    error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
+
+  gdb_type = SYMBOL_TYPE (gdb_val_sym);
+  CHECK_TYPEDEF (gdb_type);
+
+  gdb_ptr_type_sym = block_lookup_symbol (block, COMPILE_I_EXPR_PTR_TYPE,
+					  VAR_DOMAIN);
+  if (gdb_ptr_type_sym == NULL)
+    error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
+  gdb_ptr_type = SYMBOL_TYPE (gdb_ptr_type_sym);
+  CHECK_TYPEDEF (gdb_ptr_type);
+  if (TYPE_CODE (gdb_ptr_type) != TYPE_CODE_PTR)
+    error (_("Type of \"%s\" is not a pointer"), COMPILE_I_EXPR_PTR_TYPE);
+  gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_ptr_type);
+
+  if (types_deeply_equal (gdb_type, gdb_type_from_ptr))
+    {
+      if (scope != COMPILE_I_PRINT_ADDRESS_SCOPE)
+	error (_("Expected address scope in compiled module \"%s\"."),
+	       objfile_name (objfile));
+      return gdb_type;
+    }
+
+  if (TYPE_CODE (gdb_type) != TYPE_CODE_PTR)
+    error (_("Invalid type code %d of symbol \"%s\" "
+	     "in compiled module \"%s\"."),
+	   TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_VAL,
+	   objfile_name (objfile));
+  
+  switch (TYPE_CODE (gdb_type_from_ptr))
+    {
+    case TYPE_CODE_ARRAY:
+      retval = gdb_type_from_ptr;
+      gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_type_from_ptr);
+      break;
+    case TYPE_CODE_FUNC:
+      retval = gdb_type_from_ptr;
+      break;
+    default:
+      error (_("Invalid type code %d of symbol \"%s\" "
+	       "in compiled module \"%s\"."),
+	     TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_PTR_TYPE,
+	     objfile_name (objfile));
+    }
+  if (!types_deeply_equal (gdb_type_from_ptr,
+			   TYPE_TARGET_TYPE (gdb_type)))
+    error (_("Referenced types do not match for symbols \"%s\" and \"%s\" "
+	     "in compiled module \"%s\"."),
+	   COMPILE_I_EXPR_PTR_TYPE, COMPILE_I_EXPR_VAL,
+	   objfile_name (objfile));
+  if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE)
+    return NULL;
+  return retval;
+}
+
 /* Fetch the type of first parameter of FUNC_SYM.
    Return NULL if FUNC_SYM has no parameters.  Throw an error otherwise.  */
 
@@ -440,7 +549,9 @@  store_regs (struct type *regs_type, CORE_ADDR regs_base)
    Caller must fully dispose the return value by calling compile_object_run.
    SOURCE_FILE's copy is stored into the returned object.
    Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this
-   function returns.  */
+   function returns.
+   Function returns NULL only for COMPILE_I_PRINT_ADDRESS_SCOPE when
+   COMPILE_I_PRINT_VALUE_SCOPE should have been used instead.  */
 
 struct compile_module *
 compile_object_load (const char *object_file, const char *source_file,
@@ -449,7 +560,7 @@  compile_object_load (const char *object_file, const char *source_file,
   struct cleanup *cleanups, *cleanups_free_objfile;
   bfd *abfd;
   struct setup_sections_data setup_sections_data;
-  CORE_ADDR addr, regs_addr;
+  CORE_ADDR addr, regs_addr, out_value_addr = 0;
   struct symbol *func_sym;
   struct type *func_type;
   struct bound_minimal_symbol bmsym;
@@ -459,7 +570,7 @@  compile_object_load (const char *object_file, const char *source_file,
   struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
   unsigned dptr_type_len = TYPE_LENGTH (dptr_type);
   struct compile_module *retval;
-  struct type *regs_type;
+  struct type *regs_type, *out_value_type = NULL;
   char *filename, **matching;
   struct objfile *objfile;
   int expect_parameters;
@@ -521,6 +632,11 @@  compile_object_load (const char *object_file, const char *source_file,
       expect_parameters = 0;
       expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
       break;
+    case COMPILE_I_PRINT_ADDRESS_SCOPE:
+    case COMPILE_I_PRINT_VALUE_SCOPE:
+      expect_parameters = 2;
+      expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
+      break;
     default:
       internal_error (__FILE__, __LINE__, _("invalid scope %d"), scope);
     }
@@ -597,6 +713,23 @@  compile_object_load (const char *object_file, const char *source_file,
       store_regs (regs_type, regs_addr);
     }
 
+  if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE
+      || scope == COMPILE_I_PRINT_VALUE_SCOPE)
+    {
+      out_value_type = get_out_value_type (func_sym, objfile, scope);
+      if (out_value_type == NULL)
+	{
+	  do_cleanups (cleanups);
+	  return NULL;
+	}
+      check_typedef (out_value_type);
+      out_value_addr = gdbarch_infcall_mmap (target_gdbarch (),
+					     TYPE_LENGTH (out_value_type),
+					     (GDB_MMAP_PROT_READ
+					      | GDB_MMAP_PROT_WRITE));
+      gdb_assert (out_value_addr != 0);
+    }
+
   discard_cleanups (cleanups_free_objfile);
   do_cleanups (cleanups);
 
@@ -607,5 +740,7 @@  compile_object_load (const char *object_file, const char *source_file,
   retval->regs_addr = regs_addr;
   retval->scope = scope;
   retval->scope_data = scope_data;
+  retval->out_value_addr = out_value_addr;
+  retval->out_value_type = out_value_type;
   return retval;
 }
diff --git a/gdb/compile/compile-object-load.h b/gdb/compile/compile-object-load.h
index 94f77be..ac72a6a 100644
--- a/gdb/compile/compile-object-load.h
+++ b/gdb/compile/compile-object-load.h
@@ -37,6 +37,14 @@  struct compile_module
 
   /* User data for SCOPE in use.  */
   void *scope_data;
+
+  /* Inferior parameter out value address or NULL if the inferior function does not
+     have one.  */
+  CORE_ADDR out_value_addr;
+
+  /* Inferior parameter out value type or NULL if the inferior function does not
+     have one.  */
+  struct type *out_value_type;
 };
 
 extern struct compile_module *compile_object_load (const char *object_file,
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
index c454bd1..216cbf9 100644
--- a/gdb/compile/compile-object-run.c
+++ b/gdb/compile/compile-object-run.c
@@ -25,6 +25,8 @@ 
 #include "compile-internal.h"
 #include "dummy-frame.h"
 #include "block.h"
+#include "valprint.h"
+#include "compile.h"
 
 /* Helper for do_module_cleanup.  */
 
@@ -41,6 +43,10 @@  struct do_module_cleanup
   enum compile_i_scope_types scope;
   void *scope_data;
 
+  /* Copy from struct compile_module.  */
+  CORE_ADDR out_value_addr;
+  struct type *out_value_type;
+
   /* objfile_name of our objfile.  */
   char objfile_name_string[1];
 };
@@ -56,7 +62,21 @@  do_module_cleanup (void *arg)
   struct objfile *objfile;
 
   if (data->executedp != NULL)
-    *data->executedp = 1;
+    {
+      *data->executedp = 1;
+
+      /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
+	 no longer exists there.  */
+      if (data->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
+	  || data->scope == COMPILE_I_PRINT_VALUE_SCOPE)
+	{
+	  struct value *addr_value;
+	  struct type *ptr_type = lookup_pointer_type (data->out_value_type);
+
+	  addr_value = value_from_pointer (ptr_type, data->out_value_addr);
+	  compile_print_value (value_ind (addr_value), data->scope_data);
+	}
+    }
 
   ALL_OBJFILES (objfile)
     if ((objfile->flags & OBJF_USERLOADED) == 0
@@ -104,6 +124,8 @@  compile_object_run (struct compile_module *module)
   strcpy (data->objfile_name_string, objfile_name_s);
   data->scope = module->scope;
   data->scope_data = module->scope_data;
+  data->out_value_addr = module->out_value_addr;
+  data->out_value_type = module->out_value_type;
 
   xfree (module->source_file);
   xfree (module);
@@ -133,6 +155,13 @@  compile_object_run (struct compile_module *module)
 			  (TYPE_FIELD_TYPE (func_type, current_arg), regs_addr);
 	  ++current_arg;
 	}
+      if (TYPE_NFIELDS (func_type) >= 2)
+	{
+	  gdb_assert (data->out_value_addr != 0);
+	  vargs[current_arg] = value_from_pointer
+	       (TYPE_FIELD_TYPE (func_type, current_arg), data->out_value_addr);
+	  ++current_arg;
+	}
       gdb_assert (current_arg == TYPE_NFIELDS (func_type));
       call_function_by_hand_dummy (func_val, TYPE_NFIELDS (func_type), vargs,
 				   do_module_cleanup, data);
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index ab19a5d..c4b8f2e 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -38,6 +38,7 @@ 
 #include "target.h"
 #include "osabi.h"
 #include "gdb_wait.h"
+#include "valprint.h"
 
 
 
@@ -163,6 +164,49 @@  compile_code_command (char *arg, int from_tty)
   do_cleanups (cleanup);
 }
 
+/* Callback for compile_print_command.  */
+
+void
+compile_print_value (struct value *val, void *data_voidp)
+{
+  const struct format_data *fmtp = data_voidp;
+
+  print_value (val, fmtp);
+}
+
+/* Handle the input from the 'compile print' command.  The "compile
+   print" command is used to evaluate and print an expression that may
+   contain calls to the GCC compiler.  The language expected in this
+   compile command is the language currently set in GDB.  */
+
+static void
+compile_print_command (char *arg_param, int from_tty)
+{
+  const char *arg = arg_param;
+  struct cleanup *cleanup;
+  enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
+  struct format_data *fmtp;
+
+  cleanup = make_cleanup_restore_integer (&interpreter_async);
+  interpreter_async = 0;
+
+  fmtp = print_command_parse_format (&arg, "compile print");
+
+  if (arg && *arg)
+    eval_compile_command (NULL, arg, scope, fmtp);
+  else
+    {
+      struct command_line *l = get_command_line (compile_control, "");
+
+      make_cleanup_free_command_lines (&l);
+      l->control_u.compile.scope = scope;
+      l->control_u.compile.scope_data = fmtp;
+      execute_control_command_untraced (l);
+    }
+
+  do_cleanups (cleanup);
+}
+
 /* A cleanup function to remove a directory and all its contents.  */
 
 static void
@@ -576,6 +620,14 @@  eval_compile_command (struct command_line *cmd, const char *cmd_string,
       make_cleanup (cleanup_unlink_file, source_file);
       compile_module = compile_object_load (object_file, source_file,
 					    scope, scope_data);
+      if (compile_module == NULL)
+	{
+	  gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
+	  do_cleanups (cleanup_xfree);
+	  eval_compile_command (cmd, cmd_string,
+				COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
+	  return;
+	}
       discard_cleanups (cleanup_unlink);
       do_cleanups (cleanup_xfree);
       compile_object_run (compile_module);
@@ -654,6 +706,27 @@  Usage: compile file [-r|-raw] [filename]\n\
 	       &compile_command_list);
   set_cmd_completer (c, filename_completer);
 
+  add_cmd ("print", class_obscure, compile_print_command,
+	   _("\
+Evaluate EXPR with the compiler and print result.\n\
+\n\
+Usage: compile print[/FMT] [EXPR]\n\
+\n\
+The expression may be specified on the same line as the command, e.g.:\n\
+\n\
+    compile print i\n\
+\n\
+Alternatively, you can type the expression interactively.\n\
+You can invoke this mode when no argument is given to the command\n\
+(i.e., \"compile print\" is typed with nothing after it).  An\n\
+interactive prompt will be shown allowing you to enter multiple\n\
+lines of source code.  Type a line containing \"end\" to indicate\n\
+the end of the source code.\n\
+\n\
+EXPR may be preceded with /FMT, where FMT is a format letter\n\
+but no count or size letter (see \"x\" command)."),
+	   &compile_command_list);
+
   add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
 Set compile command debugging."), _("\
 Show compile command debugging."), _("\
diff --git a/gdb/compile/compile.h b/gdb/compile/compile.h
index a973167..6e108c7 100644
--- a/gdb/compile/compile.h
+++ b/gdb/compile/compile.h
@@ -101,4 +101,6 @@  extern void compile_dwarf_bounds_to_c (struct ui_file *stream,
 				       const gdb_byte *op_end,
 				       struct dwarf2_per_cu_data *per_cu);
 
+extern void compile_print_value (struct value *val, void *data_voidp);
+
 #endif /* GDB_COMPILE_H */
diff --git a/gdb/defs.h b/gdb/defs.h
index 6476f80..f6e010c 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -70,6 +70,15 @@  enum compile_i_scope_types
     /* Do not wrap the expression,
        it has to provide function "_gdb_expr" on its own.  */
     COMPILE_I_RAW_SCOPE,
+
+    /* A printable expression scope.  Wrap an expression into a scope
+       suitable for the "compile print" command.  It uses the generic
+       function name "_gdb_expr".  COMPILE_I_PRINT_ADDRESS_SCOPE variant
+       is the usual one, taking address of the object.
+       COMPILE_I_PRINT_VALUE_SCOPE is needed for arrays where the array
+       name already specifies its address.  */
+    COMPILE_I_PRINT_ADDRESS_SCOPE,
+    COMPILE_I_PRINT_VALUE_SCOPE,
   };
 
 /* Just in case they're not defined in stdio.h.  */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d794893..3eaf9d3 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -17223,6 +17223,25 @@  compile file /home/user/example.c
 @end smallexample
 @end table
 
+@table @code
+@item compile print @var{expr}
+@itemx compile print /@var{f} @var{expr}
+Compile @var{expr} with the compiler language found as the current
+language in @value{GDBN} (@pxref{Languages}).  By default the
+value of @var{expr} is printed in a format appropriate to its data type;
+you can choose a different format by specifying @samp{/@var{f}}, where
+@var{f} is a letter specifying the format; see @ref{Output Formats,,Output
+Formats}.
+
+@item compile print
+@itemx compile print /@var{f}
+@cindex reprint the last value
+Alternatively you can enter the expression (source code producing it) as
+multiple lines of text.  To enter this mode, invoke the @samp{compile print}
+command without any text following the command.  This will start the
+multiple-line editor.
+@end table
+
 @subsection Caveats when using the @code{compile} command
 
 There are a few caveats to keep in mind when using the @code{compile}
diff --git a/gdb/testsuite/gdb.compile/compile-print.c b/gdb/testsuite/gdb.compile/compile-print.c
new file mode 100644
index 0000000..e83b620
--- /dev/null
+++ b/gdb/testsuite/gdb.compile/compile-print.c
@@ -0,0 +1,32 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2015 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+
+int varint = 10;
+int vararray[] = { 1, 2, 3, 4, 5 };
+int *vararrayp = vararray;
+struct object
+{
+  int field;
+} varobject = { 1 };
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.compile/compile-print.exp b/gdb/testsuite/gdb.compile/compile-print.exp
new file mode 100644
index 0000000..92c6240
--- /dev/null
+++ b/gdb/testsuite/gdb.compile/compile-print.exp
@@ -0,0 +1,58 @@ 
+# Copyright 2015 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if { [prepare_for_testing ${testfile}.exp "$testfile"] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+if {[skip_compile_feature_tests]} {
+    untested "compile command not supported (could not find libcc1 shared library?)"
+    return -1
+}
+
+gdb_test "compile print varint" " = 10"
+gdb_test "compile print vararray" " = \\{1, 2, 3, 4, 5\\}"
+gdb_test "compile print main" " = \\{int \\(void\\)\\} 0x\[0-9a-f\]+"
+
+set test "compile print *vararray@3"
+gdb_test_multiple $test $test {
+    -re " = \\{1, 2, 3\\}\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re ": error: stray '@' in program\r\n.*\r\n$gdb_prompt $" {
+	xfail "$test (gcc does not support '@')"
+    }
+}
+
+set test "compile print *vararrayp@3"
+gdb_test_multiple $test $test {
+    -re " = \\{1, 2, 3\\}\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re ": error: stray '@' in program\r\n.*\r\n$gdb_prompt $" {
+	xfail "$test (gcc does not support '@')"
+    }
+}
+
+gdb_test "compile print/x 256" " = 0x100"
+gdb_test {print $} " = 256"
+
+gdb_test "compile print varobject" { = {field = 1}}