You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/general.md
+157Lines changed: 157 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -721,6 +721,163 @@ To compile a program to start at address 65536 (at the 64K boundary), do:
721
721
flexspin -2 -H 0x10000 -E fibo.bas
722
722
```
723
723
724
+
## Preprocessor
725
+
726
+
All of the languages have a preprocessor, which is (mostly) compatible with the standard C preprocessor. Internally flexspin has a different preprocessor for C (based on Kiyoshi Matsui's `mcpp`) and for the other languages. The Spin and BASIC preprocessors ignore the case of defines by default, although this may be changed with a `#pragma`.
727
+
728
+
### Directives
729
+
730
+
#### \#DEFINE
731
+
```
732
+
#define FOO hello
733
+
```
734
+
Defines a new macro `FOO` with the value `hello`. Whenever the symbol `FOO` appears in the text, the preprocessor will substitute `hello`.
735
+
736
+
Note that unlike the C preprocessor, this one cannot accept arguments in macros. Only simple defines are permitted.
737
+
738
+
If no value is given, e.g.
739
+
```
740
+
#define BAR
741
+
```
742
+
then the symbol is defined as the string `1`.
743
+
744
+
#### \#IFDEF
745
+
746
+
Introduces a conditional compilation section, which is only compiled if
747
+
the symbol after the `#ifdef` is in fact defined. For example:
748
+
```
749
+
#ifdef __P2__
750
+
'' propeller 2 code goes here
751
+
#else
752
+
'' propeller 1 code goes here
753
+
#endif
754
+
```
755
+
756
+
#### \#IFNDEF
757
+
758
+
Introduces a conditional compilation section, which is only compiled if
759
+
the symbol after the `#ifndef` is *not* defined. For example:
760
+
```
761
+
#ifndef __P2__
762
+
#error this code only works on Propeller 2
763
+
#endif
764
+
```
765
+
766
+
#### \#ELSE
767
+
768
+
Switches the meaning of conditional compilation.
769
+
770
+
#### \#ELSEIFDEF
771
+
772
+
A combination of `#else` and `#ifdef`.
773
+
774
+
#### \#ELSEIFNDEF
775
+
776
+
A combination of `#else` and `#ifndef`.
777
+
778
+
#### \#ERROR
779
+
780
+
Prints an error message. Mainly used in conditional compilation to report an unhandled condition. Everything after the `#error` directive is printed.
781
+
782
+
#### \#INCLUDE
783
+
784
+
Includes a file.
785
+
786
+
#### \#WARN
787
+
788
+
Prints a warning message.
789
+
790
+
#### \#PRAGMA
791
+
792
+
Control the preprocessor. At the moment, only a few pragmas are implemented, namely `ignore_case`, `keep_case`, and `exportdef`.
793
+
794
+
```
795
+
#pragma ignore_case
796
+
```
797
+
Makes the preprocessor, like the rest of the compiler, case insensitive. This is the default for Spin2 and BASIC. This pragma is ignored in C, which is always case sensitive.
798
+
799
+
```
800
+
#pragma keep_case
801
+
```
802
+
803
+
Forces the preprocessor to be case sensitive. This is the default for C, and cannot be changed there.
804
+
805
+
```
806
+
#define NAME whatever
807
+
#pragma exportdef NAME
808
+
```
809
+
exports the definition of the macro `NAME` to other files. Normally a preprocessor macro only takes effect in the single source file in which it was defined. `#pragma exportdef` applied to the macro causes it to be exported to the global namespace, so that it will be in effect in all subsequent files, including objects.
810
+
811
+
Note that macros exported to other files by `#pragma exportdef` have lower priority than macros defined on the command line, that is, `#pragma exportdef NAME` has lower priority than `-DNAME=x`.
812
+
813
+
Example of `exportdef` use:
814
+
815
+
Top level file main.spin2:
816
+
```
817
+
#define MEMDRIVER "driver2.spin2"
818
+
#pragma exportdef MEMDRIVER
819
+
820
+
' instantiate flash.spin2 with the
821
+
' default memory driver overridden by
822
+
' MEMDRIVER
823
+
824
+
OBJ s: "flash.spin2"
825
+
```
826
+
827
+
Subobject obj.spin2:
828
+
```
829
+
#ifndef MEMDRIVER
830
+
#define MEMDRIVER "default_driver"
831
+
#endif
832
+
833
+
OBJ d : MEMDRIVER
834
+
```
835
+
836
+
Note that if there are multiple uses of `#pragma exportdef` on the same symbol, only the first one will actually be used -- that is, a macro may be exported from a file only once.
837
+
838
+
#### \#UNDEF
839
+
840
+
Removes the definition of a symbol, e.g. to undefine `FOO` do:
841
+
```
842
+
#undef FOO
843
+
```
844
+
845
+
### Predefined Symbols
846
+
847
+
There are several predefined symbols:
848
+
849
+
Symbol | When Defined
850
+
-----------------|-------------
851
+
`__propeller__` | always defined to 1 (for P1) or 2 (for P2)
852
+
`__P1__` | if compiling for Propeller 1
853
+
`__propeller2__` | if compiling for Propeller 2
854
+
`__P2__` | if compiling for Propeller 2
855
+
`__FLEXSPIN__` | if the `flexspin` front end is used
856
+
`__FLEX_MAJOR__` | always defined to the flexspin/flexcc major version number (e.g. "5" in 5.9.26)
857
+
`__FLEX_MINOR__` | always defined to the flexspin/flexcc minor version number (e.g. "9" in 5.9.26)
858
+
`__FLEX_REV__` | always defined to the flexspin/flexcc revision number (e.g. "26" in 5.9.26)
859
+
`__SPINCVT__` | always defined
860
+
`__SPIN2PASM__` | if --asm is given (PASM output) (always defined by flexspin)
861
+
`__SPIN2CPP__` | if C++ or C is being output (never in flexspin)
862
+
`__HAVE_FCACHE__` | if the FCACHE optimization is enabled
863
+
`__cplusplus` | if C++ is being output (never in flexspin)
864
+
`__DATE__` | a string containing the date when compilation was begun
865
+
`__FILE__` | a string giving the current file being compiled
866
+
`__LINE__` | the current source line number
867
+
`__TIME__` | a string containing the time when compilation was begun
868
+
`__VERSION__` | a string containing the full version of flexspin in use
869
+
`__DEBUG__` | if debugging is enabled (-g or -gbrk given)
870
+
871
+
A predefined symbol is also generated for type of output being created:
872
+
873
+
Symbol | When Defined
874
+
-------------------------|-------------
875
+
`__OUTPUT_ASM__` | if PASM code is being generated (the default)
876
+
`__OUTPUT_BYTECODE__` | if bytecode is being generated
877
+
`__OUTPUT_C__` | if C code is being generated
878
+
`__OUTPUT_CPP__` | if C++ code is being generated
879
+
880
+
724
881
## Special defines
725
882
726
883
There are some special defines that may be made with `-DDEF=xxx`. The special features of these defines are only activated when given on the command line; that is, they do not have any special effects when defined with `#define`.
0 commit comments