From patchwork Fri Jan 19 21:57:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoni Boucher X-Patchwork-Id: 189683 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1302551dyb; Fri, 19 Jan 2024 13:58:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPZiV8oI7md17Mfdt5vYG+aMvj07pUkPuDrA2VimKo1kIYeUacjNv0EkV/9dSN7jh0bbxo X-Received: by 2002:ac8:4e45:0:b0:429:92de:bd04 with SMTP id e5-20020ac84e45000000b0042992debd04mr453362qtw.67.1705701487913; Fri, 19 Jan 2024 13:58:07 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1705701487; cv=pass; d=google.com; s=arc-20160816; b=GLCO2d4ttnLNYdxfNHx7vmugqrU+ptVCg6Dzmn5jdESc0+Avg1RQvsqnYHD+ctJfrH mcGDik/a5PJdwB/o6KH9Kz9NRT/MOkqSoQPOO8QKu8ZFLUr4E1dXFVx341UDSDTSbgIa +1nHPxaaRaJC8fsF6gZIFBUG6NvO3LDK3L6EXHBWLZ6ETaylIgD/F2oqlol/uUrwf4Kp sfCUVHj46urYEfDh8pWj1O59b1jSodilLX2Ie4zUVAjKD8d3iC96hs7iQMhGghY+CAEQ LkzXp08yWmstnOuHddCf3aKmdCfHrirvbRDj2lO2+x0fHyZoEcnE06T8O5pO/Je4a7xT vawg== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id:mime-version :user-agent:autocrypt:date:cc:to:from:subject:message-id :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=7dT1dkW7aLeXkuVRho33OEOYWQS6bg/Uag3mzenr+Ig=; fh=vAsBZubqglMVgYvXuALnbO1gpFxpxAkhTRAP1jtVR04=; b=WqdnSRtmqCOBnpfWuM0lrXpFPUNtos/eOZM6iA7iTgfPSm7oRSCa+LYUtc1i4XU1sg wFDBO0EqEpGsPSwV/oFZVEgy2loibjyq6vNQi3KY3zIAcHzjWKXUQ58qOevrP8fD3dYk ecUISR/+RDwTS6CMyrITbTa7lIoIgRHNVf/w7eqi9xTQw3yq3zGn4gIx6TvyjIoit/kX J6oes0DqCvoIY39wuYgkC8us2+B5y4XXV9mkQUgjFI2OjlB5cN7wSmfzD9hlM1k8RsNm nvrGPxjzQxhtBAdTZhaOYczki5S0vpNou05THQBiO848mRXB2h2CB2ygKbuTkyXJnNrd 1PFg== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@zoho.com header.s=zm2022 header.b=dAyDraHc; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=zoho.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t17-20020a05622a01d100b00429ffeb6f9esi154574qtw.468.2024.01.19.13.58.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 13:58:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@zoho.com header.s=zm2022 header.b=dAyDraHc; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=zoho.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D9D93857C6F for ; Fri, 19 Jan 2024 21:58:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sender4-pp-o90.zoho.com (sender4-pp-o90.zoho.com [136.143.188.90]) by sourceware.org (Postfix) with ESMTPS id C3A303858415; Fri, 19 Jan 2024 21:57:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C3A303858415 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=zoho.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=zoho.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C3A303858415 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=136.143.188.90 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1705701450; cv=pass; b=xNnVfRa57rYrxpijwvLCcELpGOeWmECLuS2K+2udknFeRfkqHxT+0IJWYfpGW8iUFBvDmyvze/xVVEYaBq2Bh3Wr9Z7qI5fAvfkH90P2JEGC1w9KyKbenEgyUANvQSRYDMXXckPXFLTNNistp8+OzuJuXB/Zrb8tnOfIwpicT78= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1705701450; c=relaxed/simple; bh=eUNe1jUue6iUgVdfKMT8YjGxeH5fkOx3sXlohbDoxnc=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=UH0CGjijBTAJgQUfjnm0AMekCAiB1/j3BaT0WHgh+/YkGcGGrmkemRs9pHHXUSJ2OoCIgu29O1Ea1W03Mv6F4QwMYk3iu7gZn9WDXyWR95CBY8ViosGjIPXkSUslPUKD29iuofevcZUZofwzZWkUx+t/FIM5udt6Ytq8YFYXG8A= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1705701443; cv=none; d=zohomail.com; s=zohoarc; b=T8iO8ZvoFhTi+1/WQ8PD+3F2GtqmA4+r/rpw2AX+h87yBnjfEG62IBX1rYPsodPxhpsnafYIz9d/RIDSVukRT43lCA2MmqXOCTKHTOz6FdIPUvfH/RabSdoY4KW5CbmY8J3Z0vVh9i//x+Tewo+tkau69HlRNJJ2ggOVU+wG6dM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705701443; h=Content-Type:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=7dT1dkW7aLeXkuVRho33OEOYWQS6bg/Uag3mzenr+Ig=; b=jFs5ntVJgRxEI1B6fnDXDi1zPGrlT4D/xL13bmjrKfaTqXCQZMAIv+eFvc38ARuKxAIdHPek6wMTdGzQ7RYjQ/6n7nMFQ0RibWNSLYcAspfHtvGte14Wo6QRpOMc9A1OrAhtD33wZrTaEtCs2prE2YzU+hz5q+IJtQSZdnl4nF8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=zoho.com; spf=pass smtp.mailfrom=bouanto@zoho.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1705701443; s=zm2022; d=zoho.com; i=bouanto@zoho.com; h=Message-ID:Subject:Subject:From:From:To:To:Cc:Cc:Date:Date:Content-Type:MIME-Version:Feedback-ID:Message-Id:Reply-To; bh=7dT1dkW7aLeXkuVRho33OEOYWQS6bg/Uag3mzenr+Ig=; b=dAyDraHc7shZSKrm898cbOlxRyO69vN225IaI1qolE217INrsu91vIPbHSr1JYwH 6BYbHP4HeLZqTvLNIMcA06+zDp+MRMEbZYw2VofLUGDzzdc2f+f8cAcN51u3CXlVAZh nCUbAR9CFByYstZCvPkEkAEXgK0++Lp7KblSJxkk= Received: from [192.168.1.172] (38.87.11.6 [38.87.11.6]) by mx.zohomail.com with SMTPS id 1705701441997701.869219252613; Fri, 19 Jan 2024 13:57:21 -0800 (PST) Message-ID: <20a1db244d8234683b61c7d124999ba45462af90.camel@zoho.com> Subject: [PATCH] libgccjit: Add gcc_jit_global_set_readonly From: Antoni Boucher To: "gcc-patches@gcc.gnu.org" , "jit@gcc.gnu.org" Cc: David Malcolm Date: Fri, 19 Jan 2024 16:57:20 -0500 Autocrypt: addr=bouanto@zoho.com; prefer-encrypt=mutual; keydata=mQENBFOSMLQBCADO5aw6Ys8thMQUNzrwAnfJX2wbgWiz0pQ01DjYj22eeIpChkoZn6LWdt4dieq30u2rFi/yQzJ02foHwI2+aL9rU6xz/x4TwqyRJQGMOqklNc3R+pdXmH4WDQkQDWmLxvc07vu+zb8Tx5A6pMDh4J2ncCEhLEUcH39Yq/yg4eBnFwUX6N7kakvHrnScGNqhnSFCacoJeMJUAR+1G7VBSBd++jmnHLnx3mj7QkRZVECJUw2zqiv1yReCC6GU4SvqLjdqm5ZGeoWOqD/NHjBRoEeOVjzp6M/qOjjWRbkJVqmvgfcD8UytSSqqboR35YFT4L+rZt2ri3T12MJb3i5syCAXABEBAAG0IUFudG9uaSBCb3VjaGVyIDxib3VhbnRvQHpvaG8uY29tPokBVgQTAQgAQAIbIwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAFiEEOELC4Uw1Jeb66YE6RVeGAwR4zcEFAlz4QM4FCRLMEZoACgkQRVeGAwR4zcFBQQf/afttJrA/puADQZhrDfkgr0MFvq6iB+GCy1b8BkXimk1TOXTPt87YLehSeijNu3JkYhl5eRc87BNfU9J87KfI/KIy6hZxqlDXk16FhW9bw/7wYEA0hpb3MUn7xLElXDT0ZHaD+KTe8Oun7qfzgx5RlL6r/WODf3CkSpO085R/rfeBqDEx9mVlhDWgq6Az3CZoD+3CqiCKVqmDuHTWz4kwrd9AM5eVcLvvLKnZIdoIp+G5Ao6BvaGlZyfenN1iOSjLy2NXNt4MnUt0lUYEP5KSIIRhHQ8xkUbj7eWUmaahkxhNb3fH3sAPwGnRZrPpb4rgYzNmSk63wWMh9M2xk+rLb7kBDQRTkjC0AQgAumZzsAV/UFWI+dpzebQfma36kKYZZFuseant5sq/HWP553XQ/U6ttJiKyN5MpCqtxvCAoRplf42YhlHuFqgf73WJxoJ6Y+sdyqoBSwlR+ gzAneAmsa8gmmY0wawH0Z2leazjKuS7mJjVEQZg0ZGsiCVRGeRnDqFGzDEzDc9ngWKSoTq0fKzlGy1X85OrtmUrvEbhSo6HP+FoeunHkIqrxu3w3vDoFEXxVQlKI6V3I4nCz5n6DB8WR3L7nsiiTnOiGirPw1ngvWFLW86kkA4FJpayc8Xl3va3SLY+2y4yuROboX2DVI4AC/Qeug/mDiBicPxkP6YfUartQRMe6obkEQARAQABiQE8BBgBCAAmAhsMFiEEOELC4Uw1Jeb66YE6RVeGAwR4zcEFAlz4QRsFCRLMEecACgkQRVeGAwR4zcE56ggAgTgrJInBKC+7552Dpccuo6Clh3wZfjlNLv9/6r5lKEbaNzaTrfhPiAP4WgnluIUmj8amOFLFJpj+BAVNOXpZ4D2R3o9ch8z7fot+fW4Yw+PKIxH4I2xEys8ndoEB3aiQwHjKcGIhkIU7uyMJFQr2aWjdTY0gmXw0YZueHOSLgo7uX4XKxB8fEO/yto/Tff2YBAAq+AtNwt+Gh5YS9rZw7rwUTWMi84yVOlc+zRE79E9NJkvdTwX7IJYo64VzIRNfgHsn7QNdVzuM1XIFHl+Glk6cIlI8s6BO7nEoCn3hTF104fQTAO3fEs+XXZOKXo2lk8faowEoPq5r58StrV0nyg== User-Agent: Evolution 3.50.3 MIME-Version: 1.0 X-Zoho-Virus-Status: 1 X-Zoho-AV-Stamp: zmail-av-1.1.0/204.902.92 Feedback-ID: rr08011228eeb7a2c51fae361d339584120000d6e91f8674a558e0496eef8f072f14384f0af2bb6b29f7543589:zu08011226252e8332142f529bc296bc750000b0bca82d111055755ce8b876b71bdd424737de480edbe7f0:rf08011232363c4b6c9681451a4c06443b000054b3cb26fa6e756e340ce44b1a7d81e248629816c768d09e6fbe1b1feff815389a04f3d6:ZohoMail X-ZohoMailClient: External X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788557643468598825 X-GMAIL-MSGID: 1788557643468598825 Hi. This patch adds a new API gcc_jit_global_set_readonly: it's equivalent to having a const global variable, but it is useful in the case of complex compilers where it is not convenient to use const. Thanks for the review. From ff3aa19207a6cdaeff6fcb6521ad2ad92f5448ff Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 24 May 2022 17:45:01 -0400 Subject: [PATCH] libgccjit: Add gcc_jit_global_set_readonly gcc/jit/ChangeLog: * docs/topics/compatibility.rst (LIBGCCJIT_ABI_26): New ABI tag. * docs/topics/expressions.rst: Document gcc_jit_global_set_readonly. * jit-playback.cc (global_new_decl, new_global, new_global_initialized): New parameter readonly. * jit-playback.h (global_new_decl, new_global, new_global_initialized): New parameter readonly. * jit-recording.cc (recording::global::replay_into): Use m_readonly. (recording::global::write_reproducer): Dump reproducer for gcc_jit_global_set_readonly. * jit-recording.h (get_readonly, set_readonly): New methods. (m_readonly): New attribute. * libgccjit.cc (gcc_jit_global_set_readonly): New function. (gcc_jit_block_add_assignment): Check that we don't assign to a readonly variable. * libgccjit.h (gcc_jit_global_set_readonly): New function. (LIBGCCJIT_HAVE_gcc_jit_global_set_readonly): New define. * libgccjit.map: New function. gcc/testsuite/ChangeLog: * jit.dg/all-non-failing-tests.h: Mention test-readonly.c. * jit.dg/test-error-assign-readonly.c: New test. * jit.dg/test-readonly.c: New test. --- gcc/jit/docs/topics/compatibility.rst | 7 +++ gcc/jit/docs/topics/expressions.rst | 12 ++++ gcc/jit/jit-playback.cc | 15 +++-- gcc/jit/jit-playback.h | 9 ++- gcc/jit/jit-recording.cc | 10 ++- gcc/jit/jit-recording.h | 12 ++++ gcc/jit/libgccjit.cc | 22 +++++++ gcc/jit/libgccjit.h | 5 ++ gcc/jit/libgccjit.map | 5 ++ gcc/testsuite/jit.dg/all-non-failing-tests.h | 3 + .../jit.dg/test-error-assign-readonly.c | 62 +++++++++++++++++++ gcc/testsuite/jit.dg/test-readonly.c | 38 ++++++++++++ 12 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/jit.dg/test-error-assign-readonly.c create mode 100644 gcc/testsuite/jit.dg/test-readonly.c diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst index ebede440ee4..e13581d0685 100644 --- a/gcc/jit/docs/topics/compatibility.rst +++ b/gcc/jit/docs/topics/compatibility.rst @@ -378,3 +378,10 @@ alignment of a variable: -------------------- ``LIBGCCJIT_ABI_25`` covers the addition of :func:`gcc_jit_type_get_restrict` + +.. _LIBGCCJIT_ABI_26: + +``LIBGCCJIT_ABI_26`` +-------------------- +``LIBGCCJIT_ABI_26`` covers the addition of +:func:`gcc_jit_global_set_readonly` diff --git a/gcc/jit/docs/topics/expressions.rst b/gcc/jit/docs/topics/expressions.rst index 42cfee36302..8d4aa96e64a 100644 --- a/gcc/jit/docs/topics/expressions.rst +++ b/gcc/jit/docs/topics/expressions.rst @@ -944,6 +944,18 @@ Global variables #ifdef LIBGCCJIT_HAVE_CTORS +.. function:: void\ + gcc_jit_global_set_readonly (gcc_jit_lvalue *global) + + Set the global variable as read-only, meaning you cannot assign to this variable. + + This entrypoint was added in :ref:`LIBGCCJIT_ABI_26`; you can test for its + presence using: + + .. code-block:: c + + #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_readonly + Working with pointers, structs and unions ----------------------------------------- diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc index dddd537f3b1..420f3a843cf 100644 --- a/gcc/jit/jit-playback.cc +++ b/gcc/jit/jit-playback.cc @@ -607,7 +607,8 @@ global_new_decl (location *loc, enum gcc_jit_global_kind kind, type *type, const char *name, - enum global_var_flags flags) + enum global_var_flags flags, + bool readonly) { gcc_assert (type); gcc_assert (name); @@ -646,7 +647,7 @@ global_new_decl (location *loc, break; } - if (TYPE_READONLY (type_tree)) + if (TYPE_READONLY (type_tree) || readonly) TREE_READONLY (inner) = 1; if (loc) @@ -674,10 +675,11 @@ new_global (location *loc, enum gcc_jit_global_kind kind, type *type, const char *name, - enum global_var_flags flags) + enum global_var_flags flags, + bool readonly) { tree inner = - global_new_decl (loc, kind, type, name, flags); + global_new_decl (loc, kind, type, name, flags, readonly); return global_finalize_lvalue (inner); } @@ -822,9 +824,10 @@ new_global_initialized (location *loc, size_t initializer_num_elem, const void *initializer, const char *name, - enum global_var_flags flags) + enum global_var_flags flags, + bool readonly) { - tree inner = global_new_decl (loc, kind, type, name, flags); + tree inner = global_new_decl (loc, kind, type, name, flags, readonly); vec *constructor_elements = NULL; diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h index b0166f8f6ce..7c1d8d88b4d 100644 --- a/gcc/jit/jit-playback.h +++ b/gcc/jit/jit-playback.h @@ -111,7 +111,8 @@ public: enum gcc_jit_global_kind kind, type *type, const char *name, - enum global_var_flags flags); + enum global_var_flags flags, + bool readonly); lvalue * new_global_initialized (location *loc, @@ -121,7 +122,8 @@ public: size_t initializer_num_elem, const void *initializer, const char *name, - enum global_var_flags flags); + enum global_var_flags flags, + bool readonly); rvalue * new_ctor (location *log, @@ -306,7 +308,8 @@ private: enum gcc_jit_global_kind kind, type *type, const char *name, - enum global_var_flags flags); + enum global_var_flags flags, + bool readonly); lvalue * global_finalize_lvalue (tree inner); diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc index 9b5b8005ebe..f86e7439301 100644 --- a/gcc/jit/jit-recording.cc +++ b/gcc/jit/jit-recording.cc @@ -4879,12 +4879,14 @@ recording::global::replay_into (replayer *r) / m_type->dereference ()->get_size (), m_initializer, playback_string (m_name), - m_flags) + m_flags, + m_readonly) : r->new_global (playback_location (r, m_loc), m_kind, m_type->playback_type (), playback_string (m_name), - m_flags); + m_flags, + m_readonly); if (m_tls_model != GCC_JIT_TLS_MODEL_NONE) global->set_tls_model (recording::tls_models[m_tls_model]); @@ -5042,6 +5044,10 @@ recording::global::write_reproducer (reproducer &r) id, m_link_section->c_str ()); + if (m_readonly) + r.write (" gcc_jit_global_set_readonly (%s, /* gcc_jit_lvalue *lvalue */);\n", + id); + if (m_initializer) switch (m_type->dereference ()->get_size ()) { diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index 4a8082991fb..5b3c855b7e8 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -1236,6 +1236,17 @@ public: as_rvalue () { return this; } const char *access_as_rvalue (reproducer &r) override; + + bool get_readonly () const + { + return m_readonly; + } + + void set_readonly () + { + m_readonly = true; + } + virtual const char *access_as_lvalue (reproducer &r); virtual bool is_global () const { return false; } void set_tls_model (enum gcc_jit_tls_model model); @@ -1249,6 +1260,7 @@ protected: string *m_reg_name; enum gcc_jit_tls_model m_tls_model; unsigned m_alignment; + bool m_readonly = false; }; class param : public lvalue diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index 0451b4df7f9..1e96ee5559f 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -1868,6 +1868,23 @@ gcc_jit_global_set_initializer (gcc_jit_lvalue *global, return global; } +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::global::set_readonly method, in + jit-recording.cc. */ + +extern void +gcc_jit_global_set_readonly (gcc_jit_lvalue *global) +{ + RETURN_IF_FAIL (global, NULL, NULL, "NULL global"); + RETURN_IF_FAIL_PRINTF1 (global->is_global (), NULL, NULL, + "lvalue \"%s\" not a global", + global->get_debug_string ()); + + global->set_readonly (); +} + /* Public entrypoint. See description in libgccjit.h. After error-checking, this calls the trivial @@ -2844,6 +2861,11 @@ gcc_jit_block_add_assignment (gcc_jit_block *block, lvalue->get_type ()->get_debug_string (), rvalue->get_debug_string (), rvalue->get_type ()->get_debug_string ()); + RETURN_IF_FAIL_PRINTF1 ( + !lvalue->get_readonly (), + ctxt, loc, + "cannot assign to readonly variable: %s", + lvalue->get_debug_string ()); gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue); diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index 749f6c24177..5ac587e5bfc 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -1045,6 +1045,11 @@ gcc_jit_global_set_initializer (gcc_jit_lvalue *global, const void *blob, size_t num_bytes); +extern void +gcc_jit_global_set_readonly (gcc_jit_lvalue *global); + +#define LIBGCCJIT_HAVE_gcc_jit_global_set_readonly + /* Upcasting. */ extern gcc_jit_object * gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue); diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index 8b90a0e2ff3..319e0e9b796 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -276,3 +276,8 @@ LIBGCCJIT_ABI_25 { global: gcc_jit_type_get_restrict; } LIBGCCJIT_ABI_24; + +LIBGCCJIT_ABI_26 { + global: + gcc_jit_global_set_readonly; +} LIBGCCJIT_ABI_25; diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index e762563f9bd..1dd45accdc9 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -313,6 +313,9 @@ #undef create_code #undef verify_code +/* test-readonly.c: This can't be in the testcases array as it + is target-specific. */ + /* test-restrict.c: This can't be in the testcases array as it needs the `-O3` flag. */ diff --git a/gcc/testsuite/jit.dg/test-error-assign-readonly.c b/gcc/testsuite/jit.dg/test-error-assign-readonly.c new file mode 100644 index 00000000000..628bdb86d1c --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-assign-readonly.c @@ -0,0 +1,62 @@ +#include +#include + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + + const long integer = 10; + + void + test_fn () + { + integer = 12; + } + + and verify that the API complains about assigning to a read-only + variable. + */ + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_type *long_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG); + + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + void_type, + "test_fn", + 0, NULL, + 0); + gcc_jit_block *initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_lvalue *integer = + gcc_jit_context_new_global ( + ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, long_type, "integer"); + + gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, long_type, 10); + gcc_jit_global_set_initializer_rvalue (integer, ten); + gcc_jit_global_set_readonly(integer); + + gcc_jit_rvalue *twelve = gcc_jit_context_new_rvalue_from_int (ctxt, long_type, 12); + gcc_jit_block_add_assignment(initial, NULL, integer, twelve); + + gcc_jit_block_end_with_void_return (initial, NULL); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + /* Verify that the correct error messages were emitted. */ + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_block_add_assignment:" + " cannot assign to readonly variable: integer"); +} diff --git a/gcc/testsuite/jit.dg/test-readonly.c b/gcc/testsuite/jit.dg/test-readonly.c new file mode 100644 index 00000000000..554fa0bad78 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-readonly.c @@ -0,0 +1,38 @@ +/* { dg-do compile { target x86_64-*-* } } */ + +#include +#include + +#include "libgccjit.h" + +/* We don't want set_options() in harness.h to set -O3 so our little local + is optimized away. */ +#define TEST_ESCHEWS_SET_OPTIONS +static void set_options (gcc_jit_context *ctxt, const char *argv0) +{ +} + +#define TEST_COMPILING_TO_FILE +#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER +#define OUTPUT_FILENAME "output-of-test-readonly.c.s" +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + const int foo; + */ + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_lvalue *foo = + gcc_jit_context_new_global ( + ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, int_type, "foo"); + + gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10); + gcc_jit_global_set_initializer_rvalue (foo, ten); + gcc_jit_global_set_readonly(foo); +} + +/* { dg-final { jit-verify-output-file-was-created "" } } */ +/* { dg-final { jit-verify-assembler-output ".section\t.rodata" } } */ -- 2.43.0