tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] C99 static array indices in function args


From: Michael B. Smith
Subject: Re: [Tinycc-devel] C99 static array indices in function args
Date: Tue, 26 Dec 2017 23:21:15 +0000

> > 'restrict' has several defined (not just implied) contracts. This should 
> > require at least a warning.

> No.  Warnings are never required diagnostics (and even if, what 
> specifically would you want to warn about in this case?).  The only 
> require diagnostics are from constraint violations.  Most other undefined 
> behaviours don't need to be diagnosed (of course, if easily doable it's 
> nice to diagnose them).

Here we disagree.

If a compiler accepts a construct, but does not implement the construct
properly or completely, then the compiler should emit a warning indicating
that. I learned this from Dr. Wirth in 1981, and I used this in the compilers
I wrote/co-wrote in the 1980's for a mainframe computer manufacturer. I
believe it is still accurate today.

> There are no changes to emitted code necessary.  The [static 3] syntax is 
> an assertion of the programmer, the compiler doesn't have to check it.  On 
> the contrary, the compiler is allowed (but not required) to make use of 
> this assertion, and assume there are at least 3 elements.  If the 
> programmer then gives fewer its his problem, it's undefined behaviour, no 
> diagnostic required.  

I repeat my prior paragraph, with this added: if we have a reasonable 
expectation that a programmer uses tcc for "quick dev" purposes and may
then move to gcc for optimization purposes, then we should also need to
recognize that the programmer will want similar behavior between the two
compilers. I think this philosophy is already quite well represented in many
places in tcc. This is simply another similar place.

I absolutely recognize that you know far more about tcc than I do and I
respect that. :-) This discussion isn't really about tcc per se. 

-----Original Message-----
From: Tinycc-devel [mailto:address@hidden On Behalf Of Michael Matz
Sent: Saturday, December 23, 2017 8:44 AM
To: address@hidden
Subject: Re: [Tinycc-devel] C99 static array indices in function args

Hi,

On Wed, 20 Dec 2017, Michael B. Smith wrote:

> It's in 6.7.5.3/7 in C99 for 'static'.
>
> It's in 6.7.3/5 in C99 for 'const'.
>
> Using 'static' seems to have two implied contracts:
>
> [1] don't allow NULL parameters
> [2] verify, when possible, that the passed array has AT LEAST the number 
> of elements defined
>
> Using 'const' has an implied contract:
>
> [3] treat the array as if were a const array (e.g., "char a[const] --> 
> char * const a")
>
> Recent versions of gcc and clang do 1 and 2 (as warnings). I can't find 
> anything definitive about 3.

6.7.5.3 #7 is pretty clear, the type qualifiers applied are those from 
within the [ ].  TCC doesn't support this syntax properly right now.

> 'restrict' is allowed by tcc, but ignored. I think that that is a bug.

Why would you think so?  restrict is a type qualifier that is allowed to 
be applied only to pointer types, parmdecls of array type transform into 
pointer types hence "int a[restrict 3]" in a parmdecl is well defined and 
meaningful.

> 'restrict' has several defined (not just implied) contracts. This should 
> require at least a warning.

No.  Warnings are never required diagnostics (and even if, what 
specifically would you want to warn about in this case?).  The only 
require diagnostics are from constraint violations.  Most other undefined 
behaviours don't need to be diagnosed (of course, if easily doable it's 
nice to diagnose them).

> Supporting 'const' properly is actually pretty easy.
>
> Supporting 'static' - well, [1] requires that tcc generate code (to 
> handle both runtime and compile-time cases), and [2] may (or may not) be 
> easy to do depending on the parameter. If we assume locally defined 
> arrays, like VLAs, then it is easy.

There are no changes to emitted code necessary.  The [static 3] syntax is 
an assertion of the programmer, the compiler doesn't have to check it.  On 
the contrary, the compiler is allowed (but not required) to make use of 
this assertion, and assume there are at least 3 elements.  If the 
programmer then gives fewer its his problem, it's undefined behaviour, no 
diagnostic required.  (Of course TCC, not being an optimizing compiler 
wouldn't have much opportunity to make use of these kinds of assertion, 
the same that it doesn't make use of restrict qualitications).


Ciao,
Michael.

_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel



reply via email to

[Prev in Thread] Current Thread [Next in Thread]