From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13950 invoked by alias); 19 Feb 2014 03:02:40 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 13937 invoked by uid 89); 19 Feb 2014 03:02:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ve0-f179.google.com Received: from mail-ve0-f179.google.com (HELO mail-ve0-f179.google.com) (209.85.128.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 19 Feb 2014 03:02:37 +0000 Received: by mail-ve0-f179.google.com with SMTP id jx11so14638412veb.10 for ; Tue, 18 Feb 2014 19:02:35 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.221.55.133 with SMTP id vy5mr23822475vcb.17.1392778955637; Tue, 18 Feb 2014 19:02:35 -0800 (PST) Received: by 10.58.195.199 with HTTP; Tue, 18 Feb 2014 19:02:35 -0800 (PST) In-Reply-To: <20140219025428.GA5417@lithium.compnerd.org> References: <20140219025428.GA5417@lithium.compnerd.org> Date: Wed, 19 Feb 2014 03:02:00 -0000 Message-ID: Subject: Re: ARM inline assembly usage in Linux kernel From: Andrew Pinski To: Saleem Abdulrasool Cc: GCC Mailing List , Renato Golin Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2014-02/txt/msg00343.txt.bz2 On Tue, Feb 18, 2014 at 6:56 PM, Saleem Abdulrasool wrote: > Hello. > > I am sending this at the behest of Renato. I have been working on the ARM > integrated assembler in LLVM and came across an interesting item in the Linux > kernel. > > I am wondering if this is an unstated covenant between the kernel and GCC or > simply a clever use of an unintended/undefined behaviour. > > The Linux kernel uses the *compiler* as a fancy preprocessor to generate a > specially crafted assembly file. This file is then post-processed via sed to > generate a header containing constants which is shared across assembly and C > sources. > > In order to clarify the question, I am selecting a particular example and > pulling out the relevant bits of the source code below. > > #define DEFINE(sym, val) asm volatile("\n->" #sym " %0 " #val : : "i" (val)) > > #define __NR_PAGEFLAGS 22 > > void definitions(void) { > DEFINE(NR_PAGEFLAGS, __NR_PAGEFLAGS); > } > > This is then assembled to generate the following: > > ->NR_PAGEFLAGS #22 __NR_PAGEFLAGS > > This will later be post-processed to generate: > > #define NR_PAGELAGS 22 /* __NR_PAGEFLAGS */ > > By using the inline assembler to evaluate (constant) expressions into constant > values and then emit that using a special identifier (->) is a fairly clever > trick. This leads to my question: is this just use of an unintentional > "feature" or something that was worked out between the two projects. > > Please explicitly CC me on any response as I am not subscribed to this mailing > list. I don't see why this is a bad use of the inline-asm. GCC does not know and is not supposed to know what the string inside the inline-asm is going to be. In fact if you have a newer assembler than the compiler, you could use instructions that GCC does not even know about. This is the purpose of inline-asm. I think it was a bad design decision on LLVM/clang's part that it would check the assembly code up front. Thanks, Andrew Pinski > > Thanks. > > -- > Saleem Abdulrasool > compnerd (at) compnerd (dot) org >