🎨 opcode dump can get global variable name

This commit is contained in:
ValKmjolnir 2025-03-15 18:17:35 +08:00
parent 10d2965197
commit b5f02de883
6 changed files with 54 additions and 8 deletions

View File

@ -1436,6 +1436,7 @@ void codegen::print(std::ostream& out) {
codestream::set(
const_number_table.data(),
const_string_table.data(),
global,
native_function.data()
);

View File

@ -163,7 +163,13 @@ void dbg::step_info() {
begin = (ctx.pc>>3)==0? 0:((ctx.pc>>3)<<3);
end = (1+(ctx.pc>>3))<<3;
codestream::set(const_number, const_string, native_function.data(), files);
codestream::set(
const_number,
const_string,
global_symbol_name,
native_function.data(),
files
);
std::clog << "\nnext bytecode:\n";
for(u64 i = begin; i<end && bytecode[i].op!=op_exit; ++i) {

View File

@ -5,12 +5,31 @@ namespace nasal {
void codestream::set(const f64* number_list,
const std::string* string_list,
const std::unordered_map<std::string, u64>& globals,
const nasal_builtin_table* native_table,
const std::string* file_list) {
const_number = number_list;
const_string = string_list;
natives = native_table;
files = file_list;
global_variable.resize(globals.size());
for(auto& [name, index]: globals) {
global_variable[index] = name;
}
}
void codestream::set(const f64* number_list,
const std::string* string_list,
const std::vector<std::string>& globals,
const nasal_builtin_table* native_table,
const std::string* file_list) {
const_number = number_list;
const_string = string_list;
natives = native_table;
files = file_list;
global_variable = globals;
}
void codestream::dump(std::ostream& out) const {
@ -93,13 +112,16 @@ void codestream::dump(std::ostream& out) const {
case op_jmp:
case op_jt:
case op_jf:
case op_callg:
case op_mcallg:
case op_loadg:
case op_calll:
case op_mcalll:
case op_loadl:
out << hex << "0x" << num << dec; break;
case op_callg:
out << hex << "0x" << num << dec;
out << " (" << util::rawstr(global_variable[num], 32) << ")";
break;
case op_callb:
out << hex << "0x" << num << dec;
out << " <" << natives[num].name << "@0x";

View File

@ -4,6 +4,10 @@
#include "natives/builtin.h"
#include <iostream>
#include <vector>
#include <cstring>
#include <sstream>
#include <unordered_map>
namespace nasal {
@ -207,11 +211,18 @@ private:
inline static const std::string* const_string = nullptr;
inline static const nasal_builtin_table* natives = nullptr;
inline static const std::string* files = nullptr;
inline static std::vector<std::string> global_variable;
public:
codestream(const opcode& c, const u64 i): code(c), index(i) {}
static void set(const f64*,
const std::string*,
const std::unordered_map<std::string, u64>&,
const nasal_builtin_table*,
const std::string* file_list = nullptr);
static void set(const f64*,
const std::string*,
const std::vector<std::string>&,
const nasal_builtin_table*,
const std::string* file_list = nullptr);
void dump(std::ostream&) const;

View File

@ -327,7 +327,13 @@ void vm::trace_back() {
std::clog << "\nback trace ";
std::clog << (ngc.cort? "(coroutine)":"(main)") << "\n";
codestream::set(const_number, const_string, native_function.data(), files);
codestream::set(
const_number,
const_string,
global_symbol_name,
native_function.data(),
files
);
for(u64 p = 0, same = 0, prev = 0xffffffff; !ret.empty(); prev = p, ret.pop()) {
if ((p = ret.top())==prev) {