Commit Graph

79 Commits

Author SHA1 Message Date
Stephen Heumann b43036409e Add a new optimize flag for FP math optimizations that break IEEE rules.
There were several existing optimizations that could change behavior in ways that violated the IEEE standard with regard to infinities, NaNs, or signed zeros. They are now gated behind a new #pragma optimize flag. This change allows intermediate code peephole optimization and common subexpression elimination to be used while maintaining IEEE conformance, but also keeps the rule-breaking optimizations available if desired.

See section F.9.2 of recent C standards for a discussion of how these optimizations violate IEEE rules.
2021-11-29 20:31:15 -06:00
Stephen Heumann 6fa294aa3b Add documentation and headers for new <math.h> functions. 2021-11-28 19:54:51 -06:00
Stephen Heumann 6d8e019443 Work around SANE bug in FX2C.
This could give incorrect results for extended-to-comp conversions of certain negative integers like -2147483648 and -53021371269120. To get a fix for the same problem with regard to long long, ORCA/C should be linked with the latest version of ORCALib (which also works around some instances of the problem at run time). There are still other cases involving code in SysFloat that has not yet been patched.
2021-11-28 15:20:26 -06:00
Stephen Heumann 3e08ba39aa Note that scalbn uses FLT_RADIX.
This is the reason that it is distinct from ldexp, although they amount to the same thing in the case of binary floating-point.
2021-11-21 14:40:45 -06:00
Stephen Heumann b2874b8bf6 Add declarations and docs for float/long double versions of existing functions. 2021-11-21 14:38:30 -06:00
Stephen Heumann 4ebdb4ad04 Add the <tgmath.h> header containing type-generic math macros.
So far this only has macros for the newly-added functions, since the existing math functions in SysFloat do not have f- and l-suffixed versions.
2021-11-20 19:45:07 -06:00
Stephen Heumann 73a081bd55 Add header declarations and documentation for new math functions. 2021-11-20 19:33:04 -06:00
Stephen Heumann c1b2a88a84 Document the current partial support for compound literals. 2021-11-07 22:23:39 -06:00
Stephen Heumann 8db7a62f49 Document use of type qualifiers and "static" in array parameters. 2021-11-07 20:46:33 -06:00
Stephen Heumann a6359f67e0 Adjust parameters with typedef'd array types to have pointer types.
Parameters declared directly with array types were already adjusted to pointer types in commit 5b953e2db0, but this code is needed for the remaining case where a typedef'd array type is used.

With these changes, 'array' parameters are treated for all purposes as really having pointer types, which is what the standards call for. This affects at least their size as reported by sizeof and the debugging information generated for them.
2021-11-07 18:54:27 -06:00
Stephen Heumann bd1d2101eb Update release notes to account for varargs changes.
Specifically, va_start/va_arg/va_end calls no longer require stack repair code to be disabled.
2021-11-05 22:37:23 -05:00
Stephen Heumann 1010f9a906 Add the FP comparison macros in <math.h> (from C99).
These rely on a new internal function that has been added to ORCALib.
2021-11-02 21:59:01 -05:00
Stephen Heumann 73d194c12f Allow string constants with up to 32760 bytes.
This allows the length of the string plus a few extra bytes used internally to be represented by a 16-bit integer. Since the size limit for memory allocations has been raised, there is no good reason to impose a shorter limit on strings.

Note that C99 and later specify a minimum translation limit for string constants of at least 4095 characters.
2021-10-24 21:43:43 -05:00
Stephen Heumann 26d0f2ad35 Add the va_copy macro (from C99).
The previous changes to varargs handling enable this to work.
2021-10-23 22:36:53 -05:00
Stephen Heumann a20d69a211 Revise variable argument handling to better comply with standards.
In the new implementation, variable arguments are not removed until the end of the function. This allows variable argument processing to be restarted, and it prevents the addresses of local variables from changing in the middle of the function. The requirement to turn off stack repair code around varargs functions is also removed.

This fixes #58.
2021-10-23 22:36:34 -05:00
Stephen Heumann daede21819 Fix bug with assembly-language functions that return structs/unions. 2021-10-19 18:12:46 -05:00
Stephen Heumann f567d60429 Allow bit-fields in unions.
All versions of standard C allow this, but ORCA/C previously did not.
2021-10-18 21:48:18 -05:00
Stephen Heumann ad5063a9a3 Support hexadecimal floating-point constants. 2021-10-17 18:19:29 -05:00
Stephen Heumann 5871820e0c Support UTF-8/16/32 string literals and character constants (C11).
These have u8, u, or U prefixes, respectively. The types char16_t and char32_t (defined in <uchar.h>) are used for UTF-16 and UTF-32 code points.
2021-10-11 20:54:37 -05:00
Stephen Heumann 222c34a385 Fix bug in initialization using string literals with embedded nulls.
When using such a string literal to initialize an array with automatic storage duration, the bytes after the first null would be set to 0, rather than the values from the string literal.

Here is an example program showing the problem:

#include <stdio.h>
int main(void) {
        char s[] = "a\0b";
        puts(s+2);
}
2021-10-11 19:55:09 -05:00
Stephen Heumann 27be3e26ae Update release notes.
Binary literals and #warning have been approved to be in C23, so they are now documented as such.
2021-10-11 18:51:17 -05:00
Stephen Heumann 020f5ca5b2 Add documentation of <uchar.h> functions. 2021-10-02 22:40:31 -05:00
Stephen Heumann 47478604af Add documentation for new functions. 2021-10-02 13:57:15 -05:00
Stephen Heumann 38dc91892b Add header declaration and documentation for strftime. 2021-09-26 21:29:47 -05:00
Stephen Heumann 1b9955bf8b Allow access to fields from all struct-typed expressions.
This affects field selection expressions where the left expressions is a struct/union assignment or a function call returning a struct or union. Such expressions should be accepted, but they were giving spurious errors.

The following program illustrates the problem:

struct S {int a,b;} x, y={2,3};

struct S f(void) {
        struct S s = {7,8};
        return s;
}

int main(void) {
        return f().a + (x=y).b;
}
2021-09-17 22:04:10 -05:00
Stephen Heumann 650ff4697f Update release notes to include a bug fix in ORCALib.
Also, update a comment to reflect the actual behavior.
2021-09-17 19:28:21 -05:00
Stephen Heumann 8077a248a4 Treat short and int as compatible if using loose type checks.
This gives a clearer pattern of matching ORCA/C's historical behavior if loose type checks are used, and the documentation is updated accordingly.

It also avoids breaking existing code that may be relying on the old behavior. I am aware of at least one place that does (conflicting declarations of InstallNetDriver in GNO's <gno/gno.h>).
2021-09-12 18:12:24 -05:00
Stephen Heumann 7848e50218 Implement stricter type checks for comparisons.
These rules are used if loose type checks are disabled. They are intended to strictly implement the constraints in C17 sections 6.5.9 and 6.5.10.

This patch also fixes a bug where object pointer comparisons to "const void *" should be permitted but were not.
2021-09-10 21:02:55 -05:00
Stephen Heumann af455d1900 If not doing loose type checks, use stricter checks for function types.
These will check that the prototypes (if present) match in number and types of arguments. This primarily affects operations on function pointers, since similar checks were already done elsewhere on function declarations themselves.
2021-09-10 18:04:30 -05:00
Stephen Heumann a8682e28d3 Give an error for pointer assignments that discard qualifiers.
This is controlled by #pragma ignore bit 5, which is now a more general "loose type checks" bit.
2021-09-10 17:58:20 -05:00
Stephen Heumann 2f7e71cd24 Treat the fields of const structs as const-qualified.
This causes an error to be produced when trying to assign to these fields, which was being allowed before. It is also necessary for correct behavior of _Generic in some cases.
2021-09-09 18:39:19 -05:00
Stephen Heumann 438942692a Make va_arg(ap,double) work correctly.
This was not working because floating-point arguments are really passed in the extended format, but based on the wording in the C standard a type of "double" should still work for arguments passed with that type.

This fixes #29. (The bug report was valid only with respect to double, not float or long double.)
2021-09-03 21:25:20 -05:00
Stephen Heumann d72c0fb9a5 Fix bug in some cases where a byte value is loaded and then stored as a word.
It could wind up storing garbage in the upper 8 bits of the destination, because it was not doing a proper 8-bit to 16-bit conversion.

This is an old bug, but the change in commit 95f5182442 caused it to be triggered in more cases, e.g. in the C7.5.1.1.CC test case.

Here is a case that could exhibit the bug even before that:

#pragma optimize 1
#include <stdio.h>
int main(void) {
        int k[1];
        int i = 0;
        unsigned char uch = 'm';
        k[i] = uch;
        printf("%i\n", k[0]);
}
2021-09-03 18:10:27 -05:00
Stephen Heumann 3375e5ccc8 Update release notes. 2021-09-02 18:04:14 -05:00
Stephen Heumann b8c332deeb Treat invalid escape sequences as errors.
This applies to octal and hexadecimal sequences with out-of-range values, and also to unrecognized escape characters. The C standards say both of these cases are syntax/constraint violations requiring a diagnostic.
2021-08-31 18:36:06 -05:00
Stephen Heumann b16210a50b Record volatile and restrict qualifiers in types.
These are needed to correctly distinguish pointer types in _Generic. They should also be used for type compatibility checks in other contexts, but currently are not.

This also fixes a couple small problems related to type qualifiers:
*restrict was not allowed to appear after * in type-names
*volatile status was not properly recorded in sym files

Here is an example of using _Generic to distinguish pointer types based on the qualifiers of the pointed-to type:

#include <stdio.h>

#define f(e) _Generic((e),\
        int * restrict *: 1,\
        int * volatile const *: 2,\
        int **: 3,\
        default: 0)

#define g(e) _Generic((e),\
        int *: 1,\
        const int *: 2,\
        volatile int *: 3,\
        default: 0)

int main(void) {
        int * restrict * p1;
        int * volatile const * p2;
        int * const * p3;

        // should print "1 2 0 1"
        printf("%i %i %i %i\n", f(p1), f(p2), f(p3), f((int * restrict *)0));

        int *q1;
        const int *q2;
        volatile int *q3;
        const volatile int *q4;

        // should print "1 2 3 0"
        printf("%i %i %i %i\n", g(q1), g(q2), g(q3), g(q4));
}

Here is an example of a problem resulting from volatile not being recorded in sym files (if a sym file was present, the read of x was lifted out of the loop):

#pragma optimize -1
static volatile int x;
#include <stdio.h>
int main(void) {
        int y;
        for (unsigned i = 0; i < 100; i++) {
                y = x*2 + 7;
        }
}
2021-08-30 18:19:58 -05:00
Stephen Heumann 586e3f9146 Document that toint() is a non-standard extension. 2021-08-26 22:27:08 -05:00
Stephen Heumann 08dbe1eea3 Include the function name in assertion failure messages.
This is required by C99 and later, enabled by the availability of __func__.

This requires an updated assertion-printing function in ORCALib. Unfortunately, GNO has the assertion-printing function in its libc rather than in ORCALib, because it calls the GNO implementation of stdio. Therefore, we continue to use the old form under GNO for now, to maintain compatibility with its existing libc.
2021-08-24 18:35:01 -05:00
Stephen Heumann aa5b239824 Make CLOCKS_PER_SEC and CLK_TCK work in 50Hz video mode.
Previously, they were hard-coded as 60, but the clock tick frequency actually depends on the video mode. They now call a new library function that can detect the video mode and return the proper value.

This also makes CLOCKS_PER_SEC have the type clock_t, as C99 and later require.
2021-08-23 21:58:19 -05:00
Stephen Heumann 2b9d332580 Give an appropriate error for an illegal operator in a constant expression.
This was being reported as an "illegal type cast".
2021-08-22 20:33:34 -05:00
Stephen Heumann e4515e580a Omit all non-standard stuff from <ctype.h> if __KeepNamespacePure__ is defined.
This affects the toint function and the _tolower and _toupper macros. Several other non-standard functions and macros were already being omitted.
2021-08-22 17:35:16 -05:00
Stephen Heumann d5f1987dc4 Small updates to release notes. 2021-08-22 17:35:16 -05:00
Stephen Heumann fbdbad1f45 Report an error for certain large unsigned enumeration constants.
Enumeration constants must have values representable as an int (i.e. 16-bit signed values, in ORCA/C), but errors were not being reported if code tried to use the values 0xFFFF8000 to 0xFFFFFFFF. This problem could also affect certain larger values of type unsigned long long. The issue stemmed from not properly accounting for whether the constant expression had a signed or unsigned type.

This sample code demonstrated the problem:

enum E {
        a = 0xFFFFFFFF,
        b = 0xFFFF8000,
        y = 0x7FFFFFFFFFFFFFFFull,
        z = 0x8000000000000000
};
2021-07-07 20:06:05 -05:00
Stephen Heumann ae45bd4538 Update release notes. 2021-07-06 18:41:40 -05:00
Stephen Heumann 031af54112 Save the original value when doing postfix ++/-- on fp types.
The old code would add 1 and then subtract 1, which does not necessarily give the original value (e.g. if it is much less than 1).
2021-03-09 19:29:55 -06:00
Stephen Heumann db7a0a995d Update release notes with discussion of new floating-point features. 2021-03-09 18:01:30 -06:00
Stephen Heumann 57d11a573d Document _Generic expressions in the release notes. 2021-03-08 00:29:55 -06:00
Stephen Heumann 2b7e72ac49 Document <fenv.h> and standard pragmas in the release notes. 2021-03-07 15:11:49 -06:00
Stephen Heumann 77d66ab699 Support the predefined identifier __func__ (from C99).
This gives the name of the current function, as if the following definition appeared at the beginning of the function body:

static const char __func__[] = "function-name";
2021-03-02 22:28:28 -06:00
Stephen Heumann e226bba4c1 Update release notes. 2021-02-26 19:54:22 -06:00