Skip to content

Commit c13a5da

Browse files
committed
More detail in some usecode tracing messages
1 parent feb5700 commit c13a5da

File tree

4 files changed

+36
-61
lines changed

4 files changed

+36
-61
lines changed

usecode/stackframe.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ int Stack_frame::LastCallChainID = 0;
3737
Stack_frame::Stack_frame(
3838
Usecode_function* fun, int event, Game_object* caller, int chain,
3939
int depth)
40-
: function(fun), ip(nullptr), data(nullptr), externs(nullptr),
41-
code(nullptr), endp(nullptr), line_number(-1), call_chain(chain),
42-
call_depth(depth), num_externs(0), num_args(0), num_vars(0),
43-
locals(nullptr), eventid(event), caller_item(shared_from_obj(caller)),
44-
save_sp(nullptr) {
40+
: function(fun), ins_ip(nullptr), ip(nullptr), data(nullptr),
41+
externs(nullptr), code(nullptr), endp(nullptr), line_number(-1),
42+
call_chain(chain), call_depth(depth), num_externs(0), num_args(0),
43+
num_vars(0), locals(nullptr), eventid(event),
44+
caller_item(shared_from_obj(caller)), save_sp(nullptr) {
4545
ip = function->code;
4646
endp = ip + function->len;
4747

@@ -69,7 +69,7 @@ Stack_frame::Stack_frame(
6969

7070
ip += 2 * num_externs; // now points to actual code
7171

72-
code = ip;
72+
code = ins_ip = ip;
7373
}
7474

7575
Stack_frame::~Stack_frame() {
@@ -83,7 +83,7 @@ std::ostream& operator<<(std::ostream& out, Stack_frame& frame) {
8383
// #depth: 0xIP in functionname (obj=...,event=..., arg1name=..., ...)
8484

8585
out << "#" << frame.call_depth << ": 0x" << std::hex << std::setw(4)
86-
<< std::setfill('0') << static_cast<int>(frame.ip - frame.code)
86+
<< std::setfill('0') << static_cast<int>(frame.ins_ip - frame.code)
8787
<< " in 0x" << std::setw(4) << frame.function->id
8888
<< "(obj=" << std::setw(8) << frame.caller_item.get()
8989
<< ",ev=" << frame.eventid << std::setfill(' ') << std::dec;

usecode/stackframe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Stack_frame {
3939
~Stack_frame();
4040

4141
Usecode_function* function;
42+
const uint8* ins_ip; // IP for current instruction
4243
const uint8* ip; // current IP
4344
const uint8* data; // pointer to start of data segment
4445
const uint8* externs; // pointer to start of externs

usecode/ucinternal.cc

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
324337
void 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) {
462445
inline 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

usecode/ucinternal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class Usecode_internal : public Usecode_machine {
100100
void say_string(); // "Say" the string.
101101
Usecode_value* stack; // Stack.
102102
Usecode_value* sp; // Stack ptr. Grows upwards.
103-
void stack_error(int under);
104103
void push(const Usecode_value& val); // Push/pop stack.
105104
Usecode_value pop();
106105
Usecode_value peek();

0 commit comments

Comments
 (0)