void execute(pruCPU *cpu) { aluInstruction inst; fmt2InstructionHeader fmt2Hdr; while(1) { int didBranch = 0; cgc_memcpy(&inst, (aluInstruction *)&cpu->code[cpu->pc], 4); switch(inst.opFmt) { case 0b000: switch(inst.aluOp) { case ADD: doAdd(cpu, inst); break; case ADC: doAdc(cpu, inst); break; case SUB: doSub(cpu, inst); break; case SUC: doSuc(cpu, inst); break; case LSL: doLsl(cpu, inst); break; case LSR: doLsr(cpu, inst); break; case RSB: doRsb(cpu, inst); break; case RSC: doRsc(cpu, inst); break; case AND: doAnd(cpu, inst); break; case OR: doOr(cpu, inst); break; case XOR: doXor(cpu, inst); break; case NOT: doNot(cpu, inst); break; case MIN: doMin(cpu, inst); break; case MAX: doMax(cpu, inst); break; case CLR: doClr(cpu, inst); break; case SET: doSet(cpu, inst); break; } break; case 0b001: cgc_memcpy(&fmt2Hdr, &inst, sizeof(fmt2Hdr)); switch(fmt2Hdr.subOp) { case JMP: case JAL: ; fmt2BranchInstruction fmt2Branch; cgc_memcpy(&fmt2Branch, &inst, 4); doBranch(cpu, fmt2Branch); didBranch = 1; break; case LDI: ; fmt2LdiInstruction fmt2Ldi; cgc_memcpy(&fmt2Ldi, &inst, 4); doLdi(cpu, fmt2Ldi); break; case LMBD: ; fmt2LmbdInstruction fmt2Lmbd; cgc_memcpy(&fmt2Lmbd, &inst, 4); doLmbd(cpu, fmt2Lmbd); break; case HALT: return; case SCAN: ; fmt2ScanInstruction fmt2Scan; cgc_memcpy(&fmt2Scan, &inst, 4); doScan(cpu, fmt2Scan); break; case SLP: case RESERVED_1: case RESERVED_2: case RESERVED_3: case RESERVED_4: case RESERVED_5: case RESERVED_6: case RESERVED_7: case RESERVED_8: case RESERVED_9: return; } break; case 0b11: ; fmtQatbInstruction qatbInstruction; cgc_memcpy(&qatbInstruction, &inst, 4); doQATB(cpu, qatbInstruction); default: return; } if(didBranch == 0) cpu->pc++; cpu->numExecuted++; if(cpu->numExecuted >= MAX_INSNS) return; } }
// read a file with queries // parse the queries // and output to file as well as stdout void analyze(Indexed* indexed, std::string queryFilename, std::string outputFilename){ std::string line; std::ifstream queryfile(queryFilename.c_str()); std::ofstream outputfile(outputFilename.c_str()); if (!outputfile.is_open()){ std::cout << "could not open output file\n"; return; }; const Rows* rows = indexed->rows; if (queryfile.is_open()){ // read all the lines while ( getline (queryfile, line) ) { Tokens t = tokenize(line); bool valid = true; if (t.size() < 3){ valid = false; }; int colNum1 = 0; int colType1 = 0; int colNum2 = 0; if (valid){ ColsByName::const_iterator colIt = rows->header.find(t[1]); if (colIt == rows->header.end()){ std::cout << "column: " << t[1] << " not found!\n"; valid = false; }else{ colNum2 = colIt->second.pos; if (!typeCanAggregate(colIt->second.type)){ std::cout << "can not perform: " << t[0] << " over " << t[1] << " of type " << typeName(colIt->second.type) << "\n"; valid = false; }; }; colIt = rows->header.find(t[2]); if (colIt == rows->header.end()){ std::cout << "column: " << t[2] << " not found!\n"; valid = false; }else{ colNum1 = colIt->second.pos; colType1 = colIt->second.type; }; }; if (valid){ // output to file and std out std::cout << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n"; outputfile << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n"; if (t[0] == "AVG"){ doAvg(indexed, colNum1, colNum2, colType1, outputfile); }else if (t[0] == "MIN"){ doMin(indexed, colNum1, colNum2, colType1, outputfile); }else if (t[0] == "MAX"){ doMax(indexed, colNum1, colNum2, colType1, outputfile); }else{ std::cout << "unknown aggregate function: " << t[0] << "\n"; }; // new line std::cout << "\n"; outputfile << "\n"; }; }; }; outputfile.close(); };