Datum ltq_regex(PG_FUNCTION_ARGS) { ltree *tree = PG_GETARG_LTREE(0); lquery *query = PG_GETARG_LQUERY(1); bool res = false; if (query->flag & LQUERY_HASNOT) { FieldNot fn; fn.q = NULL; res = checkCond(LQUERY_FIRST(query), query->numlevel, LTREE_FIRST(tree), tree->numlevel, &fn); } else { res = checkCond(LQUERY_FIRST(query), query->numlevel, LTREE_FIRST(tree), tree->numlevel, NULL); } PG_FREE_IF_COPY(tree, 0); PG_FREE_IF_COPY(query, 1); PG_RETURN_BOOL(res); }
Exp *GenericExpTransformer::applyTo(Exp *e, bool &bMod) { bool change; Exp *bindings = e->match(match); if (bindings == NULL) { #if 0 if (e->getOper() == match->getOper()) LOG << "match failed: " << e << " with " << match << "\n"; #endif return e; } if (where) { Exp *cond = where->clone(); if (checkCond(cond, bindings) == false) return e; } LOG << "applying generic exp transformer match: " << match; if (where) LOG << " where: " << where; LOG << " become: " << become; LOG << " to: " << e; LOG << " bindings: " << bindings << "\n"; e = become->clone(); for (Exp *l = bindings; l->getOper() != opNil; l = l->getSubExp2()) e = e->searchReplaceAll(l->getSubExp1()->getSubExp1(), l->getSubExp1()->getSubExp2(), change); LOG << "calculated result: " << e << "\n"; bMod = true; Exp *r; if (e->search(new Unary(opVar, new Terminal(opWild)), r)) { LOG << "error: variable " << r << " in result!\n"; assert(false); } return e; }
bool GenericExpTransformer::checkCond(Exp *cond, Exp *bindings) { switch (cond->getOper()) { case opAnd: return checkCond(cond->getSubExp1(), bindings) && checkCond(cond->getSubExp2(), bindings); case opEquals: { Exp *lhs = cond->getSubExp1(), *rhs = cond->getSubExp2(); for (Exp *l = bindings; l->getOper() != opNil; l = l->getSubExp2()) { Exp *e = l->getSubExp1(); bool change = false; lhs = lhs->searchReplaceAll(e->getSubExp1()->clone(), e->getSubExp2()->clone(), change); #if 0 if (change) LOG << "replaced " << e->getSubExp1() << " with " << e->getSubExp2() << "\n"; #endif change = false; rhs = rhs->searchReplaceAll(e->getSubExp1()->clone(), e->getSubExp2()->clone(), change); #if 0 if (change) LOG << "replaced " << e->getSubExp1() << " with " << e->getSubExp2() << "\n"; #endif } if (lhs->getOper() == opTypeOf) { #if 0 // ADHOC TA Type *ty = lhs->getSubExp1()->getType(); #else Type *ty = NULL; #endif if (ty == NULL) { #if 0 if (VERBOSE) LOG << "no type for typeof " << lhs << "\n"; #endif return false; } lhs = new TypeVal(ty); #if 0 LOG << "got typeval: " << lhs << "\n"; #endif } if (lhs->getOper() == opKindOf) { OPER op = lhs->getSubExp1()->getOper(); lhs = new Const(operStrings[op]); } rhs = applyFuncs(rhs); #if 0 LOG << "check equals in cond: " << lhs << " == " << rhs << "\n"; #endif if (lhs->getOper() == opVar) { Exp *le; for (le = bindings; le->getOper() != opNil && le->getSubExp2()->getOper() != opNil; le = le->getSubExp2()) ; assert(le->getOper() != opNil); le->setSubExp2(new Binary(opList, new Binary(opEquals, lhs->clone(), rhs->clone()), new Terminal(opNil))); #if 0 LOG << "bindings now: " << bindings << "\n"; #endif return true; } if (*lhs == *rhs) return true; #if 0 // ADHOC TA if (lhs->getOper() == opTypeVal && rhs->getOper() == opTypeVal && lhs->getType()->resolvesToCompound() && rhs->getType()->isCompound()) return true; #endif Exp *new_bindings = lhs->match(rhs); if (new_bindings == NULL) return false; #if 0 LOG << "matched lhs with rhs, bindings: " << new_bindings << "\n"; #endif Exp *le; for (le = bindings; le->getOper() != opNil && le->getSubExp2()->getOper() != opNil; le = le->getSubExp2()) ; assert(le->getOper() != opNil); le->setSubExp2(new_bindings); #if 0 LOG << "bindings now: " << bindings << "\n"; #endif return true; } default: LOG << "don't know how to handle oper " << operStrings[cond->getOper()] << " in cond.\n"; } return false; }
int main(int argc, char **argv) { // allocate and zero out memory and registers initialiseARMstate(); // ensure correct number of args if (argc != 2) { printUsageMsg(); return EXIT_FAILURE; } // open inary file FILE *instrFile = fopen(argv[1], "r"); if (instrFile == NULL) { printUsageMsg(); return EXIT_FAILURE; } // load instructions into memory size_t numRead = fread(MEM, MEM_WORD_SIZE, MEM_SIZE, instrFile); if (numRead == 0) { fputs("Error: Could not read binary file", stderr); printUsageMsg(); return EXIT_FAILURE; } if (!feof(instrFile)) { fputs("Error: Ran out of memory to hold instructions.", stderr); return EXIT_FAILURE; } // close the binary file if (fclose(instrFile)) { fputs("Warning: failed to close binary file.", stderr); } // initialise fetched and current instruction uint32_t fetchedInstr = getNextInstr(); uint32_t currInstr = fetchedInstr; // loop until next instruction is 0 (halt instruction) while (currInstr) { // gets next instruction and increments PC currInstr = fetchedInstr; fetchedInstr = getNextInstr(); // skip this instruction if condition says to if (!checkCond(currInstr)) { continue; } // decode and execute the instruction bool wasBranch = decodeAndExecute(currInstr); if (wasBranch) { fetchedInstr = getNextInstr(); } } // print the final system state printState(); // free memory used deallocARMState(); return EXIT_SUCCESS; }
static bool checkCond(lquery_level * curq, int query_numlevel, ltree_level * curt, int tree_numlevel, FieldNot * ptr) { uint32 low_pos = 0, high_pos = 0, cur_tpos = 0; int tlen = tree_numlevel, qlen = query_numlevel; int isok; lquery_level *prevq = NULL; ltree_level *prevt = NULL; if (SomeStack.muse) { high_pos = SomeStack.high_pos; qlen--; prevq = curq; curq = LQL_NEXT(curq); SomeStack.muse = false; } while (tlen > 0 && qlen > 0) { if (curq->numvar) { prevt = curt; while (cur_tpos < low_pos) { curt = LEVEL_NEXT(curt); tlen--; cur_tpos++; if (tlen == 0) return false; if (ptr && ptr->q) ptr->nt++; } if (ptr && curq->flag & LQL_NOT) { if (!(prevq && prevq->numvar == 0)) prevq = curq; if (ptr->q == NULL) { ptr->t = prevt; ptr->q = prevq; ptr->nt = 1; ptr->nq = 1 + ((prevq == curq) ? 0 : 1); ptr->posq = query_numlevel - qlen - ((prevq == curq) ? 0 : 1); ptr->post = cur_tpos; } else { ptr->nt++; ptr->nq++; } if (qlen == 1 && ptr->q->numvar == 0) ptr->nt = tree_numlevel - ptr->post; curt = LEVEL_NEXT(curt); tlen--; cur_tpos++; if (high_pos < cur_tpos) high_pos++; } else { isok = false; while (cur_tpos <= high_pos && tlen > 0 && !isok) { isok = checkLevel(curq, curt); curt = LEVEL_NEXT(curt); tlen--; cur_tpos++; if (isok && prevq && prevq->numvar == 0 && tlen > 0 && cur_tpos <= high_pos) { FieldNot tmpptr; if (ptr) memcpy(&tmpptr, ptr, sizeof(FieldNot)); SomeStack.high_pos = high_pos - cur_tpos; SomeStack.muse = true; if (checkCond(prevq, qlen + 1, curt, tlen, (ptr) ? &tmpptr : NULL)) return true; } if (!isok && ptr) ptr->nt++; } if (!isok) return false; if (ptr && ptr->q) { if (checkCond(ptr->q, ptr->nq, ptr->t, ptr->nt, NULL)) return false; ptr->q = NULL; } low_pos = cur_tpos; high_pos = cur_tpos; } } else { low_pos = cur_tpos + curq->low; high_pos = cur_tpos + curq->high; if (ptr && ptr->q) { ptr->nq++; if (qlen == 1) ptr->nt = tree_numlevel - ptr->post; } } prevq = curq; curq = LQL_NEXT(curq); qlen--; } if (low_pos > tree_numlevel || tree_numlevel > high_pos) return false; while (qlen > 0) { if (curq->numvar) { if (!(curq->flag & LQL_NOT)) return false; } else { low_pos = cur_tpos + curq->low; high_pos = cur_tpos + curq->high; } curq = LQL_NEXT(curq); qlen--; } if (low_pos > tree_numlevel || tree_numlevel > high_pos) return false; if (ptr && ptr->q && checkCond(ptr->q, ptr->nq, ptr->t, ptr->nt, NULL)) return false; return true; }
/** * pendingProcess * INPUT: none * OUTPUT: none * INFO: The master function for pending command sequence execution. * This function is called from the pending process state in the state status monitoring * state machine once per second. While this should happen immediately after telemetry is * recorded, and therefore should not interfere with the telemetry interrupt, it is not * occurring within an interrupt and therefore is placed within an uninterrupted block (by adjusting the processor priority) * to prevent any interrupts from happening during it and potentially leaving the satellite in an * indeterminate or unsafe state. * * A sequence is only "ready" if there are commands loaded and the ready flag is set to true * * Exit conditions that are relative times are changed to absolute time exit conditions since * the time in which the link was closed (the time the sequence starts processing) is not tracked * anywhere within here (It's not quickly accessible because it may or may not be in the response poll that does * not keep track of opcode. The link could've timed out as well). * * The next item on the sequence is peeked at (to get a copy) so that we can quickly check the wait conditions after the exit conditions. * Exit conditions are checked first since we do not want to continue to process a sequence if the exit conditions evaluate to TRUE. * If they do evaluate to TRUE, the sequence is aborted and both response poll and the beacon is updated. * Next the conditions of the next item in the sequence are checked. If they are false nothing happens to the sequence. * If it evaluates to TRUE, that item is pulled off of the stack and executed, the response poll for it is updated, and if there are more * commands, then the current time is stored as the last command time (for relative time checks). * Since the pending process state is a special, transitory state in the state status response state machine, it needs to be put back into * its previous mode so it may resume other operations. * * Finally, the processor mode is returned to normal so interrupts may occur. */ void pendingProcess(){ //major issues could arise if the processing of the line is interrupted, especially due to the link being established //Does not take long and occurs immediately after gathering telemetry so should not cause any issues. //the contents of this function are as narrow as this window can be placed. cpu_priority_t priority = Metal_SetCPUPriority(UNINTERRUPTIBLE_PRIORITY); if( (Global->csSequence.cmd_queue.count > 0) && (Global->csSequence.seq_ready_flag != 0) /*&& (Link_GetMode() & ~(LINK_SEQUENCING | LINK_ACTIVE))*/){ //check the exit conditions and fix them if either is a relative time uint32_t time = csunSatEpoch(getRTC()); dprintf("time = %ld\r\n", time); conditions_t exitCheck = Global->csSequence.exit; if(exitCheck.left.sensor_id == 254){ //change this to an absolute time value dprintf("delta time of %ld being changed -",exitCheck.left.value); exitCheck.left.value += time; dprintf("now absolute of %ld\r\n",exitCheck.left.value); exitCheck.left.sensor_id = 255; G_SET(csSequence.exit, &exitCheck); } if(exitCheck.op != JUST){//AND or OR is the operator, meaning right comparator is present if(exitCheck.right.sensor_id == 254){ //change this to an absolute time value exitCheck.right.value += time; exitCheck.right.sensor_id = 255; G_SET(csSequence.exit, &exitCheck); } } dprintf("Checking pending Command sequence\r\n"); //going to place EXIT/WAIT checks for pending commands here. resp_poll_t updating; seq_command_t pendingCmd; PendCmdQueue_Peek(&Global->csSequence.cmd_queue, &pendingCmd); BOOL result = false; //first check to see if we need to EXIT //pull EXIT conditions dprintf("Exit condition check - "); exitCheck = Global->csSequence.exit; //select the appropriate analysis depending on operator switch(exitCheck.op){ case JUST: result = checkCond(exitCheck.left); updating.status = 1; break; case AND: result = (checkCond(exitCheck.left) && checkCond(exitCheck.right)); updating.status = 2; break; case OR: result = checkCond(exitCheck.left); if(result){ updating.status = 3; } result = result || checkCond(exitCheck.right); if(result && updating.status != 3){ updating.status = 4; } else if(result){ updating.status = 5; } break; //no default since these three cases are all possible values } if(result){ dprintf("ABORTING SEQUENCE!\r\n"); //if exit conditions met, ABORT SEQUENCE resetPayload(); abortSequence(updating.status, time); beaconMsgUpdateSingle(SOFTWARE_STATE,'D'); } else{ dprintf("Good!\r\nChecking wait conditons - "); result = false; //then, if we did NOT exit, peek at the next item in the sequence //break down the wait conditions and process according to JUST, AND, or OR switch(pendingCmd.wait.op){ case JUST: result = checkCond(pendingCmd.wait.left); updating.status = 1; break; case AND: result = (checkCond(pendingCmd.wait.left) && checkCond(pendingCmd.wait.right)); updating.status = 2; break; case OR: result = checkCond(pendingCmd.wait.left); if(result){ updating.status = 3; } result = result || checkCond(pendingCmd.wait.right); if(result && updating.status != 3){ updating.status = 4; } else if(result){ updating.status = 5; } break; //no default since these three cases are all possible values } //check to see if all necessary conditions have been met if(result){ dprintf("Executing next pending command!\r\n"); //pop & do the next item in the sequence PendCmdQueue queueTemp; memcpy(&queueTemp, &Global->csSequence.cmd_queue, sizeof(queueTemp)); //read PendCmdQueue_Dequeue(&queueTemp, &pendingCmd); //mod G_SET(csSequence.cmd_queue, &queueTemp); //write decodeAndRunPending(pendingCmd); updating.epoch = time; updating.cmd_ID = pendingCmd.cmd_id; updating.type = PENDING_COMPLETE; updating.status = 0; respPollUpdatePending(updating); //check if another item is waiting to be run if(PendCmdQueue_Count(&Global->csSequence.cmd_queue) > 0){ //if there is, check to see if the wait condition has a time PendCmdQueue_Peek(&Global->csSequence.cmd_queue, &pendingCmd); if(pendingCmd.wait.left.sensor_id == 254 || (pendingCmd.wait.op != JUST && pendingCmd.wait.right.sensor_id == 254)){ //if so, record the time so the delta can be calculated G_SET(csSequence.lastCmdTime,&time); } } else{ dprintf("Wait conditions not satisfied currently\r\n"); //if something needs to be done when a sequence is empty, add that code here } } } } //go back to whatever type of sub state it was in before if(Global->csState.statMonState == PENDING_PROCESS){ statMonStateToPrevious(); } //return processor to previous priority mode Metal_SetCPUPriority(priority); }
std::vector<int> Matcher::matchBetter(const dps::Signal * event) { Node *next; int mark; std::vector<int> subscribers; this->skip.clear(); for (auto cond: this->sub->getPrimary()){ try { if (this->skip.at(cond.first) == false) { continue; } } catch (const std::out_of_range& oor){ if (!checkCond(event, cond.first)){ continue; }//TODO: add to cache } for (auto path: cond.second->ownPaths){ next = path.second; mark = path.first; while (next!=nullptr) { try { if (this->skip.at(next->condition) == false) { next=nullptr; continue; } else { if (next->paths.count(mark)>0) { next=next->paths[mark]; } else { next=nullptr; subscribers.push_back(mark); handleSubscriber(mark); } } } catch (const std::out_of_range& oor){ if (checkCond(event, next->condition)){ if (this->sub->inPrimary(next->condition)) { this->newRoots(next, mark); this->moveParents(next, mark); } else if (this->sub->inSecondary(next->condition)) { this->moveParents(next, mark); } if (next->paths.count(mark)>0) { next = next->paths[mark]; /*try{ Node* newNode = this->pathMap.at(next->condition+std::string(":")+std::to_string(mark)) ; next = newNode; } catch (const std::out_of_range& oor){ // leave next at the same state }*/ } else { next = nullptr; subscribers.push_back(mark); handleSubscriber(mark); } } else { if (this->sub->inPrimary(next->condition)) { this->disablePrimary(next); } else if (this->sub->inSecondary(next->condition)) { this->dropParents(next, mark); } next = nullptr; continue; } } } } } return subscribers; }
int main(int argc, char *argv[]) { //Check for binary file argument if(argc != 2) { printf("No binary file provided!\n"); printf("Usage: emulate [binary_file]\n\n"); exit(EXIT_FAILURE); } //----------INITIALIZE------------- //Initialize arm struct ARM arm; //memset(arm.memory, 0, MAX_MEMORY_SIZE); //zero array for(int i = 0; i < MAX_MEMORY_SIZE; ++i) { arm.memory[i] = 0; } //GPIO setGpioAddress(&arm); //setup GPIO address arm.pc = 0; //Execute from the beginning //memset(arm.registers, 0, NUMBER_OF_REGISTERS); for(int i = 0; i < NUMBER_OF_REGISTERS; ++i) { arm.registers[i] = 0; } arm.cpsr = 0; //Read the binary file (program) to the memory, starting from pos0 binRead(arm.memory, argv[1]); int ir = 0; //INSTRUCTION REGISTER //-------FETCH-EXECUTE CYCLE------- do { //FETCH ir = arm.memory[arm.pc]; //Fetch the current instruction ++arm.pc; //Increment pc counter //DECODE int cond = checkCond(&ir, &arm); if(cond) { //Determine type switch(getType(&ir)) { case DATA_PROCESSING: dataprocessing(&ir, &arm); break; case BRANCH: branch(&ir, &arm); break; case MULTIPLY: multiply(&ir, &arm); break; case SINGLE_DATA_TRANSFER: single_data_transfer(&ir, &arm); break; } } } while (ir != 0); //---------CLEAR GPIO VIRTUAL MEMORY------- clearGpioAddress(&arm); //---------PRINT ARM STATUS------- printARM(&arm); return EXIT_SUCCESS; }