home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / pascal / tpas2msc.arc / HOWTO.DOC next >
Text File  |  1987-12-01  |  15KB  |  469 lines

  1.               GLOCKENSPIEL TURBO PASCAL(TM)-TO-QUICKC(TM) TRANSLATOR
  2.                                   VERSION 1.0
  3.                                    HOWTO.DOC
  4.  
  5.  
  6. The Glockenspiel Turbo Pascal-to-QuickC Translator automatically converts Turbo
  7. Pascal source programs to C source programs.  Turbo Pascal routines are
  8. converted to equivalent C routines, which are defined in the T2C.LIB library.
  9. (These C routines are described in the RUNTIME.DOC file on this disk).
  10.  
  11.  
  12. System Requirements
  13. ===================
  14. To run the translator, you must have the following:
  15.  
  16. o    Microsoft QuickC Compiler, Version 1.0 or later, or Microsoft C Compiler,
  17.      Version 5.0 or later
  18.  
  19. o    Microsoft Overlay Linker, Version 3.61 or later
  20.  
  21. o    A syntactically correct Turbo Pascal input file (that is, a Turbo Pascal
  22.      file that can be compiled and run successfully)
  23.  
  24.  
  25. Installation
  26. ============
  27. Before you run the Turbo-to-C Translator, the Microsoft C Compiler must be
  28. installed as described in the User's Guide.  The translator files must be in the
  29. current directory or in the path given by the appropriate environment
  30. variables, as shown below:
  31.  
  32.      Files          Variable
  33.      -----          --------
  34.      *.EXE          PATH
  35.      *.BAT          PATH
  36.      *.H            INCLUDE
  37.      T2C.LIB        LIB
  38.  
  39.  
  40. The Conversion Process
  41. ======================
  42. This section gives instructions for running the Turbo-Pascal-to-QuickC
  43. Translator and describes how the translator works.
  44.  
  45. Running the Translator
  46. ---------------------
  47. To run the translator make sure the T2C.BAT file is in your path.  Then type
  48.  
  49.      t2c <pasorig>
  50.  
  51. and press ENTER.  In this command, <pasorig> is the name of your Pascal source
  52. file WITHOUT THE .PAS EXTENSION.  The translator creates a C source file named
  53. <pasorig>.C.  It also creates several intermediate files, which are described
  54. under "How Conversions Are Performed" below.
  55.  
  56. To compile and link the new C source file, type
  57.  
  58.      qcomp <pasorig>.C
  59.  
  60. and press ENTER.  Note that, to maintain pointer compatibility between
  61. the Turbo Pascal input and C output programs, the qcomp command compiles
  62. the C output program using the large memory model (4-byte data and code
  63. pointers).
  64.  
  65.  
  66. How Conversions Are Performed
  67. -----------------------------
  68. The conversion process is performed in two separate stages, or "passes":
  69.  
  70. 1.   The Pascal source file is processed.  Nested procedure are unnested, and
  71.      WITH statements are expanded, in the Pascal source file.  If the Pascal
  72.      source contains include directives, the files named in the include
  73.      directives are also processed.
  74.  
  75.      The T2CP1.EXE program performs pass 1.  To run T2CP1.EXE separately, enter
  76.      a command of the following form:
  77.  
  78.           T2CP1 <pasorig> [<pasmod>]
  79.  
  80.      In this command, <pasorig> is the original Pascal source file, and
  81.      <pasmod> is the modified Pascal file.  If you do not give <pasmod>, the
  82.      modified code is written to standard output.
  83.  
  84.      Processed include files are written to a file with the same name as the
  85.      original include file but with "2" appended to the extension.  For
  86.      example, if an include file named INCL is processed, the processed file is
  87.      named INCL.2; if an include file named FILE.INC is processed, the
  88.      processed file is named FILE.IN2.  An include directive for the processed
  89.      file is placed in <pasfile2>.
  90.  
  91.      Other changes performed during pass 1 include:
  92.  
  93.      o    Replacement of the control-character notation "^G" with the
  94.           equivalent "#7"
  95.  
  96.      o    Conversion of multidimensional array elements from the form
  97.  
  98.                array1[x,y]
  99.  
  100.           to the form
  101.  
  102.                array1[x][y]
  103.  
  104. 2.   The Pascal code generated in Pass 1 is converted to C code.
  105.  
  106.      The T2CP2.EXE program performs pass 2.  You can invoke T2CP2.EXE
  107.      separately with a command of the following form:
  108.  
  109.           T2CP2 <pasmod> [<cfile>]
  110.  
  111.      In this command, <pasmod> is the output file from pass 1, and <cfile> is
  112.      the resulting C source file.  If you do not give <cfile>, the C code is
  113.      written to standard output.
  114.  
  115.      As in pass 1, processed include files are written to a file with the same
  116.      name as the original include files but with extensions of "TI".  For
  117.      example, if an include file named INCL is processed, the processed file is
  118.      named INCL.TI; if an include file named FILE.IN2 is processed, the
  119.      processed file is named FILE.TI.  An include directive for the processed
  120.      file is placed in <cfile>.
  121.  
  122.      During pass 2, nested procedures in the Pascal source program are assigned
  123.      unique names in the C program.  For example, if the Pascal program
  124.      contains
  125.  
  126.           procedure proc1;
  127.                procedure proc2;
  128.                     procedure proc3
  129.  
  130.      the corresponding C functions are named proc1, proc1_proc2, and
  131.      proc1_proc2_proc3.  This conversion prevents problems in programs where
  132.      the same function name is used for separate, unrelated nested functions.
  133.  
  134.  
  135.  
  136. Problems and Work-Arounds
  137. =========================
  138. This section describes situations in which you may have to modify a Turbo
  139. Pascal source file or the output file from translator pass 1 to make a
  140. successful conversion.
  141.  
  142.  
  143. Set Declarations
  144. -----------------
  145. Every declaration of a Pascal set (variable or constant) is converted to a call
  146. to the C function T2CSOP_tsmake (for "Turbo set make"). Because T2CSOP_tsmake
  147. calls malloc, repeated creation of sets in the Pascal program will slow the
  148. resulting C program.  In some cases, the C program may abort with the message
  149. "error allocating memory when creating a set".
  150.  
  151. This problem occurs most frequently in the C code generated from loops like the
  152. following:
  153.  
  154.      for i := 1 to BIGNUM
  155.      do
  156.      begin
  157.           read(c);
  158.           if c in ['y', 'n']
  159.      .
  160.      .
  161.      .
  162.      end;
  163.  
  164. To speed up the C program, move the set declaration outside the loop in the
  165. Pascal source program, as shown below:
  166.  
  167.      CONST temp = ['y','n'];
  168.      .
  169.      .
  170.      .
  171.      for i := 1 to BIGNUM
  172.      do
  173.      begin
  174.           read(c);
  175.           if c in temp
  176.      .
  177.      .
  178.      .
  179.      end;
  180.  
  181.  
  182. Nested Procedures
  183. -----------------
  184. Nested procedures in the Pascal source program may cause forward declaration
  185. problems.  For example, consider the following nested procedures:
  186.  
  187.      procedure A;
  188.      var ax:integer;
  189.           procedure B;
  190.           var bx:array[1..10] of integer;
  191.                procedure C;
  192.                begin
  193.                .
  194.                .
  195.                .
  196.                end;
  197.           begin
  198.           .
  199.           .
  200.           .
  201.           end;
  202.      begin
  203.      .
  204.      .
  205.      .
  206.      end;
  207.  
  208. Because Pascal and C have different scoping rules, pass 1 of the translator
  209. changes the parameter list of the nested procedures to include variables from
  210. the enclosing scope(s). (Note that the variable's name is always preserved.)
  211. Pass 1 also unnests procedures by defining the innermost procedure first,
  212. followed by each outer procedure, as illustrated below:
  213.  
  214.      procedure C (var ax:integer; var bx:array[1..10] of integer);
  215.      begin
  216.      .
  217.      .
  218.      .
  219.      end;
  220.  
  221.      procedure B (var ax:integer);
  222.      var bx:array[1..10] of integer;
  223.      begin
  224.      .
  225.      .
  226.      .
  227.  
  228. Because of these changes, you may need to modify the Pascal source file in the
  229. following cases:
  230.  
  231. 1.   If a structured type declaration appears in a formal parameter list.
  232.  
  233.      Pass 2 does not accept structured type declarations in a formal parameter
  234.      list.  As a result, pass 1 generates the following error message if it
  235.      finds a variable of this type in the parameter list of a nested procedure
  236.      (for example, when if finds var bx in procedure C above):
  237.  
  238.           [warning] Proc/Fn C needs a type definition for var parameter bx
  239.  
  240.      In this case, insert a separate type declaration in an enclosing block and
  241.      modify the parameter list accordingly.  In the example above, change the
  242.      parameter list as follows:
  243.  
  244.           type arrint = array[1..10] of integer;
  245.           procedure C (var ax:integer; var bx:arrint);
  246.  
  247. 2.   In a program (such as an expression evaluator) where a nested procedure
  248.      invokes one of the enclosing procedures.
  249.  
  250.      In this case, you must insert a forward reference before the calling
  251.      procedure in the output file from pass 1.  In the example above, if
  252.      procedure B calls procedure A, change the output file from pass 1 from
  253.  
  254.           procedure B (var ax:integer);
  255.           var bx:array[1..10] of integer;
  256.           begin
  257.           .
  258.           .
  259.           .
  260.  
  261.      to
  262.  
  263.           procedure A; forward;
  264.           procedure B (var ax:integer);
  265.           var bx:array[1..10] of integer;
  266.           begin
  267.           .
  268.           .
  269.           .
  270.  
  271. 3.   In a program where the parameter list of the called procedure is expanded
  272.      to include variables from the enclosing scope; for example, if procedure C
  273.      above calls procedure B.  Pass 2 reports this problem with the message
  274.  
  275.           (filename) line xxx near ")": Warning  Incorrect number of parameters
  276.  
  277.      at the line where the function/procedure is called.
  278.  
  279.      Besides inserting the forward declaration (described in case 2) in the
  280.      output file from pass 1, you must change the called procedure's argument
  281.      list (both at the point of invocation and in the procedure definition).
  282.      Assuming that procedure C calls procedure B, the following code in the
  283.      pass 1 output file
  284.  
  285.           procedure C (var ax:integer; var bx:arrint);
  286.           begin
  287.           B;
  288.           .
  289.           .
  290.           .
  291.           end;
  292.  
  293.           procedure B (var ax:integer);
  294.           var bx:array[1..10] of integer;
  295.           begin
  296.           .
  297.           .
  298.           .
  299.           end;
  300.  
  301.      must be changed to
  302.  
  303.           procedure B (var ax:integer); forward;
  304.           procedure C (var ax:integer; var bx:arrint);
  305.           begin
  306.           B(ax);
  307.           .
  308.           .
  309.           .
  310.           end;
  311.  
  312.           procedure B;
  313.           var bx:array[1..10] of integer;
  314.           begin
  315.           .
  316.           .
  317.           .
  318.           end;
  319.  
  320.      Naturally, you must also change any forward declarations that appear in
  321.      the original Pascal program.
  322.  
  323.  
  324. Pascal Function and Procedure Names
  325. -----------------------------------
  326. Function and procedure names in the Pascal source program may conflict with C
  327. reserved words, library-function names, or macros defined in the C include
  328. files used by the translator.
  329.  
  330. The simplest way to avoid these conflicts is to change the name used in the
  331. Pascal source program.  Alternatively, you may be able to insert #undef
  332. directives in the C output file to prevent macro conflicts.
  333.  
  334.  
  335. Forward References of Types
  336. ---------------------------
  337. The translator assumes that Pascal pointers of undefined types are actually
  338. pointers to Pascal record types.  If this is not the case, you must modify the
  339. C output file accordingly.
  340.  
  341.  
  342. Pascal and C Strings
  343. --------------------
  344. Pascal and C strings have different formats.  In a Pascal string, the first
  345. character specifies the string length.  In a C string, the length depends on
  346. the position of the NULL (ASCII 0) character, which is the last character of
  347. the string.  The translator attempts to convert between the two formats if an
  348. index into a string is 0; however, the conversion can be made only if the index
  349. is a literal value.
  350.  
  351.  
  352.  
  353. Features That Work Differently in C Output Programs
  354. ===================================================
  355. The following Turbo Pascal constructs are translated to their closest C
  356. equivalents; however, they may work slightly differently in the C program
  357. output by the translator.
  358.  
  359.  
  360. Real Data Type
  361. --------------
  362. Real (6-byte) data types in Turbo Pascal programs are treated as double
  363. (8-byte) data types in the C output programs.
  364.  
  365.  
  366. Absolute Variable Type
  367. ----------------------
  368. The Turbo Pascal absolute variable type, which assigns a variable to a specific
  369. address, is converted to its closest C equivalent in the C output file;
  370. however, the results may differ slightly between the Turbo Pascal input
  371. and the C output files.
  372.  
  373.  
  374. Argument-Passing Conventions
  375. ----------------------------
  376. Note that, in Pascal, any parameter may be passed by value so that changes do
  377. not affect the memory location containing the value that is passed.  In C,
  378. array arguments are always passed by reference, and changes made to the array
  379. are retained on exit from the function.
  380.  
  381.  
  382. Random Function
  383. ---------------
  384. The Turbo Pascal Random function is converted to a C random-number-generation
  385. function, but the results from the C function will vary slightly from those
  386. given by the Random function.
  387.  
  388.  
  389. Argument-Passing Conventions
  390. ----------------------------
  391. Note that, in Pascal, any parameter may be passed by value so that changes do
  392. not affect the memory location containing the value that is passed.  In C,
  393. array arguments are always passed by reference, and changes made to the array
  394. are retained on exit from the function.
  395.  
  396.  
  397.  
  398. Unsupported Turbo Pascal Features
  399. =================================
  400. The translator does not handle the following constructs in Turbo Pascal source
  401. files.
  402.  
  403.  
  404. Case Statements
  405. ---------------
  406. The translator can convert case labels only if they are of standard types
  407. types such as Integer or Boolean.
  408.  
  409. The translator cannot convert Case statements used to define variants in
  410. record structures.
  411.  
  412.  
  413. Compiler Directives
  414. -------------------
  415. Turbo Pascal compiler directives other than $C and $I have no effect.
  416.  
  417.  
  418. External Procedure and Functions
  419. --------------------------------
  420. A Pascal source program containing declarations of external procedures and
  421. functions can be converted to C and compiled, but the resulting object file
  422. cannot be linked.  The translator places a declaration for the external function
  423. in the C program, but you must write the function itself.
  424.  
  425.  
  426. In-Line Machine Code
  427. --------------------
  428. A program using in-line machine code can be converted to C, but the resulting C
  429. program cannot be run unless you create an appropriate assembly-language
  430. function and call it from the C program.
  431.  
  432.  
  433. Overlays
  434. --------
  435. The overlay keyword in Turbo Pascal source programs is ignored.
  436.  
  437.  
  438. Interrupt-Service Routines
  439. --------------------------
  440. User-defined interrupt service routines in Turbo Pascal source programs are not
  441. supported.
  442.  
  443.  
  444. Mark and Release Procedures
  445. ---------------------------
  446. The Pascal Mark and Release procedures are not supported.
  447.  
  448.  
  449. Port Arrays
  450. -----------
  451. The Pascal Port and PortW arrays are ignored.
  452.  
  453.  
  454. Array and String Parameters and Return Values
  455. ---------------------------------------------
  456. Pascal functions may not return local arrays or strings. Similarly, procedures
  457. or functions with array parameters cannot be recursive.
  458.  
  459.  
  460. Turbo-BCD
  461. ---------
  462. C programs output by the translator cannot support the Turbo Pascal
  463. binary-coded-decimal (BCD) format for real numbers
  464.  
  465. ----------------------------------------------------------------------
  466. QuickC is a trademark of Microsoft Corporation.
  467.  
  468. Turbo Pascal is a trademark of Borland International, Inc..
  469.