[jit] Add gcc_jit_function_add_comment

David Malcolm dmalcolm@redhat.com
Tue Jan 21 20:36:00 GMT 2014


Committed to dmalcolm/jit branch:

Add gcc_jit_function_add_comment, a way to add a no-op textual comment to
the internal representation of the code.

It will be optimized away, but will be visible in some dumps
and thus may be of use when debugging how client code's internal
representation gets converted to the libgccjit IR.

Internally, it's implemented by adding a dummy label to the IR, where the
name of the label is the text of the comment, wrapped in C-style comment
delimiters.

Currently this is directly visible in the tree and gimple dumps, and
seems to survive all the way through to the end of RTL, in the form
of NOTE_INSN_DELETED_LABEL.  It doesn't make it into the final assembler
output, though.
---
 gcc/jit/ChangeLog.jit                   |  8 ++++++++
 gcc/jit/internal-api.c                  | 21 +++++++++++++++++++++
 gcc/jit/internal-api.h                  |  4 ++++
 gcc/jit/libgccjit.c                     | 11 +++++++++++
 gcc/jit/libgccjit.h                     | 13 +++++++++++++
 gcc/jit/libgccjit.map                   |  1 +
 gcc/testsuite/ChangeLog.jit             |  5 +++++
 gcc/testsuite/jit.dg/test-hello-world.c |  5 +++++
 8 files changed, 68 insertions(+)

diff --git a/gcc/jit/ChangeLog.jit b/gcc/jit/ChangeLog.jit
index 30664be..c899c9a 100644
--- a/gcc/jit/ChangeLog.jit
+++ b/gcc/jit/ChangeLog.jit
@@ -1,3 +1,11 @@
+2014-01-21  David Malcolm  <dmalcolm@redhat.com>
+
+	* internal-api.c (gcc::jit::function::add_comment): New.
+	* internal-api.h (gcc::jit::function::add_comment): New.
+	* libgccjit.c (gcc_jit_function_add_comment): New.
+	* libgccjit.h (gcc_jit_function_add_comment): New.
+	* libgccjit.map: Add gcc_jit_function_add_comment.
+
 2013-10-24  David Malcolm  <dmalcolm@redhat.com>
 
 	* internal-api.c (gcc::jit::function::add_eval): Handle non-NULL
diff --git a/gcc/jit/internal-api.c b/gcc/jit/internal-api.c
index b21aaa6..a92c797 100644
--- a/gcc/jit/internal-api.c
+++ b/gcc/jit/internal-api.c
@@ -923,6 +923,27 @@ add_assignment (location *loc,
 
 void
 gcc::jit::function::
+add_comment (location *loc,
+	     const char *text)
+{
+  gcc_assert (m_kind != GCC_JIT_FUNCTION_IMPORTED);
+
+  /* Wrap the text in C-style comment delimiters.  */
+  size_t sz =
+    (3 /* opening delim */
+     + strlen (text)
+     + 3 /* closing delim */
+     + 1 /* terminator */);
+  char *wrapped = (char *)ggc_internal_alloc_stat (sz);
+  snprintf (wrapped, sz, "/* %s */", text);
+
+  /* For now we simply implement this by adding a dummy label with a name
+     containing the given text.  */
+  add_label (loc, wrapped);
+}
+
+void
+gcc::jit::function::
 add_conditional (location *loc,
 		 rvalue *boolval,
 		 label *on_true,
diff --git a/gcc/jit/internal-api.h b/gcc/jit/internal-api.h
index 01f0bb3..1ec2ea1 100644
--- a/gcc/jit/internal-api.h
+++ b/gcc/jit/internal-api.h
@@ -319,6 +319,10 @@ public:
 		  rvalue *rvalue);
 
   void
+  add_comment (location *loc,
+	       const char *text);
+
+  void
   add_conditional (location *loc,
 		   rvalue *boolval,
 		   label *on_true,
diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c
index 3222144..d094def 100644
--- a/gcc/jit/libgccjit.c
+++ b/gcc/jit/libgccjit.c
@@ -639,6 +639,17 @@ gcc_jit_function_add_conditional (gcc_jit_function *func,
 }
 
 void
+gcc_jit_function_add_comment (gcc_jit_function *func,
+			      gcc_jit_location *loc,
+			      const char *text)
+{
+  RETURN_IF_NOT_FUNC_DEFINITION (func);
+  RETURN_IF_FAIL (text, NULL, "NULL text");
+
+  func->add_comment (loc, text);
+}
+
+void
 gcc_jit_function_add_jump (gcc_jit_function *func,
 			gcc_jit_location *loc,
 			gcc_jit_label *target)
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 0712533..8a6682b 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -668,6 +668,19 @@ gcc_jit_function_add_assignment_op (gcc_jit_function *func,
 				    enum gcc_jit_binary_op op,
 				    gcc_jit_rvalue *rvalue);
 
+/* Add a no-op textual comment to the internal representation of the
+   code.  It will be optimized away, but will be visible in the dumps
+   seen via
+     GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE
+   and
+     GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
+   and thus may be of use when debugging how your project's internal
+   representation gets converted to the libgccjit IR.  */
+extern void
+gcc_jit_function_add_comment (gcc_jit_function *func,
+			      gcc_jit_location *loc,
+			      const char *text);
+
 /* Add evaluation of an rvalue, branching on the result to the
    appropriate label.
 
diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map
index aaa2112..bcb4411 100644
--- a/gcc/jit/libgccjit.map
+++ b/gcc/jit/libgccjit.map
@@ -30,6 +30,7 @@
     gcc_jit_context_zero;
     gcc_jit_function_add_assignment;
     gcc_jit_function_add_assignment_op;
+    gcc_jit_function_add_comment;
     gcc_jit_function_add_conditional;
     gcc_jit_function_add_eval;
     gcc_jit_function_add_jump;
diff --git a/gcc/testsuite/ChangeLog.jit b/gcc/testsuite/ChangeLog.jit
index 18b6d9a..34a558c 100644
--- a/gcc/testsuite/ChangeLog.jit
+++ b/gcc/testsuite/ChangeLog.jit
@@ -1,3 +1,8 @@
+2014-01-21  David Malcolm  <dmalcolm@redhat.com>
+
+	* jit.dg/test-hello-world.c (code_making_callback): Add usage of
+	gcc_jit_function_add_comment.
+
 2013-10-24  David Malcolm  <dmalcolm@redhat.com>
 
 	* jit.dg/harness.h (main): Wrap with #ifndef TEST_PROVIDES_MAIN
diff --git a/gcc/testsuite/jit.dg/test-hello-world.c b/gcc/testsuite/jit.dg/test-hello-world.c
index 1c18ce1b..22e4e9e 100644
--- a/gcc/testsuite/jit.dg/test-hello-world.c
+++ b/gcc/testsuite/jit.dg/test-hello-world.c
@@ -12,6 +12,7 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data)
      void
      hello_world (const char *name)
      {
+        // a test comment
         printf ("hello %s\n", name);
      }
   */
@@ -43,6 +44,10 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data)
   args[0] = gcc_jit_context_new_string_literal (ctxt, "hello %s\n");
   args[1] = gcc_jit_param_as_rvalue (param_name);
 
+  gcc_jit_function_add_comment (
+    func, NULL,
+    "a test comment");
+
   gcc_jit_function_add_eval (
     func, NULL,
     gcc_jit_context_new_call (ctxt,
-- 
1.7.11.7



More information about the Gcc-patches mailing list