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; }
/*LDI*/ static void op_ED_0xa0(Z80EX_CONTEXT *cpu) { LDI(/*rd*/4, /*wr*/7); T_WAIT_UNTIL(12); return; }
int main(int argc, char *argv[]) { E_RT_init(argc, argv); JMP(main); main: MOVI(0, R999); JMP(ML1); RL3: MOVI(2, R901); MOVI(10000, R777); MOVS("NAVDEEP", R777); PRTS(R777); PRTS("a:b\n"); JMP(L4); RL5: MOVI(2, R904); MOVI(10000, R777); MOVS("NAVDEEP", R777); PRTS(R777); PRTS("a OR b:c\n"); JMP(L6); ML1: R900=MUL(128,0); R900=ADD(10999,R900); R001=ADD(R900,97); STI(1, R001); R001=ADD(R900,98); STI(-1, R001); R001=ADD(R900,99); STI(-1, R001); R001=ADD(R900,100); STI(-1, R001); R900=MUL(128,1); R900=ADD(10999,R900); R001=ADD(R900,97); STI(-1, R001); R001=ADD(R900,98); STI(2, R001); R001=ADD(R900,99); STI(-1, R001); R001=ADD(R900,100); STI(-1, R001); R900=MUL(128,2); R900=ADD(10999,R900); R001=ADD(R900,97); STI(-1, R001); R001=ADD(R900,98); STI(-1, R001); R001=ADD(R900,99); STI(-1, R001); R001=ADD(R900,100); STI(-1, R001); MOVI(2, R902); MOVI(0, R901); R903=MUL(128,0); R903=ADD(11383,R903); R001=ADD(R903,97); STI(1, R001); R001=ADD(R903,98); STI(1, R001); R001=ADD(R903,99); STI(-1, R001); R001=ADD(R903,100); STI(-1, R001); R903=MUL(128,1); R903=ADD(11383,R903); R001=ADD(R903,97); STI(-1, R001); R001=ADD(R903,98); STI(-1, R001); R001=ADD(R903,99); STI(2, R001); R001=ADD(R903,100); STI(-1, R001); R903=MUL(128,2); R903=ADD(11383,R903); R001=ADD(R903,97); STI(-1, R001); R001=ADD(R903,98); STI(-1, R001); R001=ADD(R903,99); STI(-1, R001); R001=ADD(R903,100); STI(-1, R001); MOVI(2, R905); MOVI(0, R904); L2: IN(R000); JMPC(GT(0, R000), L0); JMPC(GT(0, R901), L4); R001=MUL(128,R901); R001=ADD(10999,R001); R001=ADD(R000,R001); LDI(R001, R002); JMPC(EQ(R002, R902), RL3); MOVI(R002, R901); L4: JMPC(GT(0, R904), L6); R001=MUL(128,R904); R001=ADD(11383,R001); R001=ADD(R000,R001); LDI(R001, R002); JMPC(EQ(R002, R905), RL5); MOVI(R002, R904); L6: JMP(L2); L0: MOVI(31000, R000); MOVS("The End", R000); PRTS(R000); E_RT_exit(); return 0; }
int main(int argc, char *argv[]) { E_RT_init(argc, argv); JMP(begin); Label0: R005=ADD(R000,2); STI(R001, R000); R000=SUB(R000,1); MOVI(R000, R001); LDI(R005, R008); R005=ADD(R005,1); LDF(R005, F001); R005=ADD(R005,1); MOVIF(R008, F003); MOVIF(R008, F006); F005=FADD(F006,F001); MOVF(F005, F008); R010=ADD(R008,1); MOVI(R010, R011); MOVI(R010, R012); MOVIF(R008, F010); MOVF(F005, F011); while_1_start: JMPC(GT(1000, R008), while_1_begin); JMP(while_1_end); while_1_begin: MOVIF(R008, F013); MOVIF(R008, F014); F008=FADD(F014,F001); F001=FMUL(F001,2.0); R008=ADD(R008,100); JMP(while_1_start); while_1_end: PRTF(F008); PRTS(R007); MOVI(R008, R002); JMP(Label1); Label1: MOVI(R001, R000); R000=ADD(R000,1); LDI(R000, R001); R000=ADD(R000,1); LDI(R000, R004); R000=ADD(R000,2); JMPI(R004); eventLabel_a: INI(R010); INF(F013); STI(R010, R000); R000=SUB(R000,1); STF(F013, R000); R000=SUB(R000,1); STF(F013, R000); R000=SUB(R000,1); STI(R010, R000); R000=SUB(R000,1); MOVL(Label2, R004); STI(R004, R000); R000=SUB(R000,1); JMP(Label0); Label2: MOVI(R002, R006); R000=ADD(R000,1); LDF(R000, F013); R000=ADD(R000,1); LDI(R000, R010); JMP(EventMStart); begin: MOVI(10000, R000); MOVI(0, R006); MOVS("\n", R007); IN(R010); IN(R010); IN(R010); EventMStart: IN(R010); JMPC(GT(64, R010), EventMOut); JMPC(EQ(97, R010), eventLabel_a); JMP(EventMStart); EventMOut: PRTS("\nDone\n"); E_RT_exit(); return 0; }
/*LDI*/ static void op_ED_0xa0(void) { LDI(/*rd*/4, /*wr*/7); T_WAIT_UNTIL(12); return; }
/** * Asynchronous (using pthreads) API call function * * command: must be a command in apiCommands * result: pointer to an *already allocated* buffer for storing result * ent: entity who made the request * count: number of variadic arguments */ qboolean G_AsyncAPICall(char *command, char *result, gentity_t *ent, int count, ...) { struct query_s *queryStruct; pthread_t thread; pthread_attr_t attr; int returnCode = 0; void *(*handler)(void *) = NULL; va_list ap; int i = 0; if (api_module == NULL || API_query == NULL) { LDE("%s\n", "API module is not loaded"); return qfalse; } // Check if thread limit is reached if (activeThreadsCounter >= THREADS_MAX) { LDE("threads limit (%d) reached: %d\n", THREADS_MAX, activeThreadsCounter); return qfalse; } // Check if we are allowed to start a new thread if (!threadingAllowed) { LDE("%s\n", "starting new threads is forbidden"); return qfalse; } // Check number of arguments in ... (=count) if (count <= 0) { LDE("invalid argument count %d\n", count); return qfalse; } queryStruct = malloc(sizeof (struct query_s)); if (queryStruct == NULL) { LDE("%s\n", "failed to allocate memory"); return qfalse; } va_start(ap, count); // Init query buffer memset(queryStruct->query, 0, QUERY_MAX_SIZE); for (i = 0; i < count; ++i) { char *arg; arg = va_arg(ap, char *); if (!arg) { LDE("empty argument %d with command '%s'\n", i, command); free(queryStruct); va_end(ap); return qfalse; } Q_strcat(queryStruct->query, QUERY_MAX_SIZE, arg); // No trailing / if (i + 1 < count) { Q_strcat(queryStruct->query, QUERY_MAX_SIZE, CHAR_SEPARATOR); } } va_end(ap); Q_strncpyz(queryStruct->cmd, command, sizeof (queryStruct->cmd)); queryStruct->result = result; queryStruct->ent = ent; // Get the command handler handler = getHandlerForCommand(command); if (!handler) { LDE("no handler for command '%s'\n", command); free(queryStruct); return qfalse; } LDI("asynchronous API call with command: '%s', query '%s'\n", command, queryStruct->query); // Create threads as detached as they will never be joined if (pthread_attr_init(&attr)) { LDE("%s\n", "error in pthread_attr_init"); free(queryStruct); return qfalse; } if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { LDE("%s\n", "error in pthread_attr_setdetachstate"); free(queryStruct); return qfalse; } returnCode = pthread_create(&thread, &attr, handler, (void *)queryStruct); if (returnCode) { LDE("error in pthread_create, return value is %d\n", returnCode); free(queryStruct); return qfalse; } // Nico, increase active threads counter activeThreadsCounter++; G_DPrintf("%s: increasing threads counter to %d\n", GAME_VERSION, activeThreadsCounter); if (pthread_attr_destroy(&attr)) { LDE("%s\n", "error in pthread_attr_destroy"); // Nico, note: I don't free querystruct because it's used in the thread return qfalse; } return qtrue; }
/** * Synchronous API call function * * command: must be a command in apiCommands * result: pointer to an *already allocated* buffer for storing result * ent: entity who made the request * count: number of variadic arguments */ qboolean G_SyncAPICall(char *command, char *result, gentity_t *ent, int count, ...) { struct query_s *queryStruct; void *(*handler)(void *) = NULL; va_list ap; int i = 0; if (api_module == NULL || API_query == NULL) { LDE("%s\n", "API module is not loaded"); return qfalse; } // Check number of arguments in ... (=count) if (count <= 0) { LDE("invalid argument count %d\n", count); return qfalse; } queryStruct = malloc(sizeof (struct query_s)); if (queryStruct == NULL) { LDE("%s\n", "failed to allocate memory"); return qfalse; } va_start(ap, count); // Init query buffer memset(queryStruct->query, 0, QUERY_MAX_SIZE); for (i = 0; i < count; ++i) { char *arg; arg = va_arg(ap, char *); if (!arg) { LDE("empty argument %d with command '%s'\n", i, command); free(queryStruct); va_end(ap); return qfalse; } Q_strcat(queryStruct->query, QUERY_MAX_SIZE, arg); // No trailing / if (i + 1 < count) { Q_strcat(queryStruct->query, QUERY_MAX_SIZE, CHAR_SEPARATOR); } } va_end(ap); Q_strncpyz(queryStruct->cmd, command, sizeof (queryStruct->cmd)); queryStruct->result = result; queryStruct->ent = ent; // Get the command handler handler = getHandlerForCommand(command); if (!handler) { LDE("no handler for command '%s'\n", command); free(queryStruct); return qfalse; } LDI("synchronous API call with command: '%s', query '%s'\n", command, queryStruct->query); handler(queryStruct); return qtrue; }