TugBoat - new compiler from a Clipper to C
What such TugBoat?
TugBoat - intellectual additional compiler from the language a Clipper into the language C.
TugBoat_55.zip (360K) |
http://www.geocities.com/cl2cru/faq.htm FAQ (beginning, Engl.) |
And now about it is more detailed and more really …
The additional compiler
From the very beginning for me the task consist in acceleration of execution of existing Clippers - programs. I shall be repeated, “existing”. That is if the developer used the library QWERTY_for_Clipper of the left developer with all source texts — he can speed up the application including speed up functions them of this library. If he uses the library ASDFG_for_Clipper of the right developer and source texts of this library no in limits of reach — well that, it is possible to speed up operation only of code, and the another's code should be used, "as is".
How it works? Well, I simply create C-code, and on the "C" language 85 % of a Clipper is written. Naturally, it can work together, if all is made correctly. The list of my defects look in “Bugs”. More in detail mechanism of operation read further.
Increase of speed
In the beginning there was an idea. “ If the interpreter of a Clipper is written mostly on C and fulfils in main decoding of the byte-code in calls appropriate C of functions, maybe, to try to compile the source texts on a Clipper to C - program, with direct calls of these functions? ”
The first variant TugBoat about it also did. C-code consisting entirely of calls of other functions was derivated. The speed has increased, but not it is a little - on tens percents. The lion's share of time was borrowed by runtime check of types.
Following step was detection and selection of a static type of variables. Especially of type “whole”, “ long whole ”, “logical”, “date”, the operations above which can be fulfilled without engaging the Clipper - computer. Here “pseudo-intelligence” in the compiler (see below) also has appeared
But all the same turning out C-code looked obscenely. A reason — bad practice N-level of addressing in the clippers arrays. But someone wrote, and to respond me! In the Clippers program
f [1] [2] [3] + = a [4] [5] [6]
Looks is almost acceptable. In C-program it looks simply awfully. Also it was necessary to add the classical global optimizer — with removal of the common subexpressions and cleaning of cycles upwards. As a rule, after pass of optimization C-code on 30 % becomes shorter (and a little bit faster). Now output code looks better:
The code without the optimizer |
The code with the optimizer |
CLC_putaa0any (f (, 1), (2)); _putln (CLC_puta1long (3)); CLC_putaa0any (a (, 4), (5)); _putln (CLC_puta1long (6)); _tos -; LONGCAST (_tos) + = LONGCAST (_tos-1); CLC_putaa0any (f (, 1), (2)); CLC_popa2 ((3)); |
{ ITEM _loc1 = CLC_putaa0 (a (, 1), (2), -1); CLC_putaa0any (f (, 4), (5)); CLC_popa0long (_loc1 (, 3), ( CLC_puta0long (_loc1 (, 3)) + CLC_puta1long (6))); } _pull (1); |
More in detail about the mechanism of operation look in “Theory” and “Histories”
“Pseudo-intelligence” or detection of semantic errors
The task of calculation of static types for the language with dynamic types, certainly, is difficult. Nevertheless, for the language LISP she for a long time is solved. Remember the Japanese project of the COMPUTER of fifth generation? Then.
I have made it. For example, you will see something like:
Warning FILE.prg 123: Local variable I type SHORTINT in function …
Here “pseudo-intelligence” in the compiler — simply inside him also has appeared there is an error of calculation of a static type in that place, where in the program there would be a violation of runtime check of types (for example, 1 + “1”, or 2 > nil — all this expressions with violations of types). For example:
> > local i: = 0
> > i: = str (i) + i
> > ----------- the message ------------
> > Error a. PRG 4: Type clash, SHORTINT to STRING
In detail to describe here, as the static types are calculated, I can not (it is three passes of the compiler, that is words it will be very long and consequently boringly). The god will give, the commercially acceptable variant of translation TugBoat on “ Open Source ” will turn up, then see. Well and if such variant will not be — well that, it is necessary more - less in detail to describe. But later. That is I do not know when.
New possibilities
It is possible to describe types of parameters of the function, for example:
external function qwerty(__integer,__integer|__real,__array)__nil
Syntax of the description EXTERNAL of functions:
external function name_function(arg1, arg2...) returned
Where "arg" or "returned" is
[Type] | [the type] |...
That is it is possible to write, for example:
external function val (__ string) __ INTEGER | __ REAL
Syntax of the description STATIC of functions:
__ static function name_functiuon (arg1, arg2...) returned
Where "arg" or "returned" same
Warning: all descriptions of functions should be in the beginning of the file
It is possible to describe obviously types of variables, for example
function qwerty (a, b, c)
local __ shortint i
if a == int (b)
for i = 1 to len (ñ)
c [i]: = str (i)
next
endif
return nil
Syntax of variables with types:
STATIC [round] a name: = expression, name: = expression...
LOCAL [round] a name: = expression, name: = expression...
Where [round] there is one of variants:
__ shortint that is " short int " in "C"
__ integer that is " long int " in "C"
__ logical that is "int" in "C"
__ real that is "ITEM" in "C"
__ string that is "ITEM" in "C"
__ array that is "ITEM" in "C"
__ nil that is "ITEM" in "C"
__ date that is "long" in "C"
The theory
Certainly, you know it, but, nevertheless, I shall repeat the initial data
These procedures (called by the interpreter) are written on C. There is a valid question
Can be if call these procedures directly from C-program, the speed will increase?
And from here history TugBoat … starts
Buglist
The variables with names conterminous to keywords are unavailable
If: = 1
" Protected mode " from under Blinker is extremely astable, ExoSpace - it is very good
Best regards,
http://www.geocities.com/cl2cru/
Vladislav Bulatov