linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Ramsay Jones <ramsay@ramsayjones.plus.com>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH v2 2/8] builtin: define a symbol_op for a generic op acting on integer
Date: Mon, 12 Apr 2021 23:21:05 +0200	[thread overview]
Message-ID: <20210412212111.29186-3-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20210412212111.29186-1-luc.vanoostenryck@gmail.com>

This can be used to define some generic (polymorphic) builtin
with a signature like:
	<name>(int)
	<name>(T, T)
	<name>(int, T)
	<name>(int, T, long, T, ... T)
where T is some integer type which will be instantiated at each call.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 builtin.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin.h |  2 ++
 2 files changed, 63 insertions(+)

diff --git a/builtin.c b/builtin.c
index ff03dbab9a06..928e03050375 100644
--- a/builtin.c
+++ b/builtin.c
@@ -390,6 +390,67 @@ static struct symbol_op overflow_p_op = {
 };
 
 
+///
+// Evaluate the arguments of 'generic' integer operators.
+//
+// Parameters with a complete type are used like in a normal prototype.
+// The first parameter with a 'dynamic' type will be consider
+// as polymorphic and for each calls will be instancied with the type
+// of its effective argument.
+// The next dynamic parameters will the use this polymorphic type.
+// This allows to declare functions with some parameters having
+// a type variably defined at call time:
+//	int foo(int, T, T);
+static int evaluate_generic_int_op(struct expression *expr)
+{
+	struct symbol *fntype = expr->fn->ctype->ctype.base_type;
+	struct symbol_list *types = NULL;
+	struct symbol *ctype = NULL;
+	struct expression *arg;
+	struct symbol *t;
+	int n = 0;
+
+	PREPARE_PTR_LIST(fntype->arguments, t);
+	FOR_EACH_PTR(expr->args, arg) {
+		if (!is_dynamic_type(t)) {
+			;
+		} else if (!ctype) {
+			// fist 'dynamic' type, chat that it is an integer
+			t = arg->ctype;
+			if (!t)
+				return 0;
+			if (t->type == SYM_NODE)
+				t = t->ctype.base_type;
+			if (!t)
+				return 0;
+			if (t->ctype.base_type != &int_type)
+				goto err;
+
+			// next 'dynamic' arguments will use this type
+			ctype = t;
+		} else {
+			// use the previous 'dynamic' type
+			t = ctype;
+		}
+		add_ptr_list(&types, t);
+		NEXT_PTR_LIST(t);
+	} END_FOR_EACH_PTR(arg);
+	FINISH_PTR_LIST(t);
+	return evaluate_arguments(types, expr->args);
+
+err:
+	sparse_error(arg->pos, "non-integer type for argument %d:", n);
+	info(arg->pos, "        %s", show_typename(arg->ctype));
+	expr->ctype = &bad_ctype;
+	return 0;
+}
+
+struct symbol_op generic_int_op = {
+	.args = args_prototype,
+	.evaluate = evaluate_generic_int_op,
+};
+
+
 static int eval_atomic_common(struct expression *expr)
 {
 	struct symbol *fntype = expr->fn->ctype->ctype.base_type;
diff --git a/builtin.h b/builtin.h
index 9cb6728444fe..5fe77c926244 100644
--- a/builtin.h
+++ b/builtin.h
@@ -14,4 +14,6 @@ struct builtin_fn {
 
 void declare_builtins(int stream, const struct builtin_fn tbl[]);
 
+extern struct symbol_op generic_int_op;
+
 #endif
-- 
2.31.1


  parent reply	other threads:[~2021-04-12 21:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-12 21:21 [PATCH v2 0/8] scheck: add a symbolic checker Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 1/8] export declare_builtins() Luc Van Oostenryck
2021-04-12 21:21 ` Luc Van Oostenryck [this message]
2021-04-13  0:23   ` [PATCH v2 2/8] builtin: define a symbol_op for a generic op acting on integer Ramsay Jones
2021-04-13 16:42     ` Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 3/8] .gitignore is a bit too greedy Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 4/8] scheck: add a symbolic checker Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 5/8] scheck: assert_eq() Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 6/8] scheck: allow multiple assertions Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 7/8] scheck: assert_const() Luc Van Oostenryck
2021-04-12 21:21 ` [PATCH v2 8/8] scheck: support pre-conditions via __assume() Luc Van Oostenryck

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=20210412212111.29186-3-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=ramsay@ramsayjones.plus.com \
    /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 NNTP newsgroup(s).