int main() { Code code; void (*f)() = code.getCode<void (*)()>(); dump(code.getCode(), code.getSize()); f(); }
void wasm::ToggleProfiling(const Code& code, const CallThunk& callThunk, bool enabled) { const CodeRange& cr = code.metadata().codeRanges[callThunk.u.codeRangeIndex]; uint32_t calleeOffset = enabled ? cr.funcProfilingEntry() : cr.funcNonProfilingEntry(); MacroAssembler::repatchFarJump(code.segment().base(), callThunk.offset, calleeOffset); }
int main(int argc, const char * argv[]) { char* text_buffer = loadFile(argv[1]); if(!text_buffer) { cout << "Can't load file: " << argv[1] << endl; return 1; } const std::string text(text_buffer); Parser parser; Status *pStatus = parser.parseProgram(text); if(pStatus && pStatus->isError()) { cout << pStatus->getError(); return 1; } Code* code = new MyCode(); BytecodeFunction * main2 = new BytecodeFunction(parser.top()); code->addFunction(main2); parser.top()->node()->visit(new ByteCodeVisitor(code, main2->bytecode())); main2->bytecode()->add(BC_STOP); //main2->bytecode()->dump(std::cout); interpreter interp(code); interp.generate(main2); return 0; }
void Compressor::compress(const char *filename) { buildCharMap(); buildCharTree(); buildCodeTable(charTree); ofstream out(filename, ios::out | ios::binary); dumpCharMap(out); int count = 0, codeSize, i; char buf = 0; while (!inputFile.eof()) { Code code = codeTable[inputFile.get()]; codeSize = (int) code.size(); for(i = 0; i < codeSize; i++) { buf |= code[i] << (7 - count); if (++count == 8) { out << buf; count = buf = NULL; } } } if(count > 0) { out << buf; } out.close(); }
bool CodeStack::validate(const Code& cf) { TypeFilter tf; // returns r, i, or j switch (tf.Get(cf.name)) { // r requires // rs, rd, rt case 'r': if (cf.rs!="" && cf.rd!="" && cf.rt!="") return true; setErr("rs or rt or rd not set"); return false; // i requires // rs, rt, imd(or lb) case 'i': if (cf.rs!="" && cf.rt!="" && cf.dirt()) return true; setErr("rs or rt or immediate not set"); return false; // j requires // imd case 'j': if (cf.dirt()) return true; setErr("immediate not set"); return false; default: // ÕâÊÇɶ return false; } return true; }
// Compares guess to secret code and returns the number of correct digits // in the incorrect locations. int Code::checkIncorrect(Code& guess) { const int secretCodeLength = getLength(); int count = 0; // correct digits in the incorrect location vector<bool> secretUsed = getUsed(); vector<bool> guessUsed = guess.getUsed(); /// check vector lengths if (guess.getLength() != secretCodeLength) { throw InvalidVectSize("Code::checkCorrect - vectors are not the same length!"); } // compare all unused guess values to current unused secret code value for (int i = 0; i < secretCodeLength; i++) { for (int j = 0; j < guess.getLength(); j++) { if ((!secretUsed[i] && !guessUsed[j]) && (getCode()[i] == guess.getCode()[j] )) { // we have a match, mark these indices as used and // increase the count before continuing the compare secretUsed[i] = true; guessUsed[j] = true; count++; break; } } } return count; }
void wasm::ToggleProfiling(const Code& code, const CallSite& callSite, bool enabled) { if (callSite.kind() != CallSite::FuncDef) return; uint8_t* callerRetAddr = code.segment().base() + callSite.returnAddressOffset(); #if defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64) void* callee = X86Encoding::GetRel32Target(callerRetAddr); #elif defined(JS_CODEGEN_ARM) uint8_t* caller = callerRetAddr - 4; Instruction* callerInsn = reinterpret_cast<Instruction*>(caller); BOffImm calleeOffset; callerInsn->as<InstBLImm>()->extractImm(&calleeOffset); void* callee = calleeOffset.getDest(callerInsn); #elif defined(JS_CODEGEN_ARM64) MOZ_CRASH(); void* callee = nullptr; (void)callerRetAddr; #elif defined(JS_CODEGEN_MIPS32) || defined(JS_CODEGEN_MIPS64) uint8_t* caller = callerRetAddr - 2 * sizeof(uint32_t); InstImm* callerInsn = reinterpret_cast<InstImm*>(caller); BOffImm16 calleeOffset; callerInsn->extractImm16(&calleeOffset); void* callee = calleeOffset.getDest(reinterpret_cast<Instruction*>(caller)); #elif defined(JS_CODEGEN_NONE) MOZ_CRASH(); void* callee = nullptr; #else # error "Missing architecture" #endif const CodeRange* codeRange = code.lookupRange(callee); if (!codeRange->isFunction()) return; uint8_t* from = code.segment().base() + codeRange->funcNonProfilingEntry(); uint8_t* to = code.segment().base() + codeRange->funcProfilingEntry(); if (!enabled) Swap(from, to); MOZ_ASSERT(callee == from); #if defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64) X86Encoding::SetRel32(callerRetAddr, to); #elif defined(JS_CODEGEN_ARM) new (caller) InstBLImm(BOffImm(to - caller), Assembler::Always); #elif defined(JS_CODEGEN_ARM64) (void)to; MOZ_CRASH(); #elif defined(JS_CODEGEN_MIPS32) || defined(JS_CODEGEN_MIPS64) new (caller) InstImm(op_regimm, zero, rt_bgezal, BOffImm16(to - caller)); #elif defined(JS_CODEGEN_NONE) MOZ_CRASH(); #else # error "Missing architecture" #endif }
int main(int argc, char** argv) { const char* script = 0; std::string mode = INTERPRETE; for (int32_t i = 1; i < argc; i++) { if (string(argv[i]) == "-d") { mode = DISASM; } else if (string(argv[i]) == "-j") { mode = JIT; } else { script = argv[i]; } } if (script == 0) { cout << "Usage: interpreter [-d | -j] <source_file>" << endl; return WRONG_ARG_COUNT; } const char* expr = loadFile(script); if (expr == 0) { cout << "Cannot read file: " << script << endl; return CANNOT_READ_SOURCE; } Translator* translator = new BytecodeTranslatorImpl(); Code* code = 0; Status* translateStatus = translator->translate(expr, &code); if (translateStatus == 0) { //todo } else if (translateStatus->isError()) { uint32_t position = translateStatus->getPosition(); uint32_t line = 0, offset = 0; positionToLineOffset(expr, position, line, offset); cout << "Cannot translate expression: expression at " << line << ", " << offset << "; " << "error '" << translateStatus->getError().c_str() << "'" << endl; } else { assert(code != 0); if (mode == DISASM) { code->disassemble(cout); } else if (mode == JIT) { } else { vector<Var*> t; code->execute(t); } delete code; } delete translateStatus; delete translator; return 0; }
int main( int argc, char** argv ) { std::cout << "Hello World" << std::endl; Code c; c.setProperty( "foo", "bar" ); c.setProperty( "hello", "world" ); cout << "Property foo = " << c.getProperty( "foo" ) << endl; cout << "Property hello = " << c.getProperty( "hello" ) << endl; }
int main() { const int count = 1000; Xbyak::util::Clock clk; Code c; void (*f)() = (void (*)())c.getCode(); for (int i = 0; i < count; i++) { clk.begin(); f(); clk.end(); } printf("%.3fclk\n", clk.getClock() / double(N) / clk.getCount()); }
static void UpdateEntry(const Code& code, bool profilingEnabled, void** entry) { const CodeRange& codeRange = *code.lookupRange(*entry); void* from = code.segment().base() + codeRange.funcNonProfilingEntry(); void* to = code.segment().base() + codeRange.funcProfilingEntry(); if (!profilingEnabled) Swap(from, to); MOZ_ASSERT(*entry == from); *entry = to; }
void GuessResponse::setCorrectNumber(const Code &secret, const Code &guess) { // iterate through each, marking when a code place is accounted for for (int i = 0; i < secret.GetLength(); i++) { if (secret.GetValue(i) == guess.GetValue(i)) { _numberCorrect++; guessCodeMemberAccountedFor[i] = true; secretCodeMemberAccountedFor[i] = true; } } }
void CodeEditorWindow::onSave() { Code *pNewCode = new Code(m_codeEditor->toHtml(), m_keywordsEditor.text()); if (m_editCode == nullptr) { pNewCode->setCreateTime(QDateTime::currentMSecsSinceEpoch()); pCodeBase->add(pNewCode); } else { pNewCode->copyMembers(m_editCode); pCodeBase->add(pNewCode, m_editCode->getId()); } close(); codePaster->save(); }
bool Thread::step() { if (state == RUN) { Thread::Frame& frame = frames.back(); ThunkPrototype* thunk = frame.thunk->thunkPrototype(); size_t nextInstr = frame.nextInstr; Code* code = thunk->body[nextInstr]; frame.nextInstr++; // Uncomment to get *verbose* debug info on scripting /*cout << thunk; for (size_t i = 0; i < frames.size(); ++i) cout << "[" << frames[i].stack.size() << "]"; cout << " " << usl->debug.find(thunk, nextInstr) << ": "; code->dump(cout); cout << endl;*/ code->execute(this); while (true) { Thread::Frame& frame = frames.back(); if (frame.nextInstr < frame.thunk->thunkPrototype()->body.size()) break; Value* retVal = frame.stack.back(); frames.pop_back(); if (!frames.empty()) { frames.back().stack.push_back(retVal); } else { #ifdef DEBUG_USL retVal->dump(cout); cout << endl; #endif state = STOP; break; } } return true; } else { return false; } }
bool LWZState::Init( RageFile &f ) { unsigned char input_code_size; /* code size: */ if( !ReadOK(f, &input_code_size, 1) ) { // RWSetMsg("EOF / read error on image data"); return false; } set_code_size = input_code_size; code_size = set_code_size + 1; clear_code = 1 << set_code_size; end_code = clear_code + 1; max_code_size = 2 * clear_code; max_code = clear_code + 2; m_Code.Init(); fresh = true; memset( table, 0, sizeof(table) ); for( int i = 0; i < clear_code; ++i ) table[1][i] = i; sp = stack; return true; }
bool CodeStack::insert(const Code& code) { #ifdef _DEBUG_ clog << "[InsertCode]" << code.name << ":" << code.rs << ":" << code.rd << ": " << code.rt << ":" << code.Imd() << endl; clog << "[InsertCode]" << code.lb << ":" << code.Dirt() << endl; #endif if (! validate(code)) { // Ìî¿Ó std::cerr << "[debug][codeformat]" << cf << endl; return false; } origin.push_back(code); return true; }
HSAIL_C_API brig_code_section_offset brig_container_find_code_module_symbol_offset(brig_container_t handle, const char *symbol_name) { BrigContainer& c = ((Api*)handle)->container; for (Code d = c.code().begin(), e = c.code().end(); d != e; ) { if (DirectiveExecutable e = d) { if (e.name().str() == symbol_name) { return e.brigOffset(); } d = e.nextModuleEntry(); // Skip to next top level directive. } else if (DirectiveVariable v = d) { if (v.name().str() == symbol_name) { return v.brigOffset(); } d = d.next(); // Skip to next directive. } else { d = d.next(); // Skip to next directive. } } return 0; }
// Game loop int main(int argc, const char * argv[]) { Code code = Code::random(); cout << "Code to break: " << code.repr() << endl; string line; while (true) { cout << "> "; getline(cin, line); Code guess = Code(line); Response response = code.respond(guess); cout << "\t\t" << guess.repr() << ": " << response.repr() << endl; if (response.isSolved()) { cout << "You won!" << endl; return 0; } } }
QString Microbe::compile( const QString & url, bool optimize ) { QFile file( url ); if( file.open( IO_ReadOnly ) ) { QTextStream stream(&file); unsigned line = 0; while( !stream.atEnd() ) m_program += SourceLine( stream.readLine(), url, line++ ); file.close(); simplifyProgram(); } else { m_errorReport += i18n("Could not open file '%1'\n").arg(url); return 0; } Parser parser(this); // Extract the PIC ID if ( !m_program.isEmpty() ) { m_picType = PIC14::toType( m_program[0].text() ); m_program.remove( m_program.begin() ); } PIC14 * pic = makePic(); if ( !pic ) return 0; Code * code = parser.parse( m_program ); pic->setCode( code ); pic->addCommonFunctions( (PIC14::DelaySubroutine)m_maxDelaySubroutine ); pic->postCompileConstruct( m_usedInterrupts ); code->postCompileConstruct(); if ( optimize ) { Optimizer opt; opt.optimize( code ); } return code->generateCode( pic ); }
unsigned _nth(Env& env) { //if (not has_elements<Code>(env,1)) return 1; //if (not has_elements<int>(env,1)) return 1; Code first = pop<Code>(env); int val = pop<int>(env); if (first->get_stack().size() == 0) { // nil or atom push(env, first); return 1; } const CodeArray& stack = first->get_stack(); val = std::abs(val) % stack.size(); push(env, stack[stack.size()-val-1]); return first->len(); }
int ExtractCode( Image < uint8 > & DATANOR, Code < float > & codedata, Matrix < float > & Feat, struct osiris_parameter & param, Filtres < float > & Gabor, Appoints & points ) { Feat = Featextract( DATANOR, Gabor, points ); codedata.SetSize( Feat.GetRows(), Feat.GetCols() ); codedata.TMPLATE = TMextract( Feat ); codedata.MASK.init( 1 ); return ( 0 ); }
bool CodeTable::exists(Code &c) { unsigned hash = c.hash() % CODE_TABLE_SIZE; for (Deque<Code*>::iterator i = table[hash].begin(); i != table[hash].end(); i++) if ((*i)->equiv(c)) return true; return false; }
void bench(int mode) { const int N = 100000; Code code; code.makeBench(N, mode); int (*p)(uint64_t*, const uint64_t*, const uint64_t*) = (int (*)(uint64_t*, const uint64_t*, const uint64_t*))code.getCode(); uint64_t a[4] = { uint64_t(-1), uint64_t(-2), uint64_t(-3), 544443221 }; uint64_t b[4] = { uint64_t(-123), uint64_t(-3), uint64_t(-4), 222222222 }; uint64_t c[5] = { 0, 0, 0, 0, 0 }; const int M = 100; Xbyak::util::Clock clk; for (int i = 0; i < M; i++) { clk.begin(); p(c, a, b); clk.end(); } printf("%.2fclk\n", clk.getClock() / double(M) / double(N) / innerN); }
int compile(const char *program, const char *filename, unsigned options) { file::buffer_t file = file::read_file(filename); if (!file.data || !file.size) { fprintf(stderr, "Can't find file or file is empty.\n"); usage(program); } Reader reader; Object *e = reader.read((const char *)file.data); ScriptCompiler sc(Object::cast_pair(e), true); Code *c = sc.compile_code(); int r = 0; if (!c) { printf("\nCompile failed!\n"); r = -1; } else { FileOutStream out(format::format("%s.sbin", filename).c_str()); c->write(out); out.close(); if ((options & LISTING) != 0) { std::string s = Disassembler::disassemble(c); FileOutStream listing(format::format("%s.lst", filename).c_str()); listing.write(s.data(), s.size()); listing.close(); } } delete [] file.data; return r; }
unsigned _nthcdr(Env& env) { //if (not has_elements<Code>(env,1)) return 1; //if (not has_elements<int>(env,1)) return 1; Code first = pop<Code>(env); int val = pop<int>(env); if (first->get_stack().size() == 0) { // nil or atom push(env, first); return 1; } CodeArray stack = first->get_stack(); val = std::abs(val) % stack.size(); while(--val >= 0) stack.pop_back(); push(env, Code(new CodeList(stack))); return first->len(); }
static inline void AssertMatchesCallSite(const WasmActivation& activation, void* callerPC, void* callerFP, void* fp) { #ifdef DEBUG Code* code = activation.compartment()->wasm.lookupCode(callerPC); MOZ_ASSERT(code); const CodeRange* callerCodeRange = code->lookupRange(callerPC); MOZ_ASSERT(callerCodeRange); if (callerCodeRange->kind() == CodeRange::Entry) { MOZ_ASSERT(callerFP == nullptr); return; } const CallSite* callsite = code->lookupCallSite(callerPC); MOZ_ASSERT(callsite); MOZ_ASSERT(callerFP == (uint8_t*)fp + callsite->stackDepth()); #endif }
PhaseScope::PhaseScope(Code& code, const char* name) : m_code(code) , m_name(name) { if (shouldDumpIRAtEachPhase()) { dataLog("Air after ", code.lastPhaseName(), ", before ", name, ":\n"); dataLog(code); } if (shouldSaveIRBeforePhase()) m_dumpBefore = toCString(code); }
// Compares guess to secret code and returns the number of correct digits // in the correct locations. int Code::checkCorrect(Code& guess) { const int secretCodeLength = getLength(); int count = 0; vector<bool> correct(secretCodeLength, false); /// check vector lengths if (guess.getLength() != secretCodeLength) { throw InvalidVectSize("Code::checkCorrect - vectors are not the same length!"); } for (int i = 0; i < guess.getLength(); i++) { if (getCode()[i] == guess.getCode()[i]) { // mark both the secret and guess code index values if used correct[i] = true; count++; } } setUsed(correct); guess.setUsed(correct); return count; }
unsigned _position(Env& env) { //if (not has_elements<Code>(env,2)) return 1; Code first = pop<Code>(env); Code second = pop<Code>(env); if (first->get_stack().size() == 0) { if (equal_to(first,second)) push(env, 0); else push(env,-1); return 1; } const CodeArray& stack = first->get_stack(); for (int i = stack.size()-1;i >= 0; --i) { if (equal_to(stack[i], second)) { push<int>(env, stack.size()-i-1); return stack.size() * second->len(); } } push<int>(env, -1); return stack.size() * second->len(); }
QString code2str (const Code &code) { /// Converts bitarray to string QString str; for (int i = 0; i < code.size (); ++i) { str += code[i] ? "1" : "0"; } return str; }