poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[COMMITTED] pkl: use the right compilation environment in GEN pass


From: Jose E. Marchesi
Subject: [COMMITTED] pkl: use the right compilation environment in GEN pass
Date: Sun, 23 Jan 2022 18:01:31 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

The code generation pass now uses the compilation environment being
compiled, as it should be.

So we are back to having a single pkl-rt.pk file.

2022-01-23  Jose E. Marchesi  <jemarch@gnu.org>

        * libpoke/ras: Document RAS_COMP_ENV and RAS_COMPILER and use them.
        * libpoke/pkl-gen.c (RAS_COMP_ENV): define.
        (RAS_COMPILER): Likewise.
        * libpoke/pkl-gen.h (struct pkl_gen_payload): New field `env'.
        (pkl_gen_init_payload): Install `env' in the GEN payload.
        * libpoke/pkl-asm.c (pkl_asm_call): Get an `env' argument.
        * libpoke/pkl.c (rest_of_compilation): Get an `env' argument.
        (pkl_execute_buffer): Pass `env' to rest_of_compilation.
        (pkl_execute_statement): Likewise.
        (pkl_compile_expression): Likewise.
        (pkl_execute_expression): Likewise.
        (pkl_execute_file): Likewise.
        * libpoke/pk-rt-2.pk: Remove file.
        * libpoke/pkl-rt.k: Rename from pkl-rt-1.pk
        * libpoke/Makefile.am (dist_pkgdata_DATA): Adapt accordingly.
---
 ChangeLog                          | 18 +++++++++++++++
 libpoke/Makefile.am                |  2 +-
 libpoke/libpoke.h                  |  2 +-
 libpoke/pkl-asm.c                  |  7 +++---
 libpoke/pkl-asm.h                  |  6 +++--
 libpoke/pkl-gen.c                  |  7 ++++--
 libpoke/pkl-gen.h                  |  5 +++-
 libpoke/pkl-rt-2.pk                | 47 --------------------------------------
 libpoke/{pkl-rt-1.pk => pkl-rt.pk} | 25 +++++++++++++++++++-
 libpoke/pkl.c                      | 24 ++++++++-----------
 libpoke/pvm-val.c                  |  2 +-
 libpoke/pvm.h                      |  2 +-
 libpoke/ras                        | 10 ++++++--
 13 files changed, 79 insertions(+), 78 deletions(-)
 delete mode 100644 libpoke/pkl-rt-2.pk
 rename libpoke/{pkl-rt-1.pk => pkl-rt.pk} (95%)

diff --git a/ChangeLog b/ChangeLog
index 65624591..c4a87213 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2022-01-23  Jose E. Marchesi  <jemarch@gnu.org>
 
+       * libpoke/ras: Document RAS_COMP_ENV and RAS_COMPILER and use them.
+       * libpoke/pkl-gen.c (RAS_COMP_ENV): define.
+       (RAS_COMPILER): Likewise.
+       * libpoke/pkl-gen.h (struct pkl_gen_payload): New field `env'.
+       (pkl_gen_init_payload): Install `env' in the GEN payload.
+       * libpoke/pkl-asm.c (pkl_asm_call): Get an `env' argument.
+       * libpoke/pkl.c (rest_of_compilation): Get an `env' argument.
+       (pkl_execute_buffer): Pass `env' to rest_of_compilation.
+       (pkl_execute_statement): Likewise.
+       (pkl_compile_expression): Likewise.
+       (pkl_execute_expression): Likewise.
+       (pkl_execute_file): Likewise.
+       * libpoke/pk-rt-2.pk: Remove file.
+       * libpoke/pkl-rt.k: Rename from pkl-rt-1.pk
+       * libpoke/Makefile.am (dist_pkgdata_DATA): Adapt accordingly.
+
+2022-01-23  Jose E. Marchesi  <jemarch@gnu.org>
+
        * libpoke/libpoke.c (pk_compile_file): Fix indentation.
        * libpoke/pkl.c (pkl_execute_file): Return an error in case of
        memory exhaustion.
diff --git a/libpoke/Makefile.am b/libpoke/Makefile.am
index 327705eb..05f8cdbd 100644
--- a/libpoke/Makefile.am
+++ b/libpoke/Makefile.am
@@ -22,7 +22,7 @@ CLEANFILES =
 DISTCLEANFILES =
 MAINTAINERCLEANFILES =
 
-dist_pkgdata_DATA = pkl-rt-1.pk pkl-rt-2.pk std.pk std-types.pk
+dist_pkgdata_DATA = pkl-rt.pk std.pk std-types.pk
 dist_pkgconfig_lib_DATA = $(pkgconfig_libfile)
 
 lib_LTLIBRARIES = libpoke.la
diff --git a/libpoke/libpoke.h b/libpoke/libpoke.h
index 05d8373b..8f217880 100644
--- a/libpoke/libpoke.h
+++ b/libpoke/libpoke.h
@@ -42,7 +42,7 @@ typedef uint64_t pk_val;
 #define PK_EINVAL 4
 
 /* The following macros are standard exception codes defined in
-   pkl-rt-1.pk.  */
+   pkl-rt.pk.  */
 
 #define PK_EC_GENERIC       0
 #define PK_EC_DIV_BY_ZERO   1
diff --git a/libpoke/pkl-asm.c b/libpoke/pkl-asm.c
index 26dfcfd5..06f7ad70 100644
--- a/libpoke/pkl-asm.c
+++ b/libpoke/pkl-asm.c
@@ -2106,15 +2106,14 @@ pkl_asm_for_in_endloop (pkl_asm pasm)
 }
 
 void
-pkl_asm_call (pkl_asm pasm, const char *funcname)
+pkl_asm_call (pkl_asm pasm, pkl_env env, const char *funcname)
 {
-  pkl_env compiler_env = pkl_get_env (pasm->compiler);
   int back, over;
   pkl_ast_node tmp;
 
-  assert (pkl_env_toplevel_p (compiler_env));
+  assert (pkl_env_toplevel_p (env));
 
-  tmp = pkl_env_lookup (compiler_env, PKL_ENV_NS_MAIN,
+  tmp = pkl_env_lookup (env, PKL_ENV_NS_MAIN,
                         funcname, &back, &over);
   assert (tmp != NULL);
   assert (back == 0);
diff --git a/libpoke/pkl-asm.h b/libpoke/pkl-asm.h
index 7e39b06d..618781dc 100644
--- a/libpoke/pkl-asm.h
+++ b/libpoke/pkl-asm.h
@@ -24,6 +24,7 @@
 
 #include "pkl.h" /* For pkl_compiler */
 #include "pkl-ast.h"
+#include "pkl-env.h"
 #include "ios.h" /* For IOS_NENC_* and IOS_ENDIAN_* */
 #include "pvm.h"
 
@@ -83,9 +84,10 @@ pvm_program pkl_asm_finish (pkl_asm pasm, int epilogue);
 void pkl_asm_insn (pkl_asm pasm, enum pkl_asm_insn insn, ...);
 
 /* Emit assembly code for calling the function FUNCNAME, which should
-   be defined in the global environment.  */
+   be defined in the environment ENV.  Note that ENV is required to be
+   a top-level environment.  */
 
-void pkl_asm_call (pkl_asm pasm, const char *funcname);
+void pkl_asm_call (pkl_asm pasm, pkl_env env, const char *funcname);
 
 /* Conditionals.
  *
diff --git a/libpoke/pkl-gen.c b/libpoke/pkl-gen.c
index e5cfc9a4..bcb6159a 100644
--- a/libpoke/pkl-gen.c
+++ b/libpoke/pkl-gen.c
@@ -112,6 +112,8 @@
 #define RAS_ASM PKL_GEN_ASM
 #define RAS_PUSH_ASM PKL_GEN_PUSH_ASM
 #define RAS_POP_ASM PKL_GEN_POP_ASM
+#define RAS_COMPILER (PKL_GEN_PAYLOAD->compiler)
+#define RAS_COMP_ENV (PKL_GEN_PAYLOAD->env)
 #include "pkl-gen.pkc"
 #include "pkl-gen-builtins.pkc"
 
@@ -1427,7 +1429,8 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_format)
               pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH, idx); /* ARR ARR IDX */
               pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH,
                             pvm_make_ulong (n, 64));         /* ARR ARR IDX 
LEN */
-              pkl_asm_call (PKL_GEN_ASM, "_pkl_reduce_string_array"); /* ARR 
STR */
+              pkl_asm_call (PKL_GEN_ASM, PKL_GEN_PAYLOAD->env,
+                            "_pkl_reduce_string_array"); /* ARR STR */
               pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_SWAP);     /* STR ARR */
 
               for (; n > 1; --n)
@@ -1520,7 +1523,7 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_format)
       pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_DUP);
       pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH, pvm_make_ulong (0, 64));
       pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH, pvm_make_ulong (nstr, 64));
-      pkl_asm_call (PKL_GEN_ASM, "_pkl_reduce_string_array");
+      pkl_asm_call (PKL_GEN_ASM, PKL_GEN_PAYLOAD->env, 
"_pkl_reduce_string_array");
       pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_NIP);
     }
 
diff --git a/libpoke/pkl-gen.h b/libpoke/pkl-gen.h
index 022d60c1..6de48fd3 100644
--- a/libpoke/pkl-gen.h
+++ b/libpoke/pkl-gen.h
@@ -91,6 +91,7 @@ struct pkl_gen_payload
   int constructor_depth;
   int mapper_depth;
   int in_file_p;
+  pkl_env env;
 };
 
 typedef struct pkl_gen_payload *pkl_gen_payload;
@@ -119,10 +120,12 @@ typedef struct pkl_gen_payload *pkl_gen_payload;
 extern struct pkl_phase pkl_phase_gen;
 
 static inline void
-pkl_gen_init_payload (pkl_gen_payload payload, pkl_compiler compiler)
+pkl_gen_init_payload (pkl_gen_payload payload, pkl_compiler compiler,
+                      pkl_env env)
 {
   memset (payload, 0, sizeof (struct pkl_gen_payload));
   payload->compiler = compiler;
+  payload->env = env;
 }
 
 
diff --git a/libpoke/pkl-rt-2.pk b/libpoke/pkl-rt-2.pk
deleted file mode 100644
index 745af2b6..00000000
--- a/libpoke/pkl-rt-2.pk
+++ /dev/null
@@ -1,47 +0,0 @@
-/* pkl-rt-2.pkl - Run-time library for the poke compiler.  */
-
-/* Copyright (C) 2021, 2022 The poke authors.  */
-
-/* This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-/* Any function that uses `format' cannot be used in `pkl-rt-1'.
-   The reason why the problem is happening is that `format' is using
-   `pkl_asm_call' to call `_pkl_reduce_string_array' and `pkl_asm_call'
-   relies on the global environment in the compiler and at that time the
-   on-going environment is not added yet.
-   So we have to put functions that use `format' here, in `pkl-rt-2'.  */
-
-/* Assertion function.
-
-   The compiler transforms assert statement to invocation of this
-   function.  COND is first argument of assert statement, and MSG is
-   the optional second argument.  FILENAME is the name of source
-   file.  LINE and COL are, respectively, line and column number
-   of the assert statement in the source file.  */
-
-fun _pkl_assert = (uint<64> cond, string msg, string filename,
-                   uint<64> line, uint<64> col) void:
-  {
-    if (cond)
-      return;
-
-    raise Exception {
-      code = EC_assert,
-      name = "assertion failure",
-      location = format ("%s:%u64d:%u64d:", filename, line, col),
-      msg = msg,
-      exit_status = 1,
-    };
-  }
diff --git a/libpoke/pkl-rt-1.pk b/libpoke/pkl-rt.pk
similarity index 95%
rename from libpoke/pkl-rt-1.pk
rename to libpoke/pkl-rt.pk
index 1fb69527..2cfcdc1f 100644
--- a/libpoke/pkl-rt-1.pk
+++ b/libpoke/pkl-rt.pk
@@ -1,4 +1,4 @@
-/* pkl-rt-1.pk - Run-time library for the poke compiler.  */
+/* pkl-rt.pk - Run-time library for the poke compiler.  */
 
 /* Copyright (C) 2019, 2020, 2021, 2022 Jose E. Marchesi.  */
 
@@ -392,6 +392,29 @@ fun _pkl_unit_name = (uint<64> bits) string:
     return "";
   }
 
+/* Assertion function.
+
+   The compiler transforms assert statement to invocation of this
+   function.  COND is first argument of assert statement, and MSG is
+   the optional second argument.  FILENAME is the name of source
+   file.  LINE and COL are, respectively, line and column number
+   of the assert statement in the source file.  */
+
+fun _pkl_assert = (uint<64> cond, string msg, string filename,
+                   uint<64> line, uint<64> col) void:
+  {
+    if (cond)
+      return;
+
+    raise Exception {
+      code = EC_assert,
+      name = "assertion failure",
+      location = format ("%s:%u64d:%u64d:", filename, line, col),
+      msg = msg,
+      exit_status = 1,
+    };
+  }
+
 /* The Type struct below describes the values returned by the `typeof'
    language construction.  This type is used in the compiler phases,
    so pretty please do not use `typeof' until the compiler has been
diff --git a/libpoke/pkl.c b/libpoke/pkl.c
index 1aa2bec7..c5e68718 100644
--- a/libpoke/pkl.c
+++ b/libpoke/pkl.c
@@ -119,14 +119,7 @@ pkl_new (pvm vm, const char *rt_path, uint32_t flags)
   /* Bootstrap the compiler.  An error bootstraping is an internal
      error and should be reported as such.  */
   {
-    char *poke_rt_pk = pk_str_concat (rt_path, "/pkl-rt-1.pk", NULL);
-    if (!poke_rt_pk)
-      goto out_of_memory;
-
-    if (!pkl_load_rt (compiler, poke_rt_pk))
-      return NULL;
-
-    poke_rt_pk = pk_str_concat (rt_path, "/pkl-rt-2.pk", NULL);
+    char *poke_rt_pk = pk_str_concat (rt_path, "/pkl-rt.pk", NULL);
     if (!poke_rt_pk)
       goto out_of_memory;
 
@@ -188,7 +181,8 @@ pkl_free (pkl_compiler compiler)
 
 static pvm_program
 rest_of_compilation (pkl_compiler compiler,
-                     pkl_ast ast)
+                     pkl_ast ast,
+                     pkl_env env)
 {
   struct pkl_gen_payload gen_payload;
 
@@ -263,7 +257,7 @@ rest_of_compilation (pkl_compiler compiler,
   pkl_trans_init_payload (&trans2_payload);
   pkl_trans_init_payload (&trans3_payload);
   pkl_trans_init_payload (&trans4_payload);
-  pkl_gen_init_payload (&gen_payload, compiler);
+  pkl_gen_init_payload (&gen_payload, compiler, env);
 
   if (!pkl_do_pass (compiler, ast,
                     frontend_phases, frontend_payloads, PKL_PASS_F_TYPES, 1))
@@ -327,7 +321,7 @@ pkl_execute_buffer (pkl_compiler compiler,
     /* Memory exhaustion.  */
     goto error;
 
-  program = rest_of_compilation (compiler, ast);
+  program = rest_of_compilation (compiler, ast, env);
   if (program == NULL)
     goto error;
 
@@ -375,7 +369,7 @@ pkl_execute_statement (pkl_compiler compiler,
     /* Memory exhaustion.  */
     goto error;
 
-  program = rest_of_compilation (compiler, ast);
+  program = rest_of_compilation (compiler, ast, env);
   if (program == NULL)
     goto error;
 
@@ -417,7 +411,7 @@ pkl_compile_expression (pkl_compiler compiler,
      /* Memory exhaustion.  */
      goto error;
 
-   program = rest_of_compilation (compiler, ast);
+   program = rest_of_compilation (compiler, ast, env);
    if (program == NULL)
      goto error;
 
@@ -456,7 +450,7 @@ pkl_execute_expression (pkl_compiler compiler,
     /* Memory exhaustion.  */
     goto error;
 
-  program = rest_of_compilation (compiler, ast);
+  program = rest_of_compilation (compiler, ast, env);
   if (program == NULL)
     goto error;
 
@@ -503,7 +497,7 @@ pkl_execute_file (pkl_compiler compiler, const char *fname,
     /* Memory exhaustion.  */
     goto error;
 
-  program = rest_of_compilation (compiler, ast);
+  program = rest_of_compilation (compiler, ast, env);
   if (program == NULL)
     goto error;
 
diff --git a/libpoke/pvm-val.c b/libpoke/pvm-val.c
index 38a35856..9dfb8fd7 100644
--- a/libpoke/pvm-val.c
+++ b/libpoke/pvm-val.c
@@ -1719,7 +1719,7 @@ pvm_call_pretty_printer (pvm vm, pvm_val val, pvm_val 
*exit_exception)
 }
 
 /* IMPORTANT: please keep pvm_make_exception in sync with the
-   definition of the struct Exception in pkl-rt-1.pk.  */
+   definition of the struct Exception in pkl-rt.pk.  */
 
 pvm_val
 pvm_make_exception (int code, const char *name, int exit_status,
diff --git a/libpoke/pvm.h b/libpoke/pvm.h
index 5cde57fd..4baee433 100644
--- a/libpoke/pvm.h
+++ b/libpoke/pvm.h
@@ -489,7 +489,7 @@ enum pvm_exit_code
   };
 
 /* Exceptions.  These should be in sync with the exception code macros
-   in libpoke.h, and variables and exception names, declared in pkl-rt-1.pk */
+   in libpoke.h, and variables and exception names, declared in pkl-rt.pk */
 
 #define PVM_E_GENERIC       0
 #define PVM_E_GENERIC_NAME "generic"
diff --git a/libpoke/ras b/libpoke/ras
index 16ff49a5..04e3f537 100755
--- a/libpoke/ras
+++ b/libpoke/ras
@@ -63,6 +63,12 @@
 #    This macro is invoked by RAS when it no longer needs the
 #    current assembler.
 #
+# RAS_COMPILER
+#    The PKL compiler.
+#
+# RAS_COMP_ENV
+#    Compilation environment.
+#
 # Example;
 #
 #  #define RAS_ASM PKL_GEN_ASM
@@ -710,7 +716,7 @@ BEGIN {
     out("\tpvm_program program;                         \\")
     out("\t                                             \\")
     out("\tRAS_PUSH_ASM (pkl_asm_new (PKL_PASS_AST,              \\")
-    out("\t                           PKL_GEN_PAYLOAD->compiler, \\")
+    out("\t                           RAS_COMPILER,              \\")
     out("\t                           0 /* prologue */));        \\")
     out("\t                                                      \\")
     out("---LABELDECLS---")
@@ -860,7 +866,7 @@ BEGIN {
 /^[ \t]*\.call[ \t]+[a-zA-Z_][0-9a-zA-Z_]*[ \t]*(;.*)?$/ {
     fname = gensub (/^[ \t]*\.call[ \t]+([a-zA-Z_][0-9a-zA-Z_]*)[ 
\t]*(;.*)?$/, \
                     "\\1", 1, $0)
-    out("\tpkl_asm_call (RAS_ASM,\"" fname "\"); \\")
+    out("\tpkl_asm_call (RAS_ASM, RAS_COMP_ENV, \"" fname "\"); \\")
     next
 }
 
-- 
2.11.0





reply via email to

[Prev in Thread] Current Thread [Next in Thread]