prog.tex 216 KB

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