📄 p2c.man
字号:
Modula-2. Based on Wirth's.I Programming in Modula-2,3rd edition. Proper setting of the.B Languageparameter is.I notoptional. Translation will be incomplete in most cases, but should begood enough to work with. Structure of local sub-modules is essentiallyignored; like-named identifiers may be confused. Type WORD is translatedas an integer, but type ADDRESS is translated as char * or void *; thismay cause inconsistencies in the output code..IPModula-2 modules have two parts in separate files. Suppose these arecalled.I foo.def(definition part) and.I foo.mod(implementation part) for module.I foo.Then a pattern like.B %s.defmust be included in the.B ImportDirlist, and.B LibraryFilemust be changed to refer to.I system.m2instead of.I system.imp.To translate the definition part, give the command.IP\ \ \ \ \ p2c foo.def.IPto translate the definition part into files.I foo.hand.IR foo.c ;the latter will usually be empty. The command.IP\ \ \ \ \ p2c -s foo.def foo.mod.IPwill translate the implementation part into file.I foo.c..PPEven if all language features are supported for a dialect, somepredefined functions may be omitted. In these cases, the function callwill be translated literally into C with a warning. Some hand modificationmay be required..SH CONFIGURATION PARAMETERS.I P2cis highly configurable. The defaults are suitable for most applications,but customizing these parameters will help you get the best possibletranslation. Since the output of.I p2cis intended to be used as human-maintainable source code, there are manyparameters for describing the coding style and conventions you prefer.Others give hints about your program that help.I p2cto generate more correct, efficient, or readable code..PPThe.I p2crcfiles contain a list of parameters, one per line. The systemconfiguration file, which may be viewed using the.B \-ioption to.I p2c,serves as an example of the proper format. Parameter names arecase-insensitive. If a parameter name occurs exactly once in the system.I p2crc,this indicates that it must have a unique value and the last value given toit by theconfiguration files is used. Other parameters are written several timesin a row; these are lists to which each configuration line adds an entry..PPMany.I p2crcoptions take a numeric value of 0 or 1, roughly corresponding to"no" or "yes." Sometimes a blank value or the value "\fBdef\fR"corresponds to an intermediate "maybe" state. For example, the stylisticoption.B ExtraParensswitches between copious or minimal parentheses in expressions, withthe default being a nice compromise intended to be best for readerswith an average knowledge of C operator precedences..PPConfiguration options may also be embedded in the source file in theform of Pascal comments:.PP\ \ \ \ \ {ShortOpt=0} {AvoidName=fred}.br\ \ \ \ \ {FuncMacro slope(x,y)=atan2(y,x)*RadDeg}.PPdisables automatic short-circuiting of.B andand.B orexpressions, adds "\fIfred\fR" to the list of names to avoid using ingenerated C code, and defines a special translation for the Pascalprogram's.I slopefunction using the standard C.I atan2function and a constant.I RadDegpresumably defined in the program. Whitespace is generally not allowedin embedded parameters.The `=' sign is required for embedded parameters, though it is optional in.I p2crcfiles. Comments within embedded parameters are delimited by `##'.Numeric parameters may replace `=' with `+' or `-' to increaseor decrease the parameter; list-based parameters may use `-' to removea name from a list rather than adding it. Also, the parameter nameby itself in comment braces means to restore the parameter's valuethat was current before the last change:.PP\ \ \ \ \ {VarFiles=0 ## Pass FILE *'s params by value even if VAR}.br.I \ \ \ \ \ some declarations.br\ \ \ \ \ {VarFiles ## Back to original FILE * passing}.PPcauses the parameter.B VarFilesto have the value 0 for those few declarations, without affecting theparameter's value elsewhere in the file..PPIf an embedded parameter appears in an include file or in interface textfor a module,the effect of the assignment normally carries over to any programs thatincluded that file. If the parameter name is preceded by a `*', thenthe assignment is automatically undone after the source file that containsit ends:.PP\ \ \ \ \ {IncludeFrom strings=<p2c/strings.h>}.br\ \ \ \ \ {*ExportSymbol=pascal_%s}.br\ \ \ \ \ module strings;.PPwill record the location of the.I stringsmodule's include file for the rest of the translation, but the assignmentof.B ExportSymbolpertains only to the module itself..PPFor the complete list of.I p2crcparameters, run.I p2cwith the.B \-ioption. Here are some additional comments on selected parameters:.TP 15.B ImportAllBecause Turbo Pascal only allows one unit per source file,.I p2cnormally stops reading past the word.I implementationin a file being scanned for interface text. But HP Pascal allows severalmodules per file and so this would not be safe to do. The.B ImportAlloption lets you override the default behavior for your Pascal dialect..TP.B AnsiCThis parameter selects which dialect of C to use. If 1, all conventionsof ANSI C such as prototypes,.B void *pointers, etc. are used. If 0, only strict K&R (first edition) C is used.The default is to use "traditional UNIX C," which includes.B enumand.B voidbut not.B void *or prototypes. Once again there are a number of other parameters whichmay be used to control the individual features if just setting.B AnsiCis not enough..TP.B C++This tells.I p2cto use a number of language extensions present in C++: Specifically,it enables the "//" format for comments, use of "anonymous unions" forvariant records, use of declarations within the function body, use ofreferences for VAR parameters, and use of "new" and "delete" insteadof "malloc" and "free"..I P2cwill check for collisions with C++ reserved words unless youexplicitly set the C++ option to zero..TP.B TurboObjects.I P2crecognizes two major dialects of object-oriented Pascal. TurboPascal 6.0.B objecttypes translate fairly directly into C++ classes. In Apple'sObject Pascal, the.B objecttype has similar syntax but represents a handle (a double pointer)to an object rather than an object itself. The.B TurboObjectsoption (whose default is determined by the.B Languagesetting) says whether objects should be direct or indirectthrough pointers..RI ( P2cuses pointers instead of handles;.I p2cis most often used to make programs more portable, and few systemsexcept the Mac use handles in this way.).TP.B UseVExternMany non-UNIX linkers prohibit variables from being defined (not declared)by more than one source file. One module must declare, e.g., "int foo;",and all others must declare "extern int foo;"..I P2caccomplishes this by declaring public variables "\fBvextern\fR"in header files, and arranging for the macro.B vexternto expand to.B externor to nothing when appropriate. If you set.BR UseVExtern =0.I p2cwill instead declare variables in a simpler way that works only onUNIX-style linkers..TP.B UseAnyptrMacrosCertain C reserved words have meanings which may vary from one Cimplementation to another..I P2cuses special capitalized names for these words; these names aredefined as macros in the file.I p2c.hwhich all translated programs include. You can set.BR UseAnyptrMacros =0to disable the use of these macros. Note that the functions of many ofthese macros can also be had directly using other parameters; for example,.B UseConstsallows you to specify whether your target language recognizes the word.B constin constant declarations. The default is to use the.B Constmacro instead, so that your code will be portable to either kind ofimplementation..IP.B Signedexpands to the reserved word.B signedif that word is available, otherwise it is given a null definition.Similarly,.B Constexpands to.B constif that feature is available. The words.B Volatileand.B Registerare also defined in.I p2c.h,although.I p2cdoes not use them at present. The word.B Charexpands to.B charby default, but might need to be redefined to.B signed charor.B unsigned charin a particular implementation. This is used for the Pascal charactertype; lowercase.B charis used when the desired meaning is "byte," not "character.".IPThe word.B Staticalways expands to.B staticby default. This is used in situations where a function or variable isdeclared static to make it local to the source file; lowercase.B staticis used for static local variables. Thus you can redefine.B Staticto be null if you want to force private names to be public for purposesof debugging..IPThe word.B Voidexpands to.B voidin all cases; it is used when declaring a function with no return value.The word.B Anyptris a typedef for.B void *or.B char *as necessary; it represents a generic pointer..TP.B UsePPMacrosThe.I p2c.hheader also declares two macros for function prototyping,.BR PP (x)and.BR PV ().These macros are used as follows:.IP\ \ \ \ \ Void foo PP( (int x, int y, Char *z) );.br\ \ \ \ \ Char *bar PV( );.IPIf prototypes are available, these macros will expand to.IP\ \ \ \ \ Void foo (int x, int y, Char *z);.br\ \ \ \ \ Char *bar (void);.IPbut if only old-style declarations are supported, you instead get.IP\ \ \ \ \ Void foo ();.br\ \ \ \ \ Char *bar ();.IPBy default,.I p2cuses these macros for all function declarations, but function.I definitionsare written in old-style C. The.B UsePPMacrosparameter can be set to 0 to disable all use of.B PPand.BR PV ,or it can be set to 1 to use the macros even when defining a function.(This is accomplished by preceding each old-style definition with a.BR PP -styledeclaration.) If you know your code will always becompiled on systems that support prototyping, it is prettier to set.BR Prototypes =1or simply.BR AnsiC =1to get true function prototypes..TP.B EatNotesNotes and warning messages containing any of these strings as sub-stringsare not emitted. Each type of message includes an identifier like.BR [145] ;you can add this identifier to the.B EatNoteslist to suppress that message. Another useful form is to use a variablename or other identifier to suppress warnings about that variable. Thestrings are a space-separated list, and thus may not contain embedded spaces.To suppress notes around a section of code, use, e.g.,.I {EatNotes+[145]}and.I {EatNotes-[145]}.Most notes are generated during parsing, but to suppress those generatedduring output the string may need to remain in the list far beyond the pointwhere it appears to be generated. Use the string "1" or "0" to disable orenable all notes, respectively..TP.B ExpandIncludesThe default action is to expand Pascal include files in-line. Thismay not be desirable if include files are being used to simulate modules.With.BR ExpandIncludes =0,.I p2cattempts to convert include files containing only whole procedures andglobal declarations into analogous C include files. This may not alwayswork, though; if you get error messages, don't use this option. Bycombining this option with.BR StaticFunctions =0,then doing some fairly minor editing on the result, you can converta pseudo-modular Pascal program into a truly modular collection of Csource files..TP.B ElimDeadCodeSome transformations that.I p2cdoes on the program may result in unreachable or "dead" code. Bydefault.I p2cremoves such code, but sometimes it removes more than it should.If you have "if false" segments which you wish to retain in C, youmay have to set.BR ElimDeadCode =0..TP.B AnalyzeFlowBy default.I p2cdoes some basic dataflow analysis on the program in an attempt tolocate code that can be simplified due to knowledge about thepossible values of certain variables. For example, a Pascal.B rewritestatement must translate to an.B ifthat either calls.B fopenon a formerly closed file variable, or.B freopenon an already-open file. If flow analysis can prove that thefile was open or closed upon entry to the statement, a muchcleaner translation is possible..IPIt is possible that flow analysis will make simplificationsthat are undesirable or buggy. If this occurs, you can set.B AnalyzeFlowto 0 to disable this feature..TP.B SkipIndicesNormally Pascal arrays not based at zero are "shifted" down for C,preserving the total size of the array. A Pascal array a[2..10] istranslated to a C array a[9] with references like "a[i]" changed to"a[i-2]" everywhere. If.B SkipIndicesis set to a value of 2 or higher, this array would instead be translatedto a[11] with the first two elements never used. This arrangement maygenerate incorrect code, though, for tricky source programs..TP.B FoldConstantsPascal non-structured constants generally translate to.BR #define 'sin C. Set this to 1 to have constants instantiated directly into the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -