[Mesa-dev] [PATCH 25/33] intel: compiler: abstract printing

Scott D Phillips scott.d.phillips at intel.com
Tue Oct 31 20:25:02 UTC 2017


Lionel Landwerlin <lionel.g.landwerlin at intel.com> writes:

> This is required to have output redirected to something else than a
> file descriptor (stdout).

an alternative here might be to fdopen() a pipe(). Maybe a bit weird but
it could save several hundred lines of change and maybe get you
buffering to boot. however you feel about that, modulo comments made,
Patches 16-25 are:

Reviewed-by: Scott D Phillips <scott.d.phillips at intel.com>

> Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
> ---
>  src/intel/compiler/brw_compile_clip.c     |   5 +-
>  src/intel/compiler/brw_compile_sf.c       |   5 +-
>  src/intel/compiler/brw_disasm.c           | 645 ++++++++++++++++--------------
>  src/intel/compiler/brw_eu.c               |  39 --
>  src/intel/compiler/brw_eu.h               |  13 +-
>  src/intel/compiler/brw_eu_compact.c       |   9 +-
>  src/intel/compiler/intel_asm_annotation.c |   5 +-
>  src/intel/compiler/test_eu_compact.cpp    |   7 +-
>  src/intel/tools/aubinator.c               |  12 +-
>  src/intel/tools/disasm.c                  |  63 ++-
>  src/intel/tools/gen_disasm.h              |   7 +-
>  11 files changed, 459 insertions(+), 351 deletions(-)
>
> diff --git a/src/intel/compiler/brw_compile_clip.c b/src/intel/compiler/brw_compile_clip.c
> index 83788e4b648..8ed82a78fb4 100644
> --- a/src/intel/compiler/brw_compile_clip.c
> +++ b/src/intel/compiler/brw_compile_clip.c
> @@ -88,7 +88,10 @@ brw_compile_clip(const struct brw_compiler *compiler,
>     if (unlikely(INTEL_DEBUG & DEBUG_CLIP)) {
>        fprintf(stderr, "clip:\n");
>        brw_disassemble(compiler->devinfo,
> -                      program, 0, *final_assembly_size, stderr);
> +                      program, 0, *final_assembly_size,
> +                      &(struct brw_print){
> +                         .handle = stderr,
> +                         .string = (brw_print_cb) fputs, });
>        fprintf(stderr, "\n");
>     }
>  
> diff --git a/src/intel/compiler/brw_compile_sf.c b/src/intel/compiler/brw_compile_sf.c
> index 91e8a6da6cf..8ad5497290e 100644
> --- a/src/intel/compiler/brw_compile_sf.c
> +++ b/src/intel/compiler/brw_compile_sf.c
> @@ -871,7 +871,10 @@ brw_compile_sf(const struct brw_compiler *compiler,
>     if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
>        fprintf(stderr, "sf:\n");
>        brw_disassemble(compiler->devinfo,
> -                      program, 0, *final_assembly_size, stderr);
> +                      program, 0, *final_assembly_size,
> +                      &(struct brw_print){
> +                         .handle = stderr,
> +                         .string = (brw_print_cb) fputs, });
>        fprintf(stderr, "\n");
>     }
>  
> diff --git a/src/intel/compiler/brw_disasm.c b/src/intel/compiler/brw_disasm.c
> index 1a94ed39540..b09813529ac 100644
> --- a/src/intel/compiler/brw_disasm.c
> +++ b/src/intel/compiler/brw_disasm.c
> @@ -24,6 +24,8 @@
>  #include <string.h>
>  #include <stdarg.h>
>  
> +#include "common/gen_debug.h"
> +
>  #include "brw_eu_defines.h"
>  #include "brw_inst.h"
>  #include "brw_shader.h"
> @@ -568,18 +570,18 @@ static const char *const sampler_target_format[4] = {
>  static int column;
>  
>  static int
> -string(FILE *file, const char *string)
> +string(const struct brw_print *prnt, const char *string)
>  {
> -   fputs(string, file);
> +   prnt->string(string, prnt->handle);
>     column += strlen(string);
>     return 0;
>  }
>  
>  static int
> -format(FILE *f, const char *format, ...) PRINTFLIKE(2, 3);
> +format(const struct brw_print *prnt, const char *format, ...) PRINTFLIKE(2, 3);
>  
>  static int
> -format(FILE *f, const char *format, ...)
> +format(const struct brw_print *prnt, const char *format, ...)
>  {
>     char buf[1024];
>     va_list args;
> @@ -587,39 +589,39 @@ format(FILE *f, const char *format, ...)
>  
>     vsnprintf(buf, sizeof(buf) - 1, format, args);
>     va_end(args);
> -   string(f, buf);
> +   string(prnt, buf);
>     return 0;
>  }
>  
>  static int
> -newline(FILE *f)
> +newline(const struct brw_print *prnt)
>  {
> -   putc('\n', f);
> +   prnt->string("\n", prnt->handle);
>     column = 0;
>     return 0;
>  }
>  
>  static int
> -pad(FILE *f, int c)
> +pad(const struct brw_print *prnt, int c)
>  {
>     do
> -      string(f, " ");
> +      string(prnt, " ");
>     while (column < c);
>     return 0;
>  }
>  
>  static int
> -control(FILE *file, const char *name, const char *const ctrl[],
> +control(const struct brw_print *prnt, const char *name, const char *const ctrl[],
>          unsigned id, int *space)
>  {
>     if (!ctrl[id]) {
> -      fprintf(file, "*** invalid %s value %d ", name, id);
> +      format(prnt, "*** invalid %s value %d ", name, id);
>        return 1;
>     }
>     if (ctrl[id][0]) {
>        if (space && *space)
> -         string(file, " ");
> -      string(file, ctrl[id]);
> +         string(prnt, " ");
> +      string(prnt, ctrl[id]);
>        if (space)
>           *space = 1;
>     }
> @@ -627,20 +629,20 @@ control(FILE *file, const char *name, const char *const ctrl[],
>  }
>  
>  static int
> -print_opcode(FILE *file, const struct gen_device_info *devinfo,
> +print_opcode(const struct brw_print *prnt, const struct gen_device_info *devinfo,
>               enum opcode id)
>  {
>     const struct opcode_desc *desc = brw_opcode_desc(devinfo, id);
>     if (!desc) {
> -      format(file, "*** invalid opcode value %d ", id);
> +      format(prnt, "*** invalid opcode value %d ", id);
>        return 1;
>     }
> -   string(file, desc->name);
> +   string(prnt, desc->name);
>     return 0;
>  }
>  
>  static int
> -reg(FILE *file, unsigned _reg_file, unsigned _reg_nr)
> +reg(const struct brw_print *prnt, unsigned _reg_file, unsigned _reg_nr)
>  {
>     int err = 0;
>  
> @@ -651,55 +653,56 @@ reg(FILE *file, unsigned _reg_file, unsigned _reg_nr)
>     if (_reg_file == BRW_ARCHITECTURE_REGISTER_FILE) {
>        switch (_reg_nr & 0xf0) {
>        case BRW_ARF_NULL:
> -         string(file, "null");
> +         string(prnt, "null");
>           break;
>        case BRW_ARF_ADDRESS:
> -         format(file, "a%d", _reg_nr & 0x0f);
> +         format(prnt, "a%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_ACCUMULATOR:
> -         format(file, "acc%d", _reg_nr & 0x0f);
> +         format(prnt, "acc%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_FLAG:
> -         format(file, "f%d", _reg_nr & 0x0f);
> +         format(prnt, "f%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_MASK:
> -         format(file, "mask%d", _reg_nr & 0x0f);
> +         format(prnt, "mask%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_MASK_STACK:
> -         format(file, "msd%d", _reg_nr & 0x0f);
> +         format(prnt, "msd%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_STATE:
> -         format(file, "sr%d", _reg_nr & 0x0f);
> +         format(prnt, "sr%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_CONTROL:
> -         format(file, "cr%d", _reg_nr & 0x0f);
> +         format(prnt, "cr%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_NOTIFICATION_COUNT:
> -         format(file, "n%d", _reg_nr & 0x0f);
> +         format(prnt, "n%d", _reg_nr & 0x0f);
>           break;
>        case BRW_ARF_IP:
> -         string(file, "ip");
> +         string(prnt, "ip");
>           return -1;
>           break;
>        case BRW_ARF_TDR:
> -         format(file, "tdr0");
> +         format(prnt, "tdr0");
>           return -1;
>        case BRW_ARF_TIMESTAMP:
> -         format(file, "tm%d", _reg_nr & 0x0f);
> +         format(prnt, "tm%d", _reg_nr & 0x0f);
>           break;
>        default:
> -         format(file, "ARF%d", _reg_nr);
> +         format(prnt, "ARF%d", _reg_nr);
>           break;
>        }
>     } else {
> -      err |= control(file, "src reg file", reg_file, _reg_file, NULL);
> -      format(file, "%d", _reg_nr);
> +      err |= control(prnt, "src reg file", reg_file, _reg_file, NULL);
> +      format(prnt, "%d", _reg_nr);
>     }
>     return err;
>  }
>  
>  static int
> -dest(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +dest(const struct brw_print *prnt,
> +     const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     enum brw_reg_type type = brw_inst_dst_type(devinfo, inst);
>     unsigned elem_size = brw_reg_type_to_size(type);
> @@ -707,46 +710,46 @@ dest(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>  
>     if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
>        if (brw_inst_dst_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         err |= reg(file, brw_inst_dst_reg_file(devinfo, inst),
> +         err |= reg(prnt, brw_inst_dst_reg_file(devinfo, inst),
>                      brw_inst_dst_da_reg_nr(devinfo, inst));
>           if (err == -1)
>              return 0;
>           if (brw_inst_dst_da1_subreg_nr(devinfo, inst))
> -            format(file, ".%"PRIu64, brw_inst_dst_da1_subreg_nr(devinfo, inst) /
> +            format(prnt, ".%"PRIu64, brw_inst_dst_da1_subreg_nr(devinfo, inst) /
>                     elem_size);
> -         string(file, "<");
> -         err |= control(file, "horiz stride", horiz_stride,
> +         string(prnt, "<");
> +         err |= control(prnt, "horiz stride", horiz_stride,
>                          brw_inst_dst_hstride(devinfo, inst), NULL);
> -         string(file, ">");
> -         string(file, brw_reg_type_to_letters(type));
> +         string(prnt, ">");
> +         string(prnt, brw_reg_type_to_letters(type));
>        } else {
> -         string(file, "g[a0");
> +         string(prnt, "g[a0");
>           if (brw_inst_dst_ia_subreg_nr(devinfo, inst))
> -            format(file, ".%"PRIu64, brw_inst_dst_ia_subreg_nr(devinfo, inst) /
> +            format(prnt, ".%"PRIu64, brw_inst_dst_ia_subreg_nr(devinfo, inst) /
>                     elem_size);
>           if (brw_inst_dst_ia1_addr_imm(devinfo, inst))
> -            format(file, " %d", brw_inst_dst_ia1_addr_imm(devinfo, inst));
> -         string(file, "]<");
> -         err |= control(file, "horiz stride", horiz_stride,
> +            format(prnt, " %d", brw_inst_dst_ia1_addr_imm(devinfo, inst));
> +         string(prnt, "]<");
> +         err |= control(prnt, "horiz stride", horiz_stride,
>                          brw_inst_dst_hstride(devinfo, inst), NULL);
> -         string(file, ">");
> -         string(file, brw_reg_type_to_letters(type));
> +         string(prnt, ">");
> +         string(prnt, brw_reg_type_to_letters(type));
>        }
>     } else {
>        if (brw_inst_dst_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         err |= reg(file, brw_inst_dst_reg_file(devinfo, inst),
> +         err |= reg(prnt, brw_inst_dst_reg_file(devinfo, inst),
>                      brw_inst_dst_da_reg_nr(devinfo, inst));
>           if (err == -1)
>              return 0;
>           if (brw_inst_dst_da16_subreg_nr(devinfo, inst))
> -            format(file, ".%u", 16 / elem_size);
> -         string(file, "<1>");
> -         err |= control(file, "writemask", writemask,
> +            format(prnt, ".%u", 16 / elem_size);
> +         string(prnt, "<1>");
> +         err |= control(prnt, "writemask", writemask,
>                          brw_inst_da16_writemask(devinfo, inst), NULL);
> -         string(file, brw_reg_type_to_letters(type));
> +         string(prnt, brw_reg_type_to_letters(type));
>        } else {
>           err = 1;
> -         string(file, "Indirect align16 address mode not supported");
> +         string(prnt, "Indirect align16 address mode not supported");
>        }
>     }
>  
> @@ -754,7 +757,8 @@ dest(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>  }
>  
>  static int
> -dest_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +dest_3src(const struct brw_print *prnt,
> +          const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     bool is_align1 = brw_inst_3src_access_mode(devinfo, inst) == BRW_ALIGN_1;
>     int err = 0;
> @@ -769,7 +773,7 @@ dest_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>     else
>        reg_file = BRW_GENERAL_REGISTER_FILE;
>  
> -   err |= reg(file, reg_file, brw_inst_3src_dst_reg_nr(devinfo, inst));
> +   err |= reg(prnt, reg_file, brw_inst_3src_dst_reg_nr(devinfo, inst));
>     if (err == -1)
>        return 0;
>  
> @@ -783,36 +787,36 @@ dest_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>     subreg_nr /= brw_reg_type_to_size(type);
>  
>     if (subreg_nr)
> -      format(file, ".%u", subreg_nr);
> -   string(file, "<1>");
> +      format(prnt, ".%u", subreg_nr);
> +   string(prnt, "<1>");
>  
>     if (!is_align1) {
> -      err |= control(file, "writemask", writemask,
> +      err |= control(prnt, "writemask", writemask,
>                       brw_inst_3src_a16_dst_writemask(devinfo, inst), NULL);
>     }
> -   string(file, brw_reg_type_to_letters(type));
> +   string(prnt, brw_reg_type_to_letters(type));
>  
>     return 0;
>  }
>  
>  static int
> -src_align1_region(FILE *file,
> +src_align1_region(const struct brw_print *prnt,
>                    unsigned _vert_stride, unsigned _width,
>                    unsigned _horiz_stride)
>  {
>     int err = 0;
> -   string(file, "<");
> -   err |= control(file, "vert stride", vert_stride, _vert_stride, NULL);
> -   string(file, ",");
> -   err |= control(file, "width", width, _width, NULL);
> -   string(file, ",");
> -   err |= control(file, "horiz_stride", horiz_stride, _horiz_stride, NULL);
> -   string(file, ">");
> +   string(prnt, "<");
> +   err |= control(prnt, "vert stride", vert_stride, _vert_stride, NULL);
> +   string(prnt, ",");
> +   err |= control(prnt, "width", width, _width, NULL);
> +   string(prnt, ",");
> +   err |= control(prnt, "horiz_stride", horiz_stride, _horiz_stride, NULL);
> +   string(prnt, ">");
>     return err;
>  }
>  
>  static int
> -src_da1(FILE *file,
> +src_da1(const struct brw_print *prnt,
>          const struct gen_device_info *devinfo,
>          unsigned opcode,
>          enum brw_reg_type type, unsigned _reg_file,
> @@ -823,26 +827,26 @@ src_da1(FILE *file,
>     int err = 0;
>  
>     if (devinfo->gen >= 8 && is_logic_instruction(opcode))
> -      err |= control(file, "bitnot", m_bitnot, _negate, NULL);
> +      err |= control(prnt, "bitnot", m_bitnot, _negate, NULL);
>     else
> -      err |= control(file, "negate", m_negate, _negate, NULL);
> +      err |= control(prnt, "negate", m_negate, _negate, NULL);
>  
> -   err |= control(file, "abs", _abs, __abs, NULL);
> +   err |= control(prnt, "abs", _abs, __abs, NULL);
>  
> -   err |= reg(file, _reg_file, reg_num);
> +   err |= reg(prnt, _reg_file, reg_num);
>     if (err == -1)
>        return 0;
>     if (sub_reg_num) {
>        unsigned elem_size = brw_reg_type_to_size(type);
> -      format(file, ".%d", sub_reg_num / elem_size);   /* use formal style like spec */
> +      format(prnt, ".%d", sub_reg_num / elem_size);   /* use formal style like spec */
>     }
> -   src_align1_region(file, _vert_stride, _width, _horiz_stride);
> -   string(file, brw_reg_type_to_letters(type));
> +   src_align1_region(prnt, _vert_stride, _width, _horiz_stride);
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
>  static int
> -src_ia1(FILE *file,
> +src_ia1(const struct brw_print *prnt,
>          const struct gen_device_info *devinfo,
>          unsigned opcode,
>          enum brw_reg_type type,
> @@ -856,25 +860,25 @@ src_ia1(FILE *file,
>     int err = 0;
>  
>     if (devinfo->gen >= 8 && is_logic_instruction(opcode))
> -      err |= control(file, "bitnot", m_bitnot, _negate, NULL);
> +      err |= control(prnt, "bitnot", m_bitnot, _negate, NULL);
>     else
> -      err |= control(file, "negate", m_negate, _negate, NULL);
> +      err |= control(prnt, "negate", m_negate, _negate, NULL);
>  
> -   err |= control(file, "abs", _abs, __abs, NULL);
> +   err |= control(prnt, "abs", _abs, __abs, NULL);
>  
> -   string(file, "g[a0");
> +   string(prnt, "g[a0");
>     if (_addr_subreg_nr)
> -      format(file, ".%d", _addr_subreg_nr);
> +      format(prnt, ".%d", _addr_subreg_nr);
>     if (_addr_imm)
> -      format(file, " %d", _addr_imm);
> -   string(file, "]");
> -   src_align1_region(file, _vert_stride, _width, _horiz_stride);
> -   string(file, brw_reg_type_to_letters(type));
> +      format(prnt, " %d", _addr_imm);
> +   string(prnt, "]");
> +   src_align1_region(prnt, _vert_stride, _width, _horiz_stride);
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
>  static int
> -src_swizzle(FILE *file, unsigned swiz)
> +src_swizzle(const struct brw_print *prnt, unsigned swiz)
>  {
>     unsigned x = BRW_GET_SWZ(swiz, BRW_CHANNEL_X);
>     unsigned y = BRW_GET_SWZ(swiz, BRW_CHANNEL_Y);
> @@ -883,20 +887,20 @@ src_swizzle(FILE *file, unsigned swiz)
>     int err = 0;
>  
>     if (x == y && x == z && x == w) {
> -      string(file, ".");
> -      err |= control(file, "channel select", chan_sel, x, NULL);
> +      string(prnt, ".");
> +      err |= control(prnt, "channel select", chan_sel, x, NULL);
>     } else if (swiz != BRW_SWIZZLE_XYZW) {
> -      string(file, ".");
> -      err |= control(file, "channel select", chan_sel, x, NULL);
> -      err |= control(file, "channel select", chan_sel, y, NULL);
> -      err |= control(file, "channel select", chan_sel, z, NULL);
> -      err |= control(file, "channel select", chan_sel, w, NULL);
> +      string(prnt, ".");
> +      err |= control(prnt, "channel select", chan_sel, x, NULL);
> +      err |= control(prnt, "channel select", chan_sel, y, NULL);
> +      err |= control(prnt, "channel select", chan_sel, z, NULL);
> +      err |= control(prnt, "channel select", chan_sel, w, NULL);
>     }
>     return err;
>  }
>  
>  static int
> -src_da16(FILE *file,
> +src_da16(const struct brw_print *prnt,
>           const struct gen_device_info *devinfo,
>           unsigned opcode,
>           enum brw_reg_type type,
> @@ -911,13 +915,13 @@ src_da16(FILE *file,
>     int err = 0;
>  
>     if (devinfo->gen >= 8 && is_logic_instruction(opcode))
> -      err |= control(file, "bitnot", m_bitnot, _negate, NULL);
> +      err |= control(prnt, "bitnot", m_bitnot, _negate, NULL);
>     else
> -      err |= control(file, "negate", m_negate, _negate, NULL);
> +      err |= control(prnt, "negate", m_negate, _negate, NULL);
>  
> -   err |= control(file, "abs", _abs, __abs, NULL);
> +   err |= control(prnt, "abs", _abs, __abs, NULL);
>  
> -   err |= reg(file, _reg_file, _reg_nr);
> +   err |= reg(prnt, _reg_file, _reg_nr);
>     if (err == -1)
>        return 0;
>     if (_subreg_nr) {
> @@ -925,13 +929,13 @@ src_da16(FILE *file,
>  
>        /* bit4 for subreg number byte addressing. Make this same meaning as
>           in da1 case, so output looks consistent. */
> -      format(file, ".%d", 16 / elem_size);
> +      format(prnt, ".%d", 16 / elem_size);
>     }
> -   string(file, "<");
> -   err |= control(file, "vert stride", vert_stride, _vert_stride, NULL);
> -   string(file, ">");
> -   err |= src_swizzle(file, BRW_SWIZZLE4(swz_x, swz_y, swz_z, swz_w));
> -   string(file, brw_reg_type_to_letters(type));
> +   string(prnt, "<");
> +   err |= control(prnt, "vert stride", vert_stride, _vert_stride, NULL);
> +   string(prnt, ">");
> +   err |= src_swizzle(prnt, BRW_SWIZZLE4(swz_x, swz_y, swz_z, swz_w));
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
> @@ -1016,7 +1020,8 @@ implied_width(enum brw_vertical_stride _vert_stride,
>  }
>  
>  static int
> -src0_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +src0_3src(const struct brw_print *prnt,
> +          const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     int err = 0;
>     unsigned reg_nr, subreg_nr;
> @@ -1041,11 +1046,11 @@ src0_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>           enum brw_reg_type type = brw_inst_3src_a1_src0_type(devinfo, inst);
>  
>           if (type == BRW_REGISTER_TYPE_W) {
> -            format(file, "%dW", imm_val);
> +            format(prnt, "%dW", imm_val);
>           } else if (type == BRW_REGISTER_TYPE_UW) {
> -            format(file, "0x%04xUW", imm_val);
> +            format(prnt, "0x%04xUW", imm_val);
>           } else if (type == BRW_REGISTER_TYPE_HF) {
> -            format(file, "%-gF", _mesa_half_to_float(imm_val));
> +            format(prnt, "%-gF", _mesa_half_to_float(imm_val));
>           }
>           return 0;
>        }
> @@ -1077,24 +1082,25 @@ src0_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>  
>     subreg_nr /= brw_reg_type_to_size(type);
>  
> -   err |= control(file, "negate", m_negate,
> +   err |= control(prnt, "negate", m_negate,
>                    brw_inst_3src_src0_negate(devinfo, inst), NULL);
> -   err |= control(file, "abs", _abs, brw_inst_3src_src0_abs(devinfo, inst), NULL);
> +   err |= control(prnt, "abs", _abs, brw_inst_3src_src0_abs(devinfo, inst), NULL);
>  
> -   err |= reg(file, _file, reg_nr);
> +   err |= reg(prnt, _file, reg_nr);
>     if (err == -1)
>        return 0;
>     if (subreg_nr || is_scalar_region)
> -      format(file, ".%d", subreg_nr);
> -   src_align1_region(file, _vert_stride, _width, _horiz_stride);
> +      format(prnt, ".%d", subreg_nr);
> +   src_align1_region(prnt, _vert_stride, _width, _horiz_stride);
>     if (!is_scalar_region && !is_align1)
> -      err |= src_swizzle(file, brw_inst_3src_a16_src0_swizzle(devinfo, inst));
> -   string(file, brw_reg_type_to_letters(type));
> +      err |= src_swizzle(prnt, brw_inst_3src_a16_src0_swizzle(devinfo, inst));
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
>  static int
> -src1_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +src1_3src(const struct brw_print *prnt,
> +          const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     int err = 0;
>     unsigned reg_nr, subreg_nr;
> @@ -1145,24 +1151,25 @@ src1_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>  
>     subreg_nr /= brw_reg_type_to_size(type);
>  
> -   err |= control(file, "negate", m_negate,
> +   err |= control(prnt, "negate", m_negate,
>                    brw_inst_3src_src1_negate(devinfo, inst), NULL);
> -   err |= control(file, "abs", _abs, brw_inst_3src_src1_abs(devinfo, inst), NULL);
> +   err |= control(prnt, "abs", _abs, brw_inst_3src_src1_abs(devinfo, inst), NULL);
>  
> -   err |= reg(file, _file, reg_nr);
> +   err |= reg(prnt, _file, reg_nr);
>     if (err == -1)
>        return 0;
>     if (subreg_nr || is_scalar_region)
> -      format(file, ".%d", subreg_nr);
> -   src_align1_region(file, _vert_stride, _width, _horiz_stride);
> +      format(prnt, ".%d", subreg_nr);
> +   src_align1_region(prnt, _vert_stride, _width, _horiz_stride);
>     if (!is_scalar_region && !is_align1)
> -      err |= src_swizzle(file, brw_inst_3src_a16_src1_swizzle(devinfo, inst));
> -   string(file, brw_reg_type_to_letters(type));
> +      err |= src_swizzle(prnt, brw_inst_3src_a16_src1_swizzle(devinfo, inst));
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
>  static int
> -src2_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +src2_3src(const struct brw_print *prnt,
> +          const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     int err = 0;
>     unsigned reg_nr, subreg_nr;
> @@ -1187,11 +1194,11 @@ src2_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>           enum brw_reg_type type = brw_inst_3src_a1_src2_type(devinfo, inst);
>  
>           if (type == BRW_REGISTER_TYPE_W) {
> -            format(file, "%dW", imm_val);
> +            format(prnt, "%dW", imm_val);
>           } else if (type == BRW_REGISTER_TYPE_UW) {
> -            format(file, "0x%04xUW", imm_val);
> +            format(prnt, "0x%04xUW", imm_val);
>           } else if (type == BRW_REGISTER_TYPE_HF) {
> -            format(file, "%-gF", _mesa_half_to_float(imm_val));
> +            format(prnt, "%-gF", _mesa_half_to_float(imm_val));
>           }
>           return 0;
>        }
> @@ -1227,82 +1234,84 @@ src2_3src(FILE *file, const struct gen_device_info *devinfo, const brw_inst *ins
>  
>     subreg_nr /= brw_reg_type_to_size(type);
>  
> -   err |= control(file, "negate", m_negate,
> +   err |= control(prnt, "negate", m_negate,
>                    brw_inst_3src_src2_negate(devinfo, inst), NULL);
> -   err |= control(file, "abs", _abs, brw_inst_3src_src2_abs(devinfo, inst), NULL);
> +   err |= control(prnt, "abs", _abs, brw_inst_3src_src2_abs(devinfo, inst), NULL);
>  
> -   err |= reg(file, _file, reg_nr);
> +   err |= reg(prnt, _file, reg_nr);
>     if (err == -1)
>        return 0;
>     if (subreg_nr || is_scalar_region)
> -      format(file, ".%d", subreg_nr);
> -   src_align1_region(file, _vert_stride, _width, _horiz_stride);
> +      format(prnt, ".%d", subreg_nr);
> +   src_align1_region(prnt, _vert_stride, _width, _horiz_stride);
>     if (!is_scalar_region && !is_align1)
> -      err |= src_swizzle(file, brw_inst_3src_a16_src2_swizzle(devinfo, inst));
> -   string(file, brw_reg_type_to_letters(type));
> +      err |= src_swizzle(prnt, brw_inst_3src_a16_src2_swizzle(devinfo, inst));
> +   string(prnt, brw_reg_type_to_letters(type));
>     return err;
>  }
>  
>  static int
> -imm(FILE *file, const struct gen_device_info *devinfo, enum brw_reg_type type,
> +imm(const struct brw_print *prnt,
> +    const struct gen_device_info *devinfo, enum brw_reg_type type,
>      const brw_inst *inst)
>  {
>     switch (type) {
>     case BRW_REGISTER_TYPE_UQ:
> -      format(file, "0x%016lxUQ", brw_inst_imm_uq(devinfo, inst));
> +      format(prnt, "0x%016lxUQ", brw_inst_imm_uq(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_Q:
> -      format(file, "%ldQ", brw_inst_imm_uq(devinfo, inst));
> +      format(prnt, "%ldQ", brw_inst_imm_uq(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_UD:
> -      format(file, "0x%08xUD", brw_inst_imm_ud(devinfo, inst));
> +      format(prnt, "0x%08xUD", brw_inst_imm_ud(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_D:
> -      format(file, "%dD", brw_inst_imm_d(devinfo, inst));
> +      format(prnt, "%dD", brw_inst_imm_d(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_UW:
> -      format(file, "0x%04xUW", (uint16_t) brw_inst_imm_ud(devinfo, inst));
> +      format(prnt, "0x%04xUW", (uint16_t) brw_inst_imm_ud(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_W:
> -      format(file, "%dW", (int16_t) brw_inst_imm_d(devinfo, inst));
> +      format(prnt, "%dW", (int16_t) brw_inst_imm_d(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_UV:
> -      format(file, "0x%08xUV", brw_inst_imm_ud(devinfo, inst));
> +      format(prnt, "0x%08xUV", brw_inst_imm_ud(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_VF:
> -      format(file, "[%-gF, %-gF, %-gF, %-gF]VF",
> +      format(prnt, "[%-gF, %-gF, %-gF, %-gF]VF",
>               brw_vf_to_float(brw_inst_imm_ud(devinfo, inst)),
>               brw_vf_to_float(brw_inst_imm_ud(devinfo, inst) >> 8),
>               brw_vf_to_float(brw_inst_imm_ud(devinfo, inst) >> 16),
>               brw_vf_to_float(brw_inst_imm_ud(devinfo, inst) >> 24));
>        break;
>     case BRW_REGISTER_TYPE_V:
> -      format(file, "0x%08xV", brw_inst_imm_ud(devinfo, inst));
> +      format(prnt, "0x%08xV", brw_inst_imm_ud(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_F:
> -      format(file, "%-gF", brw_inst_imm_f(devinfo, inst));
> +      format(prnt, "%-gF", brw_inst_imm_f(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_DF:
> -      format(file, "%-gDF", brw_inst_imm_df(devinfo, inst));
> +      format(prnt, "%-gDF", brw_inst_imm_df(devinfo, inst));
>        break;
>     case BRW_REGISTER_TYPE_HF:
> -      string(file, "Half Float IMM");
> +      string(prnt, "Half Float IMM");
>        break;
>     case BRW_REGISTER_TYPE_UB:
>     case BRW_REGISTER_TYPE_B:
> -      format(file, "*** invalid immediate type %d ", type);
> +      format(prnt, "*** invalid immediate type %d ", type);
>     }
>     return 0;
>  }
>  
>  static int
> -src0(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +src0(const struct brw_print *prnt,
> +     const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     if (brw_inst_src0_reg_file(devinfo, inst) == BRW_IMMEDIATE_VALUE) {
> -      return imm(file, devinfo, brw_inst_src0_type(devinfo, inst), inst);
> +      return imm(prnt, devinfo, brw_inst_src0_type(devinfo, inst), inst);
>     } else if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
>        if (brw_inst_src0_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         return src_da1(file,
> +         return src_da1(prnt,
>                          devinfo,
>                          brw_inst_opcode(devinfo, inst),
>                          brw_inst_src0_type(devinfo, inst),
> @@ -1315,7 +1324,7 @@ src0(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>                          brw_inst_src0_abs(devinfo, inst),
>                          brw_inst_src0_negate(devinfo, inst));
>        } else {
> -         return src_ia1(file,
> +         return src_ia1(prnt,
>                          devinfo,
>                          brw_inst_opcode(devinfo, inst),
>                          brw_inst_src0_type(devinfo, inst),
> @@ -1330,7 +1339,7 @@ src0(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>        }
>     } else {
>        if (brw_inst_src0_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         return src_da16(file,
> +         return src_da16(prnt,
>                           devinfo,
>                           brw_inst_opcode(devinfo, inst),
>                           brw_inst_src0_type(devinfo, inst),
> @@ -1345,20 +1354,21 @@ src0(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>                           brw_inst_src0_da16_swiz_z(devinfo, inst),
>                           brw_inst_src0_da16_swiz_w(devinfo, inst));
>        } else {
> -         string(file, "Indirect align16 address mode not supported");
> +         string(prnt, "Indirect align16 address mode not supported");
>           return 1;
>        }
>     }
>  }
>  
>  static int
> -src1(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +src1(const struct brw_print *prnt,
> +     const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     if (brw_inst_src1_reg_file(devinfo, inst) == BRW_IMMEDIATE_VALUE) {
> -      return imm(file, devinfo, brw_inst_src1_type(devinfo, inst), inst);
> +      return imm(prnt, devinfo, brw_inst_src1_type(devinfo, inst), inst);
>     } else if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
>        if (brw_inst_src1_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         return src_da1(file,
> +         return src_da1(prnt,
>                          devinfo,
>                          brw_inst_opcode(devinfo, inst),
>                          brw_inst_src1_type(devinfo, inst),
> @@ -1371,7 +1381,7 @@ src1(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>                          brw_inst_src1_abs(devinfo, inst),
>                          brw_inst_src1_negate(devinfo, inst));
>        } else {
> -         return src_ia1(file,
> +         return src_ia1(prnt,
>                          devinfo,
>                          brw_inst_opcode(devinfo, inst),
>                          brw_inst_src1_type(devinfo, inst),
> @@ -1386,7 +1396,7 @@ src1(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>        }
>     } else {
>        if (brw_inst_src1_address_mode(devinfo, inst) == BRW_ADDRESS_DIRECT) {
> -         return src_da16(file,
> +         return src_da16(prnt,
>                           devinfo,
>                           brw_inst_opcode(devinfo, inst),
>                           brw_inst_src1_type(devinfo, inst),
> @@ -1401,14 +1411,15 @@ src1(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
>                           brw_inst_src1_da16_swiz_z(devinfo, inst),
>                           brw_inst_src1_da16_swiz_w(devinfo, inst));
>        } else {
> -         string(file, "Indirect align16 address mode not supported");
> +         string(prnt, "Indirect align16 address mode not supported");
>           return 1;
>        }
>     }
>  }
>  
>  static int
> -qtr_ctrl(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst)
> +qtr_ctrl(const struct brw_print *prnt,
> +         const struct gen_device_info *devinfo, const brw_inst *inst)
>  {
>     int qtr_ctl = brw_inst_qtr_control(devinfo, inst);
>     int exec_size = 1 << brw_inst_exec_size(devinfo, inst);
> @@ -1416,27 +1427,27 @@ qtr_ctrl(FILE *file, const struct gen_device_info *devinfo, const brw_inst *inst
>                              brw_inst_nib_control(devinfo, inst);
>  
>     if (exec_size < 8 || nib_ctl) {
> -      format(file, " %dN", qtr_ctl * 2 + nib_ctl + 1);
> +      format(prnt, " %dN", qtr_ctl * 2 + nib_ctl + 1);
>     } else if (exec_size == 8) {
>        switch (qtr_ctl) {
>        case 0:
> -         string(file, " 1Q");
> +         string(prnt, " 1Q");
>           break;
>        case 1:
> -         string(file, " 2Q");
> +         string(prnt, " 2Q");
>           break;
>        case 2:
> -         string(file, " 3Q");
> +         string(prnt, " 3Q");
>           break;
>        case 3:
> -         string(file, " 4Q");
> +         string(prnt, " 4Q");
>           break;
>        }
>     } else if (exec_size == 16) {
>        if (qtr_ctl < 2)
> -         string(file, " 1H");
> +         string(prnt, " 1H");
>        else
> -         string(file, " 2H");
> +         string(prnt, " 2H");
>     }
>     return 0;
>  }
> @@ -1449,12 +1460,18 @@ brw_disassemble_imm(const struct gen_device_info *devinfo,
>     brw_inst inst;
>     inst.data[0] = (((uint64_t) dw1) << 32) | ((uint64_t) dw0);
>     inst.data[1] = (((uint64_t) dw3) << 32) | ((uint64_t) dw2);
> -   return brw_disassemble_inst(stderr, devinfo, &inst, false);
> +   struct brw_print prnt = {
> +      .handle = stderr,
> +      .string = (brw_print_cb) fputs,
> +   };
> +
> +   return brw_disassemble_inst(&prnt, devinfo, &inst, false);
>  }
>  #endif
>  
>  int
> -brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
> +brw_disassemble_inst(const struct brw_print *prnt,
> +                     const struct gen_device_info *devinfo,
>                       const brw_inst *inst, bool is_compacted)
>  {
>     int err = 0;
> @@ -1464,35 +1481,35 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>     const struct opcode_desc *desc = brw_opcode_desc(devinfo, opcode);
>  
>     if (brw_inst_pred_control(devinfo, inst)) {
> -      string(file, "(");
> -      err |= control(file, "predicate inverse", pred_inv,
> +      string(prnt, "(");
> +      err |= control(prnt, "predicate inverse", pred_inv,
>                       brw_inst_pred_inv(devinfo, inst), NULL);
> -      format(file, "f%"PRIu64, devinfo->gen >= 7 ? brw_inst_flag_reg_nr(devinfo, inst) : 0);
> +      format(prnt, "f%"PRIu64, devinfo->gen >= 7 ? brw_inst_flag_reg_nr(devinfo, inst) : 0);
>        if (brw_inst_flag_subreg_nr(devinfo, inst))
> -         format(file, ".%"PRIu64, brw_inst_flag_subreg_nr(devinfo, inst));
> +         format(prnt, ".%"PRIu64, brw_inst_flag_subreg_nr(devinfo, inst));
>        if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
> -         err |= control(file, "predicate control align1", pred_ctrl_align1,
> +         err |= control(prnt, "predicate control align1", pred_ctrl_align1,
>                          brw_inst_pred_control(devinfo, inst), NULL);
>        } else {
> -         err |= control(file, "predicate control align16", pred_ctrl_align16,
> +         err |= control(prnt, "predicate control align16", pred_ctrl_align16,
>                          brw_inst_pred_control(devinfo, inst), NULL);
>        }
> -      string(file, ") ");
> +      string(prnt, ") ");
>     }
>  
> -   err |= print_opcode(file, devinfo, opcode);
> -   err |= control(file, "saturate", saturate, brw_inst_saturate(devinfo, inst),
> +   err |= print_opcode(prnt, devinfo, opcode);
> +   err |= control(prnt, "saturate", saturate, brw_inst_saturate(devinfo, inst),
>                    NULL);
>  
> -   err |= control(file, "debug control", debug_ctrl,
> +   err |= control(prnt, "debug control", debug_ctrl,
>                    brw_inst_debug_control(devinfo, inst), NULL);
>  
>     if (opcode == BRW_OPCODE_MATH) {
> -      string(file, " ");
> -      err |= control(file, "function", math_function,
> +      string(prnt, " ");
> +      err |= control(prnt, "function", math_function,
>                       brw_inst_math_function(devinfo, inst), NULL);
>     } else if (opcode != BRW_OPCODE_SEND && opcode != BRW_OPCODE_SENDC) {
> -      err |= control(file, "conditional modifier", conditional_modifier,
> +      err |= control(prnt, "conditional modifier", conditional_modifier,
>                       brw_inst_cond_modifier(devinfo, inst), NULL);
>  
>        /* If we're using the conditional modifier, print which flags reg is
> @@ -1503,80 +1520,80 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>            (devinfo->gen < 6 || (opcode != BRW_OPCODE_SEL &&
>                                  opcode != BRW_OPCODE_IF &&
>                                  opcode != BRW_OPCODE_WHILE))) {
> -         format(file, ".f%"PRIu64,
> +         format(prnt, ".f%"PRIu64,
>                  devinfo->gen >= 7 ? brw_inst_flag_reg_nr(devinfo, inst) : 0);
>           if (brw_inst_flag_subreg_nr(devinfo, inst))
> -            format(file, ".%"PRIu64, brw_inst_flag_subreg_nr(devinfo, inst));
> +            format(prnt, ".%"PRIu64, brw_inst_flag_subreg_nr(devinfo, inst));
>        }
>     }
>  
>     if (opcode != BRW_OPCODE_NOP && opcode != BRW_OPCODE_NENOP) {
> -      string(file, "(");
> -      err |= control(file, "execution size", exec_size,
> +      string(prnt, "(");
> +      err |= control(prnt, "execution size", exec_size,
>                       brw_inst_exec_size(devinfo, inst), NULL);
> -      string(file, ")");
> +      string(prnt, ")");
>     }
>  
>     if (opcode == BRW_OPCODE_SEND && devinfo->gen < 6)
> -      format(file, " %"PRIu64, brw_inst_base_mrf(devinfo, inst));
> +      format(prnt, " %"PRIu64, brw_inst_base_mrf(devinfo, inst));
>  
>     if (has_uip(devinfo, opcode)) {
>        /* Instructions that have UIP also have JIP. */
> -      pad(file, 16);
> -      format(file, "JIP: %d", brw_inst_jip(devinfo, inst));
> -      pad(file, 32);
> -      format(file, "UIP: %d", brw_inst_uip(devinfo, inst));
> +      pad(prnt, 16);
> +      format(prnt, "JIP: %d", brw_inst_jip(devinfo, inst));
> +      pad(prnt, 32);
> +      format(prnt, "UIP: %d", brw_inst_uip(devinfo, inst));
>     } else if (has_jip(devinfo, opcode)) {
> -      pad(file, 16);
> +      pad(prnt, 16);
>        if (devinfo->gen >= 7) {
> -         format(file, "JIP: %d", brw_inst_jip(devinfo, inst));
> +         format(prnt, "JIP: %d", brw_inst_jip(devinfo, inst));
>        } else {
> -         format(file, "JIP: %d", brw_inst_gen6_jump_count(devinfo, inst));
> +         format(prnt, "JIP: %d", brw_inst_gen6_jump_count(devinfo, inst));
>        }
>     } else if (devinfo->gen < 6 && (opcode == BRW_OPCODE_BREAK ||
>                                     opcode == BRW_OPCODE_CONTINUE ||
>                                     opcode == BRW_OPCODE_ELSE)) {
> -      pad(file, 16);
> -      format(file, "Jump: %d", brw_inst_gen4_jump_count(devinfo, inst));
> -      pad(file, 32);
> -      format(file, "Pop: %"PRIu64, brw_inst_gen4_pop_count(devinfo, inst));
> +      pad(prnt, 16);
> +      format(prnt, "Jump: %d", brw_inst_gen4_jump_count(devinfo, inst));
> +      pad(prnt, 32);
> +      format(prnt, "Pop: %"PRIu64, brw_inst_gen4_pop_count(devinfo, inst));
>     } else if (devinfo->gen < 6 && (opcode == BRW_OPCODE_IF ||
>                                     opcode == BRW_OPCODE_IFF ||
>                                     opcode == BRW_OPCODE_HALT)) {
> -      pad(file, 16);
> -      format(file, "Jump: %d", brw_inst_gen4_jump_count(devinfo, inst));
> +      pad(prnt, 16);
> +      format(prnt, "Jump: %d", brw_inst_gen4_jump_count(devinfo, inst));
>     } else if (devinfo->gen < 6 && opcode == BRW_OPCODE_ENDIF) {
> -      pad(file, 16);
> -      format(file, "Pop: %"PRIu64, brw_inst_gen4_pop_count(devinfo, inst));
> +      pad(prnt, 16);
> +      format(prnt, "Pop: %"PRIu64, brw_inst_gen4_pop_count(devinfo, inst));
>     } else if (opcode == BRW_OPCODE_JMPI) {
> -      pad(file, 16);
> -      err |= src1(file, devinfo, inst);
> +      pad(prnt, 16);
> +      err |= src1(prnt, devinfo, inst);
>     } else if (desc && desc->nsrc == 3) {
> -      pad(file, 16);
> -      err |= dest_3src(file, devinfo, inst);
> +      pad(prnt, 16);
> +      err |= dest_3src(prnt, devinfo, inst);
>  
> -      pad(file, 32);
> -      err |= src0_3src(file, devinfo, inst);
> +      pad(prnt, 32);
> +      err |= src0_3src(prnt, devinfo, inst);
>  
> -      pad(file, 48);
> -      err |= src1_3src(file, devinfo, inst);
> +      pad(prnt, 48);
> +      err |= src1_3src(prnt, devinfo, inst);
>  
> -      pad(file, 64);
> -      err |= src2_3src(file, devinfo, inst);
> +      pad(prnt, 64);
> +      err |= src2_3src(prnt, devinfo, inst);
>     } else if (desc) {
>        if (desc->ndst > 0) {
> -         pad(file, 16);
> -         err |= dest(file, devinfo, inst);
> +         pad(prnt, 16);
> +         err |= dest(prnt, devinfo, inst);
>        }
>  
>        if (desc->nsrc > 0) {
> -         pad(file, 32);
> -         err |= src0(file, devinfo, inst);
> +         pad(prnt, 32);
> +         err |= src0(prnt, devinfo, inst);
>        }
>  
>        if (desc->nsrc > 1) {
> -         pad(file, 48);
> -         err |= src1(file, devinfo, inst);
> +         pad(prnt, 48);
> +         err |= src1(prnt, devinfo, inst);
>        }
>     }
>  
> @@ -1585,78 +1602,78 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>  
>        if (brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE) {
>           /* show the indirect descriptor source */
> -         pad(file, 48);
> -         err |= src1(file, devinfo, inst);
> +         pad(prnt, 48);
> +         err |= src1(prnt, devinfo, inst);
>        }
>  
> -      newline(file);
> -      pad(file, 16);
> +      newline(prnt);
> +      pad(prnt, 16);
>        space = 0;
>  
> -      fprintf(file, "            ");
> -      err |= control(file, "SFID", devinfo->gen >= 6 ? gen6_sfid : gen4_sfid,
> +      format(prnt, "            ");
> +      err |= control(prnt, "SFID", devinfo->gen >= 6 ? gen6_sfid : gen4_sfid,
>                       sfid, &space);
>  
>  
>        if (brw_inst_src1_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE) {
> -         format(file, " indirect");
> +         format(prnt, " indirect");
>        } else {
>           switch (sfid) {
>           case BRW_SFID_MATH:
> -            err |= control(file, "math function", math_function,
> +            err |= control(prnt, "math function", math_function,
>                             brw_inst_math_msg_function(devinfo, inst), &space);
> -            err |= control(file, "math saturate", math_saturate,
> +            err |= control(prnt, "math saturate", math_saturate,
>                             brw_inst_math_msg_saturate(devinfo, inst), &space);
> -            err |= control(file, "math signed", math_signed,
> +            err |= control(prnt, "math signed", math_signed,
>                             brw_inst_math_msg_signed_int(devinfo, inst), &space);
> -            err |= control(file, "math scalar", math_scalar,
> +            err |= control(prnt, "math scalar", math_scalar,
>                             brw_inst_math_msg_data_type(devinfo, inst), &space);
> -            err |= control(file, "math precision", math_precision,
> +            err |= control(prnt, "math precision", math_precision,
>                             brw_inst_math_msg_precision(devinfo, inst), &space);
>              break;
>           case BRW_SFID_SAMPLER:
>              if (devinfo->gen >= 5) {
> -               err |= control(file, "sampler message", gen5_sampler_msg_type,
> +               err |= control(prnt, "sampler message", gen5_sampler_msg_type,
>                                brw_inst_sampler_msg_type(devinfo, inst), &space);
> -               err |= control(file, "sampler simd mode", gen5_sampler_simd_mode,
> +               err |= control(prnt, "sampler simd mode", gen5_sampler_simd_mode,
>                                brw_inst_sampler_simd_mode(devinfo, inst), &space);
> -               format(file, " Surface = %"PRIu64" Sampler = %"PRIu64,
> +               format(prnt, " Surface = %"PRIu64" Sampler = %"PRIu64,
>                        brw_inst_binding_table_index(devinfo, inst),
>                        brw_inst_sampler(devinfo, inst));
>              } else {
> -               format(file, " (%"PRIu64", %"PRIu64", %"PRIu64", ",
> +               format(prnt, " (%"PRIu64", %"PRIu64", %"PRIu64", ",
>                        brw_inst_binding_table_index(devinfo, inst),
>                        brw_inst_sampler(devinfo, inst),
>                        brw_inst_sampler_msg_type(devinfo, inst));
>                 if (!devinfo->is_g4x) {
> -                  err |= control(file, "sampler target format",
> +                  err |= control(prnt, "sampler target format",
>                                   sampler_target_format,
>                                   brw_inst_sampler_return_format(devinfo, inst), NULL);
>                 }
> -               string(file, ")");
> +               string(prnt, ")");
>              }
>              break;
>           case GEN6_SFID_DATAPORT_SAMPLER_CACHE:
>           case GEN6_SFID_DATAPORT_CONSTANT_CACHE:
>              /* aka BRW_SFID_DATAPORT_READ on Gen4-5 */
>              if (devinfo->gen >= 6) {
> -               format(file, " (%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64")",
> +               format(prnt, " (%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64")",
>                        brw_inst_binding_table_index(devinfo, inst),
>                        brw_inst_dp_msg_control(devinfo, inst),
>                        brw_inst_dp_msg_type(devinfo, inst),
>                        devinfo->gen >= 7 ? 0 : brw_inst_dp_write_commit(devinfo, inst));
>              } else {
>                 bool is_965 = devinfo->gen == 4 && !devinfo->is_g4x;
> -               err |= control(file, "DP read message type",
> +               err |= control(prnt, "DP read message type",
>                                is_965 ? gen4_dp_read_port_msg_type :
>                                         g45_dp_read_port_msg_type,
>                                brw_inst_dp_read_msg_type(devinfo, inst),
>                                &space);
>  
> -               format(file, " MsgCtrl = 0x%"PRIx64,
> +               format(prnt, " MsgCtrl = 0x%"PRIx64,
>                        brw_inst_dp_read_msg_control(devinfo, inst));
>  
> -               format(file, " Surface = %"PRIu64, brw_inst_binding_table_index(devinfo, inst));
> +               format(prnt, " Surface = %"PRIu64, brw_inst_binding_table_index(devinfo, inst));
>              }
>              break;
>  
> @@ -1664,7 +1681,7 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>              /* aka BRW_SFID_DATAPORT_WRITE on Gen4-5 */
>              unsigned msg_type = brw_inst_dp_write_msg_type(devinfo, inst);
>  
> -            err |= control(file, "DP rc message type",
> +            err |= control(prnt, "DP rc message type",
>                             dp_rc_msg_type(devinfo), msg_type, &space);
>  
>              bool is_rt_write = msg_type ==
> @@ -1672,58 +1689,58 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>                                    : BRW_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE);
>  
>              if (is_rt_write) {
> -               err |= control(file, "RT message type", m_rt_write_subtype,
> +               err |= control(prnt, "RT message type", m_rt_write_subtype,
>                                brw_inst_rt_message_type(devinfo, inst), &space);
>                 if (devinfo->gen >= 6 && brw_inst_rt_slot_group(devinfo, inst))
> -                  string(file, " Hi");
> +                  string(prnt, " Hi");
>                 if (brw_inst_rt_last(devinfo, inst))
> -                  string(file, " LastRT");
> +                  string(prnt, " LastRT");
>                 if (devinfo->gen < 7 && brw_inst_dp_write_commit(devinfo, inst))
> -                  string(file, " WriteCommit");
> +                  string(prnt, " WriteCommit");
>              } else {
> -               format(file, " MsgCtrl = 0x%"PRIx64,
> +               format(prnt, " MsgCtrl = 0x%"PRIx64,
>                        brw_inst_dp_write_msg_control(devinfo, inst));
>              }
>  
> -            format(file, " Surface = %"PRIu64, brw_inst_binding_table_index(devinfo, inst));
> +            format(prnt, " Surface = %"PRIu64, brw_inst_binding_table_index(devinfo, inst));
>              break;
>           }
>  
>           case BRW_SFID_URB: {
>              unsigned opcode = brw_inst_urb_opcode(devinfo, inst);
>  
> -            format(file, " %"PRIu64, brw_inst_urb_global_offset(devinfo, inst));
> +            format(prnt, " %"PRIu64, brw_inst_urb_global_offset(devinfo, inst));
>  
>              space = 1;
>  
> -            err |= control(file, "urb opcode",
> +            err |= control(prnt, "urb opcode",
>                             devinfo->gen >= 7 ? gen7_urb_opcode
>                                               : gen5_urb_opcode,
>                             opcode, &space);
>  
>              if (devinfo->gen >= 7 &&
>                  brw_inst_urb_per_slot_offset(devinfo, inst)) {
> -               string(file, " per-slot");
> +               string(prnt, " per-slot");
>              }
>  
>              if (opcode == GEN8_URB_OPCODE_SIMD8_WRITE ||
>                  opcode == GEN8_URB_OPCODE_SIMD8_READ) {
>                 if (brw_inst_urb_channel_mask_present(devinfo, inst))
> -                  string(file, " masked");
> +                  string(prnt, " masked");
>              } else {
> -               err |= control(file, "urb swizzle", urb_swizzle,
> +               err |= control(prnt, "urb swizzle", urb_swizzle,
>                                brw_inst_urb_swizzle_control(devinfo, inst),
>                                &space);
>              }
>  
>              if (devinfo->gen < 7) {
> -               err |= control(file, "urb allocate", urb_allocate,
> +               err |= control(prnt, "urb allocate", urb_allocate,
>                                brw_inst_urb_allocate(devinfo, inst), &space);
> -               err |= control(file, "urb used", urb_used,
> +               err |= control(prnt, "urb used", urb_used,
>                                brw_inst_urb_used(devinfo, inst), &space);
>              }
>              if (devinfo->gen < 8) {
> -               err |= control(file, "urb complete", urb_complete,
> +               err |= control(prnt, "urb complete", urb_complete,
>                                brw_inst_urb_complete(devinfo, inst), &space);
>              }
>              break;
> @@ -1732,70 +1749,70 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>              break;
>  
>           case BRW_SFID_MESSAGE_GATEWAY:
> -            format(file, " (%s)",
> +            format(prnt, " (%s)",
>                     gen7_gateway_subfuncid[brw_inst_gateway_subfuncid(devinfo, inst)]);
>              break;
>  
>           case GEN7_SFID_DATAPORT_DATA_CACHE:
>              if (devinfo->gen >= 7) {
> -               format(file, " (");
> +               format(prnt, " (");
>  
> -               err |= control(file, "DP DC0 message type",
> +               err |= control(prnt, "DP DC0 message type",
>                                dp_dc0_msg_type_gen7,
>                                brw_inst_dp_msg_type(devinfo, inst), &space);
>  
> -               format(file, ", %"PRIu64", ", brw_inst_binding_table_index(devinfo, inst));
> +               format(prnt, ", %"PRIu64", ", brw_inst_binding_table_index(devinfo, inst));
>  
>                 switch (brw_inst_dp_msg_type(devinfo, inst)) {
>                 case GEN7_DATAPORT_DC_UNTYPED_ATOMIC_OP:
> -                  control(file, "atomic op", aop,
> +                  control(prnt, "atomic op", aop,
>                            brw_inst_imm_ud(devinfo, inst) >> 8 & 0xf, &space);
>                    break;
>                 default:
> -                  format(file, "%"PRIu64, brw_inst_dp_msg_control(devinfo, inst));
> +                  format(prnt, "%"PRIu64, brw_inst_dp_msg_control(devinfo, inst));
>                 }
> -               format(file, ")");
> +               format(prnt, ")");
>                 break;
>              }
>              /* FALLTHROUGH */
>  
>           case HSW_SFID_DATAPORT_DATA_CACHE_1: {
>              if (devinfo->gen >= 7) {
> -               format(file, " (");
> +               format(prnt, " (");
>  
>                 unsigned msg_ctrl = brw_inst_dp_msg_control(devinfo, inst);
>  
> -               err |= control(file, "DP DC1 message type",
> +               err |= control(prnt, "DP DC1 message type",
>                                dp_dc1_msg_type_hsw,
>                                brw_inst_dp_msg_type(devinfo, inst), &space);
>  
> -               format(file, ", Surface = %"PRIu64", ",
> +               format(prnt, ", Surface = %"PRIu64", ",
>                        brw_inst_binding_table_index(devinfo, inst));
>  
>                 switch (brw_inst_dp_msg_type(devinfo, inst)) {
>                 case HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP:
>                 case HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP:
>                 case HSW_DATAPORT_DC_PORT1_ATOMIC_COUNTER_OP:
> -                  format(file, "SIMD%d,", (msg_ctrl & (1 << 4)) ? 8 : 16);
> +                  format(prnt, "SIMD%d,", (msg_ctrl & (1 << 4)) ? 8 : 16);
>                    /* fallthrough */
>                 case HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP_SIMD4X2:
>                 case HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP_SIMD4X2:
>                 case HSW_DATAPORT_DC_PORT1_ATOMIC_COUNTER_OP_SIMD4X2:
> -                  control(file, "atomic op", aop, msg_ctrl & 0xf, &space);
> +                  control(prnt, "atomic op", aop, msg_ctrl & 0xf, &space);
>                    break;
>                 case HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_READ:
>                 case HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_WRITE:
>                 case HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_READ:
>                 case HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_WRITE: {
>                    static const char *simd_modes[] = { "4x2", "16", "8" };
> -                  format(file, "SIMD%s, Mask = 0x%x",
> +                  format(prnt, "SIMD%s, Mask = 0x%x",
>                           simd_modes[msg_ctrl >> 4], msg_ctrl & 0xf);
>                    break;
>                 }
>                 default:
> -                  format(file, "0x%x", msg_ctrl);
> +                  format(prnt, "0x%x", msg_ctrl);
>                 }
> -               format(file, ")");
> +               format(prnt, ")");
>                 break;
>              }
>              /* FALLTHROUGH */
> @@ -1803,7 +1820,7 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>  
>           case GEN7_SFID_PIXEL_INTERPOLATOR:
>              if (devinfo->gen >= 7) {
> -               format(file, " (%s, %s, 0x%02"PRIx64")",
> +               format(prnt, " (%s, %s, 0x%02"PRIx64")",
>                        brw_inst_pi_nopersp(devinfo, inst) ? "linear" : "persp",
>                        pixel_interpolator_msg_types[brw_inst_pi_message_type(devinfo, inst)],
>                        brw_inst_pi_message_data(devinfo, inst));
> @@ -1812,65 +1829,105 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
>              /* FALLTHROUGH */
>  
>           default:
> -            format(file, "unsupported shared function ID %d", sfid);
> +            format(prnt, "unsupported shared function ID %d", sfid);
>              break;
>           }
>  
>           if (space)
> -            string(file, " ");
> -         format(file, "mlen %"PRIu64, brw_inst_mlen(devinfo, inst));
> -         format(file, " rlen %"PRIu64, brw_inst_rlen(devinfo, inst));
> +            string(prnt, " ");
> +         format(prnt, "mlen %"PRIu64, brw_inst_mlen(devinfo, inst));
> +         format(prnt, " rlen %"PRIu64, brw_inst_rlen(devinfo, inst));
>        }
>     }
> -   pad(file, 64);
> +   pad(prnt, 64);
>     if (opcode != BRW_OPCODE_NOP && opcode != BRW_OPCODE_NENOP) {
> -      string(file, "{");
> +      string(prnt, "{");
>        space = 1;
> -      err |= control(file, "access mode", access_mode,
> +      err |= control(prnt, "access mode", access_mode,
>                       brw_inst_access_mode(devinfo, inst), &space);
>        if (devinfo->gen >= 6) {
> -         err |= control(file, "write enable control", wectrl,
> +         err |= control(prnt, "write enable control", wectrl,
>                          brw_inst_mask_control(devinfo, inst), &space);
>        } else {
> -         err |= control(file, "mask control", mask_ctrl,
> +         err |= control(prnt, "mask control", mask_ctrl,
>                          brw_inst_mask_control(devinfo, inst), &space);
>        }
> -      err |= control(file, "dependency control", dep_ctrl,
> +      err |= control(prnt, "dependency control", dep_ctrl,
>                       ((brw_inst_no_dd_check(devinfo, inst) << 1) |
>                        brw_inst_no_dd_clear(devinfo, inst)), &space);
>  
>        if (devinfo->gen >= 6)
> -         err |= qtr_ctrl(file, devinfo, inst);
> +         err |= qtr_ctrl(prnt, devinfo, inst);
>        else {
>           if (brw_inst_qtr_control(devinfo, inst) == BRW_COMPRESSION_COMPRESSED &&
>               desc && desc->ndst > 0 &&
>               brw_inst_dst_reg_file(devinfo, inst) == BRW_MESSAGE_REGISTER_FILE &&
>               brw_inst_dst_da_reg_nr(devinfo, inst) & BRW_MRF_COMPR4) {
> -            format(file, " compr4");
> +            format(prnt, " compr4");
>           } else {
> -            err |= control(file, "compression control", compr_ctrl,
> +            err |= control(prnt, "compression control", compr_ctrl,
>                             brw_inst_qtr_control(devinfo, inst), &space);
>           }
>        }
>  
> -      err |= control(file, "compaction", cmpt_ctrl, is_compacted, &space);
> -      err |= control(file, "thread control", thread_ctrl,
> +      err |= control(prnt, "compaction", cmpt_ctrl, is_compacted, &space);
> +      err |= control(prnt, "thread control", thread_ctrl,
>                       brw_inst_thread_control(devinfo, inst), &space);
>        if (has_branch_ctrl(devinfo, opcode)) {
> -         err |= control(file, "branch ctrl", branch_ctrl,
> +         err |= control(prnt, "branch ctrl", branch_ctrl,
>                          brw_inst_branch_control(devinfo, inst), &space);
>        } else if (devinfo->gen >= 6) {
> -         err |= control(file, "acc write control", accwr,
> +         err |= control(prnt, "acc write control", accwr,
>                          brw_inst_acc_wr_control(devinfo, inst), &space);
>        }
>        if (opcode == BRW_OPCODE_SEND || opcode == BRW_OPCODE_SENDC)
> -         err |= control(file, "end of thread", end_of_thread,
> +         err |= control(prnt, "end of thread", end_of_thread,
>                          brw_inst_eot(devinfo, inst), &space);
>        if (space)
> -         string(file, " ");
> -      string(file, "}");
> +         string(prnt, " ");
> +      string(prnt, "}");
>     }
> -   string(file, ";");
> -   newline(file);
> +   string(prnt, ";");
> +   newline(prnt);
>     return err;
>  }
> +
> +void
> +brw_disassemble(const struct gen_device_info *devinfo,
> +                const void *assembly, int start, int end,
> +                const struct brw_print *prnt)
> +{
> +   bool dump_hex = (INTEL_DEBUG & DEBUG_HEX) != 0;
> +
> +   for (int offset = start; offset < end;) {
> +      const brw_inst *insn = assembly + offset;
> +      brw_inst uncompacted;
> +      bool compacted = brw_inst_cmpt_control(devinfo, insn);
> +      if (0)
> +         format(prnt, "0x%08x: ", offset);
> +
> +      if (compacted) {
> +         brw_compact_inst *compacted = (void *)insn;
> +         if (dump_hex) {
> +            format(prnt, "0x%08x 0x%08x                       ",
> +                   ((uint32_t *)insn)[1],
> +                   ((uint32_t *)insn)[0]);
> +         }
> +
> +         brw_uncompact_instruction(devinfo, &uncompacted, compacted);
> +         insn = &uncompacted;
> +         offset += 8;
> +      } else {
> +         if (dump_hex) {
> +            format(prnt, "0x%08x 0x%08x 0x%08x 0x%08x ",
> +                   ((uint32_t *)insn)[3],
> +                   ((uint32_t *)insn)[2],
> +                   ((uint32_t *)insn)[1],
> +                   ((uint32_t *)insn)[0]);
> +         }
> +         offset += 16;
> +      }
> +
> +      brw_disassemble_inst(prnt, devinfo, insn, compacted);
> +   }
> +}
> diff --git a/src/intel/compiler/brw_eu.c b/src/intel/compiler/brw_eu.c
> index b0bdc38f4b5..f580e8ff239 100644
> --- a/src/intel/compiler/brw_eu.c
> +++ b/src/intel/compiler/brw_eu.c
> @@ -335,45 +335,6 @@ const unsigned *brw_get_program( struct brw_codegen *p,
>     return (const unsigned *)p->store;
>  }
>  
> -void
> -brw_disassemble(const struct gen_device_info *devinfo,
> -                const void *assembly, int start, int end, FILE *out)
> -{
> -   bool dump_hex = (INTEL_DEBUG & DEBUG_HEX) != 0;
> -
> -   for (int offset = start; offset < end;) {
> -      const brw_inst *insn = assembly + offset;
> -      brw_inst uncompacted;
> -      bool compacted = brw_inst_cmpt_control(devinfo, insn);
> -      if (0)
> -         fprintf(out, "0x%08x: ", offset);
> -
> -      if (compacted) {
> -         brw_compact_inst *compacted = (void *)insn;
> -	 if (dump_hex) {
> -	    fprintf(out, "0x%08x 0x%08x                       ",
> -		    ((uint32_t *)insn)[1],
> -		    ((uint32_t *)insn)[0]);
> -	 }
> -
> -	 brw_uncompact_instruction(devinfo, &uncompacted, compacted);
> -	 insn = &uncompacted;
> -	 offset += 8;
> -      } else {
> -	 if (dump_hex) {
> -	    fprintf(out, "0x%08x 0x%08x 0x%08x 0x%08x ",
> -		    ((uint32_t *)insn)[3],
> -		    ((uint32_t *)insn)[2],
> -		    ((uint32_t *)insn)[1],
> -		    ((uint32_t *)insn)[0]);
> -	 }
> -	 offset += 16;
> -      }
> -
> -      brw_disassemble_inst(out, devinfo, insn, compacted);
> -   }
> -}
> -
>  enum gen {
>     GEN4  = (1 << 0),
>     GEN45 = (1 << 1),
> diff --git a/src/intel/compiler/brw_eu.h b/src/intel/compiler/brw_eu.h
> index d8c9be2e544..94cb25887ac 100644
> --- a/src/intel/compiler/brw_eu.h
> +++ b/src/intel/compiler/brw_eu.h
> @@ -51,6 +51,13 @@ extern "C" {
>   */
>  #define brw_last_inst (&p->store[p->nr_insn - 1])
>  
> +typedef void (*brw_print_cb)(const char *string, void *handle);
> +
> +struct brw_print {
> +   void *handle;
> +   brw_print_cb string;
> +};
> +
>  struct brw_codegen {
>     brw_inst *store;
>     int store_size;
> @@ -114,10 +121,12 @@ void brw_set_default_acc_write_control(struct brw_codegen *p, unsigned value);
>  
>  void brw_init_codegen(const struct gen_device_info *, struct brw_codegen *p,
>  		      void *mem_ctx);
> -int brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
> +int brw_disassemble_inst(const struct brw_print *prnt,
> +                         const struct gen_device_info *devinfo,
>                           const struct brw_inst *inst, bool is_compacted);
>  void brw_disassemble(const struct gen_device_info *devinfo,
> -                     const void *assembly, int start, int end, FILE *out);
> +                     const void *assembly, int start, int end,
> +                     const struct brw_print *prnt);
>  const unsigned *brw_get_program( struct brw_codegen *p, unsigned *sz );
>  
>  brw_inst *brw_next_insn(struct brw_codegen *p, unsigned opcode);
> diff --git a/src/intel/compiler/brw_eu_compact.c b/src/intel/compiler/brw_eu_compact.c
> index a9da46957a5..8c4a4014e56 100644
> --- a/src/intel/compiler/brw_eu_compact.c
> +++ b/src/intel/compiler/brw_eu_compact.c
> @@ -1348,14 +1348,19 @@ void brw_debug_compact_uncompact(const struct gen_device_info *devinfo,
>                                   brw_inst *orig,
>                                   brw_inst *uncompacted)
>  {
> +   struct brw_print prnt = {
> +      .handle = stderr,
> +      .string = (brw_print_cb) fputs,
> +   };
> +
>     fprintf(stderr, "Instruction compact/uncompact changed (gen%d):\n",
>             devinfo->gen);
>  
>     fprintf(stderr, "  before: ");
> -   brw_disassemble_inst(stderr, devinfo, orig, true);
> +   brw_disassemble_inst(&prnt, devinfo, orig, true);
>  
>     fprintf(stderr, "  after:  ");
> -   brw_disassemble_inst(stderr, devinfo, uncompacted, false);
> +   brw_disassemble_inst(&prnt, devinfo, uncompacted, false);
>  
>     uint32_t *before_bits = (uint32_t *)orig;
>     uint32_t *after_bits = (uint32_t *)uncompacted;
> diff --git a/src/intel/compiler/intel_asm_annotation.c b/src/intel/compiler/intel_asm_annotation.c
> index b07a545a12e..e34cc369f0f 100644
> --- a/src/intel/compiler/intel_asm_annotation.c
> +++ b/src/intel/compiler/intel_asm_annotation.c
> @@ -65,7 +65,10 @@ dump_assembly(void *assembly, int num_annotations, struct annotation *annotation
>              fprintf(stderr, "   %s\n", last_annotation_string);
>        }
>  
> -      brw_disassemble(devinfo, assembly, start_offset, end_offset, stderr);
> +      brw_disassemble(devinfo, assembly, start_offset, end_offset,
> +                      &(struct brw_print){
> +                         .handle = stderr,
> +                         .string = (brw_print_cb) fputs, });
>  
>        if (annotation[i].error) {
>           fputs(annotation[i].error, stderr);
> diff --git a/src/intel/compiler/test_eu_compact.cpp b/src/intel/compiler/test_eu_compact.cpp
> index 1532e3b9840..427dd8e1cc9 100644
> --- a/src/intel/compiler/test_eu_compact.cpp
> +++ b/src/intel/compiler/test_eu_compact.cpp
> @@ -48,7 +48,12 @@ test_compact_instruction(struct brw_codegen *p, brw_inst src)
>        if (memcmp(&unchanged, &dst, sizeof(dst))) {
>  	 fprintf(stderr, "Failed to compact, but dst changed\n");
>  	 fprintf(stderr, "  Instruction: ");
> -	 brw_disassemble_inst(stderr, p->devinfo, &src, false);
> +
> +         struct brw_print prnt = {
> +            .handle = strderr,
> +            .string = fputs,
> +         };
> +         brw_disassemble_inst(&prnt, p->devinfo, &src, false);
>  	 return false;
>        }
>     }
> diff --git a/src/intel/tools/aubinator.c b/src/intel/tools/aubinator.c
> index 2c4eaab1701..436a4928979 100644
> --- a/src/intel/tools/aubinator.c
> +++ b/src/intel/tools/aubinator.c
> @@ -277,7 +277,7 @@ handle_media_interface_descriptor_load(struct gen_spec *spec, uint32_t *p)
>        }
>  
>        insns = (struct brw_instruction *) (gtt + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>  
>        dump_samplers(spec, descriptors[3] & ~0x1f);
>        dump_binding_table(spec, descriptors[4] & ~0x1f);
> @@ -375,7 +375,7 @@ handle_3dstate_vs(struct gen_spec *spec, uint32_t *p)
>                instruction_base, start);
>  
>        insns = (struct brw_instruction *) (gtt + instruction_base + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>     }
>  }
>  
> @@ -399,7 +399,7 @@ handle_3dstate_hs(struct gen_spec *spec, uint32_t *p)
>                instruction_base, start);
>  
>        insns = (struct brw_instruction *) (gtt + instruction_base + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>     }
>  }
>  
> @@ -493,21 +493,21 @@ handle_3dstate_ps(struct gen_spec *spec, uint32_t *p)
>     fprintf(outfile, "  Kernel[0] %s\n", k0);
>     if (k0 != unused) {
>        insns = (struct brw_instruction *) (gtt + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>     }
>  
>     start = instruction_base + (p[k1_offset] & mask);
>     fprintf(outfile, "  Kernel[1] %s\n", k1);
>     if (k1 != unused) {
>        insns = (struct brw_instruction *) (gtt + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>     }
>  
>     start = instruction_base + (p[k2_offset] & mask);
>     fprintf(outfile, "  Kernel[2] %s\n", k2);
>     if (k2 != unused) {
>        insns = (struct brw_instruction *) (gtt + start);
> -      gen_disasm_disassemble(disasm, insns, 0, stdout);
> +      gen_disasm_disassemble_print(disasm, insns, 0, stdout);
>     }
>  }
>  
> diff --git a/src/intel/tools/disasm.c b/src/intel/tools/disasm.c
> index 251acd313dc..e2f5c11f6f5 100644
> --- a/src/intel/tools/disasm.c
> +++ b/src/intel/tools/disasm.c
> @@ -69,12 +69,71 @@ gen_disasm_find_end(struct gen_disasm *disasm, void *assembly, int start)
>     return offset;
>  }
>  
> +static void
> +append_string(const char *string, void *data)
> +{
> +   char **old = data;
> +   size_t len = strlen(*old) + strlen(string) + 1;
> +   char *new = malloc(len);
> +   new[0] = '\0';
> +   strcat(new, *old);
> +   strcat(new, string);
> +   *old = new;
> +}
> +
>  void
>  gen_disasm_disassemble(struct gen_disasm *disasm, void *assembly,
> -                       int start, FILE *out)
> +                       int start, char **ret)
>  {
>     struct gen_device_info *devinfo = &disasm->devinfo;
>     int end = gen_disasm_find_end(disasm, assembly, start);
> +   struct brw_print prnt = {
> +      .handle = ret,
> +      .string = append_string,
> +   };
> +
> +   *ret = strdup("");
> +
> +   /* Make a dummy annotation structure that brw_validate_instructions
> +    * can work from.
> +    */
> +   struct annotation_info annotation_info = {
> +      .ann_count = 1,
> +      .ann_size = 2,
> +   };
> +   annotation_info.mem_ctx = ralloc_context(NULL);
> +   annotation_info.ann = rzalloc_array(annotation_info.mem_ctx,
> +                                       struct annotation,
> +                                       annotation_info.ann_size);
> +   annotation_info.ann[0].offset = start;
> +   annotation_info.ann[1].offset = end;
> +   brw_validate_instructions(devinfo, assembly, start, end, &annotation_info);
> +   struct annotation *annotation = annotation_info.ann;
> +
> +   for (int i = 0; i < annotation_info.ann_count; i++) {
> +      int start_offset = annotation[i].offset;
> +      int end_offset = annotation[i + 1].offset;
> +
> +      brw_disassemble(devinfo, assembly, start_offset, end_offset, &prnt);
> +
> +      if (annotation[i].error) {
> +         append_string(annotation[i].error, ret);
> +      }
> +   }
> +
> +   ralloc_free(annotation_info.mem_ctx);
> +}
> +
> +void
> +gen_disasm_disassemble_print(struct gen_disasm *disasm, void *assembly,
> +                             int start, FILE *out)
> +{
> +   struct gen_device_info *devinfo = &disasm->devinfo;
> +   int end = gen_disasm_find_end(disasm, assembly, start);
> +   struct brw_print prnt = {
> +      .handle = out,
> +      .string = (brw_print_cb) fputs,
> +   };
>  
>     /* Make a dummy annotation structure that brw_validate_instructions
>      * can work from.
> @@ -96,7 +155,7 @@ gen_disasm_disassemble(struct gen_disasm *disasm, void *assembly,
>        int start_offset = annotation[i].offset;
>        int end_offset = annotation[i + 1].offset;
>  
> -      brw_disassemble(devinfo, assembly, start_offset, end_offset, out);
> +      brw_disassemble(devinfo, assembly, start_offset, end_offset, &prnt);
>  
>        if (annotation[i].error) {
>           fputs(annotation[i].error, out);
> diff --git a/src/intel/tools/gen_disasm.h b/src/intel/tools/gen_disasm.h
> index d2764bb90b7..281c2dc9cff 100644
> --- a/src/intel/tools/gen_disasm.h
> +++ b/src/intel/tools/gen_disasm.h
> @@ -32,8 +32,11 @@ struct gen_disasm;
>  
>  struct gen_disasm *gen_disasm_create(int pciid);
>  void gen_disasm_disassemble(struct gen_disasm *disasm,
> -                            void *assembly, int start, FILE *out);
> -
> +                            void *assembly, int start,
> +                            char **ret);
> +void gen_disasm_disassemble_print(struct gen_disasm *disasm,
> +                                  void *assembly,
> +                                  int start, FILE *out);
>  void gen_disasm_destroy(struct gen_disasm *disasm);
>  
>  #ifdef __cplusplus
> -- 
> 2.15.0.rc2
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list