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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
|
/* OpenCL runtime library: print the content of the printf_buffer to STDOUT
Copyright (c) 2013 Erik Schnetter <eschnetter@perimeterinstitute.ca>
Perimeter Institute for Theoretical Physics
Copyright (c) 2018 Michal Babej / Tampere University of Technology
Copyright (c) 2024 Michal Babej / Intel Finland Oy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "printf_buffer.h"
#include "common.h"
#include "pocl_compiler_macros.h"
#include "pocl_debug.h"
#include "printf_base.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdarg.h>
#if 0
int debugprintf(const char *fmt, ...)
{
int ret;
va_list vargs;
va_start(vargs, fmt);
ret = vdprintf(2, fmt, vargs);
va_end(vargs);
return ret;
}
#define DEBUG_PRINTF(args) (debugprintf args)
#else
#define DEBUG_PRINTF(args) ((void)0)
#endif
/**************************************************************************/
/* flags are saved on each loop iter because
* __pocl_printf_(u)long can change flags */
#define DEFINE_PRINT_INTS(NAME, INT_TYPE, UINT_TYPE) \
void __pocl_print_ints_##NAME (param_t *p, const char *vals, int n, \
int is_unsigned) \
{ \
DEBUG_PRINTF (("[printf:ints:n=%df]\n", n)); \
flags_t saved_user_flags = p->flags; \
for (int d = 0; d < n; ++d) \
{ \
DEBUG_PRINTF ( \
("[printf:ints:d=%d|size=%d]\n", d, sizeof (UINT_TYPE))); \
p->flags = saved_user_flags; \
if (d != 0) \
__pocl_printf_putcf (p, ','); \
if (is_unsigned) \
{ \
UINT_TYPE tmp; \
memcpy (&tmp, vals, sizeof (UINT_TYPE)); \
vals += sizeof (UINT_TYPE); \
DEBUG_PRINTF (("[printf:ints:VAL=%lu]\n", (UINT_T)tmp)); \
__pocl_printf_ulong (p, (UINT_T)tmp); \
} \
else \
{ \
INT_TYPE tmp; \
memcpy (&tmp, vals, sizeof (INT_TYPE)); \
vals += sizeof (INT_TYPE); \
DEBUG_PRINTF (("[printf:ints:VAL=%li]\n", (INT_T)tmp)); \
__pocl_printf_long (p, (INT_T)tmp); \
} \
} \
DEBUG_PRINTF (("[printf:ints:done]\n")); \
}
DEFINE_PRINT_INTS (uchar, int8_t, uint8_t)
DEFINE_PRINT_INTS (ushort, int16_t, uint16_t)
DEFINE_PRINT_INTS (uint, int32_t, uint32_t)
DEFINE_PRINT_INTS (ulong, int64_t, uint64_t)
#undef DEFINE_PRINT_INTS
/**************************************************************************/
/* Note: NANs are printed always positive.
* This is required to pass 1.2 conformance test. */
#define DEFINE_PRINT_FLOATS(FLOAT_TYPE) \
void __pocl_print_floats_##FLOAT_TYPE (param_t *p, const void *vals, int n) \
{ \
const char *NANs[2] = { "nan", "NAN" }; \
const char *INFs[2] = { "inf", "INF" }; \
FLOAT_TYPE val; \
DEBUG_PRINTF (("[printf:floats:n=%d]\n", n)); \
flags_t saved_user_flags = p->flags; \
for (int d = 0; d < n; ++d) \
{ \
DEBUG_PRINTF (("[printf:floats:d=%d]\n", d)); \
p->flags = saved_user_flags; \
if (d != 0) \
__pocl_printf_putcf (p, ','); \
memcpy (&val, vals, sizeof (FLOAT_TYPE)); \
vals = (char *)vals + sizeof (FLOAT_TYPE); \
const char *other = NULL; \
if (isnan (val)) \
other = NANs[p->flags.uc]; \
if (isinf (val)) \
{ \
p->flags.sign = signbit (val) ? 1 : 0; \
other = INFs[p->flags.uc]; \
} \
if (other) \
{ \
__pocl_printf_nonfinite (p, other); \
DEBUG_PRINTF ( \
("[printf:floats:sign=%u,other=%s]\n", p->flags.sign, other)); \
} \
else \
{ \
__pocl_printf_float (p, (FLOAT_T)val); \
} \
} \
DEBUG_PRINTF (("[printf:floats:done]\n")); \
}
#ifdef HOST_COMPILER_SUPPORTS_FLOAT16
DEFINE_PRINT_FLOATS (_Float16)
#endif
DEFINE_PRINT_FLOATS (float)
DEFINE_PRINT_FLOATS (double)
#undef DEFINE_PRINT_FLOATS
/**************************************************************************/
#define FORWARD_BUFFER(SIZE) \
buffer += (SIZE); \
if (buffer_size < (SIZE)) \
{ \
POCL_MSG_ERR ( \
"printf error: exhausted arguments before format string end\n"); \
return -1; \
} \
buffer_size -= (SIZE);
uint32_t
__pocl_printf_format_full (param_t *p, char *buffer, uint32_t buffer_size)
{
char ch = 0;
/* fetch & decode the control dword */
uint32_t ctr_dword;
memcpy (&ctr_dword, buffer, sizeof (uint32_t));
FORWARD_BUFFER (sizeof (uint32_t));
/* if this flag is set, the fmt str is a constant stored as pointer
* otherwise it's a dynamic string stored directly in the buffer */
uint32_t skip_fmt_str = ctr_dword & PRINTF_BUFFER_CTWORD_SKIP_FMT_STR;
uint32_t float_promotion = ctr_dword & PRINTF_BUFFER_CTWORD_FLOAT_PR;
uint32_t big_endian = ctr_dword & PRINTF_BUFFER_CTWORD_BIG_ENDIAN;
uint32_t pointer_size_bytes
= ctr_dword & PRINTF_BUFFER_CTWORD_32BIT_POINTERS ? 4 : 8;
if (big_endian)
{
POCL_MSG_ERR ("printf error: printf for big endian devices not yet"
"implemented\n");
return -1;
}
assert ((ctr_dword >> PRINTF_BUFFER_CTWORD_FLAG_BITS)
== (buffer_size + sizeof (uint32_t)));
const char *format;
if (skip_fmt_str)
{
/* pointer to format string
* 8 bytes are always reserved for the address */
memcpy (&format, buffer, sizeof (const char *));
FORWARD_BUFFER (sizeof (uint64_t));
DEBUG_PRINTF (("[printf:SKIP START:buffer=%p buffer_size=%u]\n", buffer,
buffer_size));
if (format == NULL)
{
POCL_MSG_ERR ("printf error: invalid (NULL) format string!\n");
return -1;
}
if (*format == 0)
return 0;
}
else
{
/* format string stored directly */
format = buffer;
/* strlen */
while (*buffer && (buffer - format < buffer_size))
++buffer;
if (*buffer)
{
POCL_MSG_ERR ("printf error: unterminated format string ?\n");
return -1;
}
else
{
/* skip NULL */
++buffer;
}
uint32_t fmt_str_len = buffer - format;
buffer_size -= fmt_str_len;
DEBUG_PRINTF (("[printf:FULL START:buffer=%p buffer_size=%u]\n", buffer,
buffer_size));
if (fmt_str_len == 0)
return 0;
}
DEBUG_PRINTF (("[printf:format=%s]\n", format));
while ((ch = *format++))
{
if (ch == '%')
{
ch = *format++;
if (ch == 0)
{
POCL_MSG_ERR ("printf error: NULL after format sign (%%)\n");
return -1;
}
if (ch == '%')
{
DEBUG_PRINTF (("[printf:%%]\n"));
__pocl_printf_putcf (p, '%'); /* literal % */
}
else
{
DEBUG_PRINTF (("###################\n[printf:arg]\n"));
if (buffer_size == 0)
{
POCL_MSG_ERR ("printf error: exhausted arguments but format "
"string contains more specifiers\n");
return -1;
}
/* Flags */
flags_t flags;
flags.align_left = 0;
flags.sign = 0;
flags.space = 0;
flags.alt = 0;
flags.zero = 0;
flags.uc = 0;
flags.always_sign = 0;
flags.nonzeroparam = 0;
for (;;)
{
switch (ch)
{
case '-':
if (flags.align_left)
{
POCL_MSG_ERR (
"printf error: repeated align-left flag (-)\n");
return -1;
}
flags.align_left = 1;
break;
case '+':
if (flags.always_sign)
{
POCL_MSG_ERR (
"printf error: repeated always-sign flag (+)\n");
return -1;
}
flags.always_sign = 1;
break;
case ' ':
if (flags.space)
{
POCL_MSG_ERR ("printf error: repeated space flag\n");
return -1;
}
flags.space = 1;
break;
case '#':
if (flags.alt)
{
POCL_MSG_ERR (
"printf error: repeated sharp flag (#)\n");
return -1;
}
flags.alt = 1;
break;
case '0':
if (flags.zero)
{
POCL_MSG_ERR (
"printf error: repeated zero flag (0)\n");
return -1;
}
if (flags.align_left == 0)
flags.zero = 1;
break;
default:
goto flags_done;
}
ch = *format++;
}
flags_done:;
DEBUG_PRINTF (
("[printf:flags:left=%d,plus=%d,space=%d,alt=%d,zero=%d]\n",
flags.align_left, flags.sign, flags.space, flags.alt,
flags.zero));
/* Field width */
uint32_t field_width = 0;
while (ch >= '0' && ch <= '9')
{
if (ch == '0' && field_width == 0)
{
POCL_MSG_ERR ("printf error: field-width is zero\n");
return -1;
}
if (field_width > (UINT32_MAX - 9) / 10)
{
POCL_MSG_ERR ("printf error: field-width overflow\n");
return -1;
}
field_width = 10 * field_width + (ch - '0');
ch = *format++;
}
DEBUG_PRINTF (("[printf:width=%d]\n", field_width));
/* Precision */
int32_t precision = -1;
if (ch == '.')
{
precision = 0;
ch = *format++;
while (ch >= '0' && ch <= '9')
{
if (precision > (INT32_MAX - 9) / 10)
{
POCL_MSG_ERR ("printf error: precision overflow\n");
return -1;
}
precision = 10 * precision + (ch - '0');
ch = *format++;
}
}
DEBUG_PRINTF (("[printf:precision=%d]\n", precision));
/* Vector specifier */
int32_t vector_length = 0;
if (ch == 'v')
{
ch = *format++;
while (ch >= '0' && ch <= '9')
{
if (ch == '0' && vector_length == 0)
{
POCL_MSG_ERR (
"printf error: vector-length is zero\n");
return -1;
}
if (vector_length > 16)
{
POCL_MSG_ERR (
"printf error: vector-length overflow\n");
return -1;
}
vector_length = 10 * vector_length + (ch - '0');
ch = *format++;
}
if (!(vector_length == 2 || vector_length == 3
|| vector_length == 4 || vector_length == 8
|| vector_length == 16))
{
POCL_MSG_ERR (
"printf error: unrecognized vector length (%d)\n",
vector_length);
return -1;
}
}
/* Length modifier */
uint32_t element_size = 0;
if (ch == 'h')
{
ch = *format++;
if (ch == 'h')
{
ch = *format++;
element_size = 1; /* "hh" -> char */
}
else if (ch == 'l')
{
ch = *format++;
element_size = 4; /* "hl" -> int */
}
else
{
element_size = 2; /* "h" -> short */
}
}
else if (ch == 'l')
{
ch = *format++;
element_size = 8; /* "l" -> long */
}
if (vector_length > 0 && element_size == 0)
{
POCL_MSG_ERR (
"printf error: vector-length used without element size\n");
return -1;
}
if (vector_length == 0 && element_size == 4)
{
POCL_MSG_ERR (
"printf error: hl modifier used without vector length\n");
return -1;
}
if (vector_length == 0)
vector_length = 1;
uint32_t alloca_length = vector_length;
if (vector_length == 3)
alloca_length = 4;
DEBUG_PRINTF (("[printf:vector_length=%d]\n", vector_length));
DEBUG_PRINTF (("[printf:elem_length=%d]\n", element_size));
p->flags = flags;
p->conv = ch;
p->width = field_width;
p->precision = precision;
switch (ch)
{
/* Output integers */
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
{
unsigned base = 10;
if (ch == 'x' || ch == 'X')
base = 16;
if (ch == 'o')
base = 8;
if (ch == 'X')
p->flags.uc = 1;
int is_unsigned = (ch == 'u') || (base != 10);
/* if a precision is specified, the 0 flag is ignored */
if (p->precision > 0)
p->flags.zero = 0; /* The default precision is 1. */
if (precision < 0)
precision = p->precision = 1;
p->base = base;
DEBUG_PRINTF (("[printf:int:conversion=%c]\n", ch));
if (element_size == 0)
element_size = 4;
alloca_length *= element_size;
switch (element_size)
{
default:
return -1;
case 1:
__pocl_print_ints_uchar (p, buffer, vector_length,
is_unsigned);
/* stores of integers & vectors of size <= 64bits
* are expanded to 64bits in the EmitPrintf. This
* is to avoid having to deal with various rules for
* int & vector promotions by different backends
* (x86, ARM, RISC-V etc...) */
if (vector_length <= 8)
{
alloca_length = 8;
}
break;
case 2:
__pocl_print_ints_ushort (p, buffer, vector_length,
is_unsigned);
if (vector_length <= 4)
{
alloca_length = 8;
}
break;
case 4:
__pocl_print_ints_uint (p, buffer, vector_length,
is_unsigned);
if (vector_length <= 2)
{
alloca_length = 8;
}
break;
case 8:
__pocl_print_ints_ulong (p, buffer, vector_length,
is_unsigned);
break;
}
FORWARD_BUFFER (alloca_length);
DEBUG_PRINTF (
("[printf:after int:buffer=%p buffer_size=%u]\n", buffer,
buffer_size));
break;
}
/* Output floats */
case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
case 'a':
case 'A':
{
p->base = 10;
if (ch < 'X')
{
p->flags.uc = 1;
p->conv += 32;
}
DEBUG_PRINTF (
("[printf:float:conversion=%c|promotion=%u]\n", ch,
(unsigned)float_promotion));
if (element_size == 0)
element_size = 4;
alloca_length *= element_size;
switch (element_size)
{
default:
case 2:
/* we assume if the device supports float promotion,
* EmitPrintf ensures half & single floats are always
* promoted to double */
if (float_promotion && vector_length == 1)
{
alloca_length = 8;
__pocl_print_floats_double (p, buffer, vector_length);
break;
}
else
{
#ifdef HOST_COMPILER_SUPPORTS_FLOAT16
__pocl_print_floats__Float16 (p, buffer,
vector_length);
#else
POCL_MSG_ERR ("printf error: host-side "
"support for FP16 missing\n");
__pocl_print_ints_ushort (p, buffer,
vector_length,
0);
#endif
// half2 vectors <64 bits are stored in 64bits
if (alloca_length < 8)
alloca_length = 8;
break;
}
case 4:
/* we assume if the device supports float promotion,
* EmitPrintf ensures half & single floats are always
* promoted to double */
if (float_promotion && vector_length == 1)
{
/* .. else fallthrough to double */
alloca_length = 8;
__pocl_print_floats_double (p, buffer, vector_length);
break;
}
else
{
__pocl_print_floats_float (p, buffer,
vector_length);
break;
}
case 8:
__pocl_print_floats_double (p, buffer, vector_length);
break;
}
FORWARD_BUFFER (alloca_length);
DEBUG_PRINTF (
("[printf:after float:buffer=%p buffer_size=%u]\n",
buffer, buffer_size));
break;
}
/* Output a character */
case 'c':
{
DEBUG_PRINTF (("[printf:char]\n"));
if (flags.always_sign || flags.space || flags.alt
|| flags.zero)
{
POCL_MSG_ERR (
"printf error: flags used with '%%c' conversion\n");
return -1;
}
DEBUG_PRINTF (("[printf:char1]\n"));
if (precision >= 0)
{
POCL_MSG_ERR ("printf error: precision used with "
"'%%c' conversion\n");
return -1;
}
DEBUG_PRINTF (("[printf:char2]\n"));
if (vector_length != 1)
{
POCL_MSG_ERR ("printf error: vector length used with "
"'%%c' conversion\n");
return -1;
}
DEBUG_PRINTF (("[printf:char3]\n"));
if (element_size != 0)
{
POCL_MSG_ERR ("printf error: length-modifier used "
"with '%%c' conversion\n");
return -1;
}
DEBUG_PRINTF (("[printf:char4]\n"));
p->bf[0] = (char)*buffer;
p->bf[1] = 0;
/* char is always promoted to int64 */
FORWARD_BUFFER (sizeof (int64_t));
__pocl_printf_putchw (p);
DEBUG_PRINTF (
("[printf:after char:buffer=%p buffer_size=%u]\n",
buffer, buffer_size));
break;
}
/**************************************************************************/
/* Output a string */
case 's':
{
if (flags.always_sign || flags.space || flags.alt
|| flags.zero)
{
POCL_MSG_ERR (
"printf error: flags used with '%%s' conversion\n");
return -1;
}
if (vector_length != 1)
{
POCL_MSG_ERR ("printf error: vector-length used with "
"'%%s' conversion\n");
return -1;
}
if (element_size != 0)
{
POCL_MSG_ERR ("printf error: length-modifier used "
"with '%%s' conversion\n");
return -1;
}
const char *val = buffer;
// strings are stored directly
const char *tmp = buffer;
// strlen
while (*tmp && (tmp - val < buffer_size))
++tmp;
if (*tmp)
{
POCL_MSG_ERR ("printf error: string not terminated by "
"NULL for '%%s' conversion\n");
return -1;
}
else
{
// skip NULL
++tmp;
}
uint32_t str_len = tmp - val;
DEBUG_PRINTF (("[printf:string:%s:%u]\n", val, str_len));
FORWARD_BUFFER (str_len);
DEBUG_PRINTF (("[printf:START:buffer=%p buffer_size=%u]\n",
buffer, buffer_size));
if (flags.align_left)
__pocl_printf_puts_ljust (p, val, field_width,
precision);
else
__pocl_printf_puts_rjust (p, val, field_width,
precision);
break;
}
/**************************************************************************/
/* Output a pointer */
case 'p':
{
DEBUG_PRINTF (("[printf:pointer]\n"));
if (flags.always_sign || flags.space || flags.alt
|| flags.zero)
{
POCL_MSG_ERR (
"printf error: flags used with '%%p' conversion\n");
return -1;
}
if (precision >= 0)
{
POCL_MSG_ERR ("printf error: precision used with "
"'%%p' conversion\n");
return -1;
}
if (vector_length != 1)
{
POCL_MSG_ERR ("printf error: vector-length used with "
"'%%p' conversion\n");
return -1;
}
if (element_size != 0)
{
POCL_MSG_ERR ("printf error: length-modifier used "
"with '%%p' conversion\n");
return -1;
}
const void *val;
memcpy (&val, buffer, pointer_size_bytes);
FORWARD_BUFFER (pointer_size_bytes);
DEBUG_PRINTF (("[printf:ptr:%p]\n", val));
__pocl_printf_ptr (p, val);
DEBUG_PRINTF (
("[printf:after ptr:buffer=%p buffer_size=%u]\n", buffer,
buffer_size));
break;
}
/**************************************************************************/
default:
{
POCL_MSG_ERR (
"printf error: unknown conversion specifier (%c)\n", ch);
return -1;
}
}
}
}
else
{
DEBUG_PRINTF (("###################\n[printf:literal]\n"));
__pocl_printf_putcf (p, ch);
}
}
DEBUG_PRINTF (("[printf:done]\n"));
return 0;
}
/**************************************************************************/
#define IMM_FLUSH_BUFFER_SIZE 65536
void
pocl_flush_printf_buffer (char *buffer, uint32_t buffer_size)
{
param_t p = { 0 };
char bf[BUFSIZE];
memset (bf, 0, BUFSIZE);
p.bf = bf;
char result[IMM_FLUSH_BUFFER_SIZE];
p.printf_buffer = result;
p.printf_buffer_capacity = IMM_FLUSH_BUFFER_SIZE;
p.printf_buffer_index = 0;
__pocl_printf_format_full (&p, buffer, buffer_size);
if (p.printf_buffer_index > 0)
{
#ifdef _MSC_VER
write (_fileno (stdout), p.printf_buffer, p.printf_buffer_index);
#else
write (STDOUT_FILENO, p.printf_buffer, p.printf_buffer_index);
#endif
}
}
/**************************************************************************/
void
pocl_write_printf_buffer (char *printf_buffer, uint32_t bytes)
{
uint32_t control_dword, single_entry_bytes;
if (bytes == 0)
return;
do
{
if (bytes < sizeof (uint32_t))
{
POCL_MSG_ERR ("printf buffer entry size < sizeof(control word)\n");
return;
}
memcpy (&control_dword, printf_buffer, sizeof (uint32_t));
single_entry_bytes = control_dword >> PRINTF_BUFFER_CTWORD_FLAG_BITS;
if (single_entry_bytes > bytes)
{
POCL_MSG_ERR ("Error: less bytes stored in printf_buffer "
"than control word suggests\n");
return;
}
/* at minimum, an empty format string must exist */
if (single_entry_bytes < 5)
{
POCL_MSG_ERR ("Error: malformed entry in printf_buffer\n");
return;
}
pocl_flush_printf_buffer (printf_buffer, single_entry_bytes);
printf_buffer += single_entry_bytes;
bytes -= single_entry_bytes;
}
while (bytes > 0);
}
|