/*Busca y almacena en el arbol un nuevo camino a un spot libre */ void network_newFreeSpot (AbbNet net){ /*Precondicion: network_newFreeSpot() se tiene que llamar despues de haber agregado un elemento*/ networkNode * pivot; networkNode * ancestor; pivot = STACK_TOP(net->freeSpot); STACK_POP(net->freespot); assert(pivot->left == Leaf); if(pivote->right != Leaf){ if(!STACK_IS_EMPTY(net->freeSpot)){ ancestor = STACK_TOP(net->freeSpot); /*mientras el hijo derecho del ancestro es el pivote*/ while(networkNode_compare(ancestor->right, pivot) && STACK_SIZE(net->freeSpot) > 1 ){ /*sigo subiendo en el arbol*/ pivot = ancestor; STACK_POP(net->freespot); ancestor = STACK_TOP(net->freeSpot); } /*pude haber salido por que el stack esta vacion o porque ancestor->right != pivot * si sali por que el ancestro->right es diferente al pivote, entonces me voy por el * el hijo derecho del ancestro todo a la izquierda. * si sali porque llegue a la copa del arbol me voo todo a la izq a iniciar un nivel del arbol nuevo */ if(!networkNode_compare(ancestor->right, pivot){ ancestro = ancestro->right; } while(ancestor->left != Leaf){ STACK_ADD(net->freeSpot, ancestor); ancestor = ancestor->left; } } }
static void op_roll(void) { int depth; int rolls; int i; int j; STACK_POP(rolls); STACK_POP(depth); if (depth < 0 || depth > stack.sp) return; if (rolls > 0) { for (i=0; i<rolls; i++) { int last = stack.array[stack.sp-1]; for(j=stack.sp-1; j>stack.sp-depth; j--) { stack.array[j] = stack.array[j-1]; } stack.array[stack.sp-depth] = last; } } else if (rolls < 0) { rolls = -rolls; for (i=0; i<rolls; i++) { int last = stack.array[stack.sp-depth]; for(j=stack.sp-depth; j<stack.sp-1; j++) { stack.array[j] = stack.array[j+1]; } stack.array[stack.sp-1] = last; } } }
int fn_for(_kforth_context *context) { LOOP_STACK_PUSH(STACK_POP()); LOOP_STACK_PUSH(STACK_POP()); LOOP_STACK_PUSH(STACK_POP()); return 0; }
static void op_greater(void) { int val1; int val2; if (stack.sp < 2) return; STACK_POP(val1); STACK_POP(val2); if (val2 > val1) STACK_PUSH(1); else STACK_PUSH(0); }
int fn_read(_kforth_context *context) { platform_printf("Reading From 0x%08X\r\n", STACK(0)); STACK_POP(); STACK_PUSH(0); return 0; }
static void op_out_char(void) { int val; STACK_POP(val); printf("%c", val); fflush(stdout); }
static void op_out_number(void) { int val; STACK_POP(val); printf("%d", val); fflush(stdout); }
int fn_while(_kforth_context *context) { context->loop_stack_ptr--; // effectively popping LABEL2 if (STACK_POP()==FORTH_TRUE) { return DO_WHILE_LABEL1-DO_WHILE_LABEL2; } context->loop_stack_ptr--; return 0; }
static void op_switch(void) { unsigned int val; if (stack.sp < 1) return; STACK_POP(val); if (val % 2) interp.cc = -interp.cc; }
static void op_pointer(void) { int val; STACK_POP(val); /* Bring inter.dp into the [-3, +3] range then in [0, 3]. */ interp.dp = (interp.dp + val) % 4; if (interp.dp < 0) interp.dp += 4; }
static void op_not(void) { int val; if (stack.sp < 1) return; STACK_POP(val); if (val) STACK_PUSH(0); else STACK_PUSH(1); }
static void mark(void **block) { void *obj = *block; struct segment *seg; size_t index; bitptr_t b; ASSERT(STACK_TOP() == NULL); if (!IS_IN_HEAP(obj)) { DBG(("%p at %p outside", obj, block)); if (obj != NULL) sml_trace_ptr(obj); return; } seg = OBJ_TO_SEGMENT(obj); index = OBJ_TO_INDEX(seg, obj); BITPTR_INIT(b, BITMAP0_BASE(seg), index); if (BITPTR_TEST(b)) { DBG(("already marked: %p", obj)); return; } MARKBIT(b, index, seg); DBG(("MARK: %p", obj)); if (OBJ_HAS_NO_POINTER(obj)) { DBG(("EARLYMARK: %p", obj)); return; } for (;;) { sml_obj_enum_ptr(obj, push); obj = STACK_TOP(); if (obj == NULL) { DBG(("MARK END")); break; } STACK_POP(obj); DBG(("POP: %p", obj)); } }
int EndElement(void *UserData, const XMLCH *uri, const XMLCH *localName, const XMLCH *qName) { XMLCONFPARSER *xcp = (XMLCONFPARSER*)UserData; if (xcp->inMixedContent) { xcp->inMixedContent--; fprintf(PFOUT, "</%s>", qName); /* EM or B tags */ } else { if (STACK_POP(xcp->stateStack, &xcp->state)) { if (xcp->state == TEST) { /* close TEST description column and row: */ fputs("</td>", PFOUT); EMPTY_COLS(3, PFOUT); fputs("</tr>", PFOUT); } else if (xcp->state == TESTCASES) { int *pstate = STACK_PEEK(xcp->stateStack); /* check is needed 'cos there can be TESTCASES inside TESTCASES */ if (pstate && *pstate != TESTCASES) fputs("</table>", PFOUT); } } } return 0; }
/* Returns the index of the first unmatched character, or -1 if the match * failed */ int run (Instruction *prog, const char *target) { unsigned int pc = 0; unsigned int pos = 0; void *capture = NULL; size_t target_len = strlen(target); Instruction *instr; for (;;) { if (pc == FAIL) { /* Machine is in fail state */ if (STACK_EMPTY()) { /* No further options */ return -1; } if (STACK_TOPTYPE() == BacktrackData) { /* Backtrack to stacked alternative */ pc = STACK_TOP().alternative; pos = STACK_TOP().pos; capture = STACK_TOP().capture_info; } /* Pop one stack entry */ STACK_POP(); continue; } instr = &prog[pc]; #if 0 printf("pc = %d, pos = %d, instr = %s\n", pc, pos, instruction_names[instr->instr]); #endif switch (instr->instr) { case iEnd: return pos; case iChar: if (target[pos] == instr->character) { pos += 1; pc += 1; } else { pc = FAIL; } break; case iJump: pc += instr->offset; break; case iChoice: STACK_PUSHALT(pc + instr->offset, pos - instr->count, capture); pc += 1; break; case iCall: STACK_PUSHRET(pc + 1); pc += instr->offset; break; case iReturn: pc = STACK_TOP().ret; STACK_POP(); break; case iCommit: STACK_POP(); pc += instr->offset; break; case iCapture: /* TODO: Add capture info */ pc += 1; break; case iFail: pc = FAIL; break; case iAny: if (pos + instr->count <= target_len) { pc += 1; pos += instr->count; } else { pc = FAIL; } break; case iCharset: if (IN_CHARSET(instr->cset, target[pos])) { pc += 1; pos += 1; } else { pc = FAIL; } break; case iPartialCommit: if (STACK_TOPTYPE() == BacktrackData) { /* Replace the backtrack data on the top of the stack */ STACK_TOP().pos = pos; STACK_TOP().capture_info = capture; } else { /* Cannot happen */ assert(0); } pc += instr->offset; break; case iSpan: if (IN_CHARSET(instr->cset, target[pos])) pos += 1; else pc += 1; break; case iFailTwice: STACK_POP(); pc = FAIL; break; case iBackCommit: if (STACK_TOPTYPE() == BacktrackData) { /* Pop the position and capture info, but jump */ pos = STACK_TOP().pos; capture = STACK_TOP().capture_info; STACK_POP(); } else { /* Cannot happen */ assert(0); } pc += instr->offset; break; case iTestChar: if (target[pos] == instr->character) { pos += 1; pc += 1; } else { pc += instr->offset; } break; case iTestCharset: if (IN_CHARSET(instr->cset, target[pos])) { pos += 1; pc += 1; } else { pc += instr->offset; } break; case iTestAny: if (pos + instr->count <= target_len) { pos += instr->count; pc += 1; } else { pc += instr->offset; } break; default: /* Cannot happen - skip the instruction */ pc += 1; break; } } /* We never actually reach here - we exit at the End instruction */ return pos; }
int fn_elif(_kforth_context *context) { return STACK_POP(); }
/* Multikey introsort for medium size groups. */ static void _multikey_introsort(const sauchar_t *T, const saidx_t *PA, saidx_t *first, saidx_t *last, saidx_t depth) { struct { saidx_t *a, *b, c, d; } stack[STACK_SIZE]; const sauchar_t *Td; saidx_t *a, *b, *c, *d, *e, *f; saidx_t s, t; saidx_t ssize; saidx_t limit; saint_t v, x = 0; for(ssize = 0, limit = _lg(last - first);;) { if((last - first) <= SS_INSERTIONSORT_THRESHOLD) { if(1 < (last - first)) { _insertionsort(T, PA, first, last, depth); } STACK_POP(first, last, depth, limit); continue; } Td = T + depth; if(limit-- == 0) { _heapsort(Td, PA, first, last - first); } if(limit < 0) { for(a = first + 1, v = Td[PA[*first]]; a < last; ++a) { if((x = Td[PA[*a]]) != v) { if(1 < (a - first)) { break; } v = x; first = a; } } if(Td[PA[*first] - 1] < v) { first = _substring_partition(PA, first, a, depth); } if((a - first) <= (last - a)) { if(1 < (a - first)) { STACK_PUSH(a, last, depth, -1); last = a, depth += 1, limit = _lg(a - first); } else { first = a, limit = -1; } } else { if(1 < (last - a)) { STACK_PUSH(first, a, depth + 1, _lg(a - first)); first = a, limit = -1; } else { last = a, depth += 1, limit = _lg(a - first); } } continue; } /* choose pivot */ a = _pivot(Td, PA, first, last); v = Td[PA[*a]]; SWAP(*first, *a); /* partition */ for(b = first; (++b < last) && ((x = Td[PA[*b]]) == v);) { } if(((a = b) < last) && (x < v)) { for(; (++b < last) && ((x = Td[PA[*b]]) <= v);) { if(x == v) { SWAP(*b, *a); ++a; } } } for(c = last; (b < --c) && ((x = Td[PA[*c]]) == v);) { } if((b < (d = c)) && (x > v)) { for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) { if(x == v) { SWAP(*c, *d); --d; } } } for(; b < c;) { SWAP(*b, *c); for(; (++b < c) && ((x = Td[PA[*b]]) <= v);) { if(x == v) { SWAP(*b, *a); ++a; } } for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) { if(x == v) { SWAP(*c, *d); --d; } } } if(a <= d) { c = b - 1; if((s = a - first) > (t = b - a)) { s = t; } for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); } if((s = d - c) > (t = last - d - 1)) { s = t; } for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); } a = first + (b - a), c = last - (d - c); b = (v <= Td[PA[*a] - 1]) ? a : _substring_partition(PA, a, c, depth); if((a - first) <= (last - c)) { if((last - c) <= (c - b)) { STACK_PUSH(b, c, depth + 1, _lg(c - b)); STACK_PUSH(c, last, depth, limit); last = a; } else if((a - first) <= (c - b)) { STACK_PUSH(c, last, depth, limit); STACK_PUSH(b, c, depth + 1, _lg(c - b)); last = a; } else { STACK_PUSH(c, last, depth, limit); STACK_PUSH(first, a, depth, limit); first = b, last = c, depth += 1, limit = _lg(c - b); } } else { if((a - first) <= (c - b)) { STACK_PUSH(b, c, depth + 1, _lg(c - b)); STACK_PUSH(first, a, depth, limit); first = c; } else if((last - c) <= (c - b)) { STACK_PUSH(first, a, depth, limit); STACK_PUSH(b, c, depth + 1, _lg(c - b)); first = c; } else { STACK_PUSH(first, a, depth, limit); STACK_PUSH(c, last, depth, limit); first = b, last = c, depth += 1, limit = _lg(c - b); } } } else { limit += 1; if(Td[PA[*first] - 1] < v) { first = _substring_partition(PA, first, last, depth); limit = _lg(last - first); } depth += 1; } } }
void Interpret (int pop_return_p) { long dispatch_code; struct interpreter_state_s new_state; /* Primitives jump back here for errors, requests to evaluate an expression, apply a function, or handle an interrupt request. On errors or interrupts they leave their arguments on the stack, the primitive itself in GET_EXP. The code should do a primitive backout in these cases, but not in others (apply, eval, etc.), since the primitive itself will have left the state of the interpreter ready for operation. */ bind_interpreter_state (&new_state); dispatch_code = (setjmp (interpreter_catch_env)); preserve_signal_mask (); fixup_float_environment (); switch (dispatch_code) { case 0: /* first time */ if (pop_return_p) goto pop_return; /* continue */ else break; /* fall into eval */ case PRIM_APPLY: PROCEED_AFTER_PRIMITIVE (); goto internal_apply; case PRIM_NO_TRAP_APPLY: PROCEED_AFTER_PRIMITIVE (); goto Apply_Non_Trapping; case PRIM_APPLY_INTERRUPT: PROCEED_AFTER_PRIMITIVE (); PREPARE_APPLY_INTERRUPT (); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); case PRIM_APPLY_ERROR: PROCEED_AFTER_PRIMITIVE (); APPLICATION_ERROR (prim_apply_error_code); case PRIM_DO_EXPRESSION: SET_VAL (GET_EXP); PROCEED_AFTER_PRIMITIVE (); REDUCES_TO (GET_VAL); case PRIM_NO_TRAP_EVAL: SET_VAL (GET_EXP); PROCEED_AFTER_PRIMITIVE (); NEW_REDUCTION (GET_VAL, GET_ENV); goto eval_non_trapping; case PRIM_POP_RETURN: PROCEED_AFTER_PRIMITIVE (); goto pop_return; case PRIM_RETURN_TO_C: PROCEED_AFTER_PRIMITIVE (); unbind_interpreter_state (interpreter_state); return; case PRIM_NO_TRAP_POP_RETURN: PROCEED_AFTER_PRIMITIVE (); goto pop_return_non_trapping; case PRIM_INTERRUPT: back_out_of_primitive (); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); case PRIM_ABORT_TO_C: back_out_of_primitive (); unbind_interpreter_state (interpreter_state); return; case ERR_ARG_1_WRONG_TYPE: back_out_of_primitive (); Do_Micro_Error (ERR_ARG_1_WRONG_TYPE, true); goto internal_apply; case ERR_ARG_2_WRONG_TYPE: back_out_of_primitive (); Do_Micro_Error (ERR_ARG_2_WRONG_TYPE, true); goto internal_apply; case ERR_ARG_3_WRONG_TYPE: back_out_of_primitive (); Do_Micro_Error (ERR_ARG_3_WRONG_TYPE, true); goto internal_apply; default: back_out_of_primitive (); Do_Micro_Error (dispatch_code, true); goto internal_apply; } do_expression: /* GET_EXP has an Scode item in it that should be evaluated and the result left in GET_VAL. A "break" after the code for any operation indicates that all processing for this operation has been completed, and the next step will be to pop a return code off the stack and proceed at pop_return. This is sometimes called "executing the continuation" since the return code can be considered the continuation to be performed after the operation. An operation can terminate with a REDUCES_TO or REDUCES_TO_NTH macro. This indicates that the value of the current Scode item is the value returned when the new expression is evaluated. Therefore no new continuation is created and processing continues at do_expression with the new expression in GET_EXP. Finally, an operation can terminate with a DO_NTH_THEN macro. This indicates that another expression must be evaluated and them some additional processing will be performed before the value of this S-Code item available. Thus a new continuation is created and placed on the stack (using SAVE_CONT), the new expression is placed in the GET_EXP, and processing continues at do_expression. */ /* Handling of Eval Trapping. If we are handling traps and there is an Eval Trap set, turn off all trapping and then go to internal_apply to call the user supplied eval hook with the expression to be evaluated and the environment. */ #ifdef COMPILE_STEPPER if (trapping && (!WITHIN_CRITICAL_SECTION_P ()) && ((FETCH_EVAL_TRAPPER ()) != SHARP_F)) { trapping = false; Will_Push (4); PUSH_ENV (); PUSH_EXP (); STACK_PUSH (FETCH_EVAL_TRAPPER ()); PUSH_APPLY_FRAME_HEADER (2); Pushed (); goto Apply_Non_Trapping; } #endif /* COMPILE_STEPPER */ eval_non_trapping: #ifdef EVAL_UCODE_HOOK EVAL_UCODE_HOOK (); #endif switch (OBJECT_TYPE (GET_EXP)) { case TC_BIG_FIXNUM: /* The self evaluating items */ case TC_BIG_FLONUM: case TC_CHARACTER_STRING: case TC_CHARACTER: case TC_COMPILED_CODE_BLOCK: case TC_COMPLEX: case TC_CONTROL_POINT: case TC_DELAYED: case TC_ENTITY: case TC_ENVIRONMENT: case TC_EXTENDED_PROCEDURE: case TC_FIXNUM: case TC_HUNK3_A: case TC_HUNK3_B: case TC_INTERNED_SYMBOL: case TC_LIST: case TC_NON_MARKED_VECTOR: case TC_NULL: case TC_PRIMITIVE: case TC_PROCEDURE: case TC_QUAD: case TC_RATNUM: case TC_REFERENCE_TRAP: case TC_RETURN_CODE: case TC_UNINTERNED_SYMBOL: case TC_CONSTANT: case TC_VECTOR: case TC_VECTOR_16B: case TC_VECTOR_1B: default: SET_VAL (GET_EXP); break; case TC_ACCESS: Will_Push (CONTINUATION_SIZE); PUSH_NTH_THEN (RC_EXECUTE_ACCESS_FINISH, ACCESS_ENVIRONMENT); case TC_ASSIGNMENT: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_EXECUTE_ASSIGNMENT_FINISH, ASSIGN_VALUE); case TC_BROKEN_HEART: Microcode_Termination (TERM_BROKEN_HEART); case TC_COMBINATION: { long length = ((VECTOR_LENGTH (GET_EXP)) - 1); Will_Push (length + 2 + CONTINUATION_SIZE); stack_pointer = (STACK_LOC (-length)); STACK_PUSH (MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, length)); /* The finger: last argument number */ Pushed (); if (length == 0) { PUSH_APPLY_FRAME_HEADER (0); /* Frame size */ DO_NTH_THEN (RC_COMB_APPLY_FUNCTION, COMB_FN_SLOT); } PUSH_ENV (); DO_NTH_THEN (RC_COMB_SAVE_VALUE, (length + 1)); } case TC_COMBINATION_1: Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG + 1); PUSH_ENV (); DO_NTH_THEN (RC_COMB_1_PROCEDURE, COMB_1_ARG_1); case TC_COMBINATION_2: Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG + 2); PUSH_ENV (); DO_NTH_THEN (RC_COMB_2_FIRST_OPERAND, COMB_2_ARG_2); case TC_COMMENT: REDUCES_TO_NTH (COMMENT_EXPRESSION); case TC_CONDITIONAL: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_CONDITIONAL_DECIDE, COND_PREDICATE); #ifdef CC_SUPPORT_P case TC_COMPILED_ENTRY: dispatch_code = (enter_compiled_expression ()); goto return_from_compiled_code; #endif case TC_DEFINITION: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_EXECUTE_DEFINITION_FINISH, DEFINE_VALUE); case TC_DELAY: /* Deliberately omitted: EVAL_GC_CHECK (2); */ SET_VAL (MAKE_POINTER_OBJECT (TC_DELAYED, Free)); (Free[THUNK_ENVIRONMENT]) = GET_ENV; (Free[THUNK_PROCEDURE]) = (MEMORY_REF (GET_EXP, DELAY_OBJECT)); Free += 2; break; case TC_DISJUNCTION: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_DISJUNCTION_DECIDE, OR_PREDICATE); case TC_EXTENDED_LAMBDA: /* Deliberately omitted: EVAL_GC_CHECK (2); */ SET_VAL (MAKE_POINTER_OBJECT (TC_EXTENDED_PROCEDURE, Free)); (Free[PROCEDURE_LAMBDA_EXPR]) = GET_EXP; (Free[PROCEDURE_ENVIRONMENT]) = GET_ENV; Free += 2; break; case TC_IN_PACKAGE: Will_Push (CONTINUATION_SIZE); PUSH_NTH_THEN (RC_EXECUTE_IN_PACKAGE_CONTINUE, IN_PACKAGE_ENVIRONMENT); case TC_LAMBDA: case TC_LEXPR: /* Deliberately omitted: EVAL_GC_CHECK (2); */ SET_VAL (MAKE_POINTER_OBJECT (TC_PROCEDURE, Free)); (Free[PROCEDURE_LAMBDA_EXPR]) = GET_EXP; (Free[PROCEDURE_ENVIRONMENT]) = GET_ENV; Free += 2; break; case TC_MANIFEST_NM_VECTOR: EVAL_ERROR (ERR_EXECUTE_MANIFEST_VECTOR); case TC_PCOMB0: /* The argument to Will_Eventually_Push is determined by how much will be on the stack if we back out of the primitive. */ Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG); Finished_Eventual_Pushing (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG); SET_EXP (OBJECT_NEW_TYPE (TC_PRIMITIVE, GET_EXP)); goto primitive_internal_apply; case TC_PCOMB1: Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG + 1); DO_NTH_THEN (RC_PCOMB1_APPLY, PCOMB1_ARG_SLOT); case TC_PCOMB2: Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG + 2); PUSH_ENV (); DO_NTH_THEN (RC_PCOMB2_DO_1, PCOMB2_ARG_2_SLOT); case TC_PCOMB3: Will_Eventually_Push (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG + 3); PUSH_ENV (); DO_NTH_THEN (RC_PCOMB3_DO_2, PCOMB3_ARG_3_SLOT); case TC_SCODE_QUOTE: SET_VAL (MEMORY_REF (GET_EXP, SCODE_QUOTE_OBJECT)); break; case TC_SEQUENCE_2: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_SEQ_2_DO_2, SEQUENCE_1); case TC_SEQUENCE_3: Will_Push (CONTINUATION_SIZE + 1); PUSH_ENV (); PUSH_NTH_THEN (RC_SEQ_3_DO_2, SEQUENCE_1); case TC_SYNTAX_ERROR: EVAL_ERROR (ERR_SYNTAX_ERROR); case TC_THE_ENVIRONMENT: SET_VAL (GET_ENV); break; case TC_VARIABLE: { SCHEME_OBJECT val = GET_VAL; SCHEME_OBJECT name = (GET_VARIABLE_SYMBOL (GET_EXP)); long temp = (lookup_variable (GET_ENV, name, (&val))); if (temp != PRIM_DONE) { /* Back out of the evaluation. */ if (temp == PRIM_INTERRUPT) { PREPARE_EVAL_REPEAT (); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); } EVAL_ERROR (temp); } SET_VAL (val); } } /* Now restore the continuation saved during an earlier part of the EVAL cycle and continue as directed. */ pop_return: #ifdef COMPILE_STEPPER if (trapping && (!WITHIN_CRITICAL_SECTION_P ()) && ((FETCH_RETURN_TRAPPER ()) != SHARP_F)) { Will_Push (3); trapping = false; PUSH_VAL (); STACK_PUSH (FETCH_RETURN_TRAPPER ()); PUSH_APPLY_FRAME_HEADER (1); Pushed (); goto Apply_Non_Trapping; } #endif /* COMPILE_STEPPER */ pop_return_non_trapping: #ifdef POP_RETURN_UCODE_HOOK POP_RETURN_UCODE_HOOK (); #endif RESTORE_CONT (); #ifdef ENABLE_DEBUGGING_TOOLS if (!RETURN_CODE_P (GET_RET)) { PUSH_VAL (); /* For possible stack trace */ SAVE_CONT (); Microcode_Termination (TERM_BAD_STACK); } #endif /* Dispatch on the return code. A BREAK here will cause a "goto pop_return" to occur, since this is the most common occurrence. */ switch (OBJECT_DATUM (GET_RET)) { case RC_COMB_1_PROCEDURE: POP_ENV (); PUSH_VAL (); /* Arg. 1 */ STACK_PUSH (SHARP_F); /* Operator */ PUSH_APPLY_FRAME_HEADER (1); Finished_Eventual_Pushing (CONTINUATION_SIZE); DO_ANOTHER_THEN (RC_COMB_APPLY_FUNCTION, COMB_1_FN); case RC_COMB_2_FIRST_OPERAND: POP_ENV (); PUSH_VAL (); PUSH_ENV (); DO_ANOTHER_THEN (RC_COMB_2_PROCEDURE, COMB_2_ARG_1); case RC_COMB_2_PROCEDURE: POP_ENV (); PUSH_VAL (); /* Arg 1, just calculated */ STACK_PUSH (SHARP_F); /* Function */ PUSH_APPLY_FRAME_HEADER (2); Finished_Eventual_Pushing (CONTINUATION_SIZE); DO_ANOTHER_THEN (RC_COMB_APPLY_FUNCTION, COMB_2_FN); case RC_COMB_APPLY_FUNCTION: END_SUBPROBLEM (); goto internal_apply_val; case RC_COMB_SAVE_VALUE: { long Arg_Number; POP_ENV (); Arg_Number = ((OBJECT_DATUM (STACK_REF (STACK_COMB_FINGER))) - 1); (STACK_REF (STACK_COMB_FIRST_ARG + Arg_Number)) = GET_VAL; (STACK_REF (STACK_COMB_FINGER)) = (MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, Arg_Number)); /* DO NOT count on the type code being NMVector here, since the stack parser may create them with #F here! */ if (Arg_Number > 0) { PUSH_ENV (); DO_ANOTHER_THEN (RC_COMB_SAVE_VALUE, ((COMB_ARG_1_SLOT - 1) + Arg_Number)); } /* Frame Size */ STACK_PUSH (MEMORY_REF (GET_EXP, 0)); DO_ANOTHER_THEN (RC_COMB_APPLY_FUNCTION, COMB_FN_SLOT); } #ifdef CC_SUPPORT_P #define DEFINE_COMPILER_RESTART(return_code, entry) \ case return_code: \ { \ dispatch_code = (entry ()); \ goto return_from_compiled_code; \ } DEFINE_COMPILER_RESTART (RC_COMP_INTERRUPT_RESTART, comp_interrupt_restart); DEFINE_COMPILER_RESTART (RC_COMP_LOOKUP_TRAP_RESTART, comp_lookup_trap_restart); DEFINE_COMPILER_RESTART (RC_COMP_ASSIGNMENT_TRAP_RESTART, comp_assignment_trap_restart); DEFINE_COMPILER_RESTART (RC_COMP_OP_REF_TRAP_RESTART, comp_op_lookup_trap_restart); DEFINE_COMPILER_RESTART (RC_COMP_CACHE_REF_APPLY_RESTART, comp_cache_lookup_apply_restart); DEFINE_COMPILER_RESTART (RC_COMP_SAFE_REF_TRAP_RESTART, comp_safe_lookup_trap_restart); DEFINE_COMPILER_RESTART (RC_COMP_UNASSIGNED_TRAP_RESTART, comp_unassigned_p_trap_restart); DEFINE_COMPILER_RESTART (RC_COMP_LINK_CACHES_RESTART, comp_link_caches_restart); DEFINE_COMPILER_RESTART (RC_COMP_ERROR_RESTART, comp_error_restart); case RC_REENTER_COMPILED_CODE: dispatch_code = (return_to_compiled_code ()); goto return_from_compiled_code; #endif case RC_CONDITIONAL_DECIDE: END_SUBPROBLEM (); POP_ENV (); REDUCES_TO_NTH ((GET_VAL == SHARP_F) ? COND_ALTERNATIVE : COND_CONSEQUENT); case RC_DISJUNCTION_DECIDE: /* Return predicate if it isn't #F; else do ALTERNATIVE */ END_SUBPROBLEM (); POP_ENV (); if (GET_VAL != SHARP_F) goto pop_return; REDUCES_TO_NTH (OR_ALTERNATIVE); case RC_END_OF_COMPUTATION: { /* Signals bottom of stack */ interpreter_state_t previous_state; previous_state = (interpreter_state -> previous_state); if (previous_state == NULL_INTERPRETER_STATE) { termination_end_of_computation (); /*NOTREACHED*/ } else { dstack_position = interpreter_catch_dstack_position; interpreter_state = previous_state; return; } } case RC_EVAL_ERROR: /* Should be called RC_REDO_EVALUATION. */ POP_ENV (); REDUCES_TO (GET_EXP); case RC_EXECUTE_ACCESS_FINISH: { SCHEME_OBJECT val; long code; code = (lookup_variable (GET_VAL, (MEMORY_REF (GET_EXP, ACCESS_NAME)), (&val))); if (code == PRIM_DONE) SET_VAL (val); else if (code == PRIM_INTERRUPT) { PREPARE_POP_RETURN_INTERRUPT (RC_EXECUTE_ACCESS_FINISH, GET_VAL); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); } else POP_RETURN_ERROR (code); } END_SUBPROBLEM (); break; case RC_EXECUTE_ASSIGNMENT_FINISH: { SCHEME_OBJECT variable = (MEMORY_REF (GET_EXP, ASSIGN_NAME)); SCHEME_OBJECT old_val; long code; POP_ENV (); if (TC_VARIABLE == (OBJECT_TYPE (variable))) code = (assign_variable (GET_ENV, (GET_VARIABLE_SYMBOL (variable)), GET_VAL, (&old_val))); else code = ERR_BAD_FRAME; if (code == PRIM_DONE) SET_VAL (old_val); else { PUSH_ENV (); if (code == PRIM_INTERRUPT) { PREPARE_POP_RETURN_INTERRUPT (RC_EXECUTE_ASSIGNMENT_FINISH, GET_VAL); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); } else POP_RETURN_ERROR (code); } } END_SUBPROBLEM (); break; case RC_EXECUTE_DEFINITION_FINISH: { SCHEME_OBJECT name = (MEMORY_REF (GET_EXP, DEFINE_NAME)); SCHEME_OBJECT value = GET_VAL; long result; POP_ENV (); result = (define_variable (GET_ENV, name, value)); if (result == PRIM_DONE) { END_SUBPROBLEM (); SET_VAL (name); break; } PUSH_ENV (); if (result == PRIM_INTERRUPT) { PREPARE_POP_RETURN_INTERRUPT (RC_EXECUTE_DEFINITION_FINISH, value); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); } SET_VAL (value); POP_RETURN_ERROR (result); } case RC_EXECUTE_IN_PACKAGE_CONTINUE: if (ENVIRONMENT_P (GET_VAL)) { END_SUBPROBLEM (); SET_ENV (GET_VAL); REDUCES_TO_NTH (IN_PACKAGE_EXPRESSION); } POP_RETURN_ERROR (ERR_BAD_FRAME); case RC_HALT: Microcode_Termination (TERM_TERM_HANDLER); case RC_HARDWARE_TRAP: { /* This just reinvokes the handler */ SCHEME_OBJECT info = (STACK_REF (0)); SCHEME_OBJECT handler = SHARP_F; SAVE_CONT (); if (VECTOR_P (fixed_objects)) handler = (VECTOR_REF (fixed_objects, TRAP_HANDLER)); if (handler == SHARP_F) { outf_fatal ("There is no trap handler for recovery!\n"); termination_trap (); /*NOTREACHED*/ } Will_Push (STACK_ENV_EXTRA_SLOTS + 2); STACK_PUSH (info); STACK_PUSH (handler); PUSH_APPLY_FRAME_HEADER (1); Pushed (); } goto internal_apply; /* internal_apply, the core of the application mechanism. Branch here to perform a function application. At this point the top of the stack contains an application frame which consists of the following elements (see sdata.h): - A header specifying the frame length. - A procedure. - The actual (evaluated) arguments. No registers (except the stack pointer) are meaning full at this point. Before interrupts or errors are processed, some registers are cleared to avoid holding onto garbage if a garbage collection occurs. */ case RC_INTERNAL_APPLY_VAL: internal_apply_val: (APPLY_FRAME_PROCEDURE ()) = GET_VAL; case RC_INTERNAL_APPLY: internal_apply: #ifdef COMPILE_STEPPER if (trapping && (!WITHIN_CRITICAL_SECTION_P ()) && ((FETCH_APPLY_TRAPPER ()) != SHARP_F)) { unsigned long frame_size = (APPLY_FRAME_SIZE ()); (* (STACK_LOC (0))) = (FETCH_APPLY_TRAPPER ()); PUSH_APPLY_FRAME_HEADER (frame_size); trapping = false; } #endif /* COMPILE_STEPPER */ Apply_Non_Trapping: if (PENDING_INTERRUPTS_P) { unsigned long interrupts = (PENDING_INTERRUPTS ()); PREPARE_APPLY_INTERRUPT (); SIGNAL_INTERRUPT (interrupts); } perform_application: #ifdef APPLY_UCODE_HOOK APPLY_UCODE_HOOK (); #endif { SCHEME_OBJECT Function = (APPLY_FRAME_PROCEDURE ()); apply_dispatch: switch (OBJECT_TYPE (Function)) { case TC_ENTITY: { unsigned long frame_size = (APPLY_FRAME_SIZE ()); SCHEME_OBJECT data = (MEMORY_REF (Function, ENTITY_DATA)); if ((VECTOR_P (data)) && (frame_size < (VECTOR_LENGTH (data))) && ((VECTOR_REF (data, frame_size)) != SHARP_F) && ((VECTOR_REF (data, 0)) == (VECTOR_REF (fixed_objects, ARITY_DISPATCHER_TAG)))) { Function = (VECTOR_REF (data, frame_size)); (APPLY_FRAME_PROCEDURE ()) = Function; goto apply_dispatch; } (STACK_REF (0)) = (MEMORY_REF (Function, ENTITY_OPERATOR)); PUSH_APPLY_FRAME_HEADER (frame_size); /* This must be done to prevent an infinite push loop by an entity whose handler is the entity itself or some other such loop. Of course, it will die if stack overflow interrupts are disabled. */ STACK_CHECK (0); goto internal_apply; } case TC_PROCEDURE: { unsigned long frame_size = (APPLY_FRAME_SIZE ()); Function = (MEMORY_REF (Function, PROCEDURE_LAMBDA_EXPR)); { SCHEME_OBJECT formals = (MEMORY_REF (Function, LAMBDA_FORMALS)); if ((frame_size != (VECTOR_LENGTH (formals))) && (((OBJECT_TYPE (Function)) != TC_LEXPR) || (frame_size < (VECTOR_LENGTH (formals))))) APPLICATION_ERROR (ERR_WRONG_NUMBER_OF_ARGUMENTS); } if (GC_NEEDED_P (frame_size + 1)) { PREPARE_APPLY_INTERRUPT (); IMMEDIATE_GC (frame_size + 1); } { SCHEME_OBJECT * end = (Free + 1 + frame_size); SCHEME_OBJECT env = (MAKE_POINTER_OBJECT (TC_ENVIRONMENT, Free)); (*Free++) = (MAKE_OBJECT (TC_MANIFEST_VECTOR, frame_size)); (void) STACK_POP (); while (Free < end) (*Free++) = (STACK_POP ()); SET_ENV (env); REDUCES_TO (MEMORY_REF (Function, LAMBDA_SCODE)); } } case TC_CONTROL_POINT: if ((APPLY_FRAME_SIZE ()) != 2) APPLICATION_ERROR (ERR_WRONG_NUMBER_OF_ARGUMENTS); SET_VAL (* (APPLY_FRAME_ARGS ())); unpack_control_point (Function); RESET_HISTORY (); goto pop_return; /* After checking the number of arguments, remove the frame header since primitives do not expect it. NOTE: This code must match the application code which follows primitive_internal_apply. */ case TC_PRIMITIVE: if (!IMPLEMENTED_PRIMITIVE_P (Function)) APPLICATION_ERROR (ERR_UNIMPLEMENTED_PRIMITIVE); { unsigned long n_args = (APPLY_FRAME_N_ARGS ()); /* Note that the first test below will fail for lexpr primitives. */ if (n_args != (PRIMITIVE_ARITY (Function))) { if ((PRIMITIVE_ARITY (Function)) != LEXPR_PRIMITIVE_ARITY) APPLICATION_ERROR (ERR_WRONG_NUMBER_OF_ARGUMENTS); SET_LEXPR_ACTUALS (n_args); } stack_pointer = (APPLY_FRAME_ARGS ()); SET_EXP (Function); APPLY_PRIMITIVE_FROM_INTERPRETER (Function); POP_PRIMITIVE_FRAME (n_args); goto pop_return; } case TC_EXTENDED_PROCEDURE: { SCHEME_OBJECT lambda; SCHEME_OBJECT temp; unsigned long nargs; unsigned long nparams; unsigned long formals; unsigned long params; unsigned long auxes; long rest_flag; long size; long i; SCHEME_OBJECT * scan; nargs = (POP_APPLY_FRAME_HEADER ()); lambda = (MEMORY_REF (Function, PROCEDURE_LAMBDA_EXPR)); Function = (MEMORY_REF (lambda, ELAMBDA_NAMES)); nparams = ((VECTOR_LENGTH (Function)) - 1); Function = (Get_Count_Elambda (lambda)); formals = (Elambda_Formals_Count (Function)); params = ((Elambda_Opts_Count (Function)) + formals); rest_flag = (Elambda_Rest_Flag (Function)); auxes = (nparams - (params + rest_flag)); if ((nargs < formals) || (!rest_flag && (nargs > params))) { PUSH_APPLY_FRAME_HEADER (nargs); APPLICATION_ERROR (ERR_WRONG_NUMBER_OF_ARGUMENTS); } /* size includes the procedure slot, but not the header. */ size = (params + rest_flag + auxes + 1); if (GC_NEEDED_P (size + 1 + ((nargs > params) ? (2 * (nargs - params)) : 0))) { PUSH_APPLY_FRAME_HEADER (nargs); PREPARE_APPLY_INTERRUPT (); IMMEDIATE_GC (size + 1 + ((nargs > params) ? (2 * (nargs - params)) : 0)); } scan = Free; temp = (MAKE_POINTER_OBJECT (TC_ENVIRONMENT, scan)); (*scan++) = (MAKE_OBJECT (TC_MANIFEST_VECTOR, size)); if (nargs <= params) { for (i = (nargs + 1); (--i) >= 0; ) (*scan++) = (STACK_POP ()); for (i = (params - nargs); (--i) >= 0; ) (*scan++) = DEFAULT_OBJECT; if (rest_flag) (*scan++) = EMPTY_LIST; for (i = auxes; (--i) >= 0; ) (*scan++) = UNASSIGNED_OBJECT; } else { /* rest_flag must be true. */ SCHEME_OBJECT list = (MAKE_POINTER_OBJECT (TC_LIST, (scan + size))); for (i = (params + 1); (--i) >= 0; ) (*scan++) = (STACK_POP ()); (*scan++) = list; for (i = auxes; (--i) >= 0; ) (*scan++) = UNASSIGNED_OBJECT; /* Now scan == OBJECT_ADDRESS (list) */ for (i = (nargs - params); (--i) >= 0; ) { (*scan++) = (STACK_POP ()); (*scan) = MAKE_POINTER_OBJECT (TC_LIST, (scan + 1)); scan += 1; } (scan[-1]) = EMPTY_LIST; } Free = scan; SET_ENV (temp); REDUCES_TO (Get_Body_Elambda (lambda)); } #ifdef CC_SUPPORT_P case TC_COMPILED_ENTRY: { guarantee_cc_return (1 + (APPLY_FRAME_SIZE ())); dispatch_code = (apply_compiled_procedure ()); return_from_compiled_code: switch (dispatch_code) { case PRIM_DONE: goto pop_return; case PRIM_APPLY: goto internal_apply; case PRIM_INTERRUPT: SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); case PRIM_APPLY_INTERRUPT: PREPARE_APPLY_INTERRUPT (); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); case ERR_INAPPLICABLE_OBJECT: case ERR_WRONG_NUMBER_OF_ARGUMENTS: APPLICATION_ERROR (dispatch_code); default: Do_Micro_Error (dispatch_code, true); goto internal_apply; } } #endif default: APPLICATION_ERROR (ERR_INAPPLICABLE_OBJECT); } } case RC_JOIN_STACKLETS: unpack_control_point (GET_EXP); break; case RC_NORMAL_GC_DONE: SET_VAL (GET_EXP); /* Paranoia */ if (GC_NEEDED_P (gc_space_needed)) termination_gc_out_of_space (); gc_space_needed = 0; EXIT_CRITICAL_SECTION ({ SAVE_CONT (); }); break; case RC_PCOMB1_APPLY: END_SUBPROBLEM (); PUSH_VAL (); /* Argument value */ Finished_Eventual_Pushing (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG); SET_EXP (MEMORY_REF (GET_EXP, PCOMB1_FN_SLOT)); primitive_internal_apply: #ifdef COMPILE_STEPPER if (trapping && (!WITHIN_CRITICAL_SECTION_P ()) && ((FETCH_APPLY_TRAPPER ()) != SHARP_F)) { Will_Push (3); PUSH_EXP (); STACK_PUSH (FETCH_APPLY_TRAPPER ()); PUSH_APPLY_FRAME_HEADER (1 + (PRIMITIVE_N_PARAMETERS (GET_EXP))); Pushed (); trapping = false; goto Apply_Non_Trapping; } #endif /* COMPILE_STEPPER */ /* NOTE: This code must match the code in the TC_PRIMITIVE case of internal_apply. This code is simpler because: 1) The arity was checked at syntax time. 2) We don't have to deal with "lexpr" primitives. 3) We don't need to worry about unimplemented primitives because unimplemented primitives will cause an error at invocation. */ { SCHEME_OBJECT primitive = GET_EXP; APPLY_PRIMITIVE_FROM_INTERPRETER (primitive); POP_PRIMITIVE_FRAME (PRIMITIVE_ARITY (primitive)); break; } case RC_PCOMB2_APPLY: END_SUBPROBLEM (); PUSH_VAL (); /* Value of arg. 1 */ Finished_Eventual_Pushing (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG); SET_EXP (MEMORY_REF (GET_EXP, PCOMB2_FN_SLOT)); goto primitive_internal_apply; case RC_PCOMB2_DO_1: POP_ENV (); PUSH_VAL (); /* Save value of arg. 2 */ DO_ANOTHER_THEN (RC_PCOMB2_APPLY, PCOMB2_ARG_1_SLOT); case RC_PCOMB3_APPLY: END_SUBPROBLEM (); PUSH_VAL (); /* Save value of arg. 1 */ Finished_Eventual_Pushing (CONTINUATION_SIZE + STACK_ENV_FIRST_ARG); SET_EXP (MEMORY_REF (GET_EXP, PCOMB3_FN_SLOT)); goto primitive_internal_apply; case RC_PCOMB3_DO_1: { SCHEME_OBJECT Temp = (STACK_POP ()); /* Value of arg. 3 */ POP_ENV (); STACK_PUSH (Temp); /* Save arg. 3 again */ PUSH_VAL (); /* Save arg. 2 */ DO_ANOTHER_THEN (RC_PCOMB3_APPLY, PCOMB3_ARG_1_SLOT); } case RC_PCOMB3_DO_2: SET_ENV (STACK_REF (0)); PUSH_VAL (); /* Save value of arg. 3 */ DO_ANOTHER_THEN (RC_PCOMB3_DO_1, PCOMB3_ARG_2_SLOT); case RC_POP_RETURN_ERROR: case RC_RESTORE_VALUE: SET_VAL (GET_EXP); break; /* The following two return codes are both used to restore a saved history object. The difference is that the first does not copy the history object while the second does. In both cases, the GET_EXP contains the history object and the next item to be popped off the stack contains the offset back to the previous restore history return code. */ case RC_RESTORE_DONT_COPY_HISTORY: { prev_restore_history_offset = (OBJECT_DATUM (STACK_POP ())); (void) STACK_POP (); history_register = (OBJECT_ADDRESS (GET_EXP)); break; } case RC_RESTORE_HISTORY: { if (!restore_history (GET_EXP)) { SAVE_CONT (); Will_Push (CONTINUATION_SIZE); SET_EXP (GET_VAL); SET_RC (RC_RESTORE_VALUE); SAVE_CONT (); Pushed (); IMMEDIATE_GC (HEAP_AVAILABLE); } prev_restore_history_offset = (OBJECT_DATUM (STACK_POP ())); (void) STACK_POP (); if (prev_restore_history_offset > 0) (STACK_LOCATIVE_REFERENCE (STACK_BOTTOM, (-prev_restore_history_offset))) = (MAKE_RETURN_CODE (RC_RESTORE_HISTORY)); break; } case RC_RESTORE_INT_MASK: SET_INTERRUPT_MASK (UNSIGNED_FIXNUM_TO_LONG (GET_EXP)); if (GC_NEEDED_P (0)) REQUEST_GC (0); if (PENDING_INTERRUPTS_P) { SET_RC (RC_RESTORE_VALUE); SET_EXP (GET_VAL); SAVE_CONT (); SIGNAL_INTERRUPT (PENDING_INTERRUPTS ()); } break; case RC_STACK_MARKER: /* Frame consists of the return code followed by two objects. The first object has already been popped into GET_EXP, so just pop the second argument. */ stack_pointer = (STACK_LOCATIVE_OFFSET (stack_pointer, 1)); break; case RC_SEQ_2_DO_2: END_SUBPROBLEM (); POP_ENV (); REDUCES_TO_NTH (SEQUENCE_2); case RC_SEQ_3_DO_2: SET_ENV (STACK_REF (0)); DO_ANOTHER_THEN (RC_SEQ_3_DO_3, SEQUENCE_2); case RC_SEQ_3_DO_3: END_SUBPROBLEM (); POP_ENV (); REDUCES_TO_NTH (SEQUENCE_3); case RC_SNAP_NEED_THUNK: /* Don't snap thunk twice; evaluation of the thunk's body might have snapped it already. */ if ((MEMORY_REF (GET_EXP, THUNK_SNAPPED)) == SHARP_T) SET_VAL (MEMORY_REF (GET_EXP, THUNK_VALUE)); else { MEMORY_SET (GET_EXP, THUNK_SNAPPED, SHARP_T); MEMORY_SET (GET_EXP, THUNK_VALUE, GET_VAL); } break; default: POP_RETURN_ERROR (ERR_INAPPLICABLE_CONTINUATION); }
enum stack_ret stack_luint_pop(struct stack *stk, unsigned long int *ptr) { STACK_POP(stk, ptr, luint); }
static int calculation(double *result) { DATA *cur = NULL, *next = NULL; int ret = 0; cur = new_data(); ret = get_data(cur); if(ret) { printf("expression error.\n"); goto exit; } while(cur->sign != '=') { next = new_data(); ret = get_data(next); if(ret) { printf("expression error.\n"); goto exit; } while(cur->prio >= next->prio) { switch(cur->sign) { //计算 case '+': next->num = cur->num + next->num; break; case '-': next->num = cur->num - next->num; break; case '*': next->num = cur->num * next->num; break; case '/': if(next->num > -0.00000001 && next->num < 0.00000001) { ret = -1; goto exit; } next->num = cur->num / next->num; break; default: ret = -1; goto exit; } free(cur); if(!(cur = STACK_POP(stack))) { cur = next; next = NULL; break; } } if(!next) continue; if(!STACK_PUSH(stack, cur)) goto exit; cur = next; next = NULL; } *result = cur->num; exit: if(cur) free(cur); if(next) free(next); return ret; }
void run(void *literals[], byte instructions[]) { byte *ip = instructions; // instruction pointer Object *stack[STACK_MAX]; // the famous stack Object **sp = stack; // stack pointer Object *locals[STACK_MAX] = {}; // where we store our local variables // Setup the runtime Object *self = Object_new(); retain(self); // Start processing instructions while (1) { switch (*ip) { case CALL: { ip++; // moving to the 1st operand char *method = (char *)literals[*ip]; ip++; int argc = *ip; Object *argv[10]; int i; for(i = 0; i < argc; ++i) argv[i] = STACK_POP(); Object *receiver = STACK_POP(); Object *result = call(receiver, method, argv, argc); STACK_PUSH(result); // Releasing what was poped from the stack after we're done with it. for(i = 0; i < argc; ++i) release(argv[i]); release(receiver); break; } case PUSH_STRING: { ip++; STACK_PUSH(String_new((char *)literals[*ip])); break; } case PUSH_NUMBER: { ip++; STACK_PUSH(Number_new((long)literals[*ip])); break; } case PUSH_SELF: { STACK_PUSH(self); break; } case PUSH_NIL: { STACK_PUSH(NilObject); break; } case PUSH_BOOL: { ip++; if (*ip == 0) { STACK_PUSH(FalseObject); } else { STACK_PUSH(TrueObject); } break; } case GET_LOCAL: { ip++; // index of local in the locals table STACK_PUSH(locals[*ip]); break; } case SET_LOCAL: { ip++; locals[*ip] = STACK_POP(); break; } case ADD: { Object *a = STACK_POP(); Object *b = STACK_POP(); STACK_PUSH(Number_new(Number_value(a) + Number_value(b))); release(a); release(b); break; } case JUMP_UNLESS: { ip++; // number of bytes to more forward byte offset = *ip; Object *condition = STACK_POP(); if (!Object_is_true(condition)) ip += offset; release(condition); break; } case RETURN: { goto cleanup; break; } } ip++; } cleanup: release(self); int i; for(i = 0; i < STACK_MAX; ++i) if (locals[i]) release(locals[i]); while (sp > stack) release(STACK_POP()); }
enum stack_ret stack_dbl_pop(struct stack *stk, double *ptr) { STACK_POP(stk, ptr, dbl); }
enum stack_ret stack_ptr_pop(struct stack *stk, void **ptr) { STACK_POP(stk, ptr, ptr); }
/* Faster merge (based on divide and conquer technique). */ static void _merge(const sauchar_t *T, const saidx_t *PA, saidx_t *first, saidx_t *middle, saidx_t *last, saidx_t *buf, saidx_t bufsize, saidx_t depth) { #define GETIDX(a) ((0 <= (a)) ? (a) : (~(a))) #define MERGE_CHECK_EQUAL(a)\ do {\ if((0 <= *(a)) &&\ (_compare(T, PA + GETIDX(*((a) - 1)), PA + *(a), depth) == 0)) {\ *(a) = ~*(a);\ }\ } while(0) struct { saidx_t *a, *b, *c; int d; } stack[STACK_SIZE]; saidx_t *i, *j; saidx_t m, len, half; saidx_t ssize; int check, next; for(check = 0, ssize = 0;;) { if((last - middle) <= bufsize) { if((first < middle) && (middle < last)) { _merge_backward(T, PA, buf, first, middle, last, depth); } if(check & 1) { MERGE_CHECK_EQUAL(first); } if(check & 2) { MERGE_CHECK_EQUAL(last); } STACK_POP(first, middle, last, check); continue; } if((middle - first) <= bufsize) { if(first < middle) { _merge_forward(T, PA, buf, first, middle, last, depth); } if(check & 1) { MERGE_CHECK_EQUAL(first); } if(check & 2) { MERGE_CHECK_EQUAL(last); } STACK_POP(first, middle, last, check); continue; } for(m = 0, len = MIN(middle - first, last - middle), half = len >> 1; 0 < len; len = half, half >>= 1) { if(_compare(T, PA + GETIDX(*(middle + m + half)), PA + GETIDX(*(middle - m - half - 1)), depth) < 0) { m += half + 1; half -= (len & 1) ^ 1; } } if(0 < m) { _block_swap(middle - m, middle, m); i = j = middle, next = 0; if((middle + m) < last) { if(*(middle + m) < 0) { for(; *(i - 1) < 0; --i) { } *(middle + m) = ~*(middle + m); } for(j = middle; *j < 0; ++j) { } next = 1; } if((i - first) <= (last - j)) { STACK_PUSH(j, middle + m, last, (check & 2) | (next & 1)); middle -= m, last = i, check = (check & 1); } else { if((i == middle) && (middle == j)) { next <<= 1; } STACK_PUSH(first, middle - m, i, (check & 1) | (next & 2)); first = j, middle += m, check = (check & 2) | (next & 1); } } else { if(check & 1) { MERGE_CHECK_EQUAL(first); } MERGE_CHECK_EQUAL(middle); if(check & 2) { MERGE_CHECK_EQUAL(last); } STACK_POP(first, middle, last, check); } } }
/* * main() - * * The entry point to the program. * */ int main(int argc, char *argv[]) { // set maximum array size LEAVES = (argc <= 1 ? 10 : atoi( argv[1] ) ) ; MAX = (argc <= 1 ? 10 : 2 * LEAVES ); printf ("\n**** beginning pointer tests ** \n" ) ; enum TreeType treetype = 0 ; for ( treetype = 0 ; treetype < TTNUM ; treetype++ ) { printf ( "** testing treetype %s\n" , TTLabel[treetype] ) ; MaxErrCode = STACK_INIT ( &randomForest , MAX ) ; if ( MaxErrCode ) { MaxErrCode = 99 ; printf ( "ERROR Forest could not be initialized (%d)\n", MaxErrCode ) ; exit ( MaxErrCode ) ; } ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; } TreeHarnessTest ( treetype ) ; STACK_DUMP ( &randomForest ) ; } printf ("**** beginning STACK tests ** \n" ) ; // *randomForrest is now a stack of full trees int retCode = 0 ; TYPE_PTRTREE LeftTree, RightTree = NULL; printf ( "** beginning POP test ** \n" ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; } LeftTree = STACK_POP ( &randomForest ) ; STACK_DUMP ( &randomForest ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; printf ( "..after STACK_POP, STACK_DUMP returns %p\n" , LeftTree ) ; } printf ("** beginning PUSH test ** \n" ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; } retCode = STACK_PUSH ( &randomForest, LeftTree ) ; STACK_DUMP ( &randomForest ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; printf ( "..after STACK_PUSH returns %d\n" , retCode ) ; } printf ("** beginning LIFT test ** \n" ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; } retCode = STACK_LIFT ( &randomForest ) ; STACK_DUMP ( &randomForest ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; printf ( "..after STACK_LIFT returns %d\n" , retCode ) ; } printf ("** beginning SWAP test ** \n" ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; } retCode = STACK_SWAP ( &randomForest , STACK_TOP_IS ( &randomForest ) , STACK_BOTTOM_IS ( &randomForest ) ) ; if ( DEBUG ) { STACK_DUMP ( &randomForest ) ; printf ( "..after STACK_SWAP ( %u, %u ) returning %d\n" , STACK_TOP_IS ( &randomForest ) , STACK_BOTTOM_IS ( &randomForest ) , retCode ) ; } } // main()