@@ -321,6 +321,19 @@ void Usecode_internal::previous_stack_frame() {
321321 delete frame;
322322}
323323
324+ static ostream& print_usecode_function (
325+ Usecode_symbol_table* symtbl, const int function) {
326+ Usecode_symbol* fsym = symtbl ? (*symtbl)[function] : nullptr ;
327+ if (fsym) {
328+ cout << fsym->get_name () << " (" ;
329+ }
330+ cout << hex << setw (4 ) << setfill (' 0' ) << function << dec << setfill (' ' );
331+ if (fsym) {
332+ cout << ' )' ;
333+ }
334+ return cout;
335+ };
336+
324337void Usecode_internal::return_from_function (Usecode_value& retval) {
325338#ifdef DEBUG
326339 // store old function ID for debugging output
@@ -339,27 +352,12 @@ void Usecode_internal::return_from_function(Usecode_value& retval) {
339352 cout << " Returning (" ;
340353 retval.print (cout);
341354 cout << " ) from usecode " ;
342- Usecode_symbol* fsym = symtbl ? (*symtbl)[oldfunction] : nullptr ;
343- if (fsym) {
344- cout << fsym->get_name ();
345- } else {
346- cout << hex << setw (4 ) << setfill (' 0' ) << oldfunction << dec
347- << setfill (' ' );
348- }
349- cout << endl;
355+ print_usecode_function (symtbl, oldfunction) << endl;
350356
351357 if (parent_frame) {
352- const int newfunction = call_stack.front ()->function ->id ;
353- Usecode_symbol* fsym = symtbl ? (*symtbl)[newfunction] : nullptr ;
354-
358+ const int newfunction = call_stack.front ()->function ->id ;
355359 cout << " ...back into usecode " ;
356- if (fsym) {
357- cout << fsym->get_name ();
358- } else {
359- cout << hex << setw (4 ) << setfill (' 0' ) << newfunction << dec
360- << setfill (' ' );
361- }
362- cout << endl;
360+ print_usecode_function (symtbl, newfunction) << endl;
363361 }
364362#endif
365363}
@@ -374,30 +372,15 @@ void Usecode_internal::return_from_procedure() {
374372 previous_stack_frame ();
375373
376374#ifdef DEBUG
377- Stack_frame* parent_frame = call_stack.front ();
378- Usecode_symbol* fsym = symtbl ? (*symtbl)[oldfunction] : nullptr ;
375+ Stack_frame* parent_frame = call_stack.front ();
379376
380377 cout << " Returning from usecode " ;
381- if (fsym) {
382- cout << fsym->get_name ();
383- } else {
384- cout << hex << setw (4 ) << setfill (' 0' ) << oldfunction << dec
385- << setfill (' ' );
386- }
387- cout << endl;
378+ print_usecode_function (symtbl, oldfunction) << endl;
388379
389380 if (parent_frame) {
390- const int newfunction = call_stack.front ()->function ->id ;
391- Usecode_symbol* fsym = symtbl ? (*symtbl)[newfunction] : nullptr ;
392-
381+ const int newfunction = call_stack.front ()->function ->id ;
393382 cout << " ...back into usecode " ;
394- if (fsym) {
395- cout << fsym->get_name ();
396- } else {
397- cout << hex << setw (4 ) << setfill (' 0' ) << newfunction << dec
398- << setfill (' ' );
399- }
400- cout << endl;
383+ print_usecode_function (symtbl, newfunction) << endl;
401384 }
402385#endif
403386}
@@ -462,9 +445,14 @@ inline void Usecode_internal::push(const Usecode_value& val) {
462445inline Usecode_value Usecode_internal::pop () {
463446 if (sp <= stack) {
464447 // Happens in SI #0x939
465- cerr << " Stack underflow" << endl;
448+ cerr << " Stack underflow on function " ;
449+ print_usecode_function (symtbl, call_stack.front ()->function ->id );
450+ cerr << " at IP " ;
451+ cout << hex << setw (4 ) << setfill (' 0' ) << (frame->ins_ip - frame->code )
452+ << dec << setfill (' ' ) << std::endl;
466453 return Usecode_value (0 );
467454 }
455+
468456 // +++++SHARED: Shouldn't we reset *sp.
469457 return *--sp;
470458}
@@ -646,20 +634,6 @@ void Usecode_internal::say_string() {
646634 String = nullptr ;
647635}
648636
649- /*
650- * Stack error.
651- */
652-
653- void Usecode_internal::stack_error (int under // 1 if underflow.
654- ) {
655- if (under) {
656- cerr << " Stack underflow." << endl;
657- } else {
658- cerr << " Stack overflow." << endl;
659- }
660- exit (1 );
661- }
662-
663637/*
664638 * Gets the face for an NPC.
665639 */
@@ -1979,7 +1953,8 @@ int Usecode_internal::run() {
19791953 continue ;
19801954 }
19811955
1982- const int current_IP = frame->ip - frame->code ;
1956+ const auto current_IP = frame->ip - frame->code ;
1957+ frame->ins_ip = frame->ip ;
19831958
19841959 auto opcode = static_cast <UsecodeOps>(*(frame->ip ));
19851960
0 commit comments