Contents of the TC2TP.DOC file
Prelim documentation for TC2TP
by Richard S. Sadowsky
August 10, 1988
Los Angeles, California
Released to the public domain
TC2TP is a utility that allows you to use routines written in Turbo C within
your Turbo Pascal programs via the $L option (link OBJ file). An explanation
of how this is done follows.
Please read the contents of this file before attempting to use this utility.
Copy TC2TP.EXE, C2P.EXE, and TC2TPINS.EXE to a directory which is on the
system path. A good choice is the directory where you keep your assembler.
You may copy the sample source code and test program anywhere you'd like.
Before running TC2TP, you must first run the TC2TPINS installation program
to tell TC2TP some important things about your setup.
TC2TPIns is an installation program for the TC2TP utility. It allows you to
specify the pathnames of various programs required by TC2TP. TC2TP and
TC2TPINS must both be in the default directory for this step (in other words
change to the directory you copied these files to). TC2TPINS will prompt
you for the following information:
TCC path :
This is the name and complete pathname of the Turbo C command line compiler,
This is the path passed to TCC with the -I option. Any pathname acceptable
to TCC is acceptable here, for example
This is the name and complete pathname of your assembler.
C2P name (and Path):
This is the name of the program to translate the C generated asm code into
TP4 ready asm code.
Now say 'Y' to save the changes to TC2TP.EXE.
TC2TP is easy to use once it's installed. Just type
where FileName is a C source file with or without extension. If no extension
is specified then .C is assumed. If you are successful, you will have two new
files on your disk, FileName.ASP and FileName.OBJ. The ASP file is the TP4
ready assembly language source, and the OBJ file is the TP4 ready object code
(use the $L compiler directive to link it in).
LET'S TRY IT:
Once the formality of telling TC2TP where to find various programs is behind
you, you're ready for action! For starters, lets take the CPASDEMO program
provided on the Turbo Pascal masters. This is a good example because it
declares several publics, as well as calls a routine implemented in
pascal. It is a poor example because it combines more than one routine into
an object file. This is undesirable because the Turbo Pascal smart linker
can only strip external code at the object file level (meaning if you use one
it links them all).
If all goes well, you will see a bunch of messages noting the success of
various parts of the process. If no errors occur, a file called
CPASDEMO.OBJ is waiting in the current directory. Now compile the
CPASDEMO.PAS program (with TP4) and run it! If you feel curious, take a peek
at the assembly language source code in the ASP file.
As of this writing, I haven't decided which other C sources (with
accompanying pascal samples) I'll pack up and send along (see LOCASE SAMPLE
Can not deal with C functions that rely on the C calling sequence, like
a function that takes a variable number of arguments. Current default is
medium memory model (small code, far data). This is correct for code declared
in Turbo Pascal for use within the current module and not defined in the
interface section of a unit. For reasons I'll get into later, you MUST not
use the keyword HUGE in the C source with this version of TC2TP. A command
line switch has been added to TC2TP that will generate FAR code by compiling
under large (FAR code, FAR data) memory model. Avoid calls to the TC library
routines, unless you plan on using TC2TP on them as well so they can be
included in a TP4 program (this would require that you have the TC Runtime
HOW and WHY?
I decided to tack on the answer to two important questions to this
file. The first question that comes to mind is "How is it done?"
The next logical question is "Why bother linking C code to Pascal programs?"
The how is actually quite simple. TCC, the Turbo C command line compiler,
is used to compile the C source to an ASM file (the -S option). TCC is
told not to generate underscores in identifiers, to use Pascal calling
conventions, and enable all optimizations for speed. The ASM file produced
by TCC is then processed by a program called C2P. This program reads the ASM
file line by line, and translates it into an ASM that will produce object
code acceptable to Turbo Pascal (version 4). This file is given the
same name as the C source with the extension .ASP (ASM for Pascal). This
ASP file and then assembled, which creates the TP4 ready object file. The
beauty of TC2TP is that automates all these steps so one command takes you
from C source to TP4 ready object code!
Why would anyone go through all this trouble? The answer is
OPTIMIZATION! Turbo C can produce some very efficient code. It uses
techniques like Register variables and jump optimization to produce code
that executes faster than a similar algorithm coded in Pascal. Also, since
you can step through arrays VERY QUICKLY in C by incrementing pointers,
routines can be coded that are inherently faster than Pascal's indexing
tactics. Another nicety is the ability to use Turbo C's inline ASM facility.
I personally much prefer coding in Turbo C's Inline ASM than in Turbo Pascal's
Inline machine code. It is also possible to take the MASM ready ASP file
and further optimize it by hand!
ANOTHER SAMPLE SESSION:
I have also included a small file called LOCASE.C. This little routine
written in Turbo C's inline ASM produces excellent code. It implements
a function like the Turbo Pascal Upcase command except that it returns the
character as lower case. Note: by default, Locase creates a NEAR function.
To create a FAR version of LOCASE, use the -L command line option. A test
program called TestLo.PAS is included to test the LoCase routine (this test
program expects LOCASE to be NEAR, the default).
when it finishes, the file LOCASE.OBJ will be in the default directory.
Now fire up Turbo Pascal, and compile and run the test program TestLo.PAS.
You have just successfully used a routine written in Turbo C in your
Turbo Pascal application!
COMMAND LINE OPTIONS:
TC2TP implements the following command line options. Command line options
must start with either of the characters / or -. Command line options
may be specified in any order (before or after the filename).
-N = No assembly (create an ASP file, but don't create OBJ)
-L = Large code (creates FAR routines)
-Ixxx = TCC include path (used just like the -I option of TCC)
-U = unit desired (not implemented yet)
Here's a sample command line:
TC2TP -L -N LOWLEVEL.C -Id:\c\inc
This would compile the file LOWLEVEL.C in large memory model (-L option),
with the Include path d:\c\inc passed to TCC (-I option). The C generated
ASM file would be processed to LOWLEVEL.ASP, but LOWLEVEL.ASP will not be
assembled (-N option).
FOR THE RECORD:
TC2TP, C2P, and TC2TPINS are all written in Turbo Pascal and Assembly
Requires Turbo Pascal 4.0, TC 1.0 or 1.5, and a MASM compatible assembler.
This product is provided as is.
Turbo Pascal and Turbo C are registered trademarks of Borland International.
Richard S. Sadowsky
Los Angeles, California