pike.git / src / code / riscv.c

version» Context lines:

pike.git/src/code/riscv.c:399: Inside #if defined(__riscv_compressed)
   ptr[1] = (unsigned INT16)(n >> 16);   #else    ptr[0] = (unsigned INT16)(n >> 16);    ptr[1] = (unsigned INT16)n;   #endif   #else    *(INT32*)ptr = n;   #endif   }    + #if PIKE_BYTEORDER == 4321 + /* Big endian RISC-V stores parcels as little endian */ + #define RISCV_SWAP_PARCEL(x) (__builtin_bswap16((x))) + #else + #define RISCV_SWAP_PARCEL(x) (x) + #endif +    static void rv_emit(unsigned INT32 instr)   { -  add_to_program((unsigned INT16)instr); +  add_to_program(RISCV_SWAP_PARCEL((unsigned INT16)instr));    if ((instr & 3) == 3) -  add_to_program((unsigned INT16)(instr >> 16)); +  add_to_program(RISCV_SWAP_PARCEL((unsigned INT16)(instr >> 16)));   }      static void rv_call_millicode(enum rv_register reg, enum rv_millicode milli)   {    INT32 delta = (PIKE_PC - compiler_state.millicode[milli]) * -2;    if (delta < -1048576 || delta > 1048575)    Pike_fatal("rv_call_millicode: branch out of range for JAL\n");    rv_emit(RV_JAL(reg, delta));   }      static void rv_func_epilogue(void)   {    rv_call_millicode(RV_REG_ZERO, RV_MILLICODE_EPILOGUE);   }      void riscv_update_f_jump(INT32 offset, INT32 to_offset)   {    PIKE_OPCODE_T *op = &Pike_compiler->new_program->program[offset]; -  unsigned INT32 instr = op[0]; +  unsigned INT32 instr = RISCV_SWAP_PARCEL(op[0]);    to_offset -= offset;    if ((instr & 3) == 3) { -  instr |= op[1] << 16; +  instr |= RISCV_SWAP_PARCEL(op[1]) << 16;    if ((instr & 0x7f) == 111) {    /* JAL */    if (to_offset < -524288 || to_offset > 524287)    Pike_fatal("riscv_update_f_jump: branch out or range for JAL: %d\n", (int)to_offset);    instr = RV_J(111, (instr&0xf80)>>7, to_offset*2);    } else if((instr & 0x7f) == 99) {    /* BRANCH */    if (to_offset < -2048 || to_offset > 2047)    Pike_fatal("riscv_update_f_jump: branch out or range for BRANCH: %d\n", (int)to_offset);    instr = RV_B(99, (instr & 0x7000)>>12, (instr & 0xf8000)>>15,    (instr & 0x1ff00000)>>20, to_offset*2);    } else {    Pike_fatal("riscv_update_f_jump on unknown instruction: %x\n", (unsigned)instr);    } -  op[0] = instr; -  op[1] = instr >> 16; +  op[0] = RISCV_SWAP_PARCEL(instr); +  op[1] = RISCV_SWAP_PARCEL(instr >> 16);    } else {   #ifdef __riscv_compressed    if ((instr & 0x6003) == 0x2001) {    /* C.JAL / C.J */    if (to_offset < -1024 || to_offset > 1023)    Pike_fatal("riscv_update_f_jump: branch out or range for C.JAL/C.J: %d\n", (int)to_offset);    instr = RV_CJ(1, instr>>13, to_offset*2);    } else if ((instr & 0xc003) == 0xc001) {    /* C.BEQZ / C.BNEZ */    if (to_offset < -128 || to_offset > 127)    Pike_fatal("riscv_update_f_jump: branch out or range for C.BEQZ/C.BNEZ: %d\n", (int)to_offset);    instr = RV_CB(1, instr>>13, ((instr & 0x0380)>>7)+8, to_offset*2);    } else    Pike_fatal("riscv_update_f_jump on unknown instruction: %x\n", (unsigned)instr); -  op[0] = instr; +  op[0] = RISCV_SWAP_PARCEL(instr);   #else    Pike_fatal("riscv_update_f_jump on unknown instruction: %x\n", (unsigned)instr);   #endif    }   }      int riscv_read_f_jump(INT32 offset)   {    PIKE_OPCODE_T *op = &Pike_compiler->new_program->program[offset]; -  INT32 instr = op[0]; +  INT32 instr = RISCV_SWAP_PARCEL(op[0]);    if ((instr & 3) == 3) { -  instr |= (INT32)(op[1] << 16); +  instr |= (INT32)(RISCV_SWAP_PARCEL(op[1]) << 16);    if ((instr & 0x7f) == 111) {    /* JAL */    return offset +    ((((instr>>11)&~0xfffff)|    ((instr>>20)&0x7fe)|(((instr)>>9)&0x800)|((instr)&0xff000)) >> 1);    } else if((instr & 0x7f) == 99) {    /* BRANCH */    return offset +    ((((instr>>19)&~0xfff)|((instr>>20)&0x7e0)|    ((instr>>7)&0x1e)|((instr<<4)&0x800)) >> 1);
pike.git/src/code/riscv.c:508:       Pike_fatal("riscv_read_f_jump on unknown instruction: %x\n", (unsigned)instr);    return 0;   }      static void rv_update_pcrel(INT32 offset, enum rv_register reg, INT32 delta)   {    PIKE_OPCODE_T *instr = &Pike_compiler->new_program->program[offset];    unsigned INT32 first = RV_AUIPC(reg, delta);    unsigned INT32 second = RV_ADDI_(reg, reg, delta); -  if ((instr[0] & 0xfff) != (first & 0xfff) || instr[2] != (unsigned INT16)second) +  if ((RISCV_SWAP_PARCEL(instr[0]) & 0xfff) != (first & 0xfff) || RISCV_SWAP_PARCEL(instr[2]) != (unsigned INT16)second)    Pike_fatal("rv_update_pcrel on mismatching instruction pair\n"); -  instr[0] = first; -  instr[1] = first >> 16; -  instr[3] = second >> 16; +  instr[0] = RISCV_SWAP_PARCEL(first); +  instr[1] = RISCV_SWAP_PARCEL(first >> 16); +  instr[3] = RISCV_SWAP_PARCEL(second >> 16);   }      static void rv_mov_int32(enum rv_register reg, INT32 val)   {    if (RV_IN_RANGE_IMM12(val))    rv_emit(RV_ADDI(reg, RV_REG_ZERO, val));    else {    /* Adjust for signedness of lower 12 bits... */    if (val & 0x800) {   #if __riscv_xlen > 32
pike.git/src/code/riscv.c:1040:    static const char * const op_op[] = {"*add", "sll", "slt", "sltu",    "xor", "*srl", "or", "and"};    static const char * const mul_op[] = {"mul", "mulh", "mulhsu", "mulhu",    "div", "divu", "rem", "remu"};    static const char * const branch_op[] = {"beq ", "bne ", NULL, NULL,    "blt ", "bge ", "bltu", "bgeu"};    static const char * const system_op[] = {"rw","rs","rc"};    const unsigned INT16 *parcel = addr;       while (bytes >= 2) { -  unsigned INT32 instr = *parcel; +  unsigned INT32 instr = RISCV_SWAP_PARCEL(*parcel);    if ((instr&3) != 3) {    /* 16-bit format */    fprintf(stderr, "%p %04x ", parcel, instr);    switch (((instr&3)<<3) | RV_CFUNCT3(instr)) {    case 0:    if (!(instr & 0x1f30))    fprintf(stderr, "illegal\n");    else    fprintf(stderr, "addi %s,%s,%d\n",    riscv_regname(RV_REGCD_(instr)), riscv_regname(2),
pike.git/src/code/riscv.c:1333:    (int)(RV_GET_BITS(instr,5,3,10)|RV_GET_BITS(instr,8,6,7)),    riscv_regname(2));   #endif    break;    }    bytes -= 2;    parcel++;    } else if((instr&0x1f) != 0x1f) {    /* 32-bit format */    if (bytes < 4) break; -  instr |= ((unsigned INT32)parcel[1])<<16; +  instr |= ((unsigned INT32)RISCV_SWAP_PARCEL(parcel[1]))<<16;    fprintf(stderr, "%p %08x ", parcel, instr);    switch ((instr >> 2) & 0x1f) {    case 0:    if (RV_FUNCT3(instr) <= 6)    fprintf(stderr, "%s %s,%d(%s)\n",    load_op[RV_FUNCT3(instr)],    riscv_regname(RV_REGD(instr)),    (int)RV_I_IMM(instr),    riscv_regname(RV_REGS1(instr)));    else
pike.git/src/code/riscv.c:1504:    else if(!(instr & 0x40))    parcel_count = 4; /* 64-bit */    else if((instr & 0x7000) != 0x7000)    parcel_count = 5 + ((instr >> 12) & 7); /* 80-176-bit */    else    parcel_count = 1; /* Reserved for >=192-bit, not defined... */    if (bytes < parcel_count * 2)    break;    fprintf(stderr, "%p ", parcel);    for (i = parcel_count; i > 0; ) { -  fprintf(stderr, "%04x", parcel[--i]); +  fprintf(stderr, "%04x", RISCV_SWAP_PARCEL(parcel[--i]));    }    fprintf(stderr, "\n");    bytes -= parcel_count * 2;    parcel += parcel_count;    }    }    if (bytes > 0) {    const unsigned char *rest = (const unsigned char *)parcel;    while(bytes > 0) {    fprintf(stderr, "%p %02x\n", rest, *rest);    --bytes;    rest++;    }    }   }