int Scanner::GetToken(int *sub) { *sub = 0; skip_non_token(); // char c = cur_char(); if (is_dec(c)) { return read_num(); } // struct OperatorTableEntry *op = lookup_op(); if (op) { int r; r = read_op(op); *sub = op->sub_op; return r; } // if (c == '\"') { return read_str(); } if (is_symhead(c)) { return read_sym(); } return -1; }
static void validate_block(Validator *val) { int block_id = val->cur_mark[1]; switch (block_id) { case 'a': { MVMuint16 index; ensure_op(val, MVM_OP_prepargs); validate_operands(val); index = GET_UI16(val->cur_op, -2); val->cur_call = val->cu->body.callsites[index]; val->cur_arg = 0; val->expected_named_arg = 0; val->remaining_positionals = val->cur_call->num_pos; break; } default: fail(val, MSG(val, "unknown instruction block '%c'"), block_id); } while (val->cur_op < val->bc_end) { int type, id; read_op(val); type = val->cur_mark[0]; id = val->cur_mark[1]; if (id != block_id) fail(val, MSG(val, "expected instruction marked '%c' but got '%c'"), block_id, id); switch (type) { case MARK_body: break; case MARK_tail: goto terminate_block; default: fail_illegal_mark(val); } switch (block_id) { case 'a': validate_operands(val); validate_arg(val); break; } } terminate_block: switch (block_id) { case 'a': validate_operands(val); ensure_no_remaining_positionals(val); break; } }
static void validate_sequence(Validator *val) { int seq_id = val->cur_mark[1]; switch (seq_id) { case 'j': { ensure_op(val, MVM_OP_jumplist); validate_operands(val); val->remaining_jumplabels = (MVMuint32)MVM_BC_get_I64(val->cur_op, -10); break; } default: fail(val, MSG(val, "unknown instruction sequence '%c'"), seq_id); } while (val->cur_op < val->bc_end) { int type, id; read_op(val); type = val->cur_mark[0]; id = val->cur_mark[1]; switch (type) { case MARK_special: if (id == seq_id) break; else; /* fallthrough */ case MARK_regular: case MARK_sequence: case MARK_head: unread_op(val); goto terminate_seq; default: fail_illegal_mark(val); } switch (seq_id) { case 'j': ensure_op(val, MVM_OP_goto); validate_operands(val); val->remaining_jumplabels--; if (val->remaining_jumplabels == 0) goto terminate_seq; break; } } terminate_seq: switch (seq_id) { case 'j': ensure_no_remaining_jumplabels(val); break; } }
gf8 do_read( char * s, int * index ) { char op = 0; skip(' ',s,index); if (is_num(s[*index])) return read_number(s,index); if (is_op(s[*index])) { op = s[*index];*index = (*index) + 1; return read_op(op,s,index); } if (s[*index] == 0) return 0; if (s[*index] == '\n') { *index = *index + 1; return do_read(s,index); } printf("Unknown character %c\n",s[*index]); exit(-1); }
void hcd62121_cpu_device::execute_run() { do { UINT32 pc = ( m_cseg << 16 ) | m_ip; UINT8 op; debugger_instruction_hook(this, pc); m_prev_pc = pc; op = read_op(); m_icount -= 4; switch ( op ) { #include "hcd62121_ops.h" }; } while (m_icount > 0); }
static CPU_EXECUTE( hcd62121 ) { hcd62121_state *cpustate = get_safe_token(device); do { UINT32 pc = ( cpustate->cseg << 16 ) | cpustate->ip; UINT8 op; debugger_instruction_hook(device, pc); cpustate->prev_pc = pc; op = read_op( cpustate ); cpustate->icount -= 4; switch ( op ) { #include "hcd62121_ops.h" }; } while (cpustate->icount > 0); }
void main() { BYTE type; float op2; puts("FP RPN Calculator"); sp = 0; pos = 0; while((type = read_op(s)) != 0) { switch(type) { case NUMBER: push(f); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if(op2 != 0) push(pop() / op2); else puts("Divide by 0"); break; case '\n': printf("==> %f\n", top()); break; } } }
int read_operator(char **stream, t_token *token) { size_t i; static t_token operators[] = { {OP_SEMI_COLON, ";"}, {OP_LREDIR, "<"}, {OP_DRREDIR, ">>"}, {OP_RREDIR, ">"}, {OP_OR, "||"}, {OP_PIPE, "|"}, {OP_AND, "&&"}, {OP_AMPERSAND, "&"} }; i = 0; while (i < sizeof(operators) / sizeof(t_token)) { if (read_op(stream, operators + i, token)) return (1); ++i; } return (0); }
/* Validate that a static frame's bytecode is executable by the interpreter. */ void MVM_validate_static_frame(MVMThreadContext *tc, MVMStaticFrame *static_frame) { MVMStaticFrameBody *fb = &static_frame->body; Validator val[1]; val->tc = tc; val->cu = fb->cu; val->frame = static_frame; val->loc_count = fb->num_locals; val->loc_types = fb->local_types; val->bc_size = fb->bytecode_size; val->src_cur_op = fb->bytecode; val->src_bc_end = fb->bytecode + fb->bytecode_size; val->labels = MVM_calloc(fb->bytecode_size, 1); val->cur_info = NULL; val->cur_mark = NULL; val->cur_instr = 0; val->cur_call = NULL; val->cur_arg = 0; val->expected_named_arg = 0; val->remaining_positionals = 0; val->remaining_jumplabels = 0; val->reg_type_var = 0; #ifdef MVM_BIGENDIAN assert(fb->bytecode == fb->orig_bytecode); val->bc_start = MVM_malloc(fb->bytecode_size); memset(val->bc_start, 0xDB, fb->bytecode_size); fb->bytecode = val->bc_start; #else val->bc_start = fb->bytecode; #endif val->bc_end = val->bc_start + fb->bytecode_size; val->cur_op = val->bc_start; while (val->cur_op < val->bc_end) { read_op(val); if (val->cur_mark && val->cur_mark[0] == 's') fail(val, MSG(val, "Illegal appearance of spesh op")); switch (val->cur_mark[0]) { case MARK_regular: case MARK_special: validate_operands(val); break; case MARK_sequence: validate_sequence(val); break; case MARK_head: validate_block(val); break; default: fail_illegal_mark(val); } } validate_branch_targets(val); validate_final_return(val); /* Validation successful. Cache the located instruction offsets. */ fb->instr_offsets = val->labels; }
Token* Scanner::get_token() { Token* t; if (tokens) { t = tokens->token; tokens = tokens->prev; } else { t = NULL; while (t == NULL) { fint c = get_char(); switch (c) { case EOF: depth = 0; t = new Token(Token::ACCEPT, line, column, sourceAddr()); break; case '\n': case '\r': if (depth <= 0 && !is_string_scanner()) { t = new Token(Token::ACCEPT, line, column - 1, sourceAddr() - 1); depth = 0; } break; case ' ': case '\t': case '\v': case '\b': case '\f': break; case '"': t = skip_comment(); break; case '(': depth ++; t = new Token(as_TokenType('('), line, column - 1, sourceAddr() - 1); break; case ')': depth --; t = new Token(as_TokenType(')'), line, column - 1, sourceAddr() - 1); break; case '[': depth ++; t = new Token(as_TokenType('['), line, column - 1, sourceAddr() - 1); break; case ']': depth --; t = new Token(as_TokenType(']'), line, column - 1, sourceAddr() - 1); break; case '.': t = read_dot(); break; case '\'': t = read_string(); break; case '\\': c = get_char(); if (c == '\n' || c == '\r') { // an escaped newline; ignore } else { push_char(c); c = '\\'; t = read_op(c); } break; case '{': t = new Token(Token::ANNOTATION_START, line, column - 1, sourceAddr() - 1); break; case '}': t = new Token(Token::ANNOTATION_END, line, column - 1, sourceAddr() - 1); break; default: if (is_digit(c) || c == '-') t = read_number(c); else if (is_id_alpha(c) || c == ':') t = read_name(c); else if (is_punct(c)) t = read_op(c); else t = TokenizingError("unknown character in input"); } } } if (t && PrintTokens) t->print(); return t; }
Token* Scanner::read_number(fint c) { const int MAXSELFINT = smiOop_max->value() + 1; // maximum Self integer + 1 fint l = line; fint col = column - 1; const char* ss = sourceAddr() - 1; bool neg = false; Token::TokenType t = Token::INTEGER; if (c == '-') { neg = true; c = get_char(); if (!is_digit(c)) { push_char(c); return read_op('-'); } } int32 i = asnum(c); fint base; double f = 0; if (c == '0') { if (c = get_char(), c == 'x' || c == 'X') { return TokenizingError("invalid numeric constant (use 16r...)"); } else if (is_digit(c)) { return TokenizingError("C-style octals are not supported (use 8r...)"); } else { push_char(c); } } while (is_digit(c = get_char())) { // read decimal number /* The following test is necessary; else i*10 may overflow. */ if (i > MAXSELFINT / 10) return TokenizingError("numeric constant too large"); i *= 10; i += asnum(c); if (i > MAXSELFINT || (i == MAXSELFINT && !neg)) { return TokenizingError("numeric constant too large"); } } if (c == 'r' || c == 'R') { // base specification ? c = get_char(); if (i < 2 || i > 36) { return TokenizingError("illegal base for numeric literal"); } base = i; i = 0; if (is_alphadigit(c)) { do { fint v = asnum(c); if (v >= base) { if (is_digit(c)) { return TokenizingError("digit too large for given base"); } else { return TokenizingError("need spaces between numeric literals and selectors\n\t(letter too large for given base)"); } } int32 newi = i * base + v; int32 maxi = MAXSELFINT / base; if ((i > maxi || (i == maxi && !neg)) || (newi > MAXSELFINT || (newi == MAXSELFINT && !neg))) { return TokenizingError("numeric constant too large"); } i = newi; c = get_char(); } while (is_digit(c) || (base > 10 && is_alphadigit(c))); } else { return TokenizingError("expecting a number after the base specifier"); } } else { if (is_alpha(c) && c != 'e' && c != 'E') { return TokenizingError("need spaces between numeric literals and selectors"); } if (c == '.') { // read fraction part c = get_char(); if (is_digit(c)) { t = Token::FLOAT; double place = 1.0; f = (double) i; do { fint v = asnum(c); f += v * (place /= 10.0); } while (c = get_char(), is_digit(c)); } else { push_char(c); c = '.'; } } if (c == 'e' || c == 'E') { // read exponent if (t == Token::INTEGER) { t = Token::FLOAT; f = (double) i; } bool sign = false; c = get_char(); if (c == '+') { c = get_char(); } else if (c == '-') { c = get_char(); sign = true; } fint exp = asnum(c); if (! is_digit(c)) { return TokenizingError("illegal exponent"); } while (is_alphadigit(c = get_char())) { if (! is_digit(c)) { return TokenizingError("illegal exponent"); } exp *= 10; exp += asnum(c); } if (sign) { while (exp --) f /= 10.0; } else { while (exp --) f *= 10.0; } } } push_char(c); if (neg) { if (t == Token::INTEGER) i = - i; else f = - f; } if (t == Token::INTEGER) { return new Token(t, ss, i, l, col); } else { assert(t == Token::FLOAT, "unexpected token type"); return new Token(t, f, l, col, ss); } }
int main(int argc, char *argv[]) { try { po::variables_map vm; po::options_description generic("options"); generic.add_options() ("help,h", "show this help") ("trees,t", po::value<uint32_t>(), "the number of trees"); po::options_description cmdline_options; cmdline_options.add(generic); po::store(po::command_line_parser(argc, argv). options(cmdline_options).run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << generic << std::endl; return 0; } if (vm.count("trees")) { uint32_t trees = vm["trees"].as<uint32_t>(); forest.init(trees); } else { std::cout << "error: the number of tree is required" << std::endl; return -1; } } catch (std::exception &e) { std::cout << e.what() << std::endl; return -1; } std::string str1, str2; while (std::cin) { std::string line; std::getline(std::cin, line); if (line.empty()) continue; switch (state) { case SIM_OP: read_op(line); break; case SIM_ADD_STR: str1 = line; state = SIM_ADD_HASH; std::cout << "input the hash file" << std::endl; break; case SIM_ADD_HASH: str2 = line; state = SIM_ADD_HIST; std::cout << "input the histogram file" << std::endl; break; case SIM_ADD_HIST: add_hash(str1, str2, line); state = SIM_OP; break; case SIM_DEL: forest.remove_hash(line); std::cout << "true" << std::endl; state = SIM_OP; break; case SIM_GET_HASH: str1 = line; state = SIM_GET_FEAT; std::cout << "input the histogram file" << std::endl; break; case SIM_GET_FEAT: get_similar(str1, line); state = SIM_OP; break; case SIM_THRESHOLD: try { forest.set_threshold(boost::lexical_cast<float>(line)); std::cout << "true" << std::endl; } catch (...) { std::cout << "false" << std::endl; } state = SIM_OP; break; } } return 0; }
int main(int argc, char **argv) { int err; struct capfs_upcall up; struct capfs_downcall down; struct capfs_dirent *dent = NULL; char *link_name = NULL; int opt = 0; int capfsd_log_level = CRITICAL_MSG | WARNING_MSG; char options[256]; struct cas_options cas_options = { doInstrumentation:0, use_sockets:0, }; #ifdef DEBUG capfsd_log_level |= INFO_MSG; capfsd_log_level |= DEBUG_MSG; #endif set_log_level(capfsd_log_level); /* capfsd must register a callback with the meta-data server at the time of mount */ check_for_registration = 1; while((opt = getopt(argc, argv, "dhsn:p:")) != EOF) { switch(opt){ case 's': cas_options.use_sockets = 1; break; case 'd': is_daemon = 0; break; case 'p': err = sscanf(optarg, "%x", &capfs_debug); if(err != 1){ usage(); exiterror("bad arguments"); exit(1); } break; case 'n': num_threads = atoi(optarg); break; case 'h': usage(); exit(0); case '?': default: usage(); exiterror("bad arguments"); exit(1); } } if (getuid() != 0 && geteuid() != 0) { exiterror("must be run as root"); exit(1); } if (setup_capfsdev() < 0) { exiterror("setup_capfsdev() failed"); exit(1); } if ((dev_fd = open_capfsdev()) < 0) { exiterror("open_capfsdev() failed"); exit(1); } startup(argc, argv); /* Initialize the plugin interface */ capfsd_plugin_init(); capfs_comm_init(); /* allocate a 64K, page-aligned buffer for small operations */ capfs_opt_io_size = ROUND_UP(CAPFS_OPT_IO_SIZE); if ((orig_iobuf = (char *) valloc(capfs_opt_io_size)) == NULL) { exiterror("calloc failed"); capfsd_plugin_cleanup(); exit(1); } memset(orig_iobuf, 0, capfs_opt_io_size); capfs_dent_size = ROUND_UP((FETCH_DENTRY_COUNT * sizeof(struct capfs_dirent))); /* allocate a suitably large dent buffer for getdents speed up */ if ((dent = (struct capfs_dirent *) valloc(capfs_dent_size)) == NULL) { exiterror("calloc failed"); capfsd_plugin_cleanup(); exit(1); } memset(dent, 0, capfs_dent_size); /* maximum size of a link target cannot be > 4096 */ capfs_link_size = ROUND_UP(4096); link_name = (char *) valloc(capfs_link_size); if(!link_name) { exiterror("calloc failed"); capfsd_plugin_cleanup(); exit(1); } memset(link_name, 0, capfs_link_size); fprintf(stderr, "------------ Starting client daemon servicing VFS requests using a thread pool [%d threads] ----------\n", num_threads); /* * Start up the local RPC service on both TCP/UDP * for callbacks. */ pmap_unset(CAPFS_CAPFSD, clientv1); if (setup_service(CAPFS_CAPFSD /* program number */, clientv1 /* version */, -1 /* both tcp/udp */, -1 /* any available port */, CAPFS_DISPATCH_FN(clientv1) /* dispatch routine */, &info) < 0) { exiterror("Could not setup local RPC service!\n"); capfsd_plugin_cleanup(); exit(1); } /* * Initialize the hash cache. * Note that we are using default values of cache sizes, * and this should probably be an exposed knob to the user. * CMGR_BSIZE is == CAPFS_MAXHASHLENGTH for SHA1-hash. So we dont need to set * that. We use environment variables to communicate the parameters * to the caches. */ snprintf(options, 256, "%d", CAPFS_CHUNK_SIZE); setenv("CMGR_CHUNK_SIZE", options, 1); snprintf(options, 256, "%d", CAPFS_HCACHE_COUNT); setenv("CMGR_BCOUNT", options, 1); init_hashes(); #if 0 /* * Initialize the client-side data cache. * Note that we are not using this layer * right now. It is getting fairly complicated already. */ snprintf(options, 256, "%d", CAPFS_DCACHE_BSIZE); setenv("CMGR_BSIZE", options, 1); snprintf(options, 256, "%d", CAPFS_DCACHE_COUNT); setenv("CMGR_BCOUNT", options, 1); #endif /* * Initialize the client-side data server communication * stuff. */ clnt_init(&cas_options, num_threads, CAPFS_CHUNK_SIZE); /* loop forever, doing: * - read from device * - service request * - write back response */ for (;;) { struct timeval begin, end; err = read_capfsdev(dev_fd, &up, 30); if (err < 0) { /* cleanup the hash cache */ cleanup_hashes(); /* Cleanup the RPC service */ cleanup_service(&info); capfs_comm_shutdown(); close_capfsdev(dev_fd); /* cleanup the plugins */ capfsd_plugin_cleanup(); /* cleanup the client-side stuff */ clnt_finalize(); exiterror("read failed\n"); exit(1); } if (err == 0) { /* timed out */ capfs_comm_idle(); continue; } gettimeofday(&begin, NULL); /* the do_capfs_op() call does this already; can probably remove */ init_downcall(&down, &up); err = 0; switch (up.type) { /* all the easy operations */ case GETMETA_OP: case SETMETA_OP: case LOOKUP_OP: case CREATE_OP: case REMOVE_OP: case RENAME_OP: case SYMLINK_OP: case MKDIR_OP: case RMDIR_OP: case STATFS_OP: case HINT_OP: case FSYNC_OP: case LINK_OP: { PDEBUG(D_UPCALL, "read upcall; type = %d, name = %s\n", up.type, up.v1.fhname); err = do_capfs_op(&up, &down); if (err < 0) { PDEBUG(D_LIB, "do_capfs_op failed for type %d\n", up.type); } break; /* the more interesting ones */ } case GETDENTS_OP: /* need to pass location and size of buffer to do_capfs_op() */ up.xfer.ptr = dent; up.xfer.size = capfs_dent_size; err = do_capfs_op(&up, &down); if (err < 0) { PDEBUG(D_LIB, "do_capfs_op failed for getdents\n"); } break; case READLINK_OP: /* need to pass location and size of buffer to hold the target name */ up.xfer.ptr = link_name; up.xfer.size = capfs_link_size; err = do_capfs_op(&up, &down); if(err < 0) { PDEBUG(D_LIB, "do_capfs_op failed for readlink\n"); } break; case READ_OP: err = read_op(&up, &down); if (err < 0) { PDEBUG(D_LIB, "read_op failed\n"); } break; case WRITE_OP: err = write_op(&up, &down); if (err < 0) { PDEBUG(D_LIB, "do_capfs_op failed\n"); } break; /* things that aren't done yet */ default: err = -ENOSYS; break; } gettimeofday(&end, NULL); /* calculate the total time spent servicing this call */ if (end.tv_usec < begin.tv_usec) { end.tv_usec += 1000000; end.tv_sec--; } end.tv_sec -= begin.tv_sec; end.tv_usec -= begin.tv_usec; down.total_time = (end.tv_sec * 1000000 + end.tv_usec); down.error = err; switch(up.type) { case HINT_OP: /* this is a one shot hint, we don't want a response in case of HINT_OPEN/HINT_CLOSE */ if (up.u.hint.hint == HINT_CLOSE || up.u.hint.hint == HINT_OPEN) { err = 0; break; } /* fall through */ default: /* the default behavior is to write a response to the device */ err = write_capfsdev(dev_fd, &down, -1); if (err < 0) { /* cleanup the hash cache */ cleanup_hashes(); /* Cleanup the RPC service */ cleanup_service(&info); capfs_comm_shutdown(); close_capfsdev(dev_fd); /* Cleanup the plugins */ capfsd_plugin_cleanup(); /* cleanup the client-side stuff */ clnt_finalize(); exiterror("write failed"); exit(1); } break; } /* If we used a big I/O buffer, free it after we have successfully * returned the downcall. */ if (big_iobuf != NULL) { free(big_iobuf); big_iobuf = NULL; } } /* Not reached */ /* cleanup the hash cache */ cleanup_hashes(); /* Cleanup the RPC service */ cleanup_service(&info); capfs_comm_shutdown(); close_capfsdev(dev_fd); /* cleanup the plugins */ capfsd_plugin_cleanup(); /* cleanup the client-side stuff */ clnt_finalize(); exit(1); }
void read_op(sm4_op* pop) { sm4_op& op = *pop; sm4_token_operand optok; read_token(&optok); assert(optok.file < SM4_FILE_COUNT); op.swizzle[0] = 0; op.swizzle[1] = 1; op.swizzle[2] = 2; op.swizzle[3] = 3; op.mask = 0xf; switch(optok.comps_enum) { case SM4_OPERAND_COMPNUM_0: op.comps = 0; break; case SM4_OPERAND_COMPNUM_1: op.comps = 1; break; case SM4_OPERAND_COMPNUM_4: op.comps = 4; op.mode = optok.mode; switch(optok.mode) { case SM4_OPERAND_MODE_MASK: op.mask = SM4_OPERAND_SEL_MASK(optok.sel); break; case SM4_OPERAND_MODE_SWIZZLE: op.swizzle[0] = SM4_OPERAND_SEL_SWZ(optok.sel, 0); op.swizzle[1] = SM4_OPERAND_SEL_SWZ(optok.sel, 1); op.swizzle[2] = SM4_OPERAND_SEL_SWZ(optok.sel, 2); op.swizzle[3] = SM4_OPERAND_SEL_SWZ(optok.sel, 3); break; case SM4_OPERAND_MODE_SCALAR: op.swizzle[0] = op.swizzle[1] = op.swizzle[2] = op.swizzle[3] = SM4_OPERAND_SEL_SCALAR(optok.sel); break; } break; case SM4_OPERAND_COMPNUM_N: fail("Unhandled operand component type"); } op.file = (sm4_file)optok.file; op.num_indices = optok.num_indices; if(optok.extended) { sm4_token_operand_extended optokext; read_token(&optokext); if(optokext.type == 0) {} else if(optokext.type == 1) { op.neg = optokext.neg; op.abs= optokext.abs; } else fail("Unhandled extended operand token type"); } for(unsigned i = 0; i < op.num_indices; ++i) { unsigned repr; if(i == 0) repr = optok.index0_repr; else if(i == 1) repr = optok.index1_repr; else if(i == 2) repr = optok.index2_repr; else fail("Unhandled operand index representation"); op.indices[0].disp = 0; // TODO: is disp supposed to be signed here?? switch(repr) { case SM4_OPERAND_INDEX_REPR_IMM32: op.indices[i].disp = (int32_t)read32(); break; case SM4_OPERAND_INDEX_REPR_IMM64: op.indices[i].disp = read64(); break; case SM4_OPERAND_INDEX_REPR_REG: relative: op.indices[i].reg.reset(new sm4_op()); read_op(&*op.indices[0].reg); break; case SM4_OPERAND_INDEX_REPR_REG_IMM32: op.indices[i].disp = (int32_t)read32(); goto relative; case SM4_OPERAND_INDEX_REPR_REG_IMM64: op.indices[i].disp = read64(); goto relative; } } if(op.file == SM4_FILE_IMMEDIATE32) { for(unsigned i = 0; i < op.comps; ++i) op.imm_values[i].i32 = read32(); } else if(op.file == SM4_FILE_IMMEDIATE64) { for(unsigned i = 0; i < op.comps; ++i) op.imm_values[i].i64 = read64(); } }