static void fast_read_loader_definition(lref_t reader, enum fasl_opcode_t opcode) { lref_t symbol_to_define; fast_read(reader, &symbol_to_define, false); if (!SYMBOLP(symbol_to_define)) vmerror_fast_read("Expected symbol for definition", reader, symbol_to_define); lref_t definition; fast_read(reader, &definition, false); dscwritef(DF_SHOW_FAST_LOAD_FORMS, (_T("; DEBUG: FASL defining ~s = ~s\n"), symbol_to_define, definition)); switch (opcode) { case FASL_OP_LOADER_DEFINEQ: /* quoted definition, do nothing. */ break; case FASL_OP_LOADER_DEFINEA0: definition = apply1(definition, 0, NULL); break; default: panic("invalid opcode in fast_read_loader_definition"); break; } lidefine_global(symbol_to_define, definition); }
void unwind_stack_for_throw() { for(lref_t *frame = CURRENT_TIB()->frame; frame != NULL; frame = fstack_prev_frame(frame)) { if (fstack_frame_type(frame) == FRAME_UNWIND) { dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: throw invoking unwind, frame: ~c&\n"), frame)); apply1(frame[FOFS_UNWIND_AFTER], 0, NULL); continue; } if (fstack_frame_type(frame) != FRAME_ESCAPE) continue; if (frame == CURRENT_TIB()->escape_frame) { jmp_buf *jmpbuf = (jmp_buf *)frame[FOFS_ESCAPE_JMPBUF_PTR]; dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: longjmp to frame: ~c&, jmpbuf: ~c&\n"), frame, jmpbuf)); CURRENT_TIB()->escape_frame = NULL; CURRENT_TIB()->frame = (lref_t *)frame[FOFS_ESCAPE_FRAME]; CURRENT_TIB()->fsp = CURRENT_TIB()->frame + 1; longjmp(*jmpbuf, 1); } } }
void unwind_stack_for_throw() { for(lref_t *frame = CURRENT_TIB()->frame; frame != NULL; frame = fstack_prev_frame(frame)) { if (fstack_frame_type(frame) == FRAME_UNWIND) { dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: throw invoking unwind : ~c&\n"), frame)); apply1(frame[FOFS_UNWIND_AFTER], 0, NULL); continue; } if (fstack_frame_type(frame) != FRAME_ESCAPE) continue; if (frame == CURRENT_TIB()->escape_frame) { dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: setjmp (from fsp=~c&) to target frame: ~c&\n"), CURRENT_TIB()->fsp, frame)); CURRENT_TIB()->escape_frame = NULL; CURRENT_TIB()->frame = (lref_t *)frame[FOFS_ESCAPE_FRAME]; CURRENT_TIB()->fsp = CURRENT_TIB()->frame + 1; longjmp((struct __jmp_buf_tag *)frame[FOFS_ESCAPE_JMPBUF_PTR], 1); } } }
int main() { int i, j, t, s, q; for (scanf("%d", &t); t-- > 0 && scanf("%d", &n) == 1;) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &a[i][j]); m = (n + 1) / 2; for (i = 0; i < m && scanf("%d", &s) == 1; i++) { while (s-- > 0 && scanf("%d", &q) == 1) { if (q == 1) apply1(i); if (q == 2) apply2(i); if (q == 3) apply3(i); if (q == 4) apply4(i); } } for (i = 0; i < n; i++) for (j = 0; j < n; j++) printf(((j + 1) < n) ? "%d " : "%d\n", a[i][j]); } return 0; }
static void apply(ElementType const & element_in, MeshTypeOut & segment_out, EdgeRefinementFlagAccessor const edge_refinement_flag_accessor, VertexToVertexHandleAccessor const vertex_to_vertex_handle_accessor, EdgeToVertexHandleAccessor edge_to_vertex_handle_accessor) { typedef typename viennagrid::result_of::const_element_range<ElementType, viennagrid::line_tag>::type EdgeOnCellRange; typedef typename viennagrid::result_of::iterator<EdgeOnCellRange>::type EdgeOnCellIterator; std::size_t edges_to_refine = 0; EdgeOnCellRange edges_on_cell(element_in); for (EdgeOnCellIterator eocit = edges_on_cell.begin(); eocit != edges_on_cell.end(); ++eocit) { if ( edge_refinement_flag_accessor(*eocit) ) ++edges_to_refine; } switch (edges_to_refine) { case 0: apply0(element_in, segment_out, edge_refinement_flag_accessor, vertex_to_vertex_handle_accessor, edge_to_vertex_handle_accessor); break; case 1: apply1(element_in, segment_out, edge_refinement_flag_accessor, vertex_to_vertex_handle_accessor, edge_to_vertex_handle_accessor); break; case 2: apply2(element_in, segment_out, edge_refinement_flag_accessor, vertex_to_vertex_handle_accessor, edge_to_vertex_handle_accessor); break; case 3: apply3(element_in, segment_out, edge_refinement_flag_accessor, vertex_to_vertex_handle_accessor, edge_to_vertex_handle_accessor); break; default: //nothing to do... break; } } //apply()
lref_t vmtrap(enum trap_type_t trap, enum vmt_options_t options, size_t argc, ...) { assert((trap > 0) && (trap <= TRAP_LAST)); assert(argc < ARG_BUF_LEN); dscwritef(DF_SHOW_TRAPS, (_T("; DEBUG: trap : ~cS\n"), trap_type_name(trap))); lref_t handler = interp.trap_handlers[trap]; if (!PROCEDUREP(handler)) { if(!NULLP(handler)) vmtrap_panic(trap, "bad trap handler"); if (!(options & VMT_OPTIONAL_TRAP)) vmtrap_panic(trap, "missing trap handler"); return NIL; } lref_t retval = NIL; va_list args; va_start(args, argc); lref_t argv[ARG_BUF_LEN]; argv[0] = fixcons(trap); argv[1] = fixcons((fixnum_t)CURRENT_TIB()->frame); for (size_t ii = 2; ii < argc + 2; ii++) argv[ii] = va_arg(args, lref_t); va_end(args); retval = apply1(handler, argc + 2, argv); if (options & VMT_HANDLER_MUST_ESCAPE) vmtrap_panic(trap, "trap handler must escape"); return retval; }
lref_t lapply(size_t argc, lref_t argv[]) { size_t fn_argc = 0; lref_t fn_argv[ARG_BUF_LEN]; lref_t fn = (argc > 0) ? argv[0] : NIL; if (!PROCEDUREP(fn)) vmerror_wrong_type_n(1, fn); for (size_t ii = 1; ii < argc - 1; ii++) { if (fn_argc >= ARG_BUF_LEN) break; fn_argv[fn_argc] = argv[ii]; fn_argc++; } lref_t args = (argc > 1) ? argv[argc - 1] : NIL; while (CONSP(args)) { if (fn_argc >= ARG_BUF_LEN) break; fn_argv[fn_argc] = CAR(args); fn_argc++; args = CDR(args); } if (fn_argc >= ARG_BUF_LEN) vmerror_unsupported(_T("too many actual arguments in call to apply")); if (!NULLP(args)) vmerror_arg_out_of_range(args, _T("bad formal argument list")); return apply1(fn, fn_argc, fn_argv); }
lref_t ltime_apply0(lref_t fn) { if (!PROCEDUREP(fn)) vmerror_wrong_type_n(1, fn); flonum_t t = sys_runtime(); flonum_t gc_t = interp.gc_total_run_time; size_t cells = interp.gc_total_cells_allocated; size_t fops = CURRENT_TIB()->count_fop; size_t frames = CURRENT_TIB()->count_enter_frame; lref_t argv[6]; argv[0] = apply1(fn, 0, NULL); argv[1] = flocons(sys_runtime() - t); argv[2] = flocons(interp.gc_total_run_time - gc_t); argv[3] = fixcons(interp.gc_total_cells_allocated - cells); argv[4] = fixcons(CURRENT_TIB()->count_fop - fops); argv[5] = fixcons(CURRENT_TIB()->count_enter_frame - frames); return lvector(6, argv); }
static lref_t execute_fast_op(lref_t fop, lref_t env) { lref_t retval = NIL; lref_t sym; lref_t binding; lref_t fn; lref_t args; size_t argc; lref_t argv[ARG_BUF_LEN]; lref_t after; lref_t tag; lref_t cell; lref_t escape_retval; jmp_buf *jmpbuf; STACK_CHECK(&fop); _process_interrupts(); fstack_enter_eval_frame(&fop, fop, env); while(!NULLP(fop)) { switch(fop->header.opcode) { case FOP_LITERAL: retval = fop->as.fast_op.arg1; fop = fop->as.fast_op.next; break; case FOP_GLOBAL_REF: sym = fop->as.fast_op.arg1; binding = SYMBOL_VCELL(sym); if (UNBOUND_MARKER_P(binding)) vmerror_unbound(sym); retval = binding; fop = fop->as.fast_op.next; break; case FOP_GLOBAL_SET: sym = fop->as.fast_op.arg1; binding = SYMBOL_VCELL(sym); if (UNBOUND_MARKER_P(binding)) vmerror_unbound(sym); SET_SYMBOL_VCELL(sym, retval); fop = fop->as.fast_op.next; break; case FOP_APPLY_GLOBAL: sym = fop->as.fast_op.arg1; fn = SYMBOL_VCELL(sym); if (UNBOUND_MARKER_P(fn)) vmerror_unbound(sym); argc = 0; args = fop->as.fast_op.arg2; while (CONSP(args)) { if (argc >= ARG_BUF_LEN) { vmerror_unsupported(_T("too many actual arguments")); break; } argv[argc] = execute_fast_op(CAR(args), env); args = CDR(args); argc++; } if (!NULLP(args)) vmerror_arg_out_of_range(fop->as.fast_op.arg2, _T("bad formal argument list")); fop = apply(fn, argc, argv, &env, &retval); break; case FOP_APPLY: argc = 0; fn = execute_fast_op(fop->as.fast_op.arg1, env); args = fop->as.fast_op.arg2; while (CONSP(args)) { if (argc >= ARG_BUF_LEN) { vmerror_unsupported(_T("too many actual arguments")); break; } argv[argc] = execute_fast_op(CAR(args), env); args = CDR(args); argc++; } if (!NULLP(args)) vmerror_arg_out_of_range(fop->as.fast_op.arg2, _T("bad formal argument list")); fop = apply(fn, argc, argv, &env, &retval); break; case FOP_IF_TRUE: if (TRUEP(retval)) fop = fop->as.fast_op.arg1; else fop = fop->as.fast_op.arg2; break; case FOP_RETVAL: fop = fop->as.fast_op.next; break; case FOP_SEQUENCE: retval = execute_fast_op(fop->as.fast_op.arg1, env); fop = fop->as.fast_op.arg2; break; case FOP_THROW: tag = execute_fast_op(fop->as.fast_op.arg1, env); escape_retval = execute_fast_op(fop->as.fast_op.arg2, env); dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: throw ~a, retval = ~a\n"), tag, escape_retval)); CURRENT_TIB()->escape_frame = find_matching_escape(CURRENT_TIB()->frame, tag); CURRENT_TIB()->escape_value = escape_retval; if (CURRENT_TIB()->escape_frame == NULL) { /* If we don't find a matching catch for the throw, we have a * problem and need to invoke a trap. */ vmtrap(TRAP_UNCAUGHT_THROW, (enum vmt_options_t)(VMT_MANDATORY_TRAP | VMT_HANDLER_MUST_ESCAPE), 2, tag, escape_retval); } unwind_stack_for_throw(); fop = fop->as.fast_op.next; break; case FOP_CATCH: tag = execute_fast_op(fop->as.fast_op.arg1, env); jmpbuf = fstack_enter_catch_frame(tag, CURRENT_TIB()->frame); dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: setjmp tag: ~a, frame: ~c&, jmpbuf: ~c&\n"), tag, CURRENT_TIB()->frame, jmpbuf)); if (setjmp(*jmpbuf) == 0) { retval = execute_fast_op(fop->as.fast_op.arg2, env); } else { dscwritef(DF_SHOW_THROWS, (_T("; DEBUG: catch, retval = ~a\n"), CURRENT_TIB()->escape_value)); retval = CURRENT_TIB()->escape_value; CURRENT_TIB()->escape_value = NIL; } fstack_leave_frame(); fop = fop->as.fast_op.next; break; case FOP_WITH_UNWIND_FN: fstack_enter_unwind_frame(execute_fast_op(fop->as.fast_op.arg1, env)); retval = execute_fast_op(fop->as.fast_op.arg2, env); after = CURRENT_TIB()->frame[FOFS_UNWIND_AFTER]; fstack_leave_frame(); apply1(after, 0, NULL); fop = fop->as.fast_op.next; break; case FOP_CLOSURE: retval = lclosurecons(env, lcons(lcar(fop->as.fast_op.arg1), fop->as.fast_op.arg2), lcdr(fop->as.fast_op.arg1)); fop = fop->as.fast_op.next; break; case FOP_CAR: retval = lcar(retval); fop = fop->as.fast_op.next; break; case FOP_CDR: retval = lcdr(retval); fop = fop->as.fast_op.next; break; case FOP_NOT: retval = boolcons(!TRUEP(retval)); fop = fop->as.fast_op.next; break; case FOP_NULLP: retval = boolcons(NULLP(retval)); fop = fop->as.fast_op.next; break; case FOP_EQP: retval = boolcons(EQ(execute_fast_op(fop->as.fast_op.arg1, env), execute_fast_op(fop->as.fast_op.arg2, env))); fop = fop->as.fast_op.next; break; case FOP_GET_ENV: retval = env; fop = fop->as.fast_op.next; break; case FOP_GLOBAL_DEF: // three args, third was genv, but currently unused retval = lidefine_global(fop->as.fast_op.arg1, fop->as.fast_op.arg2); fop = fop->as.fast_op.next; break; case FOP_GET_FSP: retval = fixcons((fixnum_t)CURRENT_TIB()->fsp); fop = fop->as.fast_op.next; break; case FOP_GET_FRAME: retval = fixcons((fixnum_t)CURRENT_TIB()->frame); fop = fop->as.fast_op.next; break; case FOP_GET_HFRAMES: retval = CURRENT_TIB()->handler_frames; fop = fop->as.fast_op.next; break; case FOP_SET_HFRAMES: CURRENT_TIB()->handler_frames = execute_fast_op(fop->as.fast_op.arg1, env); fop = fop->as.fast_op.next; break; case FOP_GLOBAL_PRESERVE_FRAME: sym = fop->as.fast_op.arg1; binding = SYMBOL_VCELL(sym); if (UNBOUND_MARKER_P(binding)) vmerror_unbound(sym); SET_SYMBOL_VCELL(sym, fixcons((fixnum_t)CURRENT_TIB()->frame)); retval = execute_fast_op(fop->as.fast_op.arg2, env); fop = fop->as.fast_op.next; break; case FOP_STACK_BOUNDARY: sym = execute_fast_op(fop->as.fast_op.arg1, env); fstack_enter_boundary_frame(sym); retval = execute_fast_op(fop->as.fast_op.arg2, env); fstack_leave_frame(); fop = fop->as.fast_op.next; break; case FOP_FAST_ENQUEUE_CELL: retval = execute_fast_op(fop->as.fast_op.arg2, env); cell = execute_fast_op(fop->as.fast_op.arg1, env); SET_CDR(CAR(retval), cell); SET_CAR(retval, cell); fop = fop->as.fast_op.next; break; case FOP_WHILE_TRUE: while(TRUEP(execute_fast_op(fop->as.fast_op.arg1, env))) { retval = execute_fast_op(fop->as.fast_op.arg2, env); } fop = fop->as.fast_op.next; break; case FOP_LOCAL_REF_BY_INDEX: retval = lenvlookup_by_index(FIXNM(fop->as.fast_op.arg1), FIXNM(fop->as.fast_op.arg2), env); fop = fop->as.fast_op.next; break; case FOP_LOCAL_REF_RESTARG: retval = lenvlookup_restarg_by_index(FIXNM(fop->as.fast_op.arg1), FIXNM(fop->as.fast_op.arg2), env); fop = fop->as.fast_op.next; break; case FOP_LOCAL_SET_BY_INDEX: lenvlookup_set_by_index(FIXNM(fop->as.fast_op.arg1), FIXNM(fop->as.fast_op.arg2), env, retval); fop = fop->as.fast_op.next; break; default: panic("Unsupported fast-op"); } } fstack_leave_frame(); return retval; }