prog.tex 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \documentclass{report}
  22. %
  23. % Preamble
  24. %
  25. \usepackage{ifthen}
  26. \usepackage{xspace}
  27. \usepackage{a4}
  28. \usepackage{makeidx}
  29. \usepackage{html}
  30. \usepackage{htmllist}
  31. \usepackage{fancyhdr}
  32. \usepackage{epsfig}
  33. \usepackage{multicol}
  34. \usepackage{fpc}
  35. \latex{%
  36. \ifpdf
  37. \usepackage[pdftex,bookmarks=true]{hyperref}
  38. \pdfcompresslevel=9
  39. \pdfpagewidth=210mm
  40. \pdfpageheight=297mm
  41. \pdfinfo{/Author(Michael Van Canneyt)
  42. /Title(Programmers' Guide)
  43. /Subject(Free Pascal Programmers' guide)
  44. /Keywords(Free Pascal)
  45. }
  46. \fi
  47. }
  48. %
  49. \html{\input{fpc-html.tex}}
  50. %
  51. % Settings
  52. %
  53. \pagestyle{fancy}
  54. \makeindex
  55. %
  56. % Start of document.
  57. %
  58. \begin{document}
  59. \title{Free Pascal \\ Programmers' manual}
  60. \docdescription{Programmers' manual for \fpc, version \fpcversion}
  61. \docversion{1.5}
  62. \input{date.inc}
  63. \author{Micha\"el Van Canneyt}
  64. \maketitle
  65. \tableofcontents
  66. \newpage
  67. \listoftables
  68. \newpage
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  70. % Introduction
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. \section*{About this document}
  73. This is the programmer's manual for \fpc.
  74. It describes some of the peculiarities of the \fpc compiler, and provides a
  75. glimpse of how the compiler generates its code, and how you can change the
  76. generated code. It will not, however, provide you with a detailed account of
  77. the inner workings of the compiler, nor will it tell you how to use the
  78. compiler (described in the \userref). It also will not describe the inner
  79. workings of the Run-Time Library (RTL). The best way to learn about the way
  80. the RTL is implemented is from the sources themselves.
  81. The things described here are useful if you want to do things which need
  82. greater flexibility than the standard Pascal language constructs.
  83. (described in the \refref)
  84. Since the compiler is continuously under development, this document may get
  85. out of date. Wherever possible, the information in this manual will be
  86. updated. If you find something which isn't correct, or you think something
  87. is missing, feel free to contact me\footnote{at
  88. \var{[email protected]}}.
  89. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  90. % Compiler switches
  91. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  92. \chapter{Compiler directives}
  93. \label{ch:CompSwitch}
  94. \fpc supports compiler directives in your source file. They are not the same
  95. as Turbo Pascal directives, although some are supported for compatibility.
  96. There is a distinction between local and global directives; local directives
  97. take effect from the moment they are encountered, global directives have an
  98. effect on all of the compiled code.
  99. Many switches have a long form also. If they do, then the name of the
  100. long form is given also. For long switches, the + or - character to switch
  101. the option on or off, may be replaced by \var{ON} or \var{OFF} keywords.
  102. Thus \verb|{$I+}| is equivalent to \verb|{$IOCHECKS ON}| or
  103. \verb|{$IOCHECKS +}| and
  104. \verb|{$C-}| is equivalent to \verb|{$ASSERTIONS OFF}| or
  105. \verb|{$ASSERTIONS -}|
  106. The long forms of the switches are the same as their Delphi
  107. counterparts.
  108. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  109. % Local switches
  110. \section{Local directives}
  111. \label{se:LocalSwitch}
  112. Local directives have no command-line counterpart. They influence the
  113. compiler's behaviour from the moment they're encountered until the moment
  114. another switch annihilates their behaviour, or the end of the unit or
  115. program is reached.
  116. \subsection{\var{\$A} or \var{\$ALIGN}: Align Data}
  117. This switch is recognized for Turbo Pascal Compatibility, but is not
  118. yet implemented. The alignment of data will be different in any case, since
  119. \fpc is a 32-bit compiler.
  120. \subsection{\var{\$ASMMODE} : Assembler mode}
  121. \label{se:AsmReader}
  122. The \var{\{\$ASMMODE XXX} directive informs the compiler what kind of assembler
  123. it can expect in an \var{asm} block. The \var{XXX} should be replaced by one
  124. of the following:
  125. \begin{description}
  126. \item [att\ ] Indicates that \var{asm} blocks contain AT\&T syntax assembler.
  127. \item [intel\ ] Indicates that \var{asm} blocks contain Intel syntax
  128. assembler.
  129. \item [direct\ ] Tells the compiler that asm blocks should be copied
  130. directly to the assembler file.
  131. \end{description}
  132. These switches are local, and retain their value to the end of the unit that
  133. is compiled, unless they are replaced by another directive of the same type.
  134. The command-line switch that corresponds to this switch is \var{-R}.
  135. \subsection{\var{\$B} or \var{\$BOOLEVAL}: Complete boolean evaluation}
  136. This switch is understood by the \fpc compiler, but is ignored. The compiler
  137. always uses shortcut evaluation, i.e. the evaluation of a boolean expression
  138. is stopped once the result of the total exression is known with certainty.
  139. So, in the following example, the function \var{Bofu}, which has a boolean
  140. result, will never get called.
  141. \begin{verbatim}
  142. If False and Bofu then
  143. ...
  144. \end{verbatim}
  145. \subsection{\var{\$C} or \var{\$ASSERTIONS} : Assertion support}
  146. This switch is recognised for Delphi compatibility only. Assertions are not
  147. yet supported by the compiler, but will be implemented in the future.
  148. \subsection{\var{\$DEFINE} : Define a symbol}
  149. The directive
  150. \begin{verbatim}
  151. {$DEFINE name}
  152. \end{verbatim}
  153. defines the symbol \var{name}. This symbol remains defined until the end of
  154. the current module, or until a \var{\$UNDEF name} directive is encountered.
  155. If \var{name} is already defined, this has no effect. \var{Name} is case
  156. insensitive.
  157. \subsection{\var{\$ELSE} : Switch conditional compilation}
  158. The \var{\{\$ELSE \}} switches between compiling and ignoting the source
  159. text delimited by the preceding \var{\{\$IFxxx\}} and following
  160. \var{\{\$ENDIF\}}. Any text after the \var{ELSE} keyword but before the
  161. brace is ignored:
  162. \begin{verbatim}
  163. {$ELSE some ignored text}
  164. \end{verbatim}
  165. is the same as
  166. \begin{verbatim}
  167. {$ELSE}
  168. \end{verbatim}
  169. This is useful for indication what switch is meant.
  170. \subsection{\var{\$ENDIF} : End conditional compilation}
  171. The \var{\{\$ENDIF\}} directive ends the conditional compilation initiated by the
  172. last \var{\{\$IFxxx\}} directive. Any text after the \var{ENDIF} keyword but
  173. before the closing brace is ignored:
  174. \begin{verbatim}
  175. {$ENDIF some ignored text}
  176. \end{verbatim}
  177. is the same as
  178. \begin{verbatim}
  179. {$ENDIF}
  180. \end{verbatim}
  181. This is useful for indication what switch is meant to be ended.
  182. \subsection{\var{\$ERROR} : Generate error message}
  183. The following code
  184. \begin{verbatim}
  185. {$ERROR This code is erroneous !}
  186. \end{verbatim}
  187. will display an error message when the compiler encounters it,
  188. and increase the error count of the compiler.
  189. The compiler will continue to compile, but no code will be emitted.
  190. \subsection{\var{\$F} : Far or near functions}
  191. This directive is recognized for compatibility with Turbo Pascal. Under the
  192. 32-bit programming model, the concept of near and far calls have no meaning,
  193. hence the directive is ignored. A warning is printed to the screen, telling
  194. you so.
  195. As an example, : the following piece of code :
  196. \begin{verbatim}
  197. {$F+}
  198. Procedure TestProc;
  199. begin
  200. Writeln ('Hello From TestProc');
  201. end;
  202. begin
  203. testProc
  204. end.
  205. \end{verbatim}
  206. Generates the following compiler output:
  207. \begin{verbatim}
  208. malpertuus: >pp -vw testf
  209. Compiler: ppc386
  210. Units are searched in: /home/michael;/usr/bin/;/usr/lib/ppc/0.9.1/linuxunits
  211. Target OS: Linux
  212. Compiling testf.pp
  213. testf.pp(1) Warning: illegal compiler switch
  214. 7739 kB free
  215. Calling assembler...
  216. Assembled...
  217. Calling linker...
  218. 12 lines compiled,
  219. 1.00000000000000E+0000
  220. \end{verbatim}
  221. You can see that the verbosity level was set to display warnings.
  222. If you declare a function as \var{Far} (this has the same effect as setting it
  223. between \var{\{\$F+\}...\{\$F-\}} directives), the compiler also generates a
  224. warning :
  225. \begin{verbatim}
  226. testf.pp(3) Warning: FAR ignored
  227. \end{verbatim}
  228. The same story is true for procedures declared as \var{Near}. The warning
  229. displayed in that case is:
  230. \begin{verbatim}
  231. testf.pp(3) Warning: NEAR ignored
  232. \end{verbatim}
  233. \subsection{\var{\$FATAL} : Generate fatal error message}
  234. The following code
  235. \begin{verbatim}
  236. {$FATAL This code is erroneous !}
  237. \end{verbatim}
  238. will display an error message when the compiler encounters it, and trigger
  239. and increase the error count of the compiler.
  240. The compiler will immediatly stop the compilation process.
  241. \subsection{\var{\$GOTO} : Support \var{Goto} and \var{Label}}
  242. If \var{\{\$GOTO ON\}} is specified, the compiler will support \var{Goto}
  243. statements and \var{Label} declarations. By default, \var{\$GOTO OFF} is
  244. assumed. This directive corresponds to the \var{-Sg} command-line option.
  245. As an example, the following code can be compiled:
  246. \begin{verbatim}
  247. {$GOTO ON}
  248. label Theend;
  249. begin
  250. If ParamCount=0 then
  251. GoTo TheEnd;
  252. Writeln ('You spcified command-line options');
  253. TheEnd:
  254. end.
  255. \end{verbatim}
  256. \subsection{\var{\$H} or \var{\$LONGSTRINGS} : Use AnsiStrings}
  257. If \var{\{\$LONGSTRINGS ON\}} is specified, the keyword \var{String} (no
  258. length specifier) will be treated as \var{AnsiString}, and the compiler
  259. will treat the corresponding varible as an ansistring, and will
  260. generate corresponding code.
  261. By default, the use of ansistrings is off, corresponding to \var{\{\$H-\}}.
  262. This feature is still experimental, and should be used with caution for the
  263. time being.
  264. \subsection{\var{\$HINT} : Generate hint message}
  265. If the generation of hints is turned on, through the \var{-vh} command-line
  266. option or the \var{\{\$HINTS ON\}} directive, then
  267. \begin{verbatim}
  268. {$Hint This code should be optimized }
  269. \end{verbatim}
  270. will display a hint message when the compiler encounters it.
  271. \subsection{\var{\$HINTS} : Emit hints}
  272. \var{\{\$HINTS ON\}} switches the generation of hints on.
  273. \var{\{\$HINTS OFF\}} switches the generation of hints off.
  274. Contrary to the command-line option \var{-vh} this is a local switch,
  275. this is useful for checking parts of your code.
  276. \subsection{\var{\$IF} : Start conditional compilation}
  277. The directive \var{\{\$IF expr\}} will continue the compilation
  278. if the boolean expression \var{expr} evaluates to \var{true}. If the
  279. compilation evaluates to false, then the source are skipped to the first
  280. \var{\{\$ELSE\}} or \var{\{\$ENDIF\}} directive.
  281. The compiler must be able to evaluate the expression at compile time.
  282. This means that you cannot use variables or constants that are defined in
  283. the source. Macros and symbols may be used, however.
  284. More information on this can be found in the section about
  285. conditionals.
  286. \subsection{\var{\$IFDEF} : Start conditional compilation}
  287. The \var{\{\$IFDEF name\}} will skip the compilation of the text that
  288. follows it if the symbol \var{name} is not defined. If it is defined, then
  289. compilation continues as if the directive wasn't there.
  290. \subsection{\var{\$IFNDEF} : Start conditional compilation}
  291. The \var{\{\$IFNDEF name\}} will skip the compilation of the text that
  292. follows it if the symbol \var{name} is defined. If it is not defined, then
  293. compilation continues as if the directive wasn't there.
  294. \subsection{\var{\$IFOPT} : Start conditional compilation}
  295. The \var{\{\$IFOPT switch\}} will compile the text that follows it if the
  296. switch \var{switch} is currently in the specified state.
  297. If it isn't in the specified state, then compilation continues after the
  298. corresponding \var{\{\$ENDIF\}} directive.
  299. As an example:
  300. \begin{verbatim}
  301. {$IFOPT M+}
  302. Writeln ('Compiled with type information');
  303. {$ENDIF}
  304. \end{verbatim}
  305. Will compile the writeln statement if generation of type information is on.
  306. \subsection{\var{\$INFO} : Generate info message}
  307. If the generation of info is turned on, through the \var{-vi} command-line
  308. option, then
  309. \begin{verbatim}
  310. {$INFO This was coded on a rainy day by Bugs Bunny }
  311. \end{verbatim}
  312. will display an info message when the compiler encounters it.
  313. \subsection{\var{\$INLINE} : Allow inline code.}
  314. The \var{\{\$INLINE ON\}} directive tells the compiler that the \var{Inline}
  315. procedure modifier should be allowed. Procedures that are declared inline
  316. are copied to the places where they are called. This has the effect that
  317. there is no actual procedure call, the code of the procedure iis just copied
  318. to where the procedure is needed. By default, \var{Inline} procedures are
  319. not allowed. You need to specify this directive if you want to use inlined
  320. code. This directive is equivalent to the command-line switch \var{-Si}.
  321. Inline code is NOT exported from a unit. This means that if you call an
  322. inline procedure from another unit, a normal procedure call will be
  323. performed. Only inside units, \var{Inline} procedures are really inline.
  324. \subsection{\var{\$I} or \var{\$IOCHECKS} : Input/Output checking}
  325. The \var{\{\$I-\}} or \var{\{\$IOCHECKS OFF\}} directive tells the compiler
  326. not to generate input/output checking code in your program. By default, the
  327. compiler does not generate this code, you must switch it on using the
  328. \var{-Ci} command-lne switch.
  329. If you compile using the \var{-Ci} compiler switch, the \fpc compiler inserts input/output
  330. checking code after every input/output call in your program. If an error
  331. occurred during input or output, then a run-time error will be generated.
  332. Use this switch if you wish to avoid this behavior.
  333. If you still want to check if something went wrong, you can use the
  334. \var{IOResult} function to see if everything went without problems.
  335. Conversely, \var{\{\$I+\}} will turn error-checking back on, until another
  336. directive is encountered which turns it off again.
  337. The most common use for this switch is to check if the opening of a file
  338. went without problems, as in the following piece of code:
  339. \begin{verbatim}
  340. ...
  341. assign (f,'file.txt');
  342. {$I-}
  343. rewrite (f);
  344. {$I+}
  345. if IOResult<>0 then
  346. begin
  347. Writeln ('Error opening file : "file.txt"');
  348. exit
  349. end;
  350. ...
  351. \end{verbatim}
  352. \subsection{\var{\$I} or \var{\$INCLUDE} : Include file }
  353. The \var{\{\$I filename\}} or \var{\{\$INCLUDE filename\}} directive
  354. tells the compiler to read further statements from the file \var{filename}.
  355. The statements read there will be inserted as if they occurred in the
  356. current file.
  357. The compiler will append the \file{.pp} extension to the file if you don't
  358. specify an extension yourself. Do not put the filename between quotes, as
  359. they will be regarded as part of the file's name.
  360. You can nest included files, but not infinitely deep. The number of files is
  361. restricted to the number of file descriptors available to the \fpc compiler.
  362. Contrary to Turbo Pascal, include files can cross blocks. I.e. you can start
  363. a block in one file (with a \var{Begin} keyword) and end it in another (with
  364. a \var{End} keyword). The smallest entity in an include file must be a token,
  365. i.e. an identifier, keyword or operator.
  366. The compiler will look for the file to include in the following places:
  367. \begin{enumerate}
  368. \item It will look in the path specified in the incude file name.
  369. \item It will look in the directory where the current source file is.
  370. \item it will look in all directories specified in the include file search
  371. path.
  372. \end{enumerate}
  373. You can add files to the include file search path with the \var{-I}
  374. command-line option.
  375. \subsection{\var{\$I} or \var{\$INCLUDE} : Include compiler info}
  376. In this form:
  377. \begin{verbatim}
  378. {$INCLUDE %xxx%}
  379. \end{verbatim}
  380. where \var{xxx} is one of \var{TIME}, \var{DATE}, \var{FPCVERSION} or
  381. \var{FPCTARGET}, will generate a macro with the value of these things.
  382. If \var{xxx} is none of the above, then it is assumed to be the value of
  383. an environment variable. It's value will be fetched, and inserted in the coe
  384. as if it were a srtring.
  385. For example, the following program
  386. \begin{verbatim}
  387. Program InfoDemo;
  388. Const User = {$I %USER%};
  389. joe begin
  390. Write ('This program was comilped at ',{$I %TIME%});
  391. Writeln (' on ',{$I %DATE%});
  392. Writeln ('By ',User);
  393. Writeln ('Compiler version : ',{$I %FPCVERSION%});
  394. Writeln ('Target CPU : ',{$I %FPCTARGET%});
  395. end.
  396. \end{verbatim}
  397. Creates the following output :
  398. \begin{verbatim}
  399. This program was comilped at 17:40:18 on 1998/09/09
  400. By michael
  401. Compiler version : 0.99.7
  402. Target CPU : i386
  403. \end{verbatim}
  404. % Assembler type
  405. \subsection{\var{\$I386\_XXX} : Specify assembler format}
  406. This switch selects the assembler reader. \var{\{\$I386\_XXX\}}
  407. has the same effect as \var{\{\$ASMMODE XXX\}}, \sees{AsmReader}
  408. \subsection{\var{\$L} or \var{\$LINK} : Link object file}
  409. The \var{\{\$L filename\}} or \var{\{\$LINK filename\}} directive
  410. tells the compiler that the file \file{filename} should be linked to
  411. your program.
  412. the compiler will look for this file in the following way:
  413. \begin{enumerate}
  414. \item It will look in the path specified in the object file name.
  415. \item It will look in the directory where the current source file is.
  416. \item it will look in all directories specified in the object file search path.
  417. \end{enumerate}
  418. You can add files to the object file search path with the \var{-Fo}
  419. option.
  420. On \linux systems, the name is case sensitive, and must be typed
  421. exactly as it appears on your system.
  422. {\em Remark :} Take care that the object file you're linking is in a
  423. format the linker understands. Which format this is, depends on the platform
  424. you're on. Typing \var{ld} on the command line gives a list of formats
  425. \var{ld} knows about.
  426. You can pass other files and options to the linker using the \var{-k}
  427. command-line option. You can specify more than one of these options, and
  428. they will be passed to the linker, in the order that you specified them on
  429. the command line, just before the names of the object files that must be
  430. linked.
  431. \subsection{\var{\$LINKLIB} : Link to a library}
  432. The \var{\{\$LINKLIB name\}} will link to a library \file{name}.
  433. This has the effect of passing \var{-lname} to the linker.
  434. As an example, consider the following unit:
  435. \begin{verbatim}
  436. unit getlen;
  437. interface
  438. {$LINKLIB c}
  439. function strlen (P : pchar) : longint;cdecl;
  440. implementation
  441. function strlen (P : pchar) : longint;cdecl;external;
  442. end.
  443. \end{verbatim}
  444. If one would issue the command the command
  445. \begin{verbatim}
  446. ppc386 foo.pp
  447. \end{verbatim}
  448. where foo.pp has the above unit in its \var{uses} clause,
  449. then the compiler would link your program to the c library, by passing the
  450. linker the \var{-lc} option.
  451. The same effect could be obtained by removing the linklib directive in the
  452. above unit, and specify \var{-k-lc} on the command-line:
  453. \begin{verbatim}
  454. ppc386 -k-lc foo.pp
  455. \end{verbatim}
  456. \subsection{\var{\$M} or \var{\$TYPEINFO} : Generate type info}
  457. For classes that are compiled in the \var{\{\$M+ \}} or \var{\{\$TYPEINFO ON\}}
  458. state, the compiler will generate Run-Time Type Information (RTTI). All
  459. descendent objects of an object that was compiled in the \var{\{\$M+\}} state
  460. will get RTTI information too, as well as any published classes.
  461. By default, no Run-Time Type Information is generated. The \var{TPersistent}
  462. object that is present in the FCL (Free Component Library) is generated in
  463. the \var{\{\$M+\}} state. The generation of RTTI allows programmers to
  464. stream objects, and to access published properties of objects, without
  465. knowing the actual class of the object.
  466. The run-time type information is accessible through the \var{TypInfo} unit,
  467. which is part of the \fpc Run-Time Library.
  468. \subsection{\var{\$MACRO} : Allow use of macros.}
  469. In the \var{\{\$MACRO ON\}} state, the compiler allows you to use C-style
  470. (although not as elaborate) macros. Macros provide a means for simple text
  471. substitution. More information on using macros can be found in the
  472. \sees{Macros} section. This directive is equivalent to the command-line
  473. switch \var{-Sm}.
  474. \subsection{\var{\$MESSAGE} : Generate info message}
  475. If the generation of info is turned on, through the \var{-vi} command-line
  476. option, then
  477. \begin{verbatim}
  478. {$MESSAGE This was coded on a rainy day by Bugs Bunny }
  479. \end{verbatim}
  480. will display an info message when the compiler encounters it. The effect is
  481. the same as the \var{\{\$INFO\}} directive.
  482. \subsection{\var{\$MMX} : Intel MMX support}
  483. As of version 0.9.8, \fpc supports optimization for the \textbf{MMX} Intel
  484. processor (see also \ref{ch:MMXSupport}).
  485. This optimizes certain code parts for the \textbf{MMX} Intel
  486. processor, thus greatly improving speed. The speed is noticed mostly when
  487. moving large amounts of data. Things that change are
  488. \begin{itemize}
  489. \item Data with a size that is a multiple of 8 bytes is moved using the
  490. \var{movq} assembler instruction, which moves 8 bytes at a time
  491. \end{itemize}
  492. Remark that MMX support is NOT emulated on non-MMX systems, i.e. if
  493. the processor doesn't have the MMX extensions, you cannot use the MMX
  494. optimizations.
  495. When \textbf{MMX} support is on, you aren't allowed to do floating point
  496. arithmetic. You are allowed to move floating point data, but no arithmetic
  497. can be done. If you wish to do floating point math anyway, you must first
  498. switch of \textbf{MMX} support and clear the FPU using the \var{emms}
  499. function of the \file{cpu} unit.
  500. The following example will make this more clear:
  501. \begin{verbatim}
  502. Program MMXDemo;
  503. uses cpu;
  504. var
  505. d1 : double;
  506. a : array[0..10000] of double;
  507. i : longint;
  508. begin
  509. d1:=1.0;
  510. {$mmx+}
  511. { floating point data is used, but we do _no_ arithmetic }
  512. for i:=0 to 10000 do
  513. a[i]:=d2; { this is done with 64 bit moves }
  514. {$mmx-}
  515. emms; { clear fpu }
  516. { now we can do floating point arithmetic }
  517. ....
  518. end.
  519. \end{verbatim}
  520. See, however, the chapter on MMX (\ref{ch:MMXSupport}) for more information
  521. on this topic.
  522. \subsection{\var{\$NOTE} : Generate note message}
  523. If the generation of notes is turned on, through the \var{-vn} command-line
  524. option or the \var{\{\$NOTES ON\}} directive, then
  525. \begin{verbatim}
  526. {$NOTE Ask Santa Claus to look at this code }
  527. \end{verbatim}
  528. will display a note message when the compiler encounters it.
  529. \subsection{\var{\$NOTES} : Emit notes}
  530. \var{\{\$NOTES ON\}} switches the generation of notes on.
  531. \var{\{\$NOTES OFF\}} switches the generation of notes off.
  532. Contrary to the command-line option \var{-vn} this
  533. is a local switch, this is useful for checking parts of your code.
  534. \subsection{\var{\$OUTPUT\_FORMAT} : Specify the output format}
  535. \var{\{\$OUTPUT\_FORMAT format\}} has the same functionality as the \var{-A}
  536. command-line option : It tells the compiler what kind of object file must be
  537. generated. You can specify this switch \textbf{only} befor the \var{Program}
  538. or \var{Unit} clause in your source file. The different kinds of formats are
  539. shown in \seet{Formats}.
  540. \begin{FPCltable}{ll}{Formats generated by the x86 compiler}{Formats} \hline
  541. Switch value & Generated format \\ \hline
  542. att & AT\&T assembler file. \\
  543. o & Unix object file.\\
  544. obj & OMF file.\\
  545. wasm & assembler for the Watcom assembler. \\ \hline
  546. \end{FPCltable}
  547. \subsection{\var{\$P} or \var{\$OPENSTRINGS} : Use open strings}
  548. \subsection{\var{\$PACKENUM} : Minimum enumeration type size}
  549. This directive tells the compiler the minimum number of bytes it should
  550. use when storing enumerated types. It is of the following form:
  551. \begin{verbatim}
  552. {$PACKENUM xxx}
  553. {$MINENUMSIZE xxx}
  554. \end{verbatim}
  555. Where the form with \var{\$MINENUMSIZE} is for Delphi compatibility.
  556. var{xxx} can be one of \var{1,2} or \var{4}, or \var{NORMAL} or
  557. \var{DEFAULT}, corresponding to the default value of 4.
  558. As an alternative form one can use \var{\{\$Z1\}}, \var{\{\$Z2\}}
  559. \var{\{\$Z4\}}. Contrary to Delphi, the default size is 4 bytes
  560. (\var{\{\$Z4\}}).
  561. So the follwoing code
  562. \begin{verbatim}
  563. {$PACKENUM 1}
  564. Type
  565. Days = (monday, tuesday, wednesday, thursday, friday,
  566. saturday, sunday);
  567. \end{verbatim}
  568. will use 1 byte to store a variable of type \var{Days}, wheras it nomally
  569. would use 4 bytes. The above code is equivalent to
  570. \begin{verbatim}
  571. {$Z1}
  572. Type
  573. Days = (monday, tuesday, wednesday, thursday, friday,
  574. saturday, sunday);
  575. \end{verbatim}
  576. {\em Remark:}
  577. Sets are always put in 32 bit or 32 bytes, this cannot be changed
  578. \subsection{\var{\$PACKRECORDS} : Alignment of record elements}
  579. This directive controls the byte alignment of the elements in a record,
  580. object or class type definition.
  581. It is of the following form:
  582. \begin{verbatim}
  583. {$PACKRECORDS n}
  584. \end{verbatim}
  585. Where \var{n} is one of 1,2,4,16 or \var{NORMAL} or \var{DEFAULT}.
  586. This means that the elements of a record that have size greater than \var{n}
  587. will be aligned on \var{n} byte boundaries. Elements with size less than or
  588. equal to \var{n} will be aligned to a natural boundary, i.e. to a power of
  589. two that is equal to or larger than the element's size.
  590. The default alignment (which can be selected with \var{DEFAULT}) is 2,
  591. contrary to Turbo Pascal, where it is 1.
  592. More information on this and an exmple program can be found in the reference
  593. guide, in the section about record types.
  594. {\em Remark:}
  595. Sets are always put in 32 bit or 32 bytes, this cannot be changed
  596. \subsection{\var{\$Q} \var{\$OVERFLOWCHECKS}: Overflow checking}
  597. The \var{\{\$Q+\}} or \var{\{\$OVERFLOWCHECKS ON\}} directive turns on
  598. integer overflow checking. This means that the compiler inserts code
  599. to check for overflow when doing computations with integers.
  600. When an overflow occurs, the run-time library will print a message
  601. \var{Overflow at xxx}, and exit the program with exit code 215.
  602. \emph{ Remark: } Overflow checking behaviour is not the same as in
  603. Turbo Pascal since all arithmetic operations are done via 32-bit
  604. values. Furthermore, the Inc() and Dec() standard system procedures
  605. \emph{ are } checked for overflow in \fpc, while in Turbo Pascal they
  606. are not.
  607. Using the \var{\{\$Q-\}} switch switches off the overflow checking code
  608. generation.
  609. The generation of overflow checking code can also be controlled
  610. using the \var{-Co} command line compiler option (see \userref).
  611. \subsection{\var{\$R} or \var{\$RANGECHECKS} : Range checking}
  612. By default, the computer doesn't generate code to check the ranges of array
  613. indices, enumeration types, subrange types, etc. Specifying the
  614. \var{\{\$R+\}} switch tells the computer to generate code to check these
  615. indices. If, at run-time, an index or enumeration type is specified that is
  616. out of the declared range of the compiler, then a run-time error is
  617. generated, and the program exits with exit code 201.
  618. The \var{\{\$RANGECHECKS OFF\}} switch tells the compiler not to generate range checking
  619. code. This may result in faulty program behaviour, but no run-time errors
  620. will be generated.
  621. {\em Remark: } Range checking for sets and enumerations are not yet fully
  622. implemented.
  623. \subsection{\var{\$SATURATION} : Saturation operations}
  624. This works only on the intel compiler, and MMX support must be on
  625. (\var{\{\$MMX +\}}) for this to have any effect. See the section on
  626. saturation support (\sees{SaturationSupport}) for more information
  627. on the effect of this directive.
  628. \subsection{\var{\$SMARTLINK} : Use smartlinking}
  629. A unit that is compiled in the \var{\{\$SMARTLINK ON\}} state will be
  630. compiled in such a way that it can be used for smartlinking. This means that
  631. the unit is chopped in logical pieces: each procedure is put in it's own
  632. object file, and all object files are put together in a big archive. When
  633. using such a unit, only the pieces of code that you really need or call,
  634. will be linked in your program, thus reducing the size of your executable
  635. substantially. Beware that using smartlinked units slows down the
  636. compilation process, because a separate object file must be creayed for each
  637. procedure. If you have units with many functions and procedures, this can
  638. be a time consuming process, even more so if you use an external assembler
  639. (the assembler is called to assemble each procedure or function code block).
  640. The smartlinking directive should be specified {\em before} the unit
  641. declaration part:
  642. \begin{verbatim}
  643. {$SMARTLINK ON}
  644. Unit MyUnit;
  645. Interface
  646. ...
  647. \end{verbatim}
  648. This directive is equivalent to the \var{-Cx} command-line switch.
  649. \subsection{\var{\$STATIC} : Allow use of \var{Static} keyword.}
  650. If you specify the \var{\{\$STATIC ON\}} directive, then \var{Static}
  651. methods are allowed for objects. \var{Static} objects methods do not require
  652. a \var{Self} variable. They are equivalent to \var{Class} methods for
  653. classes. By default, \var{Static} methods are not allowed.
  654. This directive is equivalent to the \var{-St} command-line option.
  655. \subsection{\var{\$STOP} : Generate fatal error message}
  656. The following code
  657. \begin{verbatim}
  658. {$STOP This code is erroneous !}
  659. \end{verbatim}
  660. will display an error message when the compiler encounters it.
  661. The compiler will immediatly stop the compilation process.
  662. It has the same effect as the \var{\{\$FATAL\}} directive.
  663. \subsection{\var{\$T} or \var{\$TYPEDADDRESS} : Typed address operator (@)}
  664. In the \var{\{\$T+\}} or \var{\{\$TYPEDADDRESS ON\}} state the @ operator,
  665. when applied to a variable, returns a result of type \var{\^{}T}, if the
  666. type of the variable is \var{T}. In the \var{\{\$T-\}} state, the result is
  667. always an untyped pointer, which is assignment compatible with all other
  668. pointer types.
  669. \subsection{\var{\$UNDEF} : Undefine a symbol}
  670. The directive
  671. \begin{verbatim}
  672. {$UNDEF name}
  673. \end{verbatim}
  674. un-defines the symbol \var{name} if it was previously defined.
  675. \var{Name} is case insensitive.
  676. \subsection{\var{\$V} or \var{\$VARSTRINGCHECKS} : Var-string checking}
  677. When in the \var{+} or \var{ON} state, the compiler checks that strings
  678. passed as parameters are of the same, identical, string type as the declared
  679. parameters of the procedure.
  680. \subsection{\var{\$WAIT} : Wait for enter key press}
  681. If the compiler encounters a
  682. \begin{verbatim}
  683. {$WAIT }
  684. \end{verbatim}
  685. directive, it will resume compiling only after the user has pressed the
  686. enter key. If the generation of info messages is turned on, then the compiler
  687. will display the follwing message:
  688. \begin{verbatim}
  689. Press <return> to continue
  690. \end{verbatim}
  691. before waiting for a keypress. Careful ! this may interfere with automatic
  692. compilation processes. It should be used for debuggig purposes only.
  693. \subsection{\var{\$WARNING} : Generate warning message}
  694. If the generation of warnings is turned on, through the \var{-vw}
  695. command-line option or the \var{\{\$WARNINGS ON\}} directive, then
  696. \begin{verbatim}
  697. {$WARNING This is dubious code }
  698. \end{verbatim}
  699. will display a warning message when the compiler encounters it.
  700. \subsection{\var{\$WARNINGS} : Emit warnings}
  701. \var{\{\$WARNINGS ON\}} switches the generation of warnings on.
  702. \var{\{\$WARNINGS OFF\}} switches the generation of warnings off.
  703. Contrary to the command-line option \var{-vw} this
  704. is a local switch, this is useful for checking parts of your code.
  705. \subsection{\var{\$X} or \var{\$EXTENDEDSYNTAX} : Extended syntax}
  706. Extended syntax allows you to drop the result of a function. This means that
  707. you can use a function call as if it were a procedure. Standard this feature
  708. is on. You can switch it off using the \var{\{\$X-\}} or
  709. \var{\{\$EXTENDEDSYNTAX OFF\}}directive.
  710. The following, for instance, will not compile :
  711. \begin{verbatim}
  712. function Func (var Arg : sometype) : longint;
  713. begin
  714. ... { declaration of Func }
  715. end;
  716. ...
  717. {$X-}
  718. Func (A);
  719. \end{verbatim}
  720. The reason this construct is supported is that you may wish to call a
  721. function for certain side-effects it has, but you don't need the function
  722. result. In this case you don't need to assign the function result, saving
  723. you an extra variable.
  724. The command-line compiler switch \var{-Sa1} has the same effect as the
  725. \var{\{\$X+\}} directive.
  726. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  727. % Global switches
  728. \section{Global directives}
  729. \label{se:GlobalSwitch}
  730. Global directives affect the whole of the compilation process. That is why
  731. they also have a command - line counterpart. The command-line counterpart is
  732. given for each of the directives.
  733. \subsection{\var{\$APPTYPE} : Specify type of application (Win32 only)}
  734. The \var{\{\$APPTYPE XXX\}} accepts one argument that can have two possible
  735. values : \var{GUI} or \var{CONSOLE}. It is used to tell the windows
  736. Operating system if an application is a console application or a graphical
  737. application. By default, a program compiled by \fpc is a console
  738. application. Running it will display a console window. Specifying the
  739. \var{\{\$APPTYPE GUI\}} directive will mark the application as a graphical
  740. application; no console window will be opened when the application is run.
  741. Care should be taken when compiling \var{GUI} applications; the \var{Input}
  742. and \var{Output} files are not available in a GUI application, and
  743. attempting to read from or write to them will result in a run-time error.
  744. It is possible to determine the application type of a windows application
  745. at runtime. The \var{IsConsole} constant, declared as
  746. \begin{verbatim}
  747. Const
  748. IsConsole : Boolean
  749. \end{verbatim}
  750. contains \var{True} if the application is a console application, \var{False}
  751. if the application is a GUI application.
  752. \subsection{\var{\$D} or \var{\$DEBUGINFO}: Debugging symbols}
  753. When this switch is on (\var{\{\$DEBUGINFO ON\}}),
  754. the compiler inserts GNU debugging information in
  755. the executable. The effect of this switch is the same as the command-line
  756. switch \var{-g}. By default, insertion of debugging information is off.
  757. \subsection{\var{\$DESCRIPTION}}
  758. This switch is recognised for compatibility only, but is ignored completely
  759. by the compiler. At a later stage, this switch may be activated.
  760. \subsection{\var{\$E} : Emulation of coprocessor}
  761. This directive controls the emulation of the coprocessor. There is no
  762. command-line counterpart for this directive.
  763. \subsubsection{ Intel x86 version }
  764. When this switch is enabled, all floating point instructions
  765. which are not supported by standard coprocessor emulators will give out
  766. a warning.
  767. The compiler itself doesn't do the emulation of the coprocessor.
  768. To use coprocessor emulation under \dos go32v1 there is nothing special
  769. required, as it is handled automatically. (As of version 0.99.10, the
  770. go32v1 platform will no longer be supported)
  771. To use coprocessor emulation under \dos go32v2 you must use the
  772. emu387 unit, which contains correct initialization code for the
  773. emulator.
  774. Under \linux, the kernel takes care of the coprocessor support.
  775. \subsubsection{ Motorola 680x0 version }
  776. When the switch is on, no floating point opcodes are emitted
  777. by the code generator. Instead, internal run-time library routines
  778. are called to do the necessary calculations. In this case all
  779. real types are mapped to the single IEEE floating point type.
  780. \emph{ Remark : } By default, emulation is on. It is possible to
  781. intermix emulation code with real floating point opcodes, as
  782. long as the only type used is single or real.
  783. \subsection{\var{\$G} : Generate 80286 code}
  784. This option is recognised for Turbo Pascal compatibility, but is ignored,
  785. \subsection{\var{\$INCLUDEPATH} : Specify include path.}
  786. This option serves to specify the include path, where the compiler looks for
  787. include files. \var{\{\$INCLUDEPATH XXX} will add \var{XXX} to the include
  788. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  789. colons.
  790. for example
  791. \begin{verbatim}
  792. {$INCLUDEPATH ../inc;../i386}
  793. {$I strings.inc}
  794. \end{verbatim}
  795. Will add the directories \file{../inc} and \file{../i386} to the include
  796. path of the compiler. The compiler will look for the file \file{strings.inc}
  797. in both these directories, and will include the first found file. This directive is
  798. equivalent to the \var{-Fi} command-line switch.
  799. Caution is in order when using this directive: If you distribute files, the
  800. places of the files may not be the same as on your machine; moreover, the
  801. directory structure may be different. In general it would be fair to say
  802. that you should avoid using {\em absolute} paths, instead use {\em relative}
  803. paths, as in the example above. Only use this directive if you are certain
  804. of the places where the files reside. If you are not sure, it is better
  805. practice to use makefiles and makefile variables.
  806. \subsection{\var{\$L} or \var{\$LOCALSYMBOLS}: Local symbol information}
  807. This switch (not to be confused with the \var{\{\$L file\}} file linking
  808. directive) is recognised for Turbo Pascal compatibility, but is ignored.
  809. generation of symbol information is controlled by the \var{\$D} switch.
  810. \subsection{\var{\$LIBRARYPATH} : Specify library path.}
  811. This option serves to specify the library path, where the linker looks for
  812. static or dynamic libraries. \var{\{\$LIBRARYPATH XXX} will add \var{XXX}
  813. to the library path. \var{XXX} can contain one or more paths, separated
  814. by semi-colons or colons.
  815. for example
  816. \begin{verbatim}
  817. {$LIBRARYPATH /usr/X11/lib;/usr/local/lib}
  818. {$LINKLIB X11}
  819. \end{verbatim}
  820. Will add the directories \file{/usr/X11/lib} and \file{/usr/local/lib} to
  821. the linker library path. The linker will look for the library \file{libX11.so}
  822. in both these directories, and use the first found file. This directive is
  823. equivalent to the \var{-Fl} command-line switch.
  824. Caution is in order when using this directive: If you distribute files, the
  825. places of the libraries may not be the same as on your machine; moreover, the
  826. directory structure may be different. In general it would be fair to say
  827. that you should avoid using this directive. If you are not sure, it is better
  828. practice to use makefiles and makefile variables.
  829. \subsection{\var{\$M} or \var{\$MEMORY}: Memory sizes}
  830. This switch can be used to set the heap and stacksize. It's format is as
  831. follows:
  832. \begin{verbatim}
  833. {$M StackSize,HeapSize}
  834. \end{verbatim}
  835. Wher \var{StackSize} and \var{HeapSize} should be two integer values,
  836. greater than 1024. The first number sets the size of the stack, and the
  837. second the size of the heap. (Stack setting is ignored under \linux).
  838. The two numbers can be set on the command line using the \var{-Ch}
  839. and \var{-Cs} switches.
  840. \subsection{\var{\$MODE} : Set compiler compatibility mode}
  841. The \var{\{\$MODE\}} sets the compatibility mode of the compiler. This
  842. is equivalent to setting one of the command-line options \var{-So} or
  843. \var{-Sd} or \var{-S2}. it has the following arguments:
  844. \begin{description}
  845. \item[Default] Default mode. This reverts back to the mode that was set on
  846. the command-line.
  847. \item[Delphi] Delphi compatibility mode. All object-pascal extensions are
  848. enabled. This is the same as the command-line option \var{-Sd}.
  849. \item[TP] Turbo pascal compatibility mode. Object pascal extensions are
  850. disabled, except ansistrings, which remain valid. This is the same as the command-line option \var{-So}
  851. \item[FPC] FPC mode.
  852. \item[OBJFPC] Object pascal mode. This is the same as the \var{-S2}
  853. command-line option.
  854. \item[GPC] GNU pascal mode. This is the same as the \var{-Sp} command-line
  855. option.
  856. \end{description}
  857. For an exact description of each of these modes, see appendix \ref{ch:AppD},
  858. on page \pageref{ch:AppD}
  859. \subsection{\var{\$N} : Numeric processing }
  860. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  861. ignored, since the compiler always uses the coprocessor for floating point
  862. mathematics.
  863. \subsection{\var{\$O} : Overlay code generation }
  864. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  865. ignored.
  866. \subsection{\var{\$OBJECTPATH} : Specify object path.}
  867. This option serves to specify the object path, where the compiler looks for
  868. object files. \var{\{\$OBJECTPATH XXX} will add \var{XXX} to the object
  869. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  870. colons.
  871. for example
  872. \begin{verbatim}
  873. {$OBJECTPATH ../inc;../i386}
  874. {$L strings.o}
  875. \end{verbatim}
  876. Will add the directories \file{../inc} and \file{../i386} to the
  877. object path of the compiler. The compiler will look for the file \file{strings.o}
  878. in both these directories, and will link the first found file in the
  879. program. This directive is equivalent to the \var{-Fo} command-line switch.
  880. Caution is in order when using this directive: If you distribute files, the
  881. places of the files may not be the same as on your machine; moreover, the
  882. directory structure may be different. In general it would be fair to say
  883. that you should avoid using {\em absolute} paths, instead use {\em relative}
  884. paths, as in the example above. Only use this directive if you are certain
  885. of the places where the files reside. If you are not sure, it is better
  886. practice to use makefiles and makefile variables.
  887. \subsection{\var{\$S} : Stack checking}
  888. The \var{\{\$S+\}} directive tells the compiler to generate stack checking
  889. code. This generates code to check if a stack overflow occurred, i.e. to see
  890. whether the stack has grown beyond its maximally allowed size. If the stack
  891. grows beyond the maximum size, then a run-time error is generated, and the
  892. program will exit with exit code 202.
  893. Specifying \var{\{\$S-\}} will turn generation of stack-checking code off.
  894. The command-line compiler switch \var{-Ct} has the same effect as the
  895. \var{\{\$S+\}} directive.
  896. \subsection{\var{\$UNITPATH} : Specify unit path.}
  897. This option serves to specify the unit path, where the compiler looks for
  898. unit files. \var{\{\$UNITPATH XXX\}} will add \var{XXX} to the unit
  899. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  900. colons.
  901. for example
  902. \begin{verbatim}
  903. {$UNITPATH ../units;../i386/units}
  904. Uses strings;
  905. \end{verbatim}
  906. Will add the directories \file{../units} and \file{../i386/units} to the unit
  907. path of the compiler. The compiler will look for the file \file{strings.ppu}
  908. in both these directories, and will link the first found file in the
  909. program. This directive is equivalent to the \var{-Fu} command-line switch.
  910. Caution is in order when using this directive: If you distribute files, the
  911. places of the files may not be the same as on your machine; moreover, the
  912. directory structure may be different. In general it would be fair to say
  913. that you should avoid using {\em absolute} paths, instead use {\em relative}
  914. paths, as in the example above. Only use this directive if you are certain
  915. of the places where the files reside. If you are not sure, it is better
  916. practice to use makefiles and makefile variables.
  917. \subsection{\var{\$W} or \var{\$STACKFRAMES} : Generate stackframes}
  918. The \var{\{\$W\}} switch directove controls the generation of stackframes.
  919. In the on state (\var{\{\$STACKFRAMES ON\}}), the compiler will generate a
  920. stackframe for every procedure or function.
  921. In the off state, the compiler will omit the generation of a stackframe if
  922. the following conditions are satisfied:
  923. \begin{itemize}
  924. \item The procedure has no parameters.
  925. \item The procedure has no local variables.
  926. \item If the procedure is not an \var{assembler} procedure, it must not have
  927. a \var{asm ... end;} block.
  928. \item it is not a constuctor or desctructor.
  929. \end{itemize}
  930. If these conditions are satisfied, the stack frame will be omitted.
  931. \subsection{\var{\$Y} or \var{\$REFERENCEINFO} : Insert Browser information}
  932. This switch controls the generation of browser inforation. It is recognized
  933. for compatibility with Turbo Pascal and Delphi only, as Browser information
  934. generation is not yet fully supported.
  935. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  936. % Using conditionals and macros
  937. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  938. \chapter{Using conditionals, Messages and macros}
  939. \label{ch:CondMessageMacro}
  940. The \fpc compiler supports conditionals as in normal Turbo Pascal. It does,
  941. however, more than that. It allows you to make macros which can be used in
  942. your code, and it allows you to define messages or errors which will be
  943. displayed when compiling.
  944. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  945. % Conditionals
  946. \section{Conditionals}
  947. \label{se:Conditionals}
  948. The rules for using conditional symbols are the same as under Turbo Pascal.
  949. Defining a symbol goes as follows:
  950. \begin{verbatim}
  951. {$Define Symbol }
  952. \end{verbatim}
  953. From this point on in your code, the compiler knows the symbol \var{Symbol}.
  954. Symbols are, like the Pascal language, case insensitive.
  955. You can also define a symbol on the command line. the \var{-dSymbol} option
  956. defines the symbol \var{Symbol}. You can specify as many symbols on the
  957. command line as you want.
  958. Undefining an existing symbol is done in a similar way:
  959. \begin{verbatim}
  960. {$Undef Symbol }
  961. \end{verbatim}
  962. If the symbol didn't exist yet, this doesn't do anything. If the symbol
  963. existed previously, the symbol will be erased, and will not be recognized
  964. any more in the code following the \verb|{$Undef ...}| statement.
  965. You can also undefine symbols from the command line with the \var{-u}
  966. command-line switch..
  967. To compile code conditionally, depending on whether a symbol is defined or
  968. not, you can enclose the code in a \verb|{$ifdef Symbol}| .. \verb|{$endif}|
  969. pair. For instance the following code will never be compiled :
  970. \begin{verbatim}
  971. {$Undef MySymbol}
  972. {$ifdef Mysymbol}
  973. DoSomething;
  974. ...
  975. {$endif}
  976. \end{verbatim}
  977. Similarly, you can enclose your code in a \verb|{$Ifndef Symbol}| .. \verb|{$endif}|
  978. pair. Then the code between the pair will only be compiled when the used
  979. symbol doesn't exist. For example, in the following example, the call to the
  980. \var{DoSomething} will always be compiled:
  981. \begin{verbatim}
  982. {$Undef MySymbol}
  983. {$ifndef Mysymbol}
  984. DoSomething;
  985. ...
  986. {$endif}
  987. \end{verbatim}
  988. You can combine the two alternatives in one structure, namely as follows
  989. \begin{verbatim}
  990. {$ifdef Mysymbol}
  991. DoSomething;
  992. {$else}
  993. DoSomethingElse
  994. {$endif}
  995. \end{verbatim}
  996. In this example, if \var{MySymbol} exists, then the call to \var{DoSomething}
  997. will be compiled. If it doesn't exist, the call to \var{DoSomethingElse} is
  998. compiled.
  999. The \fpc compiler defines some symbols before starting to compile your
  1000. program or unit. You can use these symbols to differentiate between
  1001. different versions of the compiler, and between different compilers.
  1002. In \seet{Symbols}, a list of pre-defined symbols is given\footnote{Remark:
  1003. The \var{FPK} symbol is still defined for compatibility with older versions.}. In that table,
  1004. you should change \var{v} with the version number of the compiler
  1005. you're using, \var{r} with the release number and \var{p}
  1006. with the patch-number of the compiler. 'OS' needs to be changed by the type
  1007. of operating system. Currently this can be one of \var{DOS}, \var{GO32V2},
  1008. \var{LINUX}, \var{OS2}, \var{WIN32}, \var{MACOS}, \var{AMIGA} or \var{ATARI}.
  1009. This symbol is undefined if you specify a target that is different from the
  1010. platform you're compiling on.
  1011. The \var{-TSomeOS} option on the command line will define the \var{SomeOS} symbol,
  1012. and will undefine the existing platform symbol\footnote{In versions prior to
  1013. 0.9.4, this didn't happen, thus making Cross-compiling impossible.}.
  1014. \begin{FPCltable}{c}{Symbols defined by the compiler.}{Symbols} \hline
  1015. Free \\
  1016. VER\var{v} \\
  1017. VER\var{v}\_\var{r} \\
  1018. VER\var{v}\_\var{r}\_\var{p} \\
  1019. OS \\ \hline
  1020. \end{FPCltable}
  1021. As an example : Version 0.9.1 of the compiler, running on a Linux system,
  1022. defines the following symbols before reading the command line arguments:
  1023. \var{FPC}, \var{VER0}, \var{VER0\_9}, \var{VER0\_9\_1} and \var{LINUX}.
  1024. Specifying \var{-TOS2} on the command-line will undefine the \var{LINUX}
  1025. symbol, and will define the \var{OS2} symbol.
  1026. {\em Remark: } Symbols, even when they're defined in the interface part of
  1027. a unit, are not available outside that unit.
  1028. %\fpc supports the \var{\{\$IFOPT \}} directive for Turbo Pascal
  1029. %compatibility, but doesn't act on it. It always rejects the condition, so
  1030. %code between \var{\{\$IFOPT \}} and \var{\{\$Endif\}} is never compiled.
  1031. Except for the Turbo Pascal constructs, from version 0.9.8 and higher,
  1032. the \fpc compiler also supports a stronger conditional compile mechanism:
  1033. The \var{\{\$If \}} construct.
  1034. The prototype of this construct is as follows :
  1035. \begin{verbatim}
  1036. {$If expr}
  1037. CompileTheseLines;
  1038. {$else}
  1039. BetterCompileTheseLines;
  1040. {$endif}
  1041. \end{verbatim}
  1042. In this directive \var{expr} is a Pascal expression which is evaluated using
  1043. strings, unless both parts of a comparision can be evaluated as numbers,
  1044. in which case they are evaluated using numbers\footnote{Otherwise
  1045. \var{\{\$If 8>54} would evaluate to \var{True}}.
  1046. If the complemete expression evaluates to \var{'0'}, then it is considered
  1047. false and rejected. Otherwise it is considered true and accepted. This may
  1048. have unexpected consequences :
  1049. \begin{verbatim}
  1050. {$If 0}
  1051. \end{verbatim}
  1052. Will evaluate to \var{False} and be rejected, while
  1053. \begin{verbatim}
  1054. {$If 00}
  1055. \end{verbatim}
  1056. Will evaluate to \var{True}.
  1057. You can use any Pascal operator to construct your expression : \var{=, <>,
  1058. >, <, >=, <=, AND, NOT, OR} and you can use round brackets to change the
  1059. precedence of the operators.
  1060. The following example shows you many of the possibilities:
  1061. \begin{verbatim}
  1062. {$ifdef fpc}
  1063. var
  1064. y : longint;
  1065. {$else fpc}
  1066. var
  1067. z : longint;
  1068. {$endif fpc}
  1069. var
  1070. x : longint;
  1071. begin
  1072. {$if (fpc_version=0) and (fpc_release>6) and (fpc_patch>4)}
  1073. {$info At least this is version 0.9.5}
  1074. {$else}
  1075. {$fatalerror Problem with version check}
  1076. {$endif}
  1077. {$define x:=1234}
  1078. {$if x=1234}
  1079. {$info x=1234}
  1080. {$else}
  1081. {$fatalerror x should be 1234}
  1082. {$endif}
  1083. {$if 12asdf and 12asdf}
  1084. {$info $if 12asdf and 12asdf is ok}
  1085. {$else}
  1086. {$fatalerror $if 12asdf and 12asdf rejected}
  1087. {$endif}
  1088. {$if 0 or 1}
  1089. {$info $if 0 or 1 is ok}
  1090. {$else}
  1091. {$fatalerror $if 0 or 1 rejected}
  1092. {$endif}
  1093. {$if 0}
  1094. {$fatalerror $if 0 accepted}
  1095. {$else}
  1096. {$info $if 0 is ok}
  1097. {$endif}
  1098. {$if 12=12}
  1099. {$info $if 12=12 is ok}
  1100. {$else}
  1101. {$fatalerror $if 12=12 rejected}
  1102. {$endif}
  1103. {$if 12<>312}
  1104. {$info $if 12<>312 is ok}
  1105. {$else}
  1106. {$fatalerror $if 12<>312 rejected}
  1107. {$endif}
  1108. {$if 12<=312}
  1109. {$info $if 12<=312 is ok}
  1110. {$else}
  1111. {$fatalerror $if 12<=312 rejected}
  1112. {$endif}
  1113. {$if 12<312}
  1114. {$info $if 12<312 is ok}
  1115. {$else}
  1116. {$fatalerror $if 12<312 rejected}
  1117. {$endif}
  1118. {$if a12=a12}
  1119. {$info $if a12=a12 is ok}
  1120. {$else}
  1121. {$fatalerror $if a12=a12 rejected}
  1122. {$endif}
  1123. {$if a12<=z312}
  1124. {$info $if a12<=z312 is ok}
  1125. {$else}
  1126. {$fatalerror $if a12<=z312 rejected}
  1127. {$endif}
  1128. {$if a12<z312}
  1129. {$info $if a12<z312 is ok}
  1130. {$else}
  1131. {$fatalerror $if a12<z312 rejected}
  1132. {$endif}
  1133. {$if not(0)}
  1134. {$info $if not(0) is OK}
  1135. {$else}
  1136. {$fatalerror $if not(0) rejected}
  1137. {$endif}
  1138. {$info *************************************************}
  1139. {$info * Now have to follow at least 2 error messages: *}
  1140. {$info *************************************************}
  1141. {$if not(0}
  1142. {$endif}
  1143. {$if not(<}
  1144. {$endif}
  1145. end.
  1146. \end{verbatim}
  1147. As you can see from the example, this construct isn't useful when used
  1148. with normal symbols, but it is if you use macros, which are explained in
  1149. \sees{Macros}, they can be very useful. When trying this example, you must
  1150. switch on macro support, with the \var{-Sm} command-line switch.
  1151. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1152. % Macros
  1153. \section{Messages}
  1154. \label{se:Messages}
  1155. \fpc lets you define normal, warning and error messages in your code.
  1156. Messages can be used to display useful information, such as copyright
  1157. notices, a list of symbols that your code reacts on etc.
  1158. Warnings can be used if you think some part of your code is still buggy, or
  1159. if you think that a certain combination of symbols isn't useful. In general
  1160. anything which may cause problems when compiling.
  1161. Error messages can be useful if you need a certain symbol to be defined
  1162. to warn that a certain variable isn't defined or so, or when the compiler
  1163. version isn't suitable for your code.
  1164. The compiler treats these messages as if they were generated by the
  1165. compiler. This means that if you haven't turned on warning messages, the
  1166. warning will not e displayed. Errors are always displayed, and the compiler
  1167. stops as if an error had occurred.
  1168. For messages, the syntax is as follows :
  1169. \begin{verbatim}
  1170. {$Message Message text }
  1171. \end{verbatim}
  1172. Or
  1173. \begin{verbatim}
  1174. {$Info Message text }
  1175. \end{verbatim}
  1176. For notes:
  1177. \begin{verbatim}
  1178. {$Note Message text }
  1179. \end{verbatim}
  1180. For warnings:
  1181. \begin{verbatim}
  1182. {$Warning Warning Message text }
  1183. \end{verbatim}
  1184. For errors :
  1185. \begin{verbatim}
  1186. {$Error Error Message text }
  1187. \end{verbatim}
  1188. Lastly, for fatal errors :
  1189. \begin{verbatim}
  1190. {$FatalError Error Message text }
  1191. \end{verbatim}
  1192. or
  1193. \begin{verbatim}
  1194. {$Stop Error Message text }
  1195. \end{verbatim}
  1196. The difference between \var{\$Error} and \var{\$FatalError} or \var{\$Stop}
  1197. messages is that when the compiler encounters an error, it still continues
  1198. to compile. With a fatal error, the compiler stops.
  1199. {\em Remark :} You cannot use the '\var{\}}' character in your message, since
  1200. this will be treated as the closing brace of the message.
  1201. As an example, the following piece of code will generate an error when
  1202. the symbol \var{RequiredVar} isn't defined:
  1203. \begin{verbatim}
  1204. {$ifndef RequiredVar}
  1205. {$Error Requiredvar isn't defined !}
  1206. {$endif}
  1207. \end{verbatim}
  1208. But the compiler will continue to compile. It will not, however, generate a
  1209. unit file or a program (since an error occurred).
  1210. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1211. % Macros
  1212. \section{Macros}
  1213. \label{se:Macros}
  1214. Macros are very much like symbols in their syntax, the difference is that
  1215. macros have a value whereas a symbol simply is defined or is not defined.
  1216. If you want macro support, you need to specify the \var{-Sm} command-line
  1217. switch, otherwise your macro will be regarded as a symbol.
  1218. Defining a macro in your program is done in the same way as defining a symbol;
  1219. in a \var{\{\$define \}} preprocessor statement\footnote{In compiler
  1220. versions older than 0.9.8, the assignment operator for a macros wasn't
  1221. \var{:=}, but \var{=}}:
  1222. \begin{verbatim}
  1223. {$define ident:=expr}
  1224. \end{verbatim}
  1225. If the compiler encounters \var{ident} in the rest of the source file, it
  1226. will be replaced immediately by \var{expr}. This replacement works
  1227. recursive, meaning that when the compiler expanded one of your macros, it
  1228. will look at the resulting expression again to see if another replacement
  1229. can be made. You need to be careful with this, because an infinite loop can
  1230. occur in this manner.
  1231. Here are two examples which illustrate the use of macros:
  1232. \begin{verbatim}
  1233. {$define sum:=a:=a+b;}
  1234. ...
  1235. sum { will be expanded to 'a:=a+b;'
  1236. remark the absence of the semicolon}
  1237. ...
  1238. {$define b:=100}
  1239. sum { Will be expanded recursively to a:=a+100; }
  1240. ...
  1241. \end{verbatim}
  1242. The previous example could go wrong :
  1243. \begin{verbatim}
  1244. {$define sum:=a:=a+b;}
  1245. ...
  1246. sum { will be expanded to 'a:=a+b;'
  1247. remark the absence of the semicolon}
  1248. ...
  1249. {$define b=sum} { DON'T do this !!!}
  1250. sum { Will be infinitely recursively expanded... }
  1251. ...
  1252. \end{verbatim}
  1253. On my system, the last example results in a heap error, causing the compiler
  1254. to exit with a run-time error 203.
  1255. {\em Remark: } Macros defined in the interface part of a unit are not
  1256. available outside that unit ! They can just be used as a notational
  1257. convenience, or in conditional compiles.
  1258. By default, from version 0.9.8 of the compiler on, the compiler predefines three
  1259. macros, containing the version number, the release number and the patch
  1260. number. They are listed in \seet{DefMacros}.
  1261. \begin{FPCltable}{ll}{Predefined macros}{DefMacros} \hline
  1262. Symbol & Contains \\ \hline
  1263. \var{FPC\_VERSION} & The version number of the compiler. \\
  1264. \var{FPC\_RELEASE} & The release number of the compiler. \\
  1265. \var{FPC\_PATCH} & The patch number of the compiler. \\
  1266. \hline
  1267. \end{FPCltable}
  1268. {\em Remark: } Don't forget that macros support isn't on by default. You
  1269. need to compile with the \var{-Sm} command-line switch.
  1270. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1271. % Using assembly language
  1272. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1273. \chapter{Using Assembly language}
  1274. \label{ch:AsmLang}
  1275. \fpc supports inserting of assembler instructions in your code. The
  1276. mechanism for this is the same as under Turbo Pascal. There are, however
  1277. some substantial differences, as will be explained in the following.
  1278. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1279. % Intel syntax
  1280. \section{Intel syntax}
  1281. \label{se:Intel}
  1282. As of version 0.9.7, \fpc supports Intel syntax for the Intel family of Ix86
  1283. processors in it's \var{asm} blocks.
  1284. The Intel syntax in your \var{asm} block is converted to AT\&T syntax by the
  1285. compiler, after which it is inserted in the compiled source.
  1286. The supported assembler constructs are a subset of the normal assembly
  1287. syntax. In what follows we specify what constructs are not supported in
  1288. \fpc, but which exist in Turbo Pascal:
  1289. \begin{itemize}
  1290. \item The \var{TBYTE} qualifier is not supported.
  1291. \item The \var{\&} identifier override is not supported.
  1292. \item The \var{HIGH} operator is not supported.
  1293. \item The \var{LOW} operator is not supported.
  1294. \item The \var{OFFSET} and \var{SEG} operators are not supported.
  1295. use \var{LEA} and the various \var{Lxx} instructions instead.
  1296. \item Expressions with constant strings are not allowed.
  1297. \item Access to record fields via parenthesis is not allowed
  1298. \item Typecasts with normal pascal types are not allowed, only
  1299. recognized assembler typecasts are allowed.\\ Example:
  1300. \begin{verbatim}
  1301. mov al, byte ptr MyWord -- allowed,
  1302. mov al, byte(MyWord) -- allowed,
  1303. mov al, shortint(MyWord) -- not allowed.
  1304. \end{verbatim}
  1305. \item Pascal type typecasts on constants are not allowed. \\
  1306. Example:
  1307. \begin{verbatim}
  1308. const s= 10; const t = 32767;
  1309. \end{verbatim}
  1310. in Turbo Pascal:
  1311. \begin{verbatim}
  1312. mov al, byte(s) -- useless typecast.
  1313. mov al, byte(t) -- syntax error!
  1314. \end{verbatim}
  1315. In this parser, either of those cases will give out a syntax error.
  1316. \item Constant references expressions with constants only are not
  1317. allowed (in all cases they do not work in protected mode,
  1318. under linux i386). \\ Examples:
  1319. \begin{verbatim}
  1320. mov al,byte ptr ['c'] -- not allowed.
  1321. mov al,byte ptr [100h] -- not allowed.
  1322. \end{verbatim}
  1323. (This is due to the limitation of Turbo Assembler).
  1324. \item Brackets within brackets are not allowed
  1325. \item Expressions with segment overrides fully in brackets are
  1326. presently not supported, but they can easily be implemented
  1327. in BuildReference if requested. \\ Example:
  1328. \begin{verbatim}
  1329. mov al,[ds:bx] -- not allowed
  1330. \end{verbatim}
  1331. use instead:
  1332. \begin{verbatim}
  1333. mov al,ds:[bx]
  1334. \end{verbatim}
  1335. \item Possible allowed indexing are as follows:
  1336. \begin{itemize}
  1337. \item \var{Sreg:[REG+REG*SCALING+/-disp]}
  1338. \item \var{SReg:[REG+/-disp]}
  1339. \item \var{SReg:[REG]}
  1340. \item \var{SReg:[REG+REG+/-disp]}
  1341. \item \var{SReg:[REG+REG*SCALING]}
  1342. \end{itemize}
  1343. Where \var{Sreg} is optional and specifies the segment override.
  1344. {\em Notes:}
  1345. \begin{enumerate}
  1346. \item The order of terms is important contrary to Turbo Pascal.
  1347. \item The Scaling value must be a value, and not an identifier
  1348. to a symbol.\\ Examples:
  1349. \begin{verbatim}
  1350. const myscale = 1;
  1351. ...
  1352. mov al,byte ptr [esi+ebx*myscale] -- not allowed.
  1353. \end{verbatim}
  1354. use:
  1355. \begin{verbatim}
  1356. mov al, byte ptr [esi+ebx*1]
  1357. \end{verbatim}
  1358. \end{enumerate}
  1359. \item Possible variable identifier syntax is as follows:
  1360. (Id = Variable or typed constant identifier.)
  1361. \begin{enumerate}
  1362. \item \var{ID}
  1363. \item \var{[ID]}
  1364. \item \var{[ID+expr]}
  1365. \item \var{ID[expr]}
  1366. \end{enumerate}
  1367. Possible fields are as follow:
  1368. \begin{enumerate}
  1369. \item \var{ID.subfield.subfield ...}
  1370. \item \var{[ref].ID.subfield.subfield ...}
  1371. \item \var{[ref].typename.subfield ...}
  1372. \end{enumerate}
  1373. \item Local Labels: Contrary to Turbo Pascal, local labels, must
  1374. at least contain one character after the local symbol indicator.\\
  1375. Example:
  1376. \begin{verbatim}
  1377. @: -- not allowed
  1378. \end{verbatim}
  1379. use instead, for example:
  1380. \begin{verbatim}
  1381. @1: -- allowed
  1382. \end{verbatim}
  1383. \item Contrary to Turbo Pascal local references cannot be used as references,
  1384. only as displacements. \\ example:
  1385. \begin{verbatim}
  1386. lds si,@mylabel -- not allowed
  1387. \end{verbatim}
  1388. \item Contrary to Turbo Pascal, \var{SEGCS}, \var{SEGDS}, \var{SEGES} and
  1389. \var{SEGSS} segment overrides are presently not supported.
  1390. (This is a planned addition though).
  1391. \item Contrary to Turbo Pascal where memory sizes specifiers can
  1392. be practically anywhere, the \fpc Intel inline assembler requires
  1393. memory size specifiers to be outside the brackets. \\
  1394. example:
  1395. \begin{verbatim}
  1396. mov al,[byte ptr myvar] -- not allowed.
  1397. \end{verbatim}
  1398. use:
  1399. \begin{verbatim}
  1400. mov al,byte ptr [myvar] -- allowed.
  1401. \end{verbatim}
  1402. \item Base and Index registers must be 32-bit registers.
  1403. (limitation of the GNU Assembler).
  1404. \item \var{XLAT} is equivalent to \var{XLATB}.
  1405. \item Only Single and Double FPU opcodes are supported.
  1406. \item Floating point opcodes are currently not supported
  1407. (except those which involve only floating point registers).
  1408. \end{itemize}
  1409. The Intel inline assembler supports the following macros :
  1410. \begin{description}
  1411. \item [@Result] represents the function result return value.
  1412. \item [Self] represents the object method pointer in methods.
  1413. \end{description}
  1414. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1415. % AT&T syntax
  1416. \section{AT\&T Syntax}
  1417. \label{se:AttSyntax}
  1418. \fpc uses the \gnu \var{as} assembler to generate its object files for
  1419. the Intel Ix86 processors . Since
  1420. the \gnu assembler uses AT\&T assembly syntax, the code you write should
  1421. use the same syntax. The differences between AT\&T and Intel syntax as used
  1422. in Turbo Pascal are summarized in the following:
  1423. \begin{itemize}
  1424. \item The opcode names include the size of the operand. In general, one can
  1425. say that the AT\&T opcode name is the Intel opcode name, suffixed with a
  1426. '\var{l}', '\var{w}' or '\var{b}' for, respectively, longint (32 bit),
  1427. word (16 bit) and byte (8 bit) memory or register references. As an example,
  1428. the Intel construct \mbox{'\var{mov al bl}} is equivalent to the AT\&T style '\var{movb
  1429. \%bl,\%al}' instruction.
  1430. \item AT\&T immediate operands are designated with '\$', while Intel syntax
  1431. doesn't use a prefix for immediate operands. Thus the Intel construct
  1432. '\var{mov ax, 2}' becomes '\var{movb \$2, \%al}' in AT\&T syntax.
  1433. \item AT\&T register names are preceded by a '\var{\%}' sign.
  1434. They are undelimited in Intel syntax.
  1435. \item AT\&T indicates absolute jump/call operands with '\var{*}', Intel
  1436. syntax doesn't delimit these addresses.
  1437. \item The order of the source and destination operands are switched. AT\&T
  1438. syntax uses '\var{Source, Dest}', while Intel syntax features '\var{Dest,
  1439. Source}'. Thus the Intel construct '\var{add eax, 4}' transforms to
  1440. '\var{addl \$4, \%eax}' in the AT\&T dialect.
  1441. \item Immediate long jumps are prefixed with the '\var{l}' prefix. Thus the
  1442. Intel '\var{call/jmp section:offset'} is transformed to '\var{lcall/ljmp
  1443. \$section,\$offset}'. Similarly the far return is '\var{lret}', instead of the
  1444. Intel '\var{ret far}'.
  1445. \item Memory references are specified differently in AT\&T and Intel
  1446. assembly. The Intel indirect memory reference
  1447. \begin{quote}
  1448. \var{Section:[Base + Index*Scale + Offs]}
  1449. \end{quote}
  1450. is written in AT\&T syntax as :
  1451. \begin{quote}
  1452. \var{Section:Offs(Base,Index,Scale)}
  1453. \end{quote}
  1454. Where \var{Base} and \var{Index} are optional 32-bit base and index
  1455. registers, and \var{Scale} is used to multiply \var{Index}. It can take the
  1456. values 1,2,4 and 8. The \var{Section} is used to specify an optional section
  1457. register for the memory operand.
  1458. \end{itemize}
  1459. More information about the AT\&T syntax can be found in the \var{as} manual,
  1460. although the following differences with normal AT\&T assembly must be taken
  1461. into account :
  1462. \begin{itemize}
  1463. \item Only the following directives are presently supported:
  1464. \begin{description}
  1465. \item[.byte]
  1466. \item[.word]
  1467. \item[.long]
  1468. \item[.ascii]
  1469. \item[.asciz]
  1470. \item[.globl]
  1471. \end{description}
  1472. \item The following directives are recognized but are not
  1473. supported:
  1474. \begin{description}
  1475. \item[.align]
  1476. \item[.lcomm]
  1477. \end{description}
  1478. Eventually they will be supported.
  1479. \item Directives are case sensitive, other identifiers are not case sensitive.
  1480. \item Contrary to GAS local labels/symbols {\em must} start with \var{.L}
  1481. \item The nor operator \var{'!'} is not supported.
  1482. \item String expressions in operands are not supported.
  1483. \item CBTW,CWTL,CWTD and CLTD are not supported, use the normal intel
  1484. equivalents instead.
  1485. \item Constant expressions which represent memory references are not
  1486. allowed even though constant immediate value expressions are supported. \\
  1487. examples:
  1488. \begin{verbatim}
  1489. const myid = 10;
  1490. ...
  1491. movl $myid,%eax -- allowed
  1492. movl myid(%esi),%eax -- not allowed.
  1493. \end{verbatim}
  1494. \item When the \var{.globl} directive is found, the symbol following
  1495. it is made public and is immediately emitted.
  1496. Therefore label names with this name will be ignored.
  1497. \item Only Single and Double FPU opcodes are supported.
  1498. \end{itemize}
  1499. The AT\&T inline assembler supports the following macros :
  1500. \begin{description}
  1501. \item [\_\_RESULT] represents the function result return value.
  1502. \item [\_\_SELF] represents the object method pointer in methods.
  1503. \item [\_\_OLDEBP] represents the old base pointer in recusrive routines.
  1504. \end{description}
  1505. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1506. % Calling mechanism
  1507. \section{Calling mechanism}
  1508. \label{se:Calling}
  1509. Procedures and Functions are called with their parameters on the stack.
  1510. Contrary to Turbo Pascal, {\em all} parameters are pushed on the stack, and
  1511. they are pushed {\em right} to {\em left}, instead of left to right for
  1512. Turbo Pascal. This is especially important if you have some assembly
  1513. subroutines in Turbo Pascal which you would like to translate to \fpc.
  1514. Function results are returned in the accumulator, if they fit in the
  1515. register.
  1516. The registers are {\em not} saved when calling a function or procedure. If
  1517. you want to call a procedure or function from assembly language, you must
  1518. save any registers you wish to preserve.
  1519. The first thing a procedure does is saving the base pointer, and setting the
  1520. base pointer equal to the stack pointer. References to the pushed parameters
  1521. and local variables are constructed using the base pointer.
  1522. When the procedure or function exits, it clears the stack.
  1523. When you want your code to be called by a C library or used in a C
  1524. program, you will run into trouble because of this calling mechanism. In C,
  1525. the calling procedure is expected to clear the stack, not the called
  1526. procedure. In other words, the arguments still are on the stack when the
  1527. procedure exits. To avoid this problem, \fpc supports the \var{export}
  1528. modifier. Procedures that are defined using the export modifier, use a
  1529. C-compatible calling mechanism. This means that they can be called from a
  1530. C program or library, or that you can use them as a callback function.
  1531. This also means that you cannot call this procedure or function from your
  1532. own program, since your program uses the Pascal calling convention.
  1533. However, in the exported function, you can of course call other Pascal
  1534. routines.
  1535. As of version 0.9.8, the \fpc compiler supports also the \var{cdecl} and
  1536. \var{stdcall} modifiers, as found in Delphi. The \var{cdecl} modifier does
  1537. the same as the \var{export} modifier, and \var{stdcall} does nothing, since
  1538. \fpc pushes the paramaters from right to left by default.
  1539. In addition to the Delphi \var{cdecl} construct, \fpc also supports the
  1540. \var{popstack} directive; it is nearly the same a the \var{cdecl} directive,
  1541. only it still mangles the name, i.e. makes it into a name such as the
  1542. compiler uses internally.
  1543. All this is summarized in \seet{Calling}. The first column lists the
  1544. modifier you specify for a procedure declaration. The second one lists the
  1545. order the paramaters are pushed on the stack. The third column specifies who
  1546. is responsible for cleaning the stack: the caller or the called function.
  1547. Finally, the last column specifies if registers are used to pass parameters
  1548. to the function.
  1549. \begin{FPCltable}{llll}{Calling mechanisms in \fpc}{Calling}\hline
  1550. Modifier & Pushing order & Stack cleaned by & Parameters in registers \\
  1551. \hline
  1552. (none) & Right-to-left & Function & No \\
  1553. cdecl & Right-to-left & Caller & No \\
  1554. export & Right-to-left & Caller & No \\
  1555. stdcall & Right-to-left & Function & No \\
  1556. popstack & Right-to-left & Caller & No \\ \hline
  1557. \end{FPCltable}
  1558. More about this can be found in \seec{Linking} on linking.
  1559. \subsection{ Ix86 calling conventions }
  1560. Standard entry code for procedures and functions is as follows on the
  1561. x86 architecture:
  1562. \begin{verbatim}
  1563. pushl %ebp
  1564. movl %esp,%ebp
  1565. \end{verbatim}
  1566. The generated exit sequence for procedure and functions looks as follows:
  1567. \begin{verbatim}
  1568. leave
  1569. ret $xx
  1570. \end{verbatim}
  1571. Where \var{xx} is the total size of the pushed parameters.
  1572. To have more information on function return values take a look at the
  1573. \sees{RegConvs} section.
  1574. \subsection{ M680x0 calling conventions }
  1575. Standard entry code for procedures and functions is as follows on the
  1576. 680x0 architecture:
  1577. \begin{verbatim}
  1578. move.l a6,-(sp)
  1579. move.l sp,a6
  1580. \end{verbatim}
  1581. The generated exit sequence for procedure and functions looks as follows:
  1582. \begin{verbatim}
  1583. unlk a6
  1584. move.l (sp)+,a0 ; Get return address
  1585. add.l #xx,sp ; Remove allocated stack
  1586. move.l a0,-(sp) ; Put back return address on top of the stack
  1587. \end{verbatim}
  1588. Where \var{xx} is the total size of the pushed parameters.
  1589. To have more information on function return values take a look at the
  1590. \sees{RegConvs} section.
  1591. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1592. % Telling the compiler what registers have changed
  1593. \section{Signalling changed registers}
  1594. \label{se:RegChanges}
  1595. When the compiler uses variables, it sometimes stores them, or the result of
  1596. some calculations, in the processor registers. If you insert assembler code
  1597. in your program that modifies the processor registers, then this may
  1598. interfere with the compiler's idea about the registers. To avoid this
  1599. problem, \fpc allows you to tell the compiler which registers have changed.
  1600. The compiler will then avoid using these registers. Telling the compiler
  1601. which registers have changed, is done by specifying a set of register names
  1602. behind an assembly block, as follows:
  1603. \begin{verbatim}
  1604. asm
  1605. ...
  1606. end ['R1',...,'Rn'];
  1607. \end{verbatim}
  1608. Here \var{R1} to \var{Rn} are the names of the 32-bit registers you
  1609. modify in your assembly code.
  1610. As an example :
  1611. \begin{verbatim}
  1612. asm
  1613. movl BP,%eax
  1614. movl 4(%eax),%eax
  1615. movl %eax,__RESULT
  1616. end ['EAX'];
  1617. \end{verbatim}
  1618. This example tells the compiler that the \var{EAX} register was modified.
  1619. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1620. % Register conventions
  1621. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1622. \section{Register Conventions}
  1623. \label{se:RegConvs}
  1624. The compiler has different register conventions, depending on the
  1625. target processor used.
  1626. \subsection{ Intel x86 version }
  1627. When optimizations are on, no register can be freely modified, without
  1628. first being saved and then restored. Otherwise, EDI is usually used as
  1629. a scratch register and can be freely used in assembler blocks.
  1630. \subsection{ Motorola 680x0 version }
  1631. Registers which can be freely modified without saving are registers
  1632. D0, D1, D6, A0, A1, and floating point registers FP2 to FP7. All other
  1633. registers are to be considered reserved and should be saved and then
  1634. restored when used in assembler blocks.
  1635. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1636. % Linking issues
  1637. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1638. \chapter{Linking issues}
  1639. \label{ch:Linking}
  1640. When you only use Pascal code, and Pascal units, then you will not see much
  1641. of the part that the linker plays in creating your executable.
  1642. The linker is only called when you compile a program. When compiling units,
  1643. the linker isn't invoked.
  1644. However, there are times that you want to C libraries, or to external
  1645. object files that are generated using a C compiler (or even another pascal
  1646. compiler). The \fpc compiler can generate calls to a C function,
  1647. and can generate functions that can be called from C (exported functions).
  1648. More on these calling conventions can be found in \sees{Calling}.
  1649. In general, there are 2 things you must do to use a function that resides in
  1650. an external library or object file:
  1651. \begin{enumerate}
  1652. \item You must make a pascal declaration of the function or procedure you
  1653. want to use.
  1654. \item You must tell the compiler where the function resides, i.e. in what
  1655. object file or what library, so the compiler can link the necessary code in.
  1656. \end{enumerate}
  1657. The same holds for variables. To access a variable that resides in an
  1658. external object file, you ust declare it, and tell the compiler where to
  1659. find it.
  1660. The following sections attempt to explain how to do this.
  1661. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1662. % Declaring an external function or procedure
  1663. \section{Using external functions or procedures}
  1664. \label{se:ExternalFunction}
  1665. The first step in using external code blocks is declaring the function you
  1666. want to use. \fpc supports Delphi syntax, i.e. you must use the
  1667. \var{external} directive. The \var{external} directive replaces, in effect,
  1668. the code block of the function. As such, It cannot be used in an interface
  1669. section of a unit, but must always reside in the implementation section.
  1670. There exist four variants of the external direcive :
  1671. \begin{enumerate}
  1672. \item A simple external declaration:
  1673. \begin{verbatim}
  1674. Procedure ProcName (Args : TPRocArgs); external;
  1675. \end{verbatim}
  1676. The \var{external} directive tells the compiler that the function resides in
  1677. an external block of code. You can use this together with the \var{\{\$L \}}
  1678. or \var{\{\$LinkLib \}} directives to link to a function or procedure in a
  1679. library or external object file.
  1680. \item You can give the \var{external} directive a library name as an
  1681. argument:
  1682. \begin{verbatim}
  1683. Procedure ProcName (Args : TPRocArgs); external 'Name';
  1684. \end{verbatim}
  1685. This tells the compiler that the procedure resides in a library with name
  1686. \var{'Name'}. This method is equivalent to the following:
  1687. \begin{verbatim}
  1688. Procedure ProcName (Args : TPRocArgs);external;
  1689. {$LinkLib 'Name'}
  1690. \end{verbatim}
  1691. \item The \var{external} can also be used with two arguments:
  1692. \begin{verbatim}
  1693. Procedure ProcName (Args : TPRocArgs); external 'Name'
  1694. name 'OtherProcName';
  1695. \end{verbatim}
  1696. This has the same meaning as the previous declaration, only the compiler
  1697. will use the name \var{'OtherProcName'} when linking to the library. This
  1698. can be used to give different names to procedures and functions in an
  1699. external library.
  1700. This method is equivalent to the following code:
  1701. \begin{verbatim}
  1702. Procedure OtherProcName (Args : TProcArgs); external;
  1703. {$LinkLib 'Name'}
  1704. Procedure ProcName (Args : TPRocArgs);
  1705. begin
  1706. OtherProcName (Args);
  1707. end;
  1708. \end{verbatim}
  1709. \item Lastly, onder \windows and \ostwo, there is a fourth possibility
  1710. to specify an external function: In \file{.DLL} files, functionas also have
  1711. a unique number (their index). It is possible to refer to these fuctions
  1712. using their index:
  1713. \begin{verbatim}
  1714. Procedure ProcName (Args : TPRocArgs); external 'Name' Index SomeIndex;
  1715. \end{verbatim}
  1716. This tells the compiler that the procedure \var{ProcName} resides in a
  1717. dynamic link library, with index {SomeIndex}.
  1718. {\em Remark :} Note that this is ONLY available under \windows and \ostwo.
  1719. \end{enumerate}
  1720. In earlier versions of the \fpc compiler, the following construct was
  1721. also possible :
  1722. \begin{verbatim}
  1723. Procedure ProcName (Args : TPRocArgs); [ C ];
  1724. \end{verbatim}
  1725. This method is equivalent to the following statement:
  1726. \begin{verbatim}
  1727. Procedure ProcName (Args : TPRocArgs); cdecl; external;
  1728. \end{verbatim}
  1729. However, the \var{[ C ]} directive is no longer supported as of version
  1730. 0.99.5 of \fpc, therefore you should use the \var{external} directive,
  1731. with the \var{cdecl} directive, if needed.
  1732. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1733. % Declaring an external variabl
  1734. \section{Using external variables}
  1735. \label{se:ExternalVars}
  1736. Some libaries or code blocks have variables which they export. You can access
  1737. these variables much in the same way as external functions. To access an
  1738. external variable, you declare it as follows:
  1739. \begin{verbatim}
  1740. Var
  1741. MyVar : MyType; external name 'varname';
  1742. \end{verbatim}
  1743. The effect of this declaration is twofold:
  1744. \begin{enumerate}
  1745. \item No space is allocated for this variable.
  1746. \item The name of the variable used in the assebler code is \var{varname}.
  1747. This is a case sensitive name, so you must be careful.
  1748. \end{enumerate}
  1749. The variable will be
  1750. accessible with it's declared name, i.e. \var{MyVar} in this case.
  1751. A second possibility is the declaration:
  1752. \begin{verbatim}
  1753. Var
  1754. varname : MyType; cvar; external;
  1755. \end{verbatim}
  1756. The effect of this declaration is twofold as in the previous case:
  1757. \begin{enumerate}
  1758. \item The \var{external} modifier ensures that no space is allocated for
  1759. this variable.
  1760. \item The \var{cvar} modifier tells the compiler that the name of the
  1761. variable used in the assebler code is exactly as specified in the
  1762. declaration. This is a case sensitive name, so you must be careful.
  1763. \end{enumerate}
  1764. In this case, you access the variable with it's C name, but case
  1765. insensitive. The first possibility allows you to change the name of the
  1766. external variable for internal use.
  1767. In order to be able to compile such statements, the compiler switch \var{-Sv}
  1768. must be used.
  1769. As an example, let's look at the following C file (in \file{extvar.c}):
  1770. \begin{verbatim}
  1771. /*
  1772. Declare a variable, allocate storage
  1773. */
  1774. int extvar = 12;
  1775. \end{verbatim}
  1776. And the following program (in \file{extdemo.pp}):
  1777. \begin{verbatim}
  1778. Program ExtDemo;
  1779. {$L extvar.o}
  1780. Var { Case sensitive declaration !! }
  1781. extvar : longint; cvar;external;
  1782. I : longint; external name 'extvar';
  1783. begin
  1784. { Extvar can be used case insensitive !! }
  1785. Writeln ('Variable ''extvar'' has value : ',ExtVar);
  1786. Writeln ('Variable ''I'' has value : ',i);
  1787. end.
  1788. \end{verbatim}
  1789. Compiling the C file, and the pascal program:
  1790. \begin{verbatim}
  1791. gcc -c -o extvar.o extvar.c
  1792. ppc386 -Sv extdemo
  1793. \end{verbatim}
  1794. Will produce a program \file{extdemo} which will print
  1795. \begin{verbatim}
  1796. Variable 'extvar' has value : 12
  1797. Variable 'I' has value : 12
  1798. \end{verbatim}
  1799. on your screen.
  1800. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1801. % Linking an object file in your program
  1802. \section{Linking to an object file}
  1803. \label{se:LinkIn}
  1804. Having declared the external function or variable that resides in an object file,
  1805. you can use it as if it was defined in your own program or unit.
  1806. To produce an executable, you must still link the object file in.
  1807. This can be done with the \var{\{\$L file.o\}} directive.
  1808. This will cause the linker to link in the object file \file{file.o}. On
  1809. \linux systems, this filename is case sensitive. Under \dos, case isn't
  1810. important. Note that \var{file.o} must be in the current directory if you
  1811. don't specify a path. The linker will not search for \file{file.o} if it
  1812. isn't found.
  1813. You cannot specify libraries in this way, it is for object files only.
  1814. Here we present an example. Consider that you have some assembly routine that
  1815. calculates the nth Fibonacci number :
  1816. \begin{verbatim}
  1817. .text
  1818. .align 4
  1819. .globl Fibonacci
  1820. .type Fibonacci,@function
  1821. Fibonacci:
  1822. pushl %ebp
  1823. movl %esp,%ebp
  1824. movl 8(%ebp),%edx
  1825. xorl %ecx,%ecx
  1826. xorl %eax,%eax
  1827. movl $1,%ebx
  1828. incl %edx
  1829. loop:
  1830. decl %edx
  1831. je endloop
  1832. movl %ecx,%eax
  1833. addl %ebx,%eax
  1834. movl %ebx,%ecx
  1835. movl %eax,%ebx
  1836. jmp loop
  1837. endloop:
  1838. movl %ebp,%esp
  1839. popl %ebp
  1840. ret
  1841. \end{verbatim}
  1842. Then you can call this function with the following Pascal Program:
  1843. \begin{verbatim}
  1844. Program FibonacciDemo;
  1845. var i : longint;
  1846. Function Fibonacci (L : longint):longint;cdecl;external;
  1847. {$L fib.o}
  1848. begin
  1849. For I:=1 to 40 do
  1850. writeln ('Fib(',i,') : ',Fibonacci (i));
  1851. end.
  1852. \end{verbatim}
  1853. With just two commands, this can be made into a program :
  1854. \begin{verbatim}
  1855. as -o fib.o fib.s
  1856. ppc386 fibo.pp
  1857. \end{verbatim}
  1858. This example supposes that you have your assembler routine in \file{fib.s},
  1859. and your Pascal program in \file{fibo.pp}.
  1860. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1861. % Linking your program to a library
  1862. \section{Linking to a library}
  1863. \label{se:LinkOut}
  1864. To link your program to a library, the procedure depends on how you declared
  1865. the external procedure.
  1866. %If you used thediffers a little from the
  1867. %procedure when you link in an object file. although the declaration step
  1868. %remains the same (see \ref{se:ExternalFunction} on how to do that).
  1869. In case you used the follwing syntax to declare your procedure:
  1870. \begin{verbatim}
  1871. Procedure ProcName (Args : TPRocArgs); external 'Name';
  1872. \end{verbatim}
  1873. You don't need to take additional steps to link your file in, the compiler
  1874. will do all that is needed for you. On \windowsnt it will link to
  1875. \file{Name.dll}, on \linux your program will be linked to library
  1876. \file{libname}, which can be a static or dynamic library.
  1877. In case you used
  1878. \begin{verbatim}
  1879. Procedure ProcName (Args : TPRocArgs); external;
  1880. \end{verbatim}
  1881. You still need to explicity link to the library. This can be done in 2 ways:
  1882. \begin{enumerate}
  1883. \item You can tell the compiler in the source file what library to link to
  1884. using the \var{\{\$LinkLib 'Name'\}} directive:
  1885. \begin{verbatim}
  1886. {$LinkLib 'gpm'}
  1887. \end{verbatim}
  1888. This will link to the \file{gpm} library. On \linux systems, you needn't
  1889. specify the extension or 'lib' prefix of the library. The compiler takes
  1890. care of that. On \dos or \windows systems, you need to specify the full
  1891. name.
  1892. \item You can also tell the compiler on the command-line to link in a
  1893. library: The \var{-k} option can be used for that. For example
  1894. \begin{verbatim}
  1895. ppc386 -k'-lgpm' myprog.pp
  1896. \end{verbatim}
  1897. Is equivalent to the above method, and tells the linker to link to the
  1898. \file{gpm} library.
  1899. \end{enumerate}
  1900. As an example; consider the following program :
  1901. \begin{verbatim}
  1902. program printlength;
  1903. {$linklib c} { Case sensitive }
  1904. { Declaration for the standard C function strlen }
  1905. Function strlen (P : pchar) : longint; cdecl;external;
  1906. begin
  1907. Writeln (strlen('Programming is easy !'));
  1908. end.
  1909. \end{verbatim}
  1910. This program can be compiled with :
  1911. \begin{verbatim}
  1912. ppc386 prlen.pp
  1913. \end{verbatim}
  1914. Supposing, of course, that the program source resides in \file{prlen.pp}.
  1915. You cannot use procedures or functions that have a variable number of
  1916. arguments in C. Pascal doesn't support this feature of C.
  1917. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1918. % Making a shared library
  1919. \section{Making libraries}
  1920. \label{se:SharedLib}
  1921. \fpc supports making shared or static libraries in a straightforward and
  1922. easy manner.
  1923. If you want to make libraries for other \fpc programmers, you just need to
  1924. provide a command line switch. If you want C programmers to be able to use
  1925. your code as well, you will need to adapt your code a little. This process
  1926. is described first.
  1927. % Exporting functions.
  1928. \subsection{Exporting functions}
  1929. When exporting functions from a library, there are 2 things you must take in
  1930. account:
  1931. \begin{enumerate}
  1932. \item Calling conventions.
  1933. \item Naming scheme.
  1934. \end{enumerate}
  1935. The calling conventions are controlled by the modifiers \var{cdecl},
  1936. \var{popstack}, \var{pascal}, \var{stdcall}. See \sees{Calling} for more
  1937. information on the different kinds of calling scheme.
  1938. The naming conventions can be controlled by 3 modifiers:
  1939. \begin{description}
  1940. \item [cdecl:\ ] A function that has a \var{cdecl} modifier, will used
  1941. with C calling conventions, that is, the caller clears the stack. Also
  1942. the mangled name will be the name {\em exactly} as in the declaration.
  1943. \var{cdecl} is part of the function declaration, and hence must be present
  1944. both in the interface and implementation section of a unit.
  1945. \item [export:\ ] A function that has an export modifier, uses also the
  1946. exact declaration name as its mangled name. Under \windowsnt and \ostwo,
  1947. this modifier signals a function that is exported from a DLL.
  1948. The calling conventions used by a \var{export} procedure depend on the OS.
  1949. this keyword can be used only in the implementation section.
  1950. \item [Alias: ] The \var{alias} modifier can be used to give a supplementary
  1951. assembler name to your function. This doesn't modify the calling conventions
  1952. of the function.
  1953. \end{description}
  1954. If you want to make your procedures and functions available to C
  1955. programmers, you can do this very easily. All you need to do is declare the
  1956. functions and procedures that you want to make available as \var{export}, as
  1957. follows:
  1958. \begin{verbatim}
  1959. Procedure ExportedProcedure; export;
  1960. \end{verbatim}
  1961. {\em Remark :} You can only declare a function as exported in the
  1962. \var{Implementation} section of a unit. This function may {\em not} appear
  1963. in the interface part of a unit. This is logical, since a Pascal routine
  1964. cannot call an exported function, anyway.
  1965. However, the generated object file will not contain the name of the function
  1966. as you declared it. The \fpc compiler ''mangles'' the name you give your
  1967. function. It makes the name all-uppercase, and adds the types of all
  1968. parameters to it. There are cases when you want to provide a mangled name
  1969. without changing the calling convention. In such cases, you can use the
  1970. \var{Alias} modifier.
  1971. The \var{Alias} modifier allows you to specify
  1972. another name (a nickname) for your function or procedure.
  1973. The prototype for an aliased function or procedure is as follows :
  1974. \begin{verbatim}
  1975. Procedure AliasedProc; [ Alias : 'AliasName'];
  1976. \end{verbatim}
  1977. The procedure \var{AliasedProc} will also be known as \var{AliasName}. Take
  1978. care, the name you specify is case sensitive (as C is).
  1979. {\em Remark: }
  1980. If you use in your unit functions that are in other units, or
  1981. system functions, then the C program will need to link in the object files
  1982. from the units too.
  1983. % Exporting variable.
  1984. \subsection{Exporting variables}
  1985. Similarly as when you export functions, you can export variables.
  1986. when exportig variables, one should only consider the names of the
  1987. variables. To declare a variable that should be used by a C program,
  1988. one declares it with the \var{cvar} modifier:
  1989. \begin{verbatim}
  1990. Var MyVar : MyTpe; cvar;
  1991. \end{verbatim}
  1992. This will tell the compiler that the assembler name of the variable (the one
  1993. which is used by C programs) should be exactly as specified in the
  1994. declaration, i.e., case sensitive.
  1995. It is not allowed to declare multiple variables as \var{cvar} in one
  1996. statement, i.e. the following code will produce an error:
  1997. \begin{verbatim}
  1998. var Z1,Z2 : longint;cvar;
  1999. \end{verbatim}
  2000. % Compiling libraries
  2001. \subsection {Compiling libraries}
  2002. Once you have your (adapted) code, with exported and other functions,
  2003. you can compile your unit, and tell the compiler to make it into a library.
  2004. The compiler will simply compile your unit, and perform the necessary steps
  2005. to transform it into a \var{static} or \var{shared} (\var{dynamical}) library.
  2006. You can do this as follows, for a dynamical library:
  2007. \begin{verbatim}
  2008. ppc386 -CD myunit
  2009. \end{verbatim}
  2010. On \linux this will leave you with a file \file{libmyunit.so}. On \windows
  2011. and \ostwo, this will leave you with \file{myunit.dll}.
  2012. If you want a static library, you can do
  2013. \begin{verbatim}
  2014. ppc386 -CS myunit
  2015. \end{verbatim}
  2016. This will leave you with \file{libmyunit.a} and a file \file{myunit.ppu}.
  2017. The \file{myunit.ppu} is the unit file needed by the \fpc compiler.
  2018. The resulting files are then libraries. To make static libraries, you need
  2019. the \file{ranlib} or \var{ar} program on your system. It is standard on any
  2020. \linux system, and is provided with the \file{GCC} compiler under \dos.
  2021. For the dos distribution, a copy of ar is included in the file
  2022. \file{gnuutils.zip}.
  2023. {\em BEWARE:} This command doesn't include anything but the current unit in
  2024. the library. Other units are left out, so if you use code from other units,
  2025. you must deploy them together with your library.
  2026. % Moving units
  2027. \subsection{Moving units into a library}
  2028. You can put multiple units into a library with the \var{ppumove} command, as
  2029. follows:
  2030. \begin{verbatim}
  2031. ppumove -e ppl -o name unit1 unit2 unit3
  2032. \end{verbatim}
  2033. This will move 3 units in 1 library (called \file{libname.so} on linux,
  2034. \file{name.dll} on \windows) and it will create 3 files \file{unit1.ppl},
  2035. \file{unit2.ppl} and \file{file3.ppl}, which are unit files, but which tell
  2036. the compiler to look in library \var{name} when linking your executable.
  2037. The \var{ppumove} program has options to create statical or dynammical
  2038. libraries. It is provided with the compiler.
  2039. % unit searching
  2040. \subsection{Unit searching strategy}
  2041. When you compile a program or unit, the compiler will by
  2042. default always look for \file{.ppl} files. If it doesn't find one, it will
  2043. look for a \file{.ppu} file.
  2044. To be able to differentiate between units that have been compiled as static
  2045. or dynamic libraries, there are 2 switches:
  2046. \begin{description}
  2047. \item [-XD:\ ] This will define the symbol \var{FPC\_LINK\_DYNAMIC}
  2048. \item [-XS:\ ] This will define the symbol \var{FPC\_LINK\_STATIC}
  2049. \end{description}
  2050. Definition of one symbol will automatically undefine the other.
  2051. These two switches can be used in conjunction with the configuration file
  2052. \file{ppc386.cfg}. The existence of one of these symbols can be used to
  2053. decide which unit search path to set. For example:
  2054. \begin{verbatim}
  2055. # Set unit paths
  2056. #IFDEF FPC_LINK_STATIC
  2057. -Up/usr/lib/fpc/linuxunits/staticunits
  2058. #ENDIF
  2059. #IFDEF FPC_LINK_DYNAMIC
  2060. -Up/usr/lib/fpc/linuxunits/sharedunits
  2061. #ENDIF
  2062. \end{verbatim}
  2063. With such a configuration file, the compiler will look for it's units in
  2064. different directories, depending on whether \var{-XD} or \var{-XS} is used.
  2065. \section{Using smart linking}
  2066. \label{se:SmartLinking}
  2067. You can compile your units using smart linking. When you use smartl linking,
  2068. the compiler creates a series of code blocks that are as small as possible,
  2069. i.e. a code block will contain only the code for one procedure or function.
  2070. When you compile a program that uses a smart-linked unit, the compiler will
  2071. only link in the code that you actually need, and will leave out all other
  2072. code. This will result in a smaller binary, which is loaded in memory
  2073. faster, thus speeding up execution.
  2074. To enable smartlinking, one can give the smartlink option on the command
  2075. line : \var{-Cx}, or one can put the \var{\{\$SMARTLINK ON\}} directive in
  2076. the unit file:
  2077. \begin{verbatim}
  2078. Unit Testunit
  2079. {SMARTLINK ON}
  2080. Interface
  2081. ...
  2082. \end{verbatim}
  2083. Smartlinking will slow down the compilation process, expecially for large
  2084. units.
  2085. When a unit \file{foo.pp} is smartlinked, the name of the codefile is
  2086. changed to \file{libfoo.a}.
  2087. Technically speaking, the compiler makes small assembler files for each
  2088. procedure and function in the unit, as well as for all global defined
  2089. variables (whether they're in the interface section or not). It then
  2090. assembles all these small files, and uses \file{ar} to collect the resulting
  2091. object fioles in one archive.
  2092. Smartlinking and the creation of shared (or dynamic) libraries are mutually
  2093. exclusive, that is, if you turn on smartlinking, then the creation of shared
  2094. libraries is turned of. The creation of static libraries is still possible.
  2095. The reason for this is that it has little sense in making a smarlinked
  2096. dynamica library. The whole shared library is loaded into memory anyway by
  2097. the dynamic linker (or \windowsnt), so there would be no gain in size by
  2098. making it smartinked.
  2099. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2100. % Objects
  2101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2102. \chapter{Objects}
  2103. \label{ch:Objects}
  2104. In this short chapter we give some technical things about objects. For
  2105. instructions on how to use and declare objects, see \refref.
  2106. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2107. % Constructor and Destructor calls.
  2108. \section{Constructor and Destructor calls}
  2109. \label{se:ConsDest}
  2110. When using objects that need virtual methods, the compiler uses two help
  2111. procedures that are in the run-time library. They are called
  2112. \var{Help\_Destructor} and \var{Help\_Constructor}, and they are written in
  2113. assembly language. They are used to allocate the necessary memory if needed,
  2114. and to insert the Virtual Method Table (VMT) pointer in the newly allocated
  2115. object.
  2116. When the compiler encounters a call to an object's constructor,
  2117. it sets up the stack frame for the call, and inserts a call to the
  2118. \var{Help\_Constructor}
  2119. procedure before issuing the call to the real constructor.
  2120. The helper procedure allocates the needed memory (if needed) and inserts the
  2121. VMT pointer in the object. After that, the real constructor is called.
  2122. A call to \var{Help\_Destructor} is inserted in every destructor declaration,
  2123. just before the destructor's exit sequence.
  2124. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2125. % memory storage of Objects
  2126. \section{Memory storage of objects}
  2127. \label{se:ObjMemory}
  2128. Objects are stored in memory just as ordinary records with an extra field :
  2129. a pointer to the Virtual Method Table (VMT). This field is stored first, and
  2130. all fields in the object are stored in the order they are declared.
  2131. This field is initialized by the call to the object's \var{Constructor} method.
  2132. If the object you defined has no virtual methods, then a \var{nil} is stored
  2133. in the VMT pointer. This ensures that the size of objects is equal, whether
  2134. they have virtual methods ore not.
  2135. The memory allocated looks as in \seet{ObjMem}.
  2136. \begin{FPCltable}{ll}{Object memory layout}{ObjMem} \hline
  2137. Offset & What \\ \hline
  2138. +0 & Pointer to VMT. \\
  2139. +4 & Data. All fields in the order the've been declared. \\
  2140. ... & \\
  2141. \hline
  2142. \end{FPCltable}
  2143. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2144. % The virtual method table.
  2145. \section{The Virtual Method Table}
  2146. \label{se:VMT}
  2147. The Virtual Method Table (VMT) for each object type consists of 2 check
  2148. fields (containing the size of the data), a pointer to the object's anchestor's
  2149. VMT (\var{Nil} if there is no anchestor), and then the pointers to all virtual
  2150. methods. The VMT layout is illustrated in \seet{VMTMem}.
  2151. The VMT is constructed by the compiler. Every instance of an object receives
  2152. a pointer to its VMT.
  2153. \begin{FPCltable}{ll}{Virtual Method Table memory layout}{VMTMem} \hline
  2154. Offset & What \\ \hline
  2155. +0 & Size of object type data \\
  2156. +4 & Minus the size of object type data. Enables determining of valid VMT
  2157. pointers. \\
  2158. +8 & Pointer to ancestor VMT, \var{Nil} if no ancestor available.\\
  2159. +12 & Pointers to the virtual methods. \\
  2160. ... & \\
  2161. \hline
  2162. \end{FPCltable}
  2163. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2164. % Generated code
  2165. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2166. \chapter{Generated code}
  2167. \label{ch:GenCode}
  2168. The \fpc compiler relies on the assembler to make object files. It generates
  2169. just the assembly language file. In the following two sections, we discuss
  2170. what is generated when you compile a unit or a program.
  2171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2172. % Units
  2173. \section{Units}
  2174. \label{se:Units}
  2175. When you compile a unit, the \fpc compiler generates 2 files :
  2176. \begin{enumerate}
  2177. \item A unit description file (with extension \file{.ppu}, or \file{.ppw on
  2178. \windowsnt}).
  2179. \item An assembly language file (with extension \file{.s}).
  2180. \end{enumerate}
  2181. The assembly language file contains the actual source code for the
  2182. statements in your unit, and the necessary memory allocations for any
  2183. variables you use in your unit. This file is converted by the assembler to
  2184. an object file (with extension \file{.o}) which can then be linked to other
  2185. units and your program, to form an executable.
  2186. By default (compiler version 0.9.4 and up), the assembly file is removed
  2187. after it has been compiled. Only in the case of the \var{-s} command-line
  2188. option, the assembly file must be left on disk, so the assembler can be
  2189. called later. You can disable the erasing of the assembler file with the
  2190. \var{-a} switch.
  2191. The unit file contains all the information the compiler needs to use the
  2192. unit:
  2193. \begin{enumerate}
  2194. \item Other used units, both in interface and implementation.
  2195. \item Types and variables from the interface section of the unit.
  2196. \item Function declarations from the interface section of the unit.
  2197. \item Some debugging information, when compiled with debugging.
  2198. \item A date and time stamp.
  2199. \end{enumerate}
  2200. Macros, symbols and compiler directives are {\em not} saved to the unit
  2201. description file. Aliases for functions are also not written to this file,
  2202. which is logical, since they cannot appear in the interface section of a
  2203. unit.
  2204. The detailed contents and structure of this file are described in the first
  2205. appendix. You can examine a unit description file using the \file{dumpppu}
  2206. program, which shows the contents of the file.
  2207. If you want to distribute a unit without source code, you must provide both
  2208. the unit description file and the object file.
  2209. You can also provide a C header file to go with the object file. In that
  2210. case, your unit can be used by someone who wishes to write his programs in
  2211. C. However, you must make this header file yourself since the \fpc compiler
  2212. doesn't make one for you.
  2213. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2214. % Programs
  2215. \section{Programs}
  2216. \label{se:Programs}
  2217. When you compile a program, the compiler produces again 2 files :
  2218. \begin{enumerate}
  2219. \item An assembly language file containing the statements of your program,
  2220. and memory allocations for all used variables.
  2221. \item A linker response file. This file contains a list of object files the
  2222. linker must link together.
  2223. \end{enumerate}
  2224. The link response file is, by default, removed from the disk. Only when you
  2225. specify the \var{-s} command-line option or when linking fails, then the ile
  2226. is left on the disk. It is named \file{link.res}.
  2227. The assembly language file is converted to an object file by the assembler,
  2228. and then linked together with the rest of the units and a program header, to
  2229. form your final program.
  2230. The program header file is a small assembly program which provides the entry
  2231. point for the program. This is where the execution of your program starts,
  2232. so it depends on the operating system, because operating systems pass
  2233. parameters to executables in wildly different ways.
  2234. It's name is \file{prt0.o}, and the
  2235. source file resides in \file{prt0.s} or some variant of this name. It
  2236. usually resided where the system unit source for your system resides.
  2237. It's main function is to save the environment and command-line arguments,
  2238. set up the stack. Then it calls the main program.
  2239. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2240. % MMX Support
  2241. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2242. \chapter{Intel MMX support}
  2243. \label{ch:MMXSupport}
  2244. \section{What is it about ?}
  2245. \label{se:WhatisMMXabout}
  2246. \fpc supports the new MMX (Multi-Media extensions)
  2247. instructions of Intel processors. The idea of MMX is to
  2248. process multiple data with one instruction, for example the processor
  2249. can add simultaneously 4 words. To implement this efficiently, the
  2250. Pascal language needs to be extended. So Free Pascal allows
  2251. to add for example two \var{array[0..3] of word},
  2252. if MMX support is switched on. The operation is done
  2253. by the \var{MMX} unit and allows people without assembler knowledge to take
  2254. advantage of the MMX extensions.
  2255. Here is an example:
  2256. \begin{verbatim}
  2257. uses
  2258. MMX; { include some predefined data types }
  2259. const
  2260. { tmmxword = array[0..3] of word;, declared by unit MMX }
  2261. w1 : tmmxword = (111,123,432,4356);
  2262. w2 : tmmxword = (4213,63456,756,4);
  2263. var
  2264. w3 : tmmxword;
  2265. l : longint;
  2266. begin
  2267. if is_mmx_cpu then { is_mmx_cpu is exported from unit mmx }
  2268. begin
  2269. {$mmx+} { turn mmx on }
  2270. w3:=w1+w2;
  2271. {$mmx-}
  2272. end
  2273. else
  2274. begin
  2275. for i:=0 to 3 do
  2276. w3[i]:=w1[i]+w2[i];
  2277. end;
  2278. end.
  2279. \end{verbatim}
  2280. \section{Saturation support}
  2281. \label{se:SaturationSupport}
  2282. One important point of MMX is the support of saturated operations.
  2283. If a operation would cause an overflow, the value stays at the
  2284. highest or lowest possible value for the data type:
  2285. If you use byte values you get normally 250+12=6. This is very
  2286. annoying when doing color manipulations or changing audio samples,
  2287. when you have to do a word add and check if the value is greater than
  2288. 255. The solution is saturation: 250+12 gives 255.
  2289. Saturated operations are supported by the \var{MMX} unit. If you
  2290. want to use them, you have simple turn the switch saturation on:
  2291. \var{\$saturation+}
  2292. Here is an example:
  2293. \begin{verbatim}
  2294. Program SaturationDemo;
  2295. {
  2296. example for saturation, scales data (for example audio)
  2297. with 1.5 with rounding to negative infinity
  2298. }
  2299. var
  2300. audio1 : tmmxword;
  2301. const
  2302. helpdata1 : tmmxword = ($c000,$c000,$c000,$c000);
  2303. helpdata2 : tmmxword = ($8000,$8000,$8000,$8000);
  2304. begin
  2305. { audio1 contains four 16 bit audio samples }
  2306. {$mmx+}
  2307. { convert it to $8000 is defined as zero, multiply data with 0.75 }
  2308. audio1:=tmmxfixed16(audio1+helpdata2)*tmmxfixed(helpdata1);
  2309. {$saturation+}
  2310. { avoid overflows (all values>$7fff becomes $ffff) }
  2311. audio1:=(audio1+helpdata2)-helpdata2;
  2312. {$saturation-}
  2313. { now mupltily with 2 and change to integer }
  2314. audio1:=(audio1 shl 1)-helpdata2;
  2315. {$mmx-}
  2316. end.
  2317. \end{verbatim}
  2318. \section{Restrictions of MMX support}
  2319. \label{se:MMXrestrictions}
  2320. In the beginning of 1997 the MMX instructions were introduced in the
  2321. Pentium processors, so multitasking systems wouldn't save the
  2322. newly introduced MMX registers. To work around that problem, Intel
  2323. mapped the MMX registers to the FPU register.
  2324. The consequence is that
  2325. you can't mix MMX and floating point operations. After using
  2326. MMX operations and before using floating point operations, you
  2327. have to call the routine \var{EMMS} of the \var{MMX} unit.
  2328. This routine restores the FPU registers.
  2329. {\em careful:} The compiler doesn't warn if you mix floating point and
  2330. MMX operations, so be careful.
  2331. The MMX instructions are optimized for multi media (what else?).
  2332. So it isn't possible to perform each operation, some opertions
  2333. give a type mismatch, see section \ref {se:SupportedMMX} for the supported
  2334. MMX operations
  2335. An important restriction is that MMX operations aren't range or overflow
  2336. checked, even when you turn range and overflow checking on. This is due to
  2337. the nature of MMX operations.
  2338. The \var{MMX} unit must be always used when doing MMX operations
  2339. because the exit code of this unit clears the MMX unit. If it wouldn't do
  2340. that, other program will crash. A consequence of this is that you can't use
  2341. MMX operations in the exit code of your units or programs, since they would
  2342. interfere with the exit code of the \var{MMX} unit. The compiler can't
  2343. check this, so you are responsible for this !
  2344. \section{Supported MMX operations}
  2345. \label{se:SupportedMMX}
  2346. \section{Optimizing MMX support}
  2347. \label{se:OptimizingMMX}
  2348. Here are some helpful hints to get optimal performance:
  2349. \begin{itemize}
  2350. \item The \var{EMMS} call takes a lot of time, so try to seperate floating
  2351. point and MMX operations.
  2352. \item Use MMX only in low level routines because the compiler
  2353. saves all used MMX registers when calling a subroutine.
  2354. \item The NOT-operator isn't supported natively by MMX, so the
  2355. compiler has to generate a workaround and this operation
  2356. is inefficient.
  2357. \item Simple assignements of floating point numbers don't access
  2358. floating point registers, so you need no call to the \var{EMMS}
  2359. procedure. Only when doing arithmetic, you need to call the \var{EMMS}
  2360. procedure.
  2361. \end{itemize}
  2362. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2363. % Memory issues
  2364. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2365. \chapter{Memory issues}
  2366. \label{ch:Memory}
  2367. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2368. % The 32-bit model
  2369. \section{The 32-bit model.}
  2370. \label{se:ThirtytwoBit}
  2371. The \fpc Pascal compiler issues 32-bit code. This has several consequences:
  2372. \begin{itemize}
  2373. \item You need a 386 processor to run the generated code. The
  2374. compiler functions on a 286 when you compile it using Turbo Pascal,
  2375. but the generated programs cannot be assembled or executed.
  2376. \item You don't need to bother with segment selectors. Memory can be
  2377. addressed using a single 32-bit pointer.
  2378. The amount of memory is limited only by the available amount of (virtual)
  2379. memory on your machine.
  2380. \item The structures you define are unlimited in size. Arrays can be as long
  2381. as you want. You can request memory blocks from any size.
  2382. \end{itemize}
  2383. The fact that 32-bit code is used, means that some of the older Turbo Pascal
  2384. constructs and functions are obsolete. The following is a list of functions
  2385. which shouldn't be used anymore:
  2386. \begin{description}
  2387. \item [Seg()] : Returned the segment of a memory address. Since segments have
  2388. no more meaning, zero is returned in the \fpc run-time library implementation of
  2389. \var{Seg}.
  2390. \item [Ofs()] : Returned the offset of a memory address. Since segments have
  2391. no more meaning, the complete address is returned in the \fpc implementation
  2392. of this function. This has as a consequence that the return type is
  2393. \var{Longint} instead of \var{Word}.
  2394. \item [Cseg(), Dseg()] : Returned, respectively, the code and data segments
  2395. of your program. This returns zero in the \fpc implementation of the
  2396. system unit, since both code and data are in the same memory space.
  2397. \item [Ptr] accepted a segment and offset from an address, and would return
  2398. a pointer to this address. This has been changed in the run-time library.
  2399. Standard it returns now simply the offset. If you want to retain the old
  2400. functionality, you can recompile the run-time library with the
  2401. \var{DoMapping} symbol defined. This will restore the Turbo Pascal
  2402. behaviour.
  2403. \item [memw and mem] these arrays gave access to the \dos memory. \fpc
  2404. supports them, they are mapped into \dos memory space. You need the
  2405. \var{GO32} unit for this.
  2406. \end{description}
  2407. You shouldn't use these functions, since they are very non-portable, they're
  2408. specific to \dos and the ix86 processor. The \fpc compiler is designed to be
  2409. portable to other platforms, so you should keep your code as portable as
  2410. possible, and not system specific. That is, unless you're writing some driver
  2411. units, of course.
  2412. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2413. % The stack
  2414. \section{The stack}
  2415. \label{se:Stack}
  2416. The stack is used to pass parameters to procedures or functions,
  2417. to store local variables, and, in some cases, to return function
  2418. results.
  2419. When a function or procedure is called, then the following is done by the
  2420. compiler :
  2421. \begin{enumerate}
  2422. \item If there are any parameters to be passed to the procedure, they are
  2423. pushed from right to left on the stack.
  2424. \item If a function is called that returns a variable of type \var{String},
  2425. \var{Set}, \var{Record}, \var{Object} or \var{Array}, then an address to
  2426. store the function result in, is pushed on the stack.
  2427. \item If the called procedure or function is an object method, then the
  2428. pointer to \var{self} is pushed on the stack.
  2429. \item If the procedure or function is nested in another function or
  2430. procedure, then the frame pointer of the parent procedure is pushed on the
  2431. stack.
  2432. \item The return address is pushed on the stack (This is done automatically
  2433. by the instruction which calls the subroutine).
  2434. \end{enumerate}
  2435. The resulting stack frame upon entering looks as in \seet{StackFrame}.
  2436. \begin{FPCltable}{llc}{Stack frame when calling a procedure}{StackFrame}
  2437. \hline
  2438. Offset & What is stored & Optional ? \\ \hline
  2439. +x & parameters & Yes \\
  2440. +12 & function result & Yes \\
  2441. +8 & self & Yes \\
  2442. +4 & Frame pointer of parent procedure & Yes \\
  2443. +0 & Return address & No\\ \hline
  2444. \end{FPCltable}
  2445. \subsection{ Intel x86 version }
  2446. The stack is cleared with the \var{ret} I386 instruction, meaning that the
  2447. size of all pushed parameters is limited to 64K.
  2448. \subsubsection{ DOS }
  2449. Under the DOS targets , the default stack is set to 256Kb. This value
  2450. cannot be modified for the GO32V1 target. But this can be modified
  2451. with the GO32V2 target using a special DJGPP utility \var{stubedit}.
  2452. It is to note that the stack size may be changed with some compiler
  2453. switches, this stack size, if \emph{greater} then the default stack
  2454. size will be used instead, otherwise the default stack size is used.
  2455. \subsubsection{ Linux }
  2456. Under Linux, stack size is only limited by the available memory by
  2457. the system.
  2458. \subsubsection{ OS/2 }
  2459. Under OS/2, stack size is determined by one of the runtime
  2460. environment variables set for EMX. Therefore, the stack size
  2461. is user defined.
  2462. \subsection{ Motorola 680x0 version }
  2463. All depending on the processor target, the stack can be cleared in two
  2464. manners, if the target processor is a MC68020 or higher, the stack will
  2465. be cleared with a simple \var{rtd} instruction, meaning that the size
  2466. of all pushed parameters is limited to 32K.
  2467. Otherwise on MC68000/68010 processors, the stack clearing mechanism
  2468. is sligthly more complicated, the exit code will look like this:
  2469. \begin{verbatim}
  2470. {
  2471. move.l (sp)+,a0
  2472. add.l paramsize,a0
  2473. move.l a0,-(sp)
  2474. rts
  2475. }
  2476. \end{verbatim}
  2477. \subsubsection{ Amiga }
  2478. Under AmigaOS, stack size is determined by the user, which sets this
  2479. value using the stack program. Typical sizes range from 4K to 40K.
  2480. \subsubsection{ Atari }
  2481. Under Atari TOS, stack size is currently limited to 8K, and it cannot
  2482. be modified. This may change in a future release of the compiler.
  2483. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2484. % The heap
  2485. \section{The heap}
  2486. \label{se:Heap}
  2487. The heap is used to store all dynamic variables, and to store class
  2488. instances. The interface to the heap is the same as in Turbo Pascal,
  2489. although the effects are maybe not the same. On top of that, the \fpc
  2490. run-time library has some extra possibilities, not available in Turbo
  2491. Pascal. These extra possibilities are explained in the next subsections.
  2492. % The heap grows
  2493. \subsection{The heap grows}
  2494. \fpc supports the \var{HeapEerror} procedural variable. If this variable is
  2495. non-nil, then it is called in case you try to allocate memory, and the heap
  2496. is full. By default, \var{HeapError} points to the \var{GrowHeap} function,
  2497. which tries to increase the heap.
  2498. The growheap function issues a system call to try to increase the size of the
  2499. memory available to your program. It first tries to increase memory in a 1 Mb.
  2500. chunk. If this fails, it tries to increase the heap by the amount you
  2501. requested from the heap.
  2502. If the call to \var{GrowHeap} has failed, then a run-time error is generated,
  2503. or nil is returned, depending on the \var{GrowHeap} result.
  2504. If the call to \var{GrowHeap} was successful, then the needed memory will be
  2505. allocated.
  2506. % Using Blocks
  2507. \subsection{Using Blocks}
  2508. If you need to allocate a lot of small block for a small period, then you
  2509. may want to recompile the run-time library with the \var{USEBLOCKS} symbol
  2510. defined. If it is recompiled, then the heap management is done in a
  2511. different way.
  2512. The run-time library keeps a linked list of allocated blocks with size
  2513. up to 256 bytes\footnote{The size can be set using the \var{max\_size}
  2514. constant in the \file{heap.inc} source file.}. By default, it keeps 32 of
  2515. these lists\footnote{The actual size is \var{max\_size div 8}.}.
  2516. When a piece of memory in a block is deallocated, the heap manager doesn't
  2517. really deallocate the occupied memory. The block is simply put in the linked
  2518. list corresponding to its size.
  2519. When you then again request a block of memory, the manager checks in the
  2520. list if there is a non-allocated block which fits the size you need (rounded
  2521. to 8 bytes). If so, the block is used to allocate the memory you requested.
  2522. This method of allocating works faster if the heap is very fragmented, and
  2523. you allocate a lot of small memory chunks.
  2524. Since it is invisible to the program, this provides an easy way of improving
  2525. the performance of the heap manager.
  2526. % The splitheap
  2527. \subsection{Using the split heap}
  2528. {\em Remark : The split heap is still somewhat buggy. Use at your own risk
  2529. for the moment.}
  2530. The split heap can be used to quickly release a lot of blocks you alloated
  2531. previously.
  2532. Suppose that in a part of your program, you allocate a lot of memory chunks
  2533. on the heap. Suppose that you know that you'll release all this memory when
  2534. this particular part of you program is finished.
  2535. In Turbo Pascal, you could foresee this, and mark the position of the heap
  2536. (using the \var{Mark} function) when entering this particular part of your
  2537. program, and release the occupied memory in one call with the \var{Release}
  2538. call.
  2539. For most purposes, this works very good. But sometimes, you may need to
  2540. allocate something on the heap that you {\em don't} want deallocated when you
  2541. release the allocated memory. That is where the split heap comes in.
  2542. When you split the heap, the heap manager keeps 2 heaps: the base heap (the
  2543. normal heap), and the temporary heap. After the call to split the heap,
  2544. memory is allocated from the temporary heap. When you're finished using all
  2545. this memory, you unsplit the heap. This clears all the memory on the split
  2546. heap with one call. After that, memory will be allocated from the base heap
  2547. again.
  2548. So far, nothing special, nothing that can't be done with calls to \var{mark}
  2549. and \var{release}. Suppose now that you have split the heap, and that you've
  2550. come to a point where you need to allocate memory that is to stay allocated
  2551. after you unsplit the heap again. At this point, mark and release are of no
  2552. use. But when using the split heap, you can tell the heap manager to
  2553. --temporarily-- use the base heap again to allocate memory.
  2554. When you've allocated the needed memory, you can tell the heap manager that
  2555. it should start using the temporary heap again.
  2556. When you're finished using the temporary heap, you release it, and the
  2557. memory you allocated on the base heap will still be allocated.
  2558. To use the split-heap, you must recompile the run-time library with the \var{TempHeap}
  2559. symbol defined.
  2560. This means that the following functions are available :
  2561. \begin{verbatim}
  2562. procedure Split_Heap;
  2563. procedure Switch_To_Base_Heap;
  2564. procedure Switch_To_Temp_Heap;
  2565. procedure Switch_Heap;
  2566. procedure ReleaseTempHeap;
  2567. procedure GetempMem(var p : pointer;size : longint);
  2568. \end{verbatim}
  2569. \var{split\_heap} is used to split the heap. It cannot be called two times
  2570. in a row, without a call to \var{releasetempheap}. \var{Releasetempheap}
  2571. completely releases the memory used by the temporary heap.
  2572. Switching temporarily back to the base heap can be done using the
  2573. \var{switch\_to\_base\_heap} call, and returning to the temporary heap is done
  2574. using the \var{switch\_to\_temp\_heap} call. Switching from one to the other
  2575. without knowing on which one your are right now, can be done using the
  2576. \var{switch\_heap} call, which will split the heap first if needed.
  2577. A call to \var{GetTempMem} will allocate a memory block on the temporary
  2578. heap, whatever the current heap is. The current heap after this call will be
  2579. the temporary heap.
  2580. Typically, what will appear in your code is the following sequence :
  2581. \begin{verbatim}
  2582. Split_Heap
  2583. ...
  2584. { Memory allocation }
  2585. ...
  2586. { !! non-volatile memory needed !!}
  2587. Switch_To_Base_Heap;
  2588. getmem (P,size);
  2589. Switch_To_Temp_Heap;
  2590. ...
  2591. {Memory allocation}
  2592. ...
  2593. ReleaseTempHeap;
  2594. {All allocated memory is now freed, except for the memory pointed to by 'P' }
  2595. ...
  2596. \end{verbatim}
  2597. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2598. % Accessing DOS memory under the GO32 extender
  2599. \section{using \dos memory under the Go32 extender}
  2600. \label{se:AccessingDosMemory}
  2601. Because \fpc is a 32 bit compiler, and uses a \dos extender, accessing DOS
  2602. memory isn't trivial. What follows is an attempt to an explanation of how to
  2603. access and use \dos or real mode memory\footnote{Thanks to an explanation of
  2604. Thomas schatzl (E-mail:\var{tom\_at\[email protected]}).}.
  2605. In {\em Proteced Mode}, memory is accessed through {\em Selectors} and
  2606. {\em Offsets}. You can think of Selectors as the protected mode
  2607. equivalents of segments.
  2608. In \fpc, a pointer is an offset into the \var{DS} selector, which points to
  2609. the Data of your program.
  2610. To access the (real mode) \dos memory, somehow you need a selector that
  2611. points to the \dos memory.
  2612. The \file{GO32} unit provides you with such a selector: The
  2613. \var{DosMemSelector} variable, as it is conveniently called.
  2614. You can also allocate memory in \dos's memory space, using the
  2615. \var{global\_dos\_alloc} function of the \file{GO32} unit.
  2616. This function will allocate memory in a place where \dos sees it.
  2617. As an example, here is a function that returns memory in real mode \dos and
  2618. returns a selector:offset pair for it.
  2619. \begin{verbatim}
  2620. procedure dosalloc(var selector : word;
  2621. var segment : word;
  2622. size : longint);
  2623. var result : longint;
  2624. begin
  2625. result := global_dos_alloc(size);
  2626. selector := word(result);
  2627. segment := word(result shr 16);
  2628. end;
  2629. \end{verbatim}
  2630. (you need to free this memory using the \var{global\_dos\_free} function.)
  2631. You can access any place in memory using a selector. You can get a selector
  2632. using the \var{allocate\_ldt\_descriptor} function, and then let this selector
  2633. point to the physical memory you want using the
  2634. \var{set\_segment\_base\_address} function, and set its length using
  2635. \var{set\_segment\_limit} function.
  2636. You can manipulate the memory pointed to by the selector using the functions
  2637. of the GO32 unit. For instance with the \var{seg\_fillchar} function.
  2638. After using the selector, you must free it again using the
  2639. \var{free\_ldt\_selector} function.
  2640. More information on all this can be found in the \unitsref, the chapter on
  2641. the \file{GO32} unit.
  2642. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2643. % Optimizations done in the compiler
  2644. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2645. \chapter{Optimizations}
  2646. \section{ Non processor specific }
  2647. The following sections describe the general optimizations
  2648. done by the compiler, they are non processor specific. Some
  2649. of these require some compiler switch override while others are done
  2650. automatically (those which require a switch will be noted as such).
  2651. \subsection{ Constant folding }
  2652. In \fpc, if the operand(s) of an operator are constants, they
  2653. will be evaluated at compile time.
  2654. Example
  2655. \begin{verbatim}
  2656. x:=1+2+3+6+5;
  2657. will generate the same code as
  2658. x:=17;
  2659. \end{verbatim}
  2660. Furthermore, if an array index is a constant, the offset will
  2661. be evaluated at compile time. This means that accessing MyData[5]
  2662. is as efficient as accessing a normal variable.
  2663. Finally, calling \var{Chr}, \var{Hi}, \var{Lo}, \var{Ord}, \var{Pred},
  2664. or \var{Succ} functions with constant parameters generates no
  2665. run-time library calls, instead, the values are evaluated at
  2666. compile time.
  2667. \subsection{ Constant merging }
  2668. Using the same constant string two or more times generates only
  2669. one copy of the string constant.
  2670. \subsection{ Short cut evaluation }
  2671. Evaluation of boolean expression stops as soon as the result is
  2672. known, which makes code execute faster then if all boolean operands
  2673. were evaluated.
  2674. \subsection{ Constant set inlining }
  2675. Using the \var{in} operator is always more efficient then using the
  2676. equivalent \verb|<>|, \verb|=|, \verb|<=|, \verb|>=|, \verb|<| and \verb|>|
  2677. operators. This is because range comparisons can be done more easily with
  2678. \var{in} then with normal comparison operators.
  2679. \subsection{ Small sets }
  2680. Sets which contain less then 33 elements can be directly encoded
  2681. using a 32-bit value, therefore no run-time library calls to
  2682. evaluate operands on these sets are required; they are directly encoded
  2683. by the code generator.
  2684. \subsection{ Range checking }
  2685. Assignments of constants to variables are range checked at compile
  2686. time, which removes the need the generation of runtime range checking
  2687. code.
  2688. \emph{Remark:} This feature was not implemented before version
  2689. 0.99.5 of \fpc.
  2690. \subsection{ Shifts instead of multiply or divide }
  2691. When one of the operands in a multiplication is a power of
  2692. two, they are encoded using arithmetic shifts instructions,
  2693. which generates more efficient code.
  2694. Similarly, if the divisor in a \var{div} operation is a power
  2695. of two, it is encoded using arithmetic shifts instructions.
  2696. The same is true when accessing array indexes which are
  2697. powers of two, the address is calculated using arithmetic
  2698. shifts instead of the multiply instruction.
  2699. \subsection{ Automatic alignment }
  2700. By default all variables larger then a byte are guaranteed to be aligned
  2701. at least on a word boundary.
  2702. Furthermore all pointers allocated using the standard runtime
  2703. library (\var{New} and \var{GetMem} among others) are guaranteed
  2704. to return pointers aligned on a quadword boundary (64-bit alignment).
  2705. Alignment of variables on the stack depends on the target processor.
  2706. \emph{ Remark: } Quadword alignment of pointers is not guaranteed
  2707. on systems which don't use an internal heap, such as for the Win32
  2708. target.
  2709. \emph{ Remark: } Alignment is also done \emph{between} fields in
  2710. records, objects and classes, this is \emph{not} the same as
  2711. in Turbo Pascal and may cause problems when using disk I/O with these
  2712. types. To get no alignment between fields use the \var{packed} directive
  2713. or the \var{\{\$PackRecords n\}} switch. For further information, take a
  2714. look at the reference manual under the \var{record} heading.
  2715. \subsection{ Smart linking }
  2716. This feature removes all unreferenced code in the final executable
  2717. file, making the executable file much smaller.
  2718. Smart linking is switched on with the \var{-Cx} command-line switch, or
  2719. using the \var{\{\$SMARTLINK ON\}} global directive.
  2720. \emph{ Remark: } Smart linking was implemented starting with
  2721. version 0.99.6 of \fpc.
  2722. \subsection{ Inline routines }
  2723. The following runtime library routines are coded directly into the
  2724. final executable : \var{Lo}, \var{Hi}, \var{High}, \var{Sizeof},
  2725. \var{TypeOf}, \var{Length}, \var{Pred}, \var{Succ}, \var{Inc},
  2726. \var{Dec} and \var{Assigned}.
  2727. \emph{ Remark: } Inline \var{Inc} and \var{Dec} were not completely
  2728. implemented until version 0.99.6 of \fpc.
  2729. \subsection{ Case optimization }
  2730. When using the \var{-O1} (or higher) switch, case statements will be
  2731. generated using a jump table if appropriate, to make them execute
  2732. faster.
  2733. \subsection{ Stack frame omission }
  2734. Under specific conditions, the stack frame (entry and exit code for
  2735. the routine, see section \ref{se:Calling}) will be omitted, and the
  2736. variable will directly be accessed via the stack pointer.
  2737. Conditions for omission of the stack frame :
  2738. \begin{itemize}
  2739. \item The function has no parameters nor local variables.
  2740. \item Routine does not call other routines.
  2741. \item Routine does not contain assembler statements. However,
  2742. a \var{assembler} routine may omit it's stack frame.
  2743. \item Routine is not declared using the \var{Interrupt} directive.
  2744. \item Routine is not a constructor or destructor.
  2745. \end{itemize}
  2746. \subsection{ Register variables }
  2747. When using the \var{-Or} switch, local variables or parameters
  2748. which are used very often will be moved to registers for faster
  2749. access.
  2750. \emph{ Remark: } Register variable allocation is currently
  2751. an experimental feature, and should be used with caution.
  2752. \subsection{ Intel x86 specific }
  2753. Here follows a listing of the opimizing techniques used in the compiler:
  2754. \begin{enumerate}
  2755. \item When optimizing for a specific Processor (\var{-Op1, -Op2, -Op3},
  2756. the following is done:
  2757. \begin{itemize}
  2758. \item In \var{case} statements, a check is done whether a jump table
  2759. or a sequence of conditional jumps should be used for optimal performance.
  2760. \item Determines a number of strategies when doing peephole optimization, e.g.:
  2761. \var{movzbl (\%ebp), \%eax} will be changed into
  2762. \var{xorl \%eax,\%eax; movb (\%ebp),\%al } for Pentium and PentiumMMX.
  2763. \end{itemize}
  2764. \item When optimizing for speed (\var{-OG}, the default) or size (\var{-Og}), a choice is
  2765. made between using shorter instructions (for size) such as \var{enter \$4},
  2766. or longer instructions \var{subl \$4,\%esp} for speed. When smaller size is
  2767. requested, things aren't aligned on 4-byte boundaries. When speed is
  2768. requested, things are aligned on 4-byte boundaries as much as possible.
  2769. \item Fast optimizations (\var{-O1}): activate the peephole optimizer
  2770. \item Slower optimizations (\var{-O2}): also activate the common subexpression
  2771. elimination (formerly called the "reloading optimizer")
  2772. \item Uncertain optimizations (\var{-Ou}): With this switch, the common subexpression
  2773. elimination algorithm can be forced into making uncertain optimizations.
  2774. Although you can enable uncertain optimizations in most cases, for people who
  2775. do not understand the following technical explanation, it might be the safest to
  2776. leave them off.
  2777. \begin{quote}
  2778. % Jonas's own words..
  2779. \em
  2780. If uncertain optimizations are enabled, the CSE algortihm assumes
  2781. that
  2782. \begin{itemize}
  2783. \item If something is written to a local/global register or a
  2784. procedure/function parameter, this value doesn't overwrite the value to
  2785. which a pointer points.
  2786. \item If something is written to memory pointed to by a pointer variable,
  2787. this value doesn't overwrite the value of a local/global variable or a
  2788. procedure/function parameter.
  2789. \end{itemize}
  2790. % end of quote
  2791. \end{quote}
  2792. The practical upshot of this is that you cannot use the uncertain
  2793. optimizations if you both write and read local or global variables directly and
  2794. through pointers (this includes \var{Var} parameters, as those are pointers too).
  2795. The following example will produce bad code when you switch on
  2796. uncertain optimizations:
  2797. \begin{verbatim}
  2798. Var temp: Longint;
  2799. Procedure Foo(Var Bar: Longint);
  2800. Begin
  2801. If (Bar = temp)
  2802. Then
  2803. Begin
  2804. Inc(Bar);
  2805. If (Bar <> temp) then Writeln('bug!')
  2806. End
  2807. End;
  2808. Begin
  2809. Foo(Temp);
  2810. End.
  2811. \end{verbatim}
  2812. The reason it produces bad code is because you access the global variable
  2813. \var{Temp} both through its name \var{Temp} and through a pointer, in this
  2814. case using the \var{Bar} variable parameter, which is nothing but a pointer
  2815. to \var{Temp} in the above code.
  2816. On the other hand, you can use the uncertain optimizations if
  2817. you access global/local variables or parameters through pointers,
  2818. and {\em only} access them through this pointer\footnote{
  2819. You can use multiple pointers to point to the same variable as well, that
  2820. doesn't matter.}.
  2821. For example:
  2822. \begin{verbatim}
  2823. Type TMyRec = Record
  2824. a, b: Longint;
  2825. End;
  2826. PMyRec = ^TMyRec;
  2827. TMyRecArray = Array [1..100000] of TMyRec;
  2828. PMyRecArray = ^TMyRecArray;
  2829. Var MyRecArrayPtr: PMyRecArray;
  2830. MyRecPtr: PMyRec;
  2831. Counter: Longint;
  2832. Begin
  2833. New(MyRecArrayPtr);
  2834. For Counter := 1 to 100000 Do
  2835. Begin
  2836. MyRecPtr := @MyRecArrayPtr^[Counter];
  2837. MyRecPtr^.a := Counter;
  2838. MyRecPtr^.b := Counter div 2;
  2839. End;
  2840. End.
  2841. \end{verbatim}
  2842. Will produce correct code, because the global variable \var{MyRecArrayPtr}
  2843. is not accessed directly, but only through a pointer (\var{MyRecPtr} in this
  2844. case).
  2845. In conclusion, one could say that you can use uncertain optimizations {\em
  2846. only} when you know what you're doing.
  2847. \end{enumerate}
  2848. \subsection{ Motorola 680x0 specific }
  2849. Using the \var{-O2} switch does several optimizations in the
  2850. code produced, the most notable being:
  2851. \begin{itemize}
  2852. \item Sign extension from byte to long will use \var{EXTB}
  2853. \item Returning of functions will use \var{RTD}
  2854. \item Range checking will generate no run-time calls
  2855. \item Multiplication will use the long \var{MULS} instruction, no
  2856. runtime library call will be generated
  2857. \item Division will use the long \var{DIVS} instruction, no
  2858. runtime library call will be generated
  2859. \end{itemize}
  2860. \section{Optimization switches}
  2861. This is where the various optimizing switches and their actions are
  2862. described, grouped per switch.
  2863. \begin{description}
  2864. \item [-On:\ ] with n = 1..3: these switches activate the optimizer.
  2865. A higher level automatically includes all lower levels.
  2866. \begin{itemize}
  2867. \item Level 1 (\var{-O1}) activates the peephole optimizer
  2868. (common instruction sequences are replaced by faster equivalents).
  2869. \item Level 2 (\var{-O2}) enables the assembler data flow analyzer,
  2870. which allows the common subexpression elimination procedure to
  2871. remove unnecessary reloads of registers with values they already contain.
  2872. \item Level 3 (\var{-O3}) enables uncertain optimizations. For more info, see -Ou.
  2873. \end{itemize}
  2874. \item[-OG:\ ]
  2875. This causes the code generator (and optimizer, IF activated), to favor
  2876. faster, but code-wise larger, instruction sequences (such as
  2877. "\verb|subl $4,%esp|") instead of slower, smaller instructions
  2878. ("\verb|enter $4|"). This is the default setting.
  2879. \item[-Og:\ ] This one is exactly the reverse of -OG, and as such these
  2880. switches are mutually exclusive: enabling one will disable the other.
  2881. \item[-Or:\ ] this setting (once it's fixed) causes the code generator to
  2882. check which variables are used most, so it can keep those in a register.
  2883. \item[-Opn:\ ] with n = 1..3: setting the target processor does NOT
  2884. activate the optimizer. It merely influences the code generator and,
  2885. if activated, the optimizer:
  2886. \begin{itemize}
  2887. \item During the code generation process, this setting is used to
  2888. decide whether a jump table or a sequence of successive jumps provides
  2889. the best performance in a case statement.
  2890. \item The peephole optimizer takes a number of decisions based on this
  2891. setting, for example it translates certain complex instructions, such
  2892. as
  2893. \begin{verbatim}
  2894. movzbl (mem), %eax|
  2895. \end{verbatim}
  2896. to a combination of simpler instructions
  2897. \begin{verbatim}
  2898. xorl %eax, %eax
  2899. movb (mem), %al
  2900. \end{verbatim}
  2901. for the Pentium.
  2902. \end{itemize}
  2903. \item[-Ou:\ ] This enables uncertain optimizations. You cannot use these
  2904. always, however. The previous section explains when they can be used, and
  2905. when they cannot be used.
  2906. \end{description}
  2907. \section{Tips to get faster code}
  2908. Here some general tips for getting better code are presented. They
  2909. mainly concern coding style.
  2910. \begin{itemize}
  2911. \item Find a better algorithm. No matter how much you and the compiler
  2912. tweak the code, a quicksort will (almost) always outperform a bubble
  2913. sort, for example.
  2914. \item Use variables of the native size of the processor you're writing
  2915. for. For the 80x86 and compatibles, this is 32 bit, so you're best of
  2916. using longint and cardinal variables.
  2917. \item Turn on the optimizer.
  2918. \item Write your if/then/else statements so that the code in the "then"-part
  2919. gets executed most of the time (improves the rate of successful jump prediction).
  2920. \item If you are allocating and disposing a lot of small memory blocks,
  2921. check out the heapblocks variable (heapblocks are on by default from
  2922. release 0.99.8 and later)
  2923. \item Profile your code (see the -pg switch) to find out where the
  2924. bottlenecks are. If you want, you can rewrite those parts in assembler.
  2925. You can take the code generated by the compiler as a starting point. When
  2926. given the \var{-a} command-line switch, the compiler will not erase the
  2927. assembler file at the end of the assembly process, so you can study the
  2928. assembler file.
  2929. {\em Note:} Code blocks which contain an assembler block, are not processed
  2930. at all by the optimizer at this time. Update: as of versino 0.99.11, the Pascal
  2931. code surrounding the assembler blocks is optimized.
  2932. \end{itemize}
  2933. \section{ Floating point }
  2934. This is where can be found processor specific information on Floating
  2935. point code generated by the compiler.
  2936. \subsection{ Intel x86 specific }
  2937. All normal floating point types map to their real type, including
  2938. \var{comp} and \var{extended}.
  2939. \subsection{ Motorola 680x0 specific }
  2940. Early generations of the Motorola 680x0 processors did not have integrated
  2941. floating point units, so to circumvent this fact, all floating point
  2942. operations are emulated (when the \var{\$E+} switch ,which is the default)
  2943. using the IEEE \var{Single} floating point type. In other words when
  2944. emulation is on, Real, Single, Double and Extended all map to the
  2945. \var{single} floating point type.
  2946. When the \var{\$E} switch is turned off, normal 68882/68881/68040
  2947. floating point opcodes are emitted. The Real type still maps to
  2948. \var{Single} but the other types map to their true floating point
  2949. types. Only basic FPU opcodes are used, which means that it can
  2950. work on 68040 processors correctly.
  2951. \emph{ Remark: } \var{Double} and \var{Extended} types in true floating
  2952. point mode have not been extensively tested as of version 0.99.5.
  2953. \emph{ Remark: } The \var{comp} data type is currently not supported.
  2954. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2955. % Appendices
  2956. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2957. \appendix
  2958. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2959. % Appendix A
  2960. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2961. \chapter{Anatomy of a unit file}
  2962. \label{ch:AppA}
  2963. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2964. % Basics
  2965. \section{Basics}
  2966. The best and most updated documentation about the ppu files can be found
  2967. in \file{ppu.pas} and \file{ppudump.pp} which can be found in
  2968. \file{rtl/utils/}.
  2969. To read or write the ppufile, you can use the ppu unit \file{ppu.pas}
  2970. which has an object called tppufile which holds all routines that deal
  2971. with ppufile handling. Describing the layout of a ppufile, the methods
  2972. which can be used for it are described.
  2973. A unit file consists of basically five or six parts:
  2974. \begin{enumerate}
  2975. \item A unit header.
  2976. \item A file interface part.
  2977. \item A definition part. Contains all type and procedure definitions.
  2978. \item A symbol part. Contains all symbol names and references to their
  2979. definitions.
  2980. \item A browser part. Contains all references from this unit to other
  2981. units and inside this unit. Only available when the \var{uf\_has\_browser} flag is
  2982. set in the unit flags
  2983. \item A file implementation part (currently unused).
  2984. implementation part.
  2985. \end{enumerate}
  2986. \section{reading ppufiles}
  2987. We will first create an object ppufile which will be used below. We are
  2988. opening unit \file{test.ppu} as an example.
  2989. \begin{verbatim}
  2990. var
  2991. ppufile : pppufile;
  2992. begin
  2993. { Initialize object }
  2994. ppufile:=new(pppufile,init('test.ppu');
  2995. { open the unit and read the header, returns false when it fails }
  2996. if not ppufile.open then
  2997. error('error opening unit test.ppu');
  2998. { here we can read the unit }
  2999. { close unit }
  3000. ppufile.close;
  3001. { release object }
  3002. dispose(ppufile,done);
  3003. end;
  3004. \end{verbatim}
  3005. Note: When a function fails (for example not enough bytes left in an
  3006. entry) it sets the \var{ppufile.error} variable.
  3007. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3008. % The Header
  3009. \section{The Header}
  3010. The header consists of a record containing 24 bytes:
  3011. \begin{verbatim}
  3012. tppuheader=packed record
  3013. id : array[1..3] of char; { = 'PPU' }
  3014. ver : array[1..3] of char;
  3015. compiler : word;
  3016. cpu : word;
  3017. target : word;
  3018. flags : longint;
  3019. size : longint; { size of the ppufile without header }
  3020. checksum : longint; { checksum for this ppufile }
  3021. end;
  3022. \end{verbatim}
  3023. The header is already read by the \var{ppufile.open} command.
  3024. You can access all fields using \var{ppufile.header} which holds
  3025. the current header record.
  3026. \begin{tabular}{lp{10cm}}
  3027. \raggedright
  3028. field & description \\ \hline
  3029. \var{id} &
  3030. this is allways 'PPU', can be checked with
  3031. \mbox{\var{function ppufile.CheckPPUId:boolean;}} \\
  3032. \var{ver} & ppu version, currently '015', can be checked with
  3033. \mbox{\var{function ppufile.GetPPUVersion:longint;}} (returns 15) \\
  3034. \var{compiler}
  3035. & compiler version used to create the unit. Doesn't contain the
  3036. patchlevel. Currently 0.99 where 0 is the high byte and 99 the
  3037. low byte \\
  3038. \var{cpu} & cpu for which this unit is created.
  3039. 0 = i386
  3040. 1 = m68k \\
  3041. \var{target} & target for which this unit is created, this depends also on the
  3042. cpu!
  3043. For i386:
  3044. \begin{tabular}[t]{ll}
  3045. 0 & Go32v1 \\
  3046. 1 & Go32V2 \\
  3047. 2 & Linux-i386 \\
  3048. 3 & OS/2 \\
  3049. 4 & Win32
  3050. \end{tabular}
  3051. For m68k:
  3052. \begin{tabular}[t]{ll}
  3053. 0 & Amiga \\
  3054. 1 & Mac68k \\
  3055. 2 & Atari \\
  3056. 3 & Linux-m68k
  3057. \end{tabular} \\
  3058. \var{flag} &
  3059. the unit flags, contains a combination of the uf\_ constants which
  3060. are definied in \file{ppu.pas} \\
  3061. \var{size} & size of this unit without this header \\
  3062. \var{checksum} &
  3063. checksum of the interface parts of this unit, which determine if
  3064. a unit is changed or not, so other units can see if they need to
  3065. be recompiled
  3066. \\ \hline
  3067. \end{tabular}
  3068. % The sections
  3069. \section{The sections}
  3070. After this header follow the sections. All sections work the same!
  3071. A section contains of entries and is ended with also an entry, but
  3072. containing the specific ibend constant (see \file{ppu.pas} for a list).
  3073. Each entry starts with an entryheader.
  3074. \begin{verbatim}
  3075. tppuentry=packed record
  3076. id : byte;
  3077. nr : byte;
  3078. size : longint;
  3079. end;
  3080. \end{verbatim}
  3081. \begin{tabular}{lp{10cm}}
  3082. field & Description \\ \hline
  3083. id & this is 1 or 2 and can be check if it the entry is correctly
  3084. found. 1 means its a main entry, which says that it is part of the
  3085. basic layout as explained before. 2 toggles that it it a sub entry
  3086. of a record or object \\
  3087. nr & contains the ib constant number which determines what kind of
  3088. entry it is \\
  3089. size & size of this entry without the header, can be used to skip entries
  3090. very easily. \\ \hline
  3091. \end{tabular}
  3092. To read an entry you can simply call \var{ppufile.readentry:byte},
  3093. it returns the
  3094. \var{tppuentry.nr} field, which holds the type of the entry.
  3095. A common way how this works is (example is for the symbols):
  3096. \begin{verbatim}
  3097. repeat
  3098. b:=ppufile.readentry;
  3099. case b of
  3100. ib<etc> : begin
  3101. end;
  3102. ibendsyms : break;
  3103. end;
  3104. until false;
  3105. \end{verbatim}
  3106. Then you can parse each entry type yourself. \var{ppufile.readentry} will take
  3107. care of skipping unread bytes in the entry an read the next entry
  3108. correctly! A special function is \var{skipuntilentry(untilb:byte):boolean;}
  3109. which will read the ppufile until it finds entry \var{untilb} in the main
  3110. entries.
  3111. Parsing an entry can be done with \var{ppufile.getxxx} functions. The
  3112. available functions are:
  3113. \begin{verbatim}
  3114. procedure ppufile.getdata(var b;len:longint);
  3115. function getbyte:byte;
  3116. function getword:word;
  3117. function getlongint:longint;
  3118. function getreal:ppureal;
  3119. function getstring:string;
  3120. \end{verbatim}
  3121. To check if you're at the end of an entry you can use the following
  3122. function:
  3123. \begin{verbatim}
  3124. function EndOfEntry:boolean;
  3125. \end{verbatim}
  3126. {\em notes:}
  3127. \begin{enumerate}
  3128. \item \var{ppureal} is the best real that exists for the cpu where the
  3129. unit is created for. Currently it is \var{extended} for i386 and
  3130. \var{single} for m68k.
  3131. \item the \var{ibobjectdef} and \var{ibrecorddef} have stored a definition
  3132. and symbol section for themselves. So you'll need a recursive call. See
  3133. \file{ppudump.pp} for a correct implementation.
  3134. \end{enumerate}
  3135. A complete list of entries and what their fields contain can be found
  3136. in \file{ppudump.pp}.
  3137. \section{Creating ppufiles}
  3138. Creating a new ppufile works almost the same as writing. First you need
  3139. to init the object and call create:
  3140. \begin{verbatim}
  3141. ppufile:=new(pppufile,'output.ppu');
  3142. ppufile.create;
  3143. \end{verbatim}
  3144. After that you can simply write all needed entries. You'll have to take
  3145. care that you write at least the basic entries for the sections:
  3146. \begin{verbatim}
  3147. ibendinterface
  3148. ibenddefs
  3149. ibendsyms
  3150. ibendbrowser (only when you've set uf_has_browser!)
  3151. ibendimplementation
  3152. ibend
  3153. \end{verbatim}
  3154. Writing an entry is a little different than reading it. You need to first
  3155. put everything in the entry with ppufile.putxxx:
  3156. \begin{verbatim}
  3157. procedure putdata(var b;len:longint);
  3158. procedure putbyte(b:byte);
  3159. procedure putword(w:word);
  3160. procedure putlongint(l:longint);
  3161. procedure putreal(d:ppureal);
  3162. procedure putstring(s:string);
  3163. \end{verbatim}
  3164. After putting all the things in the entry you need to call
  3165. \var{ppufile.writeentry(ibnr:byte)} where \var{ibnr} is the entry number
  3166. you're writing.
  3167. At the end of the file you need to call \var{ppufile.writeheader} to write the
  3168. new header to the file. This takes automatically care of the new size of the
  3169. ppufile. When that is also done you can call \var{ppufile.close} and dispose the
  3170. object.
  3171. Extra functions/variables available for writing are:
  3172. \begin{verbatim}
  3173. ppufile.NewHeader;
  3174. ppufile.NewEntry;
  3175. \end{verbatim}
  3176. This will give you a clean header or entry. Normally called automatically
  3177. in \var{ppufile.writeentry}, so you can't forget it.
  3178. \begin{verbatim}
  3179. ppufile.flush;
  3180. \end{verbatim}
  3181. to flush the current buffers to the disk
  3182. \begin{verbatim}
  3183. ppufile.do_crc:boolean;
  3184. \end{verbatim}
  3185. set to false if you don't want that the crc is updated, this is necessary
  3186. if you write for example the browser data.
  3187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3188. % Appendix B
  3189. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3190. \chapter{Compiler and RTL source tree structure}
  3191. \label{ch:AppB}
  3192. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3193. % The compiler source tree
  3194. \section{The compiler source tree}
  3195. All compiler source files are in one directory, normally in
  3196. \file{source/compiler}. For more informations
  3197. about the structure of the compiler have a look at the
  3198. Compiler Manual which contains also some informations about
  3199. compiler internals.
  3200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3201. % Appendix C
  3202. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3203. \chapter{Compiler limits}
  3204. \label{ch:AppC}
  3205. Although many of the restrictions imposed by the MS-DOS system are removed
  3206. by use of an extender, or use of another operating system, there still are
  3207. some limitations to the compiler:
  3208. \begin{enumerate}
  3209. \item Procedure or Function definitions can be nested to a level of 32.
  3210. \item Maximally 255 units can be used in a program when using the real-mode
  3211. compiler (i.e. a binary that was compiled by Borland Pascal). When using the 32-bit compiler, the limit is set to 1024. You can
  3212. change this by redefining the \var{maxunits} constant in the
  3213. \file{files.pas} compiler source file.
  3214. \end{enumerate}
  3215. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3216. % Appendix D
  3217. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3218. \chapter{Compiler modes}
  3219. \label{ch:AppD}
  3220. Here we list the exact effect of the different compiler modes. They can be
  3221. set with the \var{\$Mode} switch, or by command line switches.
  3222. \section{FPC mode}
  3223. This mode is selected by the \var{{\$MODE FPC}} switch. On the command-line,
  3224. this means that you use none of the other compatibility mode switches.
  3225. It is the default mode of the compiler. This means essentially:
  3226. \begin{enumerate}
  3227. \item You must use the address operator to assign procedural variables.
  3228. \item A forward declaration must be repeated exactly the same by the
  3229. implementation of a function/procedure. In particular, you can not omit the
  3230. parameters when implementing the function or procedure.
  3231. \item Overloading of functions is allowed.
  3232. \item Nested comments are allowed.
  3233. \item The Objpas unit is NOT loaded.
  3234. \item You can use the cvar type.
  3235. \item PChars are converted to strings automatically.
  3236. \end{enumerate}
  3237. \section{TP mode}
  3238. This mode is selected by the \var{{\$MODE TP}} switch. On the command-line,
  3239. this mode is selected by the \var{-So} switch.
  3240. \begin{enumerate}
  3241. \item You cannot use the address operator to assign procedural variables.
  3242. \item A forward declaration must not be repeated exactly the same by the
  3243. implementation of a function/procedure. In particular, you can omit the
  3244. parameters when implementing the function or procedure.
  3245. \item Overloading of functions is not allowed.
  3246. \item Nested comments are not allowed.
  3247. \item You can not use the cvar type.
  3248. \end{enumerate}
  3249. \section{Delphi mode}
  3250. This mode is selected by the \var{{\$MODE DELPHI}} switch. On the command-line,
  3251. this mode is selected by the \var{-Sd} switch.
  3252. \begin{enumerate}
  3253. \item You can not use the address operator to assign procedural variables.
  3254. \item A forward declaration must not be repeated exactly the same by the
  3255. implementation of a function/procedure. In particular, you can not omit the
  3256. parameters when implementing the function or procedure.
  3257. \item Overloading of functions is not allowed.
  3258. \item Nested comments are not allowed.
  3259. \item The Objpas unit is loaded right after the system unit. One of the
  3260. consequences of this is that the type \var{Integer} is redefined as
  3261. \var{Longint}.
  3262. \end{enumerate}
  3263. \section{GPC mode}
  3264. This mode is selected by the \var{{\$MODE GPC}} switch. On the command-line,
  3265. this mode is selected by the \var{-Sp} switch.
  3266. \begin{enumerate}
  3267. \item You cmust use the address operator to assign procedural variables.
  3268. \item A forward declaration must not be repeated exactly the same by the
  3269. implementation of a function/procedure. In particular, you can omit the
  3270. parameters when implementing the function or procedure.
  3271. \item Overloading of functions is not allowed.
  3272. \item Nested comments are not allowed.
  3273. \item You can not use the cvar type.
  3274. \end{enumerate}
  3275. \section{OBJFPC mode}
  3276. This mode is selected by the \var{{\$MODE OBJFPC}} switch. On the command-line,
  3277. this mode is selected by the \var{-S2} switch.
  3278. \begin{enumerate}
  3279. \item You must use the address operator to assign procedural variables.
  3280. \item A forward declaration must be repeated exactly the same by the
  3281. implementation of a function/procedure. In particular, you can not omit the
  3282. parameters when implementing the function or procedure.
  3283. \item Overloading of functions is allowed.
  3284. \item Nested comments are allowed.
  3285. \item The Objpas unit is loaded right after the system unit. One of the
  3286. consequences of this is that the type \var{Integer} is redefined as
  3287. \var{Longint}.
  3288. \item You can use the cvar type.
  3289. \item PChars are converted to strings automatically.
  3290. \end{enumerate}
  3291. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3292. % Appendix E
  3293. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3294. \chapter{Using \file{makefile.fpc}}
  3295. \label{ch:makefile}
  3296. \newcommand{\mvar}[1]{\var{\$(#1)}}
  3297. \section{Introduction}
  3298. \fpc comes with a special makefile, \file{makefile.fpc}, which can be
  3299. included in any makefile you use to compile with \fpc. There is a template
  3300. \file{Makefile} provided also. All sources from the \fpc team are compiled
  3301. with this system.
  3302. These files are installed in the following directories:
  3303. \begin{description}
  3304. \item[linux]
  3305. \item[Dos or Windows]
  3306. \end{description}
  3307. The template \file{Makefile} searches for the \file{makefile.fpc} in the
  3308. following places :
  3309. \begin{enumerate}
  3310. \item The {\em file} pointed to by the \var{FPCMAKE} environment variable.
  3311. \item The directory pointed to by the \var{FPCDIR} envinonment variable.
  3312. \item The directory pointed to by the \var{DEFAULTFPCDIR} make variable.
  3313. \item The current directory.
  3314. \end{enumerate}
  3315. Thus, setting \var{FPCMAKE} or \var{FPCDIR} as an environment string will
  3316. ensure that \file{makefile.fpc} is always found, and will be read by all
  3317. makefiles, derived from the template.
  3318. The following sections explain what variables are set by \var{makefile.fpc},
  3319. what variables it expects to be set, and what targets it defines. After
  3320. that, some settings in the template makefile are explained.
  3321. \section{Programs needed to use the makefile}
  3322. The following programs are needed by the makefile to function correctly:
  3323. \begin{description}
  3324. \item[cp] a copy program.
  3325. \item[date] a program that prints the date.
  3326. \item[install] a program to install files.
  3327. \item[make] the \file{make} program, obviously.
  3328. \item[pwd] a program that prints the current working directory.
  3329. \item[rm] a program to delete files.
  3330. \end{description}
  3331. These are standard programs on linux systems, with the possible exception of
  3332. \file{make}. For \dos or \windowsnt, they can be found in the
  3333. file \file{gnuutils.zip} on the \fpc FTP site.
  3334. \section{Variables used by \file{makefile.fpc}}
  3335. Many variables affect the behaviour of the makefile. The variables can be
  3336. split in several groups:
  3337. \begin{description}
  3338. \item[Required variables]
  3339. \item[Directory variables]
  3340. \item[Target variables]
  3341. \item[Compiler command-line variables]
  3342. \end{description}
  3343. Each group will be discussed separately in the subsequent.
  3344. \subsection{Required variables}
  3345. In principle, the \var{makefile.fpc} only expects one variable to be set:
  3346. \begin{description}
  3347. \item[FPCDIR] This is the base directory of \fpc sources. The makefile
  3348. expects to find a directory \file{rtl} below this directory.
  3349. \end{description}
  3350. \subsection{Directory variables}
  3351. The first set of variables controls the directories used in the makefile:
  3352. \begin{description}
  3353. \item[INC] this is a list of directories, separated by spaces, that will
  3354. be added as include directories to the compiler command-line.
  3355. \item[LIBDIR] is a list of library paths, separated by spaces. Each
  3356. directory in the list is prepended with \var{-Fl} and added to the
  3357. compiler options.
  3358. \item[NEEDLIBDIR] is a space-separated list of library paths. Each
  3359. directory in the list is
  3360. prepended with \var{-Fl} and added to the compiler options.
  3361. \item[NEEDOBJDIR] is a list of object file directories, separated by
  3362. spaces. Each directory in the list is prepended with \var{-Fo} and
  3363. added to the compiler options.
  3364. \item[NEEDUNITDIR] is a list of unit directories, separated by spaces.
  3365. Each directory in the list is prepended with \var{-Fu} and is added to the
  3366. compiler options.
  3367. \item[OBJDIR] is a list of object file directories, separated by spaces, that is
  3368. added to the object files path, i.e. Each directory in the list is prepended with
  3369. \var{-Fo}.
  3370. \item[OSINC] this is a space-separated list of OS-dependent directories
  3371. that will be added as include directories to the compiler command line.
  3372. \item[PROCINC] is a space-separated list of processor-dependent directories
  3373. that will be added as include directories to the compiler command-line.
  3374. \item[RTL] If \var{RTLDIR} is not set, \var{RTL} is used to construct
  3375. \var{RTLDIR}, after which \var{RTLDIR} is added to the compiler unit
  3376. path, with \var{-Fu} prepended. If \var{RTLDIR} is not set, it is set
  3377. to \mvar{RTL}/\mvar{OS\_TARGET}.
  3378. \item[RTLDIR] Directory where the \var{RTL} unit sources are.
  3379. If \var{RTLDIR} is not set, it is set to \mvar{RTL}/\mvar{OS\_TARGET}. \\
  3380. If \var{RTL} is also not set, it is set to \mvar{FPCDIR}\var{/rtl/}\mvar{OS\_TARGET}.
  3381. \item[TARGETDIR] If set, this directory is added as the output directory of
  3382. the compiler, where all units and executables are written, i.e. it gets
  3383. \var{-FE} prepended.
  3384. \item[UNIT] If \var{UNITDIR} is not set, \var{UNIT} is used to construct
  3385. \var{UNITDIR}. \var{UNITDIR} is added to the compiler unit path, with \var{-Fu}
  3386. prepended.
  3387. \item[UNITDIR] Directory where the \var{RTL} compiled units are.
  3388. If \var{UNITDIR} is not set, it is set to \mvar{UNIT}/\mvar{OS\_TARGET}. \\
  3389. If \var{UNIT} is also not set, it is set to \mvar{FPCDIR}\var{/rtl/}\mvar{OS\_TARGET}.
  3390. \item[UNITS] The content of this variable are appended to the
  3391. \var{BASEINSTALLDIR} variable to install the units.
  3392. \item[UNITTARGETDIR] If set, this directory is added as the output directory of
  3393. the compiler, where all units are written, i.e. it gets
  3394. \var{-FU} prepended. This overrides \var{TARGETDIR}.
  3395. \end{description}
  3396. \subsection{Target variables}
  3397. The second set of variables controls the targets that are constructed
  3398. by the makefile:
  3399. \begin{description}
  3400. \item[DEFAULTUNITS] If defined, only units will be made by the makefile. If
  3401. not defined, then executables are made also.
  3402. \item[EXEOBJECTS] This is a list of executable names that will be compiled.
  3403. the makefile appends \mvar{EXEEXT} to these names.
  3404. \item[LOADEROBJECTS] is a list of space-separated names that identify
  3405. loaders to be compiled. This is mainly used in the compiler's RTL sources.
  3406. \item[UNITOBJECTS] This is a list of unit names that will be compiled. The
  3407. makefile appends \mvar{PPUEXT} to each of these names to form the unit file
  3408. name. The sourcename is formed by adding \mvar{PASEXT}.
  3409. \item[ZIPNAME] is the name of the archive that will be created by the
  3410. makefile.
  3411. \item[ZIPTARGET] is the target that is built before the archive is made.
  3412. this target is built first. If successful, the zip archive will be made.
  3413. \end{description}
  3414. \subsection{Compiler command-line variables}
  3415. The following variables control the compiler command-line:
  3416. \begin{description}
  3417. \item[CFGFILE] if this variable is set, it will be used as the name of the
  3418. config file to be used by the compiler.
  3419. \item[CPU] the CPU type is added as a define to the compiler command line.
  3420. Automatically determined by the makefile.
  3421. \item[LIBNAME] if smartlinking is requested (i.e. \var{SMARTLINK} is set to
  3422. \var{YES}), this is the name of the static library to produce. Don't add
  3423. \var{lib} to this, the compiler will do that.
  3424. \item[LIBTYPE] if set to \var{shared}, then the compiler will emit a shared
  3425. library, with name \var{LIBNAME}.If \var{LIBTYPE} is set to \var{static},
  3426. the compiler will emit a static, smartlinked library,
  3427. \item[NEEDGCCLIB] if this variable is defined, then the path to \file{libgcc}
  3428. is added to the library path.
  3429. \item[NEEDOTHERLIB] (\linux only) If this is defined, then the makefile will
  3430. append all directories that appear in \var{/etc/ld.so.conf} to the library path.
  3431. \item[OPT] Any options that you want to pass to the compiler. The contents
  3432. of \var{OPT} is simply added to the compiler command-line.
  3433. \item[OPTDEF] Are optional defines, added to the command-line of the
  3434. compiler. They do not get \var{-d} prepended.
  3435. \item[OS\_TARGET] What platform you want to compile for. Added to the
  3436. compiler command-line with a \var{-T} prepended.
  3437. \item[SMARTLINK] if \var{SMARTLINK} is set to \var{YES} then the compiler
  3438. will output smartlinked units if \var{LIBTYPE} is not set to \var{shared}.
  3439. \end{description}
  3440. \section{Variables set by \file{makefile.fpc}}
  3441. All of the following variables are only set by \var{makefile.fpc}, if
  3442. they aren't already defined. This means that you can override them by
  3443. setting them on the make command line, or setting them in the makefile you
  3444. use, BEFORE \file{makefile.fpc} is included.
  3445. The following sets of variables are defined:
  3446. \begin{description}
  3447. \item[Directory variables]
  3448. \item[Program names]
  3449. \item[File extensions]
  3450. item[Target files]
  3451. \end{description}
  3452. Each of these sets is discussed in the subsequent:
  3453. \subsection{Directory variables}
  3454. The following directories are defined by the makefile:
  3455. \begin{description}
  3456. \item[BASEDIR] is set to the current directory if the \file{pwd} command is
  3457. available. If not, it is set to '.'.
  3458. \item[BASEINSTALLDIR] is the base for all directories where units are
  3459. installed. On \linux, this is set to
  3460. \mvar{PREFIXINSTALLDIR}\var{/lib/fpc/}\mvar{RELEASEVER}.\\ On other systems,
  3461. it is set to \mvar{PREFIXINSTALLDIR}
  3462. \item[BININSTALLDIR] is set to \mvar{BASEINSTALLDIR}/\var{bin} on \linux,
  3463. and\\
  3464. \mvar{BASEINSTALLDIR}/\var{bin}/\mvar{OS\_TARGET} on other systems. This is
  3465. the place where binaries are installed.
  3466. \item[GCCLIBDIR] (\linux only) is set to the directory where \file{libgcc.a}
  3467. is.
  3468. \item[LIBINSTALLDIR] is set to \mvar{BASEINSTALLDIR} on \linux,\\
  3469. and \mvar{BASEINSTALLDIR}/\var{lib} on other systems.
  3470. \item[OTHERLIBDIR] (\linux only) is set to the full set of paths in \file{/etc/ld.so.conf}
  3471. \item[PREFIXINSTALLDIR] is set to \file{/usr} on \linux, \file{/pp} on \dos
  3472. or \windowsnt.
  3473. \item[SHARED\_LIBINSTALLDIR] is where shared libraries are
  3474. installed. This equals \mvar{PREFIXINSTALLDIR}/\var{lib} on \linux, and
  3475. \var{SHARED\_UNITINSTALLDIR} on other systems.
  3476. \item[SHARED\_UNITINSTALLDIR] is where units from libraries are
  3477. installed. This equals \mvar{UNITINSTALLDIR}/\var{shared}
  3478. \item[STATIC\_LIBINSTALLDIR] is where static libraries will be
  3479. installed. By default, it equals \mvar{STATIC\_UNITINSTALLDIR}.
  3480. \item[STATIC\_UNITINSTALLDIR] is where static, smartlinked units will be
  3481. installed. It equals \mvar{UNITINSTALLDIR}/\var{static}.
  3482. \item[UNITINSTALLDIR] is where units will be installed. This is set to\\
  3483. \mvar{BASEINSTALLDIR}/\mvar{UNITPREFIX} \\
  3484. on \linux. On other systems, it is set to \\
  3485. \mvar{BASEINSTALLDIR}/\mvar{UNITPREFIX}/\mvar{OS\_TARGET}.
  3486. \end{description}
  3487. \subsection{Program names}
  3488. The following variables are program names, used in makefile targets.
  3489. \begin{description}
  3490. \item[AS] The assembler. Default set to \file{as}.
  3491. \item[COPY] a file copy program. Default set to \file{cp -fp}.
  3492. \item[CMP] a program to compare files. Default set to \var{cmp}.
  3493. \item[DEL] a file removal program. Default set to \file{rm -f}.
  3494. \item[DELTREE] a directory removal program. Default set to \file{rm -rf}.
  3495. \item[DATE] a program to display the date.
  3496. \item[DIFF] a program to produce diff files.
  3497. \item[ECHO] an echo program.
  3498. \item[INSTALL] a program to install files. Default set to \file{install -m
  3499. 644} on linux.
  3500. \item[INSTALLEXE] a program to install executable files. Default set to \file{install -m
  3501. 755} on linux.
  3502. \item[LD] The linker. Default set to \file{ld}.
  3503. \item[LDCONFIG] (\linux only) the program used to update the loader cache.
  3504. \item[MKDIR] a program to create directories if they don't exist yet. Default
  3505. set to \file{install -m 755 -d}
  3506. \item[MOVE] a file move program. Default set to \file{mv -f}
  3507. \item[PP] the Free Pascal compiler executable. Default set to
  3508. \var{ppc386.exe}
  3509. \item[PPAS] the name of the shell script created by the compiler if the
  3510. \var{-s} option is specified. This command will be executed after
  3511. compilation, if the \var{-s} option was detected among the options.
  3512. \item[PPUMOVE] the program to move units into one big unit library.
  3513. \item[SED] a stream-line editor program. Default set to \file{sed}.
  3514. \item[UPX] an executable packer to compress your executables into
  3515. self-extracting compressed executables.
  3516. \item[ZIPEXE] a zip program to compress files. zip targets are made with
  3517. this program
  3518. \end{description}
  3519. \subsection{File extensions}
  3520. The following variables denote extensions of files. These variables include
  3521. the \var{.} (dot) of the extension. They are appended to object names.
  3522. \begin{description}
  3523. \item[ASMEXT] is the extension of assembler files produced by the compiler.
  3524. \item[LOADEREXT] is the extension of the assembler files that make up the
  3525. executable startup code.
  3526. \item[OEXT] is the extension of the object files that the compiler creates.
  3527. \item[PACKAGESUFFIX] is a suffix that is appended to package names in zip
  3528. targets. This serves so packages can be made for different OSes.
  3529. \item[PASEXT] is the extension of pascal files used in the compile rules.
  3530. It is determined by looking at the first \var{EXEOBJECTS} source file or
  3531. the first \var{UNITOBJECTS} files.
  3532. \item[PPLEXT] is the extension of shared library unit files.
  3533. \item[PPUEXT] is the extension of default units.
  3534. \item[SHAREDLIBEXT] is the extension of shared libraries.
  3535. \item[SMARTEXT] is the extension of smartlinked unit assembler files.
  3536. \item[STATICLIBEXT] is the extension of static libraries.
  3537. \end{description}
  3538. \subsection{Target files}
  3539. The following variables are defined to make targets and rules easier:
  3540. \begin{description}
  3541. \item[COMPILER] is the complete compiler commandline, with all options
  3542. added, after all \file{Makefile} variables have been examined.
  3543. \item[DATESTR] contains the date.
  3544. \item[EXEFILES] is a list of executables that will be created by the
  3545. makefile.
  3546. \item[EXEOFILES] is a list of executable object files that will be created
  3547. by the makefile.
  3548. \item[LOADEROFILES] is a list of object files that will be made from the
  3549. loader assembler files. This is mainly for use in the compiler's RTL sources.
  3550. \item[UNITFILES] a list of unit files that will be made. This is just
  3551. the list of unit objects, with the correct unit extension appended.
  3552. \item[UNITOFILES] a list of unit object files that will be made.
  3553. This is just the list of unit objects, with the correct object file
  3554. extension appended.
  3555. \end{description}
  3556. \section{Rules and targets created by \file{makefile.fpc}}
  3557. The \var{makefile.fpc} defines a series of targets, which can be called by
  3558. your own targets. They have names that resemble default names (such as
  3559. 'all', 'clean'), only they have \var{fpc\_} prepended.
  3560. \subsection{Pattern rules}
  3561. The makefile makes the following pattern rules:
  3562. \begin{description}
  3563. \item[units] how to make a pascal unit form a pascal source file.
  3564. \item[executables] how to make an executable from a pascal source file.
  3565. \item[object file] how to make an object file from an assembler file.
  3566. \end{description}
  3567. \subsection{Build rules}
  3568. The following build targets are defined:
  3569. \begin{description}
  3570. \item[fpc\_all] target that builds all units and executables as well as
  3571. loaders. If \var{DEFAULTUNITS} is defined, executables are excluded from the
  3572. targets.
  3573. \item[fpc\_exes] target to make all executables in \var{EXEOBJECTS}.
  3574. \item[fpc\_loaders] target to make all files in \var{LOADEROBJECTS}.
  3575. \item[fpc\_sharedlib] target that makes all units as dynamic libraries.
  3576. \item[fpc\_staticlib] target that makes all units as smartlinked units.
  3577. \item[fpc\_units] target to make all units in \var{UNITOBJECTS}.
  3578. \end{description}
  3579. \subsection{Cleaning rules}
  3580. The following cleaning targets are defined:
  3581. \begin{description}
  3582. \item[fpc\_clean] cleans all files that result when \var{fpc\_all} was made.
  3583. \item[fpc\_libsclean] is the same as \var{fpc\_clean}, but also removes any
  3584. shared or dynamic libraries that may have been built.
  3585. \item[fpc\_cleanall] is the same as both previous target commands, but also
  3586. deletes all object, unit and assembler files that are present.
  3587. \end{description}
  3588. \subsection{archiving rules}
  3589. The following archiving targets are defined:
  3590. \begin{description}
  3591. \item[fpc\_zipinstalladd] will add to a (possibibly existing) archive file (it's
  3592. name is taken from \mvar{ZIPNAME}.
  3593. \item[fpc\_zipinstall] is the same, only the archive is cleared first.
  3594. \end{description}
  3595. The zip is made uzing the \var{ZIPEXE} program. Under \linux, a
  3596. \file{.tar.gz} file is created.
  3597. \subsection{Informative rules}
  3598. The following targets produce information about the makefile:
  3599. \begin{description}
  3600. \item[fpc\_cfginfo] gives general configuration information: the location of
  3601. the makefile, the compiler version, target OS, CPU.
  3602. \item[fpc\_dirinfo] gives the directories, used by the compiler.
  3603. \item[fpc\_info] executes all other info targets.
  3604. \item[fpc\_installinfo] gives all directories where files will be installed.
  3605. \item[fpc\_objectinfo] lists all objects that will be made.
  3606. \item[fpc\_toolsinfo] lists all defined tools.
  3607. \end{description}
  3608. \section{Using the provided template}
  3609. The template makefile that comes with \fpc does nothing other than
  3610. offering you some variables to be set for the \file{makefile.fpc}.
  3611. After that it loads the \var{makefile.fpc} in the indicated places.
  3612. Finally it declares a set of default targets:
  3613. \begin{description}
  3614. \item[all] calls fpc\_all.
  3615. \item[clean] calls fpc\_clean.
  3616. \item[install] calls fpc\_install.
  3617. \item[info] calls fpc\_info.
  3618. \item[staticlib] calls fpc\_staticlib.
  3619. \item[sharedlib] calls fpc\_sharedlib.
  3620. \item[libsclean] calls fpc\_libsclean.
  3621. \item[staticinstall] calls fpc\_staticinstall.
  3622. \item[sharedinstall] calls fpc\_sharedinstall.
  3623. \item[libinstall] calls fpc\_libinstall.
  3624. \end{description}
  3625. You can override each of these targets to suit your setup.
  3626. If you just have to compile some units and programs, you only need to set
  3627. the following variables:
  3628. \begin{description}
  3629. \item[UNITOBJECTS] names of units you wish to be built.
  3630. \item[EXEOBJECTS] names of executables you wish to be built.
  3631. \end{description}
  3632. You may want to set some of the following variables:
  3633. \begin{description}
  3634. \item[INC,PROCINC or OSINC] To indicate where include files can be found.
  3635. \item[NEEDOPT] additional options added to the compile command.
  3636. \item[NEEDUNITDIR] space-separated list of directories where units that you
  3637. need are located.
  3638. \item[TARGETDIR,UNITTARGETDIR] where do you want executables and units to
  3639. be written. Be aware that setting this variable may interfere with
  3640. \var{make}, since it will not find the target files.
  3641. \item[DEFAULTUNITS] if you define this variable (to whatever value you want)
  3642. then the \var{all} target will by default only make the units.
  3643. \end{description}
  3644. You may also set any of the variables that appear in the previous sections,
  3645. to override default behaviour of the makefile.
  3646. After having set these variables, you can run 'make info' to see whether all
  3647. variables are set to you satisfaction. If the \file{makefile.fpc} is not
  3648. found, this command will inform you of this.
  3649. After that, a simple 'make all' will make all units and executables.
  3650. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3651. % Appendix F
  3652. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3653. \chapter{Compiling the compiler yourself}
  3654. \label{ch:AppF}
  3655. \section{Introduction}
  3656. The \fpc team releases at intervals a completely prepared package, with
  3657. compiler and units all ready to use, the so-called releases. After a
  3658. release, work on the compiler continues, bugs are fixed and features are
  3659. added. The \fpc team doesn't make a new release whenever they change
  3660. something in the compiler, instead the sources are available for anyone to
  3661. use and compile. Compiled versions of RTL and compiler are also made daily,
  3662. and put on the web.
  3663. There are, nevertheless, circumstances when you'll want to compile the
  3664. compiler yourself. For instance if you made changes to compiler code,
  3665. or when you download the compiler via CVS.
  3666. There are essentially 2 ways of recompiling the compiler: by hand, or using
  3667. the makefiles. Each of these methods will be discussed.
  3668. \section{Before you begin}
  3669. To compile the compiler easily, it is best to keep the following directory
  3670. structure (a base directory of \file{/pp/src} is supposed, but that may be
  3671. different):
  3672. \begin{verbatim}
  3673. /pp/src/Makefile
  3674. /makefile.fpc
  3675. /rtl/linux
  3676. /inc
  3677. /i386
  3678. /...
  3679. /compiler
  3680. \end{verbatim}
  3681. If you want to use the makefiles, you {\em must} use the above directory
  3682. tree.
  3683. The compiler and rtl source are zipped in such a way that if you unzip both
  3684. files in the same directory (\file{/pp/src} in the above) the above
  3685. directory tree results.
  3686. The \file{makefile.fpc} and \file{Makefile} come from the \file{base.zip}
  3687. file on the ftp site. If you compile manually, you don't need them.
  3688. There are 2 ways to start compiling the compiler and RTL. Both ways must be
  3689. used, depending on the situation. Usually, the RTL must be compiled first,
  3690. before compiling the compiler, after which the compiler is compiled using
  3691. the current compiler. In some special cases the compiler must be compiled
  3692. first, with a previously compiled RTL.
  3693. How to decide which should be compiled first? In general, the answer is that
  3694. you should compile the RTL first. There are 2 exceptions to this rule:
  3695. \begin{enumerate}
  3696. \item The first case is when some of the internal routines in the RTL
  3697. have changed, or if new internal routines appeared. Since the OLD compiler
  3698. doesn't know about these changed internal routines, it will emit function
  3699. calls that are based on the old compiled RTL, and hence are not correct.
  3700. Either the result will not link, or the binary will give errors.
  3701. \item The second case is when something is added to the RTL that the
  3702. compiler needs to know about (a new default assembler mechanism, for
  3703. example).
  3704. \end{enumerate}
  3705. How to know if one of these things has occurred ? There is no way to know,
  3706. except by mailing the \fpc team. If you cannot recompile the compiler
  3707. when you first compile the RTL, then try the other way.
  3708. \section{Compiling using \file{make}}
  3709. When compiling with \var{make} it is necessary to have the above directory
  3710. structure. Compiling the compiler is achieved with the target \var{cycle}.
  3711. Under normal circumstances, recompiling the compiler is limited to the
  3712. following instructions (assuming you start in directory \file{/pp/src}):
  3713. \begin{verbatim}
  3714. cd compiler
  3715. make cycle
  3716. \end{verbatim}
  3717. This will work only if the \file{makefile.fpc} is installed correctly and
  3718. if the needed tools are present in the \var{PATH}. Which tools must be
  3719. installed can be found in appendix \ref{ch:makefile}.
  3720. The above instructions will do the following:
  3721. \begin{enumerate}
  3722. \item Using the current compiler, the RTL is compiled in the correct
  3723. directory, which is determined by the OS you are under. e.g. under \linux,
  3724. the RTL is compiled in directory \file{rtl/linux}.
  3725. \item The compiler is compiled using the newly compiled RTL. If successful,
  3726. the newly compiled compiler executable is copied to a temporary executable.
  3727. \item Using the temporary executable from the previous step, the RTL is
  3728. re-compiled.
  3729. \item Using the temporary executable and the newly compiled RTL from the
  3730. last step, the compiler is compiled again.
  3731. \end{enumerate}
  3732. The last two steps are repeated 3 times, until three passes have been made or
  3733. until the generated compiler binary is equal to the binary it was compiled
  3734. with. This process ensures that the compiler binary is correct.
  3735. Compiling for another target:
  3736. When you want to compile the compiler for another target, you must specify
  3737. the \var{OS\_TARGET} makefile variable. It can be set to the following
  3738. values: \var{win32}, \var{go32v2}, \var{os2} and \var{linux}.
  3739. As an example, cross-compilation for the go32v2 target from the win32 target
  3740. is chosen:
  3741. \begin{verbatim}
  3742. cd compiler
  3743. make cycle OS_TARGET=go32v2
  3744. \end{verbatim}
  3745. This will compile the go32v2 RTL, and compile a \var{go32v2} compiler.
  3746. If you want to compile a new compiler, but you want the compiler to be
  3747. compiled first using an existing compiled RTL, you should specify the
  3748. \var{all} target, and specify another RTL directory than the default (which
  3749. is the \file{../rtl/\$(OS\_TARGET)} directory). For instance, assuming that
  3750. the compiled RTL units are in \var{/pp/rtl}, you could type
  3751. \begin{verbatim}
  3752. cd compiler
  3753. make clean
  3754. make all UNITDIR=/pp/rtl
  3755. \end{verbatim}
  3756. This will then compile the compiler using the RTL units in \file{/pp/rtl}.
  3757. After this has been done, you can do the 'make cycle', starting with this
  3758. compiler:
  3759. \begin{verbatim}
  3760. make cycle PP=./ppc386
  3761. \end{verbatim}
  3762. This will do the \var{make cycle} from above, but will start with the compiler
  3763. that was generated by the \var{make all} instruction.
  3764. In all cases, many options can be passed to \var{make} to influence the
  3765. compile process. In general, the makefiles add any needed compiler options
  3766. to the command-line, so that the RTL and compiler can be compiled. You can
  3767. specify additional options (e.g. optimization options) by passing them in
  3768. \var{OPT}.
  3769. \section{Compiling by hand}
  3770. Compiling by hand is difficult and tedious, but can be done. We'll treat the
  3771. compilation of RTL and compiler separately.
  3772. \subsection{Compiling the RTL}
  3773. To recompile the RTL, so a new compiler can be built, at least the following
  3774. units must be built, in the order specified:
  3775. \begin{enumerate}
  3776. \item[loaders] the program stubs, that are the startup code for each pascal
  3777. program. These files have the \file{.as} extension, because they are written
  3778. in assembler. They must be assembled with the \gnu \file{as} assembler. These stubs
  3779. are in the OS-dependent directory, except for \linux, where they are in a
  3780. processor dependent subdirectory of the linux directory (\file{i386} or
  3781. \file{m68k}).
  3782. \item[system] the \file{system} unit. This unit is named differently on different
  3783. systems:
  3784. \begin{itemize}
  3785. \item Only on GO32v2, it's called \file{system}.
  3786. \item For \linux it's called \file{syslinux}.
  3787. \item For \windowsnt it's calles \file{syswin32}.
  3788. \item For \ostwo it's called \file{sysos2}
  3789. \end{itemize}
  3790. This unit resides in the OS-depentent subirectories of the RTL.
  3791. \item[strings] The strings unit. This unit resides in the \file{inc}
  3792. subdirectory of the RTL.
  3793. \item[dos] The \file{dos} unit. It resides in the OS-dependent subdirectory
  3794. of the RTL. Possibly other units will be compiled as a consequence of trying
  3795. to compile this unit (e.g. on \linux, the \file{linux} unit will be
  3796. compiled, on go32, the \file{go32} unit will be compiled).
  3797. \item[objects] the objects unit. It resides in the \file{inc} subdirectory
  3798. of the RTL.
  3799. \end{enumerate}
  3800. To compile these units on a i386, the following statements will do:
  3801. \begin{verbatim}
  3802. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 -Us -Sg syslinux.pp
  3803. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/strings.pp
  3804. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 dos.pp
  3805. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/objects.pp
  3806. \end{verbatim}
  3807. These are the minimum command-line options, needed to compile the RTL.
  3808. For another processor, you should change the \var{i386} into the appropriate
  3809. processor. For another operating system (target) you should change the
  3810. \file{syslinux} in the appropriate system unit file, and you should change
  3811. the target OS setting (\var{-T}).
  3812. Depending on the target OS there are other units that you may wish to
  3813. compile, but which are not strictly needed to recompile the compiler.
  3814. The following units are available for all plaforms:
  3815. \begin{description}
  3816. \item[objpas] Needed for Delphi mode. Needs \var{-S2} as an option. Resides
  3817. in the \file{objpas} subdirectory.
  3818. \item[sysutils] many utility functions, like in Delphi. Resides in the
  3819. \file{objpas} directory, and needs \var{-S2} to compile.
  3820. \item[typinfo] functions to access RTTI information, like Delphi. Resides in
  3821. the \file{objpas} directory.
  3822. \item[math] math functions like in Delphi. Resides in the \file{objpas}
  3823. directory.
  3824. \item[mmx] extensions for MMX class Intel processors. Resides in
  3825. in the \file{i386} directory.
  3826. \item[getopts] a GNU compatible getopts unit. resides in the \file{inc}
  3827. directory.
  3828. \item[heaptrc] to debug the heap. resides in the \file{inc} directory.
  3829. \end{description}
  3830. \subsection{Compiling the compiler}
  3831. Compiling the compiler can be done with one statement. It's always best to
  3832. remove all units from the compiler directory first, so something like
  3833. \begin{verbatim}
  3834. rm *.ppu *.o
  3835. \end{verbatim}
  3836. on \linux, and on \dos
  3837. \begin{verbatim}
  3838. del *.ppu
  3839. del *.o
  3840. \end{verbatim}
  3841. After this, the compiler can be compiled with the following command-line:
  3842. \begin{verbatim}
  3843. ppc386 -Tlinux -Fu../rtl/linux -di386 -dGDB pp.pas
  3844. \end{verbatim}
  3845. So, the minimum options are:
  3846. \begin{enumerate}
  3847. \item The target OS. Can be skipped if you're compiling for the same target as
  3848. the compiler you're using.
  3849. \item A path to an RTL. Can be skipped if a correct ppc386.cfg configuration
  3850. is on your system. If you want to compile with the RTL you compiled first,
  3851. this should be \file{../rtl/OS} (replace the OS with the appropriate
  3852. operating system subdirectory of the RTL).
  3853. \item A define with the processor you're compiling for. is required.
  3854. \item \var{-dGDB} is not strictly needed, but is better to add since
  3855. otherwise you won't be able to compile with debug information.
  3856. \item \var{-Sg} is needed, some parts of the compiler use \var{goto}
  3857. statements (to be specific: the scanner).
  3858. \end{enumerate}
  3859. So the absolute minimal command line is
  3860. \begin{verbatim}
  3861. ppc386 -di386 -Sg pp.pas
  3862. \end{verbatim}
  3863. You can define some other command-line options, but the above are the
  3864. minimum. A list of recognised options can be found in \seet{FPCdefines}.
  3865. \begin{FPCltable}{ll}{Possible defines when compiling FPC}{FPCdefines}
  3866. Define & does what \\ \hline
  3867. USE\_RHIDE & Generates errors and warnings in a format recognized\\
  3868. & by \file{RHIDE}. \\
  3869. TP & Needed to compile the compiler with Turbo or Borland Pascal. \\
  3870. Delphi & Needed to compile the compiler with Delphi from Borland. \\
  3871. GDB & Support of the GNU Debugger. \\
  3872. I386 & Generate a compiler for the Intel i386+ processor family. \\
  3873. M68K & Generate a compiler for the M68000 processor family. \\
  3874. USEOVERLAY & Compiles a TP version which uses overlays. \\
  3875. EXTDEBUG & Some extra debug code is executed. \\
  3876. SUPPORT\_MMX & only i386: enables the compiler switch \var{MMX} which \\
  3877. &allows the compiler to generate MMX instructions.\\
  3878. EXTERN\_MSG & Don't compile the msgfiles in the compiler, always use \\
  3879. & external messagefiles (default for TP).\\
  3880. NOAG386INT & no Intel Assembler output.\\
  3881. NOAG386NSM & no NASM output.\\
  3882. NOAG386BIN & leaves out the binary writer.\\ \hline
  3883. \end{FPCltable}
  3884. This list may be subject to change, the source file \file{pp.pas} always
  3885. contains an up-to-date list.
  3886. \end{document}