summaryrefslogtreecommitdiff
path: root/backend/src/ir/printf.cpp
blob: eb1c1990b7e14d6b2bdb3164e1923cd314e02611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * \file printf.cpp
 *
 */

#include <stdarg.h>
#include "printf.hpp"

namespace gbe
{
  namespace ir
  {

    pthread_mutex_t PrintfSet::lock = PTHREAD_MUTEX_INITIALIZER;

    PrintfSlot::~PrintfSlot(void)
    {
        if (ptr)
        {
          if (type == PRINTF_SLOT_TYPE_STRING) {
            free(ptr);
            ptr = NULL;
          } else if (type == PRINTF_SLOT_TYPE_STATE) {
            delete state;
            state = NULL;
          } else {
            type = PRINTF_SLOT_TYPE_NONE;
            ptr = NULL;
          }
        }
    }

    uint32_t PrintfSet::append(PrintfFmt* fmt, Unit& unit)
    {
      fmts.push_back(*fmt);
      vector<PrintfSlot>& vp = fmts.back().first;

      for (vector<PrintfSlot>::iterator f = vp.begin(); f !=  vp.end(); ++f) {
        if (f->type == PRINTF_SLOT_TYPE_STRING)
          continue;

        slots.push_back(*f);
      }

      /* Update the total size of size. */
      if (slots.size() > 0)
        sizeOfSize = slots.back().state->out_buf_sizeof_offset
                     + getPrintfBufferElementSize(slots.size() - 1);

      return (uint32_t)fmts.size();
    }

    static void generatePrintfFmtString(PrintfState& state, std::string& str)
    {
      char num_str[16];
      str += "%";

      if (state.left_justified) {
        str += "-";
      }

      if (state.sign_symbol == 1) {
        str += "+";
      } else if (state.sign_symbol == 2) {
        str += " ";
      }

      if (state.alter_form) {
        str += "#";
      }

      if (state.zero_padding) {
        str += "0";
      }

      if (state.min_width >= 0) {
        snprintf(num_str, 16, "%d", state.min_width);
        str += num_str;
      }

      if (state.precision >= 0) {
        str += ".";
        snprintf(num_str, 16, "%d", state.precision);
        str += num_str;
      }

      switch (state.length_modifier) {
        case PRINTF_LM_HH:
          str += "hh";
          break;
        case PRINTF_LM_H:
          str += "h";
          break;
        case PRINTF_LM_L:
          str += "l";
          break;
        case PRINTF_LM_HL:
          str += "";
          break;
        default:
          assert(state.length_modifier == PRINTF_LM_NONE);
      }
    }

#define PRINT_SOMETHING(target_ty, conv)  do {                          \
      if (!vec_i)                                                       \
        pf_str = pf_str + std::string(#conv);                           \
      char *ptr = ((char *)buf_addr + sizeOfSize * global_wk_sz0 * global_wk_sz1 * global_wk_sz2 * n \
                   + slot.state->out_buf_sizeof_offset *                \
                   global_wk_sz0 * global_wk_sz1 * global_wk_sz2);      \
      target_ty* obj_ptr = ((target_ty *)ptr) + (k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i) * vec_num + vec_i; \
      if ((char *)obj_ptr + sizeof(target_ty) > (char *)buf_addr + output_sz) {            \
        printf("\n\n!!!The printf message is out of range because of the limited buffer, ignore.\n"); \
        return;                                                         \
      }                                                                 \
      printf(pf_str.c_str(),  *obj_ptr);                                \
    } while (0)


    void PrintfSet::outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0,
                                 size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz)
    {
      LockOutput lock;
      size_t i, j, k;
      std::string pf_str;
      int stmt = 0;

      for (size_t count = 0; count < fmts.size(); ++count) {
        for (i = 0; i < global_wk_sz0; i++) {
          for (j = 0; j < global_wk_sz1; j++) {
            for (k = 0; k < global_wk_sz2; k++) {
              int loop_num = ((int *)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
                                                 + k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2];
              int printf_num = ((int *)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
                                                 + k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2 + 1];
              if (!loop_num) continue;

              PrintfFmt* ppf = NULL;
              for (auto& f : fmts) {
                if (f.second == printf_num) {
                  ppf = &f;
                  break;
                }
              }

              PrintfFmt& pf = *ppf;

              for (int n = 0; n < loop_num; n++) {
                for (vector<PrintfSlot>::iterator pfit = pf.first.begin(); pfit != pf.first.end(); ++pfit) {
                  PrintfSlot& slot = *pfit;
                  pf_str = "";
                  int vec_num;

                  if (slot.type == PRINTF_SLOT_TYPE_STRING) {
                    printf("%s", slot.str);
                    continue;
                  }
                  assert(slot.type == PRINTF_SLOT_TYPE_STATE);

                  generatePrintfFmtString(*slot.state, pf_str);


                  vec_num = slot.state->vector_n > 0 ? slot.state->vector_n : 1;

                  for (int vec_i = 0; vec_i < vec_num; vec_i++) {
                    if (vec_i)
                      printf(",");

                    switch (slot.state->conversion_specifier) {
                      case PRINTF_CONVERSION_D:
                      case PRINTF_CONVERSION_I:
                        if (slot.state->length_modifier == PRINTF_LM_L)
                          PRINT_SOMETHING(uint64_t, d);
                        else
                          PRINT_SOMETHING(int, d);
                        break;

                      case PRINTF_CONVERSION_O:
                        if (slot.state->length_modifier == PRINTF_LM_L)
                          PRINT_SOMETHING(uint64_t, o);
                        else
                          PRINT_SOMETHING(int, o);
                        break;
                      case PRINTF_CONVERSION_U:
                        if (slot.state->length_modifier == PRINTF_LM_L)
                          PRINT_SOMETHING(uint64_t, u);
                        else
                          PRINT_SOMETHING(int, u);
                        break;
                      case PRINTF_CONVERSION_X:
                        if (slot.state->length_modifier == PRINTF_LM_L)
                          PRINT_SOMETHING(uint64_t, X);
                        else
                          PRINT_SOMETHING(int, X);
                        break;
                      case PRINTF_CONVERSION_x:
                        if (slot.state->length_modifier == PRINTF_LM_L)
                          PRINT_SOMETHING(uint64_t, x);
                        else
                          PRINT_SOMETHING(int, x);
                        break;

                      case PRINTF_CONVERSION_C:
                        PRINT_SOMETHING(char, c);
                        break;

                      case PRINTF_CONVERSION_F:
                        PRINT_SOMETHING(float, F);
                        break;
                      case PRINTF_CONVERSION_f:
                        PRINT_SOMETHING(float, f);
                        break;
                      case PRINTF_CONVERSION_E:
                        PRINT_SOMETHING(float, E);
                        break;
                      case PRINTF_CONVERSION_e:
                        PRINT_SOMETHING(float, e);
                        break;
                      case PRINTF_CONVERSION_G:
                        PRINT_SOMETHING(float, G);
                        break;
                      case PRINTF_CONVERSION_g:
                        PRINT_SOMETHING(float, g);
                        break;
                      case PRINTF_CONVERSION_A:
                        PRINT_SOMETHING(float, A);
                        break;
                      case PRINTF_CONVERSION_a:
                        PRINT_SOMETHING(float, a);
                        break;
                      case PRINTF_CONVERSION_P:
                        PRINT_SOMETHING(int, p);
                        break;

                      case PRINTF_CONVERSION_S:
                        pf_str = pf_str + "s";
                        printf(pf_str.c_str(), slot.state->str.c_str());
                        break;

                      default:
                        assert(0);
                        return;
                    }
                  }

                  pf_str = "";
                }

              }
            }
          }
        }
        stmt++;
      }
    }
  } /* namespace ir */
} /* namespace gbe */