cvsuser     03/02/25 02:25:45

  Modified:    docs     jit.pod
               .        jit.c
               jit/alpha jit_emit.h
               jit/arm  jit_emit.h
               jit/i386 jit_emit.h
               jit/ppc  jit_emit.h
               jit/sun4 jit_emit.h
               languages/imcc debug.c imc.c imc.h imcc.y instructions.c
                        instructions.h main.c
  Log:
  imcc-Oj: Using imcc as JIT optimizer
  
  Revision  Changes    Path
  1.12      +97 -8     parrot/docs/jit.pod
  
  Index: jit.pod
  ===================================================================
  RCS file: /cvs/public/parrot/docs/jit.pod,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -w -r1.11 -r1.12
  --- jit.pod   21 Feb 2003 13:28:40 -0000      1.11
  +++ jit.pod   25 Feb 2003 10:25:31 -0000      1.12
  @@ -81,10 +81,6 @@
   format that is pre-processed by F<jit2h.pl> to produce a valid C source file,
   F<jit_cpu.c>. See L<Format of .jit Files> below.
   
  -=item jit/${jitcpuarch}/string.jit
  -
  -The string subsystem.
  -
   =item include/parrot/jit.h
   
   This file contains definitions of generic structures used by the JIT subsystem.
  @@ -134,6 +130,100 @@
   
   =back
   
  +=head1 Defines in jit_emit.h
  +
  +The architecture specific F<jit_emit.h> file communicates some defines
  +and tables with F<jit.c> and F<languages/imcc/imc.c>. The structure of
  +the file and the defines must therefore follow a specific syntax.
  +
  +=head2 Overall structure
  +
  +     #if JIT_EMIT
  +
  +     ... emit code
  +
  +     #else
  +
  +     ... defines
  +
  +     #ifndef JIT_IMCC
  +
  +     ... initialization of maps
  +     ... and possibly private static funtions
  +
  +     #endif
  +     #endif
  +
  +=head2 Defines
  +
  +=over 4
  +
  +=item INT_REGISTERS_TO_MAP
  +
  +This is the amount of integer registers to be mapped to processor
  +registers. The corresponding B<intval_map[]> has to have exactly this
  +amount of register numbers. A register with the value of zero can not
  +be in the list.
  +
  +=item FLOAT_REGISTERS_TO_MAP
  +
  +When this is defined, it works like above for floating point
  +registers.
  +
  +=item PRESERVED_INT_REGS
  +
  +When this is defined, it's the amount of integer registers, that are
  +preserved over function calls. These preserved registers have to be first in
  +B<intval_map>. When this is not defined, it is assumed that B<all>
  +registers are preserved over function calls.
  +
  +=item PRESERVED_FLOAT_REGS
  +
  +Same for floating point registers.
  +
  +=item jit_emit_noop(pc)
  +
  +=item JUMP_ALIGN
  +
  +If these are defined, B<JUMP_ALIGN> should be a small number stating
  +the desired alignment of jump targets is B<1 << JUMP_ALIGN>.  The
  +B<jit_emit_noop> gets called with the unaligned B<pc> repeatedly,
  +until the B<pc> has the desired aignment. So the function can either
  +emit a one byte B<noop> instruction, or a B<noop> like instruction
  +(sequence) with the desired size, to achieve the necessary padding.
  +The emitted code must not have any side effects.
  +
  +=item ALLOCATE_REGISTERS_PER_SECTION
  +
  +Normally F<jit.c> does register allocation per section, but there is a
  +somewhat experimental feature, to allocate registers per basic block.
  +
  +=item MAP
  +
  +Jit code generated by the F<imcc> JIT opimizer used negative numbers
  +for mapped registers and positive numbers for non mapped parrot
  +registers. To use this feature, the definition of mapped registers can
  +be redefind like so:
  +
  +     #define MAP(i) OMAP(i)
  +     #undef MAP
  +     #define MAP(i) (i) >= 0 : 0 ? OMAP(i)
  +
  +=item EXTCALL
  +
  +This macro, if defined can override, what is to be considered an
  +external function. The default definition is:
  +
  +     #  define EXTCALL(op) (op_jit[*(op)].extcall)
  +
  +JIT/i386 has jitted vtable functions, where extcall is the vtable
  +entry number and redefines B<EXTCALL> to:
  +
  +     #  define EXTCALL(op) (op_jit[*(op)].extcall == 1)
  +
  +=back
  +
  +S. F<jit/i386/jit_emit.h> for actual usage of these defines.
   
   =head1 Format of .jit Files
   
  @@ -344,11 +434,10 @@
   
   There are four mapped integer registers B<%edi>, B<%ebx>, B<%esi> and
   B<%edx>. The first 3 of these are callee saved, they preserve their
  -value around extern function calls. The register B<%ebp> is abused to
  -hold the address of the jit function table.
  +value around extern function calls.
   
  -For floating point operations the registers B<ST1> ... B<ST4> are
  -mapped.
  +Four floating point operations the registers B<ST1> ... B<ST4> are
  +mapped and considered as preserved over function calls.
   
   
   =head1 EXAMPLE
  
  
  
  1.59      +11 -9     parrot/jit.c
  
  Index: jit.c
  ===================================================================
  RCS file: /cvs/public/parrot/jit.c,v
  retrieving revision 1.58
  retrieving revision 1.59
  diff -u -w -r1.58 -r1.59
  --- jit.c     23 Feb 2003 15:44:00 -0000      1.58
  +++ jit.c     25 Feb 2003 10:25:32 -0000      1.59
  @@ -1,7 +1,7 @@
   /*
    * jit.c
    *
  - * $Id: jit.c,v 1.58 2003/02/23 15:44:00 leo Exp $
  + * $Id: jit.c,v 1.59 2003/02/25 10:25:32 leo Exp $
    */
   
   #include <parrot/parrot.h>
  @@ -260,10 +260,11 @@
   /*
    * I386 has JITed vtables, which have the vtable# in extcall.
    * This Parrot_jit_vtable_n_op() doese use register mappings.
  + *
  + * below is the standard define
    */
  -#if defined(I386)
  -#  define EXTCALL(op) (op_jit[*(op)].extcall == 1)
  -#else
  +
  +#ifndef EXTCALL
   #  define EXTCALL(op) op_jit[*(op)].extcall
   #endif
   
  @@ -482,11 +483,8 @@
               for (i = 0; i < to_map[typ]; i++) {
                   ru[typ].reg_usage[i] = i;
                   /* set rn1, N1 */
  -                if (ru[typ].registers_used == 1 &&
  -                        ru[typ].reg_usage[i] ==
  -                        (PARROT_ARGDIR_IN|PARROT_ARGDIR_OUT))
  -                    ru[typ].registers_used = 2;
               }
  +            ru[typ].registers_used = to_map[typ];
   #endif /* JIT_IMCC_OJ */
           }
           next = cur_section->next;
  @@ -520,6 +518,9 @@
       int i, op_arg, typ;
       opcode_t * cur_op;
       char * maps[] = {0, 0, 0, 0};
  +#ifdef JIT_IMCC_OJ
  +    int to_map[] = { INT_REGISTERS_TO_MAP, 0, 0, FLOAT_REGISTERS_TO_MAP };
  +#endif
       maps[0] = intval_map;
       maps[3] = floatval_map;
   
  @@ -542,11 +543,12 @@
                   if (!maps[typ])
                       continue;
                   /* If the argument is in most used list for this typ */
  -                for (i = 0; i < cur_section->ru[typ].registers_used; i++)
   #ifndef JIT_IMCC_OJ
  +                for (i = 0; i < cur_section->ru[typ].registers_used; i++)
                       if (cur_op[op_arg] ==
                               (opcode_t)cur_section->ru[typ].reg_usage[i]) {
   #else /* JIT_IMCC_OJ */
  +                for (i = 0; i < to_map[typ]; i++)
                       if (-1 - cur_op[op_arg] ==
                               (opcode_t)cur_section->ru[typ].reg_usage[i]) {
   #endif /* JIT_IMCC_OJ */
  
  
  
  1.19      +5 -1      parrot/jit/alpha/jit_emit.h
  
  Index: jit_emit.h
  ===================================================================
  RCS file: /cvs/public/parrot/jit/alpha/jit_emit.h,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -w -r1.18 -r1.19
  --- jit_emit.h        1 Dec 2002 12:49:28 -0000       1.18
  +++ jit_emit.h        25 Feb 2003 10:25:34 -0000      1.19
  @@ -3,7 +3,7 @@
    *
    * ALPHA
    *
  - * $Id: jit_emit.h,v 1.18 2002/12/01 12:49:28 leo Exp $
  + * $Id: jit_emit.h,v 1.19 2003/02/25 10:25:34 leo Exp $
    */
   
   /*  Register usage:
  @@ -506,6 +506,8 @@
   #  define REQUIRES_CONSTANT_POOL 1
   #  define INT_REGISTERS_TO_MAP 21
   
  +#ifndef JIT_IMCC
  +
   char intval_map[INT_REGISTERS_TO_MAP] =
       { REG1_t0, REG2_t1, REG3_t2, REG4_t3, REG5_t4, REG6_t5, REG7_t6, REG12_s3,
         REG13_s4, REG14_s5, REG16_a0, REG17_a1, REG18_a2, REG19_a3, REG20_a4,
  @@ -538,6 +540,8 @@
           ((char *)jit_info->constant_pool->slot_ptr - jit_info->arena.start);
       jit_info->arena.start = new_arena;
   }
  +
  +#endif
   
   #endif
   
  
  
  
  1.19      +6 -1      parrot/jit/arm/jit_emit.h
  
  Index: jit_emit.h
  ===================================================================
  RCS file: /cvs/public/parrot/jit/arm/jit_emit.h,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -w -r1.18 -r1.19
  --- jit_emit.h        1 Dec 2002 22:39:29 -0000       1.18
  +++ jit_emit.h        25 Feb 2003 10:25:37 -0000      1.19
  @@ -3,7 +3,7 @@
    *
    * ARM (I think this is all ARM2 or later, although it is APCS-32)
    *
  - * $Id: jit_emit.h,v 1.18 2002/12/01 22:39:29 grunblatt Exp $
  + * $Id: jit_emit.h,v 1.19 2003/02/25 10:25:37 leo Exp $
    */
   
   #ifdef ARM
  @@ -1000,6 +1000,9 @@
   
      NWC
   */
  +
  +#ifndef JIT_IMCC
  +
   char intval_map[INT_REGISTERS_TO_MAP] =
       { r0, r1, r2, r3, r4, r5, r6, r7, r8, r12 };
   
  @@ -1058,6 +1061,8 @@
   #error "ARM needs to sync D and I caches, and I don't know how to on this OS"
   #endif
   }
  +
  +#endif
   
   #endif
   
  
  
  
  1.57      +16 -7     parrot/jit/i386/jit_emit.h
  
  Index: jit_emit.h
  ===================================================================
  RCS file: /cvs/public/parrot/jit/i386/jit_emit.h,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -w -r1.56 -r1.57
  --- jit_emit.h        23 Feb 2003 15:44:06 -0000      1.56
  +++ jit_emit.h        25 Feb 2003 10:25:38 -0000      1.57
  @@ -3,7 +3,7 @@
    *
    * i386
    *
  - * $Id: jit_emit.h,v 1.56 2003/02/23 15:44:06 leo Exp $
  + * $Id: jit_emit.h,v 1.57 2003/02/25 10:25:38 leo Exp $
    */
   
   #include <assert.h>
  @@ -2664,27 +2664,29 @@
   }
   
   #else /* JIT_EMIT */
  -
   #  define REQUIRES_CONSTANT_POOL 0
   #  define INT_REGISTERS_TO_MAP 4
   #  define FLOAT_REGISTERS_TO_MAP 4
   
  +#ifndef JIT_IMCC
  +
   char intval_map[] =
   /* we can't use ECX, shift ops need it, push ECX before shift doesn't
    * because, when ECX is mapped you get shrl %cl, %ecx */
       { emit_EBX, emit_EDI, emit_ESI, emit_EDX };
   
  +/* ST(0) is used as a scratch register,
  + * using more then 4 registers breaks C<time N0>
  + */
  +char floatval_map[] = { 1,2,3,4 };
  +#endif
  +
   /* of these registers that much (from 0 < n) are callee saved, i.e. are
    * not changed around external calls
    */
   
   #  define PRESERVED_INT_REGS 3
   
  -/* ST(0) is used as a scratch register,
  - * using more then 4 registers breaks C<time N0>
  - */
  -char floatval_map[] = { 1,2,3,4 };
  -
   
   /*
    * if jit_emit_noop is defined, it does align a jump target
  @@ -2716,6 +2718,13 @@
   #define MAP(i) OMAP(i)
   #undef MAP
   #define MAP(i) (i) >= 0 : 0 ? OMAP(i)
  +
  +
  +/*
  + * I386 has JITed vtables, which have the vtable# in extcall.
  + * This Parrot_jit_vtable_n_op() doese use register mappings.
  + */
  +#  define EXTCALL(op) (op_jit[*(op)].extcall == 1)
   
   #endif /* JIT_EMIT */
   
  
  
  
  1.22      +5 -1      parrot/jit/ppc/jit_emit.h
  
  Index: jit_emit.h
  ===================================================================
  RCS file: /cvs/public/parrot/jit/ppc/jit_emit.h,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -w -r1.21 -r1.22
  --- jit_emit.h        2 Dec 2002 13:08:53 -0000       1.21
  +++ jit_emit.h        25 Feb 2003 10:25:39 -0000      1.22
  @@ -3,7 +3,7 @@
    *
    * PPC
    *
  - * $Id: jit_emit.h,v 1.21 2002/12/02 13:08:53 grunblatt Exp $
  + * $Id: jit_emit.h,v 1.22 2003/02/25 10:25:39 leo Exp $
    */
   
   #include <unistd.h>
  @@ -689,7 +689,9 @@
    * r15 code_start
    */
   
  +#ifndef JIT_IMCC
   char intval_map[INT_REGISTERS_TO_MAP] =
  +
       { r16, r17, r18, r19, r20, r21, r22, r23,
         r24, r25, r26, r27, r28, r29, r30, r31,
         r2, r3, r4, r5, r6, r7, r8, r9, r10 };
  @@ -715,6 +717,8 @@
       }
       __asm__ __volatile__ ("sync");
   }
  +
  +#endif
   
   #endif
   
  
  
  
  1.21      +3 -1      parrot/jit/sun4/jit_emit.h
  
  Index: jit_emit.h
  ===================================================================
  RCS file: /cvs/public/parrot/jit/sun4/jit_emit.h,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -w -r1.20 -r1.21
  --- jit_emit.h        21 Jan 2003 15:35:04 -0000      1.20
  +++ jit_emit.h        25 Feb 2003 10:25:43 -0000      1.21
  @@ -3,7 +3,7 @@
   **
   ** SPARC
   **
  -** $Id: jit_emit.h,v 1.20 2003/01/21 15:35:04 leo Exp $
  +** $Id: jit_emit.h,v 1.21 2003/02/25 10:25:43 leo Exp $
   **/
   
   /*
  @@ -640,7 +640,9 @@
   #  define REQUIRES_CONSTANT_POOL 0
   #  define INT_REGISTERS_TO_MAP 1
   
  +#ifndef JIT_IMCC
   char intval_map[INT_REGISTERS_TO_MAP] = { emitm_l(0) };
  +#endif
   
   #endif
   
  
  
  
  1.13      +4 -3      parrot/languages/imcc/debug.c
  
  Index: debug.c
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/debug.c,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -w -r1.12 -r1.13
  --- debug.c   23 Feb 2003 11:08:39 -0000      1.12
  +++ debug.c   25 Feb 2003 10:25:45 -0000      1.13
  @@ -71,14 +71,15 @@
       int pc;
   
       fprintf(stderr, "\nDumping the instructions 
status:\n-------------------------------\n");
  -    fprintf(stderr, "n\tblock\tdepth\tflags\ttype     opnum\tsize\tpc\tins\n");
  +    fprintf(stderr, "nins blck deep flags\t    type opnr size   pc  X ins\n");
       for (pc = 0, ins = instructions; ins; ins = ins->next) {
        bb = bb_list[ins->bbindex];
   
        if (bb) {
  -          fprintf(stderr, "%i\t%d\t%d\t%x\t%8x %d\t%d\t%d\t",
  +          fprintf(stderr, "%4i %4d %4d\t%x\t%8x %4d %4d %4d  %c ",
                     ins->index, bb->index, bb->loop_depth,
  -                     ins->flags, ins->type, ins->opnum, ins->opsize, pc);
  +                     ins->flags, (ins->type & ~ITEXT), ins->opnum,
  +                     ins->opsize, pc, ins->type & ITEXT ? 'X' : ' ');
        }
        else {
             fprintf(stderr, "\t");
  
  
  
  1.40      +139 -109  parrot/languages/imcc/imc.c
  
  Index: imc.c
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/imc.c,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -w -r1.39 -r1.40
  --- imc.c     23 Feb 2003 13:17:34 -0000      1.39
  +++ imc.c     25 Feb 2003 10:25:45 -0000      1.40
  @@ -15,7 +15,9 @@
   #include <assert.h>
   #include "imc.h"
   #include "optimizer.h"
  +#define JIT_IMCC
   #include "parrot/jit.h"
  +#include "parrot/jit_emit.h"
   
   static void make_stat(int *sets, int *cols);
   static void imc_stat_init(void);
  @@ -38,6 +40,7 @@
           return;
   
       init_tables(interpreter);
  +    allocated = 0;
   
       debug(DEBUG_IMC, "\n------------------------\n");
       debug(DEBUG_IMC, "processing sub %s\n", function);
  @@ -97,8 +100,10 @@
           order_spilling();          /* puts the remaing item on stack */
   
           to_spill = try_allocate();
  +        allocated = 1;
   
           if ( to_spill >= 0 ) {
  +            allocated = 0;
               spill(interpreter, to_spill);
               build_reglist();
               life_analysis();
  @@ -770,141 +775,166 @@
    *       so they are save.
    */
   
  +#ifndef EXTCALL
  +#  define EXTCALL(op) op_jit[*(op)].extcall
  +#endif
  +
  +#ifndef INT_REGISTERS_TO_MAP
  +#  define INT_REGISTERS_TO_MAP 0
  +#endif
  +
  +#ifndef FLOAT_REGISTERS_TO_MAP
  +#  define FLOAT_REGISTERS_TO_MAP 0
  +#endif
  +
  +#ifndef PRESERVED_INT_REGS
  +#  define PRESERVED_INT_REGS MAX_MAPPED
  +#endif
  +
  +#ifndef PRESERVED_FLOAT_REGS
  +#  define PRESERVED_FLOAT_REGS MAX_MAPPED
  +#endif
  +
   static void
   allocate_jit(struct Parrot_Interp *interpreter)
   {
  -    int i, j, f, k;
  -/* int to_map[] = { INT_REGISTERS_TO_MAP, 0, 0,
  - *      FLOAT_REGISTERS_TO_MAP };
  - *      XXX communicate these with jit_emit.h
  - */
  -    int to_map[] = { 4, 0, 0, 4 };
  +    int c, j, k, typ;
  +    int to_map[4] = {0,0,0,0};
  +#define MAX_MAPPED 32
  +    int preserved[] =
  +    {PRESERVED_INT_REGS, MAX_MAPPED, MAX_MAPPED, PRESERVED_FLOAT_REGS};
       const char *types = "IPSN";
  -    Instruction * ins, *next, *last, *tmp, *prev;
  -    SymReg * r, *cpu[64], *regs[IMCC_MAX_REGS];       /* XXX */
  -    /* TODO
  -     * cpu, par, reads, writes may be more then the actual mapped registers
  -     * because register get reused and may have the same color
  -     */
  -    int reads[64], writes[64], nr, nw;
  -    static SymReg *par[64];       /* XXX */
  -    static int ncpu;
  -
  -    for (j = 0; j < ncpu; j++)
  -        free(par[j]);
  -    for(i = ncpu = 0; i < HASH_SIZE; i++) {
  -        r = hash[i];
  -        /* Add each mapped register to cpu reglist */
  -        for(; r; r = r->next) {
  -            if ((r->type & VTREGISTER) && r->set != 'K') {
  -                if (r->color >= 0 &&
  -                        r->color < to_map[strchr(types, r->set) - types]) {
  -                    r->color = -1 - r->color;
  -                    cpu[ncpu++] = r;
  -                }
  -            }
  -        }
  +    Instruction * ins, *last, *tmp, *prev;
  +    SymReg * r;
  +    SymReg * regs[IMCC_MAX_REGS];
  +    static SymReg  *cpu[4][MAX_MAPPED];
  +    static SymReg  *par[4][MAX_MAPPED];
  +    int reads[4][MAX_MAPPED], writes[4][MAX_MAPPED], nr, nw;
  +    int maxc[4] = {0,0,0,0};
  +
  +    assert(INT_REGISTERS_TO_MAP < MAX_MAPPED);
  +    assert(FLOAT_REGISTERS_TO_MAP < MAX_MAPPED);
  +    to_map[0] = INT_REGISTERS_TO_MAP;
  +    to_map[3] = FLOAT_REGISTERS_TO_MAP;
  +    /* make a list of mapped cpu regs */
  +    if (cpu[0][0] == NULL) {
  +        for (typ = 0; typ < 4; typ++)
  +            for (k = 0; k < to_map[typ]; k++) {
  +                char name[16];
  +                sprintf(name, "%c%d#c", types[typ], k);
  +                cpu[typ][k] = mk_pasm_reg(str_dup(name));
  +                cpu[typ][k]->color = -1 - k;
  +                sprintf(name, "%c%d#p", types[typ], k);
  +                par[typ][k] = mk_pasm_reg(str_dup(name));
       }
  -    assert(ncpu <= 64);
  -    /* generate a list of parrot registers */
  -    for (j = 0; j < ncpu; j++) {
  -        par[j] = malloc(sizeof(SymReg));
  -        memcpy(par[j], cpu[j], sizeof(SymReg));
  -        par[j]->color = -1 - cpu[j]->color;
       }
       prev = last = NULL;
       nr = nw = 0;
  -    for (ins = instructions; ins; prev = ins, ins = ins->next) {
  -        if (ins->opnum >= 0 &&
  -                op_jit[ins->opnum].extcall == 1) {  /* XXX i386 */
  -            for (k = 0; k < ncpu; k++)
  -                reads[k] = writes[k] = 999;
  -            /* TODO non preserved regs */
  -            for (last = next = ins;
  -                    next && op_jit[next->opnum].extcall == 1;
  -                    next = next->next) {
  -                for (j = 0; j < ncpu; j++) {
  -                    if (instruction_reads(next, cpu[j]) &&
  -                            instruction_writes(next, cpu[j])) {
  -                        /* or set reads[j] - could be faster */
  -                        for (k = f = 0; k < nr; k++) {
  -                            if (reads[k] == j) {
  -                                f = 1;
  -                                break;
  -                            }
  -                        }
  -                        if (!f)
  -                            reads[nr++] = j;
  -                        for (k = f = 0; k < nr; k++) {
  -                            if (writes[k] == j) {
  -                                f = 1;
  -                                break;
  -                            }
  -                        }
  -                        if (!f)
  -                            writes[nw++] = j;
  -                        for (k = 0; next->r[k] && k < IMCC_MAX_REGS; k++)
  -                            if (next->r[k] == cpu[j])
  -                                next->r[k] = par[j];
  -                    }
  -                    else if (instruction_reads(next, cpu[j])) {
  -                        for (k = f = 0; k < nr; k++) {
  -                            if (reads[k] == j) {
  -                                f = 1;
  -                                break;
  -                            }
  -                        }
  -                        if (!f)
  -                            reads[nr++] = j;
  -                        for (k = 0; next->r[k] && k < IMCC_MAX_REGS; k++)
  -                            if (next->r[k] == cpu[j])
  -                                next->r[k] = par[j];
  -                    }
  -                    else if (instruction_writes(next, cpu[j])) {
  -                        for (k = 0; next->r[k] && k < IMCC_MAX_REGS; k++)
  -                            if (next->r[k] == cpu[j])
  -                                next->r[k] = par[j];
  -                        for (k = f = 0; k < nr; k++) {
  -                            if (writes[k] == j) {
  -                                f = 1;
  -                                break;
  +    /* change all mappable registers to mapped ones */
  +    for (j = 0; j < n_symbols; j++) {
  +        r = reglist[j];
  +        typ = strchr(types, r->set) - types;
  +        if (r->color < to_map[typ]) {
  +            if (r->color >= maxc[typ])
  +                maxc[typ] = r->color + 1;
  +            r->color = -1 - r->color;
                               }
  +
                           }
  -                        if (!f)
  -                            writes[nw++] = j;
  +    to_map[0] = maxc[0];
  +    to_map[3] = maxc[3];
  +    /* clear all used regs at beginning */
  +    for (typ = 0; typ < 4; typ++)
  +        for (j = 0; j < to_map[typ]; j++) {
  +            regs[0] = cpu[typ][j];
  +            regs[1] = par[typ][j];
  +            tmp = INS(interpreter, "set", "%s, %s\t# init",
  +                    regs, 2, 0, 0);
  +            insert_ins(NULL, tmp);
                       }
  +    /* TODO restore regs before end if non main */
  +    for (ins = instructions; ins; prev = ins, ins = ins->next) {
  +        /* clear preserved regs, set rw of non preserved regs */
  +        for (typ = 0; ins != instructions && typ < 4; typ++)
  +            for (k = 0; k < to_map[typ]; k++) {
  +                reads[typ][k] = writes[typ][k] =
  +                    k >= preserved[typ];
  +            }
  +        /* if extern, go through regs and check the usage */
  +        if (ins->opnum >= 0 && EXTCALL(&ins->opnum)) {
  +            nr = nw = 1;
  +            /* TODO stop at basic block end */
  +            for (last = ins; ins && ins->opnum >= 0 && EXTCALL(&ins->opnum);
  +                    ins = ins->next) {
  +                ins->type |= ITEXT;
  +                for (j = 0; j < n_symbols; j++) {
  +                    r = reglist[j];
  +                    if (r->color >= 0)
  +                        continue;
  +                    typ = strchr(types, r->set) - types;
  +                    c = -1 - r->color;
  +                    if (instruction_reads(ins, r) &&
  +                            instruction_writes(ins, r)) {
  +                        reads[typ][c] = 1;
  +                        writes[typ][c] = 1;
  +                        nr = nw = 1;
  +                    }
  +                    else if (instruction_reads(ins, r)) {
  +                        reads[typ][c] = 1;
  +                        nr = 1;
  +                    }
  +                    else if (instruction_writes(ins, r)) {
  +                        writes[typ][c] = 1;
  +                        nw = 1;
  +                    }
  +                }
  +                /* changed mapped regs to parrot regs */
  +                for (j = 0; (r = ins->r[j]) && j < IMCC_MAX_REGS; j++) {
  +                    typ = strchr(types, r->set) - types;
  +                    if ((r->type & VTREGISTER) && r->color < 0)
  +                        ins->r[j] = par[typ][-1 - r->color];
                   }
                   /* remember last extern opcode, all loads are inserted
                    * after this instruction
                    */
  -                if (next->type & ITBRANCH) {
  +                last = ins;
  +                if (ins->type & ITBRANCH) {
                       break;
                   }
  -                last = next;
               }
           }
           /* insert load ins after non JIT block */
           if (last && nw) {
  -            for ( ; nw ; nw--) {
  -                j = writes[nw-1];
  -                regs[0] = cpu[j];
  -                regs[1] = par[j];
  +            for (typ = 0; typ < 4; typ++)
  +                for (j = 0; j < to_map[typ]; j++) {
  +                    if (!writes[typ][j])
  +                        continue;
  +                    regs[0] = cpu[typ][j];
  +                    regs[1] = par[typ][j];
                   tmp = INS(interpreter, "set", "%s, %s\t# load",
                           regs, 2, 0, 0);
                   insert_ins(last, tmp);
               }
  -            last = NULL;
  +            nw = 0;
           }
           /* insert save ins before extern op */
           if (nr) {
  -            for ( ; nr ; nr--) {
  -                j = reads[nr-1];
  -                regs[0] = par[j];
  -                regs[1] = cpu[j];
  -                tmp = INS(interpreter, "set", "%s, %s\t# save", regs, 2, 0, 0);
  +            for (typ = 0; typ < 4; typ++)
  +                for (j = 0; j < to_map[typ]; j++) {
  +                    if (!reads[typ][j])
  +                        continue;
  +                    regs[0] = par[typ][j];
  +                    regs[1] = cpu[typ][j];
  +                    tmp = INS(interpreter, "set", "%s, %s\t# save",
  +                            regs, 2, 0, 0);
                   insert_ins(prev, tmp);
               }
  +            nr = 0;
           }
  +        /* continue with/after last non JIT ins */
  +        if (last)
  +            ins = last;
  +        last = NULL;
       }
   }
   
  
  
  
  1.30      +2 -0      parrot/languages/imcc/imc.h
  
  Index: imc.h
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/imc.h,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -w -r1.29 -r1.30
  --- imc.h     22 Feb 2003 15:12:25 -0000      1.29
  +++ imc.h     25 Feb 2003 10:25:45 -0000      1.30
  @@ -81,6 +81,8 @@
   EXTERN int optimizer_level;
   EXTERN int dont_optimize;
   EXTERN int has_compile;
  +EXTERN int allocated;
  +
   
   enum {
        OPT_NONE,
  
  
  
  1.49      +2 -0      parrot/languages/imcc/imcc.y
  
  Index: imcc.y
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/imcc.y,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -w -r1.48 -r1.49
  --- imcc.y    22 Feb 2003 15:12:25 -0000      1.48
  +++ imcc.y    25 Feb 2003 10:25:45 -0000      1.49
  @@ -299,6 +299,8 @@
                        * previously, so:
                        * goon
                        */
  +                     if (r[i]->set != 'P')
  +                        dirs |= 1 << (16 + i);
            case PARROT_ARGDIR_IN:
                       dirs |= 1 << i ;
                break;
  
  
  
  1.29      +2 -2      parrot/languages/imcc/instructions.c
  
  Index: instructions.c
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/instructions.c,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -w -r1.28 -r1.29
  --- instructions.c    22 Feb 2003 15:12:25 -0000      1.28
  +++ instructions.c    25 Feb 2003 10:25:45 -0000      1.29
  @@ -397,7 +397,7 @@
            sprintf(regb[i], "%c%d", p->set, p->color);
            regstr[i] = regb[i];
        }
  -        else if ((optimizer_level & OPT_J) && p->set != 'K' &&
  +        else if (allocated && (optimizer_level & OPT_J) && p->set != 'K' &&
                   p->color < 0 && (p->type & VTREGISTER)) {
            sprintf(regb[i], "r%c%d", tolower(p->set), -1 - p->color);
            regstr[i] = regb[i];
  @@ -408,7 +408,7 @@
                   if (k->reg && k->reg->color >= 0)
                       sprintf(regb[i]+strlen(regb[i]), "%c%d",
                               k->reg->set, k->reg->color);        /* XXX */
  -                else if ((optimizer_level & OPT_J) &&  k->reg &&
  +                else if (allocated && (optimizer_level & OPT_J) &&  k->reg &&
                           k->reg->color < 0)
                       sprintf(regb[i]+strlen(regb[i]), "r%c%d",
                               tolower(k->reg->set), -1 - k->reg->color);
  
  
  
  1.19      +2 -1      parrot/languages/imcc/instructions.h
  
  Index: instructions.h
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/instructions.h,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -w -r1.18 -r1.19
  --- instructions.h    22 Feb 2003 15:12:25 -0000      1.18
  +++ instructions.h    25 Feb 2003 10:25:45 -0000      1.19
  @@ -8,7 +8,8 @@
       ITLABEL =  0x80000, /*            label         */
       ITALIAS = 0x100000, /*   set P,P  */
       ITADDR  = 0x200000, /*   set_addr P, addr*/
  -    ITSPILL = 0x400000  /*   set P31,x ; set x, p31 spilling */
  +    ITSPILL = 0x400000, /*   set P31,x ; set x, p31 spilling */
  +    ITEXT   = 0x800000  /*   instruction is extcall in JIT */
   };
   
   
  
  
  
  1.19      +12 -11    parrot/languages/imcc/main.c
  
  Index: main.c
  ===================================================================
  RCS file: /cvs/public/parrot/languages/imcc/main.c,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -w -r1.18 -r1.19
  --- main.c    21 Feb 2003 12:26:40 -0000      1.18
  +++ main.c    25 Feb 2003 10:25:45 -0000      1.19
  @@ -151,6 +151,18 @@
               case 'O':
                   strncpy(optimizer_opt, (*argv)[0]+2,sizeof(optimizer_opt));
                   optimizer_opt[sizeof(optimizer_opt)-1] = '\0';
  +                if (strchr(optimizer_opt, '1'))
  +                    optimizer_level |= OPT_PRE;
  +                if (strchr(optimizer_opt, '2'))
  +                    optimizer_level |= (OPT_CFG | OPT_PRE);
  +                if (strchr(optimizer_opt, 'j')) {
  +                    int one = 1;
  +                    optimizer_level |= (OPT_J | OPT_PASM);
  +                    Parrot_setflag(interp, PARROT_JIT_FLAG, &one);
  +                }
  +                if (strchr(optimizer_opt, 'p'))
  +                    optimizer_level |= OPT_PASM;
  +
                   break;
               case '-':
                   if ((*argv)[0][2] == '\0') {
  @@ -204,17 +216,6 @@
           strcpy(optimizer_opt, "0");
           optimizer_level = 0;
       }
  -    else {
  -        if (strchr(optimizer_opt, '1'))
  -            optimizer_level |= OPT_PRE;
  -        if (strchr(optimizer_opt, '2'))
  -            optimizer_level |= (OPT_CFG | OPT_PRE);
  -        if (strchr(optimizer_opt, 'j'))
  -            optimizer_level |= (OPT_J | OPT_PASM);
  -        if (strchr(optimizer_opt, 'p'))
  -            optimizer_level |= OPT_PASM;
  -    }
  -
   
       if (!sourcefile || !*sourcefile) {
           fatal(EX_NOINPUT, "main", "No source file specified.\n" );
  
  
  

Reply via email to