public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Paul Iannetta <piannetta@kalrayinc.com>
To: binutils@sourceware.org
Cc: Paul Iannetta <piannetta@kalrayinc.com>
Subject: [PATCH 2/7] kvx: Improve lexing & parsing
Date: Fri, 16 Feb 2024 17:42:16 +0100	[thread overview]
Message-ID: <20240216164221.24165-3-piannetta@kalrayinc.com> (raw)
In-Reply-To: <20240216164221.24165-1-piannetta@kalrayinc.com>

Up until now, we used ENV.PROMOTE_IMMEDIATE to get the next candidates,
however this candidate can be directly extracted from the array (in
kvx-parse.h) registering all the immediates.

During lexing, we ignored trailing characters after a number, this is
not good enough since now number can be followed by a modifier.  The
function READ_TOKEN and GET_TOKEN_CLASS have been update to take this
into account.

gas/ChangeLog:

	* config/kvx-parse.c (promote_token): Do not rely on
	  env.promote_immediate anymore.
	(get_token_class): Do not ignore trailing characters after a
	number.
	(read_token): Likewise.
	(print_token_list): THIS SHOULD NOT BE HERE.
---
 gas/config/kvx-parse.c | 53 ++++++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 18 deletions(-)

diff --git a/gas/config/kvx-parse.c b/gas/config/kvx-parse.c
index 0825b182319..bf837fc9b07 100644
--- a/gas/config/kvx-parse.c
+++ b/gas/config/kvx-parse.c
@@ -414,21 +414,25 @@ promote_token (struct token_s tok)
 	  input_line_pointer = tok.insn + tok.begin;
 	  expression (&exp);
 	  input_line_pointer = ilp_save;
-	  int64_t new_class_id = tok.class_id;
-	  int64_t old_class_id = tok.class_id;
-	  while (((new_class_id = env.promote_immediate (old_class_id))
-		  != old_class_id)
-		 && ((exp.X_op == O_symbol
-		      && !(has_relocation_of_size
-			   (str_hash_find (env.reloc_hash,
-					   TOKEN_NAME (new_class_id)))))
-		     || (exp.X_op == O_pseudo_fixup
-			 && !(kvx_get_pseudo_func2
-			      (exp.X_op_symbol,
-			       str_hash_find (env.reloc_hash,
-					      TOKEN_NAME (new_class_id)))))))
-	    old_class_id = new_class_id;
-	  return new_class_id;
+	  uint64_t val = tok.val;
+	  uint64_t pval = ((int64_t) val) < 0 ? -val : val;
+	  int neg_power2_p = ((int64_t) val) < 0 && !(pval & (pval - 1));
+	  struct token_class *class = env.token_classes->imm_classes;
+	  unsigned len = pval ? 8 * sizeof (pval) - __builtin_clzll (pval) : 0;
+
+	  /* Find the imm class */
+	  int imm_idx = 0;
+	  for (imm_idx = 0 ; class[imm_idx].class_id ; ++imm_idx)
+	    if (class[imm_idx].class_id == tok.class_id)
+	      break;
+
+	  while (class[imm_idx + 1].class_id != -1
+	      && ((unsigned int) (class[imm_idx + 1].sz < 0 ? - class[imm_idx + 1].sz - !neg_power2_p : class[imm_idx + 1].sz) < len
+		 || (exp.X_op == O_symbol && !has_relocation_of_size (str_hash_find (env.reloc_hash, TOKEN_NAME (class[imm_idx + 1].class_id))))
+		 || (exp.X_op == 64 && !kvx_get_pseudo_func2 (exp.X_op_symbol, str_hash_find (env.reloc_hash, TOKEN_NAME (class[imm_idx + 1].class_id))))))
+	    imm_idx += 1;
+
+	  return class[imm_idx + 1].class_id == -1 ? class[imm_idx].class_id : class[imm_idx + 1].class_id;
 	}
       default:
 	return tok.class_id;
@@ -481,6 +485,7 @@ get_token_class (struct token_s *token, struct token_classes *classes, int insn_
       char *ilp_save = input_line_pointer;
       input_line_pointer = tok;
       expression (&exp);
+      token->end = token->begin + (input_line_pointer - tok);
       token->val = exp.X_add_number;
       token_val_p = 1;
       input_line_pointer = ilp_save;
@@ -585,6 +590,16 @@ read_token (struct token_s *tok)
   char *str = tok->insn;
   int *begin = &tok->begin;
   int *end = &tok->end;
+  int last_imm_p = 0;
+
+  /* Was the last previous token was an immediate?  */
+  for (int i = 1; *begin - i > 0; ++i)
+    {
+      if ('0' <= str[*begin - i] && str[*begin - i] <= '9')
+	last_imm_p = 1;
+      else if (str[*begin - i] != ' ' && str[*begin - i] != '\t')
+	break;
+    }
 
   /* Eat up all leading spaces.  */
   while (str[*begin] && (str[*begin] == ' ' || str[*begin] == '\n'))
@@ -608,7 +623,9 @@ read_token (struct token_s *tok)
 	return 1;
       }
 
-      if (str[*begin] == '.' && !(*begin > 0 && (str[*begin - 1] == ' ' || is_delim(str[*begin - 1]))))
+      if (str[*begin] == '.'
+	  && (!(*begin > 0 && (str[*begin - 1] == ' ' || is_delim(str[*begin - 1])))
+	    || last_imm_p))
 	modifier_p = 1;
 
       /* This is a modifier or a register */
@@ -669,11 +686,11 @@ print_token_list (struct token_list *lst)
   struct token_list *cur = lst;
   while (cur)
     {
-      printf_debug (1, "%s (%d : %s : %d) / ",
+      printf_debug (0, "%s (%llu : %s : %llu) / ",
 	      cur->tok, cur->val, TOKEN_NAME (cur->class_id), cur->loc);
       cur = cur->next;
     }
-  printf_debug (1, "\n");
+  printf_debug (0, "\n");
 }
 
 void
-- 
2.35.1.500.gb896f729e2






  parent reply	other threads:[~2024-02-16 16:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 16:42 [PATCH 0/7] kvx: Miscellaneous changes since last August Paul Iannetta
2024-02-16 16:42 ` [PATCH 1/7] kvx: gas: fix the detection of negative powers of 2 Paul Iannetta
2024-02-16 16:42 ` Paul Iannetta [this message]
2024-02-16 16:42 ` [PATCH 3/7] kvx: gas: fix leak Paul Iannetta
2024-02-16 16:42 ` [PATCH 4/7] kvx: gas: move the splat modifier to the immediate Paul Iannetta
2024-02-16 16:42 ` [PATCH 5/7] kvx: gas: rename: or -> ior, xor -> eor Paul Iannetta
2024-02-16 16:42 ` [PATCH 6/7] kvx: enable magic immediates for integer multiply-accumulate and CMOVE* Paul Iannetta
2024-02-16 16:42 ` [PATCH 7/7] kvx: gas: missing aliases for $r14r15 in assembler Paul Iannetta
2024-02-19 11:35 ` [PATCH 0/7] kvx: Miscellaneous changes since last August Nick Clifton
2024-02-20 11:03   ` Paul Iannetta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240216164221.24165-3-piannetta@kalrayinc.com \
    --to=piannetta@kalrayinc.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).