From 60cb8db24d29a1f8f19bfb9b77dd6d9c803fd091 Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Fri, 16 Mar 2012 17:14:39 +0100 Subject: [PATCH] Extract tool independent file rewriting into its own file We use this opportunity to move from 4-spaces to tabs. Signed-off-by: Tobias Grosser Signed-off-by: Sven Verdoolaege --- Makefile.am | 2 ++ cuda_common.c | 56 +++----------------------------------------- rewrite.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rewrite.h | 9 ++++++++ 4 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 rewrite.c create mode 100644 rewrite.h diff --git a/Makefile.am b/Makefile.am index 3105d99..f291b31 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,6 +39,8 @@ ppcg_SOURCES = \ cuda_common.c \ gpucode.c \ gpucode.h \ + rewrite.c \ + rewrite.h \ schedule.c \ schedule.h \ ppcg_options.c \ diff --git a/cuda_common.c b/cuda_common.c index 3722768..802e9c0 100644 --- a/cuda_common.c +++ b/cuda_common.c @@ -13,41 +13,7 @@ #include #include "cuda_common.h" - -static char *skip_spaces(char *s) -{ - while (isspace(*s)) - ++s; - return s; -} - -static int is_begin_scop(char *line) -{ - line = skip_spaces(line); - if (*line != '#') - return 0; - line = skip_spaces(line + 1); - if (strncmp(line, "pragma", sizeof("pragma") - 1)) - return 0; - line = skip_spaces(line + sizeof("pragma") - 1); - if (strncmp(line, "scop", sizeof("scop") - 1)) - return 0; - return 1; -} - -static int is_end_scop(char *line) -{ - line = skip_spaces(line); - if (*line != '#') - return 0; - line = skip_spaces(line + 1); - if (strncmp(line, "pragma", sizeof("pragma") - 1)) - return 0; - line = skip_spaces(line + sizeof("pragma") - 1); - if (strncmp(line, "endscop", sizeof("endscop") - 1)) - return 0; - return 1; -} +#include "rewrite.h" /* Open the "input" file for reading and open the host .cu file * and the kernel .hu and .cu files for writing. @@ -60,7 +26,6 @@ void cuda_open_files(struct cuda_info *info, const char *input) const char *base; const char *ext; int len; - char line[1024]; base = strrchr(input, '/'); if (base) @@ -85,11 +50,7 @@ void cuda_open_files(struct cuda_info *info, const char *input) fprintf(info->kernel_h, "#include \"cuda.h\"\n\n"); info->input = fopen(input, "r"); - while (fgets(line, sizeof(line), info->input)) { - fprintf(info->host_c, "%s", line); - if (is_begin_scop(line)) - break; - } + copy_before_scop(info->input, info->host_c); } /* Copy all code starting at the endscop pragma from the input @@ -97,18 +58,7 @@ void cuda_open_files(struct cuda_info *info, const char *input) */ void cuda_close_files(struct cuda_info *info) { - char line[1024]; - - while (fgets(line, sizeof(line), info->input)) { - if (is_end_scop(line)) { - fprintf(info->host_c, "%s", line); - break; - } - } - while (fgets(line, sizeof(line), info->input)) { - fprintf(info->host_c, "%s", line); - } - + copy_after_scop(info->input, info->host_c); fclose(info->input); fclose(info->kernel_c); fclose(info->kernel_h); diff --git a/rewrite.c b/rewrite.c new file mode 100644 index 0000000..fa472cf --- /dev/null +++ b/rewrite.c @@ -0,0 +1,74 @@ +/* + * Copyright 2010 INRIA Saclay + * + * Use of this software is governed by the GNU LGPLv2.1 license + * + * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France, + * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod, + * 91893 Orsay, France + */ + +#include +#include +#include + +#include "rewrite.h" + +static char *skip_spaces(char *s) +{ + while (isspace(*s)) + ++s; + return s; +} + +static int is_begin_scop(char *line) +{ + line = skip_spaces(line); + if (*line != '#') + return 0; + line = skip_spaces(line + 1); + if (strncmp(line, "pragma", sizeof("pragma") - 1)) + return 0; + line = skip_spaces(line + sizeof("pragma") - 1); + if (strncmp(line, "scop", sizeof("scop") - 1)) + return 0; + return 1; +} + +static int is_end_scop(char *line) +{ + line = skip_spaces(line); + if (*line != '#') + return 0; + line = skip_spaces(line + 1); + if (strncmp(line, "pragma", sizeof("pragma") - 1)) + return 0; + line = skip_spaces(line + sizeof("pragma") - 1); + if (strncmp(line, "endscop", sizeof("endscop") - 1)) + return 0; + return 1; +} + +void copy_before_scop(FILE *input, FILE *output) +{ + char line[1024]; + while (fgets(line, sizeof(line), input)) { + fprintf(output, "%s", line); + if (is_begin_scop(line)) + break; + } +} + +void copy_after_scop(FILE *input, FILE *output) +{ + char line[1024]; + while (fgets(line, sizeof(line), input)) { + if (is_end_scop(line)) { + fprintf(output, "%s", line); + break; + } + } + while (fgets(line, sizeof(line), input)) { + fprintf(output, "%s", line); + } +} diff --git a/rewrite.h b/rewrite.h new file mode 100644 index 0000000..6a0b786 --- /dev/null +++ b/rewrite.h @@ -0,0 +1,9 @@ +#ifndef _REWRITE_H_ +#define _REWRITE_H_ + +#include + +void copy_before_scop(FILE *input, FILE *output); +void copy_after_scop(FILE *input, FILE *output); + +#endif -- 2.11.4.GIT