poke-devel
[Top][All Lists]
Advanced

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

[COMMITTED] pkl: emit parse error in asm templates


From: Jose E. Marchesi
Subject: [COMMITTED] pkl: emit parse error in asm templates
Date: Sat, 04 Feb 2023 16:50:22 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

2023-02-04  Jose E. Marchesi  <jemarch@gnu.org>

        * libpoke/pkl-gen.c (pkl_gen_pr_asm_stmt): Emit error whenever
        necessary.
        (pkl_gen_pr_asm_exp): Likewise.

        * libpoke/pvm-program.c (pvm_program_parse_from_string): Return an
        error status in the form of an error message.
        * libpoke/pkl-asm.c (pkl_asm_from_string): Likewise.
---
 ChangeLog             | 10 ++++++++++
 libpoke/pkl-asm.c     |  7 +++++--
 libpoke/pkl-asm.h     | 10 ++++++++--
 libpoke/pkl-gen.c     | 26 +++++++++++++++++++++-----
 libpoke/pvm-program.c | 15 +++++++++++++--
 libpoke/pvm-program.h |  9 +++++++--
 6 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e5842d1b..3a31f2c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-02-04  Jose E. Marchesi  <jemarch@gnu.org>
+
+       * libpoke/pkl-gen.c (pkl_gen_pr_asm_stmt): Emit error whenever
+       necessary.
+       (pkl_gen_pr_asm_exp): Likewise.
+
+       * libpoke/pvm-program.c (pvm_program_parse_from_string): Return an
+       error status in the form of an error message.
+       * libpoke/pkl-asm.c (pkl_asm_from_string): Likewise.
+
 2023-02-03  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pkl-tab.y (declaration): improve error message.
diff --git a/libpoke/pkl-asm.c b/libpoke/pkl-asm.c
index f09050e7..6671038b 100644
--- a/libpoke/pkl-asm.c
+++ b/libpoke/pkl-asm.c
@@ -2231,12 +2231,15 @@ pkl_asm_label (pkl_asm pasm, pvm_program_label label)
   pvm_program_append_label (pasm->program, label);
 }
 
-void
+char *
 pkl_asm_from_string (pkl_asm pasm, const char *str)
 {
   char *expanded_template
     = pvm_program_expand_asm_template (str);
 
-  pvm_program_parse_from_string (expanded_template, pasm->program);
+  char *ret
+    = pvm_program_parse_from_string (expanded_template, pasm->program);
   free (expanded_template);
+
+  return ret;
 }
diff --git a/libpoke/pkl-asm.h b/libpoke/pkl-asm.h
index d9aab802..73c9ef34 100644
--- a/libpoke/pkl-asm.h
+++ b/libpoke/pkl-asm.h
@@ -254,7 +254,13 @@ pvm_program_label pkl_asm_fresh_label (pkl_asm pasm);
 /* Append a label.  */
 void pkl_asm_label (pkl_asm pasm, pvm_program_label label);
 
-/* Assembly from a buffer containing PVM assembly code.  */
-void pkl_asm_from_string (pkl_asm pasm, const char *str);
+/* Assembly from a buffer containing PVM assembly code.
+
+   If there is a parse error, the function returns an allocated string
+   with the name of the offending token.  The caller is expected to
+   free the memory occupied by that string.  In absence of errors, this
+   function returns NULL.  */
+
+char *pkl_asm_from_string (pkl_asm pasm, const char *str);
 
 #endif /* PKL_ASM_H */
diff --git a/libpoke/pkl-gen.c b/libpoke/pkl-gen.c
index 46b12ca2..600aad78 100644
--- a/libpoke/pkl-gen.c
+++ b/libpoke/pkl-gen.c
@@ -4666,6 +4666,7 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_asm_stmt)
 {
   pkl_ast_node asm_stmt = PKL_PASS_NODE;
   pkl_ast_node input, output;
+  char *asm_ret;
 
   /* Push a canary to the stack.  */
   pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH, PVM_NULL);
@@ -4678,9 +4679,16 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_asm_stmt)
       PKL_PASS_SUBPASS (input);
     }
 
-  /* Assembly the sm template.  */
-  pkl_asm_from_string (PKL_GEN_ASM,
-                       PKL_AST_IDENTIFIER_POINTER (PKL_AST_ASM_STMT_TEMPLATE 
(asm_stmt)));
+  /* Assembly the asm template.  */
+  asm_ret = pkl_asm_from_string (PKL_GEN_ASM,
+                                 PKL_AST_IDENTIFIER_POINTER 
(PKL_AST_ASM_STMT_TEMPLATE (asm_stmt)));
+  if (asm_ret != NULL)
+    {
+      PKL_ERROR (PKL_AST_LOC (PKL_AST_ASM_STMT_TEMPLATE (asm_stmt)),
+                 "error parsing asm template near %s", asm_ret);
+      free (asm_ret);
+      PKL_PASS_ERROR;
+    }
 
   /* Generate the output assignments.  */
   for (output = PKL_AST_ASM_STMT_OUTPUTS (asm_stmt);
@@ -4718,6 +4726,7 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_asm_exp)
 {
   pkl_ast_node asm_exp = PKL_PASS_NODE;
   pkl_ast_node input;
+  char *asm_ret;
 
   /* Push a canary to the stack.  */
   pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSH, PVM_NULL);
@@ -4731,8 +4740,15 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_asm_exp)
     }
 
   /* Assembly the asm template.  */
-  pkl_asm_from_string (PKL_GEN_ASM,
-                       PKL_AST_IDENTIFIER_POINTER (PKL_AST_ASM_EXP_TEMPLATE 
(asm_exp)));
+  asm_ret = pkl_asm_from_string (PKL_GEN_ASM,
+                                 PKL_AST_IDENTIFIER_POINTER 
(PKL_AST_ASM_EXP_TEMPLATE (asm_exp)));
+  if (asm_ret != NULL)
+    {
+      PKL_ERROR (PKL_AST_LOC (PKL_AST_ASM_STMT_TEMPLATE (asm_exp)),
+                 "error parsing asm template near %s", asm_ret);
+      free (asm_ret);
+      PKL_PASS_ERROR;
+    }
 
   /* Check and drop the canary.  */
   {
diff --git a/libpoke/pvm-program.c b/libpoke/pvm-program.c
index 76c6b424..1a3c1ff2 100644
--- a/libpoke/pvm-program.c
+++ b/libpoke/pvm-program.c
@@ -358,10 +358,21 @@ pvm_program_expand_asm_template (const char *str)
   return expanded;
 }
 
-void
+char *
 pvm_program_parse_from_string (const char *str, pvm_program program)
 {
-  pvm_parse_mutable_routine_from_string (str, program->routine);
+  struct pvm_routine_parse_error *err;
+
+  err = pvm_parse_mutable_routine_from_string (str, program->routine);
+  if (err != NULL)
+    {
+      char *invalid_token = xstrdup (err->error_token_text);
+      pvm_routine_parse_error_destroy (err);
+      return invalid_token;
+    }
+
+  /* No error.  */
+  return NULL;
 }
 
 void
diff --git a/libpoke/pvm-program.h b/libpoke/pvm-program.h
index a28d136c..3e901381 100644
--- a/libpoke/pvm-program.h
+++ b/libpoke/pvm-program.h
@@ -42,8 +42,13 @@ pvm_routine pvm_program_routine (pvm_program program);
 char *pvm_program_expand_asm_template (const char *str);
 
 /* Parse PVM instructions from the given string and append them to
-   the given program.  */
+   the given program.
 
-void pvm_program_parse_from_string (const char *str, pvm_program program);
+   If there is a parse error, the function returns an allocated string
+   with the name of the offending token.  The caller is expected to
+   free the memory occupied by that string.  In absence of errors, this
+   function returns NULL.  */
+
+char *pvm_program_parse_from_string (const char *str, pvm_program program);
 
 #endif /* ! PVM_PROGRAM_H */
-- 
2.30.2




reply via email to

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