From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1861) id 75E09385840D; Wed, 24 Apr 2024 11:55:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75E09385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1713959719; bh=MtdEe+hK2J7+K/UQRnLGmTIyHp98RC2Gygg87GTb1Vw=; h=From:To:Subject:Date:From; b=UsOM4zcxMzxkYYZM4LGntlsxJ2Kc7rk5v5xwJH6IJcxpvk/WYbhWqG71SGNmMmF0x qzSXTi81DeRZb4VNaFsP5UZ+8dCOTiP+0ziZxQfSyqRo7FIEbQQOUwSat9JsceE0uu 3YPXF3umhfhYPU/a+/k97qS1Mmo4ZpH2HOqWdrlY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jose E. Marchesi To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-10107] bpf: define BPF feature pre-processor macros X-Act-Checkin: gcc X-Git-Author: Jose E. Marchesi X-Git-Refname: refs/heads/master X-Git-Oldrev: cc48418cfc2e555d837ae9138cbfac23acb3cdf9 X-Git-Newrev: 152d945d42136737305fd55f1239a3dd6037c8a7 Message-Id: <20240424115519.75E09385840D@sourceware.org> Date: Wed, 24 Apr 2024 11:55:19 +0000 (GMT) List-Id: https://gcc.gnu.org/g:152d945d42136737305fd55f1239a3dd6037c8a7 commit r14-10107-g152d945d42136737305fd55f1239a3dd6037c8a7 Author: Jose E. Marchesi Date: Wed Apr 24 13:48:51 2024 +0200 bpf: define BPF feature pre-processor macros This commit makes the BPF backend to define the following macros for c-family languages: __BPF_CPU_VERSION__ This is a numeric value identifying the version of the BPF "cpu" for which GCC is generating code. __BPF_FEATURE_ALU32 __BPF_FEATURE_JMP32 __BPF_FEATURE_JMP_EXT __BPF_FEATURE_BSWAP __BPF_FEATURE_SDIV_SMOD __BPF_FEATURE_MOVSX __BPF_FEATURE_LDSX __BPF_FEATURE_GOTOL __BPF_FEATURE_ST These are defines if the corresponding "feature" is enabled. The features are implicitly enabled by the BPF CPU version enabled, and most of them can also be enabled/disabled using target-specific -m[no-]FEATURE command line switches. Note that this patch moves the definition of bpf_target_macros, that implements TARGET_CPU_CPP_BUILTINS in the BPF backend, to a bpf-c.cc file. This is because we are now using facilities from c-family/* and these features are not available in compilers like lto1. A couple of tests are also added. Tested in target bpf-unknown-none-gcc and host x86_64-linux-gnu. No regressions. gcc/ChangeLog * config.gcc: Add bpf-c.o as a target object for C and C++. * config/bpf/bpf.cc (bpf_target_macros): Move to bpf-c.cc. * config/bpf/bpf-c.cc: New file. (bpf_target_macros): Move from bpf.cc and define BPF CPU feature macros. * config/bpf/t-bpf: Add rules to build bpf-c.o. gcc/testsuite/ChangeLog * gcc.target/bpf/feature-macro-1.c: New test. * gcc.target/bpf/feature-macro-2.c: Likewise. Diff: --- gcc/config.gcc | 2 + gcc/config/bpf/bpf-c.cc | 88 ++++++++++++++++++++++++++ gcc/config/bpf/bpf.cc | 17 ----- gcc/config/bpf/t-bpf | 4 ++ gcc/testsuite/gcc.target/bpf/feature-macro-1.c | 34 ++++++++++ gcc/testsuite/gcc.target/bpf/feature-macro-2.c | 14 ++++ 6 files changed, 142 insertions(+), 17 deletions(-) diff --git a/gcc/config.gcc b/gcc/config.gcc index ce683adcc8a..c2764095f0c 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -394,6 +394,8 @@ bfin*-*) ;; bpf-*-*) cpu_type=bpf + c_target_objs="bpf-c.o" + cxx_target_objs="bpf-c.o" ;; frv*) cpu_type=frv extra_options="${extra_options} g.opt" diff --git a/gcc/config/bpf/bpf-c.cc b/gcc/config/bpf/bpf-c.cc new file mode 100644 index 00000000000..f12f0627103 --- /dev/null +++ b/gcc/config/bpf/bpf-c.cc @@ -0,0 +1,88 @@ +/* BPF-specific code for C family languages. + Copyright (C) 2024 Free Software Foundation, Inc. + Contributed by Oracle Inc. + +This file is part of GCC. + +GCC 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, or (at your option) +any later version. + +GCC 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 GCC; see the file COPYING3. If not see +. */ + +#define IN_TARGET_CODE 1 + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "c-family/c-common.h" +#include "cpplib.h" + +/* Define target-specific CPP macros. This function in used in the + definition of TARGET_CPU_CPP_BUILTINS in bpf.h */ + +#define builtin_define(TXT) cpp_define (pfile, TXT) + +void +bpf_target_macros (cpp_reader *pfile) +{ + builtin_define ("__BPF__"); + builtin_define ("__bpf__"); + + if (TARGET_BIG_ENDIAN) + builtin_define ("__BPF_BIG_ENDIAN__"); + else + builtin_define ("__BPF_LITTLE_ENDIAN__"); + + switch (bpf_isa) + { + case ISA_V1: + builtin_define_with_int_value ("__BPF_CPU_VERSION__", 1); + break; + case ISA_V2: + builtin_define_with_int_value ("__BPF_CPU_VERSION__", 2); + break; + case ISA_V3: + builtin_define_with_int_value ("__BPF_CPU_VERSION__", 3); + break; + case ISA_V4: + builtin_define_with_int_value ("__BPF_CPU_VERSION__", 4); + break; + default: + gcc_unreachable (); + break; + } + + /* Different BPF CPU versions support different features. Some of + them can be enabled/disabled explicitly. */ + if (bpf_has_alu32) + builtin_define ("__BPF_FEATURE_ALU32"); + if (bpf_has_jmp32) + builtin_define ("__BPF_FEATURE_JMP32"); + if (bpf_has_jmpext) + builtin_define ("__BPF_FEATURE_JMP_EXT"); + if (bpf_has_bswap) + builtin_define ("__BPF_FEATURE_BSWAP"); + if (bpf_has_sdiv) + builtin_define ("__BPF_FEATURE_SDIV_SMOD"); + if (bpf_has_smov) + builtin_define ("__BPF_FEATURE_MOVSX"); + + /* Other CPU features can only be enabled/disabled generically by + selecting the corresponding CPU version. */ + if (bpf_isa >= ISA_V4) + { + builtin_define ("__BPF_FEATURE_LDSX"); + builtin_define ("__BPF_FEATURE_GOTOL"); + builtin_define ("__BPF_FEATURE_ST"); + } +} diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index d9141dd625a..98fb755bb8b 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -284,23 +284,6 @@ bpf_file_end (void) #undef TARGET_ASM_FILE_END #define TARGET_ASM_FILE_END bpf_file_end -/* Define target-specific CPP macros. This function in used in the - definition of TARGET_CPU_CPP_BUILTINS in bpf.h */ - -#define builtin_define(TXT) cpp_define (pfile, TXT) - -void -bpf_target_macros (cpp_reader *pfile) -{ - builtin_define ("__BPF__"); - builtin_define ("__bpf__"); - - if (TARGET_BIG_ENDIAN) - builtin_define ("__BPF_BIG_ENDIAN__"); - else - builtin_define ("__BPF_LITTLE_ENDIAN__"); -} - /* Return an RTX representing the place where a function returns or receives a value of data type RET_TYPE, a tree node representing a data type. */ diff --git a/gcc/config/bpf/t-bpf b/gcc/config/bpf/t-bpf index dc50332350c..2facccbd57a 100644 --- a/gcc/config/bpf/t-bpf +++ b/gcc/config/bpf/t-bpf @@ -1,6 +1,10 @@ TM_H += $(srcdir)/config/bpf/btfext-out.h $(srcdir)/config/bpf/core-builtins.h +bpf-c.o: $(srcdir)/config/bpf/bpf-c.cc + $(COMPILE) $< + $(POSTCOMPILE) + btfext-out.o: $(srcdir)/config/bpf/btfext-out.cc $(COMPILE) $< $(POSTCOMPILE) diff --git a/gcc/testsuite/gcc.target/bpf/feature-macro-1.c b/gcc/testsuite/gcc.target/bpf/feature-macro-1.c new file mode 100644 index 00000000000..5fa7d3f46a2 --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/feature-macro-1.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-mcpu=v1 -malu32 -mjmp32 -mjmpext -mbswap -msdiv -msmov" } */ + +#ifndef __BPF_FEATURE_ALU32 +#error __BPF_FEATURE_ALU32 undefined +#endif + +#ifndef __BPF_FEATURE_JMP32 +#error __BPF_FEATURE_JMP32 undefined +#endif + +#ifndef __BPF_FEATURE_JMP_EXT +#error __BPF_FEATURE_JMP_EXT undefined +#endif + +#ifndef __BPF_FEATURE_SDIV_SMOD +#error __BPF_FEATURE_SDIV_SMOD undefined +#endif + +#ifndef __BPF_FEATURE_MOVSX +#error __BPF_FEATURE_MOVSX undefined +#endif + +#ifdef __BPF_FEATURE_LDSX +#error __BPF_FEATURE_LDSX defined with -mcpu=v1 +#endif + +#ifdef __BPF_FEATURE_GOTOL +#error __BPF_FEATURE_GOTOL defined with -mcpu=v4 +#endif + +#ifdef __BPF_FEATURE_ST +#error __BPF_FEATURE_ST defined with -mcpu=v4 +#endif diff --git a/gcc/testsuite/gcc.target/bpf/feature-macro-2.c b/gcc/testsuite/gcc.target/bpf/feature-macro-2.c new file mode 100644 index 00000000000..0ae0fc72f71 --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/feature-macro-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-mcpu=v4" } */ + +#ifndef __BPF_FEATURE_LDSX +#error __BPF_FEATURE_LDSX undefined with -mcpu=v4 +#endif + +#ifndef __BPF_FEATURE_GOTOL +#error __BPF_FEATURE_GOTOL undefined with -mcpu=v4 +#endif + +#ifndef __BPF_FEATURE_ST +#error __BPF_FEATURE_ST undefined with -mcpu=v4 +#endif