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:
@@ -60,16 +60,6 @@ $(WORKDIR)/%.ref: %.c | $(WORKDIR)
|
||||
$(DIFF): ../bdiff.c | $(WORKDIR)
|
||||
$(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's built files must depend on both of them.
|
||||
#
|
||||
|
||||
@@ -8,31 +8,31 @@
|
||||
|
||||
/*
|
||||
* 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
|
||||
include so that a standard C compiler can compile this code too. */
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
/* defines are handled, but macro arguments cannot be given. No
|
||||
recursive defines are tolerated */
|
||||
#define DEFAULT_BASE 10
|
||||
|
||||
#ifdef NO_IMPLICIT_FUNC_PROTOTYPES
|
||||
help(char *name);
|
||||
void help(char *name);
|
||||
#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).
|
||||
*
|
||||
*
|
||||
* By benchmarking the execution time of this function (for example
|
||||
* 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);
|
||||
if (n <= 2)
|
||||
@@ -42,12 +42,14 @@ fib(n)
|
||||
}
|
||||
|
||||
/* 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)
|
||||
int n;
|
||||
{
|
||||
/* local variables can be declared. Only 'int' type is supported */
|
||||
int i;
|
||||
long r;
|
||||
|
||||
r = 1;
|
||||
/* 'while' and 'for' loops are supported */
|
||||
for(i=2;i<=n;i++)
|
||||
@@ -56,13 +58,15 @@ long fact(n)
|
||||
}
|
||||
|
||||
/* 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;
|
||||
/* Numbers can be entered in decimal, hexadecimal ('0x' prefix) and
|
||||
octal ('0' prefix) */
|
||||
/* more complex programs use malloc */
|
||||
tab = malloc(0x100);
|
||||
|
||||
/* Numbers can be entered in decimal, hexadecimal ('0x' prefix), and
|
||||
octal ('0' prefix). */
|
||||
/* More complex programs use malloc(). */
|
||||
tab = malloc(0x100);
|
||||
p = tab;
|
||||
while (1) {
|
||||
c = n % b;
|
||||
@@ -80,29 +84,30 @@ print_num(long n,int b)
|
||||
}
|
||||
while (p != tab) {
|
||||
p--;
|
||||
printf("%c", *(char *)p);
|
||||
printf("%c", *p);
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
|
||||
/* '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
|
||||
variable name as local variable (which is a bad habbit), you
|
||||
won't have any problem */
|
||||
int s, n, f, base;
|
||||
|
||||
|
||||
variable name as a local variable (which is a bad habit), you
|
||||
won't have any problems. */
|
||||
size_t s, f;
|
||||
int n, base;
|
||||
|
||||
/* && and || operator have the same semantics as C (left to right
|
||||
evaluation and early exit) */
|
||||
if (argc != 2 && argc != 3) {
|
||||
/* '*' operator is supported with explicit casting to 'int *',
|
||||
'char *' or 'int (*)()' (function pointer). Of course, 'int'
|
||||
are supposed to be used as pointers too. */
|
||||
s = *(int *)argv;
|
||||
help(s);
|
||||
'char *', or 'int (*)()' (function pointer). Of course, 'int'
|
||||
are supposed to be used as pointers, too. */
|
||||
s = *(size_t *)argv;
|
||||
help((char *)s);
|
||||
return 1;
|
||||
}
|
||||
/* Any libc function can be used because OTCC uses dynamic linking */
|
||||
@@ -125,15 +130,15 @@ mymain(int argc,char **argv)
|
||||
printf("Overflow");
|
||||
} else {
|
||||
/* why not using a function pointer ? */
|
||||
f = &fact;
|
||||
print_num((*(long (*)(int))f)(n), base);
|
||||
f = (size_t)&fact;
|
||||
print_num((*(long (*)())f)(n), base);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* functions can be used before being defined */
|
||||
help(char *name)
|
||||
void help(char *name)
|
||||
{
|
||||
printf("usage: %s n [base]\n", name);
|
||||
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)
|
||||
{
|
||||
char *argv[3];
|
||||
argv[0]="";
|
||||
|
||||
argv[0]="otccex";
|
||||
argv[1]="10"; /* n */
|
||||
argv[2]="8"; /* base */
|
||||
mymain(3, argv);
|
||||
return 0;
|
||||
}
|
||||
return mymain(3, argv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user