prog.tex 162 KB

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