diff --git a/doc/ca65.sgml b/doc/ca65.sgml index b0ccf6f5e..104c59bbd 100644 --- a/doc/ca65.sgml +++ b/doc/ca65.sgml @@ -2867,6 +2867,26 @@ See: , + + Switch on or off line continuations using the backslash character + before a newline. The option is off by default. + Note: Line continuations do not work in a comment. A backslash at the + end of a comment is treated as part of the comment and does not trigger + line continuation. + + Example: + + + .feature line_continuations + ; Allow line continuations + + lda \ + #$20 ; This is legal now + + + For backward compatibility reasons, the .LINECONT + control command + is also supported and enables the same feature. + long_jsr_jmp_rts Affects 65816 mode only. @@ -3371,26 +3391,6 @@ See: ,

- - Switch on or off line continuations using the backslash character - before a newline. The option is off by default. - Note: Line continuations do not work in a comment. A backslash at the - end of a comment is treated as part of the comment and does not trigger - line continuation. - The command can be followed by a '+' or '-' character to switch the - option on or off respectively. - - Example: - - - .linecont + ; Allow line continuations - - lda \ - #$20 ; This is legal now - - - .LIST

Enable output to the listing. The command can be followed by a boolean @@ -4489,9 +4489,9 @@ different: Macros defined with may not span more than a line. You may use line continuation (see ) to spread the definition over - more than one line for increased readability, but the macro itself - may not contain an end-of-line token. + id="line_continuations" name="line_continuations">) to spread the + definition over more than one line for increased readability, but the + macro itself may not contain an end-of-line token. Macros defined with share the name space with classic macros, but they are detected and replaced diff --git a/src/ca65/feature.c b/src/ca65/feature.c index 9f5ca5876..6a38900b5 100644 --- a/src/ca65/feature.c +++ b/src/ca65/feature.c @@ -67,6 +67,7 @@ static const char* const FeatureKeys[FEAT_COUNT] = { "bracket_as_indirect", "string_escapes", "long_jsr_jmp_rts", + "line_continuations", }; @@ -121,6 +122,7 @@ void SetFeature (feature_t Feature, unsigned char On) case FEAT_BRACKET_AS_INDIRECT: BracketAsIndirect = On; break; case FEAT_STRING_ESCAPES: StringEscapes = On; break; case FEAT_LONG_JSR_JMP_RTS: LongJsrJmpRts = On; break; + case FEAT_LINE_CONTINUATIONS: LineCont = On; break; default: break; } } diff --git a/src/ca65/feature.h b/src/ca65/feature.h index 8eeb62e6f..4d307f74d 100644 --- a/src/ca65/feature.h +++ b/src/ca65/feature.h @@ -69,6 +69,7 @@ typedef enum { FEAT_BRACKET_AS_INDIRECT, FEAT_STRING_ESCAPES, FEAT_LONG_JSR_JMP_RTS, + FEAT_LINE_CONTINUATIONS, /* Special value: Number of features available */ FEAT_COUNT diff --git a/src/cc65.vcxproj b/src/cc65.vcxproj index 6f3f8e4e4..9c1719538 100644 --- a/src/cc65.vcxproj +++ b/src/cc65.vcxproj @@ -69,6 +69,7 @@ + @@ -150,6 +151,7 @@ + @@ -214,4 +216,4 @@ - \ No newline at end of file + diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index 09fb0f088..6260463ca 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -723,7 +723,6 @@ void _g_getimmed(unsigned Flags, uintptr_t Val, long Offs) /* Load a constant into the primary register */ { unsigned char B1, B2, B3, B4; - unsigned Done; // LOG(("file:%s\n", file ? file : "null")); // LOG(("func:%s\n", func ? func : "null")); @@ -767,40 +766,15 @@ void _g_getimmed(unsigned Flags, uintptr_t Val, long Offs) B3 = (unsigned char) (Val >> 16); B4 = (unsigned char) (Val >> 24); - /* Remember which bytes are done */ - Done = 0; - - /* Load the value */ - AddCodeLine ("ldx #$%02X\t; g_getimmed LONG", B2); - Done |= 0x02; - if (B2 == B3) { - AddCodeLine ("stx sreg"); - Done |= 0x04; - } - if (B2 == B4) { - AddCodeLine ("stx sreg+1"); - Done |= 0x08; - } - if ((Done & 0x04) == 0 && B1 != B3) { - AddCodeLine ("lda #$%02X", B3); - AddCodeLine ("sta sreg"); - Done |= 0x04; - } - if ((Done & 0x08) == 0 && B1 != B4) { - AddCodeLine ("lda #$%02X", B4); - AddCodeLine ("sta sreg+1"); - Done |= 0x08; - } + /* Load the value. Don't be too smart here and let + * the optimizer do its job. + */ + AddCodeLine ("lda #$%02X", B4); + AddCodeLine ("sta sreg+1"); + AddCodeLine ("lda #$%02X", B3); + AddCodeLine ("sta sreg"); AddCodeLine ("lda #$%02X", B1); - Done |= 0x01; - if ((Done & 0x04) == 0) { - CHECK (B1 == B3); - AddCodeLine ("sta sreg"); - } - if ((Done & 0x08) == 0) { - CHECK (B1 == B4); - AddCodeLine ("sta sreg+1"); - } + AddCodeLine ("ldx #$%02X", B2); break; default: @@ -4096,13 +4070,7 @@ void g_dec (unsigned flags, unsigned long val) } else { /* Inline the code */ if (val < 0x300) { - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bne %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - AddCodeLine ("dea"); - } else if ((val & 0xFF) != 0) { + if ((val & 0xFF) != 0) { unsigned L = GetLocalLabel(); AddCodeLine ("sec"); AddCodeLine ("sbc #$%02X", (unsigned char) val); diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index 3a2c2a35d..c9c1592bc 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -57,6 +57,7 @@ #include "coptcmp.h" #include "coptind.h" #include "coptjmp.h" +#include "coptlong.h" #include "coptmisc.h" #include "coptptrload.h" #include "coptptrstore.h" @@ -150,6 +151,8 @@ static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 }; static OptFunc DOptLoad3 = { OptLoad3, "OptLoad3", 0, 0, 0, 0, 0, 0 }; +static OptFunc DOptLongAssign = { OptLongAssign, "OptLongAssign", 100, 0, 0, 0, 0, 0 }; +static OptFunc DOptLongCopy = { OptLongCopy, "OptLongCopy", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 165, 0, 0, 0, 0, 0 }; static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 200, 0, 0, 0, 0, 0 }; static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 }; @@ -262,6 +265,8 @@ static OptFunc* OptFuncs[] = { &DOptLoad1, &DOptLoad2, &DOptLoad3, + &DOptLongAssign, + &DOptLongCopy, &DOptNegAX1, &DOptNegAX2, &DOptPrecalc, @@ -632,6 +637,7 @@ static unsigned RunOptGroup1 (CodeSeg* S) Changes += RunOptFunc (S, &DOptAdd6, 1); Changes += RunOptFunc (S, &DOptSub1, 1); Changes += RunOptFunc (S, &DOptSub3, 1); + Changes += RunOptFunc (S, &DOptLongAssign, 1); Changes += RunOptFunc (S, &DOptStore4, 1); Changes += RunOptFunc (S, &DOptStore5, 1); Changes += RunOptFunc (S, &DOptShift1, 1); @@ -641,6 +647,7 @@ static unsigned RunOptGroup1 (CodeSeg* S) Changes += RunOptFunc (S, &DOptStore1, 1); Changes += RunOptFunc (S, &DOptStore2, 5); Changes += RunOptFunc (S, &DOptStore3, 5); + Changes += RunOptFunc (S, &DOptLongCopy, 1); /* Return the number of changes */ return Changes; diff --git a/src/cc65/compile.c b/src/cc65/compile.c index 02d37c53e..bef55e375 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -257,6 +257,7 @@ static void Parse (void) /* Parse the initialization */ ParseInit (Sym->Type); + } else { /* This is a declaration */ @@ -326,21 +327,19 @@ NextDecl: } } - /* Function declaration? */ - if (Sym && IsTypeFunc (Sym->Type)) { - - /* Function */ - if (CurTok.Tok == TOK_SEMI) { - /* Prototype only */ - NeedClean = 0; - NextToken (); - } else if (CurTok.Tok == TOK_LCURLY) { - /* ISO C: The type category in a function definition cannot be - ** inherited from a typedef. - */ + /* Finish the declaration */ + if (Sym) { + /* Function definition? */ + if (IsTypeFunc (Sym->Type) && CurTok.Tok == TOK_LCURLY) { if (IsTypeFunc (Spec.Type) && TypeCmp (Sym->Type, Spec.Type).C >= TC_EQUAL) { + /* ISO C: The type category in a function definition cannot be + ** inherited from a typedef. + */ Error ("Function cannot be defined with a typedef"); } else if (comma) { + /* ISO C: A function definition cannot shall its return type + ** specifier with other declarators. + */ Error ("';' expected after top level declarator"); } @@ -350,30 +349,19 @@ NextDecl: /* Make sure we aren't omitting any work */ CheckDeferredOpAllDone (); - } - - } else { - - if (Sym) { + } else { /* Must be followed by a semicolon */ - if (CurTok.Tok != TOK_SEMI) { + if (ConsumeSemi ()) { + NeedClean = 0; + } else { NeedClean = -1; } - ConsumeSemi (); } - } /* Try some smart error recovery */ if (PrevErrorCount != ErrorCount && NeedClean < 0) { - /* Some fix point tokens that are used for error recovery */ - static const token_t TokenList[] = { TOK_SEMI, TOK_RCURLY }; - - SmartErrorSkip (); - SkipTokens (TokenList, sizeof (TokenList) / sizeof (TokenList[0])); - if (CurTok.Tok == TOK_SEMI || CurTok.Tok == TOK_RCURLY) { - NextToken (); - } + SmartErrorSkip (1); } } diff --git a/src/cc65/coptlong.c b/src/cc65/coptlong.c new file mode 100644 index 000000000..16f089e49 --- /dev/null +++ b/src/cc65/coptlong.c @@ -0,0 +1,210 @@ +/*****************************************************************************/ +/* */ +/* coptlong.c */ +/* */ +/* Long integers optimizations */ +/* */ +/* */ +/* */ +/* (C) 2001-2009, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* (C) 2023, Colin Leroy-Mira OPC == OP65_LDA && + L[1]->OPC == OP65_STA && + !strcmp (L[1]->Arg, "sreg+1") && + L[2]->OPC == OP65_LDA && + L[3]->OPC == OP65_STA && + !strcmp (L[3]->Arg, "sreg") && + L[4]->OPC == OP65_LDA && + L[5]->OPC == OP65_LDX && + L[6]->OPC == OP65_STA && + L[7]->OPC == OP65_STX && + !strncmp(L[7]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[7]->Arg + strlen(L[6]->Arg), "+1") && + L[8]->OPC == OP65_LDY && + !strcmp (L[8]->Arg, "sreg") && + L[9]->OPC == OP65_STY && + !strncmp(L[9]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[9]->Arg + strlen(L[6]->Arg), "+2") && + L[10]->OPC == OP65_LDY && + !strcmp (L[10]->Arg, "sreg+1") && + L[11]->OPC == OP65_STY && + !strncmp(L[11]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[11]->Arg + strlen(L[6]->Arg), "+3") && + !RegXUsed (S, I+11)) { + + L[1]->AM = L[11]->AM; + CE_SetArg(L[1], L[11]->Arg); + + L[3]->AM = L[9]->AM; + CE_SetArg(L[3], L[9]->Arg); + + CS_DelEntries (S, I+8, 4); + + /* Remember, we had changes */ + ++Changes; + } + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} + +unsigned OptLongCopy (CodeSeg* S) +/* Simplify long copies. +** Recognize +** lda XXX+3 0 +** sta sreg+1 1 +** lda XXX+2 2 +** sta sreg 3 +** ldx XXX+1 4 +** lda XXX 5 +** sta YYY 6 +** stx YYY+1 7 +** ldy sreg 8 +** sty YYY+2 9 +** ldy sreg+1 10 +** sty YYY+3 11 +** and simplify if not used right after. +*/ +{ + unsigned Changes = 0; + + /* Walk over the entries */ + unsigned I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[12]; + + /* Get next entry */ + L[0] = CS_GetEntry (S, I); + + if (CS_GetEntries (S, L+1, I+1, 11)) { + if (L[0]->OPC == OP65_LDA && + !strncmp(L[0]->Arg, L[5]->Arg, strlen(L[5]->Arg)) && + !strcmp(L[0]->Arg + strlen(L[5]->Arg), "+3") && + L[1]->OPC == OP65_STA && + !strcmp (L[1]->Arg, "sreg+1") && + L[2]->OPC == OP65_LDA && + !strncmp(L[2]->Arg, L[5]->Arg, strlen(L[5]->Arg)) && + !strcmp(L[2]->Arg + strlen(L[5]->Arg), "+2") && + L[3]->OPC == OP65_STA && + !strcmp (L[3]->Arg, "sreg") && + L[4]->OPC == OP65_LDX && + !strncmp(L[4]->Arg, L[5]->Arg, strlen(L[5]->Arg)) && + !strcmp(L[4]->Arg + strlen(L[5]->Arg), "+1") && + L[5]->OPC == OP65_LDA && + L[6]->OPC == OP65_STA && + L[7]->OPC == OP65_STX && + !strncmp(L[7]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[7]->Arg + strlen(L[6]->Arg), "+1") && + L[8]->OPC == OP65_LDY && + !strcmp (L[8]->Arg, "sreg") && + L[9]->OPC == OP65_STY && + !strncmp(L[9]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[9]->Arg + strlen(L[6]->Arg), "+2") && + L[10]->OPC == OP65_LDY && + !strcmp (L[10]->Arg, "sreg+1") && + L[11]->OPC == OP65_STY && + !strncmp(L[11]->Arg, L[6]->Arg, strlen(L[6]->Arg)) && + !strcmp(L[11]->Arg + strlen(L[6]->Arg), "+3") && + !RegXUsed (S, I+11)) { + + L[1]->AM = L[11]->AM; + CE_SetArg(L[1], L[11]->Arg); + + L[3]->AM = L[9]->AM; + CE_SetArg(L[3], L[9]->Arg); + + CS_DelEntries (S, I+8, 4); + + /* Remember, we had changes */ + ++Changes; + } + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} diff --git a/src/cc65/coptlong.h b/src/cc65/coptlong.h new file mode 100644 index 000000000..c0233c299 --- /dev/null +++ b/src/cc65/coptlong.h @@ -0,0 +1,63 @@ +/*****************************************************************************/ +/* */ +/* coptlong.h */ +/* */ +/* Long integers optimizations */ +/* */ +/* */ +/* */ +/* (C) 2001-2009, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* (C) 2023, Colin Leroy-Mira = 0 && ConsumeSemi ()) { + NeedClean = 0; + } else { + NeedClean = -1; + } + + /* Try some smart error recovery */ + if (NeedClean < 0) { + SmartErrorSkip (1); + } } /* Skip the closing brace */ @@ -1187,11 +1220,6 @@ NextMember: if (CurTok.Tok != TOK_COMMA) { Flags |= SC_FICTITIOUS; } - /* Empty union is not supported now */ - if (UnionSize == 0) { - Error ("Empty union type '%s' is not supported", Name); - } - /* Make a real entry from the forward decl and return it */ return AddStructSym (Name, SC_UNION | SC_DEF | Flags, UnionSize, FieldTab, DSFlags); } @@ -1236,8 +1264,9 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags) while (CurTok.Tok != TOK_RCURLY) { /* Get the type of the entry */ - DeclSpec Spec; - int SignednessSpecified = 0; + DeclSpec Spec; + int SignednessSpecified = 0; + int NeedClean = 0; /* Check for a _Static_assert */ if (CurTok.Tok == TOK_STATIC_ASSERT) { @@ -1262,7 +1291,7 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags) } /* Get type and name of the struct field */ - ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); + NeedClean = ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); /* Check for a bit-field declaration */ FieldWidth = ParseFieldWidth (&Decl); @@ -1403,7 +1432,18 @@ NextMember: if (CurTok.Tok != TOK_COMMA) { } NextToken (); } - ConsumeSemi (); + + /* Must be followed by a semicolon */ + if (NeedClean >= 0 && ConsumeSemi ()) { + NeedClean = 0; + } else { + NeedClean = -1; + } + + /* Try some smart error recovery */ + if (NeedClean < 0) { + SmartErrorSkip (1); + } } if (BitOffs > 0) { @@ -1426,11 +1466,6 @@ NextMember: if (CurTok.Tok != TOK_COMMA) { Flags |= SC_FICTITIOUS; } - /* Empty struct is not supported now */ - if (StructSize == 0) { - Error ("Empty struct type '%s' is not supported", Name); - } - /* Make a real entry from the forward decl and return it */ return AddStructSym (Name, SC_STRUCT | SC_DEF | Flags, StructSize, FieldTab, DSFlags); } @@ -1778,7 +1813,7 @@ static void ParseOldStyleParamList (FuncDesc* F) Error ("Identifier expected for parameter name"); /* Try some smart error recovery */ - if (SmartErrorSkip () < 0) { + if (SmartErrorSkip (0) < 0) { break; } } @@ -1868,7 +1903,7 @@ static void ParseOldStyleParamList (FuncDesc* F) if (PrevErrorCount != ErrorCount && CurTok.Tok != TOK_LCURLY) { /* Try some smart error recovery */ - SmartErrorSkip (); + SmartErrorSkip (0); } } @@ -1953,7 +1988,7 @@ static void ParseAnsiParamList (FuncDesc* F) if (PrevErrorCount != ErrorCount) { /* Try some smart error recovery */ - if (SmartErrorSkip () < 0) { + if (SmartErrorSkip (0) < 0) { break; } } @@ -2335,7 +2370,7 @@ int ParseDecl (const DeclSpec* Spec, Declarator* D, declmode_t Mode) /* Try some smart error recovery */ if (CurTok.Tok != TOK_LCURLY || !IsTypeFunc (D->Type)) { - return SmartErrorSkip (); + return SmartErrorSkip (0); } } diff --git a/src/cc65/declare.h b/src/cc65/declare.h index add86594d..1ce764f7a 100644 --- a/src/cc65/declare.h +++ b/src/cc65/declare.h @@ -128,11 +128,22 @@ typedef enum { -int SmartErrorSkip (void); -/* Try some smart error recovery. Skip tokens until either a comma or semicolon -** that is not enclosed in an open parenthesis/bracket/curly brace, or until an -** unpaired right parenthesis/bracket/curly brace is reached. Return 0 if it is -** the former case, or -1 if it is the latter case. */ +int SmartErrorSkip (int WholeDecl); +/* Try some smart error recovery. +** +** - If WholeDecl is 0: +** Skip tokens until a comma or closing curly brace that is not enclosed in +** an open parenthesis/bracket/curly brace, or until a semicolon, EOF or +** unpaired right parenthesis/bracket/curly brace is reached. +** +** - If WholeDecl is non-0: +** Skip tokens until a closing curly brace that is not enclosed in an open +** parenthesis/bracket/curly brace, or until a semicolon or EOF is reached. +** +** Return 0 if this exits as soon as it reaches an EOF. Return 0 as well if +** this exits with no open parentheses/brackets/curly braces. Otherwise, return +** -1. +*/ Type* ParseType (Type* Type); /* Parse a complete type specification */ diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 61c28f6c0..686d5a9ed 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -1426,9 +1426,9 @@ static void Primary (ExprDesc* E) } else { /* Let's see if this is a C99-style declaration */ DeclSpec Spec; - ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_INT, SC_AUTO); + ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_NONE, SC_AUTO); - if (Spec.Type->C != T_END) { + if ((Spec.Flags & DS_DEF_TYPE) == 0) { /* Recognized but not supported */ Error ("Mixed declarations and code are not supported in cc65"); @@ -4816,9 +4816,10 @@ static void hieQuest (ExprDesc* Expr) /* Avoid further errors */ ResultType = NewPointerTo (type_void); } else { - /* Result has the composite type */ + /* Result has the properly qualified composite type */ ResultType = TypeDup (Expr2.Type); TypeComposition (ResultType, Expr3.Type); + ResultType[1].C |= GetQualifier (Indirect (Expr3.Type)); } } } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) { diff --git a/src/cc65/initdata.c b/src/cc65/initdata.c index 52fe1e5d5..2edd0c3bc 100644 --- a/src/cc65/initdata.c +++ b/src/cc65/initdata.c @@ -533,18 +533,20 @@ static unsigned ParseStructInit (Type* T, int* Braces, int AllowFlexibleMembers) /* This may be an anonymous bit-field, in which case it doesn't ** have an initializer. */ - if (SymIsBitField (TagSym) && (IsAnonName (TagSym->Name))) { - /* Account for the data and output it if we have at least a full - ** byte. We may have more if there was storage unit overlap, for - ** example two consecutive 7 bit fields. Those would be packed - ** into 2 bytes. - */ - SI.ValBits += TagSym->Type->A.B.Width; - CHECK (SI.ValBits <= CHAR_BIT * sizeof(SI.BitVal)); - /* TODO: Generalize this so any type can be used. */ - CHECK (SI.ValBits <= LONG_BITS); - while (SI.ValBits >= CHAR_BITS) { - DefineBitFieldData (&SI); + if (SymIsBitField (TagSym) && IsAnonName (TagSym->Name)) { + if (!IsTypeUnion (T)) { + /* Account for the data and output it if we have at least a full + ** byte. We may have more if there was storage unit overlap, for + ** example two consecutive 7 bit fields. Those would be packed + ** into 2 bytes. + */ + SI.ValBits += TagSym->Type->A.B.Width; + CHECK (SI.ValBits <= CHAR_BIT * sizeof(SI.BitVal)); + /* TODO: Generalize this so any type can be used. */ + CHECK (SI.ValBits <= LONG_BITS); + while (SI.ValBits >= CHAR_BITS) { + DefineBitFieldData (&SI); + } } /* Avoid consuming the comma if any */ goto NextMember; @@ -650,15 +652,15 @@ static unsigned ParseStructInit (Type* T, int* Braces, int AllowFlexibleMembers) /* Skip the comma next round */ SkipComma = 1; -NextMember: - /* Next member. For unions, only the first one can be initialized */ + /* For unions, only the first named member can be initialized */ if (IsTypeUnion (T)) { - /* Union */ TagSym = 0; - } else { - /* Struct */ - TagSym = TagSym->NextSym; + continue; } + +NextMember: + /* Next member */ + TagSym = TagSym->NextSym; } if (HasCurly) { diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index d30e591c9..1b5c1e0b5 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -178,7 +178,7 @@ static void CheckSymTable (SymTable* Tab) if (IS_Get (&WarnUnusedFunc)) { Warning ("Function '%s' is defined but never used", Entry->Name); } - } else { + } else if (!IsAnonName (Entry->Name)) { if (IS_Get (&WarnUnusedVar)) { Warning ("Variable '%s' is defined but never used", Entry->Name); } @@ -919,14 +919,8 @@ SymEntry* AddStructSym (const char* Name, unsigned Flags, unsigned Size, SymTabl /* SCType must be struct or union */ PRECONDITION (SCType == SC_STRUCT || SCType == SC_UNION); - if ((Flags & SC_FICTITIOUS) == 0) { - /* Do we have an entry with this name already? */ - TagEntry = FindSymInTable (CurTagTab, Name, HashStr (Name)); - } else { - /* Add a fictitious symbol in the fail-safe table */ - TagEntry = 0; - CurTagTab = FailSafeTab; - } + /* Do we have an entry with this name already? */ + TagEntry = FindSymInTable (CurTagTab, Name, HashStr (Name)); if (TagEntry) { @@ -954,6 +948,15 @@ SymEntry* AddStructSym (const char* Name, unsigned Flags, unsigned Size, SymTabl if (DSFlags != 0) { *DSFlags |= DS_NEW_TYPE_DEF; } + + if ((Flags & SC_FICTITIOUS) == SC_FICTITIOUS) { + /* Add a fictitious symbol in the fail-safe table */ + TagEntry = 0; + } else if (Size == 0) { + /* Empty struct is not supported now */ + Error ("Empty %s type '%s' is not supported", SCType == SC_STRUCT ? "struct" : "union", Name); + TagEntry = 0; + } } } @@ -1023,6 +1026,7 @@ SymEntry* AddBitField (const char* Name, const Type* T, unsigned Offs, } else { Entry->Type = NewBitFieldOf (T, BitOffs, BitWidth); } + Entry->Type[0].C |= GetQualifier (T) & T_MASK_QUAL; /* Add the entry to the symbol table */ AddSymEntry (FieldTab, Entry); diff --git a/src/cc65/typecmp.c b/src/cc65/typecmp.c index bb8bca880..e8b790a69 100644 --- a/src/cc65/typecmp.c +++ b/src/cc65/typecmp.c @@ -69,6 +69,7 @@ static int EqualFuncParams (const FuncDesc* F1, const FuncDesc* F2) /* Get the symbol types */ const Type* Type1 = Sym1->Type; const Type* Type2 = Sym2->Type; + typecmp_t CmpResult; /* If either of both functions is old style, apply the default ** promotions to the parameter type. @@ -84,9 +85,10 @@ static int EqualFuncParams (const FuncDesc* F1, const FuncDesc* F2) } } - /* Compare this field */ - if (TypeCmp (Type1, Type2).C < TC_EQUAL) { - /* Field types not equal */ + /* Compare types of this parameter */ + CmpResult = TypeCmp (Type1, Type2); + if (CmpResult.C < TC_EQUAL || (CmpResult.F & TCF_MASK_PARAM_DIFF) != 0) { + /* The types are not compatible */ return 0; } diff --git a/src/cc65/typecmp.h b/src/cc65/typecmp.h index fa97ca176..43acc7ea5 100644 --- a/src/cc65/typecmp.h +++ b/src/cc65/typecmp.h @@ -68,15 +68,17 @@ typedef enum { TCF_VOID_PTR_ON_LEFT = 0x01, /* lhs is a void pointer */ TCF_VOID_PTR_ON_RIGHT = 0x02, /* rhs is a void pointer */ TCF_MASK_VOID_PTR = TCF_VOID_PTR_ON_LEFT | TCF_VOID_PTR_ON_RIGHT, - TCF_QUAL_DIFF = 0x04, /* CVR qualifiers differ in a way that doesn't matter */ + TCF_QUAL_DIFF = 0x04, /* lhs doesn't have all of CVR qualifiers of rhs */ TCF_QUAL_IMPLICIT = 0x08, /* CVR qualifiers of lhs are stricter than those of rhs */ - TCF_PTR_QUAL_DIFF = 0x10, /* CVR qualifiers of pointers differ */ - TCF_PTR_QUAL_IMPLICIT = 0x20, /* CVR qualifiers of pointers are stricter on lhs than those on rhs */ - TCF_MASK_C_QUAL_DIFF = 0x3C, /* All C Standard qualifiers */ + TCF_MASK_CVR_DIFF = 0x0C, /* All CVR qualifiers */ + TCF_PTR_QUAL_DIFF = 0x10, /* lhs pointee doesn't have all of CVR qualifiers of rhs pointee */ + TCF_PTR_QUAL_IMPLICIT = 0x20, /* CVR qualifiers of pointees are stricter on lhs than those on rhs */ + TCF_MASK_PTR_QUAL_DIFF = 0x30, /* All CVR qualifiers of pointees */ TCF_ADDRSIZE_QUAL_DIFF = 0x40, /* Address size qualifiers differ */ TCF_CCONV_QUAL_DIFF = 0x80, /* Function calling conventions differ. Unused now */ TCF_INCOMPATIBLE_QUAL = TCF_ADDRSIZE_QUAL_DIFF | TCF_CCONV_QUAL_DIFF, - TCF_MASK_QUAL = TCF_MASK_C_QUAL_DIFF | TCF_INCOMPATIBLE_QUAL, + TCF_MASK_PARAM_DIFF = TCF_MASK_PTR_QUAL_DIFF | TCF_INCOMPATIBLE_QUAL, + TCF_MASK_QUAL = TCF_MASK_CVR_DIFF | TCF_MASK_PTR_QUAL_DIFF | TCF_INCOMPATIBLE_QUAL, } typecmpflag_t; typedef struct { diff --git a/src/cc65/typeconv.c b/src/cc65/typeconv.c index 38acd421b..8e0df5bb6 100644 --- a/src/cc65/typeconv.c +++ b/src/cc65/typeconv.c @@ -530,13 +530,6 @@ void TypeComposition (Type* lhs, const Type* rhs) } else if (RightCount != UNSPECIFIED) { SetElementCount (lhs, RightCount); } - } else { - /* Combine the qualifiers */ - if (IsClassPtr (lhs)) { - ++lhs; - ++rhs; - lhs->C |= GetQualifier (rhs); - } } /* Next type string element */ diff --git a/test/err/bug2018-bitfield.c b/test/err/bug2018-bitfield.c new file mode 100644 index 000000000..ea2928659 --- /dev/null +++ b/test/err/bug2018-bitfield.c @@ -0,0 +1,14 @@ +/* Bug #2018 - Compiler has problems with const struct fields */ + +typedef union U { + int a : 16; + const int b : 16; +} U; + +int main(void) +{ + U x = { 42 }; + x.b = 0; + + return 0; +} diff --git a/test/err/bug2285-composite-type.c b/test/err/bug2285-composite-type.c new file mode 100644 index 000000000..18a7b80a5 --- /dev/null +++ b/test/err/bug2285-composite-type.c @@ -0,0 +1,5 @@ +/* Bug #2285 - Regression in type composition */ + +void foo(); /* OK */ +void foo(int (*)(int)); /* OK */ +void foo(int (*)(long)); /* WRONG: Should be an error */ diff --git a/test/err/bug2286-param-qualifier.c b/test/err/bug2286-param-qualifier.c new file mode 100644 index 000000000..a014d0a0c --- /dev/null +++ b/test/err/bug2286-param-qualifier.c @@ -0,0 +1,4 @@ +/* Bug #2286 - Qualifiers of pointees of function parameters ignored for type compatibility check */ + +void woo(int* p); +void woo(const int* p); /* WRONG: Should be an error */ diff --git a/test/misc/Makefile b/test/misc/Makefile index c708b160b..811f7f462 100644 --- a/test/misc/Makefile +++ b/test/misc/Makefile @@ -58,24 +58,6 @@ $(ISEQUAL): ../isequal.c | $(WORKDIR) define PRG_template -# should compile, but gives an error -$(WORKDIR)/int-static-1888.$1.$2.prg: int-static-1888.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/int-static-1888.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - -# should compile, but gives an error -$(WORKDIR)/bug760.$1.$2.prg: bug760.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/bug760.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - -# should compile, but gives an error -$(WORKDIR)/bug1437.$1.$2.prg: bug1437.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/bug1437.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - # should compile, but gives an error $(WORKDIR)/bug1209-ind-goto-rev.$1.$2.prg: bug1209-ind-goto-rev.c | $(WORKDIR) @echo "FIXME: " $$@ "currently does not compile." @@ -106,18 +88,6 @@ $(WORKDIR)/pptest2.$1.$2.prg: pptest2.c | $(WORKDIR) $(if $(QUIET),echo misc/pptest2.$1.$2.prg) $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) -# should compile, but gives an error -$(WORKDIR)/bug1263.$1.$2.prg: bug1263.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/bug1263.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - -# should compile, but gives an error -$(WORKDIR)/bug1357.$1.$2.prg: bug1357.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/bug1357.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - # should compile, but compiler exits with internal error $(WORKDIR)/bug1211-ice-move-refs-2.$1.$2.prg: bug1211-ice-move-refs-2.c | $(WORKDIR) @echo "FIXME: " $$@ "currently does not compile." diff --git a/test/val/bitfield-union.c b/test/val/bitfield-union.c index 1fd201456..4c01d2183 100644 --- a/test/val/bitfield-union.c +++ b/test/val/bitfield-union.c @@ -1,5 +1,5 @@ /* - Copyright 2020 The cc65 Authors + Copyright 2020-2023 The cc65 Authors This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -25,6 +25,7 @@ #include typedef union { + const unsigned int : 1; unsigned int bf; struct { @@ -38,8 +39,12 @@ static unsigned char failures = 0; int main (void) { - bitfield_t bitfield = {0}; + bitfield_t bitfield = { 42 }; + printf ("Bitfield: %u\n", bitfield.bf); + if (bitfield.bf != 42) failures++; + + bitfield.bf ^= 42; printf ("Bitfield: %u\n", bitfield.bf); if (bitfield.bf != 0) failures++; diff --git a/test/val/long.c b/test/val/long.c new file mode 100644 index 000000000..076483c9b --- /dev/null +++ b/test/val/long.c @@ -0,0 +1,41 @@ +#include +#include + +int res = 0; + +int main(void) +{ + long a, b; + + a = 0x12345678L; + + /* Test assignment */ + b = a; + if (b != a) { + res++; + } + + /* Test increment */ + b++; + if (b != 0x12345679L) { + res++; + } + + /* Test decrement */ + b--; + if (b != 0x12345678L) { + res++; + } + + /* Test pre-decrement with test */ + if (--b != 0x12345677L) { + res++; + } + + a = --b; + if (a != 0x12345676L) { + res++; + } + + return res; +} diff --git a/test/val/static-long.c b/test/val/static-long.c new file mode 100644 index 000000000..a2e4e53a3 --- /dev/null +++ b/test/val/static-long.c @@ -0,0 +1,41 @@ +#include +#include + +int res = 0; + +int main(void) +{ + static long a, b; + + a = 0x12345678L; + + /* Test assignment */ + b = a; + if (b != a) { + res++; + } + + /* Test increment */ + b++; + if (b != 0x12345679L) { + res++; + } + + /* Test decrement */ + b--; + if (b != 0x12345678L) { + res++; + } + + /* Test pre-decrement with test */ + if (--b != 0x12345677L) { + res++; + } + + a = --b; + if (a != 0x12345676L) { + res++; + } + + return res; +} diff --git a/test/val/sub3.c b/test/val/sub3.c index 2a3646f9a..dd050224e 100644 --- a/test/val/sub3.c +++ b/test/val/sub3.c @@ -168,6 +168,31 @@ void post_dec_assign_test(void) failures++; } +void dex_tests(void) { + static unsigned int a, b; + + a = 257; + b = a - 1; + if (b != 256) { + printf("fail 257 => 256\n"); + failures++; + } + + a = 256; + b = a - 1; + if (b != 255) { + printf("fail 256 => 255\n"); + failures++; + } + + a = 255; + b = a - 1; + if (b != 254) { + printf("fail 255 => 254\n"); + failures++; + } +} + int main(void) { int0 = 5; @@ -186,6 +211,8 @@ int main(void) int1 = 5; post_dec_assign_test(); + dex_tests(); + printf("failures: %d\n",failures); return failures;