|
|
|
|
 Compiling chkint |
 Speed improvement with compiled program |
Comp is the Seed7 compiler.
Comp is written in Seed7 and compiles Seed7 programs to C programs.
It uses the analyze phase of the interpreter to convert a program from Seed7 to call-code.
Call-code consists of values and function calls and is just handled in memory.
Then it uses the call-code to generate a corresponding C program.
This C program is compiled and linked with the Seed7 runtime library aterwards.
Usage
hi comp [ options ] source
Possible options are
- -O and -O2 tell the C compiler to optimize.
Reflection
The Seed7 reflection provides access to the internal data structures of the interpreter.
Specially the call-code of a program can be accessed with the reflection.
This makes it suitable for compiling seed7.
There are several types on which the reflection is based:
- program
- Describes a program and is the entry point to the reflection for the compiler.
- reference
- Reference to an object (plain old data types count also as object here).
- ref_list
- List of references objects.
- type
- Describes a type (the types of the compiled program have their own type namespace).
The definitions for reference, ref_list and
type are in the
"seed7_05.s7i" library.
The advanced features of the reflection and the definition for the type program
can be found in the
"progs.s7i" library.
Optimisations
The Seed7 compiler does several optimisations (without using the '-O' option):
- For certain constant values some function calls are replaced by corresponding calls of special case functions:
- The string comparisons = and <>
(primitive actions 'str_eq' and 'str_ne') are simplified when
one or both parameters are constant strings.
- The string indexing like stri[num]
(primitive action 'str_idx') is simplified when
the string or the index are constant.
- The array indexing like anArray[num]
(primitive action 'arr_idx') is simplified when
the array or the index are constant.
- Searches and splits with string constant of length 1 are replaced by
equivalent functions which use a character instead:
| Function call |
replaced by |
C function |
replacement C function |
| pos(stri, "a") |
pos(stri, 'a') |
'strPos' |
'strChPos' |
| rpos(stri, "a") |
rpos(stri, 'a') |
'strRpos' |
'strRChPos' |
| pos(stri, "a", start) |
pos(stri, 'a', start) |
'strIpos' |
'strChIpos' |
| split(stri, "a") |
split(stri, 'a') |
'strSplit' |
'strChSplit' |
- Initialisations of string variables are optimized when an empty string
or a string with length 1 is used:
| Seed7 variable declaration |
C declaration |
C initialisation |
replacement C initialisation |
| var string: myStri is ""; |
stritype o_123/*myStri*/; |
o_123/*stri*/=strCreate(""); |
o_123/*stri*/=strEmpty(); /* "" */ |
| var string: myStri is "a"; |
stritype o_123/*myStri*/; |
o_123/*stri*/=strCreate("a"); |
o_123/*stri*/=chrStr('a'); /* "a" */ |
- BigInteger operations are optimized when a cheaper function can be used:
| Original expression |
Optimized expression |
| num * 0_ |
0 |
| num * 1_ |
num |
| num * 2_ |
num << 1 |
| num * 4_ |
num << 2 |
| num * 8_ |
num << 3 |
| 0_ * num |
0 |
| 1_ * num |
num |
| 2_ * num |
num << 1 |
| 4_ * num |
num << 2 |
| 8_ * num |
num << 3 |
| num ** (-1) |
raise NUMERIC_ERROR |
| num ** 0_ |
1 |
| num ** 1_ |
num |
| num ** 2_ |
num * num |
| 1_ ** num |
raise NUMERIC_ERROR for num<0 or 1_ |
| 2_ ** num |
1 << num |
| 4_ ** num |
1 << 2 * num |
| 8_ ** num |
1 << 3 * num |
| num mdiv -1 |
-num |
| num mdiv 0 |
raise NUMERIC_ERROR |
| num mdiv 1 |
num |
| num mdiv 2 |
num >> 1 |
| num mdiv 4 |
num >> 2 |
- A temporary expression which would be freed after the return from a function can be used
by special case functions. That way it is not necessary to free the temporary value afterwards:
- When the first parameter of 'strConcat' is a temporary value
'strConcatTemp' is used instead. The 'strConcatTemp' function
resizes the temorary and returns it after the second parameter has been concatenated.
- When the parameter of 'strLow' is a temporary value
'strLowTemp' is used instead. The 'strLowTemp' function
returns the parameter as result of the conversion to lower case.
- When the parameter of 'strUp' is a temporary value
'strUpTemp' is used instead. The 'strUpTemp' function
returns the parameter as result of the conversion to upper case.
|
|