prog.tex 222 KB

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