fastspin C compiler
David Betz
Posts: 14,519
I just started trying to compile my embedded BASIC interpreter for the P2 and ran into this error when compiling one of the source files:
The line in question is the reference to GetLineHandler in the System structure definition below. VMVALUE is just an integer typedef. Am I using a feature that is not yet supported by the fastspin C compiler?
db_system.h(23) error: syntax error, unexpected identifier `GetLineHandler'
The line in question is the reference to GetLineHandler in the System structure definition below. VMVALUE is just an integer typedef. Am I using a feature that is not yet supported by the fastspin C compiler?
/* line input handler */
typedef int GetLineHandler(void *cookie, char *buf, int len, VMVALUE *pLineNumber);
/* system context */
typedef struct {
jmp_buf errorTarget; /* error target */
GetLineHandler *getLine; /* function to get a line of input */
void *getLineCookie; /* cookie for the getLine function */
int lineNumber; /* current line number */
uint8_t *freeSpace; /* base of free space */
uint8_t *freeMark; /* top of permanently allocated storage */
uint8_t *freeNext; /* next free space available */
uint8_t *freeTop; /* top of free space */
char lineBuf[MAXLINE]; /* current input line */
char *linePtr; /* pointer to the current character */
} System;
In case you want to try this yourself I've attached the sources. My first attempt at compiling it was using the command:
fastspin -2 -D PROPELLER_GCC *.cThat would eventually generate errors because it is including duplicates of the OS interface functions but I thought it was an easy way to start.

Comments
Thanks for the bug report!
db_compiler.h(93) error: syntax error, unexpected type name `Block', expecting identifier or __using or '{'From this code:/* block structure */ typedef struct Block Block; struct Block { BlockType type; union { struct { int nxt; int end; } IfBlock; struct { int end; } ElseBlock; struct { int nxt; int end; } ForBlock; struct { int nxt; int end; } DoBlock; } u; };I'm not sure why I defined it this way since there is no reference to the Block typedef in the structure but I do this elsewhere where there are recursive references.Edit: Actually, the more I think of it this *is* one of the big advantages of C++. It's a pain having to split a declaration into two pieces and type the structure tag three times. C++ was certainly an improvement here! I could have left out the typedef line entirely.
typedef struct { ... } Block;and then you won't need a separate typedef line./* label structure */ typedef struct Label Label; struct Label { Label *next; int placed; int fixups; int offset; char name[1]; };So I sometimes just use that pattern for all structure definitions.Davids-Mac-mini:ebasic3 dbetz$ fastspin -2 -D PROPELLER_GCC *.c Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc. Version 3.9.20-beta-7ed0e6c6 Compiled on: Feb 14 2019 db_compiler.c warning: : Preprocessor warnings: /Users/dbetz/work/ebasic3/db_compiler.c:1: warning: Converted [CR+LF] to [LF] /* db_compiler.c - a simple basic compiler db_edit.c db_expr.c db_generate.c db_image.c db_scan.cAnd it hung after that. There are more files in the directory. Any idea why it stopped? The full set of files is:NextToken(ParseContext *C) and NextToken(System *sys), I think.
typedef struct { int a,b,c; } MyStruct; void test(void) { MyStruct buf[4]; MyStruct *p; p = buf - 1; }I looked at the assembly, and the compiler puts literal strings in a section called "rdata". So it appears that this is what's causing the warning.
void main(void) { char *ptr = "test"; const char *ptr1 = "test1"; char *ptr2 = (const char *)"test2"; "test3"[2] = 0; ptr1[2] = 0; }Yes, the C standard does say that string literals are "const", and gcc used to warn about this but i guess so many programs pass literal strings to "char *" that they disabled that warning (even if -Wall is given you still have to explicitly give -Wwrite-strings to get it). I should probably take the same route, or at least provide a compiler option to disable those warnings.
Note that even without the warning strings really are consts, e.g. the program below will segfault on Linux when compiled with gcc or clang:
#include <stdio.h> void printit(char *msg) { msg[0] = 'j'; // causes a segmentation fault printf("message=%s\n", msg); } int main() { printit("hello, world"); return 0; }https://stackoverflow.com/questions/2245664/what-is-the-type-of-string-literals-in-c-and-c
Yes, looks like automatic casting of arrays to pointers is broken. I'll look into it. Thanks for the bug report!
Huh. I guess my memory is going as I'm getting older
String *AddString(ParseContext *c, const char *value); String *AddString(ParseContext *c, char *value) { ... }static struct { char *name; int value; } keywords[] = { { "foo", 1 }, { "bar", 2 }, }; char *test(int i) { return keywords[1].name; }The odd thing is that the error goes away if I remove "static" from the declaration of the keywords array.void main(void) { int i; for (i = 0; i < sizeof(long); ++i) ; }fastspin -o ebasic3 -2 -D PROPELLER_GCC ebasic.c db_compiler.c db_edit.c db_expr.c db_generate.c db_image.c db_scan.c db_statement.c db_symbols.c db_system.c db_vmdebug.c db_vmint.c editbuf.c osint_propgcc.c Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc. Version 3.9.20 Compiled on: Feb 16 2019 ebasic.c warning: : Preprocessor warnings: /Users/dbetz/work/ebasic3/ebasic.c:1: warning: Converted [CR+LF] to [LF] #include <stdio.h> db_compiler.c db_edit.c db_expr.c db_generate.c db_image.c db_scan.c db_statement.c db_symbols.c db_system.c db_vmdebug.c db_vmint.c editbuf.c osint_propgcc.c /Users/dbetz/work/ebasic3/db_edit.c(215) warning: different types in arms of ? /Users/dbetz/work/ebasic3/db_vmdebug.c(87) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(94) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(102) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(108) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(113) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(120) warning: signed/unsigned comparison may not work properly /Users/dbetz/work/ebasic3/db_vmdebug.c(126) warning: signed/unsigned comparison may not work properly memcpy.c memmove.c strcpy.c strcmp.c strchr.c memset.c strlen.c atoi.c ctype.c isxdigit.c tolower.c make: *** [ebasic3] Segmentation fault: 11For now feel free to comment out that WARNING line in frontends/basiclang.c (another thing I have to get around to, the type checking code is shared between BASIC and C and should go in its own file).
Thanks,
Eric