summaryrefslogtreecommitdiff
path: root/backend/src/ir/printf.hpp
blob: 85153a5f85e908183724a558302243c1ad1911a5 (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
/*
 * 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 {
      int type;
      union {
        char* str;
        PrintfState* state;
        void *ptr;
      };

      PrintfSlot(void) {
        type = PRINTF_SLOT_TYPE_NONE;
        ptr = NULL;
      }

      PrintfSlot(const char * s) {
        type = PRINTF_SLOT_TYPE_STRING;
        int len = strlen(s);
        str = (char*)malloc((len + 1) * sizeof(char));
        memcpy(str, s, (len + 1) * sizeof(char));
        str[len] = 0;
      }

      PrintfSlot(PrintfState * st) {
        type = PRINTF_SLOT_TYPE_STATE;
        state = new PrintfState(*st);
      }

      PrintfSlot(const PrintfSlot & other) {
        if (other.type == PRINTF_SLOT_TYPE_STRING) {
          int len = strlen(other.str);
          str = (char*)malloc((len + 1) * sizeof(char));
          memcpy(str, other.str, (len + 1) * sizeof(char));
          str[len] = 0;
          type = PRINTF_SLOT_TYPE_STRING;
        } else if (other.type == PRINTF_SLOT_TYPE_STATE) {
          type = PRINTF_SLOT_TYPE_STATE;
          state = new PrintfState(*other.state);
        } else {
          type = PRINTF_SLOT_TYPE_NONE;
          ptr = NULL;
        }
      }

      PrintfSlot(PrintfSlot && other) {
        void *p = other.ptr;
        type = other.type;
        other.ptr = ptr;
        ptr = p;
      }

      ~PrintfSlot(void);
    };

    class Context;

    class PrintfSet //: public Serializable
    {
    public:
      PrintfSet(const PrintfSet& other) {
        for (size_t i = 0; i < other.fmts.size(); ++i) {
          const PrintfFmt& f = other.fmts[i];
          fmts.push_back(f);
        }

        for (size_t i = 0; i < other.slots.size(); ++i) {
          PrintfSlot s = other.slots[i];
          slots.push_back(s);
        }

        sizeOfSize = other.sizeOfSize;
        btiBuf = other.btiBuf;
        btiIndexBuf = other.btiIndexBuf;
      }

      PrintfSet(void) = default;

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

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

      typedef std::pair<vector<PrintfSlot>, int> PrintfFmt;
      uint32_t append(PrintfFmt* fmt, Unit &unit);

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

      uint32_t getPrintfSizeOfSize(void) const {
        return sizeOfSize;
      }

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

      uint32_t getPrintfBufferElementSize(uint32_t i) {
        PrintfSlot& slot = slots[i];
        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* index_addr, void* buf_addr, size_t global_wk_sz0,
                        size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz);

    private:
      vector<PrintfFmt> fmts;
      vector<PrintfSlot> slots;
      uint32_t sizeOfSize; // Total sizeof size.
      friend struct LockOutput;
      uint8_t btiBuf;
      uint8_t btiIndexBuf;
      static pthread_mutex_t lock;
      GBE_CLASS(PrintfSet);
    };
  } /* namespace ir */
} /* namespace gbe */

#endif /* __GBE_IR_PRINTF_HPP__ */