The small c compiler's specifities

1/ Introduction


You will ask me "why write an article about the small c compiler ?".
It's a compiler for the c language, so it is a c standard.
To this question, I answer no, :-(, because this compiler was developped during the early 80's, and the C norme as ANSI or POSIX wasn't created.
Further more, it was developped for a 8 bits machine. For technical reasons, the creators decided to write a compiler which was able to assembly and link a c source code almost standard.
Ok, this compiler doesn't able to compile current source, but if we are known of its specificities, it's easier to bypass this step.
That's I will try to explain in this short article.


2/ enter in the code


When you see the documentation of this compiler, you focus on the fact that only tree data types are available :
* First the pointer on file misses (FILE*), so how the compiler knows it's a file.
The pointers on file are replaced by the int.
So now it's easy to understand that if you need to open a file, you will use an integer variable as pointer of file.

* When you follow in the documentation you see a strange function declaration.
A function under small c, is declared as followed :
type function_name();
function_name(variables) type variable;

You need two lines. The first one is used by the compiler to know which data type returns the function.
The second line, to declare the variable name (under bracket), and their data types.
Now you will write :
type function_name(type variable);

So take care about it ;-).

* The pointers are recognized by small c, but only one and not the pointer of pointer as char **, int ** or double **.
It means you need to enter into a brainstorming when you really need such structures.

* The structures and unions are not available too.

* One more thing which is important, the tests "OR" or "AND" stranslated by a "||" and "&&" in the c standard, are not the same.
The small c compiler just needs one. If you want to test two blocks with a "OR" test, just write "&".

* The returns are sometimes complicated to debugg. I mean, when you return a pointer of caracter and you affect it, the compiler returns you an error (Variable must be a lvalue).
So it's better for the CPC and the compiler, to modify a pointer of caracter variable entered as argument.
And you return this variable without affect if.
For example :


* The small c compiler is sometimes in bad humor. It mean that it hates when an affectation is done in a variable's declaration.
It's better (for him), to declarate first the variable, and after, do an affectation.
For instance :

* another hint, is how to declare the function in a header file ?.
I don't know if it's the best, but the compiler doesn't shout me and I write fewer code (ok I'm lazzy).
So first, the declaration as type function(type variable); in a header file are not accepted by the compiler.
You just need to inform it what is th type return and the function name as :
type function();

A hint I saw in some sources, is togather all functions which return the type of data and to write as follow :
type1 function1(),
function2(),
function3();

Where function 1 2 and 3 return the same data type.

* to be sure that your functions will be seen by all the codes, force the header declaration by a little assembly code :
#asm
 GLOBAL QFUNCTION1
 GLOBAL QFUNCTION2
 GLOBAL QFUNCTION3
#endasm

It's a global function declaration.
The same declaration must be done for global variable. Use the same syntaxe even your variable is an integer, a string or a poitner ...
To understand, download this floppy image. Check the code of the f2.c file and f2.h file, in the declaration of the char* s.

Some limitations are known too, like the maximum of the line length which is 80 characters and the code length more or less 22 ko per file.

3/ solution


To avoid all those issues, I decided to write a perl script which try to correct them.
The current script corrects :

To use it under an Unix :
#> cat your_file.c | c2smallc.pl > your_csmall_file.c
and to generate the header file :
#> cat your_file.c | c2smallc.pl -g > your_csmall_header_file.h

So to conclude, good coding party ;-)

Sid