value cupl_posmin(value right) /* row position of minimum element of argument */ { if (right.rank == 0) die("POSMIN argument is a scalar"); else { value result; int n; scalar minel = right.elements[0];; make_scalar(&result, 0); for (n = 0; n < right.width * right.depth; n++) if (minel > right.elements[n]) { result.elements[0] = SUBI(right, n); minel = right.elements[n]; } return(result); } }
/* Process 2OPI Integer instructions */ bool eval_2OPI_Int(struct lilith* vm, struct Instruction* c) { #ifdef DEBUG char Name[20] = "ILLEGAL_2OPI"; #endif /* 0x0E ... 0x2B */ /* 0xB0 ... 0xDF */ switch(c->raw2) { case 0x0E: /* ADDI */ { #ifdef DEBUG strncpy(Name, "ADDI", 19); #elif TRACE record_trace("ADDI"); #endif ADDI(vm, c); break; } case 0x0F: /* ADDUI */ { #ifdef DEBUG strncpy(Name, "ADDUI", 19); #elif TRACE record_trace("ADDUI"); #endif ADDUI(vm, c); break; } case 0x10: /* SUBI */ { #ifdef DEBUG strncpy(Name, "SUBI", 19); #elif TRACE record_trace("SUBI"); #endif SUBI(vm, c); break; } case 0x11: /* SUBUI */ { #ifdef DEBUG strncpy(Name, "SUBUI", 19); #elif TRACE record_trace("SUBUI"); #endif SUBUI(vm, c); break; } case 0x12: /* CMPI */ { #ifdef DEBUG strncpy(Name, "CMPI", 19); #elif TRACE record_trace("CMPI"); #endif CMPI(vm, c); break; } case 0x13: /* LOAD */ { #ifdef DEBUG strncpy(Name, "LOAD", 19); #elif TRACE record_trace("LOAD"); #endif LOAD(vm, c); break; } case 0x14: /* LOAD8 */ { #ifdef DEBUG strncpy(Name, "LOAD8", 19); #elif TRACE record_trace("LOAD8"); #endif LOAD8(vm, c); break; } case 0x15: /* LOADU8 */ { #ifdef DEBUG strncpy(Name, "LOADU8", 19); #elif TRACE record_trace("LOADU8"); #endif LOADU8(vm, c); break; } case 0x16: /* LOAD16 */ { #ifdef DEBUG strncpy(Name, "LOAD16", 19); #elif TRACE record_trace("LOAD16"); #endif LOAD16(vm, c); break; } case 0x17: /* LOADU16 */ { #ifdef DEBUG strncpy(Name, "LOADU16", 19); #elif TRACE record_trace("LOADU16"); #endif LOADU16(vm, c); break; } case 0x18: /* LOAD32 */ { #ifdef DEBUG strncpy(Name, "LOAD32", 19); #elif TRACE record_trace("LOAD32"); #endif LOAD32(vm, c); break; } case 0x19: /* LOADU32 */ { #ifdef DEBUG strncpy(Name, "LOADU32", 19); #elif TRACE record_trace("LOADU32"); #endif LOADU32(vm, c); break; } case 0x1F: /* CMPUI */ { #ifdef DEBUG strncpy(Name, "CMPUI", 19); #elif TRACE record_trace("CMPUI"); #endif CMPUI(vm, c); break; } case 0x20: /* STORE */ { #ifdef DEBUG strncpy(Name, "STORE", 19); #elif TRACE record_trace("STORE"); #endif STORE(vm, c); break; } case 0x21: /* STORE8 */ { #ifdef DEBUG strncpy(Name, "STORE8", 19); #elif TRACE record_trace("STORE8"); #endif STORE8(vm, c); break; } case 0x22: /* STORE16 */ { #ifdef DEBUG strncpy(Name, "STORE16", 19); #elif TRACE record_trace("STORE16"); #endif STORE16(vm, c); break; } case 0x23: /* STORE32 */ { #ifdef DEBUG strncpy(Name, "STORE32", 19); #elif TRACE record_trace("STORE32"); #endif STORE32(vm, c); break; } case 0xB0: /* ANDI */ { #ifdef DEBUG strncpy(Name, "ANDI", 19); #elif TRACE record_trace("ANDI"); #endif ANDI(vm, c); break; } case 0xB1: /* ORI */ { #ifdef DEBUG strncpy(Name, "ORI", 19); #elif TRACE record_trace("ORI"); #endif ORI(vm, c); break; } case 0xB2: /* XORI */ { #ifdef DEBUG strncpy(Name, "XORI", 19); #elif TRACE record_trace("XORI"); #endif XORI(vm, c); break; } case 0xB3: /* NANDI */ { #ifdef DEBUG strncpy(Name, "NANDI", 19); #elif TRACE record_trace("NANDI"); #endif NANDI(vm, c); break; } case 0xB4: /* NORI */ { #ifdef DEBUG strncpy(Name, "NORI", 19); #elif TRACE record_trace("NORI"); #endif NORI(vm, c); break; } case 0xB5: /* XNORI */ { #ifdef DEBUG strncpy(Name, "XNORI", 19); #elif TRACE record_trace("XNORI"); #endif XNORI(vm, c); break; } case 0xC0: /* CMPJUMPI.G */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.G", 19); #elif TRACE record_trace("CMPJUMPI.G"); #endif CMPJUMPI_G(vm, c); break; } case 0xC1: /* CMPJUMPI.GE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.GE", 19); #elif TRACE record_trace("CMPJUMPI.GE"); #endif CMPJUMPI_GE(vm, c); break; } case 0xC2: /* CMPJUMPI.E */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.E", 19); #elif TRACE record_trace("CMPJUMPI.E"); #endif CMPJUMPI_E(vm, c); break; } case 0xC3: /* CMPJUMPI.NE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.NE", 19); #elif TRACE record_trace("CMPJUMPI.NE"); #endif CMPJUMPI_NE(vm, c); break; } case 0xC4: /* CMPJUMPI.LE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.LE", 19); #elif TRACE record_trace("CMPJUMPI.LE"); #endif CMPJUMPI_LE(vm, c); break; } case 0xC5: /* CMPJUMPI.L */ { #ifdef DEBUG strncpy(Name, "CMPJUMPI.L", 19); #elif TRACE record_trace("CMPJUMPI.L"); #endif CMPJUMPI_L(vm, c); break; } case 0xD0: /* CMPJUMPUI.G */ { #ifdef DEBUG strncpy(Name, "CMPJUMPUI.G", 19); #elif TRACE record_trace("CMPJUMPUI.G"); #endif CMPJUMPUI_G(vm, c); break; } case 0xD1: /* CMPJUMPUI.GE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPUI.GE", 19); #elif TRACE record_trace("CMPJUMPUI.GE"); #endif CMPJUMPUI_GE(vm, c); break; } case 0xD4: /* CMPJUMPUI.LE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPUI.LE", 19); #elif TRACE record_trace("CMPJUMPUI.LE"); #endif CMPJUMPUI_LE(vm, c); break; } case 0xD5: /* CMPJUMPUI.L */ { #ifdef DEBUG strncpy(Name, "CMPJUMPUI.L", 19); #elif TRACE record_trace("CMPJUMPUI.L"); #endif CMPJUMPUI_L(vm, c); break; } default: { illegal_instruction(vm, c); break; } } #ifdef DEBUG fprintf(stdout, "# %s reg%u reg%u %i\n", Name, c->reg0, c->reg1, c->raw_Immediate); #endif return false; }
filter_fct_t net_filter_alloc(filter_t *filter, unsigned int size, unsigned int *lenp) { struct local *s; int len, oldi, i, j, ncommon, sp; int type, value, arg, op, reg, reg1, dst, commoni; int *instructions, *instp; #if USE_EXTRA_REGS int oldmaxreg; #endif boolean_t compiling; #define SCHAR_MAX 127 /* machine/machlimits->h, anyone? */ assert(NET_MAX_FILTER <= SCHAR_MAX); assert(NET_FILTER_STACK_DEPTH <= SCHAR_MAX); assert(NREGS <= SCHAR_MAX); assert(size < NET_MAX_FILTER); s = (struct local *) kalloc(sizeof *s); #if USE_EXTRA_REGS s->maxreg = INITIAL_NSCRATCHREGS; #endif len = 0; compiling = FALSE; /* This loop runs at least twice, once with compiling==FALSE to determine the length of the instructions we will compile, and once with compiling==TRUE to compile them. The code generated on the two passes must be the same. In the USE_EXTRA_REGS case, the loop can be re-run an extra time while !compiling, if we decide to use the callee-saves registers. This is because we may be able to generate better code with the help of these registers than before. */ while (1) { /* Identify values that we can potentially preserve in a register to avoid having to reload them. All immediate values and references to known offsets in the header or data are candidates. The results of this loop are the same on every run, so with a bit of work we could run it just once; but this is not a time-critical application. */ ncommon = 0; for (i = 0; i < size; i++) { oldi = i; arg = NETF_ARG(filter[i]); if (arg == NETF_PUSHLIT) { type = NF_LITERAL; value = filter[++i]; if (value == 0) continue; } else if (arg >= NETF_PUSHSTK) { continue; } else if (arg >= NETF_PUSHHDR) { type = NF_HEADER; value = arg - NETF_PUSHHDR; } else if (arg >= NETF_PUSHWORD) { type = NF_DATA; value = arg - NETF_PUSHWORD; } else { continue; } for (j = 0; j < ncommon; j++) { if (s->common[j].type == type && s->common[j].value == value) { s->common[j].nuses++; break; } } if (j == ncommon) { s->common[j].type = type; s->common[j].value = value; s->common[j].nuses = 1; ncommon++; } s->commonpos[oldi] = j; } #if USE_EXTRA_REGS oldmaxreg = s->maxreg; #endif /* Initially, no registers hold common values or are on the stack. */ for (i = 0; i < ncommon; i++) s->common[i].reg = NO_REG; for (i = 0; i < NSCRATCHREGS; i++) { s->regs[scratchregs[i]].commoni = NOT_COMMON_VALUE; s->regs[scratchregs[i]].stacktimes = 0; } /* Now read through the filter and generate code. */ sp = -1; /* sp points to top element */ for (i = 0; i < size; i++) { if (!compiling) instp = junk_filter; assert(sp >= -1); assert(sp < NET_FILTER_STACK_DEPTH - 1); commoni = s->commonpos[i]; arg = NETF_ARG(filter[i]); op = NETF_OP(filter[i]); /* Generate code to get the required value into a register and set `reg' to the number of this register. */ switch (arg) { case NETF_PUSHLIT: value = filter[++i]; reg = s->common[commoni].reg; if (reg == 0) { if ((reg = allocate_register(s, commoni)) == 0) goto fail; assert(value >= 0); /* Comes from unsigned short. */ if (value > MAX_LDO) { *instp++ = LDIL(value & ~MAX_LDO, reg); value &= MAX_LDO; if (value != 0) *instp++ = LDO(value, reg, reg); } else *instp++ = LDO(value, 0, reg); } s->common[commoni].nuses--; break; case NETF_NOPUSH: reg = s->stackregs[sp--]; s->regs[reg].stacktimes--; break; case NETF_PUSHZERO: reg = 0; break; case NETF_PUSHIND: case NETF_PUSHHDRIND: reg1 = s->stackregs[sp--]; s->regs[reg1].stacktimes--; if (arg == NETF_PUSHIND) *instp++ = ARITH_OP(OP_COMCLR, ARITH_ULT, reg1, REG_ARG1, REG_RET0); /* comclr,< <reg1>,arg1,ret0 */ else *instp++ = COMICLR(ARITH_UGT, NET_HDW_HDR_MAX/sizeof (unsigned short), reg1, REG_RET0); /* comiclr,> N,<reg1>,ret0 */ assert((NET_HDW_HDR_MAX / sizeof(unsigned short)) <= MAX_COMICLR); *instp++ = BV_N(0, REG_RTN); /* bv,n (rp) */ if ((reg = allocate_register(s, -1)) == 0) goto fail; *instp++ = LDHX_S(reg1, (arg == NETF_PUSHIND) ? REG_ARG0 : REG_ARG2, reg); /* ldhx,s reg1(arg0/2),reg */ break; default: if (arg >= NETF_PUSHSTK) reg = s->stackregs[sp - (arg - NETF_PUSHSTK)]; else if (arg >= NETF_PUSHWORD) { assert(2 * (NETF_PUSHHDR - NETF_PUSHWORD) <= MAX_LDO); assert(NETF_PUSHHDR - NETF_PUSHWORD <= MAX_COMICLR); assert(NETF_PUSHSTK - NETF_PUSHHDR <= MAX_LDO); reg = s->common[commoni].reg; if (reg == 0) { if ((reg = allocate_register(s, commoni)) == 0) goto fail; if (arg < NETF_PUSHHDR) { value = arg - NETF_PUSHWORD; *instp++ = COMICLR(ARITH_ULT, value, REG_ARG1, REG_RET0); /* comiclr,< value,arg1,ret0 */ *instp++ = BV_N(0, REG_RTN); /* bv,n (rp) */ reg1 = REG_ARG0; } else { value = arg - NETF_PUSHHDR; reg1 = REG_ARG2; } *instp++ = LDH(2 * value, reg1, reg); } s->common[commoni].nuses--; } } /* Now generate code to do `op' on `reg1' (lhs) and `reg' (rhs). */ if (op != NETF_NOP) { reg1 = s->stackregs[sp--]; s->regs[reg1].stacktimes--; } switch (op) { case NETF_OP(NETF_CAND): case NETF_OP(NETF_COR): case NETF_OP(NETF_CNAND): case NETF_OP(NETF_CNOR): dst = -1; case NETF_OP(NETF_NOP): break; default: /* Allocate a register to put the result in. */ if ((dst = allocate_register(s, -1)) == 0) goto fail; } switch (op) { case NETF_OP(NETF_NOP): dst = reg; break; case NETF_OP(NETF_EQ): case NETF_OP(NETF_LT): case NETF_OP(NETF_LE): case NETF_OP(NETF_GT): case NETF_OP(NETF_GE): case NETF_OP(NETF_NEQ): switch (op) { case NETF_OP(NETF_EQ): j = ARITH_NE; break; case NETF_OP(NETF_LT): j = ARITH_UGE; break; case NETF_OP(NETF_LE): j = ARITH_UGT; break; case NETF_OP(NETF_GT): j = ARITH_ULE; break; case NETF_OP(NETF_GE): j = ARITH_ULT; break; case NETF_OP(NETF_NEQ): j = ARITH_EQ; break; } *instp++ = ARITH_OP(OP_COMCLR, j, reg1, reg, dst); *instp++ = LDI(1, dst); break; case NETF_OP(NETF_AND): case NETF_OP(NETF_OR): case NETF_OP(NETF_XOR): case NETF_OP(NETF_ADD): case NETF_OP(NETF_SUB): switch (op) { case NETF_OP(NETF_AND): j = OP_AND; break; case NETF_OP(NETF_OR): j = OP_OR; break; case NETF_OP(NETF_XOR): j = OP_XOR; break; case NETF_OP(NETF_ADD): j = OP_ADD; break; case NETF_OP(NETF_SUB): j = OP_SUB; break; } *instp++ = ARITH_OP(j, ARITH_NEVER, reg1, reg, dst); if (op == NETF_OP(NETF_ADD) || op == NETF_OP(NETF_SUB)) *instp++ = EXTRU(dst, 31, 16, dst); /* Adds and subtracts can produce results that don't fit in 16 bits so they have to be masked. The logical operations can't so they don't. */ break; case NETF_OP(NETF_LSH): case NETF_OP(NETF_RSH): *instp++ = SUBI(31, reg, REG_RET0); *instp++ = MTSAR(REG_RET0); if (op == NETF_OP(NETF_LSH)) { *instp++ = ZVDEP(reg1, 32, dst); *instp++ = EXTRU(dst, 31, 16, dst); } else *instp++ = VEXTRU(reg, 32, dst); /* For some reason, all arithmetic is done in 16 bits, so the result of LSH has to be masked with 0xFFFF. The result of RSH doesn't since it can't be any bigger than the 16-bit value that was shifted. We use ret0 to compute the shift amount because we can't use reg or reg1 (which might have values we subsequently use), nor dst (which might be the same as reg1). Alternatively, we could allocate another register, but we would need to temporarily do s->regs[dst].stacktimes++ to avoid just getting dst again. */ break; case NETF_OP(NETF_COR): /* comb,<>,n reg1,reg,$x | bv (rp) | ldi 1,ret0 | $x: I have found no way to do this in less than three instructions (as for the other NETF_C* operations), unless it be to branch to a "bv (rp) | ldi 1,ret0" postamble, and what would be the point in that? */ *instp++ = COMB_SKIP_1(COND_EQ, 1, 1, reg1, reg); *instp++ = BV(0, REG_RTN); *instp++ = LDI(1, REG_RET0); break; case NETF_OP(NETF_CNAND): /* xor,= reg1,reg,ret0 | bv,n (rp) This leaves a non-zero (true) value in ret0 if the values are different. */ *instp++ = ARITH_OP(OP_XOR, ARITH_EQ, reg1, reg, REG_RET0); *instp++ = BV_N(0, REG_RTN); break; case NETF_OP(NETF_CAND): case NETF_OP(NETF_CNOR): /* comclr,{=|<>} reg1,reg,ret0 | bv,n (rp) */ j = (op == NETF_OP(NETF_CAND)) ? ARITH_EQ : ARITH_NE; *instp++ = ARITH_OP(OP_COMCLR, j, reg1, reg, REG_RET0); *instp++ = BV_N(0, REG_RTN); break; default: printf("op == 0x%x\n", op); panic("net_filter_alloc: bad op"); /* Should have been caught by parse_net_filter(). */ } /* If the op generated a result, push it on the stack. */ if (dst >= 0) { s->stackregs[++sp] = dst; s->regs[dst].stacktimes++; } if (!compiling) { assert(instp - junk_filter <= MAX_INSTR_PER_ITEM); len += instp - junk_filter; } } if (compiling) { /* If the stack contains any values, we are supposed to return 0 or 1 according as the top-of-stack is zero or not. Since the only place we are called requires just zero-false/nonzero-true, we simply copy the value into ret0. If the stack is empty, we return TRUE. */ *instp++ = BV(0, REG_RTN); /* bv (rp) */ if (sp >= 0) *instp++ = COPY(s->stackregs[sp], REG_RET0); else *instp++ = LDI(1, REG_RET0); break; } else { len += 2; #if USE_EXTRA_REGS if (s->maxreg > oldmaxreg) { len = 0; continue; } len += compile_preamble(NULL, s); #endif } if ((instructions = kmem_alloc_exec(len * sizeof (int))) == NULL) return NULL; instp = instructions; #if USE_EXTRA_REGS instp += compile_preamble(instp, s); #endif compiling = TRUE; } assert(instp - instructions == len); *lenp = len * sizeof (int); fdcache(HP700_SID_KERNEL, (vm_offset_t)instructions, len * sizeof (int)); kfree((vm_offset_t) s, sizeof *s); return (filter_fct_t) instructions; fail: assert(!compiling); kfree((vm_offset_t) s, sizeof *s); printf("net_filter_alloc: failed to compile (filter too complex)\n"); printf("-- will work, but more slowly; consider enabling USE_EXTRA_REGS\n"); return NULL; }