summaryrefslogtreecommitdiff
path: root/backend/src/ir/printf.hpp
blob: 28944c71c3f201f7520cf098d960ac61819671eb (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * 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.hpp
 *
 */
#ifndef __GBE_IR_PRINTF_HPP__
#define __GBE_IR_PRINTF_HPP__

#include <string.h>
#include "sys/map.hpp"
#include "sys/vector.hpp"

namespace gbe
{
  namespace ir
  {
    class Unit;

    /* Things about printf info. */
    enum {
      PRINTF_LM_NONE,
      PRINTF_LM_HH,
      PRINTF_LM_H,
      PRINTF_LM_L,
      PRINTF_LM_HL,
    };

    enum {
      PRINTF_CONVERSION_INVALID,
      PRINTF_CONVERSION_D,
      PRINTF_CONVERSION_I,
      PRINTF_CONVERSION_O,
      PRINTF_CONVERSION_U,
      PRINTF_CONVERSION_X,
      PRINTF_CONVERSION_x,
      PRINTF_CONVERSION_F,
      PRINTF_CONVERSION_f,
      PRINTF_CONVERSION_E,
      PRINTF_CONVERSION_e,
      PRINTF_CONVERSION_G,
      PRINTF_CONVERSION_g,
      PRINTF_CONVERSION_A,
      PRINTF_CONVERSION_a,
      PRINTF_CONVERSION_C,
      PRINTF_CONVERSION_S,
      PRINTF_CONVERSION_P
    };

    struct PrintfState {
      char left_justified;
      char sign_symbol; //0 for nothing, 1 for sign, 2 for space.
      char alter_form;
      char zero_padding;
      char vector_n;
      int min_width;
      int precision;
      int length_modifier;
      char conversion_specifier;
      int out_buf_sizeof_offset;  // Should *global_total_size to get the full offset.
      std::string str;            //if %s, the string store here.

      PrintfState(void) {
        left_justified = 0;
        sign_symbol = 0;
        alter_form = 0;
        zero_padding = 0;
        vector_n = 0;
        min_width = 0;
        precision = 0;
        length_modifier = 0;
        conversion_specifier = 0;
        out_buf_sizeof_offset = 0;
      }

      PrintfState(const PrintfState & other) {
        left_justified = other.left_justified;
        sign_symbol = other.sign_symbol;
        alter_form = other.alter_form;
        zero_padding = other.zero_padding;
        vector_n = other.vector_n;
        min_width = other.min_width;
        precision = other.precision;
        length_modifier = other.length_modifier;
        conversion_specifier = other.conversion_specifier;
        out_buf_sizeof_offset = other.out_buf_sizeof_offset;
        str = other.str;
      }
    };

    enum {
      PRINTF_SLOT_TYPE_NONE,
      PRINTF_SLOT_TYPE_STRING,
      PRINTF_SLOT_TYPE_STATE
    };

    struct PrintfSlot {
      uint32_t type;
      std::string str;
      PrintfState state;

      PrintfSlot(void) {
        type = PRINTF_SLOT_TYPE_NONE;
      }

      PrintfSlot(std::string& s) : str(s) {
        type = PRINTF_SLOT_TYPE_STRING;
      }

      PrintfSlot(PrintfState& st, std::string& s) : str(s) {
        type = PRINTF_SLOT_TYPE_STATE;
        state = st;
      }

      PrintfSlot(const PrintfSlot & other) {
        if (other.type == PRINTF_SLOT_TYPE_STRING) {
          type = PRINTF_SLOT_TYPE_STRING;
          str = other.str;
        } else if (other.type == PRINTF_SLOT_TYPE_STATE) {
          type = PRINTF_SLOT_TYPE_STATE;
          state = other.state;
          str = other.str;
        } else {
          type = PRINTF_SLOT_TYPE_NONE;
        }
      }

      ~PrintfSlot(void) {
      }
    };

    struct PrintfLog {
      uint32_t magic;  // 0xAABBCCDD as magic for ASSERT.
      uint32_t size;  // Size of this printf log, include header.
      uint32_t statementNum; // which printf within one kernel.
      const char* content;

      PrintfLog(const char* p) {
        GBE_ASSERT(*((uint32_t *)p) == 0xAABBCCDD);
        magic = *((uint32_t *)p);
        p += sizeof(uint32_t);
        size = *((uint32_t *)p);
        p += sizeof(uint32_t);
        statementNum = *((uint32_t *)p);
        p += sizeof(uint32_t);
        content = p;
      }

      template <typename T>
      T getData(void) {
        T D = *((T *)content);
        content += sizeof(T);
        return D;
      }
    };

    class Context;

    class PrintfSet //: public Serializable
    {
    public:
      PrintfSet(const PrintfSet& other) {
        fmts = other.fmts;
        btiBuf = other.btiBuf;
      }

      PrintfSet(void) = default;

      struct LockOutput {
        LockOutput(void) {
          pthread_mutex_lock(&lock);
        }

        ~LockOutput(void) {
          pthread_mutex_unlock(&lock);
        }
      };

      typedef vector<PrintfSlot> PrintfFmt;

      void append(uint32_t num, PrintfFmt* fmt) {
        GBE_ASSERT(fmts.find(num) == fmts.end());
        fmts.insert(std::pair<uint32_t, PrintfFmt>(num, *fmt));
      }

      uint32_t getPrintfNum(void) const {
        return fmts.size();
      }

      void setBufBTI(uint8_t b)      { btiBuf = b; }
      uint8_t getBufBTI() const      { return btiBuf; }

      uint32_t getPrintfBufferElementSize(uint32_t i) {
        PrintfSlot slot;
        int vec_num = 1;
        if (slot.state.vector_n > 0) {
          vec_num = slot.state.vector_n;
        }

        assert(vec_num > 0 && vec_num <= 16);

        switch (slot.state.conversion_specifier) {
          case PRINTF_CONVERSION_I:
          case PRINTF_CONVERSION_D:
          case PRINTF_CONVERSION_O:
          case PRINTF_CONVERSION_U:
          case PRINTF_CONVERSION_X:
          case PRINTF_CONVERSION_x:
          case PRINTF_CONVERSION_P:
          /* Char will be aligned to sizeof(int) here. */
          case PRINTF_CONVERSION_C:
            return (uint32_t)(sizeof(int) * vec_num);
          case PRINTF_CONVERSION_E:
          case PRINTF_CONVERSION_e:
          case PRINTF_CONVERSION_F:
          case PRINTF_CONVERSION_f:
          case PRINTF_CONVERSION_G:
          case PRINTF_CONVERSION_g:
          case PRINTF_CONVERSION_A:
          case PRINTF_CONVERSION_a:
            return (uint32_t)(sizeof(float) * vec_num);
          case PRINTF_CONVERSION_S:
            return (uint32_t)0;
          default:
            break;
        }
        assert(0);
        return 0;
      }

      void outputPrintf(void* buf_addr);

      uint32_t collectPrintfStr(std::map<uint32_t, std::string>& all_printf) const {
        uint32_t n = 0;
        for (auto iter = fmts.begin(); iter != fmts.end(); iter++) {
          std::string s;
          const PrintfFmt& fmt = iter->second;
          for (auto &m : fmt) {
            if (m.type == PRINTF_SLOT_TYPE_STATE && m.state.conversion_specifier == PRINTF_CONVERSION_S) {
              std::string ss = m.state.str;
              if (m.state.precision > 0 && (static_cast<size_t>(m.state.precision) < ss.size())) {
                ss.resize(m.state.precision);
              }
              if (m.state.min_width > 0 && (static_cast<size_t>(m.state.min_width) > ss.size())) {
                std::string spaces;
                spaces.resize(static_cast<size_t>(m.state.min_width)  - ss.size(), ' ');
                if (m.state.left_justified) {
                  ss = ss + spaces;
                } else {
                  ss = spaces + ss;
                }
              }

              s += ss;
            } else {
              s += m.str;
            }
          }

          all_printf.insert(std::pair<uint32_t, std::string>(iter->first, s));
          n++;
        }

        return n;
      }

    private:
      std::map<uint32_t, PrintfFmt> fmts;
      friend struct LockOutput;
      uint8_t btiBuf;
      static pthread_mutex_t lock;
      GBE_CLASS(PrintfSet);
    };
  } /* namespace ir */
} /* namespace gbe */

#endif /* __GBE_IR_PRINTF_HPP__ */