i965/vs: Abort on unsupported opcodes rather than failing.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_emit.cpp
index 27160fb40d4913f3f074dc22a6944b0199b887f9..a2a5975c6b22320fcb8d124fb72a72d5891541bb 100644 (file)
  */
 
 #include "brw_vec4.h"
-#include "../glsl/ir_print_visitor.h"
 
 extern "C" {
 #include "brw_eu.h"
+#include "main/macros.h"
+#include "program/prog_print.h"
+#include "program/prog_parameter.h"
 };
 
-using namespace brw;
-
 namespace brw {
 
-int
-vec4_visitor::setup_attributes(int payload_reg)
-{
-   int nr_attributes;
-   int attribute_map[VERT_ATTRIB_MAX];
-
-   nr_attributes = 0;
-   for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
-      if (prog_data->inputs_read & BITFIELD64_BIT(i)) {
-        attribute_map[i] = payload_reg + nr_attributes;
-        nr_attributes++;
-
-        /* Do GL_FIXED rescaling for GLES2.0.  Our GL_FIXED
-         * attributes come in as floating point conversions of the
-         * integer values.
-         */
-        if (c->key.gl_fixed_input_size[i] != 0) {
-           struct brw_reg reg = brw_vec8_grf(attribute_map[i], 0);
-
-           brw_MUL(p,
-                   brw_writemask(reg, (1 << c->key.gl_fixed_input_size[i]) - 1),
-                   reg, brw_imm_f(1.0 / 65536.0));
-        }
-      }
-   }
-
-   foreach_iter(exec_list_iterator, iter, this->instructions) {
-      vec4_instruction *inst = (vec4_instruction *)iter.get();
-
-      for (int i = 0; i < 3; i++) {
-        if (inst->src[i].file != ATTR)
-           continue;
-
-        int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
-        inst->src[i].file = HW_REG;
-        inst->src[i].fixed_hw_reg = brw_vec8_grf(grf, 0);
-        inst->src[i].fixed_hw_reg.dw1.bits.swizzle = inst->src[i].swizzle;
-      }
-   }
-
-   /* The BSpec says we always have to read at least one thing from
-    * the VF, and it appears that the hardware wedges otherwise.
-    */
-   if (nr_attributes == 0)
-      nr_attributes = 1;
-
-   prog_data->urb_read_length = (nr_attributes + 1) / 2;
-
-   return payload_reg + nr_attributes;
-}
-
-int
-vec4_visitor::setup_uniforms(int reg)
-{
-   /* User clip planes from curbe:
-    */
-   if (c->key.nr_userclip) {
-      if (intel->gen >= 6) {
-        for (int i = 0; i < c->key.nr_userclip; i++) {
-           c->userplane[i] = stride(brw_vec4_grf(reg + i / 2,
-                                                 (i % 2) * 4), 0, 4, 1);
-        }
-        reg += ALIGN(c->key.nr_userclip, 2) / 2;
-      } else {
-        for (int i = 0; i < c->key.nr_userclip; i++) {
-           c->userplane[i] = stride(brw_vec4_grf(reg + (6 + i) / 2,
-                                                 (i % 2) * 4), 0, 4, 1);
-        }
-        reg += (ALIGN(6 + c->key.nr_userclip, 4) / 4) * 2;
-      }
-   }
-
-   /* The pre-gen6 VS requires that some push constants get loaded no
-    * matter what, or the GPU would hang.
-    */
-   if (intel->gen < 6 && this->uniforms == 0) {
-      this->uniform_size[this->uniforms] = 1;
-
-      for (unsigned int i = 0; i < 4; i++) {
-        unsigned int slot = this->uniforms * 4 + i;
-
-        c->prog_data.param[slot] = NULL;
-        c->prog_data.param_convert[slot] = PARAM_CONVERT_ZERO;
-      }
-
-      this->uniforms++;
-      reg++;
-   } else {
-      reg += ALIGN(uniforms, 2) / 2;
-   }
-
-   /* for now, we are not doing any elimination of unused slots, nor
-    * are we packing our uniforms.
-    */
-   c->prog_data.nr_params = this->uniforms * 4;
-
-   c->prog_data.curb_read_length = reg - 1;
-   c->prog_data.uses_new_param_layout = true;
-
-   return reg;
-}
-
-void
-vec4_visitor::setup_payload(void)
-{
-   int reg = 0;
-
-   /* The payload always contains important data in g0, which contains
-    * the URB handles that are passed on to the URB write at the end
-    * of the thread.  So, we always start push constants at g1.
-    */
-   reg++;
-
-   reg = setup_uniforms(reg);
-
-   reg = setup_attributes(reg);
-
-   this->first_non_payload_grf = reg;
-}
-
 struct brw_reg
 vec4_instruction::get_dst(void)
 {
@@ -163,6 +43,12 @@ vec4_instruction::get_dst(void)
       brw_reg.dw1.bits.writemask = dst.writemask;
       break;
 
+   case MRF:
+      brw_reg = brw_message_reg(dst.reg + dst.reg_offset);
+      brw_reg = retype(brw_reg, dst.type);
+      brw_reg.dw1.bits.writemask = dst.writemask;
+      break;
+
    case HW_REG:
       brw_reg = dst.fixed_hw_reg;
       break;
@@ -223,6 +109,9 @@ vec4_instruction::get_src(int i)
         brw_reg = brw_abs(brw_reg);
       if (src[i].negate)
         brw_reg = negate(brw_reg);
+
+      /* This should have been moved to pull constants. */
+      assert(!src[i].reladdr);
       break;
 
    case HW_REG:
@@ -251,10 +140,9 @@ vec4_visitor::generate_math1_gen4(vec4_instruction *inst,
    brw_math(p,
            dst,
            brw_math_function(inst->opcode),
-           BRW_MATH_SATURATE_NONE,
            inst->base_mrf,
            src,
-           BRW_MATH_DATA_SCALAR,
+           BRW_MATH_DATA_VECTOR,
            BRW_MATH_PRECISION_FULL);
 }
 
@@ -264,7 +152,7 @@ check_gen6_math_src_arg(struct brw_reg src)
    /* Source swizzles are ignored. */
    assert(!src.abs);
    assert(!src.negate);
-   assert(src.dw1.bits.swizzle = BRW_SWIZZLE_XYZW);
+   assert(src.dw1.bits.swizzle == BRW_SWIZZLE_XYZW);
 }
 
 void
@@ -280,7 +168,6 @@ vec4_visitor::generate_math1_gen6(vec4_instruction *inst,
    brw_math(p,
            dst,
            brw_math_function(inst->opcode),
-           BRW_MATH_SATURATE_NONE,
            inst->base_mrf,
            src,
            BRW_MATH_DATA_SCALAR,
@@ -288,6 +175,18 @@ vec4_visitor::generate_math1_gen6(vec4_instruction *inst,
    brw_set_access_mode(p, BRW_ALIGN_16);
 }
 
+void
+vec4_visitor::generate_math2_gen7(vec4_instruction *inst,
+                                 struct brw_reg dst,
+                                 struct brw_reg src0,
+                                 struct brw_reg src1)
+{
+   brw_math2(p,
+            dst,
+            brw_math_function(inst->opcode),
+            src0, src1);
+}
+
 void
 vec4_visitor::generate_math2_gen6(vec4_instruction *inst,
                                  struct brw_reg dst,
@@ -314,21 +213,146 @@ vec4_visitor::generate_math2_gen4(vec4_instruction *inst,
                                  struct brw_reg src0,
                                  struct brw_reg src1)
 {
-   /* Can't do writemask because math can't be align16. */
-   assert(dst.dw1.bits.writemask == WRITEMASK_XYZW);
+   /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
+    * "Message Payload":
+    *
+    * "Operand0[7].  For the INT DIV functions, this operand is the
+    *  denominator."
+    *  ...
+    * "Operand1[7].  For the INT DIV functions, this operand is the
+    *  numerator."
+    */
+   bool is_int_div = inst->opcode != SHADER_OPCODE_POW;
+   struct brw_reg &op0 = is_int_div ? src1 : src0;
+   struct brw_reg &op1 = is_int_div ? src0 : src1;
 
-   brw_MOV(p, brw_message_reg(inst->base_mrf + 1), src1);
+   brw_push_insn_state(p);
+   brw_set_saturate(p, false);
+   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
+   brw_MOV(p, retype(brw_message_reg(inst->base_mrf + 1), op1.type), op1);
+   brw_pop_insn_state(p);
 
-   brw_set_access_mode(p, BRW_ALIGN_1);
    brw_math(p,
            dst,
            brw_math_function(inst->opcode),
-           BRW_MATH_SATURATE_NONE,
            inst->base_mrf,
-           src0,
+           op0,
            BRW_MATH_DATA_VECTOR,
            BRW_MATH_PRECISION_FULL);
-   brw_set_access_mode(p, BRW_ALIGN_16);
+}
+
+void
+vec4_visitor::generate_tex(vec4_instruction *inst,
+                          struct brw_reg dst,
+                          struct brw_reg src)
+{
+   int msg_type = -1;
+
+   if (intel->gen >= 5) {
+      switch (inst->opcode) {
+      case SHADER_OPCODE_TEX:
+      case SHADER_OPCODE_TXL:
+        if (inst->shadow_compare) {
+           msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
+        } else {
+           msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
+        }
+        break;
+      case SHADER_OPCODE_TXD:
+        /* There is no sample_d_c message; comparisons are done manually. */
+        msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
+        break;
+      case SHADER_OPCODE_TXF:
+        msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
+        break;
+      case SHADER_OPCODE_TXS:
+        msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
+        break;
+      default:
+        assert(!"should not get here: invalid VS texture opcode");
+        break;
+      }
+   } else {
+      switch (inst->opcode) {
+      case SHADER_OPCODE_TEX:
+      case SHADER_OPCODE_TXL:
+        if (inst->shadow_compare) {
+           msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_LOD_COMPARE;
+           assert(inst->mlen == 3);
+        } else {
+           msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_LOD;
+           assert(inst->mlen == 2);
+        }
+        break;
+      case SHADER_OPCODE_TXD:
+        /* There is no sample_d_c message; comparisons are done manually. */
+        msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_SAMPLE_GRADIENTS;
+        assert(inst->mlen == 4);
+        break;
+      case SHADER_OPCODE_TXF:
+        msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_LD;
+        assert(inst->mlen == 2);
+        break;
+      case SHADER_OPCODE_TXS:
+        msg_type = BRW_SAMPLER_MESSAGE_SIMD4X2_RESINFO;
+        assert(inst->mlen == 2);
+        break;
+      default:
+        assert(!"should not get here: invalid VS texture opcode");
+        break;
+      }
+   }
+
+   assert(msg_type != -1);
+
+   /* Load the message header if present.  If there's a texture offset, we need
+    * to set it up explicitly and load the offset bitfield.  Otherwise, we can
+    * use an implied move from g0 to the first message register.
+    */
+   if (inst->texture_offset) {
+      /* Explicitly set up the message header by copying g0 to the MRF. */
+      brw_MOV(p, retype(brw_message_reg(inst->base_mrf), BRW_REGISTER_TYPE_UD),
+                retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
+
+      /* Then set the offset bits in DWord 2. */
+      brw_set_access_mode(p, BRW_ALIGN_1);
+      brw_MOV(p,
+             retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, inst->base_mrf, 2),
+                    BRW_REGISTER_TYPE_UD),
+             brw_imm_uw(inst->texture_offset));
+      brw_set_access_mode(p, BRW_ALIGN_16);
+   } else if (inst->header_present) {
+      /* Set up an implied move from g0 to the MRF. */
+      src = brw_vec8_grf(0, 0);
+   }
+
+   uint32_t return_format;
+
+   switch (dst.type) {
+   case BRW_REGISTER_TYPE_D:
+      return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;
+      break;
+   case BRW_REGISTER_TYPE_UD:
+      return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
+      break;
+   default:
+      return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
+      break;
+   }
+
+   brw_SAMPLE(p,
+             dst,
+             inst->base_mrf,
+             src,
+             SURF_INDEX_VS_TEXTURE(inst->sampler),
+             inst->sampler,
+             WRITEMASK_XYZW,
+             msg_type,
+             1, /* response length */
+             inst->mlen,
+             inst->header_present,
+             BRW_SAMPLER_SIMD_MODE_SIMD4X2,
+             return_format);
 }
 
 void
@@ -375,9 +399,8 @@ vec4_visitor::generate_oword_dual_block_offsets(struct brw_reg m1,
 
    brw_MOV(p, m1_0, index_0);
 
-   brw_set_predicate_inverse(p, true);
    if (index.file == BRW_IMMEDIATE_VALUE) {
-      index_4.dw1.ud++;
+      index_4.dw1.ud += second_vertex_offset;
       brw_MOV(p, m1_4, index_4);
    } else {
       brw_ADD(p, m1_4, index_4, brw_imm_d(second_vertex_offset));
@@ -391,14 +414,9 @@ vec4_visitor::generate_scratch_read(vec4_instruction *inst,
                                    struct brw_reg dst,
                                    struct brw_reg index)
 {
-   if (intel->gen >= 6) {
-      brw_push_insn_state(p);
-      brw_set_mask_control(p, BRW_MASK_DISABLE);
-      brw_MOV(p,
-             retype(brw_message_reg(inst->base_mrf), BRW_REGISTER_TYPE_D),
-             retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_D));
-      brw_pop_insn_state(p);
-   }
+   struct brw_reg header = brw_vec8_grf(0, 0);
+
+   gen6_resolve_implied_move(p, &header, inst->base_mrf);
 
    generate_oword_dual_block_offsets(brw_message_reg(inst->base_mrf + 1),
                                     index);
@@ -417,7 +435,9 @@ vec4_visitor::generate_scratch_read(vec4_instruction *inst,
     */
    struct brw_instruction *send = brw_next_insn(p, BRW_OPCODE_SEND);
    brw_set_dest(p, send, dst);
-   brw_set_src0(p, send, brw_message_reg(inst->base_mrf));
+   brw_set_src0(p, send, header);
+   if (intel->gen < 6)
+      send->header.destreg__conditionalmod = inst->base_mrf;
    brw_set_dp_read_message(p, send,
                           255, /* binding table index: stateless access */
                           BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
@@ -433,19 +453,15 @@ vec4_visitor::generate_scratch_write(vec4_instruction *inst,
                                     struct brw_reg src,
                                     struct brw_reg index)
 {
+   struct brw_reg header = brw_vec8_grf(0, 0);
+   bool write_commit;
+
    /* If the instruction is predicated, we'll predicate the send, not
     * the header setup.
     */
    brw_set_predicate_control(p, false);
 
-   if (intel->gen >= 6) {
-      brw_push_insn_state(p);
-      brw_set_mask_control(p, BRW_MASK_DISABLE);
-      brw_MOV(p,
-             retype(brw_message_reg(inst->base_mrf), BRW_REGISTER_TYPE_D),
-             retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_D));
-      brw_pop_insn_state(p);
-   }
+   gen6_resolve_implied_move(p, &header, inst->base_mrf);
 
    generate_oword_dual_block_offsets(brw_message_reg(inst->base_mrf + 1),
                                     index);
@@ -456,29 +472,112 @@ vec4_visitor::generate_scratch_write(vec4_instruction *inst,
 
    uint32_t msg_type;
 
-   if (intel->gen >= 6)
+   if (intel->gen >= 7)
+      msg_type = GEN7_DATAPORT_WRITE_MESSAGE_OWORD_DUAL_BLOCK_WRITE;
+   else if (intel->gen == 6)
       msg_type = GEN6_DATAPORT_WRITE_MESSAGE_OWORD_DUAL_BLOCK_WRITE;
    else
       msg_type = BRW_DATAPORT_WRITE_MESSAGE_OWORD_DUAL_BLOCK_WRITE;
 
    brw_set_predicate_control(p, inst->predicate);
 
+   /* Pre-gen6, we have to specify write commits to ensure ordering
+    * between reads and writes within a thread.  Afterwards, that's
+    * guaranteed and write commits only matter for inter-thread
+    * synchronization.
+    */
+   if (intel->gen >= 6) {
+      write_commit = false;
+   } else {
+      /* The visitor set up our destination register to be g0.  This
+       * means that when the next read comes along, we will end up
+       * reading from g0 and causing a block on the write commit.  For
+       * write-after-read, we are relying on the value of the previous
+       * read being used (and thus blocking on completion) before our
+       * write is executed.  This means we have to be careful in
+       * instruction scheduling to not violate this assumption.
+       */
+      write_commit = true;
+   }
+
    /* Each of the 8 channel enables is considered for whether each
     * dword is written.
     */
    struct brw_instruction *send = brw_next_insn(p, BRW_OPCODE_SEND);
    brw_set_dest(p, send, dst);
-   brw_set_src0(p, send, brw_message_reg(inst->base_mrf));
+   brw_set_src0(p, send, header);
+   if (intel->gen < 6)
+      send->header.destreg__conditionalmod = inst->base_mrf;
    brw_set_dp_write_message(p, send,
                            255, /* binding table index: stateless access */
                            BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
                            msg_type,
                            3, /* mlen */
                            true, /* header present */
-                           false, /* pixel scoreboard */
-                           0, /* rlen */
+                           false, /* not a render target write */
+                           write_commit, /* rlen */
                            false, /* eot */
-                           false /* commit */);
+                           write_commit);
+}
+
+void
+vec4_visitor::generate_pull_constant_load(vec4_instruction *inst,
+                                         struct brw_reg dst,
+                                         struct brw_reg index,
+                                         struct brw_reg offset)
+{
+   assert(index.file == BRW_IMMEDIATE_VALUE &&
+         index.type == BRW_REGISTER_TYPE_UD);
+   uint32_t surf_index = index.dw1.ud;
+
+   if (intel->gen == 7) {
+      gen6_resolve_implied_move(p, &offset, inst->base_mrf);
+      brw_instruction *insn = brw_next_insn(p, BRW_OPCODE_SEND);
+      brw_set_dest(p, insn, dst);
+      brw_set_src0(p, insn, offset);
+      brw_set_sampler_message(p, insn,
+                              surf_index,
+                              0, /* LD message ignores sampler unit */
+                              GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
+                              1, /* rlen */
+                              1, /* mlen */
+                              false, /* no header */
+                              BRW_SAMPLER_SIMD_MODE_SIMD4X2,
+                              0);
+      return;
+   }
+
+   struct brw_reg header = brw_vec8_grf(0, 0);
+
+   gen6_resolve_implied_move(p, &header, inst->base_mrf);
+
+   brw_MOV(p, retype(brw_message_reg(inst->base_mrf + 1), BRW_REGISTER_TYPE_D),
+          offset);
+
+   uint32_t msg_type;
+
+   if (intel->gen >= 6)
+      msg_type = GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
+   else if (intel->gen == 5 || intel->is_g4x)
+      msg_type = G45_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
+   else
+      msg_type = BRW_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
+
+   /* Each of the 8 channel enables is considered for whether each
+    * dword is written.
+    */
+   struct brw_instruction *send = brw_next_insn(p, BRW_OPCODE_SEND);
+   brw_set_dest(p, send, dst);
+   brw_set_src0(p, send, header);
+   if (intel->gen < 6)
+      send->header.destreg__conditionalmod = inst->base_mrf;
+   brw_set_dp_read_message(p, send,
+                          surf_index,
+                          BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
+                          msg_type,
+                          BRW_DATAPORT_READ_TARGET_DATA_CACHE,
+                          2, /* mlen */
+                          1 /* rlen */);
 }
 
 void
@@ -496,21 +595,34 @@ vec4_visitor::generate_vs_instruction(vec4_instruction *instruction,
    case SHADER_OPCODE_LOG2:
    case SHADER_OPCODE_SIN:
    case SHADER_OPCODE_COS:
-      if (intel->gen >= 6) {
+      if (intel->gen == 6) {
         generate_math1_gen6(inst, dst, src[0]);
       } else {
+        /* Also works for Gen7. */
         generate_math1_gen4(inst, dst, src[0]);
       }
       break;
 
    case SHADER_OPCODE_POW:
-      if (intel->gen >= 6) {
+   case SHADER_OPCODE_INT_QUOTIENT:
+   case SHADER_OPCODE_INT_REMAINDER:
+      if (intel->gen >= 7) {
+        generate_math2_gen7(inst, dst, src[0], src[1]);
+      } else if (intel->gen == 6) {
         generate_math2_gen6(inst, dst, src[0], src[1]);
       } else {
         generate_math2_gen4(inst, dst, src[0], src[1]);
       }
       break;
 
+   case SHADER_OPCODE_TEX:
+   case SHADER_OPCODE_TXD:
+   case SHADER_OPCODE_TXF:
+   case SHADER_OPCODE_TXL:
+   case SHADER_OPCODE_TXS:
+      generate_tex(inst, dst, src[0]);
+      break;
+
    case VS_OPCODE_URB_WRITE:
       generate_urb_write(inst);
       break;
@@ -523,71 +635,34 @@ vec4_visitor::generate_vs_instruction(vec4_instruction *instruction,
       generate_scratch_write(inst, dst, src[0], src[1]);
       break;
 
+   case VS_OPCODE_PULL_CONSTANT_LOAD:
+      generate_pull_constant_load(inst, dst, src[0], src[1]);
+      break;
+
    default:
-      if (inst->opcode < (int)ARRAY_SIZE(brw_opcodes)) {
-        fail("unsupported opcode in `%s' in VS\n",
-             brw_opcodes[inst->opcode].name);
+      if (inst->opcode < (int) ARRAY_SIZE(opcode_descs)) {
+         _mesa_problem(ctx, "Unsupported opcode in `%s' in VS\n",
+                       opcode_descs[inst->opcode].name);
       } else {
-        fail("Unsupported opcode %d in VS", inst->opcode);
+         _mesa_problem(ctx, "Unsupported opcode %d in VS", inst->opcode);
       }
+      abort();
    }
 }
 
-bool
-vec4_visitor::run()
-{
-   /* Generate VS IR for main().  (the visitor only descends into
-    * functions called "main").
-    */
-   foreach_iter(exec_list_iterator, iter, *shader->ir) {
-      ir_instruction *ir = (ir_instruction *)iter.get();
-      base_ir = ir;
-      ir->accept(this);
-   }
-
-   emit_urb_writes();
-
-   /* Before any optimization, push array accesses out to scratch
-    * space where we need them to be.  This pass may allocate new
-    * virtual GRFs, so we want to do it early.  It also makes sure
-    * that we have reladdr computations available for CSE, since we'll
-    * often do repeated subexpressions for those.
-    */
-   move_grf_array_access_to_scratch();
-
-   if (failed)
-      return false;
-
-   setup_payload();
-   reg_allocate();
-
-   if (failed)
-      return false;
-
-   brw_set_access_mode(p, BRW_ALIGN_16);
-
-   generate_code();
-
-   return !failed;
-}
-
 void
 vec4_visitor::generate_code()
 {
-   int last_native_inst = p->nr_insn;
+   int last_native_insn_offset = 0;
    const char *last_annotation_string = NULL;
-   ir_instruction *last_annotation_ir = NULL;
-
-   int loop_stack_array_size = 16;
-   int loop_stack_depth = 0;
-   brw_instruction **loop_stack =
-      rzalloc_array(this->mem_ctx, brw_instruction *, loop_stack_array_size);
-   int *if_depth_in_loop =
-      rzalloc_array(this->mem_ctx, int, loop_stack_array_size);
-
+   const void *last_annotation_ir = NULL;
 
    if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
-      printf("Native code for vertex shader %d:\n", prog->Name);
+      if (shader) {
+         printf("Native code for vertex shader %d:\n", prog->Name);
+      } else {
+         printf("Native code for vertex program %d:\n", c->vp->program.Base.Id);
+      }
    }
 
    foreach_list(node, &this->instructions) {
@@ -599,7 +674,15 @@ vec4_visitor::generate_code()
            last_annotation_ir = inst->ir;
            if (last_annotation_ir) {
               printf("   ");
-              last_annotation_ir->print();
+               if (shader) {
+                  ((ir_instruction *) last_annotation_ir)->print();
+               } else {
+                  const prog_instruction *vpi;
+                  vpi = (const prog_instruction *) inst->ir;
+                  printf("%d: ", (int)(vpi - vp->Base.Instructions));
+                  _mesa_fprint_instruction_opt(stdout, vpi, 0,
+                                               PROG_PRINT_DEBUG, NULL);
+               }
               printf("\n");
            }
         }
@@ -630,6 +713,11 @@ vec4_visitor::generate_code()
       case BRW_OPCODE_MUL:
         brw_MUL(p, dst, src[0], src[1]);
         break;
+      case BRW_OPCODE_MACH:
+        brw_set_acc_write_control(p, 1);
+        brw_MACH(p, dst, src[0], src[1]);
+        brw_set_acc_write_control(p, 0);
+        break;
 
       case BRW_OPCODE_FRC:
         brw_FRC(p, dst, src[0]);
@@ -673,6 +761,10 @@ vec4_visitor::generate_code()
         brw_SEL(p, dst, src[0], src[1]);
         break;
 
+      case BRW_OPCODE_DPH:
+        brw_DPH(p, dst, src[0], src[1]);
+        break;
+
       case BRW_OPCODE_DP4:
         brw_DP4(p, dst, src[0], src[1]);
         break;
@@ -694,7 +786,6 @@ vec4_visitor::generate_code()
            struct brw_instruction *brw_inst = brw_IF(p, BRW_EXECUTE_8);
            brw_inst->header.predicate_control = inst->predicate;
         }
-        if_depth_in_loop[loop_stack_depth]++;
         break;
 
       case BRW_OPCODE_ELSE:
@@ -702,59 +793,27 @@ vec4_visitor::generate_code()
         break;
       case BRW_OPCODE_ENDIF:
         brw_ENDIF(p);
-        if_depth_in_loop[loop_stack_depth]--;
         break;
 
       case BRW_OPCODE_DO:
-        loop_stack[loop_stack_depth++] = brw_DO(p, BRW_EXECUTE_8);
-        if (loop_stack_array_size <= loop_stack_depth) {
-           loop_stack_array_size *= 2;
-           loop_stack = reralloc(this->mem_ctx, loop_stack, brw_instruction *,
-                                 loop_stack_array_size);
-           if_depth_in_loop = reralloc(this->mem_ctx, if_depth_in_loop, int,
-                                       loop_stack_array_size);
-        }
-        if_depth_in_loop[loop_stack_depth] = 0;
+        brw_DO(p, BRW_EXECUTE_8);
         break;
 
       case BRW_OPCODE_BREAK:
-        brw_BREAK(p, if_depth_in_loop[loop_stack_depth]);
+        brw_BREAK(p);
         brw_set_predicate_control(p, BRW_PREDICATE_NONE);
         break;
       case BRW_OPCODE_CONTINUE:
         /* FINISHME: We need to write the loop instruction support still. */
         if (intel->gen >= 6)
-           gen6_CONT(p, loop_stack[loop_stack_depth - 1]);
+           gen6_CONT(p);
         else
-           brw_CONT(p, if_depth_in_loop[loop_stack_depth]);
+           brw_CONT(p);
         brw_set_predicate_control(p, BRW_PREDICATE_NONE);
         break;
 
-      case BRW_OPCODE_WHILE: {
-        struct brw_instruction *inst0, *inst1;
-        GLuint br = 1;
-
-        if (intel->gen >= 5)
-           br = 2;
-
-        assert(loop_stack_depth > 0);
-        loop_stack_depth--;
-        inst0 = inst1 = brw_WHILE(p, loop_stack[loop_stack_depth]);
-        if (intel->gen < 6) {
-           /* patch all the BREAK/CONT instructions from last BGNLOOP */
-           while (inst0 > loop_stack[loop_stack_depth]) {
-              inst0--;
-              if (inst0->header.opcode == BRW_OPCODE_BREAK &&
-                  inst0->bits3.if_else.jump_count == 0) {
-                 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
-           }
-              else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
-                       inst0->bits3.if_else.jump_count == 0) {
-                 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
-              }
-           }
-        }
-      }
+      case BRW_OPCODE_WHILE:
+        brw_WHILE(p);
         break;
 
       default:
@@ -763,28 +822,17 @@ vec4_visitor::generate_code()
       }
 
       if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
-        for (unsigned int i = last_native_inst; i < p->nr_insn; i++) {
-           if (0) {
-              printf("0x%08x 0x%08x 0x%08x 0x%08x ",
-                     ((uint32_t *)&p->store[i])[3],
-                     ((uint32_t *)&p->store[i])[2],
-                     ((uint32_t *)&p->store[i])[1],
-                     ((uint32_t *)&p->store[i])[0]);
-           }
-           brw_disasm(stdout, &p->store[i], intel->gen);
-        }
+        brw_dump_compile(p, stdout,
+                         last_native_insn_offset, p->next_insn_offset);
       }
 
-      last_native_inst = p->nr_insn;
+      last_native_insn_offset = p->next_insn_offset;
    }
 
    if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
       printf("\n");
    }
 
-   ralloc_free(loop_stack);
-   ralloc_free(if_depth_in_loop);
-
    brw_set_uip_jip(p);
 
    /* OK, while the INTEL_DEBUG=vs above is very nice for debugging VS
@@ -792,55 +840,9 @@ vec4_visitor::generate_code()
     * which is often something we want to debug.  So this is here in
     * case you're doing that.
     */
-   if (0) {
-      if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
-        for (unsigned int i = 0; i < p->nr_insn; i++) {
-           printf("0x%08x 0x%08x 0x%08x 0x%08x ",
-                  ((uint32_t *)&p->store[i])[3],
-                  ((uint32_t *)&p->store[i])[2],
-                  ((uint32_t *)&p->store[i])[1],
-                  ((uint32_t *)&p->store[i])[0]);
-           brw_disasm(stdout, &p->store[i], intel->gen);
-        }
-      }
-   }
-}
-
-extern "C" {
-
-bool
-brw_vs_emit(struct brw_vs_compile *c)
-{
-   struct brw_compile *p = &c->func;
-   struct brw_context *brw = p->brw;
-   struct intel_context *intel = &brw->intel;
-   struct gl_context *ctx = &intel->ctx;
-   struct gl_shader_program *prog = ctx->Shader.CurrentVertexProgram;
-
-   if (!prog)
-      return false;
-
-   struct brw_shader *shader =
-     (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
-   if (!shader)
-      return false;
-
-   if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
-      printf("GLSL IR for native vertex shader %d:\n", prog->Name);
-      _mesa_print_ir(shader->ir, NULL);
-      printf("\n\n");
-   }
-
-   vec4_visitor v(c, prog, shader);
-   if (!v.run()) {
-      /* FINISHME: Cleanly fail, test at link time, etc. */
-      assert(!"not reached");
-      return false;
+   if (0 && unlikely(INTEL_DEBUG & DEBUG_VS)) {
+      brw_dump_compile(p, stdout, 0, p->next_insn_offset);
    }
-
-   return true;
 }
 
-} /* extern "C" */
-
 } /* namespace brw */