summaryrefslogtreecommitdiff
path: root/runtime/gen/cl_printf_gen.c
blob: 7e6f182ee09fa043afb7eff932bf5f11bf70b298 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * 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/>.
 *
 */

#include "cl_gen.h"
#include <stdarg.h>

#define GEN_PRINTF_LOG_MAGIC 0xAABBCCDD

typedef struct _cl_gen_printf_log {
  uint32_t magic;         // 0xAABBCCDD as magic for ASSERT.
  uint32_t size;          // Size of this printf log, include header.
  uint32_t statement_num; // which printf within one kernel.
  char *content;
} _cl_gen_printf_log;
typedef _cl_gen_printf_log *cl_gen_printf_log;

/* Things about printf info. */
enum {
  GEN_PRINTF_LM_NONE,
  GEN_PRINTF_LM_HH,
  GEN_PRINTF_LM_H,
  GEN_PRINTF_LM_L,
  GEN_PRINTF_LM_HL,
};

enum {
  GEN_PRINTF_CONVERSION_INVALID,
  GEN_PRINTF_CONVERSION_D,
  GEN_PRINTF_CONVERSION_I,
  GEN_PRINTF_CONVERSION_O,
  GEN_PRINTF_CONVERSION_U,
  GEN_PRINTF_CONVERSION_X,
  GEN_PRINTF_CONVERSION_x,
  GEN_PRINTF_CONVERSION_F,
  GEN_PRINTF_CONVERSION_f,
  GEN_PRINTF_CONVERSION_E,
  GEN_PRINTF_CONVERSION_e,
  GEN_PRINTF_CONVERSION_G,
  GEN_PRINTF_CONVERSION_g,
  GEN_PRINTF_CONVERSION_A,
  GEN_PRINTF_CONVERSION_a,
  GEN_PRINTF_CONVERSION_C,
  GEN_PRINTF_CONVERSION_S,
  GEN_PRINTF_CONVERSION_P
};

typedef struct _gen_printf_state {
  struct _gen_printf_state *next;
  cl_int left_justified;
  cl_int sign_symbol; //0 for nothing, 1 for sign, 2 for space.
  cl_int alter_form;
  cl_int zero_padding;
  cl_int vector_n;
  cl_int min_width;
  cl_int precision;
  cl_int length_modifier;
  cl_int conversion_specifier;
  char *str;
} _gen_printf_state;
typedef _gen_printf_state *gen_printf_state;

static char *
generate_printf_fmt(gen_printf_state state)
{
  char num_str[16];
  char *str = CL_CALLOC(1, 256);
  int len = 0;

  str[len] = '%';
  len++;

  if (state->left_justified) {
    str[len] = '-';
    len++;
  }

  if (state->sign_symbol == 1) {
    str[len] = '+';
    len++;
  } else if (state->sign_symbol == 2) {
    str[len] = ' ';
    len++;
  }

  if (state->alter_form) {
    str[len] = '#';
    len++;
  }

  if (state->zero_padding) {
    str[len] = '0';
    len++;
  }

  if (state->min_width >= 0) {
    snprintf(num_str, 16, "%d", state->min_width);
    memcpy(&(str[len]), num_str, strlen(num_str));
    len += strlen(num_str);
  }

  if (state->precision >= 0) {
    str[len] = '.';
    len++;
    memcpy(&(str[len]), num_str, strlen(num_str));
    len += strlen(num_str);
  }

  switch (state->length_modifier) {
  case GEN_PRINTF_LM_HH:
    str[len] = 'h';
    len++;
    str[len] = 'h';
    len++;
    break;
  case GEN_PRINTF_LM_H:
    str[len] = 'h';
    len++;
    break;
  case GEN_PRINTF_LM_L:
    str[len] = 'l';
    len++;
    break;
  case GEN_PRINTF_LM_HL:
    break;
  default:
    assert(state->length_modifier == GEN_PRINTF_LM_NONE);
  }

  switch (state->conversion_specifier) {
  case GEN_PRINTF_CONVERSION_D:
  case GEN_PRINTF_CONVERSION_I:
    str[len] = 'd';
    break;

  case GEN_PRINTF_CONVERSION_O:
    str[len] = 'o';
    break;
  case GEN_PRINTF_CONVERSION_U:
    str[len] = 'u';
    break;
  case GEN_PRINTF_CONVERSION_X:
    str[len] = 'X';
    break;
  case GEN_PRINTF_CONVERSION_x:
    str[len] = 'x';
    break;
  case GEN_PRINTF_CONVERSION_C:
    str[len] = 'c';
    break;
  case GEN_PRINTF_CONVERSION_F:
    str[len] = 'F';
    break;
  case GEN_PRINTF_CONVERSION_f:
    str[len] = 'f';
    break;
  case GEN_PRINTF_CONVERSION_E:
    str[len] = 'E';
    break;
  case GEN_PRINTF_CONVERSION_e:
    str[len] = 'e';
    break;
  case GEN_PRINTF_CONVERSION_G:
    str[len] = 'G';
    break;
  case GEN_PRINTF_CONVERSION_g:
    str[len] = 'g';
    break;
  case GEN_PRINTF_CONVERSION_A:
    str[len] = 'A';
    break;
  case GEN_PRINTF_CONVERSION_a:
    str[len] = 'a';
    break;
  case GEN_PRINTF_CONVERSION_P:
    str[len] = 'p';
    break;
  default:
    assert(0);
    break;
  }

  return str;
}

static cl_int
parse_printf_state(char *begin, char *end, char **rend, gen_printf_state state)
{
  const char *fmt;
  state->left_justified = 0;
  state->sign_symbol = 0; //0 for nothing, 1 for sign, 2 for space.
  state->alter_form = 0;
  state->zero_padding = 0;
  state->vector_n = 0;
  state->min_width = -1;
  state->precision = -1;
  state->length_modifier = GEN_PRINTF_LM_NONE;
  state->conversion_specifier = GEN_PRINTF_CONVERSION_INVALID;

  fmt = begin;

  if (*fmt != '%')
    return -1;

#define FMT_PLUS_PLUS                                   \
  do {                                                  \
    if (fmt + 1 <= end)                                 \
      fmt++;                                            \
    else {                                              \
      printf("Error, line: %d, fmt > end\n", __LINE__); \
      return -1;                                        \
    }                                                   \
  } while (0)

  FMT_PLUS_PLUS;

  // parse the flags.
  while (*fmt == '-' || *fmt == '+' || *fmt == ' ' || *fmt == '#' || *fmt == '0')
    switch (*fmt) {
    case '-':
      /* The result of the conversion is left-justified within the field. */
      state->left_justified = 1;
      FMT_PLUS_PLUS;
      break;
    case '+':
      /* The result of a signed conversion always begins with a plus or minus sign. */
      state->sign_symbol = 1;
      FMT_PLUS_PLUS;
      break;
    case ' ':
      /* If the first character of a signed conversion is not a sign, or if a signed
         conversion results in no characters, a space is prefixed to the result.
         If the space and + flags both appear,the space flag is ignored. */
      if (state->sign_symbol == 0)
        state->sign_symbol = 2;
      FMT_PLUS_PLUS;
      break;
    case '#':
      /*The result is converted to an alternative form. */
      state->alter_form = 1;
      FMT_PLUS_PLUS;
      break;
    case '0':
      if (!state->left_justified)
        state->zero_padding = 1;
      FMT_PLUS_PLUS;
      break;
    default:
      break;
    }

  // The minimum field width
  while ((*fmt >= '0') && (*fmt <= '9')) {
    if (state->min_width < 0)
      state->min_width = 0;
    state->min_width = state->min_width * 10 + (*fmt - '0');
    FMT_PLUS_PLUS;
  }

  // The precision
  if (*fmt == '.') {
    FMT_PLUS_PLUS;
    state->precision = 0;
    while (*fmt >= '0' && *fmt <= '9') {
      state->precision = state->precision * 10 + (*fmt - '0');
      FMT_PLUS_PLUS;
    }
  }

  // handle the vector specifier.
  if (*fmt == 'v') {
    FMT_PLUS_PLUS;
    switch (*fmt) {
    case '2':
    case '3':
    case '4':
    case '8':
      state->vector_n = *fmt - '0';
      FMT_PLUS_PLUS;
      break;
    case '1':
      FMT_PLUS_PLUS;
      if (*fmt == '6') {
        state->vector_n = 16;
        FMT_PLUS_PLUS;
      } else
        return -1;
      break;
    default:
      //Wrong vector, error.
      return -1;
    }
  }

  // length modifiers
  if (*fmt == 'h') {
    FMT_PLUS_PLUS;
    if (*fmt == 'h') { //hh
      state->length_modifier = GEN_PRINTF_LM_HH;
      FMT_PLUS_PLUS;
    } else if (*fmt == 'l') { //hl
      state->length_modifier = GEN_PRINTF_LM_HL;
      FMT_PLUS_PLUS;
    } else { //h
      state->length_modifier = GEN_PRINTF_LM_H;
    }
  } else if (*fmt == 'l') {
    state->length_modifier = GEN_PRINTF_LM_L;
    FMT_PLUS_PLUS;
  }

#define CONVERSION_SPEC_AND_RET(XXX, xxx)                      \
  case XXX:                                                    \
    state->conversion_specifier = GEN_PRINTF_CONVERSION_##xxx; \
    FMT_PLUS_PLUS;                                             \
    *rend = (char *)fmt;                                       \
    return XXX;                                                \
    break;

  // conversion specifiers
  switch (*fmt) {
    CONVERSION_SPEC_AND_RET('d', D)
    CONVERSION_SPEC_AND_RET('i', I)
    CONVERSION_SPEC_AND_RET('o', O)
    CONVERSION_SPEC_AND_RET('u', U)
    CONVERSION_SPEC_AND_RET('x', x)
    CONVERSION_SPEC_AND_RET('X', X)
    CONVERSION_SPEC_AND_RET('f', f)
    CONVERSION_SPEC_AND_RET('F', F)
    CONVERSION_SPEC_AND_RET('e', e)
    CONVERSION_SPEC_AND_RET('E', E)
    CONVERSION_SPEC_AND_RET('g', g)
    CONVERSION_SPEC_AND_RET('G', G)
    CONVERSION_SPEC_AND_RET('a', a)
    CONVERSION_SPEC_AND_RET('A', A)
    CONVERSION_SPEC_AND_RET('c', C)
    CONVERSION_SPEC_AND_RET('s', S)
    CONVERSION_SPEC_AND_RET('p', P)

  // %% has been handled

  default:
    return -1;
  }
}

static void
free_printf_state(gen_printf_state state)
{
  gen_printf_state s;

  while (state) {
    s = state->next;

    if (state->str)
      CL_FREE(state->str);

    CL_FREE(state);
    state = s;
  }
}

static gen_printf_state
parser_printf_fmt(char *format)
{
  char *begin;
  char *end;
  char *p;
  char ret_char;
  char *rend;
  gen_printf_state curr, prev, first;

  p = format;
  begin = format;
  end = format + strlen(format);
  first = NULL;
  prev = NULL;

  /* Now parse it. */
  while (*begin) {
    p = begin;

  again:
    while (p < end && *p != '%') {
      p++;
    }
    if (p < end && p + 1 == end) { // String with % at end.
      printf("string end with %%\n");
      goto error;
    }
    if (p + 1 < end && *(p + 1) == '%') { // %%
      p += 2;
      goto again;
    }

    if (p != begin) {
      curr = CL_CALLOC(1, sizeof(_gen_printf_state));
      curr->conversion_specifier = GEN_PRINTF_CONVERSION_S;
      curr->str = CL_MALLOC(p - begin + 1);
      memcpy(curr->str, begin, p - begin);

      curr->str[p - begin] = 0;
      if (first == NULL) {
        first = curr;
      }
      if (prev) {
        prev->next = curr;
      }
      prev = curr;
    }

    if (p == end) // finish
      break;

    /* Now parse the % start conversion_specifier. */
    curr = CL_CALLOC(1, sizeof(_gen_printf_state));
    ret_char = parse_printf_state(p, end, &rend, curr);
    if (ret_char < 0) {
      goto error;
    }

    if (curr->vector_n > 0) {
      curr->str = generate_printf_fmt(curr); // Standard printf can not recognize %v4XXX
    } else {
      curr->str = CL_MALLOC(rend - p + 1);
      memcpy(curr->str, p, rend - p);
      curr->str[rend - p] = 0;
    }

    if (first == NULL) {
      first = curr;
    }
    if (prev) {
      prev->next = curr;
    }
    prev = curr;

    if (rend == end)
      break;

    begin = rend;
  }

#if 0
  {
    cl_int j = 0;
    gen_printf_state s = first;
    while (s) {
      fprintf(stderr, "---- %d ---- state : \n", j);
      fprintf(stderr, "             conversion_specifier : %d\n", s->conversion_specifier);
      fprintf(stderr, "             vector_n : %d\n", s->vector_n);
      fprintf(stderr, "             left_justified : %d\n", s->left_justified);
      fprintf(stderr, "             sign_symbol: %d\n", s->sign_symbol);
      fprintf(stderr, "             alter_form : %d\n", s->alter_form);
      fprintf(stderr, "             zero_padding : %d\n", s->zero_padding);
      fprintf(stderr, "             min_width : %d\n", s->min_width);
      fprintf(stderr, "             precision : %d\n", s->precision);
      fprintf(stderr, "             length_modifier : %d\n", s->length_modifier);
      fprintf(stderr, "             string :  %s      strlen is %ld\n", s->str, strlen(s->str));
      j++;
      s = s->next;
    }
  }
#endif

  return first;

error:
  printf("error format string.\n");
  free_printf_state(first);
  return NULL;
}

static void
output_one_printf(gen_printf_state all_state, cl_gen_printf_log log)
{
#define PRINT_SOMETHING(target_ty)                      \
  do {                                                  \
    printf(s->str, *(target_ty *)(data + data_offset)); \
    data_offset += sizeof(target_ty);                   \
  } while (0)

  gen_printf_state s = all_state;
  cl_int vec_num, vec_i;
  char *data = (char *)(log) + 3 * sizeof(uint32_t);
  size_t data_offset = 0;

  while (s) {
    if (s->conversion_specifier == GEN_PRINTF_CONVERSION_S) {
      printf("%s", s->str);
      s = s->next;
      continue;
    }

    vec_num = s->vector_n > 0 ? s->vector_n : 1;
    for (vec_i = 0; vec_i < vec_num; vec_i++) {
      if (vec_i)
        printf(",");

      switch (s->conversion_specifier) {
      case GEN_PRINTF_CONVERSION_D:
      case GEN_PRINTF_CONVERSION_I:
        if (s->length_modifier == GEN_PRINTF_LM_L)
          PRINT_SOMETHING(uint64_t);
        else
          PRINT_SOMETHING(int);
        break;

      case GEN_PRINTF_CONVERSION_O:
        if (s->length_modifier == GEN_PRINTF_LM_L)
          PRINT_SOMETHING(uint64_t);
        else
          PRINT_SOMETHING(int);
        break;
      case GEN_PRINTF_CONVERSION_U:
        if (s->length_modifier == GEN_PRINTF_LM_L)
          PRINT_SOMETHING(uint64_t);
        else
          PRINT_SOMETHING(int);
        break;
      case GEN_PRINTF_CONVERSION_X:
        if (s->length_modifier == GEN_PRINTF_LM_L)
          PRINT_SOMETHING(uint64_t);
        else
          PRINT_SOMETHING(int);
        break;
      case GEN_PRINTF_CONVERSION_x:
        if (s->length_modifier == GEN_PRINTF_LM_L)
          PRINT_SOMETHING(uint64_t);
        else
          PRINT_SOMETHING(int);
        break;

      case GEN_PRINTF_CONVERSION_C:
        PRINT_SOMETHING(char);
        break;

      case GEN_PRINTF_CONVERSION_F:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_f:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_E:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_e:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_G:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_g:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_A:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_a:
        PRINT_SOMETHING(float);
        break;
      case GEN_PRINTF_CONVERSION_P:
        PRINT_SOMETHING(int);
        break;

      default:
        assert(0);
        return;
      }
    }

    s = s->next;
  }
}

LOCAL void
cl_gen_output_printf(void *buf_addr, uint32_t buf_size, cl_uint *ids,
                     char **fmts, uint32_t printf_num)
{
  uint32_t parsed;
  uint32_t total_sz = ((uint32_t *)buf_addr)[0];
  char *p = (char *)buf_addr + sizeof(uint32_t);
  uint32_t i;
  gen_printf_state all_states;

  if (total_sz > buf_size)
    total_sz = buf_size;

  for (parsed = 4; parsed < total_sz;) {
    cl_gen_printf_log log = (cl_gen_printf_log)(p);
    if (log->magic != GEN_PRINTF_LOG_MAGIC) {
      CL_LOG_ERROR("Printf log output has wrong magic");
      return;
    }

    for (i = 0; i < printf_num; i++) {
      if (ids[i] == log->statement_num)
        break;
    }
    if (i == printf_num) {
      CL_LOG_ERROR("Printf log output, can not find the printf statement for %d",
                   log->statement_num);
      return;
    }

    all_states = parser_printf_fmt(fmts[i]);
    if (all_states == NULL) {
      CL_LOG_ERROR("Printf statement %d with wrong format %s",
                   log->statement_num, fmts[i]);
      continue;
    }

    output_one_printf(all_states, log);
    free_printf_state(all_states);

    parsed += log->size;
    p += log->size;
  }
}