some refactorings

This commit is contained in:
ProgramSnail 2025-05-11 11:06:15 +03:00
parent aff30ad7c1
commit 1f42c9ff4b
2 changed files with 3 additions and 86 deletions

View file

@ -351,13 +351,7 @@ void analyze(Bytefile *bf, std::vector<size_t> &&add_publics) {
/*uint args_count = */ ip_read_int_unsafe(&current_ip); /*uint args_count = */ ip_read_int_unsafe(&current_ip);
// NOTE: done in corresponding CALL/CALLC // NOTE: args checks done in corresponding CALL/CALLC
// TODO: no stack edit required then (?)
// current_stack_depth -= args_count;
// if (current_stack_depth < 0) {
// ip_failure(saved_current_ip, bf, "not enough elements in stack");
// }
// ++current_stack_depth;
} break; } break;
case Cmd::PATT: case Cmd::PATT:
--current_stack_depth; --current_stack_depth;

View file

@ -260,13 +260,6 @@ MergeResult merge_files(std::vector<Bytefile *> &&bytefiles) {
Offsets sizes = calc_merge_sizes(bytefiles); Offsets sizes = calc_merge_sizes(bytefiles);
size_t public_symbols_size = calc_publics_size(sizes.publics_num); size_t public_symbols_size = calc_publics_size(sizes.publics_num);
#ifdef DEBUG_VERSION
std::cout << "- inputs:\n";
for (const auto &bf : bytefiles) {
print_file(*bf, std::cout);
}
#endif
// find all builtin variations ad extract them // find all builtin variations ad extract them
BuiltinSubstMap builtins_map; BuiltinSubstMap builtins_map;
{ {
@ -296,14 +289,7 @@ MergeResult merge_files(std::vector<Bytefile *> &&bytefiles) {
size_t code_offset = 0; size_t code_offset = 0;
size_t globals_offset = 1; // NOTE: V,sysargs from, Std size_t globals_offset = 1; // NOTE: V,sysargs from, Std
for (size_t i = 0; i < bytefiles.size(); ++i) { for (size_t i = 0; i < bytefiles.size(); ++i) {
#ifdef DEBUG_VERSION
printf("bytefile <%zu>\n", i);
#endif
for (size_t j = 0; j < bytefiles[i]->public_symbols_number; ++j) { for (size_t j = 0; j < bytefiles[i]->public_symbols_number; ++j) {
#ifdef DEBUG_VERSION
printf("symbol <%zu>:<%zu>\n", i, j);
#endif
const char *name = get_public_name_unsafe(bytefiles[i], j); const char *name = get_public_name_unsafe(bytefiles[i], j);
size_t offset = size_t offset =
@ -312,10 +298,6 @@ MergeResult merge_files(std::vector<Bytefile *> &&bytefiles) {
? globals_offset // NOTE: is global id ? globals_offset // NOTE: is global id
: code_offset); // NOTE: is function offset in code : code_offset); // NOTE: is function offset in code
#ifdef DEBUG_VERSION
printf("symbol %s : %zu (code offset %zu, globals offset %zu)\n", name,
offset, code_offset, globals_offset);
#endif
if (strcmp(name, "main") == 0) { if (strcmp(name, "main") == 0) {
main_offsets.push_back(offset); main_offsets.push_back(offset);
} else if (!publics.insert({name, offset}).second) { } else if (!publics.insert({name, offset}).second) {
@ -349,11 +331,8 @@ MergeResult merge_files(std::vector<Bytefile *> &&bytefiles) {
.globals = 1, // NOTE: V,sysargs from, Std .globals = 1, // NOTE: V,sysargs from, Std
.code = 0, .code = 0,
.publics_num = 0}; .publics_num = 0};
// REMOVE printf("merge bytefiles\n");
for (size_t i = 0; i < bytefiles.size(); ++i) { for (size_t i = 0; i < bytefiles.size(); ++i) {
// REMOVE printf("rewrite offsets %zu\n", i);
rewrite_code_with_offsets(bytefiles[i], offsets); rewrite_code_with_offsets(bytefiles[i], offsets);
// REMOVE printf("subst in code %zu\n", i);
subst_in_code(bytefiles[i], publics, builtins_map); subst_in_code(bytefiles[i], publics, builtins_map);
size_t publics_offset = calc_publics_size(offsets.publics_num); size_t publics_offset = calc_publics_size(offsets.publics_num);
@ -397,12 +376,7 @@ MergeResult merge_files(std::vector<Bytefile *> &&bytefiles) {
// --- // ---
Bytefile *path_mod_load(const char *name, std::filesystem::path &&path) { Bytefile *path_mod_load(const char *name, std::filesystem::path &&path) {
#ifdef DEBUG_VERSION return read_file(path.c_str());
std::cout << "- module path load '" << name << "'\n";
#endif
Bytefile *file = read_file(path.c_str());
return file;
// return read_file(path.c_str());
} }
static std::vector<std::filesystem::path> search_paths; static std::vector<std::filesystem::path> search_paths;
@ -428,26 +402,6 @@ Bytefile *mod_load(const char *name) {
} // extern "C" } // extern "C"
// uint32_t mod_add(Bytefile *module, bool do_verification) {
// #ifdef DEBUG_VERSION
// std::cout << "- add module, no name\n";
// #endif
// return mod_add_impl(module, do_verification);
// }
// ModSearchResult mod_search_pub_symbol(const char *name) {
// auto it = manager.public_symbols_mods.find(name);
// if (it == manager.public_symbols_mods.end()) {
// return {.symbol_offset = 0, .mod_id = 0, .mod_file = NULL};
// }
// return {
// .symbol_offset = it->second.offset,
// .mod_id = it->second.mod_id,
// .mod_file = mod_get(it->second.mod_id),
// };
// }
void mod_load_rec(Bytefile *mod, void mod_load_rec(Bytefile *mod,
std::unordered_map<std::string, Bytefile *> &loaded, std::unordered_map<std::string, Bytefile *> &loaded,
std::vector<Bytefile *> &loaded_ord) { std::vector<Bytefile *> &loaded_ord) {
@ -461,7 +415,7 @@ void mod_load_rec(Bytefile *mod,
#ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
printf("- mod load <%s>\n", import_str); printf("- mod load <%s>\n", import_str);
#endif #endif
Bytefile *import_mod = mod_load(import_str); // TODO Bytefile *import_mod = mod_load(import_str);
if (import_mod == NULL) { if (import_mod == NULL) {
failure("module <%s> not found\n", import_str); failure("module <%s> not found\n", import_str);
} }
@ -580,22 +534,6 @@ BUILTIN id_by_builtin(const char *name) {
return it == std_func.end() ? BUILTIN_NONE : it->second; return it == std_func.end() ? BUILTIN_NONE : it->second;
} }
/* NOTE: all functions have returned value, some values undefined */
// bool is_builtin_with_ret(BUILTIN id) {
// switch (id) {
// case BUILTIN_Lassert:
// case BUILTIN_Lsprintf:
// case BUILTIN_Lprintf:
// case BUILTIN_Lfclose:
// case BUILTIN_Lfwrite:
// case BUILTIN_Lfprintf:
// case BUILTIN_Lfailure:
// return false;
// default:
// return true;
// }
// }
/* NOTE: from src/X86_64.ml: */ /* NOTE: from src/X86_64.ml: */
/* For vararg functions where we pass them in the stdlib function using /* For vararg functions where we pass them in the stdlib function using
va_list, we have to unbox values to print them correctly. For this we have va_list, we have to unbox values to print them correctly. For this we have
@ -827,19 +765,4 @@ void run_stdlib_func(BUILTIN id, size_t args_count) {
failure("RUNTIME ERROR: stdlib function <%u> not found\n", id); failure("RUNTIME ERROR: stdlib function <%u> not found\n", id);
break; break;
} }
// some functions do use on args pointer
// // NOTE: is checked on subst
// if (id > sizeof(std_func) / sizeof(StdFunc)) {
// failure("RUNTIME ERROR: stdlib function <%u> not found\n", id);
// }
// // TODO: move to bytecode verifier
// if ((!func.is_vararg && func.args_count != args_count) ||
// func.args_count > args_count) {
// failure("RUNTIME ERROR: stdlib function <%u> argument count <%zu> is
// not
// "
// "expected (expected is <%s%zu>)\n",
// id, func.args_count, func.is_vararg ? ">=" : "=", args_count);
// }
} }