libgccjit: handle long literals in playback::context::new_string_literal
authorAndreaCorallo <andrea.corallo@arm.com>
Sat, 7 Mar 2020 17:39:30 +0000 (17:39 +0000)
committerAndrea Corallo <andrea.corallo@arm.com>
Mon, 23 Mar 2020 18:00:26 +0000 (19:00 +0100)
gcc/jit/ChangeLog
2020-03-23  Andrea Corallo  <andrea.corallo@arm.com>

* jit-playback.h
(gcc::jit::playback::context m_recording_ctxt): Remove
m_char_array_type_node field.
* jit-playback.c
(playback::context::context) Remove m_char_array_type_node from member
initializer list.
(playback::context::new_string_literal) Fix logic to handle string
length > 200.

gcc/testsuite/ChangeLog
2020-03-23  Andrea Corallo  <andrea.corallo@arm.com>

* jit.dg/all-non-failing-tests.h: Add test-long-string-literal.c.
* jit.dg/test-long-string-literal.c: New testcase.

gcc/jit/ChangeLog
gcc/jit/jit-playback.c
gcc/jit/jit-playback.h
gcc/testsuite/ChangeLog
gcc/testsuite/jit.dg/all-non-failing-tests.h
gcc/testsuite/jit.dg/test-long-string-literal.c [new file with mode: 0644]

index b6f888f12bb3c3aeac140a5dbceb636aff1c1ad0..5d39b7bebc2158ef8615d576692cef9db0b6be27 100644 (file)
@@ -1,3 +1,14 @@
+2020-03-23  Andrea Corallo  <andrea.corallo@arm.com>
+
+       * jit-playback.h
+       (gcc::jit::playback::context m_recording_ctxt): Remove
+       m_char_array_type_node field.
+       * jit-playback.c
+       (playback::context::context) Remove m_char_array_type_node from member
+       initializer list.
+       (playback::context::new_string_literal) Fix logic to handle string
+       length > 200.
+
 2020-01-01  Jakub Jelinek  <jakub@redhat.com>
 
        Update copyright years.
index da687002a72777514dfe1c6eb9e514510c0788a8..d2c8bb4c154d6e96644cf0505facc7026c720995 100644 (file)
@@ -88,7 +88,6 @@ playback::context::context (recording::context *ctxt)
   : log_user (ctxt->get_logger ()),
     m_recording_ctxt (ctxt),
     m_tempdir (NULL),
-    m_char_array_type_node (NULL),
     m_const_char_ptr (NULL)
 {
   JIT_LOG_SCOPE (get_logger ());
@@ -670,9 +669,14 @@ playback::rvalue *
 playback::context::
 new_string_literal (const char *value)
 {
-  tree t_str = build_string (strlen (value), value);
-  gcc_assert (m_char_array_type_node);
-  TREE_TYPE (t_str) = m_char_array_type_node;
+  /* Compare with c-family/c-common.c: fix_string_type.  */
+  size_t len = strlen (value);
+  tree i_type = build_index_type (size_int (len));
+  tree a_type = build_array_type (char_type_node, i_type);
+  /* build_string len parameter must include NUL terminator when
+     building C strings.  */
+  tree t_str = build_string (len + 1, value);
+  TREE_TYPE (t_str) = a_type;
 
   /* Convert to (const char*), loosely based on
      c/c-typeck.c: array_to_pointer_conversion,
@@ -2701,10 +2705,6 @@ playback::context::
 replay ()
 {
   JIT_LOG_SCOPE (get_logger ());
-  /* Adapted from c-common.c:c_common_nodes_and_builtins.  */
-  tree array_domain_type = build_index_type (size_int (200));
-  m_char_array_type_node
-    = build_array_type (char_type_node, array_domain_type);
 
   m_const_char_ptr
     = build_pointer_type (build_qualified_type (char_type_node,
index 904cc167a0c49fc06282ef9b7e645fd813ef810e..074434a9f6b2637e290ed3425f28649602faa0b3 100644 (file)
@@ -322,7 +322,6 @@ private:
 
   auto_vec<function *> m_functions;
   auto_vec<tree> m_globals;
-  tree m_char_array_type_node;
   tree m_const_char_ptr;
 
   /* Source location handling.  */
index b6fbdc1df55089ea5d81bf1213ae5ba2084181c6..6b4c0e24ba57d0da9237d80dea7292e02af75ecf 100644 (file)
@@ -1,3 +1,8 @@
+2020-03-23  Andrea Corallo  <andrea.corallo@arm.com>
+
+       * jit.dg/all-non-failing-tests.h: Add test-long-string-literal.c.
+       * jit.dg/test-long-string-literal.c: New testcase.
+
 2020-03-23  Andre Vieira  <andre.simoesdiasvieira@arm.com>
 
        * gcc.target/arm/mve/intrinsics/mve_fp_fpu1.c: Remove dg-do.
index 0272e6f846fbb4fd0aae0808793d8d2160fa0315..b2acc74ae9504dcfbf02a3f5ca10bebd615ca104 100644 (file)
 #undef create_code
 #undef verify_code
 
+/* test-long-string-literal.c */
+#define create_code create_code_long_string_literal
+#define verify_code verify_code_long_string_literal
+#include "test-long-string-literal.c"
+#undef create_code
+#undef verify_code
+
 /* test-quadratic.c */
 #define create_code create_code_quadratic
 #define verify_code verify_code_quadratic
@@ -342,6 +349,9 @@ const struct testcase testcases[] = {
   {"long_names",
    create_code_long_names,
    verify_code_long_names},
+  {"long_string_literal",
+   create_code_long_string_literal,
+   verify_code_long_string_literal},
   {"quadratic",
    create_code_quadratic,
    verify_code_quadratic},
diff --git a/gcc/testsuite/jit.dg/test-long-string-literal.c b/gcc/testsuite/jit.dg/test-long-string-literal.c
new file mode 100644 (file)
index 0000000..6caaa78
--- /dev/null
@@ -0,0 +1,54 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+const char very_long_string[] =
+  "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
+  "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
+  "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
+  "abcabcabcabcabcabcabcabcabcabca";
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Build the test_fn.  */
+  gcc_jit_function *f =
+    gcc_jit_context_new_function (
+      ctxt, NULL,
+      GCC_JIT_FUNCTION_EXPORTED,
+      gcc_jit_context_get_type(ctxt,
+                              GCC_JIT_TYPE_CONST_CHAR_PTR),
+                               "test_long_string_literal",
+                               0, NULL, 0);
+  gcc_jit_block *blk =
+    gcc_jit_function_new_block (f, "init_block");
+
+  /* very_long_string is longer than 200 characters to specifically
+     check that the previous limitation no longer apply.  */
+
+  assert (sizeof (very_long_string) > 200);
+  gcc_jit_rvalue *res =
+    gcc_jit_context_new_string_literal (ctxt, very_long_string);
+
+  gcc_jit_block_end_with_return (blk, NULL, res);
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  typedef const char *(*fn_type) (void);
+  CHECK_NON_NULL (result);
+  fn_type test_long_string_literal =
+    (fn_type)gcc_jit_result_get_code (result, "test_long_string_literal");
+  CHECK_NON_NULL (test_long_string_literal);
+
+  /* Call the JIT-generated function.  */
+  const char *str = test_long_string_literal ();
+  CHECK_NON_NULL (str);
+  CHECK_VALUE (strcmp (str, very_long_string), 0);
+}