Allowed old-style (K and R) function declarations to be fastcall.

That lets them match old-style definitions.  It avoids "Type conflict" error messages.  It allows shorter function calls.

Fixed the types of some variables in "test/ref/otccex.c".  It avoids crashes on 64-bit Windows (32-bit Windows with 64-bit pointers).
This commit is contained in:
Greg King 2019-07-22 09:05:01 -04:00
parent cceffbdb8c
commit a0db846a97
7 changed files with 66 additions and 75 deletions

View file

@ -42,7 +42,7 @@ The standard compliant variations <tt/__cdecl__/ and <tt/__fastcall__/ are alway
If a function has a prototype, parameters are pushed to the C-stack as their respective types If a function has a prototype, parameters are pushed to the C-stack as their respective types
(i.e. a <tt/char/ parameter will push 1 byte), but if a function has no prototype, default (i.e. a <tt/char/ parameter will push 1 byte), but if a function has no prototype, default
promotions will apply. This means that with no prototype, <tt/char/ will be promoted promotions will apply. This means that with no prototype, <tt/char/ will be promoted
to <tt/int/ and be pushed as 2 bytes. K &amp; R style function prototypes may be used, to <tt/int/ and be pushed as 2 bytes. "K &amp; R"-style forward declarations may be used,
but they will function the same as if no prototype was used. but they will function the same as if no prototype was used.
<sect1>Prologue, before the function call<p> <sect1>Prologue, before the function call<p>
@ -61,8 +61,8 @@ The rightmost parameter will have the lowest address on the stack,
and multi-byte parameters will have their least significant byte at the lower address. and multi-byte parameters will have their least significant byte at the lower address.
The <tt/sp/ pseudo-register is a zeropage pointer to the base of the C-stack. The <tt/sp/ pseudo-register is a zeropage pointer to the base of the C-stack.
If the function has no prototype or is variadic If the function is variadic, the <tt/Y/ register will contain the number of
the <tt/Y/ register will contain the number of bytes pushed to the stack for this function. bytes pushed to the stack for this function.
Example: Example:
<tscreen><verb> <tscreen><verb>
@ -108,8 +108,9 @@ The C-stack pointer <tt/sp/ must be restored by the function to its value before
function call prologue. It may pop all of its parameters from the C-stack function call prologue. It may pop all of its parameters from the C-stack
(e.g. using the <tt/runtime/ function <tt/popa/), (e.g. using the <tt/runtime/ function <tt/popa/),
or it could adjust <tt/sp/ directly. or it could adjust <tt/sp/ directly.
If the function has no prototype, or is variadic the <tt/Y/ register contains the If the function is variadic, the <tt/Y/ register contains the number of bytes
number of bytes pushed to the stack on entry, which may be added to <tt/sp/ to restore its original state. pushed to the stack on entry, which may be added to <tt/sp/ to restore its
original state.
The internal pseudo-register <tt/regbank/ must not be changed by the function. The internal pseudo-register <tt/regbank/ must not be changed by the function.
@ -136,4 +137,3 @@ it may clobber any of these itself:
</article> </article>

View file

@ -171,8 +171,9 @@ static void Parse (void)
(CurTok.Tok != TOK_SEMI)) { (CurTok.Tok != TOK_SEMI)) {
FuncDesc* D = GetFuncDesc (Decl.Type); FuncDesc* D = GetFuncDesc (Decl.Type);
if (D->Flags & FD_EMPTY) { if (D->Flags & FD_EMPTY) {
D->Flags = (D->Flags & ~(FD_EMPTY | FD_VARIADIC)) | FD_VOID_PARAM; D->Flags = (D->Flags & ~FD_EMPTY) | FD_VOID_PARAM;
} }
} }

View file

@ -1132,7 +1132,7 @@ static Type* ParamTypeCvt (Type* T)
static void ParseOldStyleParamList (FuncDesc* F) static void ParseOldStyleParamList (FuncDesc* F)
/* Parse an old style (K&R) parameter list */ /* Parse an old-style (K&R) parameter list */
{ {
/* Some fix point tokens that are used for error recovery */ /* Some fix point tokens that are used for error recovery */
static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI }; static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI };
@ -1234,7 +1234,7 @@ static void ParseOldStyleParamList (FuncDesc* F)
static void ParseAnsiParamList (FuncDesc* F) static void ParseAnsiParamList (FuncDesc* F)
/* Parse a new style (ANSI) parameter list */ /* Parse a new-style (ANSI) parameter list */
{ {
/* Parse params */ /* Parse params */
while (CurTok.Tok != TOK_RPAREN) { while (CurTok.Tok != TOK_RPAREN) {
@ -1330,32 +1330,30 @@ static FuncDesc* ParseFuncDecl (void)
/* Check for several special parameter lists */ /* Check for several special parameter lists */
if (CurTok.Tok == TOK_RPAREN) { if (CurTok.Tok == TOK_RPAREN) {
/* Parameter list is empty */ /* Parameter list is empty (K&R-style) */
F->Flags |= (FD_EMPTY | FD_VARIADIC); F->Flags |= FD_EMPTY;
} else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) { } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
/* Parameter list declared as void */ /* Parameter list declared as void */
NextToken (); NextToken ();
F->Flags |= FD_VOID_PARAM; F->Flags |= FD_VOID_PARAM;
} else if (CurTok.Tok == TOK_IDENT && } else if (CurTok.Tok == TOK_IDENT &&
(NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) { (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
/* If the identifier is a typedef, we have a new style parameter list, /* If the identifier is a typedef, we have a new-style parameter list;
** if it's some other identifier, it's an old style parameter list. ** if it's some other identifier, it's an old-style parameter list.
*/ */
Sym = FindSym (CurTok.Ident); Sym = FindSym (CurTok.Ident);
if (Sym == 0 || !SymIsTypeDef (Sym)) { if (Sym == 0 || !SymIsTypeDef (Sym)) {
/* Old style (K&R) function. */ /* Old-style (K&R) function. */
F->Flags |= FD_OLDSTYLE; F->Flags |= FD_OLDSTYLE;
} }
} }
/* Parse params */ /* Parse params */
if ((F->Flags & FD_OLDSTYLE) == 0) { if ((F->Flags & FD_OLDSTYLE) == 0) {
/* New-style function */
/* New style function */
ParseAnsiParamList (F); ParseAnsiParamList (F);
} else { } else {
/* Old style function */ /* Old-style function */
ParseOldStyleParamList (F); ParseOldStyleParamList (F);
} }

View file

@ -359,8 +359,8 @@ static unsigned FunctionParamList (FuncDesc* Func, int IsFastcall)
CHECK ((Param->Flags & SC_PARAM) != 0); CHECK ((Param->Flags & SC_PARAM) != 0);
} }
} else if (!Ellipsis) { } else if (!Ellipsis) {
/* Too many arguments. Do we have an open param list? */ /* Too many arguments. Do we have an open or empty param. list? */
if ((Func->Flags & FD_VARIADIC) == 0) { if ((Func->Flags & (FD_VARIADIC | FD_EMPTY)) == 0) {
/* End of param list reached, no ellipsis */ /* End of param list reached, no ellipsis */
Error ("Too many arguments in function call"); Error ("Too many arguments in function call");
} }
@ -401,8 +401,9 @@ static unsigned FunctionParamList (FuncDesc* Func, int IsFastcall)
Flags |= TypeOf (Expr.Type); Flags |= TypeOf (Expr.Type);
/* If this is a fastcall function, don't push the last argument */ /* If this is a fastcall function, don't push the last argument */
if (ParamCount != Func->ParamCount || !IsFastcall) { if ((CurTok.Tok == TOK_COMMA && NextTok.Tok != TOK_RPAREN) || !IsFastcall) {
unsigned ArgSize = sizeofarg (Flags); unsigned ArgSize = sizeofarg (Flags);
if (FrameSize > 0) { if (FrameSize > 0) {
/* We have the space already allocated, store in the frame. /* We have the space already allocated, store in the frame.
** Because of invalid type conversions (that have produced an ** Because of invalid type conversions (that have produced an
@ -472,8 +473,14 @@ static void FunctionCall (ExprDesc* Expr)
/* Handle function pointers transparently */ /* Handle function pointers transparently */
IsFuncPtr = IsTypeFuncPtr (Expr->Type); IsFuncPtr = IsTypeFuncPtr (Expr->Type);
if (IsFuncPtr) { if (IsFuncPtr) {
/* Check whether it's a fastcall function that has parameters */ /* Check whether it's a fastcall function that has parameters.
IsFastcall = (Func->Flags & FD_VARIADIC) == 0 && Func->ParamCount > 0 && ** Note: if a function is forward-declared in the old K & R style, then
** it may be called with any number of arguments, even though its
** parameter count is zero. Handle K & R functions as though there are
** parameters.
*/
IsFastcall = (Func->Flags & FD_VARIADIC) == 0 &&
(Func->ParamCount > 0 || (Func->Flags & FD_EMPTY)) &&
(AutoCDecl ? (AutoCDecl ?
IsQualFastcall (Expr->Type + 1) : IsQualFastcall (Expr->Type + 1) :
!IsQualCDecl (Expr->Type + 1)); !IsQualCDecl (Expr->Type + 1));

View file

@ -60,16 +60,6 @@ $(WORKDIR)/%.ref: %.c | $(WORKDIR)
$(DIFF): ../bdiff.c | $(WORKDIR) $(DIFF): ../bdiff.c | $(WORKDIR)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
# Some files have "K & R"-style syntax. Therefore, some forward
# function-declarations don't match the later function definitions.
# Those programs fail when fastcall is used; but, the cdecl calling convention
# tolerates those conflicts. Therefore, make their functions default to cdecl.
#
$(WORKDIR)/init.%.prg \
$(WORKDIR)/switch.%.prg \
$(WORKDIR)/yacc.%.prg \
$(WORKDIR)/yaccdbg.%.prg: CC65FLAGS += -Wc --all-cdecl
# "yaccdbg.c" includes "yacc.c". # "yaccdbg.c" includes "yacc.c".
# yaccdbg's built files must depend on both of them. # yaccdbg's built files must depend on both of them.
# #

View file

@ -8,31 +8,31 @@
/* /*
* Sample OTCC C example. You can uncomment the first line and install * Sample OTCC C example. You can uncomment the first line and install
* otcc in /usr/local/bin to make otcc scripts ! * otcc in /usr/local/bin to make otcc scripts !
*/ */
/* Any preprocessor directive except #define are ignored. We put this /* Any preprocessor directive except #define are ignored. We put this
include so that a standard C compiler can compile this code too. */ include so that a standard C compiler can compile this code too. */
#include <stdio.h> #include <stdio.h>
#include <limits.h>
/* defines are handled, but macro arguments cannot be given. No /* defines are handled, but macro arguments cannot be given. No
recursive defines are tolerated */ recursive defines are tolerated */
#define DEFAULT_BASE 10 #define DEFAULT_BASE 10
#ifdef NO_IMPLICIT_FUNC_PROTOTYPES #ifdef NO_IMPLICIT_FUNC_PROTOTYPES
help(char *name); void help(char *name);
#endif #endif
/* /*
* Only old style K&R prototypes are parsed. Only int arguments are * Only old-style K&R prototypes are parsed. Only int arguments are
* allowed (implicit types). * allowed (implicit types).
* *
* By benchmarking the execution time of this function (for example * By benchmarking the execution time of this function (for example
* for fib(35)), you'll notice that OTCC is quite fast because it * for fib(35)), you'll notice that OTCC is quite fast because it
* generates native i386 machine code. * generates native i386 machine code.
*/ */
fib(n) int fib(n)
int n;
{ {
printf("[fib(%d)]", n); printf("[fib(%d)]", n);
if (n <= 2) if (n <= 2)
@ -42,12 +42,14 @@ fib(n)
} }
/* Identifiers are parsed the same way as C: begins with letter or /* Identifiers are parsed the same way as C: begins with letter or
'_', and then letters, '_' or digits */ '_', and then letters, '_', or digits. */
long fact(n) long fact(n)
int n;
{ {
/* local variables can be declared. Only 'int' type is supported */ /* local variables can be declared. Only 'int' type is supported */
int i; int i;
long r; long r;
r = 1; r = 1;
/* 'while' and 'for' loops are supported */ /* 'while' and 'for' loops are supported */
for(i=2;i<=n;i++) for(i=2;i<=n;i++)
@ -56,13 +58,15 @@ long fact(n)
} }
/* Well, we could use printf, but it would be too easy */ /* Well, we could use printf, but it would be too easy */
print_num(long n,int b) void print_num(n, b)
long n; int b;
{ {
char *tab, *p, c; char *tab, *p, c;
/* Numbers can be entered in decimal, hexadecimal ('0x' prefix) and
octal ('0' prefix) */ /* Numbers can be entered in decimal, hexadecimal ('0x' prefix), and
/* more complex programs use malloc */ octal ('0' prefix). */
tab = malloc(0x100); /* More complex programs use malloc(). */
tab = malloc(0x100);
p = tab; p = tab;
while (1) { while (1) {
c = n % b; c = n % b;
@ -80,29 +84,30 @@ print_num(long n,int b)
} }
while (p != tab) { while (p != tab) {
p--; p--;
printf("%c", *(char *)p); printf("%c", *p);
} }
free(tab); free(tab);
} }
/* 'main' takes standard 'argc' and 'argv' parameters */ /* 'main' takes standard 'argc' and 'argv' parameters */
mymain(int argc,char **argv) int mymain(argc, argv)
int argc; char **argv;
{ {
/* no local name space is supported, but local variables ARE /* No local name space is supported, but local variables ARE
supported. As long as you do not use a globally defined supported. As long as you do not use a globally defined
variable name as local variable (which is a bad habbit), you variable name as a local variable (which is a bad habit), you
won't have any problem */ won't have any problems. */
int s, n, f, base; size_t s, f;
int n, base;
/* && and || operator have the same semantics as C (left to right /* && and || operator have the same semantics as C (left to right
evaluation and early exit) */ evaluation and early exit) */
if (argc != 2 && argc != 3) { if (argc != 2 && argc != 3) {
/* '*' operator is supported with explicit casting to 'int *', /* '*' operator is supported with explicit casting to 'int *',
'char *' or 'int (*)()' (function pointer). Of course, 'int' 'char *', or 'int (*)()' (function pointer). Of course, 'int'
are supposed to be used as pointers too. */ are supposed to be used as pointers, too. */
s = *(int *)argv; s = *(size_t *)argv;
help(s); help((char *)s);
return 1; return 1;
} }
/* Any libc function can be used because OTCC uses dynamic linking */ /* Any libc function can be used because OTCC uses dynamic linking */
@ -125,15 +130,15 @@ mymain(int argc,char **argv)
printf("Overflow"); printf("Overflow");
} else { } else {
/* why not using a function pointer ? */ /* why not using a function pointer ? */
f = &fact; f = (size_t)&fact;
print_num((*(long (*)(int))f)(n), base); print_num((*(long (*)())f)(n), base);
} }
printf("\n"); printf("\n");
return 0; return 0;
} }
/* functions can be used before being defined */ /* functions can be used before being defined */
help(char *name) void help(char *name)
{ {
printf("usage: %s n [base]\n", name); printf("usage: %s n [base]\n", name);
printf("Compute fib(n) and fact(n) and output the result in base 'base'\n"); printf("Compute fib(n) and fact(n) and output the result in base 'base'\n");
@ -142,9 +147,9 @@ help(char *name)
int main(void) int main(void)
{ {
char *argv[3]; char *argv[3];
argv[0]="";
argv[0]="otccex";
argv[1]="10"; /* n */ argv[1]="10"; /* n */
argv[2]="8"; /* base */ argv[2]="8"; /* base */
mymain(3, argv); return mymain(3, argv);
return 0; }
}

View file

@ -44,16 +44,6 @@ all: $(TESTS)
$(WORKDIR): $(WORKDIR):
$(call MKDIR,$(WORKDIR)) $(call MKDIR,$(WORKDIR))
# Some files have "K & R"-style syntax. Therefore, some forward
# function-declarations don't match the later function definitions.
# Those programs fail when fastcall is used; but, the cdecl calling convention
# tolerates those conflicts. Therefore, make their functions default to cdecl.
#
$(WORKDIR)/cq4.%.prg \
$(WORKDIR)/cq71.%.prg \
$(WORKDIR)/cq81.%.prg \
$(WORKDIR)/cq84.%.prg: CC65FLAGS += -Wc --all-cdecl
define PRG_template define PRG_template
$(WORKDIR)/%.$1.$2.prg: %.c | $(WORKDIR) $(WORKDIR)/%.$1.$2.prg: %.c | $(WORKDIR)