Commit Graph

440 Commits

Author SHA1 Message Date
Stephen Heumann
1dc0dc7a19 Implement remaining conversions of integer types to and from long long.
The floating-point conversions are not done yet (but do now give an error).
2021-02-01 22:43:35 -06:00
Stephen Heumann
091a25b25d Update the debugging format for long long values.
For now, "long long" is represented with the existing code for the SANE comp format, since their representation is the same except for the comp NaN. This allows existing debuggers that support comp to work with it. The code for "unsigned long long" includes the unsigned flag, so it is unambiguous.
2021-01-31 20:26:51 -06:00
Stephen Heumann
0e59588191 Merge branch 'master' into longlong 2021-01-31 14:32:31 -06:00
Stephen Heumann
393fb8d635 Make floating point to character type conversions yield values within the type's range.
This affects cases where the floating value, truncated to an integer, is outside the range of the destination type. Previously, the result value might appear to be an int value outside the range of the character type.

These situations are undefined behavior under the C standards, so this was not technically a bug, but the new behavior is less surprising. (Note that it still may not raise the "invalid" floating-point exception in some cases where Annex F would call for that.)
2021-01-31 14:04:27 -06:00
Stephen Heumann
130d332284 Fix bugs with several operations on negative values of type signed char.
The basic issue with all of these is that they failed to sign-extend the 8-bit signed char value to the full 16-bit A register. This could make certain operations on negative signed char values appear to yield positive values outside the range of signed char.

The following example code demonstrates the problems:

#include <stdio.h>
signed char f(void) {return -50;}
int main(void) {
        long l = -123;
        int i = -99;
        signed char sc = -47;
        signed char *scp = &sc;
        printf("%i\n", (signed char)l);
        printf("%i\n", (signed char)i);
        printf("%i\n", f());
        printf("%i\n", (*scp)++);
        printf("%i\n", *scp = -32);
}
2021-01-31 11:40:07 -06:00
Stephen Heumann
cb99b3778e Flag that conversions may not set CPU flags usable for a subsequent comparison.
There are several conversions that do not set the necessary flags, so they must be set separately before doing a comparison. Without this fix, comparisons of a value that was just converted might be mis-evaluated.

This led to bugs where the wrong side of an "if" could be followed in some cases, as in the below examples:

#include <stdio.h>
int g(void) {return 50;}
signed char h(void) {return 50;}
long lf(void) {return 50;}
int main(void) {
    signed char sc = 50;
    if ((int)(signed char)g()) puts("OK1");
    if ((int)h()) puts("OK2");
    if ((int)sc) puts("OK3");
    if ((int)lf()) puts("OK4");
}
2021-01-31 08:52:50 -06:00
Stephen Heumann
e8497c7b8f Begin implementing conversions to and from 64-bit types.
Some conversions are implemented, but others are not yet.
2021-01-31 08:37:21 -06:00
Stephen Heumann
807a143e51 Implement 64-bit addition and subtraction. 2021-01-30 23:31:18 -06:00
Stephen Heumann
2426794194 Add support for new pcodes in optimizer. 2021-01-30 21:11:06 -06:00
Stephen Heumann
2e44c36c59 Implement unary negation and bitwise complement for 64-bit types. 2021-01-30 13:49:06 -06:00
Stephen Heumann
abb0fa0fc1 Implement bitwise and/or/xor for 64-bit types.
This introduces three new intermediate codes for these operations.
2021-01-30 00:25:15 -06:00
Stephen Heumann
8b12b7b734 Handle (unsigned) long long in the front-end code for binary conversions.
There is not yet code generation support for the conversion opcodes (pc_cnv/pc_cnn).
2021-01-29 23:25:21 -06:00
Stephen Heumann
2222e4a0b4 Restore old order of baseTypeEnum values.
The ordinal values of these are hard-coded in code for handling pc_cnv/pc_cnn, so let's avoid changing them.
2021-01-29 23:23:03 -06:00
Stephen Heumann
fa835aca43 Implement basic load/store ops for long long.
The following intermediate codes should now work:
pc_lod
pc_pop
pc_str
pc_cop
pc_sro
pc_cpo
2021-01-29 23:11:08 -06:00
Stephen Heumann
085cd7eb1b Initial code to recognize 'long long' as a type. 2021-01-29 22:27:11 -06:00
Stephen Heumann
b1d4d8d668 Give errors for certain invalid compound assignment expressions.
The following example shows cases that were erroneously permitted before:

int main(void) {
        int i, *p;
        i *= p;
        i <<= 5.0;
        i <<= (void)1;
}
2021-01-29 12:49:28 -06:00
Stephen Heumann
5dbe632f33 Update the c26.0.1.cc test case to include recently added headers.
Also, change all the header names to lower case.
2021-01-26 17:47:33 -06:00
Stephen Heumann
110d9995f4 Update release notes. 2021-01-26 17:15:45 -06:00
Stephen Heumann
f2a66a524a Fix several issues with evaluation of the ++ and -- operators.
These would generally not work correctly on bit-fields, or on floating-point values that were in a structure or were accessed via a pointer.

The below program is an example that would demonstrate problems:

#include <stdio.h>

int main(void) {
        struct {
                signed int i:7;
                unsigned long int j:6;
                _Bool b:1;
                double d;
        } s = {-10, -20, 0, 5.0};

        double d = 70.0, *dp = &d;

        printf("%i\n", (int)s.i++);
        printf("%i\n", (int)s.i--);
        printf("%i\n", (int)++s.i);
        printf("%i\n", (int)--s.i);
        printf("%i\n", (int)s.i);

        printf("%i\n", (int)s.j++);
        printf("%i\n", (int)s.j--);
        printf("%i\n", (int)++s.j);
        printf("%i\n", (int)--s.j);
        printf("%i\n", (int)s.j);

        printf("%i\n", s.b++);
        printf("%i\n", s.b--);
        printf("%i\n", ++s.b);
        printf("%i\n", --s.b);
        printf("%i\n", s.b);

        printf("%f\n", s.d++);
        printf("%f\n", s.d--);
        printf("%f\n", ++s.d);
        printf("%f\n", --s.d);
        printf("%f\n", s.d);

        printf("%f\n", (*dp)++);
        printf("%f\n", (*dp)--);
        printf("%f\n", ++*dp);
        printf("%f\n", --*dp);
        printf("%f\n", *dp);
}
2021-01-26 12:31:54 -06:00
Stephen Heumann
acab97ae08 Add <stdbool.h> header.
Also, revise handling of boolean constants in <types.h> so that they do not conflict with the definitions in <stdbool.h>.
2021-01-25 22:04:26 -06:00
Stephen Heumann
52132db18a Implement the _Bool type from C99. 2021-01-25 21:22:58 -06:00
Stephen Heumann
83a1a7ad88 Update release notes. 2021-01-24 13:44:16 -06:00
Stephen Heumann
5014fb97f9 Make 32-bit int (with #pragma unix 1) a distinct type from long. 2021-01-24 13:31:12 -06:00
Stephen Heumann
f0a3808c18 Add a new #pragma ignore option to treat char and unsigned char as compatible.
This is contrary to the C standards, but ORCA/C historically permitted it (as do some other compilers), and I think there is a fair amount of existing code that relies on it.
2020-05-22 17:11:13 -05:00
Stephen Heumann
5d64436e6e Implement __STDC_HOSTED__ macro (from C99).
This is normally 1 (indicating a hosted implementation, where the full standard library is available and the program starts by executing main()), but it is 0 if one of the pragmas for special types of programs with different entry points has been used.
2020-03-07 15:51:29 -06:00
Stephen Heumann
a62cbe531a Implement __STDC_NO_...__ macros as specified by C11.
These indicate that various optional features of the C standard are not supported.
2020-03-06 23:29:54 -06:00
Stephen Heumann
c0b2b44cad Add a new representation of C basic types and use it for type checking.
This allows us to distinguish int from short, etc.
2020-03-01 15:00:02 -06:00
Stephen Heumann
6c0ec564c6 Fix a typo in CGI.Comments. 2020-02-29 17:48:28 -06:00
Stephen Heumann
8f5b063823 Update ORCA/C version number to 2.2.0 B4. 2020-02-05 18:24:10 -06:00
Stephen Heumann
41cb879936 In preprocessor expressions, always replace identifiers with constant 0.
This was not happening for declared identifiers (variables and functions) or for enum constants, as demonstrated in the following example:

enum {a,b,c};
#if b
#error "bad b"
#endif

int x = 0;
#if x
#error "bad x"
#endif
2020-02-05 18:23:45 -06:00
Stephen Heumann
c7371b6709 Edit and update release notes. 2020-02-04 18:44:19 -06:00
Stephen Heumann
32614abfca Allow '/*' or '//' in character constants.
These should not start a comment.
2020-02-04 18:42:55 -06:00
Stephen Heumann
0065e89842 Update release notes. 2020-02-02 14:28:20 -06:00
Stephen Heumann
c84c4d9c5c Check for non-void functions that execute to the end without returning a value.
This generalizes the heuristic approach for checking whether _Noreturn functions could execute to the end of the function, extending it to apply to any function with a non-void return type. These checks use the same #pragma lint bit but give different messages depending on the situation.
2020-02-02 13:50:15 -06:00
Stephen Heumann
77dcfdf3ee Implement support for macros with variable arguments (C99). 2020-01-31 20:07:10 -06:00
Stephen Heumann
1ba25fa704 Update release notes. 2020-01-30 18:05:46 -06:00
Stephen Heumann
bc951b6735 Make lint report some more cases where noreturn functions may return.
This uses a heuristic that may produce both false positives and false negatives, but any false positives should reflect extraneous code at the end of the function that is not actually reachable.
2020-01-30 17:35:15 -06:00
Stephen Heumann
76eb476809 Address some issues with stringization of macro arguments.
We now insert spaces corresponding to whitespace between tokens, and string tokens are enclosed in quotes.

There are still issues with (at least) escape sequences in strings and comments between tokens.
2020-01-30 12:48:16 -06:00
Stephen Heumann
ea72e60b4a Spellcheck the CGI.Comments file.
There are no substantive changes.
2020-01-30 12:44:32 -06:00
Stephen Heumann
3676e685b9 Disallow dereferencing of non-pointer types in l-values.
This fixes #67.
2020-01-29 22:52:10 -06:00
Stephen Heumann
a4abc5e421 Recognize enum type specifiers as such.
They were erroneously triggering the "type specifier missing" lint warning.
2020-01-29 21:15:33 -06:00
Stephen Heumann
5210723868 Update release notes. 2020-01-29 20:29:19 -06:00
Stephen Heumann
80c513bbf2 Add a lint flag for checking if _Noreturn functions may return.
Currently, this only flags return statements, not cases where they may execute to the end of the function. (Whether the function will actually return is not decidable in general, although it may be in special cases).
2020-01-29 19:26:45 -06:00
Stephen Heumann
4fd642abb4 Add lint check for return with no value in a non-void function.
This is disallowed in C99 and later.
2020-01-29 18:50:45 -06:00
Stephen Heumann
a9f5fb13d8 Introduce a new #pragma lint bit for syntax that C99 disallows.
This currently checks for:
*Calls to undefined functions (same as bit 0)
*Parameters not declared in K&R-style function definitions
*Declarations or type names with no type specifiers (includes but is broader than the condition checked by bit 1)
2020-01-29 18:33:19 -06:00
Stephen Heumann
ffe6c4e924 Spellcheck comments throughout the code.
There are no non-comment changes.
2020-01-29 17:09:52 -06:00
Stephen Heumann
d60104cc47 Tweak handling of lint warnings.
If there were a warning and an error on the same line, and errors were treated as terminal, the warning could sometimes be reported as an error.
2020-01-29 12:16:17 -06:00
Stephen Heumann
a72b611272 Make unnamed bit-fields take up space.
They should take up the same space as a named bit-field of the same width.

This fixes #60.
2020-01-28 22:54:40 -06:00
Stephen Heumann
e6a0769bed Fix register optimizer bug that generated bad code in some cases.
The register optimizer tracks when a register is known to contain the same value as a memory location (direct page or absolute) and does optimizations based on this. But it did not always recognize when this information had become invalid because of a subsequent store to the memory location, so it might perform invalid optimizations. This patch adds those checks.

This fixes #66.
2020-01-28 12:54:18 -06:00
Stephen Heumann
f5cd1e3e3a Recognize designated initializers enough to give an error and skip them.
Previously, the designated initializer syntax could confuse the parser enough to cause null pointer dereferences. This avoids that, and also gives a more meaningful error message to the user.
2020-01-28 12:48:09 -06:00