summaryrefslogtreecommitdiff
path: root/orc/orcprogram-mips.c
blob: 73d09da2115441e17919ddc9e5fc1c31066d5c27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include <orc/orcmips.h>
#include <orc/orcdebug.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"

unsigned int orc_compiler_orc_mips_get_default_flags (void);

void orc_compiler_orc_mips_init (OrcCompiler *compiler);

void orc_compiler_orc_mips_assemble (OrcCompiler *compiler);

const char * orc_compiler_orc_mips_get_asm_preamble (void);

void orc_mips_flush_cache (OrcCode *code);

/* in orcrules-mips.c */
void orc_compiler_orc_mips_register_rules (OrcTarget *target);

static OrcTarget orc_mips_target = {
  "mips",
#ifdef HAVE_MIPSEL
  TRUE,
#else
  FALSE,
#endif
  ORC_GP_REG_BASE,
  orc_compiler_orc_mips_get_default_flags,
  orc_compiler_orc_mips_init,
  orc_compiler_orc_mips_assemble,
  { { 0 } },
  0,
  orc_compiler_orc_mips_get_asm_preamble,
  NULL,
  NULL,
  orc_mips_flush_cache,
};

enum {
  LABEL_REGION0_LOOP = 1,
  LABEL_REGION1,
  LABEL_REGION1_LOOP,
  LABEL_REGION2,
  LABEL_REGION2_LOOP, /* 5 */
  LABEL_REGION2_LOOP_END,
  LABEL_OUTER_LOOP,
  LABEL_END
};
#define LAST_LABEL LABEL_END

void
orc_mips_init (void)
{
  orc_target_register (&orc_mips_target);

  orc_compiler_orc_mips_register_rules (&orc_mips_target);
}

unsigned int
orc_compiler_orc_mips_get_default_flags (void)
{
  unsigned int flags = 0;

  if (_orc_compiler_flag_debug) {
    flags |= ORC_TARGET_MIPS_FRAME_POINTER;
  }
  return flags;
}

void
orc_compiler_orc_mips_init (OrcCompiler *compiler)
{
  int i;

  if (compiler->target_flags & ORC_TARGET_MIPS_FRAME_POINTER)
    compiler->use_frame_pointer = TRUE;

  for (i=ORC_GP_REG_BASE; i<ORC_GP_REG_BASE+32; i++)
    compiler->valid_regs[i] = 1;

  compiler->valid_regs[ORC_MIPS_ZERO] = 0; /* always 0 */
  compiler->valid_regs[ORC_MIPS_AT] = 0; /* we shouldn't touch that (assembler
                                            temporary) */
  compiler->exec_reg = ORC_MIPS_A0;
  compiler->valid_regs[ORC_MIPS_A0] = 0; /* first (and in our case only)
                                            function argument */
  compiler->valid_regs[ORC_MIPS_T0] = 0; /* $t0, $t1 and $t2 are used as loop */
  compiler->valid_regs[ORC_MIPS_T1] = 0; /* counters */
  compiler->valid_regs[ORC_MIPS_T2] = 0;
  compiler->valid_regs[ORC_MIPS_T3] = 0;
  compiler->valid_regs[ORC_MIPS_T4] = 0;
  compiler->valid_regs[ORC_MIPS_T5] = 0;
  compiler->valid_regs[ORC_MIPS_K0] = 0; /* for kernel/interupts */
  compiler->valid_regs[ORC_MIPS_K1] = 0; /* for kernel/interupts */
  compiler->valid_regs[ORC_MIPS_GP] = 0; /* global pointer */
  compiler->valid_regs[ORC_MIPS_SP] = 0; /* stack pointer */
  compiler->valid_regs[ORC_MIPS_FP] = 0; /* frame pointer */
  compiler->valid_regs[ORC_MIPS_RA] = 0; /* return address */

  for (i=0;i<ORC_N_REGS;i++){
    compiler->alloc_regs[i] = 0;
    compiler->used_regs[i] = 0;
    compiler->save_regs[i] = 0;
  }

  compiler->save_regs[ORC_MIPS_V0] = 1;
  compiler->save_regs[ORC_MIPS_V1] = 1;
  for (i=ORC_MIPS_S0; i<= ORC_MIPS_S7; i++)
    compiler->save_regs[i] = 1;

  switch (compiler->max_var_size) {
  case 1:
    compiler->loop_shift = 2;
    break;
  case 2:
    compiler->loop_shift = 1;
    break;
  case 4:
    compiler->loop_shift = 0;
    break;
  default:
    ORC_ERROR("unhandled variable size %d", compiler->max_var_size);
  }

  for(i=0;i<compiler->n_insns;i++){
    OrcInstruction *insn = compiler->insns + i;
    OrcStaticOpcode *opcode = insn->opcode;

    if ((strcmp (opcode->name, "loadupib") == 0)
        || (strcmp (opcode->name, "loadupdb") == 0)) {
      compiler->vars[insn->src_args[0]].need_offset_reg = TRUE;
    }
  }
}

const char *
orc_compiler_orc_mips_get_asm_preamble (void)
{
  return "\n"
      "/* begin Orc MIPS target preamble */\n"
      ".abicalls\n" /* not exactly sure what this is, but linker complains
                       without it  */
      ".set noreorder\n"
      "/* end Orc MIPS target preamble */\n\n";
}

int
orc_mips_emit_prologue (OrcCompiler *compiler)
{
  int i, stack_size;
  unsigned int stack_increment;

  if (compiler->use_frame_pointer) {
    stack_size = 12; /* we stack at least fp and a0 and start at stack_increment 4 */
    stack_increment = 4;
  } else {
    stack_size = 0;
    stack_increment = 0;
  }

  orc_compiler_append_code(compiler,".globl %s\n", compiler->program->name);
  orc_compiler_append_code(compiler,"%s:\n", compiler->program->name);

  /* push registers we need to save */
  for(i=0; i<32; i++)
    if (compiler->used_regs[ORC_GP_REG_BASE + i] &&
        compiler->save_regs[ORC_GP_REG_BASE + i])
      stack_size += 4;

  if (stack_size) {
    orc_mips_emit_addiu (compiler, ORC_MIPS_SP, ORC_MIPS_SP, -stack_size);

    if (compiler->use_frame_pointer) {
      orc_mips_emit_sw (compiler, ORC_MIPS_FP, ORC_MIPS_SP, stack_increment);
      stack_increment += 4;
      orc_mips_emit_move (compiler, ORC_MIPS_FP, ORC_MIPS_SP);
      orc_mips_emit_sw (compiler, ORC_MIPS_A0, ORC_MIPS_SP, stack_increment);
      stack_increment += 4;
    }


    for(i=0; i<32; i++){
      if (compiler->used_regs[ORC_GP_REG_BASE + i] &&
          compiler->save_regs[ORC_GP_REG_BASE + i]) {
        orc_mips_emit_sw (compiler, ORC_GP_REG_BASE+i,
                          ORC_MIPS_SP, stack_increment);
            stack_increment +=4;
      }
    }
  }

  return stack_size;
}

void orc_mips_emit_epilogue (OrcCompiler *compiler, int stack_size)
{
  int i;

  /* pop saved registers */
  if (stack_size) {
    unsigned int stack_increment = 0;
    if (compiler->use_frame_pointer)
      stack_increment = 8;

    for(i=0; i<32; i++){
      if (compiler->used_regs[ORC_GP_REG_BASE + i] &&
          compiler->save_regs[ORC_GP_REG_BASE + i]) {
        orc_mips_emit_lw (compiler, ORC_GP_REG_BASE+i,
                          ORC_MIPS_SP, stack_increment);
            stack_increment +=4;
      }
    }
    if (compiler->use_frame_pointer)
      orc_mips_emit_lw (compiler, ORC_MIPS_FP, ORC_MIPS_SP, 4);
    orc_mips_emit_addiu (compiler, ORC_MIPS_SP, ORC_MIPS_SP, stack_size);
  }

  orc_mips_emit_jr (compiler, ORC_MIPS_RA);
  orc_mips_emit_nop (compiler);
  if (compiler->target_flags & ORC_TARGET_CLEAN_COMPILE) {
    /* we emit some padding nops at the end to align to 16 bytes because that's
     * what gnu as does (not sure why) and we want to generate the same code
     * for testing purposes */
    orc_mips_emit_align (compiler, 4);
  }
}

void
orc_mips_load_constants_inner (OrcCompiler *compiler)
{
  int i;
  for(i=0;i<ORC_N_COMPILER_VARIABLES;i++){
    OrcVariable *var = compiler->vars + i;
    if (var->name == NULL) continue;
    switch (var->vartype) {
      case ORC_VAR_TYPE_CONST:
      case ORC_VAR_TYPE_PARAM:
        break;
      case ORC_VAR_TYPE_SRC:
      case ORC_VAR_TYPE_DEST:
        orc_mips_emit_lw (compiler,
            var->ptr_register,
            compiler->exec_reg, ORC_MIPS_EXECUTOR_OFFSET_ARRAYS(i));
        break;
      default:
        break;
    }

    if (var->ptr_offset)
      orc_mips_emit_move (compiler, var->ptr_offset, ORC_MIPS_ZERO);
  }


  for(i=0;i<compiler->n_insns;i++){
    OrcInstruction *insn = compiler->insns + i;
    OrcStaticOpcode *opcode = insn->opcode;
    OrcRule *rule;

    if (!(insn->flags & ORC_INSN_FLAG_INVARIANT)) continue;

    ORC_ASM_CODE(compiler,"# %d: %s\n", i, insn->opcode->name);

    compiler->insn_shift = compiler->loop_shift;
    if (insn->flags & ORC_INSTRUCTION_FLAG_X2) {
      compiler->insn_shift += 1;
    }
    if (insn->flags & ORC_INSTRUCTION_FLAG_X4) {
      compiler->insn_shift += 2;
    }

    rule = insn->rule;
    if (rule && rule->emit) {
      rule->emit (compiler, rule->emit_user, insn);
    } else {
      ORC_COMPILER_ERROR(compiler,"No rule for: %s", opcode->name);
    }
  }
}

void
orc_mips_emit_loop (OrcCompiler *compiler)
{
  int i, j;
  OrcInstruction *insn;
  OrcStaticOpcode *opcode;
  OrcRule *rule;
  ORC_DEBUG ("loop_shift=%d", compiler->loop_shift);

  for (i=0; i<compiler->n_insns; i++) {
    insn = compiler->insns + i;
    opcode = insn->opcode;
    if (insn->flags & ORC_INSN_FLAG_INVARIANT) continue;

    orc_compiler_append_code(compiler,"/* %d: %s */\n", i, insn->opcode->name);

    compiler->min_temp_reg = ORC_MIPS_T3;

    rule = insn->rule;
    if (rule && rule->emit) {
      compiler->insn_shift = compiler->loop_shift;
      if (insn->flags & ORC_INSTRUCTION_FLAG_X2) {
        compiler->insn_shift += 1;
      }
      if (insn->flags & ORC_INSTRUCTION_FLAG_X4) {
        compiler->insn_shift += 2;
      }
      rule->emit (compiler, rule->emit_user, insn);
    } else {
      orc_compiler_append_code (compiler, "No rule for %s\n", opcode->name);
    }
  }

  for (j=0; j<ORC_N_COMPILER_VARIABLES; j++) {
    OrcVariable *var = compiler->vars + j;

    if (var->name == NULL) continue;
    if (var->vartype == ORC_VAR_TYPE_SRC ||
        var->vartype == ORC_VAR_TYPE_DEST) {
      int offset;
      if (var->update_type == 0) {
        offset = 0;
      } else if (var->update_type == 1) {
        offset = (var->size << compiler->loop_shift) >> 1;
      } else {
        offset = var->size << compiler->loop_shift;
      }
      if (offset !=0 && var->ptr_register) {
        orc_mips_emit_addiu (compiler,
                             var->ptr_register,
                             var->ptr_register,
                             offset);
      }
    }
  }
}

static int
get_align_var (OrcCompiler *compiler)
{
  if (compiler->vars[ORC_VAR_D1].size) return ORC_VAR_D1;
  if (compiler->vars[ORC_VAR_S1].size) return ORC_VAR_S1;

  ORC_PROGRAM_ERROR(compiler, "could not find alignment variable");

  return -1;
}

static int
get_shift (int size)
{
  switch (size) {
    case 1:
      return 0;
    case 2:
      return 1;
    case 4:
      return 2;
    default:
      ORC_ERROR("bad size %d", size);
  }
  return -1;
}

/* alignment is a bit field. Each bit (from least significant) corresponds to a
 * dest or source variable in the order
 * ORC_VAR_D1-ORC_VAR_D4,ORC_VAR_S1-ORC_VAR_S8
 */
void
orc_mips_set_alignment (OrcCompiler *compiler, orc_uint16 alignment)
{
  int i;
  for (i=ORC_VAR_D1; i<=ORC_VAR_S8; i++) {
    compiler->vars[i].is_aligned = !!(alignment & (1<<i));
  }
}

orc_uint16
orc_mips_get_alignment (OrcCompiler *compiler)
{
  int i;
  orc_uint16 alignment=0;
  for (i=ORC_VAR_D1; i<=ORC_VAR_S8; i++) {
    if (compiler->vars[i].is_aligned)
      alignment |= 1<<i;
  }
  return alignment;
}

void
orc_mips_emit_full_loop (OrcCompiler *compiler, OrcMipsRegister counter,
                         int loop_shift, int loop_label, int alignment)
{
  int saved_loop_shift;
  int saved_alignment;
  orc_mips_emit_label (compiler, loop_label);
  saved_loop_shift = compiler->loop_shift;
  compiler->loop_shift = loop_shift;
  saved_alignment = orc_mips_get_alignment (compiler);
  orc_mips_set_alignment (compiler, alignment);
  orc_mips_emit_loop (compiler);
  orc_mips_set_alignment (compiler, saved_alignment);
  compiler->loop_shift = saved_loop_shift;
  orc_mips_emit_addi (compiler, counter, counter, -1);
  orc_mips_emit_bnez (compiler, counter, loop_label);
  orc_mips_emit_nop (compiler);
}

/* FIXME: this stuff should be cached */
int
orc_mips_get_loop_label (OrcCompiler *compiler, int alignments)
{
  int i,
      j=0,
      bitfield=0;
  for (i=ORC_VAR_D1; i<=ORC_VAR_S8; i++) {
    OrcVariable *var = &(compiler->vars[i]);
    if (var->name == NULL || var->ptr_register == 0 || var->is_aligned) {
      if (alignments & (1 << i))
        return -1;
      else
        continue;
    }

    if (alignments & (1 << i)) {
      bitfield |= 1 << j;
    }
    j++;
  }
  if (bitfield)
    return LAST_LABEL + bitfield;

  return -1;
}

/* overwrites $t0 and $t1 */
void
orc_mips_add_strides (OrcCompiler *compiler, int var_size_shift)
{
  int i;
  orc_mips_emit_lw (compiler, ORC_MIPS_T1, compiler->exec_reg,
                    ORC_MIPS_EXECUTOR_OFFSET_N);
  orc_mips_emit_sll (compiler, ORC_MIPS_T1, ORC_MIPS_T1, var_size_shift);
  /* $t1 now contains the number of bytes that we treated (and that the var
   * pointer registers advanced) */
  for(i=0;i<ORC_N_COMPILER_VARIABLES;i++){
    if (compiler->vars[i].name == NULL) continue;
    switch (compiler->vars[i].vartype) {
      case ORC_VAR_TYPE_CONST:
        break;
      case ORC_VAR_TYPE_PARAM:
        break;
      case ORC_VAR_TYPE_SRC:
      case ORC_VAR_TYPE_DEST:
        /* get the stride (it's in bytes) */
        orc_mips_emit_lw (compiler, ORC_MIPS_T0, compiler->exec_reg,
                          ORC_MIPS_EXECUTOR_OFFSET_PARAMS(i));
        /* $t0 = stride - bytes advanced
           we add that to the pointer so that it points to the beginning of the
           next stride */
        orc_mips_emit_sub (compiler, ORC_MIPS_T0, ORC_MIPS_T0, ORC_MIPS_T1);
        orc_mips_emit_addu (compiler, compiler->vars[i].ptr_register,
                            compiler->vars[i].ptr_register, ORC_MIPS_T0);
        break;
      case ORC_VAR_TYPE_ACCUMULATOR:
        break;
      case ORC_VAR_TYPE_TEMP:
        break;
      default:
        ORC_COMPILER_ERROR(compiler,"bad vartype");
        break;
    }
  }
}

void
orc_compiler_orc_mips_assemble (OrcCompiler *compiler)
{
  int stack_size;
  int align_shift = 2; /* this wouldn't work on mips64 */
  int align_var = get_align_var (compiler);
  int var_size_shift;
  int i;

  var_size_shift = get_shift (compiler->vars[align_var].size);

  stack_size = orc_mips_emit_prologue (compiler);

  /* FIXME: load constants and params */
#if 0
  for (i=0; i<ORC_N_COMPILER_VARIABLES; i++) {
    if (compiler->vars[i].name == NULL)
      ORC_PROGRAM_ERROR (compiler, "unimplemented");
  }
#endif

  orc_mips_load_constants_inner (compiler);

  if (compiler->program->is_2d) {
    /* ex->params[ORC_VAR_A1] contains "m", the number of lines we want to treat */
    orc_mips_emit_lw (compiler, ORC_MIPS_T0, compiler->exec_reg,
                      ORC_MIPS_EXECUTOR_OFFSET_PARAMS(ORC_VAR_A1));
    orc_mips_emit_beqz (compiler, ORC_MIPS_T0, LABEL_END);
    orc_mips_emit_label (compiler, LABEL_OUTER_LOOP);
  }

  orc_mips_emit_lw (compiler, ORC_MIPS_T2, compiler->exec_reg,
                    ORC_MIPS_EXECUTOR_OFFSET_N);
  orc_mips_emit_blez (compiler, ORC_MIPS_T2, LABEL_END);


  /* Note: in all these counter calculations ($t0, $t1 and $t2), we assume that
   * variables of k bytes are k-bytes aligned. */

  /* $t0 = number of iterations in region0 (before alignment) */
  /* = number of bytes to get to alignment / var_size
     = ((alignment - data_address) % alignment) / var_size
     = (((1 << align_shift) - data_address) % (1 << align_shift)) / var_size
     = (((1 << align_shift) - data_address) & ((1 << align_shfit) - 1)) >> var_size_shift
   */
  orc_mips_emit_addiu (compiler, ORC_MIPS_T0, ORC_MIPS_ZERO, 1 << align_shift);
  orc_mips_emit_sub (compiler, ORC_MIPS_T0, ORC_MIPS_T0,
                     compiler->vars[align_var].ptr_register);
  orc_mips_emit_andi (compiler, ORC_MIPS_T0, ORC_MIPS_T0,
                      (1 << align_shift) - 1);
  if (var_size_shift > 0)
    orc_mips_emit_srl (compiler, ORC_MIPS_T0, ORC_MIPS_T0, var_size_shift);

  /* $t1 = number of iterations in region1 (aligned)
         = (n - $t0) / loop_size
         = (n - $t0) >> loop_shift
   */
  orc_mips_emit_sub (compiler, ORC_MIPS_T2, ORC_MIPS_T2, ORC_MIPS_T0);

  /*
     handle the case where n < $t0. In that case, we want to handle n elements
     in region0, and no element in the two other regions.

     bgez $t2, usual_case
     move $t1, $0
     move $t2, $0
     lw   $t0, OFFSET_N($a0)
     beqz $0, LABEL_REGION0_LOOP
usual_case:
   */
  orc_mips_emit_conditional_branch_with_offset (compiler, ORC_MIPS_BGEZ,
                                                ORC_MIPS_T2, ORC_MIPS_ZERO,
                                                24);
  orc_mips_emit_nop (compiler);
  orc_mips_emit_move (compiler, ORC_MIPS_T1, ORC_MIPS_ZERO);
  orc_mips_emit_move (compiler, ORC_MIPS_T2, ORC_MIPS_ZERO);
  orc_mips_emit_lw (compiler, ORC_MIPS_T0, compiler->exec_reg,
                    ORC_MIPS_EXECUTOR_OFFSET_N);
  orc_mips_emit_beqz (compiler, ORC_MIPS_ZERO, LABEL_REGION0_LOOP);
  orc_mips_emit_nop (compiler);


  if (compiler->loop_shift> 0)
    orc_mips_emit_srl (compiler, ORC_MIPS_T1, ORC_MIPS_T2,
                       compiler->loop_shift);
  else
    orc_mips_emit_move (compiler, ORC_MIPS_T1, ORC_MIPS_T2);


  /* if ($t0 == 0) goto REGION1 */
  orc_mips_emit_beqz (compiler, ORC_MIPS_T0, LABEL_REGION1);

  /* $t2 = number of iterations in region2 (after aligned region)
         = (n - $t0) % loop_size
         = (previous $t2) % loop_size
         = $t2 & ((1 << loop_shift) - 1)
   */
     /* note that this instruction is in the branch delay slot */
  if (compiler->loop_shift > 0)
    orc_mips_emit_andi (compiler, ORC_MIPS_T2, ORC_MIPS_T2,
                        (1 << compiler->loop_shift) - 1);
  else
    /* loop_shift==0: $t2 should be 0 because we can handle all our data in region 1*/
    orc_mips_emit_move (compiler, ORC_MIPS_T2, ORC_MIPS_ZERO);

  /* FIXME: when loop_shift == 0, we only need to emit region1 */

  orc_mips_emit_full_loop (compiler, ORC_MIPS_T0, 0, LABEL_REGION0_LOOP, 0);

  orc_mips_emit_label (compiler, LABEL_REGION1);
  orc_mips_emit_beqz (compiler, ORC_MIPS_T1, LABEL_REGION2);
  /* branch delay slot is occupied either by the next andi in the loop below or
   * by the nop after that loop */

  /* from here on until LABEL_REGION2, align_var is known to be aligned (that's
   * the reason why we went through region 0) */
  compiler->vars[align_var].is_aligned = TRUE;


  /* We need a register that contains 1 so that we easily get 2^i */
  orc_mips_emit_ori (compiler, ORC_MIPS_T3, ORC_MIPS_ZERO, 1);
  /* That's where we will store the bitfield of aligned vars (apart from
   * align_var) */
  orc_mips_emit_ori (compiler, ORC_MIPS_T5, ORC_MIPS_ZERO, 0);

  for (i=ORC_VAR_D1; i<=ORC_VAR_S8; i++) {
    OrcVariable *var = &(compiler->vars[i]);
    if (var->name == NULL || var->ptr_register == 0 || var->is_aligned) continue;
    orc_mips_emit_andi (compiler, ORC_MIPS_T0, var->ptr_register, (1 << align_shift) - 1);
    orc_mips_emit_conditional_branch_with_offset (compiler, ORC_MIPS_BNE,
                                                  ORC_MIPS_T0, ORC_MIPS_ZERO,
                                                  8 /* skipping the next two instructions */);
    orc_mips_emit_sll (compiler, ORC_MIPS_T4, ORC_MIPS_T3, i);
    orc_mips_emit_or (compiler, ORC_MIPS_T5, ORC_MIPS_T5, ORC_MIPS_T4);
  }

  orc_mips_emit_beqz (compiler, ORC_MIPS_T5, LABEL_REGION1_LOOP);

  /* Loop on all the alignment combinations we can handle and compare them to
   * the actual alignment we have, then branch to the best loop for it*/
  /* Note: this assumes that ORC_VAR_D1 == 0 */
  for (i=1; i < (1 << (ORC_VAR_S8 +1)); i++) {
    int label = orc_mips_get_loop_label (compiler, i);
    if (label == -1) continue;
    if (label >= ORC_N_LABELS) { /* this check works because _get_loop_label() */
       break;                    /* is strictly monotonic and increasing */
    }

    /* This next line works to load i because ORC_VAR_S8 < 16 */
    orc_mips_emit_ori (compiler, ORC_MIPS_T0, ORC_MIPS_ZERO, i);
    orc_mips_emit_beq (compiler, ORC_MIPS_T5, ORC_MIPS_T0, label);
  }

  orc_mips_emit_nop (compiler);
  /* If we reach here, it means we haven't branched to any specific loop, so we
   * branch to the fallback one */
  orc_mips_emit_beqz (compiler, ORC_MIPS_ZERO, LABEL_REGION1_LOOP);
  orc_mips_emit_nop (compiler);

  /* Loop on all alignment combinations we can handle (limited by number of
   * labels available) and emit the loop for it */
  for (i=0; i < (1 << (ORC_VAR_S8 +1)); i++) {
    int label = orc_mips_get_loop_label (compiler, i);
    if (label == -1) continue;
    if (label >= ORC_N_LABELS) /* this check works because _get_loop_label() */
       break;                  /* is strictly monotonic and increasing */

    orc_mips_emit_full_loop (compiler, ORC_MIPS_T1, compiler->loop_shift,
                             label, i | (1 << align_var));

    /* Jump the other loop versions and go to REGION2 */
    orc_mips_emit_beqz (compiler, ORC_MIPS_ZERO, LABEL_REGION2);
    orc_mips_emit_nop (compiler);
  }


  /* Fallback loop that works for any alignment combination */
  orc_mips_emit_full_loop (compiler, ORC_MIPS_T1, compiler->loop_shift,
                           LABEL_REGION1_LOOP, 1 << align_var);


  compiler->vars[align_var].is_aligned = FALSE;

  orc_mips_emit_label (compiler, LABEL_REGION2);
  orc_mips_emit_beqz (compiler, ORC_MIPS_T2, LABEL_REGION2_LOOP_END);
  orc_mips_emit_nop (compiler);

  orc_mips_emit_full_loop (compiler, ORC_MIPS_T2, 0, LABEL_REGION2_LOOP, 0);
  orc_mips_emit_label (compiler, LABEL_REGION2_LOOP_END);

  if (compiler->program->is_2d) {

    /* ex->params[ORC_VAR_A1] contains "m", the number of lines we want to treat */
    orc_mips_emit_lw (compiler, ORC_MIPS_T2, compiler->exec_reg,
                      ORC_MIPS_EXECUTOR_OFFSET_PARAMS(ORC_VAR_A1));
    orc_mips_add_strides (compiler, var_size_shift);
    orc_mips_emit_addi (compiler, ORC_MIPS_T2, ORC_MIPS_T2, -1);
    orc_mips_emit_sw (compiler, ORC_MIPS_T2, compiler->exec_reg,
                      ORC_MIPS_EXECUTOR_OFFSET_PARAMS(ORC_VAR_A1));
    orc_mips_emit_bnez (compiler, ORC_MIPS_T2, LABEL_OUTER_LOOP);
    orc_mips_emit_nop (compiler);
  }

  orc_mips_emit_label (compiler, LABEL_END);

  orc_mips_do_fixups (compiler);

  orc_mips_emit_epilogue (compiler, stack_size);
}

void
orc_mips_flush_cache  (OrcCode *code)
{
#ifdef HAVE_MIPSEL
  __clear_cache (code->code, code->code + code->code_size);
#endif
}