From 474b0a8ed28cd42476b71c5b529e82ef460806df Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sat, 12 Apr 2025 22:19:28 +0300 Subject: [PATCH] SM fixes, add fixes to vork with sysargs Std variable --- byterun/src/interpreter.c | 3 ++- byterun/src/module_manager.cpp | 23 ++++++++++++++++++----- src/SM.ml | 14 +++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index 432881936..2a9a72cca 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -72,7 +72,8 @@ void set_argc_argv(int argc, char **argv) { void *args_array = Barray((aint *)s_peek(), BOX(argc)); s_popn(argc); s_push(args_array); - *var_by_category(VAR_GLOBAL, 0) = args_array; // NOTE: implementation detail, stdlib/test26.lama + // NOTE: V,sysargs from Std + *var_by_category(VAR_GLOBAL, 0) = args_array; #ifdef DEBUG_VERSION print_stack(&s); diff --git a/byterun/src/module_manager.cpp b/byterun/src/module_manager.cpp index 5d992438e..2816876f0 100644 --- a/byterun/src/module_manager.cpp +++ b/byterun/src/module_manager.cpp @@ -108,8 +108,13 @@ void rewrite_code_with_offsets(Bytefile *bytefile, const Offsets &offsets) { case Cmd::LDA: case Cmd::ST: if (to_var_category(l) == VAR_GLOBAL) { - ip_write_int_unsafe(write_ip, - ip_read_int_unsafe(&read_ip) + offsets.globals); + aint id = ip_read_int_unsafe(&read_ip); + if (id > 0) { // NOTE: do not rewrite sysargs usages + ip_write_int_unsafe( + write_ip, + id - 1 + offsets.globals); // rewrite with exclusion of local + // module sysargs reference + } } break; default: @@ -239,7 +244,8 @@ Offsets calc_merge_sizes(const std::vector &bytefiles) { Offsets sizes{.strings = 0, .globals = 0, .code = 0, .publics_num = 0}; for (size_t i = 0; i < bytefiles.size(); ++i) { sizes.strings += bytefiles[i]->stringtab_size; - sizes.globals += bytefiles[i]->global_area_size; + sizes.globals += bytefiles[i]->global_area_size - + 1; // NOTE: exclude default variable sysargs sizes.code += bytefiles[i]->code_size; // sizes.publics_num += bytefiles[i]->public_symbols_number; } @@ -268,6 +274,9 @@ MergeResult merge_files(std::vector &&bytefiles) { gen_builtins(sizes.code, builtins_map); sizes.code += builtins_code_size; + // V,sysparams fro, Std + ++sizes.globals; + Bytefile *result = (Bytefile *)malloc(sizeof(Bytefile) + sizes.strings + sizes.code + public_symbols_size); // globals are on the stack @@ -322,7 +331,10 @@ MergeResult merge_files(std::vector &&bytefiles) { result->substs_ptr = NULL; // update & merge code segments - Offsets offsets{.strings = 0, .globals = 0, .code = 0, .publics_num = 0}; + Offsets offsets{.strings = 0, + .globals = 1, + .code = 0, + .publics_num = 0}; // NOTE: one global: sysargs // REMOVE printf("merge bytefiles\n"); for (size_t i = 0; i < bytefiles.size(); ++i) { // REMOVE printf("rewrite offsets %zu\n", i); @@ -346,7 +358,8 @@ MergeResult merge_files(std::vector &&bytefiles) { // update offsets offsets.strings += bytefiles[i]->stringtab_size; - offsets.globals += bytefiles[i]->global_area_size; + offsets.globals += + bytefiles[i]->global_area_size - 1; // NOTE: exclude sysargs offsets.code += bytefiles[i]->code_size; // offsets.publics_num += bytefiles[i]->public_symbols_number; diff --git a/src/SM.ml b/src/SM.ml index 45cf47779..a0bb36363 100644 --- a/src/SM.ml +++ b/src/SM.ml @@ -176,8 +176,8 @@ module ByteCode = struct let externs = Stdlib.ref S.empty in let pubs = Stdlib.ref S.empty in let imports = Stdlib.ref S.empty in - let globals = Stdlib.ref M.empty in - let glob_count = Stdlib.ref 0 in + let globals = Stdlib.ref @@ M.add "sysargs" 0 @@ M.empty in (* sysargs is a vaiable from Std *) + let glob_count = Stdlib.ref 1 in (* sysargs *) let fixups = Stdlib.ref [] in let func_fixups = Stdlib.ref [] in let add_lab l = lmap := M.add l (Buffer.length code) !lmap in @@ -298,7 +298,7 @@ module ByteCode = struct (* 0x54 l:32 n:32 d*:32 *) | CLOSURE (s, ds) -> add_bytes [ (5 * 16) + 4 ]; - add_fixup s; + add_func_fixup s; add_ints [ 0; List.length ds ]; add_designations None ds (* 0x55 n:32 *) @@ -373,7 +373,7 @@ module ByteCode = struct @@ try M.find l !lmap with Not_found -> - failwith (Printf.sprintf "ERROR: undefined label '%s'" l) )) + failwith (Printf.sprintf "ERROR: undefined label of public '%s'" l) )) @@ S.elements !pubs in let str_table = Buffer.to_bytes st.StringTab.buffer in @@ -1314,6 +1314,10 @@ class env cmd imports = end [@@ocaml.warning "-15"] let compile cmd ((imports, _), p) = + (* TODO: better solution *) + let replace_escaped_symbols s = Str.global_replace (Str.regexp "\\\\n") "\n" @@ + Str.global_replace (Str.regexp "\\\\r") "\r" @@ + Str.global_replace (Str.regexp "\\\\t") "\t" s in let rec pattern env lfalse = function | Pattern.Wildcard -> (env, false, [ DROP ]) | Pattern.Named (_, p) -> pattern env lfalse p @@ -1477,7 +1481,7 @@ let compile cmd ((imports, _), p) = | _ -> (env, false, line @ [ LD acc ])) | Expr.Ref _ -> failwith "Should not happen. Indirect assignemts are temporarily prohibited." | Expr.Const n -> (env, false, [ CONST n ]) - | Expr.String s -> (env, false, [ STRING s ]) + | Expr.String s -> (env, false, [ STRING (replace_escaped_symbols s) ]) | Expr.Binop (op, x, y) -> let lop, env = env#get_label in add_code (compile_list false lop env [ x; y ]) lop false [ BINOP op ]