Bernd,
I'mm working through the mkoffload machinery. mkoffload.c emits a C file, and the quoting in the source is quite confusing. This patch introduces a quoting macro 'Q' that allows one to write raw C to be stringized and written out.

ok? (more cleanups to follow)

nathan
2015-07-10  Nathan Sidwell  <nat...@codesourcery.com>

        * config/nvptx/mkoffload.c (Q): New macro.
        (proacess): Use it for emitting code.

Index: config/nvptx/mkoffload.c
===================================================================
--- config/nvptx/mkoffload.c    (revision 225703)
+++ config/nvptx/mkoffload.c    (working copy)
@@ -37,6 +37,10 @@
 #include "collect-utils.h"
 #include "gomp-constants.h"
 
+/* Quoter macro so that one can write unquoted 'C' for printfs.
+   Sadly all white space is collapsed.  */
+#define Q(...) #__VA_ARGS__ "\n"
+
 const char tool_name[] = "nvptx mkoffload";
 
 #define COMMENT_PREFIX "#"
@@ -269,37 +273,41 @@ process (FILE *in, FILE *out)
 
   unsigned int nvars = 0, nfuncs = 0;
 
-  fprintf (out, "static const char *var_mappings[] = {\n");
+  fprintf (out,
+          Q (static const char *const var_mappings[] = {));
   for (id_map *id = var_ids; id; id = id->next, nvars++)
     fprintf (out, "\t\"%s\"%s\n", id->ptx_name, id->next ? "," : "");
-  fprintf (out, "};\n\n");
-  fprintf (out, "static const char *func_mappings[] = {\n");
+  fprintf (out, Q (};));
+
+  fprintf (out, Q (static const char *const func_mappings[] = {));
   for (id_map *id = func_ids; id; id = id->next, nfuncs++)
     fprintf (out, "\t\"%s\"%s\n", id->ptx_name, id->next ? "," : "");
-  fprintf (out, "};\n\n");
+  fprintf (out, Q(};));
 
-  fprintf (out, "static const void *target_data[] = {\n");
-  fprintf (out, "  ptx_code, (void *)(__UINTPTR_TYPE__)sizeof (ptx_code),\n");
-  fprintf (out, "  (void *) %u, var_mappings, (void *) %u, func_mappings\n",
+  fprintf (out,
+          Q (static const void *target_data[] = {
+              ptx_code, (void *)(__UINTPTR_TYPE__)sizeof (ptx_code),
+                (void *) %u, var_mappings, (void *) %u, func_mappings};),
           nvars, nfuncs);
-  fprintf (out, "};\n\n");
-
-  fprintf (out, "#ifdef __cplusplus\n");
-  fprintf (out, "extern \"C\" {\n");
-  fprintf (out, "#endif\n");
-
-  fprintf (out, "extern void GOMP_offload_register (const void *, int, void 
*);\n");
-
-  fprintf (out, "#ifdef __cplusplus\n");
-  fprintf (out, "}\n");
-  fprintf (out, "#endif\n");
 
-  fprintf (out, "extern void *__OFFLOAD_TABLE__[];\n\n");
-  fprintf (out, "static __attribute__((constructor)) void init (void)\n{\n");
-  fprintf (out, "  GOMP_offload_register (__OFFLOAD_TABLE__, %d,\n",
+  fprintf (out, Q (#ifdef __cplusplus));
+  fprintf (out, Q (extern "C" {));
+  fprintf (out, Q (#endif));
+
+  fprintf (out,
+          Q (extern void GOMP_offload_register (const void *, int, void *);));
+
+  fprintf (out, Q (#ifdef __cplusplus));
+  fprintf (out, Q (}));
+  fprintf (out, Q (#endif));
+
+  fprintf (out,
+          Q (extern void *__OFFLOAD_TABLE__[];
+             static __attribute__((constructor)) void init (void)
+             {
+               GOMP_offload_register (__OFFLOAD_TABLE__, %d, &target_data);
+             }),
           GOMP_DEVICE_NVIDIA_PTX);
-  fprintf (out, "                         &target_data);\n");
-  fprintf (out, "};\n");
 }
 
 static void

Reply via email to