prog.tex 279 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536
  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. \input{preamble.inc}
  24. \begin{latexonly}
  25. \ifpdf
  26. \pdfinfo{/Author(Michael Van Canneyt)
  27. /Title(Programmers' Guide)
  28. /Subject(Free Pascal Programmers' guide)
  29. /Keywords(Free Pascal)
  30. }
  31. \fi
  32. \end{latexonly}
  33. %
  34. % Settings
  35. %
  36. \makeindex
  37. \FPCexampledir{progex}
  38. %
  39. % Start of document.
  40. %
  41. \begin{document}
  42. \title{Free Pascal \\ Programmers' manual}
  43. \docdescription{Programmers' manual for \fpc, version \fpcversion}
  44. \docversion{1.10}
  45. \input{date.inc}
  46. \author{Micha\"el Van Canneyt}
  47. \maketitle
  48. \tableofcontents
  49. \newpage
  50. \listoftables
  51. \newpage
  52. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  53. % Introduction
  54. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55. \section{About this document}
  56. This is the programmer's manual for \fpc.
  57. It describes some of the peculiarities of the \fpc compiler, and provides a
  58. glimpse of how the compiler generates its code, and how you can change the
  59. generated code. It will not, however, provide a detailed account of
  60. the inner workings of the compiler, nor will it describe how to use the
  61. compiler (described in the \userref). It also will not describe the inner
  62. workings of the Run-Time Library (RTL). The best way to learn about the way
  63. the RTL is implemented is from the sources themselves.
  64. The things described here are useful when things need to be done that
  65. require greater flexibility than the standard Pascal language constructs
  66. (described in the \refref).
  67. Since the compiler is continuously under development, this document may get
  68. out of date. Wherever possible, the information in this manual will be
  69. updated. If you find something which isn't correct, or you think something
  70. is missing, feel free to contact me\footnote{at
  71. \var{[email protected]}}.
  72. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  73. % Compiler switches
  74. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  75. \chapter{Compiler directives}
  76. \label{ch:CompSwitch}
  77. \fpc supports compiler directives in the source file. They are not the same
  78. as Turbo Pascal directives, although some are supported for compatibility.
  79. There is a distinction between local and global directives; local directives
  80. take effect from the moment they are encountered, global directives have an
  81. effect on all of the compiled code.
  82. Many switches have a long form also. If they do, then the name of the
  83. long form is given also. For long switches, the + or - character to switch
  84. the option on or off, may be replaced by \var{ON} or \var{OFF} keywords.
  85. Thus \verb|{$I+}| is equivalent to \verb|{$IOCHECKS ON}| or
  86. \verb|{$IOCHECKS +}| and
  87. \verb|{$C-}| is equivalent to \verb|{$ASSERTIONS OFF}| or
  88. \verb|{$ASSERTIONS -}|
  89. The long forms of the switches are the same as their Delphi
  90. counterparts.
  91. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  92. % Local switches
  93. \section{Local directives}
  94. \label{se:LocalSwitch}
  95. Local directives can occur more than once in a unit or program,
  96. If they have a command-line counterpart, the command-line argument is
  97. restored as the default for each compiled file. The local directives
  98. influence the compiler's behaviour from the moment they're encountered
  99. until the moment another switch annihilates their behaviour, or the end
  100. of the current unit or program is reached.
  101. \subsection{\var{\protect\$A} or \var{\protect\$ALIGN} : Align Data}
  102. \label{se:Align}
  103. The \var{\{\$ALIGN} directive can be used to select the data alignment
  104. strategy of the compiler for the \macos. Only valid in MACPAS mode,
  105. it can have the following values:
  106. \begin{description}
  107. \item[MAC68K]
  108. \item[POWER]
  109. \item[RESET]
  110. \end{description}
  111. \subsection{\var{\protect\$ASMMODE} : Assembler mode (Intel 80x86 only)}
  112. \label{se:AsmReader}
  113. The \var{\{\$ASMMODE XXX\}} directive informs the compiler what kind of assembler
  114. it can expect in an \var{asm} block. The \var{XXX} should be replaced by one
  115. of the following:
  116. \begin{description}
  117. \item [att\ ] Indicates that \var{asm} blocks contain AT\&T syntax assembler.
  118. \item [intel\ ] Indicates that \var{asm} blocks contain Intel syntax
  119. assembler.
  120. \item [direct\ ] Tells the compiler that asm blocks should be copied
  121. directly to the assembler file.
  122. \end{description}
  123. These switches are local, and retain their value to the end of the unit that
  124. is compiled, unless they are replaced by another directive of the same type.
  125. The command-line switch that corresponds to this switch is \var{-R}.
  126. The default assembler reader is the AT\&T reader.
  127. \subsection{\var{\protect\$B} or \var{\protect\$BOOLEVAL} : Complete boolean evaluation}
  128. This switch is understood by the \fpc compiler, but is ignored. The compiler
  129. always uses shortcut evaluation, i.e. the evaluation of a boolean expression
  130. is stopped once the result of the total exression is known with certainty.
  131. So, in the following example, the function \var{Bofu}, which has a boolean
  132. result, will never get called.
  133. \begin{verbatim}
  134. If False and Bofu then
  135. ...
  136. \end{verbatim}
  137. This has as a consequence that any additional actions that are done by
  138. \var{Bofu} are not executed.
  139. \subsection{\var{\protect\$C} or \var{\protect\$ASSERTIONS} : Assertion support}
  140. The \var{\{\$ASSERTIONS\}} switch determines if assert statements are
  141. compiled into the binary or not. If the switch is on, the statement
  142. \begin{verbatim}
  143. Assert(BooleanExpression,AssertMessage);
  144. \end{verbatim}
  145. Will be compiled in the binary. If te \var{BooleanExpression} evaluates to
  146. \var{False}, the RTL will check if the \var{AssertErrorProc} is set. If it
  147. is set, it will be called with as parameters the \var{AssertMessage}
  148. message, the name of the file, the LineNumber and the address. If it is not
  149. set, a runtime error 227 is generated.
  150. The \var{AssertErrorProc} is defined as
  151. \begin{verbatim}
  152. Type
  153. TAssertErrorProc=procedure(const msg,fname:string;lineno,erroraddr:longint);
  154. Var
  155. AssertErrorProc = TAssertErrorProc;
  156. \end{verbatim}
  157. This can be used mainly for debugging purposes. The \file{system} unit sets the
  158. \var{AssertErrorProc} to a handler that displays a message on \var{stderr}
  159. and simply exits. The \file{sysutils} unit catches the run-time error 227
  160. and raises an \var{EAssertionFailed} exception.
  161. \subsection{\var{\protect\$DEFINE} : Define a symbol}
  162. The directive
  163. \begin{verbatim}
  164. {$DEFINE name}
  165. \end{verbatim}
  166. defines the symbol \var{name}. This symbol remains defined until the end of
  167. the current module (i.e. unit or program), or until a \var{\$UNDEF name} directive is encountered.
  168. If \var{name} is already defined, this has no effect. \var{Name} is case
  169. insensitive.
  170. The symbols that are defined in a unit, are not saved in the unit file,
  171. so they are also not exported from a unit.
  172. \subsection{\var{\protect\$ELSE} : Switch conditional compilation}
  173. The \var{\{\$ELSE\}} switches between compiling and ignoring the source
  174. text delimited by the preceding \var{\{\$IFxxx\}} and following
  175. \var{\{\$ENDIF\}}. Any text after the \var{ELSE} keyword but before the
  176. brace is ignored:
  177. \begin{verbatim}
  178. {$ELSE some ignored text}
  179. \end{verbatim}
  180. is the same as
  181. \begin{verbatim}
  182. {$ELSE}
  183. \end{verbatim}
  184. This is useful for indication what switch is meant.
  185. \subsection{\var{\protect\$ELSEC} : Switch conditional compilation}
  186. In MACPAS mode, this directive can be used as an alternative to the \var{\$ELSE}
  187. directive. It is supported for compatibility with existing \macos pascal
  188. compilers.
  189. \subsection{\var{\protect\$ENDC} : End conditional compilation}
  190. In MACPAS mode, this directive can be used as an alternative to the
  191. \var{\$ENDIF} directive. It is supported for compatibility with existing \macos pascal
  192. compilers.
  193. \subsection{\var{\protect\$ENDIF} : End conditional compilation}
  194. The \var{\{\$ENDIF\}} directive ends the conditional compilation initiated by the
  195. last \var{\{\$IFxxx\}} directive. Any text after the \var{ENDIF} keyword but
  196. before the closing brace is ignored:
  197. \begin{verbatim}
  198. {$ENDIF some ignored text}
  199. \end{verbatim}
  200. is the same as
  201. \begin{verbatim}
  202. {$ENDIF}
  203. \end{verbatim}
  204. This is useful for indication what switch is meant to be ended.
  205. \subsection{\var{\protect\$ERROR} : Generate error message}
  206. The following code
  207. \begin{verbatim}
  208. {$ERROR This code is erroneous !}
  209. \end{verbatim}
  210. will display an error message when the compiler encounters it,
  211. and increase the error count of the compiler.
  212. The compiler will continue to compile, but no code will be emitted.
  213. \subsection{\var{\protect\$F} : Far or near functions}
  214. This directive is recognized for compatibility with Turbo Pascal. Under the
  215. 32-bit and 64-bit programming models, the concept of near and far calls have
  216. no meaning, hence the directive is ignored. A warning is printed to the screen,
  217. as a reminder.
  218. As an example, the following piece of code:
  219. \begin{verbatim}
  220. {$F+}
  221. Procedure TestProc;
  222. begin
  223. Writeln ('Hello From TestProc');
  224. end;
  225. begin
  226. testProc
  227. end.
  228. \end{verbatim}
  229. Generates the following compiler output:
  230. \begin{verbatim}
  231. malpertuus: >pp -vw testf
  232. Compiler: ppc386
  233. Units are searched in: /home/michael;/usr/bin/;/usr/lib/ppc/0.9.1/linuxunits
  234. Target OS: Linux
  235. Compiling testf.pp
  236. testf.pp(1) Warning: illegal compiler switch
  237. 7739 kB free
  238. Calling assembler...
  239. Assembled...
  240. Calling linker...
  241. 12 lines compiled,
  242. 1.00000000000000E+0000
  243. \end{verbatim}
  244. One can see that the verbosity level was set to display warnings.
  245. When declaring a function as \var{Far} (this has the same effect as setting it
  246. between \var{\{\$F+\} \dots \{\$F-\}} directives), the compiler also generates a
  247. warning:
  248. \begin{verbatim}
  249. testf.pp(3) Warning: FAR ignored
  250. \end{verbatim}
  251. The same story is true for procedures declared as \var{Near}. The warning
  252. displayed in that case is:
  253. \begin{verbatim}
  254. testf.pp(3) Warning: NEAR ignored
  255. \end{verbatim}
  256. \subsection{\var{\protect\$FATAL} : Generate fatal error message}
  257. The following code
  258. \begin{verbatim}
  259. {$FATAL This code is erroneous !}
  260. \end{verbatim}
  261. will display an error message when the compiler encounters it,
  262. and the compiler will immediatly stop the compilation process.
  263. This is mainly useful inc conjunction wih \var{\{\$IFDEF\}} or
  264. \var{\{\$IFOPT\}} statements.
  265. \subsection{\var{\protect\$FPUTYPE} : Select coprocessor type}
  266. This directive selects the type of coprocessor used to do floating point
  267. calculations. The directive must be followed by the type of floating point
  268. unit. The allowed values depend on the target CPU:
  269. \begin{description}
  270. \item[all] \var{SOFT}: FPC emulates the coprocessor (not yet implemented).
  271. \item[i386] \var{X87}, \var{SSE}, \var{SSE2}: code compiled with \var{SSE}
  272. uses the sse to do calculations involving a float of type \var{Single}.
  273. This code runs only on Pentium III and above, or AthlonXP and above.
  274. Code compiled with \var{SSE2} uses the \file{sse} unit to do calculations
  275. with the single and double data type. This code runs only on PentiumIV
  276. and above or Athlon64 and above
  277. \item[x86-64] \var{SSE64}
  278. \item[powerpc] \var{STANDARD}
  279. \item[arm] \var{LIBGCC}, \var{FPA}, \var{FPA10}, \var{FPA11},\var{VFP}.
  280. \end{description}
  281. This directive corresponds to the \var{-Cf} command-line option.
  282. \subsection{\var{\protect\$GOTO} : Support \var{Goto} and \var{Label}}
  283. If \var{\{\$GOTO ON\}} is specified, the compiler will support \var{Goto}
  284. statements and \var{Label} declarations. By default, \var{\$GOTO OFF} is
  285. assumed. This directive corresponds to the \var{-Sg} command-line option.
  286. As an example, the following code can be compiled:
  287. \begin{verbatim}
  288. {$GOTO ON}
  289. label Theend;
  290. begin
  291. If ParamCount=0 then
  292. GoTo TheEnd;
  293. Writeln ('You specified command-line options');
  294. TheEnd:
  295. end.
  296. \end{verbatim}
  297. \begin{remark}When compiling assembler code using the inline assembler
  298. readers, any labels used in the assembler code must be declared, and
  299. the \var{\{\$GOTO ON\}} directive should be used.
  300. \end{remark}
  301. \subsection{\var{\protect\$H} or \var{\protect\$LONGSTRINGS} : Use AnsiStrings}
  302. If \var{\{\$LONGSTRINGS ON\}} is specified, the keyword \var{String} (no
  303. length specifier) will be treated as \var{AnsiString}, and the compiler
  304. will treat the corresponding variable as an ansistring, and will
  305. generate corresponding code.
  306. By default, the use of ansistrings is off, corresponding to \var{\{\$H-\}}.
  307. The \file{system} unit is compiled without ansistrings, all its functions accept
  308. shortstring arguments. The same is true for all RTL units, except the
  309. \file{sysutils} unit, which is compiled with ansistrings.
  310. \subsection{\var{\protect\$HINT} : Generate hint message}
  311. If the generation of hints is turned on, through the \var{-vh} command-line
  312. option or the \var{\{\$HINTS ON\}} directive, then
  313. \begin{verbatim}
  314. {$Hint This code should be optimized }
  315. \end{verbatim}
  316. will display a hint message when the compiler encounters it.
  317. By default, no hints are generated.
  318. \subsection{\var{\protect\$HINTS} : Emit hints}
  319. \var{\{\$HINTS ON\}} switches the generation of hints on.
  320. \var{\{\$HINTS OFF\}} switches the generation of hints off.
  321. Contrary to the command-line option \var{-vh} this is a local switch,
  322. this is useful for checking parts of the code.
  323. \subsection{\var{\protect\$IF} : Start conditional compilation}
  324. The directive \var{\{\$IF expr\}} will continue the compilation
  325. if the boolean expression \var{expr} evaluates to \var{true}. If the
  326. compilation evaluates to false, then the source is skipped to the first
  327. \var{\{\$ELSE\}} or \var{\{\$ENDIF\}} directive.
  328. The compiler must be able to evaluate the expression at parse time.
  329. This means that variables or constants that are defined in
  330. the source cannot be used. Macros and symbols may be used, however.
  331. More information on this can be found in the section about
  332. conditionals.
  333. \subsection{\var{\protect\$IFC} : Start conditional compilation}
  334. In MACPAS mode, this directive can be used as an alternative to the \var{\$IF}
  335. directive. It is supported for compatibility with existing \macos pascal
  336. compilers.
  337. \subsection{\var{\protect\$IFDEF Name} : Start conditional compilation}
  338. If the symbol \var{Name} is not defined then the \var{\{\$IFDEF name\}}
  339. will skip the compilation of the text that follows it to the first
  340. \var{\{\$ELSE\}} or \var{\{\$ENDIF\}} directive.
  341. If \var{Name} is defined, then compilation continues as if the directive
  342. wasn't there.
  343. \subsection{\var{\protect\$IFNDEF} : Start conditional compilation}
  344. If the symbol \var{Name} is defined then the \var{\{\$IFNDEF name\}}
  345. will skip the compilation of the text that follows it to the first
  346. \var{\{\$ELSE\}} or \var{\{\$ENDIF\}} directive.
  347. If it is not defined, then compilation continues as if the directive
  348. wasn't there.
  349. \subsection{\var{\protect\$IFOPT} : Start conditional compilation}
  350. The \var{\{\$IFOPT switch\}} will compile the text that follows it if the
  351. switch \var{switch} is currently in the specified state.
  352. If it isn't in the specified state, then compilation continues after the
  353. corresponding \var{\{\$ELSE\}} or \var{\{\$ENDIF\}} directive.
  354. As an example:
  355. \begin{verbatim}
  356. {$IFOPT M+}
  357. Writeln ('Compiled with type information');
  358. {$ENDIF}
  359. \end{verbatim}
  360. Will compile the writeln statement if generation of type information is on.
  361. \begin{remark}The \var{\{\$IFOPT\}} directive accepts only short options,
  362. i.e. \var{\{\$IFOPT TYPEINFO\}} will not be accepted.
  363. \end{remark}
  364. \subsection{\var{\protect\$IMPLICITEXCEPTIONS} : Do not generate
  365. finalization code}
  366. The compiler generates an implicit \var{try}...\var{finally} frame around
  367. each procedure that needs initialization or finalization of variables, and
  368. finalizes the variables in the \var{finally} block. This
  369. slows down these procedures (with 5-10% sometimes). With this directive, the generation of such
  370. frames can be disabled. One should be careful with this directive, because
  371. it can lead to memory leaks if an exception occurs inside the routine.
  372. Therefore, standard it is set to \var{OFF}.
  373. \subsection{\var{\protect\$INFO} : Generate info message}
  374. If the generation of info is turned on, through the \var{-vi} command-line
  375. option, then
  376. \begin{verbatim}
  377. {$INFO This was coded on a rainy day by Bugs Bunny}
  378. \end{verbatim}
  379. will display an info message when the compiler encounters it.
  380. This is useful in conjunction with the \var{\{\$IFDEF\}} directive, to show
  381. information about which part of the code is being compiled.
  382. \subsection{\var{\protect\$INLINE} : Allow inline code.}
  383. The \var{\{\$INLINE ON\}} directive tells the compiler that the \var{Inline}
  384. procedure modifier should be allowed. Procedures that are declared inline
  385. are copied to the places where they are called. This has the effect that
  386. there is no actual procedure call, the code of the procedure is just copied
  387. to where the procedure is needed, this results in faster execution speed if
  388. the function or procedure is used a lot.
  389. By default, \var{Inline} procedures are not allowed. This directive must be
  390. specified to use inlined code. The directive is equivalent to the command-line
  391. switch \var{-Si}. For more information on inline routines, consult the \refref.
  392. \subsection{\var{\protect\$INTERFACES} : Specify Interface type.}
  393. The \var{\{\$INTERFACES\}} directive tells the compiler what it should take
  394. as the parent interface of an interface declaration which does not
  395. explicitly specify a parent interface. By default the Windows COM \var{IUnknown}
  396. interface is used. Other implementations of interfaces (CORBA or Java) do
  397. not necessarily have this interface, and for such cases, this directive can
  398. be used. It accepts the following three values:
  399. \begin{description}
  400. \item[COM] Interfaces will descend from \var{IUnknown}.
  401. \item[CORBA] Interfaces will not have a parent.
  402. \item[DEFAULT] Currently, this is COM.
  403. \end{description}
  404. \subsection{\var{\protect\$I} or \var{\protect\$IOCHECKS} : Input/Output checking}
  405. The \var{\{\$I-\}} or \var{\{\$IOCHECKS OFF\}} directive tells the compiler
  406. not to generate input/output checking code in the program. By default, the
  407. compiler does not generate this code, it must be switched on using the \var{-Ci}
  408. command-line switch.
  409. When compiling using the \var{-Ci} compiler switch, the \fpc compiler inserts
  410. input/output checking code after every input/output call in the code.
  411. If an error occurred during input or output, then a run-time error will
  412. be generated. Use this switch to avoid this behaviour.
  413. To check if something went wrong, the \var{IOResult} function can be used
  414. to see if everything went without problems.
  415. Conversely, \var{\{\$I+\}} will turn error-checking back on, until another
  416. directive is encountered which turns it off again.
  417. The most common use for this switch is to check if the opening of a file
  418. went without problems, as in the following piece of code:
  419. \begin{verbatim}
  420. assign (f,'file.txt');
  421. {$I-}
  422. rewrite (f);
  423. {$I+}
  424. if IOResult<>0 then
  425. begin
  426. Writeln ('Error opening file: "file.txt"');
  427. exit
  428. end;
  429. \end{verbatim}
  430. See the \var{IOResult} function explanation in \refref for a
  431. detailed description of the possible errors that can occur when using
  432. input/output checking.
  433. \subsection{\var{\protect\$I} or \var{\protect\$INCLUDE} : Include file }
  434. The \var{\{\$I filename\}} or \var{\{\$INCLUDE filename\}} directive
  435. tells the compiler to read further statements from the file \var{filename}.
  436. The statements read there will be inserted as if they occurred in the
  437. current file.
  438. The compiler will append the \file{.pp} extension to the file if no
  439. extension is given. Do not put the filename between quotes, as
  440. they will be regarded as part of the file's name.
  441. Include files can be nested, but not infinitely deep. The number of files is
  442. restricted to the number of file descriptors available to the \fpc compiler.
  443. Contrary to Turbo Pascal, include files can cross blocks. I.e.
  444. a block can start in one file (with a \var{Begin} keyword) and can end in another (with
  445. a \var{End} keyword). The smallest entity in an include file must be a token,
  446. i.e. an identifier, keyword or operator.
  447. The compiler will look for the file to include in the following places:
  448. \begin{enumerate}
  449. \item It will look in the path specified in the include file name.
  450. \item It will look in the directory where the current source file is.
  451. \item it will look in all directories specified in the include file search
  452. path.
  453. \end{enumerate}
  454. Directories can be added to the include file search path with the \var{-I}
  455. command-line option.
  456. \subsection{\var{\protect\$I} or \var{\protect\$INCLUDE} : Include compiler info}
  457. In this form:
  458. \begin{verbatim}
  459. {$INCLUDE %xxx%}
  460. \end{verbatim}
  461. where \var{xxx} is one of the following:
  462. \begin{description}
  463. \item[DATE] Inserts the current date.
  464. \item[FPCTARGET] Inserts the target CPU name. (deprecated, use
  465. \var{FPCTARGETCPU})
  466. \item[FPCTARGETCPU] Inserts the target CPU name.
  467. \item[FPCTARGETOS] Inserts the target OS name.
  468. \item[FPCVERSION] Current compiler version number.
  469. \item[FILE] Filename in which the directive is found.
  470. \item[LINE] Linenumer on which the directive is found.
  471. \item[TIME] Current time.
  472. \end{description}
  473. If \var{xxx} is none of the above, then it is assumed to be the name of
  474. an environment variable. Its value will be fetched.
  475. As a reult, this will generate a macro with the value of these things,
  476. as if it were a string.
  477. For example, the following program
  478. \begin{verbatim}
  479. Program InfoDemo;
  480. Const User = {$I %USER%};
  481. begin
  482. Write ('This program was compiled at ',{$I %TIME%});
  483. Writeln (' on ',{$I %DATE%});
  484. Writeln ('By ',User);
  485. Writeln ('Compiler version: ',{$I %FPCVERSION%});
  486. Writeln ('Target CPU: ',{$I %FPCTARGET%});
  487. end.
  488. \end{verbatim}
  489. Creates the following output:
  490. \begin{verbatim}
  491. This program was compiled at 17:40:18 on 1998/09/09
  492. By michael
  493. Compiler version: 0.99.7
  494. Target CPU: i386
  495. \end{verbatim}
  496. % Assembler type
  497. \subsection{\var{\protect\$I386\_XXX} : Specify assembler format (Intel 80x86 only)}
  498. This switch selects the assembler reader. \var{\{\$I386\_XXX\}}
  499. has the same effect as \var{\{\$ASMMODE XXX\}}, \sees{AsmReader}
  500. This switch is deprecated, the \var{\{\$ASMMODE XXX\}} directive should
  501. be used instead.
  502. \subsection{\var{\protect\$L} or \var{\protect\$LINK} : Link object file}
  503. The \var{\{\$L filename\}} or \var{\{\$LINK filename\}} directive
  504. tells the compiler that the file \file{filename} should be linked to
  505. the program. This cannot be used for libraries, see section
  506. \sees{linklib} for that.
  507. The compiler will look for this file in the following way:
  508. \begin{enumerate}
  509. \item It will look in the path specified in the object file name.
  510. \item It will look in the directory where the current source file is.
  511. \item it will look in all directories specified in the object file search path.
  512. \end{enumerate}
  513. Directories can be added to the object file search path with the \var{-Fo}
  514. option.
  515. On \linux systems and on operating systems with case-sensitive filesystems
  516. (such as \unix systems), the name is case sensitive, and must be typed
  517. exactly as it appears on your system.
  518. \begin{remark}Take care that the object file you're linking is in a
  519. format the linker understands. Which format this is, depends on the platform
  520. you're on. Typing \var{ld} or \var{ld --help} on the command line gives a list of formats
  521. \var{ld} knows about.
  522. \end{remark}
  523. Other files and options can be passed to the linker using the \var{-k}
  524. command-line option. More than one of these options can be used, and
  525. they will be passed to the linker, in the order that they were specified on
  526. the command line, just before the names of the object files that must be
  527. linked.
  528. \subsection{\var{\protect\$LINKLIB} : Link to a library}
  529. \label{se:linklib}
  530. The \var{\{\$LINKLIB name\}} will link to a library \file{name}.
  531. This has the effect of passing \var{-lname} to the linker.
  532. As an example, consider the following unit:
  533. \begin{verbatim}
  534. unit getlen;
  535. interface
  536. {$LINKLIB c}
  537. function strlen (P : pchar) : longint;cdecl;
  538. implementation
  539. function strlen (P : pchar) : longint;cdecl;external;
  540. end.
  541. \end{verbatim}
  542. If one would issue the command
  543. \begin{verbatim}
  544. ppc386 foo.pp
  545. \end{verbatim}
  546. where foo.pp has the above unit in its \var{uses} clause,
  547. then the compiler would link the program to the c library, by passing the
  548. linker the \var{-lc} option.
  549. The same effect could be obtained by removing the linklib directive in the
  550. above unit, and specify \var{-k-lc} on the command-line:
  551. \begin{verbatim}
  552. ppc386 -k-lc foo.pp
  553. \end{verbatim}
  554. \subsection{\var{\protect\$M} or \var{\protect\$TYPEINFO} : Generate type info}
  555. For classes that are compiled in the \var{\{\$M+\}} or \var{\{\$TYPEINFO ON\}}
  556. state, the compiler will generate Run-Time Type Information (RTTI). All
  557. descendent objects of an object that was compiled in the \var{\{\$M+\}} state
  558. will get RTTI information too, as well as any published classes.
  559. By default, no Run-Time Type Information is generated. The \var{TPersistent}
  560. object that is present in the FCL (Free Component Library) is generated in
  561. the \var{\{\$M+\}} state. The generation of RTTI allows programmers to
  562. stream objects, and to access published properties of objects, without
  563. knowing the actual class of the object.
  564. The run-time type information is accessible through the \var{TypInfo} unit,
  565. which is part of the \fpc Run-Time Library.
  566. \begin{remark}
  567. The streaming system implemented by \fpc requires that all
  568. streamable components be descendent from \var{TPersistent}.
  569. \end{remark}
  570. \subsection{\var{\protect\$MACRO} : Allow use of macros.}
  571. In the \var{\{\$MACRO ON\}} state, the compiler allows to use C-style
  572. (although not as elaborate) macros. Macros provide a means for simple text
  573. substitution. More information on using macros can be found in the
  574. \sees{Macros} section. This directive is equivalent to the command-line
  575. switch \var{-Sm}.
  576. By default, macros are not allowed.
  577. \subsection{\var{\protect\$MAXFPUREGISTERS} : Maximum number of FPU registers for variables}
  578. The \var{\{\$MAXFPUREGISTERS XXX\}} directive tells the compiler how much floating point
  579. variables can be kept in the floating point processor registers on an Intel
  580. X86 processor. This switch is ignored unless the \var{-Or} (use register variables)
  581. optimization is used.
  582. This is quite tricky because the Intel FPU stack is limited to 8 entries.
  583. The compiler uses a heuristic algorithm to determine how much variables should be
  584. put onto the stack: in leaf procedures it is limited to 3 and in non leaf
  585. procedures to 1. But in case of a deep call tree or, even worse, a recursive
  586. procedure this can still lead to a FPU stack overflow, so the user can tell
  587. the compiler how much (floating point) variables should be kept in registers.
  588. The directive accepts the following arguments:
  589. \begin{description}
  590. \item [N] where \var{N} is the maximum number of FPU registers to use.
  591. Currently this can be in the range 0 to 7.
  592. \item[Normal] restores the heuristic and standard behavior.
  593. \item[Default] restores the heuristic and standard behaviour.
  594. \end{description}
  595. \begin{remark}This directive is valid until the end of the current procedure.
  596. \end{remark}
  597. \subsection{\var{\protect\$MESSAGE} : Generate info message}
  598. If the generation of info is turned on, through the \var{-vi} command-line
  599. option, then
  600. \begin{verbatim}
  601. {$MESSAGE This was coded on a rainy day by Bugs Bunny }
  602. \end{verbatim}
  603. will display an info message when the compiler encounters it. The effect is
  604. the same as the \var{\{\$INFO\}} directive.
  605. \subsection{\var{\protect\$MMX} : Intel MMX support (Intel 80x86 only)}
  606. \fpc supports optimization for the \textbf{MMX} Intel
  607. processor (see also chapter \ref{ch:MMXSupport}).
  608. This optimizes certain code parts for the \textbf{MMX} Intel
  609. processor, thus greatly improving speed. The speed is noticed mostly when
  610. moving large amounts of data. Things that change are
  611. \begin{itemize}
  612. \item Data with a size that is a multiple of 8 bytes is moved using the
  613. \var{movq} assembler instruction, which moves 8 bytes at a time
  614. \end{itemize}
  615. \begin{remark}MMX support is NOT emulated on non-MMX systems, i.e. if
  616. the processor doesn't have the MMX extensions, the MMX optimizations cannot
  617. be used.
  618. \end{remark}
  619. When \textbf{MMX} support is on, it is not allowed to do floating point
  620. arithmetic. It is allowed to move floating point data, but no arithmetic
  621. can be done. If floating point math must be done anyway, first \textbf{MMX}
  622. support must be switched off and the FPU must be cleared using the \var{emms}
  623. function of the \file{cpu} unit.
  624. The following example will make this more clear:
  625. \begin{verbatim}
  626. Program MMXDemo;
  627. uses mmx;
  628. var
  629. d1 : double;
  630. a : array[0..10000] of double;
  631. i : longint;
  632. begin
  633. d1:=1.0;
  634. {$mmx+}
  635. { floating point data is used, but we do _no_ arithmetic }
  636. for i:=0 to 10000 do
  637. a[i]:=d2; { this is done with 64 bit moves }
  638. {$mmx-}
  639. emms; { clear fpu }
  640. { now we can do floating point arithmetic }
  641. ...
  642. end.
  643. \end{verbatim}
  644. See, however, the chapter on MMX (\ref{ch:MMXSupport}) for more information
  645. on this topic.
  646. \subsection{\var{\protect\$NOTE} : Generate note message}
  647. If the generation of notes is turned on, through the \var{-vn} command-line
  648. option or the \var{\{\$NOTES ON\}} directive, then
  649. \begin{verbatim}
  650. {$NOTE Ask Santa Claus to look at this code}
  651. \end{verbatim}
  652. will display a note message when the compiler encounters it.
  653. \subsection{\var{\protect\$NOTES} : Emit notes}
  654. \var{\{\$NOTES ON\}} switches the generation of notes on.
  655. \var{\{\$NOTES OFF\}} switches the generation of notes off.
  656. Contrary to the command-line option \var{-vn} this is a local switch,
  657. this is useful for checking parts of the code.
  658. By default, \var{\{\$NOTES\}} is off.
  659. \subsection{\var{\protect\$OUTPUT\_FORMAT} : Specify the output format}
  660. \var{\{\$OUTPUT\_FORMAT format\}} has the same functionality as the \var{-A}
  661. command-line option: it tells the compiler what kind of object file must be
  662. generated. You can specify this switch only {\em before} the \var{Program}
  663. or \var{Unit} clause in your source file. The different kinds of formats are
  664. shown in \seet{Formats}.
  665. The default output format depends on the platform the compiler was compiled
  666. on.
  667. \begin{FPCltable}{ll}{Formats generated by the x86 compiler}{Formats} \hline
  668. Switch value & Generated format \\ \hline
  669. AS & AT\&T assembler file. \\
  670. AS\_AOUT & Go32v1 assembler file.\\
  671. ASW & AT\&T Win32 assembler file. \\
  672. COFF & Go32v2 COFF object file.\\
  673. MASM & Masm assembler file.\\
  674. NASM & Nasm assembler file.\\
  675. NASMCOFF & Nasm assembler file (COFF format).\\
  676. NASMELF & Nasm assembler file (ELF format).\\
  677. PECOFF & PECOFF object file (Win32).\\
  678. TASM & Tasm assembler file.\\
  679. \end{FPCltable}
  680. \subsection{\var{\protect\$P} or \var{\protect\$OPENSTRINGS} : Use open strings}
  681. If this switch is on, all function or procedure parameters of type string
  682. are considered to be open string parameters; this parameter only has effect
  683. for short strings, not for ansistrings.
  684. When using openstrings, the declared type of the string can be different
  685. from the type of string that is actually passed, even for strings that are
  686. passed by reference. The declared size of the string passed can be examined
  687. with the \var{High(P)} call.
  688. Default the use of openstrings is off.
  689. \subsection{\var{\protect\$PACKENUM} : Minimum enumeration type size}
  690. This directive tells the compiler the minimum number of bytes it should
  691. use when storing enumerated types. It is of the following form:
  692. \begin{verbatim}
  693. {$PACKENUM xxx}
  694. {$MINENUMSIZE xxx}
  695. \end{verbatim}
  696. Where the form with \var{\$MINENUMSIZE} is for Delphi compatibility.
  697. \var{xxx} can be one of \var{1,2} or \var{4}, or \var{NORMAL} or
  698. \var{DEFAULT}.
  699. As an alternative form one can use \var{\{\$Z1\}}, \var{\{\$Z2\}}
  700. \var{\{\$Z4\}}. Contrary to Delphi, the default is (\var{\{\$Z4\}}).
  701. So the following code
  702. \begin{verbatim}
  703. {$PACKENUM 1}
  704. Type
  705. Days = (monday, tuesday, wednesday, thursday, friday,
  706. saturday, sunday);
  707. \end{verbatim}
  708. will use 1 byte to store a variable of type \var{Days}, whereas it nomally
  709. would use 4 bytes. The above code is equivalent to
  710. \begin{verbatim}
  711. {$Z1}
  712. Type
  713. Days = (monday, tuesday, wednesday, thursday, friday,
  714. saturday, sunday);
  715. \end{verbatim}
  716. \subsection{\var{\protect\$PACKRECORDS} : Alignment of record elements}
  717. This directive controls the byte alignment of the elements in a record,
  718. object or class type definition.
  719. It is of the following form:
  720. \begin{verbatim}
  721. {$PACKRECORDS n}
  722. \end{verbatim}
  723. Where \var{n} is one of 1, 2, 4, 16, \var{C}, \var{NORMAL} or \var{DEFAULT}.
  724. This means that the elements of a record that have size greater than \var{n}
  725. will be aligned on \var{n} byte boundaries. Elements with size less than or
  726. equal to \var{n} will be aligned to a natural boundary, i.e. to a power of
  727. two that is equal to or larger than the element's size. The type \var{C}
  728. is used to specify alignment as by the GNU CC compiler. It should be used
  729. only when making import units for C routines.
  730. The default alignment (which can be selected with \var{DEFAULT}) is 2,
  731. contrary to Turbo Pascal, where it is 1.
  732. More information on this and an example program can be found in the reference
  733. guide, in the section about record types.
  734. \subsection{\var{\protect\$Q} \var{\protect\$OVERFLOWCHECKS}: Overflow checking}
  735. The \var{\{\$Q+\}} or \var{\{\$OVERFLOWCHECKS ON\}} directive turns on
  736. integer overflow checking. This means that the compiler inserts code
  737. to check for overflow when doing computations with integers.
  738. When an overflow occurs, the run-time library will print a message
  739. \var{Overflow at xxx}, and exit the program with exit code 215.
  740. \begin{remark}Overflow checking behaviour is not the same as in
  741. Turbo Pascal since all arithmetic operations are done via 32-bit or
  742. 64-bit values. Furthermore, the \var{Inc()} and \var{Dec} standard system
  743. procedures {\em are} checked for overflow in \fpc, while in Turbo
  744. Pascal they are not.
  745. \end{remark}
  746. Using the \var{\{\$Q-\}} switch switches off the overflow checking code
  747. generation.
  748. The generation of overflow checking code can also be controlled
  749. using the \var{-Co} command line compiler option (see \userref).
  750. \subsection{\var{\protect\$R} or \var{\protect\$RANGECHECKS} : Range checking}
  751. By default, the compiler doesn't generate code to check the ranges of array
  752. indices, enumeration types, subrange types, etc. Specifying the
  753. \var{\{\$R+\}} switch tells the computer to generate code to check these
  754. indices. If, at run-time, an index or enumeration type is specified that is
  755. out of the declared range of the compiler, then a run-time error is
  756. generated, and the program exits with exit code 201. This can happen when
  757. doing a typecast (implicit or explicit) on an enumeration type or subrange
  758. type.
  759. The \var{\{\$RANGECHECKS OFF\}} switch tells the compiler not to generate range checking
  760. code. This may result in faulty program behaviour, but no run-time errors
  761. will be generated.
  762. \begin{remark}The standard functions \var{val} and \var{Read} will also check ranges
  763. when the call is compiled in \var{\{\$R+\}} mode.
  764. \end{remark}
  765. \subsection{\var{\protect\$SATURATION} : Saturation operations (Intel 80x86 only)}
  766. This works only on the intel compiler, and MMX support must be on
  767. (\var{\{\$MMX +\}}) for this to have any effect. See the section on
  768. saturation support (\sees{SaturationSupport}) for more information
  769. on the effect of this directive.
  770. \subsection{\var{\protect\$SETC} : Define and assign a value to a symbol}
  771. In MAC mode, this directive can be used to define compiler symbols. It is
  772. an alternative to the \var{\$DEFINE} directive for macros. It is supported for
  773. compatibility with existing \macos pascal compilers. It will define a symbol
  774. with a certain value (called a compiler variable expression).
  775. The expression syntax is similar to expressions used in macros, but the
  776. expression must be evaluated at compile-time by the compiler. This means
  777. that only some basic arithmetic and logical operators can be used,
  778. and some extra possibilities such as the \var{TRUE},\var{FALSE} and
  779. \var{UNDEFINED}
  780. operators:
  781. \begin{verbatim}
  782. {$SETC TARGET_CPU_PPC := NOT UNDEFINED CPUPOWERPC}
  783. {$SETC TARGET_CPU_68K := NOT UNDEFINED CPUM68K}
  784. {$SETC TARGET_CPU_X86 := NOT UNDEFINED CPUI386}
  785. {$SETC TARGET_CPU_MIPS := FALSE}
  786. {$SETC TARGET_OS_UNIX := (NOT UNDEFINED UNIX) AND (UNDEFINED DARWIN)}
  787. \end{verbatim}
  788. The \var{:=} assignment symbol may be replaced with the \var{=} symbol.
  789. Note that this command works only in MACPAS mode, but independent of the
  790. \var{-Sm} command-line option or \var{\{\$MACROS \}} directive.
  791. \subsection{\var{\protect\$STATIC} : Allow use of \var{Static} keyword.}
  792. If you specify the \var{\{\$STATIC ON\}} directive, then \var{Static}
  793. methods are allowed for objects. \var{Static} objects methods do not require
  794. a \var{Self} variable. They are equivalent to \var{Class} methods for
  795. classes. By default, \var{Static} methods are not allowed. Class methods
  796. are always allowed.
  797. By default, the address operator returns an untyped pointer.
  798. This directive is equivalent to the \var{-St} command-line option.
  799. \subsection{\var{\protect\$STOP} : Generate fatal error message}
  800. The following code
  801. \begin{verbatim}
  802. {$STOP This code is erroneous !}
  803. \end{verbatim}
  804. will display an error message when the compiler encounters it.
  805. The compiler will immediatly stop the compilation process.
  806. It has the same effect as the \var{\{\$FATAL\}} directive.
  807. \subsection{\var{\protect\$T} or \var{\protect\$TYPEDADDRESS} : Typed address operator (@)}
  808. In the \var{\{\$T+\}} or \var{\{\$TYPEDADDRESS ON\}} state the @ operator,
  809. when applied to a variable, returns a result of type \var{\^{}T}, if the
  810. type of the variable is \var{T}. In the \var{\{\$T-\}} state, the result is
  811. always an untyped pointer, which is assignment compatible with all other
  812. pointer types.
  813. \subsection{\var{\protect\$UNDEF} : Undefine a symbol}
  814. The directive
  815. \begin{verbatim}
  816. {$UNDEF name}
  817. \end{verbatim}
  818. un-defines the symbol \var{name} if it was previously defined.
  819. \var{Name} is case insensitive.
  820. \subsection{\var{\protect\$V} or \var{\protect\$VARSTRINGCHECKS} : Var-string checking}
  821. When in the \var{+} or \var{ON} state, the compiler checks that strings
  822. passed as parameters are of the same, identical, string type as the declared
  823. parameters of the procedure.
  824. \subsection{\var{\protect\$WAIT} : Wait for enter key press}
  825. If the compiler encounters a
  826. \begin{verbatim}
  827. {$WAIT}
  828. \end{verbatim}
  829. directive, it will resume compiling only after the user has pressed the
  830. enter key. If the generation of info messages is turned on, then the compiler
  831. will display the following message:
  832. \begin{verbatim}
  833. Press <return> to continue
  834. \end{verbatim}
  835. before waiting for a keypress.
  836. \begin{remark}This may interfere with automatic
  837. compilation processes. It should be used for debugging purposes only.
  838. \end{remark}
  839. \subsection{\var{\protect\$WARNING} : Generate warning message}
  840. If the generation of warnings is turned on, through the \var{-vw}
  841. command-line option or the \var{\{\$WARNINGS ON\}} directive, then
  842. \begin{verbatim}
  843. {$WARNING This is dubious code}
  844. \end{verbatim}
  845. will display a warning message when the compiler encounters it.
  846. \subsection{\var{\protect\$WARNINGS} : Emit warnings}
  847. \var{\{\$WARNINGS ON\}} switches the generation of warnings on.
  848. \var{\{\$WARNINGS OFF\}} switches the generation of warnings off.
  849. Contrary to the command-line option \var{-vw} this
  850. is a local switch, this is useful for checking parts of your code.
  851. By default, no warnings are emitted.
  852. \subsection{\var{\protect\$X} or \var{\protect\$EXTENDEDSYNTAX} : Extended syntax}
  853. Extended syntax allows you to drop the result of a function. This means that
  854. you can use a function call as if it were a procedure. Standard this feature
  855. is on. You can switch it off using the \var{\{\$X-\}} or
  856. \var{\{\$EXTENDEDSYNTAX OFF\}}directive.
  857. The following, for instance, will not compile:
  858. \begin{verbatim}
  859. function Func (var Arg : sometype) : longint;
  860. begin
  861. ... { declaration of Func }
  862. end;
  863. ...
  864. {$X-}
  865. Func (A);
  866. \end{verbatim}
  867. The reason this construct is supported is that you may wish to call a
  868. function for certain side-effects it has, but you don't need the function
  869. result. In this case you don't need to assign the function result, saving
  870. you an extra variable.
  871. The command-line compiler switch \var{-Sa1} has the same effect as the
  872. \var{\{\$X+\}} directive.
  873. By default, extended syntax is assumed.
  874. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  875. % Global switches
  876. \section{Global directives}
  877. \label{se:GlobalSwitch}
  878. Global directives affect the whole of the compilation process. That is why
  879. they also have a command-line counterpart. The command-line counterpart is
  880. given for each of the directives. They must be specified {\em before} the
  881. \var{unit} or \var{program} clause in a source file, or they will have no
  882. effect.
  883. \subsection{\var{\protect\$APPID} : Specify application ID.}
  884. Used on the PALM os only, it can be set to specify the application name,
  885. which can be viewed on the Palm only. This directive only makes sense in a
  886. program source file, not in a unit.
  887. \begin{verbatim}
  888. {$APPID MyApplication}
  889. \end{verbatim}
  890. \subsection{\var{\protect\$APPID} : Specify application name.}
  891. Used on the PALM os only, it can be set to specify the application name
  892. which can be viewed on the Palm only. This directive only makes sense in a
  893. program source file, not in a unit.
  894. \begin{verbatim}
  895. {$APPNAME My Application, compiled using Free Pascal.}
  896. \end{verbatim}
  897. \subsection{\var{\protect\$APPTYPE} : Specify type of application.}
  898. This directive is currently only supported on the following targets:
  899. Win32, Mac, OS2 and AmigaOS. On other targets, the directive is ignored.
  900. The \var{\{\$APPTYPE XXX\}} accepts one argument which specifies what kind
  901. of application is compiled. It can have the following
  902. values:
  903. \begin{description}
  904. \item[CONSOLE] A console application. A terminal will be created and
  905. standard input, output and standard error file descriptors will be
  906. initialized. In Windows, a terminal window will be created. This is the
  907. default.
  908. Note that on \macos such applications cannot take command-line
  909. options, nor return a result code. They will run in a special
  910. terminal window, implemented as a SIOW application, see the MPW
  911. documentation for details.
  912. On \ostwo, these applications can run both full-screen and in a terminal
  913. window.
  914. \linux applications are always console applications. The application itself
  915. can decide to close the standard files, though.
  916. \item[FS] specifies a full-screen VIO application on \ostwo. These
  917. applications use a special BIOS-like API to program the screen. \ostwo
  918. starts these application allways in full screen.
  919. \item[GUI] Specifying the \var{\{\$APPTYPE GUI\}} directive will mark the
  920. application as a graphical application; no console window will be opened
  921. when the application is run. No stanrdard file descriptors will be
  922. initialized, using them (with e.g. \var{writeln} statements) will produce a
  923. run-time error. If run from the command-line, the command prompt will be
  924. returned immediatly after the application was started.
  925. On \ostwo and \macos, the GUI application type creates a GUI application, as
  926. on Windows. On \ostwo, this is a real Presentation Manager application.
  927. \item[TOOL] this is a special directive for the \macos. It tells the compiler
  928. to create a tool application: It initializes input, output, stderr files,
  929. it can take parameters and return a result code. It is implemented as an MPW
  930. tool which can only be run by MPW or ToolServer.
  931. \end{description}
  932. Care should be taken when compiling \var{GUI} applications; the \var{Input}
  933. and \var{Output} files are not available in a GUI application, and
  934. attempting to read from or write to them will result in a run-time error.
  935. It is possible to determine the application type of a \windows or \amiga application
  936. at runtime. The \var{IsConsole} constant, declared in the Win32 and Amiga
  937. system units as
  938. \begin{verbatim}
  939. Const
  940. IsConsole : Boolean;
  941. \end{verbatim}
  942. contains \var{True} if the application is a console application, \var{False}
  943. if the application is a GUI application.
  944. \subsection{\var{\protect\$CALLING} : Default calling convention}
  945. This directive allows to specify the default calling convention used by the
  946. compiler, when no calling convention is specified for a procedure or
  947. function declaration. It can be one of the following values:
  948. \begin{description}
  949. \item[CDECL] C compiler calling convention.
  950. \item[CPPDECL] C++ compiler calling convention.
  951. \item[FAR16] Ignored, but parsed for compatibility reasons.
  952. \item[FPCCALL] Older FPC (1.0.X and before) standard calling convention. If
  953. a lot of direct assembler blocks are used, this mode should be used for
  954. maximum compatibility.
  955. \item[INLINE] Use inline code: the code for the function is inserted
  956. whenever it is called.
  957. \item[PASCAL] Pascal calling convention.
  958. \item[REGISTER] Register calling convention.
  959. \item[SAFECALL] Safecall calling convention (used in COM): The called
  960. procedure/function saves all registers.
  961. \item[STDCALL] Windows library calling convention.
  962. \item[SOFTFLOAT] For ARM processors.
  963. \end{description}
  964. It is equivalent to the \var{-Cc} command-line option.
  965. \subsection{\var{\protect\$COPYRIGHT} specify copyright info}
  966. This is intended for the NETWARE version of the compiler: it specifies the
  967. copyright information that can be viewed on a module for a Netware OS.
  968. For example:
  969. \begin{verbatim}
  970. {$COPYRIGHT GNU copyleft. compiled using Free Pascal}
  971. \end{verbatim}
  972. \subsection{\var{\protect\$D} or \var{\protect\$DEBUGINFO} : Debugging symbols}
  973. When this switch is on,
  974. the compiler inserts GNU debugging information in
  975. the executable. The effect of this switch is the same as the command-line
  976. switch \var{-g}.
  977. By default, insertion of debugging information is off.
  978. \subsection{\var{\protect\$DESCRIPTION} : Application description}
  979. This switch is recognised for compatibility only, but is ignored completely
  980. by the compiler. At a later stage, this switch may be activated.
  981. \subsection{\var{\protect\$E} : Emulation of coprocessor}
  982. This directive controls the emulation of the coprocessor. There is no
  983. command-line counterpart for this directive.
  984. \subsubsection{Intel 80x86 version}
  985. When this switch is enabled, all floating point instructions
  986. which are not supported by standard coprocessor emulators will give out
  987. a warning.
  988. The compiler itself doesn't do the emulation of the coprocessor.
  989. To use coprocessor emulation under \dos go32v2 you must use the
  990. emu387 unit, which contains correct initialization code for the
  991. emulator.
  992. Under \linux and most \unix'es, the kernel takes care of the
  993. coprocessor support.
  994. \subsubsection{Motorola 680x0 version}
  995. When the switch is on, no floating point opcodes are emitted
  996. by the code generator. Instead, internal run-time library routines
  997. are called to do the necessary calculations. In this case all
  998. real types are mapped to the single IEEE floating point type.
  999. \begin{remark}By default, emulation is on for non-unix targets.
  1000. For unix targets, floating point emulation (if required) is handled
  1001. by the operating system, and by default it is off.
  1002. \end{remark}
  1003. \subsection{\var{\protect\$G} : Generate 80286 code}
  1004. This option is recognised for Turbo Pascal compatibility, but is ignored,
  1005. since the compiler works only on 32-bit and 64-bit processors.
  1006. \subsection{\var{\protect\$INCLUDEPATH} : Specify include path.}
  1007. This option serves to specify the include path, where the compiler looks for
  1008. include files. \var{\{\$INCLUDEPATH XXX\}} will add \var{XXX} to the include
  1009. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  1010. colons.
  1011. For example:
  1012. \begin{verbatim}
  1013. {$INCLUDEPATH ../inc;../i386}
  1014. {$I strings.inc}
  1015. \end{verbatim}
  1016. will add the directories \file{../inc} and \file{../i386} to the include
  1017. path of the compiler. The compiler will look for the file \file{strings.inc}
  1018. in both these directories, and will include the first found file. This directive is
  1019. equivalent to the \var{-Fi} command-line switch.
  1020. Caution is in order when using this directive: If you distribute files, the
  1021. places of the files may not be the same as on your machine; moreover, the
  1022. directory structure may be different. In general it would be fair to say
  1023. that you should avoid using {\em absolute} paths, instead use {\em relative}
  1024. paths, as in the example above. Only
  1025. use this directive if you are certain
  1026. of the places where the files reside. If you are not sure, it is better
  1027. practice to use makefiles and makefile variables.
  1028. \subsection{\var{\protect\$L} or \var{\protect\$LOCALSYMBOLS} : Local symbol information}
  1029. This switch (not to be confused with the \var{\{\$L file\}} file linking
  1030. directive) is recognised for Turbo Pascal compatibility, but is ignored.
  1031. Generation of symbol information is controlled by the \var{\$D} switch.
  1032. \subsection{\var{\protect\$LIBRARYPATH} : Specify library path.}
  1033. This option serves to specify the library path, where the linker looks for
  1034. static or dynamic libraries. \var{\{\$LIBRARYPATH XXX\}} will add \var{XXX}
  1035. to the library path. \var{XXX} can contain one or more paths, separated
  1036. by semi-colons or colons.
  1037. For example:
  1038. \begin{verbatim}
  1039. {$LIBRARYPATH /usr/X11/lib;/usr/local/lib}
  1040. {$LINKLIB X11}
  1041. \end{verbatim}
  1042. will add the directories \file{/usr/X11/lib} and \file{/usr/local/lib} to
  1043. the linker library path. The linker will look for the library \file{libX11.so}
  1044. in both these directories, and use the first found file. This directive is
  1045. equivalent to the \var{-Fl} command-line switch.
  1046. Caution is in order when using this directive: If you distribute files, the
  1047. places of the libraries may not be the same as on your machine; moreover, the
  1048. directory structure may be different. In general it would be fair to say
  1049. that you should avoid using this directive. If you are not sure, it is better
  1050. practice to use makefiles and makefile variables.
  1051. \subsection{\var{\protect\$M} or \var{\protect\$MEMORY} : Memory sizes}
  1052. This switch can be used to set the heap and stacksize. It's format is as
  1053. follows:
  1054. \begin{verbatim}
  1055. {$M StackSize,HeapSize}
  1056. \end{verbatim}
  1057. where \var{StackSize} and \var{HeapSize} should be two integer values,
  1058. greater than 1024. The first number sets the size of the stack, and the
  1059. second the size of the heap. (Stack setting is ignored under \linux, \netbsd and \freebsd).
  1060. The two numbers can be set on the command line using the \var{-Ch}
  1061. and \var{-Cs} switches.
  1062. \subsection{\var{\protect\$MODE} : Set compiler compatibility mode}
  1063. The \var{\{\$MODE\}} sets the compatibility mode of the compiler. This
  1064. is equivalent to setting one of the command-line options \var{-So},
  1065. \var{-Sd}, \var{-Sp} or \var{-S2}. it has the following arguments:
  1066. \begin{description}
  1067. \item[Default] Default mode. This reverts back to the mode that was set on
  1068. the command-line.
  1069. \item[Delphi] Delphi compatibility mode. All object-pascal extensions are
  1070. enabled. This is the same as the command-line option \var{-Sd}.
  1071. \item[TP] Turbo pascal compatibility mode. Object pascal extensions are
  1072. disabled, except ansistrings, which remain valid.
  1073. This is the same as the command-line option \var{-So}.
  1074. \item[FPC] FPC mode. This is the default, if no command-line switch is
  1075. supplied.
  1076. \item[OBJFPC] Object pascal mode. This is the same as the \var{-S2}
  1077. command-line option.
  1078. \item[GPC] GNU pascal mode. This is the same as the \var{-Sp} command-line
  1079. option.
  1080. \item[MACPAS] MACPAS mode. In this mode, the compiler tries to be more compatible
  1081. to commonly used pascal dialects on the \macos, such as Think Pascal, Metrowerks
  1082. Pascal, MPW Pascal.
  1083. \end{description}
  1084. For an exact description of each of these modes, see appendix \ref{ch:AppD},
  1085. on page \pageref{ch:AppD}.
  1086. \subsection{\var{\protect\$N} : Numeric processing }
  1087. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  1088. ignored, since the compiler always uses the coprocessor for floating point
  1089. mathematics.
  1090. \subsection{\var{\protect\$O} : Overlay code generation }
  1091. This switch is recognised for Turbo Pascal compatibility, but is otherwise
  1092. ignored.
  1093. \subsection{\var{\protect\$OBJECTPATH} : Specify object path.}
  1094. This option serves to specify the object path, where the compiler looks for
  1095. object files. \var{\{\$OBJECTPATH XXX\}} will add \var{XXX} to the object
  1096. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  1097. colons.
  1098. For example:
  1099. \begin{verbatim}
  1100. {$OBJECTPATH ../inc;../i386}
  1101. {$L strings.o}
  1102. \end{verbatim}
  1103. will add the directories \file{../inc} and \file{../i386} to the
  1104. object path of the compiler. The compiler will look for the file \file{strings.o}
  1105. in both these directories, and will link the first found file in the
  1106. program. This directive is equivalent to the \var{-Fo} command-line switch.
  1107. Caution is in order when using this directive: If you distribute files, the
  1108. places of the files may not be the same as on your machine; moreover, the
  1109. directory structure may be different. In general it would be fair to say
  1110. that you should avoid using {\em absolute} paths, instead use {\em relative}
  1111. paths, as in the example above. Only use this directive if you are certain
  1112. of the places where the files reside. If you are not sure, it is better
  1113. practice to use makefiles and makefile variables.
  1114. \subsection{\var{\protect\$PROFILE} : Profiling}
  1115. This directive turns the generation of profiling code on (or off). It is
  1116. equivalent to the \var{-gp} command-line option. Default is \var{OFF}. This
  1117. directive only makes sense in a program source file, not in a unit.
  1118. \subsection{\var{\protect\$S} : Stack checking}
  1119. The \var{\{\$S+\}} directive tells the compiler to generate stack checking
  1120. code. This generates code to check if a stack overflow occurred, i.e. to see
  1121. whether the stack has grown beyond its maximally allowed size. If the stack
  1122. grows beyond the maximum size, then a run-time error is generated, and the
  1123. program will exit with exit code 202.
  1124. Specifying \var{\{\$S-\}} will turn generation of stack-checking code off.
  1125. The command-line compiler switch \var{-Ct} has the same effect as the
  1126. \var{\{\$S+\}} directive.
  1127. By default, no stack checking is performed.
  1128. \subsection{\var{\protect\$SMARTLINK} : Use smartlinking}
  1129. A unit that is compiled in the \var{\{\$SMARTLINK ON\}} state will be
  1130. compiled in such a way that it can be used for smartlinking. This means that
  1131. the unit is chopped in logical pieces: each procedure is put in it's own
  1132. object file, and all object files are put together in a big archive. When
  1133. using such a unit, only the pieces of code that you really need or call,
  1134. will be linked in your program, thus reducing the size of your executable
  1135. substantially.
  1136. Beware: using smartlinked units slows down the compilation process, because
  1137. a separate object file must be created for each procedure. If you have units
  1138. with many functions and procedures, this can be a time consuming process,
  1139. even more so if you use an external assembler (the assembler is called to
  1140. assemble each procedure or function code block separately).
  1141. The smartlinking directive should be specified {\em before} the unit
  1142. declaration part:
  1143. \begin{verbatim}
  1144. {$SMARTLINK ON}
  1145. Unit MyUnit;
  1146. Interface
  1147. ...
  1148. \end{verbatim}
  1149. This directive is equivalent to the \var{-Cx} command-line switch.
  1150. \subsection{\var{\protect\$THREADNAME} : Set thread name in Netware}
  1151. This directive can be set to specify the thread name when compiling for
  1152. Netware.
  1153. \subsection{\var{\protect\$THREADING} : Allow use of threads.}
  1154. If a program needs threading, this directive must be specified; The compiler
  1155. will then insert the \var{systhrd} unit in the uses list of the program and
  1156. will enable some threading features. This directive defines or undefines the
  1157. \var{FPC\_THREADING} macro symbol.
  1158. Specifying \var{\{\$THREADING OFF\}} will disable threading. The
  1159. \var{FPC\_THREADING} will be undefined.
  1160. \subsection{\var{\protect\$UNITPATH} : Specify unit path.}
  1161. This option serves to specify the unit path, where the compiler looks for
  1162. unit files. \var{\{\$UNITPATH XXX\}} will add \var{XXX} to the unit
  1163. path. \var{XXX} can contain one or more paths, separated by semi-colons or
  1164. colons.
  1165. For example:
  1166. \begin{verbatim}
  1167. {$UNITPATH ../units;../i386/units}
  1168. Uses strings;
  1169. \end{verbatim}
  1170. will add the directories \file{../units} and \file{../i386/units} to the unit
  1171. path of the compiler. The compiler will look for the file \file{strings.ppu}
  1172. in both these directories, and will link the first found file in the
  1173. program. This directive is equivalent to the \var{-Fu} command-line switch.
  1174. Caution is in order when using this directive: If you distribute files, the
  1175. places of the files may not be the same as on your machine; moreover, the
  1176. directory structure may be different. In general it would be fair to say
  1177. that you should avoid using {\em absolute} paths, instead use {\em relative}
  1178. paths, as in the example above. Only use this directive if you are certain
  1179. of the places where the files reside. If you are not sure, it is better
  1180. practice to use makefiles and makefile variables.
  1181. \subsection{\var{\protect\$VERSION} : Specify DLL version.}
  1182. On \windows, this can be used to specify a version number for a library.
  1183. This version number will be used when the library is installed, and can be
  1184. viewed in the Windows Explorer by opening the property sheet of the DLL and
  1185. looking on the tab 'Version'. The version number consists of minimally one,
  1186. maximum 3 numbers:
  1187. \begin{verbatim}
  1188. {$VERSION 1}
  1189. \end{verbatim}
  1190. Or:
  1191. \begin{verbatim}
  1192. {$VERSION 1.1}
  1193. \end{verbatim}
  1194. And even:
  1195. \begin{verbatim}
  1196. {$VERSION 1.1.1}
  1197. \end{verbatim}
  1198. This can not yet be used for executables on Windows, but may be activated in
  1199. the future.
  1200. \subsection{\var{\protect\$W} or \var{\protect\$STACKFRAMES} : Generate stackframes}
  1201. The \var{\{\$W\}} switch directive controls the generation of stackframes.
  1202. In the on state, the compiler will generate a
  1203. stackframe for every procedure or function.
  1204. In the off state, the compiler will omit the generation of a stackframe if
  1205. the following conditions are satisfied:
  1206. \begin{itemize}
  1207. \item The procedure has no parameters.
  1208. \item The procedure has no local variables.
  1209. \item If the procedure is not an \var{assembler} procedure, it must not have
  1210. a \var{asm \dots end;} block.
  1211. \item it is not a constructor or destructor.
  1212. \end{itemize}
  1213. If these conditions are satisfied, the stack frame will be omitted.
  1214. \subsection{\var{\protect\$Y} or \var{\protect\$REFERENCEINFO} : Insert Browser information}
  1215. This switch controls the generation of browser inforation. It is recognized
  1216. for compatibility with Turbo Pascal and Delphi only, as Browser information
  1217. generation is not yet fully supported.
  1218. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1219. % Using conditionals and macros
  1220. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1221. \chapter{Using conditionals, messages and macros}
  1222. \label{ch:CondMessageMacro}
  1223. The \fpc compiler supports conditionals as in normal Turbo Pascal. It does,
  1224. however, more than that. It allows you to make macros which can be used in
  1225. your code, and it allows you to define messages or errors which will be
  1226. displayed when compiling.
  1227. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1228. % Conditionals
  1229. \section{Conditionals}
  1230. \label{se:Conditionals}
  1231. The rules for using conditional symbols are the same as under Turbo Pascal.
  1232. Defining a symbol goes as follows:
  1233. \begin{verbatim}
  1234. {$define Symbol}
  1235. \end{verbatim}
  1236. From this point on in your code, the compiler knows the symbol \var{Symbol}.
  1237. Symbols are, like the Pascal language, case insensitive.
  1238. You can also define a symbol on the command line. the \var{-dSymbol} option
  1239. defines the symbol \var{Symbol}. You can specify as many symbols on the
  1240. command line as you want.
  1241. Undefining an existing symbol is done in a similar way:
  1242. \begin{verbatim}
  1243. {$undef Symbol}
  1244. \end{verbatim}
  1245. If the symbol didn't exist yet, this doesn't do anything. If the symbol
  1246. existed previously, the symbol will be erased, and will not be recognized
  1247. any more in the code following the \verb|{$undef \dots}| statement.
  1248. You can also undefine symbols from the command line with the \var{-u}
  1249. command-line switch.
  1250. To compile code conditionally, depending on whether a symbol is defined or
  1251. not, you can enclose the code in a \verb|{$ifdef Symbol}| \dots \verb|{$endif}|
  1252. pair. For instance the following code will never be compiled:
  1253. \begin{verbatim}
  1254. {$undef MySymbol}
  1255. {$ifdef Mysymbol}
  1256. DoSomething;
  1257. ...
  1258. {$endif}
  1259. \end{verbatim}
  1260. Similarly, you can enclose your code in a \verb|{$ifndef Symbol}| \dots \verb|{$endif}|
  1261. pair. Then the code between the pair will only be compiled when the used
  1262. symbol doesn't exist. For example, in the following example, the call to the
  1263. \var{DoSomething} will always be compiled:
  1264. \begin{verbatim}
  1265. {$undef MySymbol}
  1266. {$ifndef Mysymbol}
  1267. DoSomething;
  1268. ...
  1269. {$endif}
  1270. \end{verbatim}
  1271. You can combine the two alternatives in one structure, namely as follows
  1272. \begin{verbatim}
  1273. {$ifdef Mysymbol}
  1274. DoSomething;
  1275. {$else}
  1276. DoSomethingElse
  1277. {$endif}
  1278. \end{verbatim}
  1279. In this example, if \var{MySymbol} exists, then the call to \var{DoSomething}
  1280. will be compiled. If it doesn't exist, the call to \var{DoSomethingElse} is
  1281. compiled.
  1282. Except for the Turbo Pascal constructs the \fpc compiler also
  1283. supports a stronger conditional compile mechanism: The \var{\{\$if\}}
  1284. construct.
  1285. The prototype of this construct is as follows:
  1286. \begin{verbatim}
  1287. {$if expr}
  1288. CompileTheseLines;
  1289. {$else}
  1290. BetterCompileTheseLines;
  1291. {$endif}
  1292. \end{verbatim}
  1293. In this directive \var{expr} is a Pascal expression which is evaluated using
  1294. strings, unless both parts of a comparision can be evaluated as numbers,
  1295. in which case they are evaluated using numbers\footnote{Otherwise
  1296. \var{\{\$if 8>54\}} would evaluate to \var{True}}.
  1297. If the complete expression evaluates to \var{'0'}, then it is considered
  1298. false and rejected. Otherwise it is considered true and accepted. This may
  1299. have unexpected consequences:
  1300. \begin{verbatim}
  1301. {$if 0}
  1302. \end{verbatim}
  1303. will evaluate to \var{False} and be rejected, while
  1304. \begin{verbatim}
  1305. {$if 00}
  1306. \end{verbatim}
  1307. will evaluate to \var{True}.
  1308. You can use any Pascal operator to construct your expression: \var{=, <>,
  1309. >, <, >=, <=, AND, NOT, OR} and you can use round brackets to change the
  1310. precedence of the operators. Additionally, the constants \var{FALSE} and
  1311. \var{TRUE} can be used, and the operator \var{UNDEFINED}. The \var{UNDEFINED}
  1312. operator returns \var{TRUE} if a macro was not yet defined.
  1313. The following example shows you many of the possibilities:
  1314. \begin{verbatim}
  1315. {$ifdef fpc}
  1316. var
  1317. y : longint;
  1318. {$else fpc}
  1319. var
  1320. z : longint;
  1321. {$endif fpc}
  1322. var
  1323. x : longint;
  1324. begin
  1325. {$if (fpc_version=0) and (fpc_release>6) and (fpc_patch>4)}
  1326. {$info At least this is version 0.9.5}
  1327. {$else}
  1328. {$fatal Problem with version check}
  1329. {$endif}
  1330. {$define x:=1234}
  1331. {$if x=1234}
  1332. {$info x=1234}
  1333. {$else}
  1334. {$fatal x should be 1234}
  1335. {$endif}
  1336. {$if 12asdf and 12asdf}
  1337. {$info $if 12asdf and 12asdf is ok}
  1338. {$else}
  1339. {$fatal $if 12asdf and 12asdf rejected}
  1340. {$endif}
  1341. {$if 0 or 1}
  1342. {$info $if 0 or 1 is ok}
  1343. {$else}
  1344. {$fatal $if 0 or 1 rejected}
  1345. {$endif}
  1346. {$if 0}
  1347. {$fatal $if 0 accepted}
  1348. {$else}
  1349. {$info $if 0 is ok}
  1350. {$endif}
  1351. {$if 12=12}
  1352. {$info $if 12=12 is ok}
  1353. {$else}
  1354. {$fatal $if 12=12 rejected}
  1355. {$endif}
  1356. {$if 12<>312}
  1357. {$info $if 12<>312 is ok}
  1358. {$else}
  1359. {$fatal $if 12<>312 rejected}
  1360. {$endif}
  1361. {$if 12<=312}
  1362. {$info $if 12<=312 is ok}
  1363. {$else}
  1364. {$fatal $if 12<=312 rejected}
  1365. {$endif}
  1366. {$if 12<312}
  1367. {$info $if 12<312 is ok}
  1368. {$else}
  1369. {$fatal $if 12<312 rejected}
  1370. {$endif}
  1371. {$if a12=a12}
  1372. {$info $if a12=a12 is ok}
  1373. {$else}
  1374. {$fatal $if a12=a12 rejected}
  1375. {$endif}
  1376. {$if a12<=z312}
  1377. {$info $if a12<=z312 is ok}
  1378. {$else}
  1379. {$fatal $if a12<=z312 rejected}
  1380. {$endif}
  1381. {$if a12<z312}
  1382. {$info $if a12<z312 is ok}
  1383. {$else}
  1384. {$fatal $if a12<z312 rejected}
  1385. {$endif}
  1386. {$if not(0)}
  1387. {$info $if not(0) is OK}
  1388. {$else}
  1389. {$fatal $if not(0) rejected}
  1390. {$endif}
  1391. {$IF NOT UNDEFINED FPC}
  1392. // Detect FPC stuff when compiling on MAC.
  1393. {$SETC TARGET_RT_MAC_68881:= FALSE}
  1394. {$SETC TARGET_OS_MAC := (NOT UNDEFINED MACOS)
  1395. OR (NOT UNDEFINED DARWIN)}
  1396. {$SETC TARGET_OS_WIN32 := NOT UNDEFINED WIN32}
  1397. {$SETC TARGET_OS_UNIX := (NOT UNDEFINED UNIX)
  1398. AND (UNDEFINED DARWIN)}
  1399. {$SETC TYPE_EXTENDED := TRUE}
  1400. {$SETC TYPE_LONGLONG := FALSE}
  1401. {$SETC TYPE_BOOL := FALSE}
  1402. {$ENDIF}
  1403. {$info *************************************************}
  1404. {$info * Now have to follow at least 2 error messages: *}
  1405. {$info *************************************************}
  1406. {$if not(0}
  1407. {$endif}
  1408. {$if not(<}
  1409. {$endif}
  1410. end.
  1411. \end{verbatim}
  1412. As you can see from the example, this construct isn't useful when used
  1413. with normal symbols, only if you use macros, which are explained in
  1414. \sees{Macros}. They can be very useful. When trying this example, you must
  1415. switch on macro support, with the \var{-Sm} command-line switch.
  1416. \subsection{Predefined symbols}
  1417. The \fpc compiler defines some symbols before starting to compile your
  1418. program or unit. You can use these symbols to differentiate between
  1419. different versions of the compiler, and between different compilers.
  1420. To get all the possible defines when starting compilation,
  1421. see appendix \ref{ch:AppG}
  1422. \begin{remark}Symbols, even when they're defined in the interface part of
  1423. a unit, are not available outside that unit.
  1424. \end{remark}
  1425. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1426. % Macros
  1427. \section{Messages}
  1428. \label{se:Messages}
  1429. \fpc lets you define normal, warning and error messages in your code.
  1430. Messages can be used to display useful information, such as copyright
  1431. notices, a list of symbols that your code reacts on etc.
  1432. Warnings can be used if you think some part of your code is still buggy, or
  1433. if you think that a certain combination of symbols isn't useful.
  1434. Error messages can be useful if you need a certain symbol to be defined,
  1435. to warn that a certain variable isn't defined, or when the compiler
  1436. version isn't suitable for your code.
  1437. The compiler treats these messages as if they were generated by the
  1438. compiler. This means that if you haven't turned on warning messages, the
  1439. warning will not be displayed. Errors are always displayed, and the
  1440. compiler stops if 50 errors have occurred. After a fatal error, the compiler
  1441. stops at once.
  1442. For messages, the syntax is as follows:
  1443. \begin{verbatim}
  1444. {$Message Message text}
  1445. \end{verbatim}
  1446. or
  1447. \begin{verbatim}
  1448. {$Info Message text}
  1449. \end{verbatim}
  1450. For notes:
  1451. \begin{verbatim}
  1452. {$Note Message text}
  1453. \end{verbatim}
  1454. For warnings:
  1455. \begin{verbatim}
  1456. {$Warning Warning Message text}
  1457. \end{verbatim}
  1458. For errors:
  1459. \begin{verbatim}
  1460. {$Error Error Message text}
  1461. \end{verbatim}
  1462. Lastly, for fatal errors:
  1463. \begin{verbatim}
  1464. {$Fatal Error Message text}
  1465. \end{verbatim}
  1466. or
  1467. \begin{verbatim}
  1468. {$Stop Error Message text}
  1469. \end{verbatim}
  1470. The difference between \var{\$Error} and \var{\$FatalError} or \var{\$Stop}
  1471. messages is that when the compiler encounters an error, it still continues
  1472. to compile. With a fatal error, the compiler stops.
  1473. \begin{remark}You cannot use the '\var{\}}' character in your message, since
  1474. this will be treated as the closing brace of the message.
  1475. \end{remark}
  1476. As an example, the following piece of code will generate an error when
  1477. the symbol \var{RequiredVar} isn't defined:
  1478. \begin{verbatim}
  1479. {$ifndef RequiredVar}
  1480. {$Error Requiredvar isn't defined !}
  1481. {$endif}
  1482. \end{verbatim}
  1483. But the compiler will continue to compile. It will not, however, generate a
  1484. unit file or a program (since an error occurred).
  1485. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1486. % Macros
  1487. \section{Macros}
  1488. \label{se:Macros}
  1489. Macros are very much like symbols in their syntax, the difference is that
  1490. macros have a value whereas a symbol simply is defined or is not defined.
  1491. If you want macro support, you need to specify the \var{-Sm} command-line
  1492. switch, otherwise your macro will be regarded as a symbol.
  1493. Defining a macro in your program is done in the same way as defining a symbol;
  1494. in a \var{\{\$define\}} preprocessor statement\footnote{In compiler
  1495. versions older than 0.9.8, the assignment operator for a macros wasn't
  1496. \var{:=} but \var{=}}:
  1497. \begin{verbatim}
  1498. {$define ident:=expr}
  1499. \end{verbatim}
  1500. If the compiler encounters \var{ident} in the rest of the source file, it
  1501. will be replaced immediately by \var{expr}. This replacement works
  1502. recursive, meaning that when the compiler expanded one of your macros, it
  1503. will look at the resulting expression again to see if another replacement
  1504. can be made. You need to be careful with this, because an infinite loop can
  1505. occur in this manner.
  1506. Here are two examples which illustrate the use of macros:
  1507. \begin{verbatim}
  1508. {$define sum:=a:=a+b;}
  1509. ...
  1510. sum { will be expanded to 'a:=a+b;'
  1511. remark the absence of the semicolon}
  1512. ...
  1513. {$define b:=100}
  1514. sum { Will be expanded recursively to a:=a+100; }
  1515. ...
  1516. \end{verbatim}
  1517. The previous example could go wrong:
  1518. \begin{verbatim}
  1519. {$define sum:=a:=a+b;}
  1520. ...
  1521. sum { will be expanded to 'a:=a+b;'
  1522. remark the absence of the semicolon}
  1523. ...
  1524. {$define b=sum} { DON'T do this !!!}
  1525. sum { Will be infinitely recursively expanded \dots }
  1526. ...
  1527. \end{verbatim}
  1528. On my system, the last example results in a heap error, causing the compiler
  1529. to exit with a run-time error 203.
  1530. \begin{remark}Macros defined in the interface part of a unit are not
  1531. available outside that unit! They can just be used as a notational
  1532. convenience, or in conditional compiles.
  1533. \end{remark}
  1534. By default the compiler predefines three
  1535. macros, containing the version number, the release number and the patch
  1536. number. They are listed in \seet{DefMacros}.
  1537. \begin{FPCltable}{ll}{Predefined macros}{DefMacros} \hline
  1538. Symbol & Contains \\ \hline
  1539. \var{FPC\_VERSION} & The version number of the compiler. \\
  1540. \var{FPC\_RELEASE} & The release number of the compiler. \\
  1541. \var{FPC\_PATCH} & The patch number of the compiler. \\
  1542. \hline
  1543. \end{FPCltable}
  1544. \begin{remark}Don't forget that macros support isn't on by default. You
  1545. need to compile with the \var{-Sm} command-line switch.
  1546. \end{remark}
  1547. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1548. % Using assembly language
  1549. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1550. \chapter{Using Assembly language}
  1551. \label{ch:AsmLang}
  1552. \fpc supports inserting assembler statements in between Pascal code. The
  1553. mechanism for this is the same as under Turbo Pascal. There are, however
  1554. some substantial differences, as will be explained in the following
  1555. sections.
  1556. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1557. % 80x86 Assembler syntax
  1558. \section{Intel 80x86 Inline assembler}
  1559. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1560. % Intel syntax
  1561. \subsection{Intel syntax}
  1562. \label{se:Intel}
  1563. \fpc supports Intel syntax for the Intel family of Ix86 processors
  1564. in its \var{asm} blocks.
  1565. The Intel syntax in your \var{asm} block is converted to AT\&T syntax by the
  1566. compiler, after which it is inserted in the compiled source.
  1567. The supported assembler constructs are a subset of the normal assembly
  1568. syntax. In what follows we specify what constructs are not supported in
  1569. \fpc, but which exist in Turbo Pascal:
  1570. \begin{itemize}
  1571. \item The \var{TBYTE} qualifier is not supported.
  1572. \item The \var{\&} identifier override is not supported.
  1573. \item The \var{HIGH} operator is not supported.
  1574. \item The \var{LOW} operator is not supported.
  1575. \item The \var{OFFSET} and \var{SEG} operators are not supported.
  1576. Use \var{LEA} and the various \var{Lxx} instructions instead.
  1577. \item Expressions with constant strings are not allowed.
  1578. \item Access to record fields via parenthesis is not allowed
  1579. \item Typecasts with normal pascal types are not allowed, only
  1580. recognized assembler typecasts are allowed. Example:
  1581. \begin{verbatim}
  1582. mov al, byte ptr MyWord -- allowed,
  1583. mov al, byte(MyWord) -- allowed,
  1584. mov al, shortint(MyWord) -- not allowed.
  1585. \end{verbatim}
  1586. \item Pascal type typecasts on constants are not allowed.
  1587. Example:
  1588. \begin{verbatim}
  1589. const s= 10; const t = 32767;
  1590. \end{verbatim}
  1591. in Turbo Pascal:
  1592. \begin{verbatim}
  1593. mov al, byte(s) -- useless typecast.
  1594. mov al, byte(t) -- syntax error!
  1595. \end{verbatim}
  1596. In this parser, either of those cases will give out a syntax error.
  1597. \item Constant references expressions with constants only are not
  1598. allowed (in all cases they do not work in protected mode,
  1599. under \linux i386). Examples:
  1600. \begin{verbatim}
  1601. mov al,byte ptr ['c'] -- not allowed.
  1602. mov al,byte ptr [100h] -- not allowed.
  1603. \end{verbatim}
  1604. (This is due to the limitation of Turbo Assembler).
  1605. \item Brackets within brackets are not allowed
  1606. \item Expressions with segment overrides fully in brackets are
  1607. presently not supported, but they can easily be implemented
  1608. in BuildReference if requested. Example:
  1609. \begin{verbatim}
  1610. mov al,[ds:bx] -- not allowed
  1611. \end{verbatim}
  1612. use instead:
  1613. \begin{verbatim}
  1614. mov al,ds:[bx]
  1615. \end{verbatim}
  1616. \item Possible allowed indexing are as follows:
  1617. \begin{itemize}
  1618. \item \var{Sreg:[REG+REG*SCALING+/-disp]}
  1619. \item \var{SReg:[REG+/-disp]}
  1620. \item \var{SReg:[REG]}
  1621. \item \var{SReg:[REG+REG+/-disp]}
  1622. \item \var{SReg:[REG+REG*SCALING]}
  1623. \end{itemize}
  1624. Where \var{Sreg} is optional and specifies the segment override.
  1625. {\em Notes:}
  1626. \begin{enumerate}
  1627. \item The order of terms is important contrary to Turbo Pascal.
  1628. \item The Scaling value must be a value, and not an identifier
  1629. to a symbol. Examples:
  1630. \begin{verbatim}
  1631. const myscale = 1;
  1632. ...
  1633. mov al,byte ptr [esi+ebx*myscale] -- not allowed.
  1634. \end{verbatim}
  1635. use:
  1636. \begin{verbatim}
  1637. mov al, byte ptr [esi+ebx*1]
  1638. \end{verbatim}
  1639. \end{enumerate}
  1640. \item Possible variable identifier syntax is as follows:
  1641. (Id = Variable or typed constant identifier.)
  1642. \begin{enumerate}
  1643. \item \var{ID}
  1644. \item \var{[ID]}
  1645. \item \var{[ID+expr]}
  1646. \item \var{ID[expr]}
  1647. \end{enumerate}
  1648. Possible fields are as follow:
  1649. \begin{enumerate}
  1650. \item \var{ID.subfield.subfield \dots}
  1651. \item \var{[ref].ID.subfield.subfield \dots}
  1652. \item \var{[ref].typename.subfield \dots}
  1653. \end{enumerate}
  1654. \item Local abels: Contrary to Turbo Pascal, local labels, must
  1655. at least contain one character after the local symbol indicator.
  1656. Example:
  1657. \begin{verbatim}
  1658. @: -- not allowed
  1659. \end{verbatim}
  1660. use instead, for example:
  1661. \begin{verbatim}
  1662. @1: -- allowed
  1663. \end{verbatim}
  1664. \item Contrary to Turbo Pascal local references cannot be used as references,
  1665. only as displacements. Example:
  1666. \begin{verbatim}
  1667. lds si,@mylabel -- not allowed
  1668. \end{verbatim}
  1669. \item Contrary to Turbo Pascal, \var{SEGCS}, \var{SEGDS}, \var{SEGES} and
  1670. \var{SEGSS} segment overrides are presently not supported.
  1671. (This is a planned addition though).
  1672. \item Contrary to Turbo Pascal where memory sizes specifiers can
  1673. be practically anywhere, the \fpc Intel inline assembler requires
  1674. memory size specifiers to be outside the brackets. Example:
  1675. \begin{verbatim}
  1676. mov al,[byte ptr myvar] -- not allowed.
  1677. \end{verbatim}
  1678. use:
  1679. \begin{verbatim}
  1680. mov al,byte ptr [myvar] -- allowed.
  1681. \end{verbatim}
  1682. \item Base and Index registers must be 32-bit registers.
  1683. (limitation of the GNU Assembler).
  1684. \item \var{XLAT} is equivalent to \var{XLATB}.
  1685. \item Only Single and Double FPU opcodes are supported.
  1686. \item Floating point opcodes are currently not supported
  1687. (except those which involve only floating point registers).
  1688. \end{itemize}
  1689. The Intel inline assembler supports the following macros:
  1690. \begin{description}
  1691. \item [@Result] represents the function result return value.
  1692. \item [Self] represents the object method pointer in methods.
  1693. \end{description}
  1694. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1695. % AT&T syntax
  1696. \subsection{AT\&T Syntax}
  1697. \label{se:AttSyntax}
  1698. \fpc uses the \gnu \var{as} assembler to generate its object files for
  1699. the Intel Ix86 processors. Since
  1700. the \gnu assembler uses AT\&T assembly syntax, the code you write should
  1701. use the same syntax. The differences between AT\&T and Intel syntax as used
  1702. in Turbo Pascal are summarized in the following:
  1703. \begin{itemize}
  1704. \item The opcode names include the size of the operand. In general, one can
  1705. say that the AT\&T opcode name is the Intel opcode name, suffixed with a
  1706. '\var{l}', '\var{w}' or '\var{b}' for, respectively, longint (32 bit),
  1707. word (16 bit) and byte (8 bit) memory or register references. As an example,
  1708. the Intel construct \mbox{'\var{mov al bl}} is equivalent to the AT\&T style '\var{movb
  1709. \%bl,\%al}' instruction.
  1710. \item AT\&T immediate operands are designated with '\$', while Intel syntax
  1711. doesn't use a prefix for immediate operands. Thus the Intel construct
  1712. '\var{mov ax, 2}' becomes '\var{movb \$2, \%al}' in AT\&T syntax.
  1713. \item AT\&T register names are preceded by a '\var{\%}' sign.
  1714. They are undelimited in Intel syntax.
  1715. \item AT\&T indicates absolute jump/call operands with '\var{*}', Intel
  1716. syntax doesn't delimit these addresses.
  1717. \item The order of the source and destination operands are switched. AT\&T
  1718. syntax uses '\var{Source, Dest}', while Intel syntax features '\var{Dest,
  1719. Source}'. Thus the Intel construct '\var{add eax, 4}' transforms to
  1720. '\var{addl \$4, \%eax}' in the AT\&T dialect.
  1721. \item Immediate long jumps are prefixed with the '\var{l}' prefix. Thus the
  1722. Intel '\var{call/jmp section:offset'} is transformed to '\var{lcall/ljmp
  1723. \$section,\$offset}'. Similarly the far return is '\var{lret}', instead of the
  1724. Intel '\var{ret far}'.
  1725. \item Memory references are specified differently in AT\&T and Intel
  1726. assembly. The Intel indirect memory reference
  1727. \begin{quote}
  1728. \var{Section:[Base + Index*Scale + Offs]}
  1729. \end{quote}
  1730. is written in AT\&T syntax as:
  1731. \begin{quote}
  1732. \var{Section:Offs(Base,Index,Scale)}
  1733. \end{quote}
  1734. Where \var{Base} and \var{Index} are optional 32-bit base and index
  1735. registers, and \var{Scale} is used to multiply \var{Index}. It can take the
  1736. values 1,2,4 and 8. The \var{Section} is used to specify an optional section
  1737. register for the memory operand.
  1738. \end{itemize}
  1739. More information about the AT\&T syntax can be found in the \var{as} manual,
  1740. although the following differences with normal AT\&T assembly must be taken
  1741. into account:
  1742. \begin{itemize}
  1743. \item Only the following directives are presently supported:
  1744. \begin{description}
  1745. \item[.byte]
  1746. \item[.word]
  1747. \item[.long]
  1748. \item[.ascii]
  1749. \item[.asciz]
  1750. \item[.globl]
  1751. \end{description}
  1752. \item The following directives are recognized but are not
  1753. supported:
  1754. \begin{description}
  1755. \item[.align]
  1756. \item[.lcomm]
  1757. \end{description}
  1758. Eventually they will be supported.
  1759. \item Directives are case sensitive, other identifiers are not case sensitive.
  1760. \item Contrary to GAS local labels/symbols {\em must} start with \var{.L}
  1761. \item The not operator \var{'!'} is not supported.
  1762. \item String expressions in operands are not supported.
  1763. \item CBTW,CWTL,CWTD and CLTD are not supported, use the normal intel
  1764. equivalents instead.
  1765. \item Constant expressions which represent memory references are not
  1766. allowed even though constant immediate value expressions are supported.
  1767. Examples:
  1768. \begin{verbatim}
  1769. const myid = 10;
  1770. ...
  1771. movl $myid,%eax -- allowed
  1772. movl myid(%esi),%eax -- not allowed.
  1773. \end{verbatim}
  1774. \item When the \var{.globl} directive is found, the symbol following
  1775. it is made public and is immediately emitted.
  1776. Therefore label names with this name will be ignored.
  1777. \item Only Single and Double FPU opcodes are supported.
  1778. \end{itemize}
  1779. The AT\&T inline assembler supports the following macros:
  1780. \begin{description}
  1781. \item [\_\_RESULT] represents the function result return value.
  1782. \item [\_\_SELF] represents the object method pointer in methods.
  1783. \item [\_\_OLDEBP] represents the old base pointer in recusrive routines.
  1784. \end{description}
  1785. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1786. % Motorola 680x0 assembler
  1787. \section{Motorola 680x0 Inline assembler}
  1788. The inline assembler reader for the Motorola 680x0 family of processors,
  1789. uses the Motorola Assembler syntax (q.v). A few differences do exit:
  1790. \begin{itemize}
  1791. \item Local labels start with the @ character, such as
  1792. \begin{verbatim}
  1793. @MyLabel:
  1794. \end{verbatim}
  1795. \item The \var{XDEF} directive in an assembler block will
  1796. make the symbol available publicly with the specified name
  1797. (this name is case sensitive)
  1798. \item The \var{DB}, \var{DW}, \var{DD} directives can only
  1799. be used to declare constants which will be stored in the
  1800. code segment.
  1801. \item The \var{Align} directive is not supported.
  1802. \item Arithmetic operations on constant expression use the same
  1803. operands as the intel version (e.g : \var{AND}, \var{XOR} ...)
  1804. \item Segment directives are not supported
  1805. \item Only 68000 and a subset of 68020 opcodes are currently supported
  1806. \end{itemize}
  1807. The inline assembler supports the following macros:
  1808. \begin{description}
  1809. \item [@Result] represents the function result return value.
  1810. \item [Self] represents the object method pointer in methods.
  1811. \end{description}
  1812. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1813. % Telling the compiler what registers have changed
  1814. \section{Signaling changed registers}
  1815. \label{se:RegChanges}
  1816. When the compiler uses variables, it sometimes stores them, or the result of
  1817. some calculations, in the processor registers. If you insert assembler code
  1818. in your program that modifies the processor registers, then this may
  1819. interfere with the compiler's idea about the registers. To avoid this
  1820. problem, \fpc allows you to tell the compiler which registers have changed.
  1821. The compiler will then avoid using these registers. Telling the compiler
  1822. which registers have changed is done by specifying a set of register names
  1823. behind an assembly block, as follows:
  1824. \begin{verbatim}
  1825. asm
  1826. ...
  1827. end ['R1', ... ,'Rn'];
  1828. \end{verbatim}
  1829. Here \var{R1} to \var{Rn} are the names of the registers you
  1830. modify in your assembly code.
  1831. As an example:
  1832. \begin{verbatim}
  1833. asm
  1834. movl BP,%eax
  1835. movl 4(%eax),%eax
  1836. movl %eax,__RESULT
  1837. end ['EAX'];
  1838. \end{verbatim}
  1839. This example tells the compiler that the \var{EAX} register was modified.
  1840. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1841. % Generated code
  1842. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1843. \chapter{Generated code}
  1844. \label{ch:GenCode}
  1845. The \fpc compiler relies on the assembler to make object files. It generates
  1846. just the assembly language file. In the following two sections, we discuss
  1847. what is generated when you compile a unit or a program.
  1848. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1849. % Units
  1850. \section{Units}
  1851. \label{se:Units}
  1852. When you compile a unit, the \fpc compiler generates 2 files:
  1853. \begin{enumerate}
  1854. \item A unit description file.
  1855. \item An assembly language file.
  1856. \end{enumerate}
  1857. The assembly language file contains the actual source code for the
  1858. statements in your unit, and the necessary memory allocations for any
  1859. variables you use in your unit. This file is converted by the assembler to
  1860. an object file (with extension \file{.o}) which can then be linked to other
  1861. units and your program, to form an executable.
  1862. By default, the assembly file is removed after it has been compiled.
  1863. Only in the case of the \var{-s} command-line option, the assembly file
  1864. will be left on disk, so the assembler can be called later. You can
  1865. disable the erasing of the assembler file with the \var{-a} switch.
  1866. The unit file contains all the information the compiler needs to use the
  1867. unit:
  1868. \begin{enumerate}
  1869. \item Other used units, both in interface and implementation.
  1870. \item Types and variables from the interface section of the unit.
  1871. \item Function declarations from the interface section of the unit.
  1872. \item Some debugging information, when compiled with debugging.
  1873. \end{enumerate}
  1874. The detailed contents and structure of this file are described in the first
  1875. appendix. You can examine a unit description file using the \file{ppudump}
  1876. program, which shows the contents of the file.
  1877. If you want to distribute a unit without source code, you must provide both
  1878. the unit description file and the object file.
  1879. You can also provide a C header file to go with the object file. In that
  1880. case, your unit can be used by someone who wishes to write his programs in
  1881. C. However, you must make this header file yourself since the \fpc compiler
  1882. doesn't make one for you.
  1883. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1884. % Programs
  1885. \section{Programs}
  1886. \label{se:Programs}
  1887. When you compile a program, the compiler produces again 2 files:
  1888. \begin{enumerate}
  1889. \item An assembly language file containing the statements of your program,
  1890. and memory allocations for all used variables.
  1891. \item A linker response file. This file contains a list of object files the
  1892. linker must link together.
  1893. \end{enumerate}
  1894. The link response file is, by default, removed from the disk. Only when you
  1895. specify the \var{-s} command-line option or when linking fails, then the file
  1896. is left on the disk. It is named \file{link.res}.
  1897. The assembly language file is converted to an object file by the assembler,
  1898. and then linked together with the rest of the units and a program header, to
  1899. form your final program.
  1900. The program header file is a small assembly program which provides the entry
  1901. point for the program. This is where the execution of your program starts,
  1902. so it depends on the operating system, because operating systems pass
  1903. parameters to executables in wildly different ways.
  1904. It's name is \file{prt0.o}, and the
  1905. source file resides in \file{prt0.as} or some variant of this name. It
  1906. usually resided where the system unit source for your system resides.
  1907. It's main function is to save the environment and command-line arguments and
  1908. set up the stack. Then it calls the main program.
  1909. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1910. % MMX Support
  1911. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1912. \chapter{Intel MMX support}
  1913. \label{ch:MMXSupport}
  1914. \section{What is it about?}
  1915. \label{se:WhatisMMXabout}
  1916. \fpc supports the new MMX (Multi-Media extensions)
  1917. instructions of Intel processors. The idea of MMX is to
  1918. process multiple data with one instruction, for example the processor
  1919. can add simultaneously 4 words. To implement this efficiently, the
  1920. Pascal language needs to be extended. So Free Pascal allows
  1921. to add for example two \var{array[0..3] of word},
  1922. if MMX support is switched on. The operation is done
  1923. by the \var{MMX} unit and allows people without assembler knowledge to take
  1924. advantage of the MMX extensions.
  1925. Here is an example:
  1926. \begin{verbatim}
  1927. uses
  1928. MMX; { include some predefined data types }
  1929. const
  1930. { tmmxword = array[0..3] of word;, declared by unit MMX }
  1931. w1 : tmmxword = (111,123,432,4356);
  1932. w2 : tmmxword = (4213,63456,756,4);
  1933. var
  1934. w3 : tmmxword;
  1935. l : longint;
  1936. begin
  1937. if is_mmx_cpu then { is_mmx_cpu is exported from unit mmx }
  1938. begin
  1939. {$mmx+} { turn mmx on }
  1940. w3:=w1+w2;
  1941. {$mmx-}
  1942. end
  1943. else
  1944. begin
  1945. for i:=0 to 3 do
  1946. w3[i]:=w1[i]+w2[i];
  1947. end;
  1948. end.
  1949. \end{verbatim}
  1950. \section{Saturation support}
  1951. \label{se:SaturationSupport}
  1952. One important point of MMX is the support of saturated operations.
  1953. If a operation would cause an overflow, the value stays at the
  1954. highest or lowest possible value for the data type:
  1955. If you use byte values you get normally 250+12=6. This is very
  1956. annoying when doing color manipulations or changing audio samples,
  1957. when you have to do a word add and check if the value is greater than
  1958. 255. The solution is saturation: 250+12 gives 255.
  1959. Saturated operations are supported by the \var{MMX} unit. If you
  1960. want to use them, you have simple turn the switch saturation on:
  1961. \var{\$saturation+}
  1962. Here is an example:
  1963. \begin{verbatim}
  1964. Program SaturationDemo;
  1965. {
  1966. example for saturation, scales data (for example audio)
  1967. with 1.5 with rounding to negative infinity
  1968. }
  1969. uses mmx;
  1970. var
  1971. audio1 : tmmxword;
  1972. i: smallint;
  1973. const
  1974. helpdata1 : tmmxword = ($c000,$c000,$c000,$c000);
  1975. helpdata2 : tmmxword = ($8000,$8000,$8000,$8000);
  1976. begin
  1977. { audio1 contains four 16 bit audio samples }
  1978. {$mmx+}
  1979. { convert it to $8000 is defined as zero, multiply data with 0.75 }
  1980. audio1:=(audio1+helpdata2)*(helpdata1);
  1981. {$saturation+}
  1982. { avoid overflows (all values>$7fff becomes $ffff) }
  1983. audio1:=(audio1+helpdata2)-helpdata2;
  1984. {$saturation-}
  1985. { now mupltily with 2 and change to integer }
  1986. for i:=0 to 3 do
  1987. audio1[i] := audio1[i] shl 1;
  1988. audio1:=audio1-helpdata2;
  1989. {$mmx-}
  1990. end.
  1991. \end{verbatim}
  1992. \section{Restrictions of MMX support}
  1993. \label{se:MMXrestrictions}
  1994. In the beginning of 1997 the MMX instructions were introduced in the
  1995. Pentium processors, so multitasking systems wouldn't save the
  1996. newly introduced MMX registers. To work around that problem, Intel
  1997. mapped the MMX registers to the FPU register.
  1998. The consequence is that
  1999. you can't mix MMX and floating point operations. After using
  2000. MMX operations and before using floating point operations, you
  2001. have to call the routine \var{EMMS} of the \var{MMX} unit.
  2002. This routine restores the FPU registers.
  2003. {\em Careful:} The compiler doesn't warn if you mix floating point and
  2004. MMX operations, so be careful.
  2005. The MMX instructions are optimized for multi media (what else?).
  2006. So it isn't possible to perform each operation, some operations
  2007. give a type mismatch, see section \ref{se:SupportedMMX} for the supported
  2008. MMX operations
  2009. An important restriction is that MMX operations aren't range or overflow
  2010. checked, even when you turn range and overflow checking on. This is due to
  2011. the nature of MMX operations.
  2012. The \var{MMX} unit must always be used when doing MMX operations
  2013. because the exit code of this unit clears the MMX unit. If it wouldn't do
  2014. that, other program will crash. A consequence of this is that you can't use
  2015. MMX operations in the exit code of your units or programs, since they would
  2016. interfere with the exit code of the \var{MMX} unit. The compiler can't
  2017. check this, so you are responsible for this!
  2018. \section{Supported MMX operations}
  2019. \label{se:SupportedMMX}
  2020. The following operations are supported in the compiler when MMX
  2021. extensions are enabled:
  2022. \begin{itemize}
  2023. \item addition (\var{+})
  2024. \item subtraction (\var{-})
  2025. \item multiplication(\var{*})
  2026. \item logical exclusive or (\var{xor})
  2027. \item logical and (\var{and})
  2028. \item logical or (\var{or})
  2029. \item sign change (\var{-})
  2030. \end{itemize}
  2031. \section{Optimizing MMX support}
  2032. \label{se:OptimizingMMX}
  2033. Here are some helpful hints to get optimal performance:
  2034. \begin{itemize}
  2035. \item The \var{EMMS} call takes a lot of time, so try to seperate floating
  2036. point and MMX operations.
  2037. \item Use MMX only in low level routines because the compiler
  2038. saves all used MMX registers when calling a subroutine.
  2039. \item The NOT-operator isn't supported natively by MMX, so the
  2040. compiler has to generate a workaround and this operation
  2041. is inefficient.
  2042. \item Simple assignements of floating point numbers don't access
  2043. floating point registers, so you need no call to the \var{EMMS}
  2044. procedure. Only when doing arithmetic, you need to call the \var{EMMS}
  2045. procedure.
  2046. \end{itemize}
  2047. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2048. % Code issues
  2049. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2050. \chapter{Code issues}
  2051. \label{ch:Code}
  2052. This chapter gives detailed information on the generated code by
  2053. \fpc. It can be useful to write external object files which will
  2054. be linked to \fpc created code blocks.
  2055. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2056. % Register conventions
  2057. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2058. \section{Register Conventions}
  2059. \label{se:RegConvs}
  2060. The compiler has different register conventions, depending on the
  2061. target processor used; some of the registers have specific uses
  2062. during the code generation. The following section describes the
  2063. generic names of the registers on a platform per platform basis. It
  2064. also indicates what registers are used as scratch registers, and
  2065. which can be freely used in assembler blocks.
  2066. \subsection{accumulator register}
  2067. The accumulator register is at least a 32-bit integer
  2068. hardware register, and is used to return results of
  2069. function calls which return integral values.
  2070. \subsection{accumulator 64-bit register}
  2071. The accumulator 64-bit register is used in 32-bit environments
  2072. and is defined as the group of registers which will be used when
  2073. returning 64-bit integral results in function calls. This is a
  2074. register pair.
  2075. \subsection{float result register}
  2076. This register is used for returning floating point values
  2077. from functions.
  2078. \subsection{self register}
  2079. The self register contains a pointer to the actual object or class.
  2080. This register gives access to the data of the object or class, and
  2081. the VMT pointer of that object or class.
  2082. \subsection{frame pointer register}
  2083. The frame pointer register is used to access parameters in subroutines,
  2084. as well as to access local variables. References to the pushed
  2085. prameters and local variables are constructed using the frame pointer.
  2086. \footnote{The frame pointer is not available on all platforms}.
  2087. \subsection{stack pointer register}
  2088. The stack pointer is used to give the address of the stack area,
  2089. where the local variables and parameters to subroutines are stored.
  2090. \subsection{scratch registers}
  2091. Scratch registers are those which can be used in assembler
  2092. blocks, or in external object files without requiring any saving
  2093. before usage.
  2094. \subsection{Processor mapping of registers}
  2095. This indicates what registers are used for what purposes
  2096. on each of the processors supported by \fpc. It also indicates
  2097. which registers can be used as scratch registers.
  2098. \subsubsection{Intel 80x86 version}
  2099. \begin{FPCltable}{ll}{Intel 80x86 Register table}{80x86Regs} \hline
  2100. Generic register name & CPU Register name \\ \hline
  2101. accumulator & EAX \\
  2102. accumulator (64-bit) high / low & EDX:EAX \\
  2103. float result & FP(0) \\
  2104. self & ESI \\
  2105. frame pointer & EBP \\
  2106. stack pointer & ESP \\
  2107. scratch regs. & N/A \\
  2108. \end{FPCltable}
  2109. \subsubsection{Motorola 680x0 version}
  2110. \begin{FPCltable}{ll}{Motorola 680x0 Register table}{680x0Regs} \hline
  2111. Generic register name & CPU Register name \\ \hline
  2112. accumulator & D0\footnotemark \\
  2113. accumulator (64-bit) high / low & D0:D1 \\
  2114. float result & FP0\footnotemark\\
  2115. self & A5 \\
  2116. frame pointer & A6 \\
  2117. stack pointer & A7 \\
  2118. scratch regs. & D0, D1, A0, A1, FP0, FP1 \\
  2119. \end{FPCltable}
  2120. \addtocounter{footnote}{-1}\footnotetext{For compatibility with some C compilers, when
  2121. the function result is a pointer and is declared with the cdecl convention,
  2122. the result is also stored in the A0 register}
  2123. \addtocounter{footnote}{1}\footnotetext{On simulated FPU's the result is returned in D0}
  2124. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2125. % Name mangling
  2126. \section{Name mangling}
  2127. \label{se:NameMangling}
  2128. Contrary to most C compilers and assemblers, all labels generated
  2129. to pascal variables and routines have mangled names\footnote{This can be avoided
  2130. by using the \var{alias} or \var{cdecl} modifiers}. This
  2131. is done so that the compiler can do stronger type checking when parsing
  2132. the pascal code. It also permits function and procedure overloading.
  2133. \subsection{Mangled names for data blocks}
  2134. The rules for mangled names for variables and typed constants
  2135. are as follows:
  2136. \begin{itemize}
  2137. \item All variable names are converted to upper case
  2138. \item Variables in the main program or private to a unit have an \_ prepended to their names
  2139. \item Typed constants in the main program have an TC\_\_ prepended to their names
  2140. \item Public variables in a unit have their unit name prepended to them : U\_UNITNAME\_
  2141. \item Public and private typed constants in a unit have their unit name prepended to them :TC\_\_UNITNAME\$\$
  2142. \end{itemize}
  2143. Currently, in \fpc v1.0, if you declare a variable in unit name \var{tunit},
  2144. with the name \var{\_a}, and you declare the same variable with name \var{a}
  2145. in unit name \var{tunit\_}, you will get the same mangled name. This is
  2146. a limitation of the compiler which will be fixed in release v1.1.
  2147. Examples
  2148. \begin{verbatim}
  2149. unit testvars;
  2150. interface
  2151. const
  2152. publictypedconst : integer = 0;
  2153. var
  2154. publicvar : integer;
  2155. implementation
  2156. const
  2157. privatetypedconst : integer = 1;
  2158. var
  2159. privatevar : integer;
  2160. end.
  2161. \end{verbatim}
  2162. Will give the following assembler output under GNU as :
  2163. \begin{verbatim}
  2164. .file "testvars.pas"
  2165. .text
  2166. .data
  2167. # [6] publictypedconst : integer = 0;
  2168. .globl TC__TESTVARS$$_PUBLICTYPEDCONST
  2169. TC__TESTVARS$$_PUBLICTYPEDCONST:
  2170. .short 0
  2171. # [12] privatetypedconst : integer = 1;
  2172. TC__TESTVARS$$_PRIVATETYPEDCONST:
  2173. .short 1
  2174. .bss
  2175. # [8] publicvar : integer;
  2176. .comm U_TESTVARS_PUBLICVAR,2
  2177. # [14] privatevar : integer;
  2178. .lcomm _PRIVATEVAR,2
  2179. \end{verbatim}
  2180. \subsection{Mangled names for code blocks}
  2181. The rules for mangled names for routines are as follows:
  2182. \begin{itemize}
  2183. \item All routine names are converted to upper case.
  2184. \item Routines in a unit have their unit name prepended to them : \_UNITNAME\$\$\_
  2185. \item All Routines in the main program have a \_ prepended to them.
  2186. \item All parameters in a routine are mangled using the type of the parameter
  2187. (in uppercase) prepended by the \$ character. This is done in left to right order
  2188. for each parameter of the routine.
  2189. \item Objects and classes use special mangling : The class type or object type
  2190. is given in the mangled name; The mangled name is as follows: \_\$\$\_TYPEDECL\_\$\$
  2191. optionally preceded by mangled name of the unit and finishing with the method
  2192. name.
  2193. \end{itemize}
  2194. The following constructs
  2195. \begin{verbatim}
  2196. unit testman;
  2197. interface
  2198. type
  2199. myobject = object
  2200. constructor init;
  2201. procedure mymethod;
  2202. end;
  2203. implementation
  2204. constructor myobject.init;
  2205. begin
  2206. end;
  2207. procedure myobject.mymethod;
  2208. begin
  2209. end;
  2210. function myfunc: pointer;
  2211. begin
  2212. end;
  2213. procedure myprocedure(var x: integer; y: longint; z : pchar);
  2214. begin
  2215. end;
  2216. end.
  2217. \end{verbatim}
  2218. will result in the following assembler file for the Intel 80x86 target:
  2219. \begin{verbatim}
  2220. .file "testman.pas"
  2221. .text
  2222. .balign 16
  2223. .globl _TESTMAN$$_$$_MYOBJECT_$$_INIT
  2224. _TESTMAN$$_$$_MYOBJECT_$$_INIT:
  2225. pushl %ebp
  2226. movl %esp,%ebp
  2227. subl $4,%esp
  2228. movl $0,%edi
  2229. call FPC_HELP_CONSTRUCTOR
  2230. jz .L5
  2231. jmp .L7
  2232. .L5:
  2233. movl 12(%ebp),%esi
  2234. movl $0,%edi
  2235. call FPC_HELP_FAIL
  2236. .L7:
  2237. movl %esi,%eax
  2238. testl %esi,%esi
  2239. leave
  2240. ret $8
  2241. .balign 16
  2242. .globl _TESTMAN$$_$$_MYOBJECT_$$_MYMETHOD
  2243. _TESTMAN$$_$$_MYOBJECT_$$_MYMETHOD:
  2244. pushl %ebp
  2245. movl %esp,%ebp
  2246. leave
  2247. ret $4
  2248. .balign 16
  2249. _TESTMAN$$_MYFUNC:
  2250. pushl %ebp
  2251. movl %esp,%ebp
  2252. subl $4,%esp
  2253. movl -4(%ebp),%eax
  2254. leave
  2255. ret
  2256. .balign 16
  2257. _TESTMAN$$_MYPROCEDURE$INTEGER$LONGINT$PCHAR:
  2258. pushl %ebp
  2259. movl %esp,%ebp
  2260. leave
  2261. ret $12
  2262. \end{verbatim}
  2263. \subsection{Modifying the mangled names}
  2264. To make the symbols externally accessible, it is possible to
  2265. give nicknames to mangled names, or to change the mangled
  2266. name directly. Two modifiers can be used:
  2267. \begin{description}
  2268. \item [cdecl:\ ] A function that has a \var{cdecl} modifier, will be used
  2269. with C calling conventions, that is, the caller clears the stack. Also
  2270. the mangled name will be the name {\em exactly} as it is declared.
  2271. \var{cdecl} is part of the function declaration, and hence must be present
  2272. both in the interface and implementation section of a unit.
  2273. \item [alias: ] The \var{alias} modifier can be used to assign a second
  2274. assembler label to your function. This label has the same name as the
  2275. alias name you declared. This doesn't modify the calling conventions
  2276. of the function. In other words, the \var{alias} modifier allows you to specify
  2277. another name (a nickname) for your function or procedure.
  2278. The prototype for an aliased function or procedure is as follows:
  2279. \begin{verbatim}
  2280. Procedure AliasedProc; alias : 'AliasName';
  2281. \end{verbatim}
  2282. The procedure \var{AliasedProc} will also be known as \var{AliasName}. Take
  2283. care, the name you specify is case sensitive (as C is).
  2284. \end{description}
  2285. Furthermore, the \var{exports} section of a library is also used to
  2286. declare the names that will be exported by the shared library. The
  2287. names in the exports section are case-sensitive (while the actual
  2288. declaration of the routine is not). For more information on the
  2289. creating shared libraries, \seec{libraries}.
  2290. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2291. % Calling mechanism
  2292. \section{Calling mechanism}
  2293. \label{se:CallingConventions}
  2294. Procedures and Functions are called with their parameters on the stack.
  2295. Contrary to Turbo Pascal, {\em all} parameters are pushed on the stack, and
  2296. they are pushed {\em right} to {\em left}, instead of left to right for
  2297. Turbo Pascal. This is especially important if you have some assembly
  2298. subroutines in Turbo Pascal which you would like to translate to \fpc.
  2299. Function results are returned in the accumulator, if they fit in the
  2300. register. Methods calls (from either objects or clases) have an additional
  2301. invisible parameter which is \var{self}. This parameter is the leftmost
  2302. parameter within a method call (it is therefore the last parameter passed
  2303. to the method).
  2304. When the procedure or function exits, it clears the stack.
  2305. Other calling methods are available for linking with
  2306. external object files and libraries, these are summarized in \seet{CallingTable}.
  2307. The first column lists the modifier you specify for a procedure declaration.
  2308. The second one lists the order the paramaters are pushed on the stack.
  2309. The third column specifies who is responsible for cleaning the stack:
  2310. the caller or the called function. The alignment column indicates the
  2311. alignment of the parameters sent to the stack area. Finally, the fifth
  2312. column indicates if any registers are saved in the entry code of the
  2313. subroutine.
  2314. \begin{FPCltable}{lllll}{Calling mechanisms in \fpc}{CallingTable}\hline
  2315. Modifier & Pushing order & Stack cleaned by & alignment & registers saved \\
  2316. \hline
  2317. <none> & Right-to-left & Function & default & None\\
  2318. cdecl & Right-to-left & Caller & GCC alignment & GCC registers\\
  2319. interrupt & Right-to-left & Function & default & all registers\\
  2320. pascal & Left-to-right & Function & default & None\\
  2321. safecall & Right-to-left & Function & default & GCC registers\\
  2322. stdcall & Right-to-left & Function & GCC alignment & GCC registers \\
  2323. popstack & Right-to-left & Caller & default & None \\
  2324. register & Left-to-right & Caller & default & None \\ \hline
  2325. \end{FPCltable}
  2326. More about this can be found in \seec{Linking} on linking. Information
  2327. on GCC registers saved, GCC stack alignment and general stack alignment
  2328. on an operating system basis can be found in Appendix \ref{ch:AppH}. The \var{register}
  2329. modifier is currently not supported, and maps to the default calling
  2330. convention.
  2331. Furthermore, the \var{saveregisters} modifier can be used with
  2332. any of the calling mechanism specifiers. When \var{saveregisters}
  2333. is used, all registers will be saved on entry to the routine,
  2334. and will be restored upon exit. Of course, if the routine is
  2335. a function, and it normally returns its retun value in a register,
  2336. that register will not be saved. Also, if the self register is
  2337. used, it will also neither be saved nor restored.
  2338. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2339. % Nested subroutines
  2340. \section{Nested procedure and functions}
  2341. \label{se:NestedRoutines}
  2342. When a routine is declared within the scope of a procedure or
  2343. function, it is said to be nested. In this case, an additional
  2344. invisible parameter is passed to the nested routine. This
  2345. additional parameter is the frame pointer address of the
  2346. calling routine. This permits the nested routine to access
  2347. the local variables and parameters of the calling routine.
  2348. The resulting stack frame after the entry code of a simple nested procedure
  2349. has been executed is shown in \seet{NestedStackFrame32}.
  2350. \begin{FPCltable}{ll}{Stack frame when calling a nested procedure (32-bit processors)}{NestedStackFrame32}
  2351. \hline
  2352. Offset from frame pointer & What is stored \\ \hline
  2353. +x & parameters\\
  2354. +8 & Frame pointer of parent routine\\
  2355. +4 & Return address\\
  2356. +0 & Saved frame pointer\\ \hline
  2357. \end{FPCltable}
  2358. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2359. % Constructor and destructor calls
  2360. \section{Constructor and Destructor calls}
  2361. \label{se:ConsDest}
  2362. Constructor and destructors have special invisible parameters
  2363. which are passed to them. These invisible parameters are used
  2364. internally to instanciate the objects and classes.
  2365. \subsection{objects}
  2366. The actual invisible declaration of an object constructoir
  2367. is as follows:
  2368. \begin{verbatim}
  2369. constructor init(_vmt : pointer; _self : pointer ...);
  2370. \end{verbatim}
  2371. Where \var{\_vmt} is a pointer to the virtual method table
  2372. for this object. This value is nil if a constructor is called
  2373. within the object instance (such as calling an ihnerited constructor).
  2374. \var{\_self} is either nil if the instance must be allocated
  2375. dynamically (object is declared as pointer), or the address of
  2376. the object instance if the object is declared as a normal object
  2377. (stored in the data area) or if the object instance has already
  2378. been allocated.
  2379. The allocated instance (if allocated via new) (\var{self})
  2380. is returned in the accumulator.
  2381. The declaration of a destructor is as follows:
  2382. \begin{verbatim}
  2383. destructor done(_vmt : pointer; _self : pointer ...);
  2384. \end{verbatim}
  2385. Where \var{\_vmt} is a pointer to the virtual method table
  2386. for this object. This value is nil if a destructor is called
  2387. within the object instance (such as calling an ihnerited constructor),
  2388. or if the object instance is a variable and not a pointer.
  2389. \var{\_self} is the address of the object instance.
  2390. \subsection{classes}
  2391. The actual invisible declaration of a class constructoir
  2392. is as follows:
  2393. \begin{verbatim}
  2394. constructor init(_vmt: pointer; flag : longint; ..);
  2395. \end{verbatim}
  2396. \var{\_vmt} is either nil if called from the instance
  2397. or if calling an inherited constructor, otherwise
  2398. it points to the address of the virtual method table.
  2399. Where \var{flag} is zero if the constructor is called
  2400. within the object instance or with an instance qualifier
  2401. otherwise this flag is set to one.
  2402. The allocated instance (\var{self}) is returned in the accumulator.
  2403. The declaration of a destructor is as follows:
  2404. \begin{verbatim}
  2405. destructor done(_self : pointer; flag : longint ...);
  2406. \end{verbatim}
  2407. \var{\_self} is the address of the object instance.
  2408. \var{flag} is zero if the destructor is called
  2409. within the object instance or with an instance qualifier
  2410. otherwise this flag is set to one.
  2411. \section{Entry and exit code}
  2412. Each Pascal procedure and function begins and ends with standard
  2413. epilogue and prologue code.
  2414. \subsection{Intel 80x86 standard routine prologue / epilogue}
  2415. Standard entry code for procedures and functions is as follows on the
  2416. 80x86 architecture:
  2417. \begin{verbatim}
  2418. pushl %ebp
  2419. movl %esp,%ebp
  2420. \end{verbatim}
  2421. The generated exit sequence for procedure and functions looks as follows:
  2422. \begin{verbatim}
  2423. leave
  2424. ret $xx
  2425. \end{verbatim}
  2426. Where \var{xx} is the total size of the pushed parameters.
  2427. To have more information on function return values take a look at
  2428. \sees{RegConvs}.
  2429. \subsection{Motorola 680x0 standard routine prologue / epilogue}
  2430. Standard entry code for procedures and functions is as follows on the
  2431. 680x0 architecture:
  2432. \begin{verbatim}
  2433. move.l a6,-(sp)
  2434. move.l sp,a6
  2435. \end{verbatim}
  2436. The generated exit sequence for procedure and functions looks as follows
  2437. (in the default processor mode):
  2438. \begin{verbatim}
  2439. unlk a6
  2440. rtd #xx
  2441. \end{verbatim}
  2442. Where \var{xx} is the total size of the pushed parameters.
  2443. To have more information on function return values take a look at
  2444. \sees{RegConvs}.
  2445. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2446. % Parameter passing
  2447. \section{Parameter passing}
  2448. \label{se:Parameters}
  2449. When a function or procedure is called, then the following is done by the
  2450. compiler:
  2451. \begin{enumerate}
  2452. \item If there are any parameters to be passed to the procedure, they are
  2453. pushed from right to left on the stack.
  2454. \item If a function is called that returns a variable of type \var{String},
  2455. \var{Set}, \var{Record}, \var{Object} or \var{Array}, then an address to
  2456. store the function result in, is pushed on the stack.
  2457. \item If the called procedure or function is an object method, then the
  2458. pointer to \var{self} is pushed on the stack.
  2459. \item If the procedure or function is nested in another function or
  2460. procedure, then the frame pointer of the parent procedure is pushed on the
  2461. stack.
  2462. \item The return address is pushed on the stack (This is done automatically
  2463. by the instruction which calls the subroutine).
  2464. \end{enumerate}
  2465. The resulting stack frame upon entering looks as in \seet{StackFrame}.
  2466. \begin{FPCltable}{llc}{Stack frame when calling a procedure (32-bit model)}{StackFrame}
  2467. \hline
  2468. Offset & What is stored & Optional? \\ \hline
  2469. +x & parameters & Yes \\
  2470. +12 & function result & Yes \\
  2471. +8 & self & Yes \\
  2472. +4 & Return address & No\\
  2473. +0 & Frame pointer of parent procedure & Yes \\ \hline
  2474. \end{FPCltable}
  2475. \subsection{Parameter alignment}
  2476. Each parameter passed to a routine is guaranteed to decrement the
  2477. stack pointer by a certain minimum amount. This behavior varies
  2478. from one operating system to another. For example, passing a
  2479. byte as a value parameter to a routine could either decrement the
  2480. stack pointer by 1, 2, 4 or even 8 bytes depending on the target
  2481. operating system and processor. The minimal default stack pointer decrement
  2482. value is given in Appendix \ref{ch:AppH}.
  2483. For example, on \freebsd, all parameters passed to a routine guarantee
  2484. a minimal stack decrease of four bytes per parameter, even if the
  2485. parameter actually takes less then 4 bytes to store on the stack (such
  2486. as passing a byte value parameter to the stack).
  2487. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2488. \section{Processor limitations}
  2489. \label{se:ProcessorLimits}
  2490. Certain processors have limitations on the size of the parameters
  2491. and local variables in routines. This is shown in \seet{CPULimits}.
  2492. \begin{FPCltable}{lll}{Maximum limits for processors}{CPULimits}
  2493. \hline
  2494. Processor & Parameters & Local variables \\ \hline
  2495. Intel 80x86 (all) & 64K & No limit\\
  2496. Motorola 68020 (default) & 32K & No limit\\
  2497. Motorola 68000 & 32K & 32K \\ \hline
  2498. \end{FPCltable}
  2499. Furthermore, the m68k compiler, in \var{68000} mode, limits the
  2500. size of data elements to 32K (arrays, records, objects, etc.).
  2501. This restriction does not exist in \var{68020} mode.
  2502. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2503. % Local variables
  2504. %\section{Local variables}
  2505. %\label{se:LocalVars}
  2506. %
  2507. % Stack alignment for local variables to complete -
  2508. % Currently the FPC version 1.0 stack alignment is
  2509. % simply too messy to describe consistently.
  2510. %
  2511. %
  2512. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2513. % Linking issues
  2514. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2515. \chapter{Linking issues}
  2516. \label{ch:Linking}
  2517. When you only use Pascal code, and Pascal units, then you will not see much
  2518. of the part that the linker plays in creating your executable.
  2519. The linker is only called when you compile a program. When compiling units,
  2520. the linker isn't invoked.
  2521. However, there are times that linking to C libraries, or to external
  2522. object files created by other compilers may be necessary. The \fpc compiler
  2523. can generate calls to a C function, and can generate functions that can
  2524. be called from C (exported functions).
  2525. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2526. % Using external code or variables
  2527. \section{Using external code and variables}
  2528. In general, there are 3 things you must do to use a function that resides in
  2529. an external library or object file:
  2530. \begin{enumerate}
  2531. \item You must make a pascal declaration of the function or procedure you
  2532. want to use.
  2533. \item You must declare the correct calling convention to use.
  2534. \item You must tell the compiler where the function resides, i.e. in what
  2535. object file or what library, so the compiler can link the necessary code in.
  2536. \end{enumerate}
  2537. The same holds for variables. To access a variable that resides in an
  2538. external object file, you must declare it, and tell the compiler where to
  2539. find it.
  2540. The following sections attempt to explain how to do this.
  2541. \subsection{Declaring external functions or procedures}
  2542. \label{se:ExternalFunction}
  2543. The first step in using external code blocks is declaring the function you
  2544. want to use. \fpc supports Delphi syntax, i.e. you must use the
  2545. \var{external} directive. The \var{external} directive replaces, in effect,
  2546. the code block of the function.
  2547. The external directive doesn't specify a calling convention; it just tells
  2548. the compiler that the code for a procedure or function resides in an
  2549. external code block. A calling convention modifier should be declared
  2550. if the external code blocks does not have the same calling conventions
  2551. as \fpc. For more information on the calling conventions
  2552. \sees{CallingConventions}.
  2553. There exist four variants of the external directive:
  2554. \begin{enumerate}
  2555. \item A simple external declaration:
  2556. \begin{verbatim}
  2557. Procedure ProcName (Args : TPRocArgs); external;
  2558. \end{verbatim}
  2559. The \var{external} directive tells the compiler that the function resides in
  2560. an external block of code. You can use this together with the \var{\{\$L\}}
  2561. or \var{\{\$LinkLib\}} directives to link to a function or procedure in a
  2562. library or external object file. Object files are looked for in the object
  2563. search path (set by \var{-Fo}) and libraries are searched for in the linker
  2564. path (set by \var{-Fl}).
  2565. \item You can give the \var{external} directive a library name as an
  2566. argument:
  2567. \begin{verbatim}
  2568. Procedure ProcName (Args : TPRocArgs); external 'Name';
  2569. \end{verbatim}
  2570. This tells the compiler that the procedure resides in a library with name
  2571. \var{'Name'}. This method is equivalent to the following:
  2572. \begin{verbatim}
  2573. Procedure ProcName (Args : TPRocArgs);external;
  2574. {$LinkLib 'Name'}
  2575. \end{verbatim}
  2576. \item The \var{external} can also be used with two arguments:
  2577. \begin{verbatim}
  2578. Procedure ProcName (Args : TPRocArgs); external 'Name'
  2579. name 'OtherProcName';
  2580. \end{verbatim}
  2581. This has the same meaning as the previous declaration, only the compiler
  2582. will use the name \var{'OtherProcName'} when linking to the library. This
  2583. can be used to give different names to procedures and functions in an
  2584. external library. The name of the routine is case-sensitive and should
  2585. match exactly the name of the routine in the object file.
  2586. This method is equivalent to the following code:
  2587. \begin{verbatim}
  2588. Procedure OtherProcName (Args : TProcArgs); external;
  2589. {$LinkLib 'Name'}
  2590. Procedure ProcName (Args : TPRocArgs);
  2591. begin
  2592. OtherProcName (Args);
  2593. end;
  2594. \end{verbatim}
  2595. \item Lastly, onder \windows and \ostwo, there is a fourth possibility
  2596. to specify an external function: In \file{.DLL} files, functions also have
  2597. a unique number (their index). It is possible to refer to these fuctions
  2598. using their index:
  2599. \begin{verbatim}
  2600. Procedure ProcName (Args : TPRocArgs); external 'Name' Index SomeIndex;
  2601. \end{verbatim}
  2602. This tells the compiler that the procedure \var{ProcName} resides in a
  2603. dynamic link library, with index {SomeIndex}.
  2604. \begin{remark}Note that this is ONLY available under \windows and \ostwo.
  2605. \end{remark}
  2606. \end{enumerate}
  2607. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2608. % Declaring an external variable
  2609. \subsection{Declaring external variables}
  2610. \label{se:ExternalVars}
  2611. Some libaries or code blocks have variables which they export. You can access
  2612. these variables much in the same way as external functions. To access an
  2613. external variable, you declare it as follows:
  2614. \begin{verbatim}
  2615. Var
  2616. MyVar : MyType; external name 'varname';
  2617. \end{verbatim}
  2618. The effect of this declaration is twofold:
  2619. \begin{enumerate}
  2620. \item No space is allocated for this variable.
  2621. \item The name of the variable used in the assembler code is \var{varname}.
  2622. This is a case sensitive name, so you must be careful.
  2623. \end{enumerate}
  2624. The variable will be
  2625. accessible with it's declared name, i.e. \var{MyVar} in this case.
  2626. A second possibility is the declaration:
  2627. \begin{verbatim}
  2628. Var
  2629. varname : MyType; cvar; external;
  2630. \end{verbatim}
  2631. The effect of this declaration is twofold as in the previous case:
  2632. \begin{enumerate}
  2633. \item The \var{external} modifier ensures that no space is allocated for
  2634. this variable.
  2635. \item The \var{cvar} modifier tells the compiler that the name of the
  2636. variable used in the assembler code is exactly as specified in the
  2637. declaration. This is a case sensitive name, so you must be careful.
  2638. \end{enumerate}
  2639. The first possibility allows you to change the name of the
  2640. external variable for internal use.
  2641. As an example, let's look at the following C file (in \file{extvar.c}):
  2642. \begin{verbatim}
  2643. /*
  2644. Declare a variable, allocate storage
  2645. */
  2646. int extvar = 12;
  2647. \end{verbatim}
  2648. And the following program (in \file{extdemo.pp}):
  2649. \begin{verbatim}
  2650. Program ExtDemo;
  2651. {$L extvar.o}
  2652. Var { Case sensitive declaration !! }
  2653. extvar : longint; cvar;external;
  2654. I : longint; external name 'extvar';
  2655. begin
  2656. { Extvar can be used case insensitive !! }
  2657. Writeln ('Variable ''extvar'' has value: ',ExtVar);
  2658. Writeln ('Variable ''I'' has value: ',i);
  2659. end.
  2660. \end{verbatim}
  2661. Compiling the C file, and the pascal program:
  2662. \begin{verbatim}
  2663. gcc -c -o extvar.o extvar.c
  2664. ppc386 -Sv extdemo
  2665. \end{verbatim}
  2666. Will produce a program \file{extdemo} which will print
  2667. \begin{verbatim}
  2668. Variable 'extvar' has value: 12
  2669. Variable 'I' has value: 12
  2670. \end{verbatim}
  2671. on your screen.
  2672. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2673. % Declaring the calling conventions
  2674. \subsection{Declaring the calling convention modifier}
  2675. To make sure that all parameters are correctly passed to the
  2676. external routines, you should declare it with the correct
  2677. calling convention modifier. When linking with code blocks
  2678. compiled with standard C compilers (such as GCC), the \var{cdecl}
  2679. modifier should be used so as to indicate that the external
  2680. routine uses C type calling conventions. For more information
  2681. on the supported calling conventions, \sees{CallingConventions}.
  2682. As might expected, external variable declarations do not require
  2683. any calling convention modifier.
  2684. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2685. % Linking an object file in your program
  2686. \subsection{Declaring the external object code}
  2687. \subsubsection{Linking to an object file}
  2688. \label{se:LinkIn}
  2689. Having declared the external function or variable that resides in an object file,
  2690. you can use it as if it was defined in your own program or unit.
  2691. To produce an executable, you must still link the object file in.
  2692. This can be done with the \var{\{\$L file.o\}} directive.
  2693. This will cause the linker to link in the object file \file{file.o}. On
  2694. most systems, this filename is case sensitive. The object file is
  2695. first searched in the current directory, and then the directories specified
  2696. by the \var{-Fo} command line.
  2697. You cannot specify libraries in this way, it is for object files only.
  2698. Here we present an example. Consider that you have some assembly routine which uses
  2699. the C calling convention that calculates the nth Fibonacci number:
  2700. \begin{verbatim}
  2701. .text
  2702. .align 4
  2703. .globl Fibonacci
  2704. .type Fibonacci,@function
  2705. Fibonacci:
  2706. pushl %ebp
  2707. movl %esp,%ebp
  2708. movl 8(%ebp),%edx
  2709. xorl %ecx,%ecx
  2710. xorl %eax,%eax
  2711. movl $1,%ebx
  2712. incl %edx
  2713. loop:
  2714. decl %edx
  2715. je endloop
  2716. movl %ecx,%eax
  2717. addl %ebx,%eax
  2718. movl %ebx,%ecx
  2719. movl %eax,%ebx
  2720. jmp loop
  2721. endloop:
  2722. movl %ebp,%esp
  2723. popl %ebp
  2724. ret
  2725. \end{verbatim}
  2726. Then you can call this function with the following Pascal Program:
  2727. \begin{verbatim}
  2728. Program FibonacciDemo;
  2729. var i : longint;
  2730. Function Fibonacci (L : longint):longint;cdecl;external;
  2731. {$L fib.o}
  2732. begin
  2733. For I:=1 to 40 do
  2734. writeln ('Fib(',i,') : ',Fibonacci (i));
  2735. end.
  2736. \end{verbatim}
  2737. With just two commands, this can be made into a program:
  2738. \begin{verbatim}
  2739. as -o fib.o fib.s
  2740. ppc386 fibo.pp
  2741. \end{verbatim}
  2742. This example supposes that you have your assembler routine in \file{fib.s},
  2743. and your Pascal program in \file{fibo.pp}.
  2744. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2745. % Linking your program to a library
  2746. \subsubsection{Linking to a library}
  2747. \label{se:LinkOut}
  2748. To link your program to a library, the procedure depends on how you declared
  2749. the external procedure.
  2750. In case you used the following syntax to declare your procedure:
  2751. \begin{verbatim}
  2752. Procedure ProcName (Args : TPRocArgs); external 'Name';
  2753. \end{verbatim}
  2754. You don't need to take additional steps to link your file in, the compiler
  2755. will do all that is needed for you. On \windows it will link to
  2756. \file{name.dll}, on \linux and most \unix'es your program will be linked to library
  2757. \file{libname}, which can be a static or dynamic library.
  2758. In case you used
  2759. \begin{verbatim}
  2760. Procedure ProcName (Args : TPRocArgs); external;
  2761. \end{verbatim}
  2762. You still need to explicity link to the library. This can be done in 2 ways:
  2763. \begin{enumerate}
  2764. \item You can tell the compiler in the source file what library to link to
  2765. using the \var{\{\$LinkLib 'Name'\}} directive:
  2766. \begin{verbatim}
  2767. {$LinkLib 'gpm'}
  2768. \end{verbatim}
  2769. This will link to the \file{gpm} library. On \unix systems (such as \linux),
  2770. you must not specify the extension or 'lib' prefix of the library. The compiler takes
  2771. care of that. On other systems (such as \windows, you need to specify the full
  2772. name.
  2773. \item You can also tell the compiler on the command-line to link in a
  2774. library: The \var{-k} option can be used for that. For example
  2775. \begin{verbatim}
  2776. ppc386 -k'-lgpm' myprog.pp
  2777. \end{verbatim}
  2778. Is equivalent to the above method, and tells the linker to link to the
  2779. \file{gpm} library.
  2780. \end{enumerate}
  2781. As an example; consider the following program:
  2782. \begin{verbatim}
  2783. program printlength;
  2784. {$linklib c} { Case sensitive }
  2785. { Declaration for the standard C function strlen }
  2786. Function strlen (P : pchar) : longint; cdecl;external;
  2787. begin
  2788. Writeln (strlen('Programming is easy !'));
  2789. end.
  2790. \end{verbatim}
  2791. This program can be compiled with:
  2792. \begin{verbatim}
  2793. ppc386 prlen.pp
  2794. \end{verbatim}
  2795. Supposing, of course, that the program source resides in \file{prlen.pp}.
  2796. To use functions in C that have a variable number of arguments, you must
  2797. compile your unit or program in \var{objfpc} mode or \var{Delphi} mode,
  2798. and use the \var{Array of const} argument, as in the following example:
  2799. \begin{verbatim}
  2800. program testaocc;
  2801. {$mode objfpc}
  2802. Const
  2803. P : Pchar
  2804. = 'example';
  2805. F : Pchar
  2806. = 'This %s uses printf to print numbers (%d) and strings.'#10;
  2807. procedure printf(fm: pchar;args: array of const);cdecl;external 'c';
  2808. begin
  2809. printf(F,[P,123]);
  2810. end.
  2811. \end{verbatim}
  2812. The output of this program looks like this:
  2813. \begin{verbatim}
  2814. This example uses printf to print numbers (123) and strings.
  2815. \end{verbatim}
  2816. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2817. % Making a library
  2818. \section{Making libraries}
  2819. \label{se:StaticLib}
  2820. \fpc supports making shared or static libraries in a straightforward and
  2821. easy manner. If you want to make static libraries for other \fpc programmers,
  2822. you just need to provide a command line switch. To make shared libraries,
  2823. refer to the \seec{libraries}. If you want C programmers to be able to use your
  2824. code as well, you will need to adapt your code a little. This process
  2825. is described first.
  2826. % Exporting functions.
  2827. \subsection{Exporting functions}
  2828. When exporting functions from a library, there are 2 things you must take in
  2829. account:
  2830. \begin{enumerate}
  2831. \item Calling conventions.
  2832. \item Naming scheme.
  2833. \end{enumerate}
  2834. The calling conventions are controlled by the modifiers \var{cdecl},
  2835. \var{popstack}, \var{pascal}, \var{safecall}, \var{stdcall} and
  2836. \var{register}. See \sees{CallingConventions} for more information on the
  2837. different kinds of calling scheme.
  2838. The naming conventions can be controlled by 2 modifiers in the case
  2839. of static libraries:
  2840. \begin{itemize}
  2841. \item \var{cdecl}
  2842. \item \var{alias}
  2843. \end{itemize}
  2844. For more information on how these different modifiers change the name
  2845. mangling of the routine \sees{NameMangling}.
  2846. \begin{remark}If you use in your unit functions that are in other units, or
  2847. system functions, then the C program will need to link in the object files
  2848. from these units too.
  2849. \end{remark}
  2850. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2851. % Exporting variables.
  2852. \subsection{Exporting variables}
  2853. Similarly as when you export functions, you can export variables.
  2854. When exportig variables, one should only consider the names of the
  2855. variables. To declare a variable that should be used by a C program,
  2856. one declares it with the \var{cvar} modifier:
  2857. \begin{verbatim}
  2858. Var MyVar : MyTpe; cvar;
  2859. \end{verbatim}
  2860. This will tell the compiler that the assembler name of the variable (the one
  2861. which is used by C programs) should be exactly as specified in the
  2862. declaration, i.e., case sensitive.
  2863. It is not allowed to declare multiple variables as \var{cvar} in one
  2864. statement, i.e. the following code will produce an error:
  2865. \begin{verbatim}
  2866. var Z1,Z2 : longint;cvar;
  2867. \end{verbatim}
  2868. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2869. % Compiling libraries
  2870. \subsection {Compiling libraries}
  2871. Once you have your (adapted) code, with exported and other functions,
  2872. you can compile your unit, and tell the compiler to make it into a library.
  2873. The compiler will simply compile your unit, and perform the necessary steps
  2874. to transform it into a \var{static} or \var{shared} (\var{dynamic}) library.
  2875. You can do this as follows, for a dynamic library:
  2876. \begin{verbatim}
  2877. ppc386 -CD myunit
  2878. \end{verbatim}
  2879. On \unix systems, such as \linux, this will leave you with a file \file{libmyunit.so}. On \windows
  2880. and \ostwo, this will leave you with \file{myunit.dll}. An easier way to
  2881. create shared libraries is to use the \var{library} keyword. For more
  2882. information on creating shared libraries, \seec{libraries}.
  2883. If you want a static library, you can do
  2884. \begin{verbatim}
  2885. ppc386 -CS myunit
  2886. \end{verbatim}
  2887. This will leave you with \file{libmyunit.a} and a file \file{myunit.ppu}.
  2888. The \file{myunit.ppu} is the unit file needed by the \fpc compiler.
  2889. The resulting files are then libraries. To make static libraries, you need
  2890. the \file{ranlib} or \var{ar} program on your system. It is standard on most
  2891. \unix systems, and is provided with the \file{gcc} compiler under \dos.
  2892. For the dos distribution, a copy of ar is included in the file
  2893. \file{gnuutils.zip}.
  2894. {\em BEWARE:} This command doesn't include anything but the current unit in
  2895. the library. Other units are left out, so if you use code from other units,
  2896. you must deploy them together with your library.
  2897. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2898. % unit searching
  2899. \subsection{Unit searching strategy}
  2900. When you compile a unit, the compiler will by
  2901. default always look for unit files.
  2902. To be able to differentiate between units that have been compiled as static
  2903. or dynamic libraries, there are 2 switches:
  2904. \begin{description}
  2905. \item [-XD:\ ] This will define the symbol \var{FPC\_LINK\_DYNAMIC}
  2906. \item [-XS:\ ] This will define the symbol \var{FPC\_LINK\_STATIC}
  2907. \end{description}
  2908. Definition of one symbol will automatically undefine the other.
  2909. These two switches can be used in conjunction with the configuration file
  2910. \file{fpc.cfg}. The existence of one of these symbols can be used to
  2911. decide which unit search path to set. For example, on \linux:
  2912. \begin{verbatim}
  2913. # Set unit paths
  2914. #IFDEF FPC_LINK_STATIC
  2915. -Up/usr/lib/fpc/linuxunits/staticunits
  2916. #ENDIF
  2917. #IFDEF FPC_LINK_DYNAMIC
  2918. -Up/usr/lib/fpc/linuxunits/sharedunits
  2919. #ENDIF
  2920. \end{verbatim}
  2921. With such a configuration file, the compiler will look for it's units in
  2922. different directories, depending on whether \var{-XD} or \var{-XS} is used.
  2923. \section{Using smart linking}
  2924. \label{se:SmartLinking}
  2925. You can compile your units using smart linking. When you use smartlinking,
  2926. the compiler creates a series of code blocks that are as small as possible,
  2927. i.e. a code block will contain only the code for one procedure or function.
  2928. When you compile a program that uses a smart-linked unit, the compiler will
  2929. only link in the code that you actually need, and will leave out all other
  2930. code. This will result in a smaller binary, which is loaded in memory
  2931. faster, thus speeding up execution.
  2932. To enable smartlinking, one can give the smartlink option on the command
  2933. line: \var{-Cx}, or one can put the \var{\{\$SMARTLINK ON\}} directive in
  2934. the unit file:
  2935. \begin{verbatim}
  2936. Unit Testunit
  2937. {SMARTLINK ON}
  2938. Interface
  2939. ...
  2940. \end{verbatim}
  2941. Smartlinking will slow down the compilation process, especially for large
  2942. units.
  2943. When a unit \file{foo.pp} is smartlinked, the name of the codefile is
  2944. changed to \file{libfoo.a}.
  2945. Technically speaking, the compiler makes small assembler files for each
  2946. procedure and function in the unit, as well as for all global defined
  2947. variables (whether they're in the interface section or not). It then
  2948. assembles all these small files, and uses \file{ar} to collect the resulting
  2949. object files in one archive.
  2950. Smartlinking and the creation of shared (or dynamic) libraries are mutually
  2951. exclusive, that is, if you turn on smartlinking, then the creation of shared
  2952. libraries is turned of. The creation of static libraries is still possible.
  2953. The reason for this is that it has little sense in making a smartlinked
  2954. dynamical library. The whole shared library is loaded into memory anyway by
  2955. the dynamic linker (or the operating system), so there would be no gain in size by
  2956. making it smartlinked.
  2957. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2958. % Memory issues
  2959. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2960. \chapter{Memory issues}
  2961. \label{ch:Memory}
  2962. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2963. % The memory model
  2964. \section{The memory model.}
  2965. \label{se:ThirtytwoBit}
  2966. The \fpc compiler issues 32-bit or 64-bit code.
  2967. This has several consequences:
  2968. \begin{itemize}
  2969. \item You need a 32-bit or 64-bit processor to run the generated code. The
  2970. compiler functions on a 286 when you compile it using Turbo Pascal,
  2971. but the generated programs cannot be assembled or executed.
  2972. \item You don't need to bother with segment selectors. Memory can be
  2973. addressed using a single 32-bit (on 32-bit processors) or 64-bit (on
  2974. 64-bit processors with 64-bit addressing) pointer.
  2975. The amount of memory is limited only by the available amount of (virtual)
  2976. memory on your machine.
  2977. \item The structures you define are unlimited in size. Arrays can be as long
  2978. as you want. You can request memory blocks from any size.
  2979. \end{itemize}
  2980. The fact that 16-bit code is no longer used, means that some of the older
  2981. Turbo Pascal constructs and functions are obsolete. The following is a
  2982. list of functions which shouldn't be used anymore:
  2983. \begin{description}
  2984. \item [Seg()]: Returned the segment of a memory address. Since segments have
  2985. no more meaning, zero is returned in the \fpc run-time library implementation of
  2986. \var{Seg}.
  2987. \item [Ofs()]: Returned the offset of a memory address. Since segments have
  2988. no more meaning, the complete address is returned in the \fpc implementation
  2989. of this function. This has as a consequence that the return type is
  2990. \var{longint} or \var{int64} instead of \var{Word}.
  2991. \item [Cseg(), Dseg()]: Returned, respectively, the code and data segments
  2992. of your program. This returns zero in the \fpc implementation of the
  2993. system unit, since both code and data are in the same memory space.
  2994. \item [Ptr]: Accepted a segment and offset from an address, and would return
  2995. a pointer to this address. This has been changed in the run-time library, it
  2996. now simply returns the offset.
  2997. \item [memw and mem]: These arrays gave access to the \dos memory. \fpc
  2998. supports them on the go32v2 platform, they are mapped into \dos memory
  2999. space. You need the \file{go32} unit for this. On other platforms, they are
  3000. {\em not} supported
  3001. \end{description}
  3002. You shouldn't use these functions, since they are very non-portable, they're
  3003. specific to \dos and the 80x86 processor. The \fpc compiler is designed to be
  3004. portable to other platforms, so you should keep your code as portable as
  3005. possible, and not system specific. That is, unless you're writing some driver
  3006. units, of course.
  3007. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3008. % Data formats
  3009. \section{Data formats}
  3010. \label{se:Dataformats}
  3011. This section gives information on the storage space occupied by
  3012. the different possible types in \fpc. Information on internal
  3013. alignment will also be given.
  3014. \subsection{integer types}
  3015. The storage size of the default integer types are given in
  3016. \refref. In the case of user defined-types, the storage space
  3017. occupied depends on the bounds of the type:
  3018. \begin{itemize}
  3019. \item If both bounds are within range -128..127, the variable
  3020. is stored as a shortint (signed 8-bit quantity).
  3021. \item If both bounds are within the range 0..255, the variable
  3022. is stored as a byte (unsigned 8-bit quantity).
  3023. \item If both bounds are within the range -32768..32767, the variable
  3024. is stored as a smallint (signed 16-bit quantity).
  3025. \item If both bounds are within the range 0..65535, the variable
  3026. is stored as a word (unsigned 16-bit quantity)
  3027. \item If both bounds are within the range 0..4294967295, the
  3028. variable is stored as a longword (unsigned 32-bit quantity).
  3029. \item Otherwise the variable is stored as a longint (signed
  3030. 32-bit quantity).
  3031. \end{itemize}
  3032. \subsection{char types}
  3033. A \var{char}, or a subrange of the char type is stored
  3034. as a byte.
  3035. \subsection{boolean types}
  3036. The \var{boolean} type is stored as a byte and can take
  3037. a value of \var{true} or \var{false}.
  3038. A \var{ByteBool} is stored as a byte, a \var{WordBool}
  3039. type is stored as a word, and a \var{longbool} is stored
  3040. as a longint.
  3041. \subsection{enumeration types}
  3042. By default all enumerations are stored as a
  3043. longword (4 bytes), which is equivalent to specifying
  3044. the \var{\{\$Z4\}}, \var{\{\$PACKENUM 4\}} or
  3045. \var{\{\$PACKENUM DEFAULT\}} switches.
  3046. This default behavior can be changed by compiler switches,
  3047. and by the compiler mode.
  3048. In the \var{tp} compiler mode, or while the \var{\{\$Z1\}} or
  3049. \var{\{\$PACKENUM 1\}} switches are in effect, the storage
  3050. space used is shown in \seet{enumstoragetp}.
  3051. \begin{FPCltable}{ll}{Enumeration storage for \var{tp} mode}{enumstoragetp}
  3052. \# Of Elements in Enum. & Storage space used\\ \hline
  3053. 0..255 & byte (1 byte) \\
  3054. 256..65535 & word (2 bytes) \\
  3055. > 65535 & longword (4 bytes) \\
  3056. \hline
  3057. \end{FPCltable}
  3058. When the \var{\{\$Z2\}} or \var{\{\$PACKENUM 2\}}
  3059. switches are in effect, the value is stored on 2
  3060. bytes (word), if the enumeration has less or equal
  3061. then 65535 elements, otherwise, the enumeration
  3062. value is stored as a 4 byte value (longword).
  3063. \subsection{floating point types}
  3064. Floating point type sizes and mapping vary from one
  3065. processor to another. Except for the Intel 80x86
  3066. architecture, the \var{extended} type maps to the IEEE
  3067. double type if a hardware floating point coprocessor
  3068. is present.
  3069. \begin{FPCltable}{lr}{Processor mapping of real type}{RealMapping}
  3070. Processor & Real type mapping\\
  3071. \hline
  3072. Intel 80x86 & \var{double}\\
  3073. Motorola 680x0 (with \{\$E-\} switch) & \var{double}\\
  3074. Motorola 680x0 (with \{\$E+\} switch) & \var{single}\\
  3075. \hline
  3076. \end{FPCltable}
  3077. Floating point types have a storage binary format divided
  3078. into three distinct fields : the mantissa, the exponent
  3079. and the sign bit which stores the sign of the floating
  3080. point value.
  3081. \subsubsection{single}
  3082. The \var{single} type occupies 4 bytes of storage space,
  3083. and its memory structure is the same as the IEEE-754 single
  3084. type. This type is the only type which is guaranteed to
  3085. be available on all platforms (either emulated via software
  3086. or directly via hardware).
  3087. The memory format of the \var{single} format looks like
  3088. what is shown in \seefig{single}.
  3089. \FPCpic{The single format}{}{single}
  3090. \subsubsection{double}
  3091. The \var{double} type occupies 8 bytes of storage space,
  3092. and its memory structure is the same as the IEEE-754 double
  3093. type.
  3094. The memory format of the \var{double} format looks like
  3095. like what is shown in \seefig{double}.
  3096. \FPCpic{The double format}{}{double}
  3097. On processors which do not support co-processor operations (and which have
  3098. the \{\$E+\} switch), the \var{double} type does not exist.
  3099. \subsubsection{extended}
  3100. For Intel 80x86 processors, the \var{extended} type has
  3101. takes up 10 bytes of memory space. For more information
  3102. on the extended type consult the Intel Programmer's reference.
  3103. For all other processors which support floating point operations,
  3104. the \var{extended} type is a nickname for the type which supports
  3105. the most precision, this is usually the \var{double} type.
  3106. On processors which do not support co-processor operations (and which have
  3107. the \{\$E+\} switch), the \var{extended} type usually maps to the
  3108. \var{single} type.
  3109. \subsubsection{comp}
  3110. For Intel 80x86 processors, the \var{comp} type contains
  3111. a 63-bit integral value, and a sign bit (in the MSB position).
  3112. The \var{comp} type takes up 8 bytes of storage space.
  3113. On other processors, the \var{comp} type is not supported.
  3114. \subsubsection{real}
  3115. Contrary to Turbo Pascal, where the \var{real} type had
  3116. a special internal format, under \fpc the \var{real} type
  3117. simply maps to one of the other real types. It maps to the
  3118. \var{double} type on processors which support floating
  3119. point operations, while it maps to the \var{single} type
  3120. on processors which do not support floating point operations
  3121. in hardware. See \seet{RealMapping} for more information
  3122. on this.
  3123. \subsection{pointer types}
  3124. A \var{pointer} type is stored as a longword (unsigned 32-bit value) on
  3125. 32-bit processors, and is stored as a 64-bit unsigned value\footnote{this
  3126. is actually the \var{qword} type, which is not supported in \fpc v1.0}
  3127. on 64-bit processors.
  3128. \subsection{string types}
  3129. \subsubsection{ansistring types}
  3130. The ansistring is a dynamically allocated string which
  3131. has no length limitation. When the string is no longer
  3132. being referenced (its reference count reaches zero),
  3133. its memory is automatically freed.
  3134. If the ansistring is a constant, then its reference
  3135. count will be equal to -1, indicating that it should
  3136. never be freed. The structure in memory for an
  3137. ansistring is shown in \seet{ansistrings}.
  3138. \begin{FPCltable}{rl}{AnsiString memory structure (32-bit model)}{ansistrings}
  3139. Offset & Contains \\ \hline
  3140. -12 & Longint with maximum string size. \\
  3141. -8 & Longint with actual string size.\\
  3142. -4 & Longint with reference count.\\
  3143. 0 & Actual array of \var{char}, null-terminated. \\ \hline
  3144. \end{FPCltable}
  3145. \subsubsection{shortstring types}
  3146. A shortstring occupies as many bytes as its maximum length plus one.
  3147. The first byte contains the current dynamic length of the string. The
  3148. following bytes contain the actual characters (of type \var{char})
  3149. of the string. The maximum size of a short string is the length
  3150. byte followed by 255 characters.
  3151. \subsubsection{widestring types}
  3152. The widestring (composed of unicode characters) is not supported
  3153. in \fpc v1.0.
  3154. \subsection{set types}
  3155. A set is stored as an array of bits, where each bit indicates
  3156. if the element is in the set or excluded from the set. The maximum
  3157. number of elements in a set is 256.
  3158. If a set has less than 32 elements, it is coded as an unsigned
  3159. 32-bit value. Otherwise it is coded as a 8 element array of
  3160. 32-bit unsigned values (longword) (hence a size of 256 bytes).
  3161. The longword number of a specific element \var{E} is given by :
  3162. \begin{verbatim}
  3163. LongwordNumber = (E div 32);
  3164. \end{verbatim}
  3165. and the bit number within that 32-bit value is given by:
  3166. \begin{verbatim}
  3167. BitNumber = (E mod 32);
  3168. \end{verbatim}
  3169. \subsection{array types}
  3170. An array is stored as a contiguous sequence of variables
  3171. of the components of the array. The components with the
  3172. lowest indexes are stored first in memory. No alignment
  3173. is done between each element of the array. A multi-dimensional
  3174. array is stored with the rightmost dimension increasing first.
  3175. \subsection{record types}
  3176. Each field of a record are stored in a contigous sequence
  3177. of variables, where the first field is stored at the
  3178. lowest address in memory. In case of variant fields in
  3179. a record, each variant starts at the same address in
  3180. memory. Fields of record are usually aligned, unless
  3181. the \var{packed} directive is specified when declaring
  3182. the record type. For more information on field alignment,
  3183. consult \sees{StructuredAlignment}.
  3184. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3185. % memory storage of Objects
  3186. \subsection{object types}
  3187. \label{subse:ObjMemory}
  3188. Objects are stored in memory just as ordinary records with an extra field:
  3189. a pointer to the Virtual Method Table (VMT). This field is stored first, and
  3190. all fields in the object are stored in the order they are declared (with possible
  3191. alignment of field addresses, unless the object was declared as being \var{packed}).
  3192. This field is initialized by the call to the object's \var{Constructor} method.
  3193. If the \var{new} operator was used to call the constructor, the data fields
  3194. of the object will be stored in heap memory, otherwise they will directly
  3195. be stored in the data section of the final executable.
  3196. If an object doesn't have virtual methods, no pointer to a VMT is inserted.
  3197. The memory allocated looks as in \seet{ObjMem}.
  3198. \begin{FPCltable}{ll}{Object memory layout (32-bit model)}{ObjMem} \hline
  3199. Offset & What \\ \hline
  3200. +0 & Pointer to VMT (optional). \\
  3201. +4 & Data. All fields in the order they've been declared. \\
  3202. \dots & \\
  3203. \hline
  3204. \end{FPCltable}
  3205. The Virtual Method Table (VMT) for each object type consists of 2 check
  3206. fields (containing the size of the data), a pointer to the object's ancestor's
  3207. VMT (\var{Nil} if there is no ancestor), and then the pointers to all virtual
  3208. methods. The VMT layout is illustrated in \seet{ObjVMTMem}. The VMT is
  3209. constructed by the compiler.
  3210. \begin{FPCltable}{ll}{Object Virtual Method Table memory layout (32-bit model)}{ObjVMTMem} \hline
  3211. Offset & What \\ \hline
  3212. +0 & Size of object type data \\
  3213. +4 & Minus the size of object type data. Enables determining of valid VMT
  3214. pointers. \\
  3215. +8 & Pointer to ancestor VMT, \var{Nil} if no ancestor available.\\
  3216. +12 & Pointers to the virtual methods. \\
  3217. \dots & \\
  3218. \hline
  3219. \end{FPCltable}
  3220. \subsection{class types}
  3221. Just like objects, classes are stored in memory just as ordinary records
  3222. with an extra field: a pointer to the Virtual Method Table (VMT).
  3223. This field is stored first, and all fields in the class are stored in
  3224. the order they are declared.
  3225. Contrary to objects, all data fields of a class are always stored
  3226. in heap memory.
  3227. The memory allocated looks as in \seet{ClassMem}.
  3228. \begin{FPCltable}{ll}{Class memory layout (32-bit model)}{ClassMem} \hline
  3229. Offset & What \\ \hline
  3230. +0 & Pointer to VMT. \\
  3231. +4 & Data. All fields in the order they've been declared. \\
  3232. \dots & \\
  3233. \hline
  3234. \end{FPCltable}
  3235. The Virtual Method Table (VMT) of each class consists of several fields,
  3236. which are used for runtime type information. The VMT layout is illustrated
  3237. in \seet{ClassVMTMem}. The VMT is constructed by the compiler.
  3238. \begin{FPCltable}{ll}{Class Virtual Method Table memory layout (32-bit model)}{ClassVMTMem} \hline
  3239. Offset & What \\ \hline
  3240. +0 & Size of object type data \\
  3241. +4 & Minus the size of object type data. Enables determining of valid VMT
  3242. pointers. \\
  3243. +8 & Pointer to ancestor VMT, \var{Nil} if no ancestor available.\\
  3244. +12 & Pointer to the class name (stored as a \var{shortstring}). \\
  3245. +16 & Pointer to the dynamic method table (using \var{message} with integers).\\
  3246. +20 & Pointer to the method definition table. \\
  3247. +24 & Pointer to the field definition table. \\
  3248. +28 & Pointer to type information table. \\
  3249. +32 & Pointer to instance initialization table. \\
  3250. +36 & Reserved.\\
  3251. +40 & Pointer to the interface table. \\
  3252. +44 & Pointer to the dynamic method table (using \var{message} with strings).\\
  3253. +48 & Pointer to the \var{Destroy} destructor. \\
  3254. +52 & Pointer to the \var{NewInstance} method. \\
  3255. +56 & Pointer to the \var{FreeInstance} method. \\
  3256. +60 & Pointer to the \var{SafeCallException} method. \\
  3257. +64 & Pointer to the \var{DefaultHandler} method. \\
  3258. +68 & Pointer to the \var{AfterConstruction} method. \\
  3259. +72 & Pointer to the \var{BeforeDestruction} method. \\
  3260. +76 & Pointer to the \var{DefaultHandlerStr} method. \\
  3261. +80 & Pointers to other virtual methods. \\
  3262. \dots & \\
  3263. \hline
  3264. \end{FPCltable}
  3265. \subsection{file types}
  3266. File types are represented as records. Typed files and untyped
  3267. files are represented as a fixed record:
  3268. \begin{verbatim}
  3269. filerec = packed record
  3270. handle : longint;
  3271. mode : longint;
  3272. recsize : longint;
  3273. _private : array[1..32] of byte;
  3274. userdata : array[1..16] of byte;
  3275. name : array[0..255] of char;
  3276. End;
  3277. \end{verbatim}
  3278. Text files are described using the following record:
  3279. \begin{verbatim}
  3280. TextBuf = array[0..255] of char;
  3281. textrec = packed record
  3282. handle : longint;
  3283. mode : longint;
  3284. bufsize : longint;
  3285. _private : longint;
  3286. bufpos : longint;
  3287. bufend : longint;
  3288. bufptr : ^textbuf;
  3289. openfunc : pointer;
  3290. inoutfunc : pointer;
  3291. flushfunc : pointer;
  3292. closefunc : pointer;
  3293. userdata : array[1..16] of byte;
  3294. name : array[0..255] of char;
  3295. buffer : textbuf;
  3296. End;
  3297. \end{verbatim}
  3298. \begin{description}
  3299. \item[handle] The handle field returns the file handle (if the file is
  3300. opened), as returned by the operating system.
  3301. \item[mode] The mode field can take one of several values. When it is
  3302. \var{fmclosed}, then the file is closed, and the \var{handle} field is invalid.
  3303. When the value is equal to \var{fminput}, it indicates that the file is
  3304. opened for read only access. \var{fmoutput} indicates write only access,
  3305. and the \var{fminout} indicates read-write access to the file.
  3306. \item[name] The \var{name} field is a null terminated character string representing
  3307. the name of the file.
  3308. \item[userdata] The \var{userdata} field is never used by \fpc, and can
  3309. be used for special purposes by software developpers.
  3310. \end{description}
  3311. \subsection{procedural types}
  3312. A procedural type is stored as a generic pointer, which stores
  3313. the address of the routine.
  3314. A procedural type to a normal procedure or function is stored as a generic pointer,
  3315. which stores the address of the entry point of the routine.
  3316. In the case of a method procedural type, the storage consists of two pointers,
  3317. the first being a pointer to the entry point of the method, and the second
  3318. one being a pointer to \var{self} (the object instance).
  3319. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3320. % The alignment of data elements
  3321. \section{Data alignment}
  3322. \label{se:Alignment}
  3323. \subsection{Typed constants and variable alignment}
  3324. All static data (variables and typed constants) which are greater than
  3325. a byte are usually aligned on a power of two boundary. This alignment
  3326. applies only to the start address of the variables, and not the alignment
  3327. of fields within structures or objects for example. For more information
  3328. on structured alignment, \sees{StructuredAlignment}. The alignment
  3329. is similar across the different target processors. \footnote{The Intel
  3330. 80x86 version does not align data in the case of constant strings,
  3331. constant sets, constant floating point values amd global variables.
  3332. This will be fixed in the version 1.1 release.}
  3333. \begin{FPCltable}{lccc}{Data alignment}{DataAlignment}
  3334. \hline
  3335. Data size (bytes) & Alignment (small size) & Alignment (fast)\\
  3336. 1 & 1 & 1\\
  3337. 2-3 & 2 & 2\\
  3338. 4-7 & 2 & 4\\
  3339. 8+ & 2 & 4\\
  3340. \hline
  3341. \end{FPCltable}
  3342. The alignment columns indicates the address alignment of the variable,
  3343. i.e the start address of the variable will be aligned on that boundary.
  3344. The small size alignment is valid when the code generated should be
  3345. optimized for size (\var{-Og} compiler option) and not speed, otherwise and
  3346. by default, the fast alignment is used to align the data.
  3347. \subsection{Structured types alignment}
  3348. \label{se:StructuredAlignment}
  3349. By default all elements in a structure are aligned to a 2 byte boundary,
  3350. unless the \var{\$PACKRECORDS} directive or \var{packed} modifier is used
  3351. to align the data in another way. For example a \var{record} or \var{object}
  3352. having a 1 byte element, will have its size rounded up to 2, so the size of
  3353. the structure will actually be 2 bytes.
  3354. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3355. % The heap
  3356. \section{The heap}
  3357. \label{se:Heap}
  3358. The heap is used to store all dynamic variables, and to store class
  3359. instances. The interface to the heap is the same as in Turbo Pascal,
  3360. although the effects are maybe not the same. On top of that, the \fpc
  3361. run-time library has some extra possibilities, not available in Turbo
  3362. Pascal. These extra possibilities are explained in the next subsections.
  3363. % The heap algorithm
  3364. \subsection{Heap allocation strategy}
  3365. The heap is a memory structure which is organized as a stack. The heap
  3366. bottom is stored in the variable \var{HeapOrg}. Initially the heap
  3367. pointer (\var{HeapPtr}) points to the bottom of the heap. When a
  3368. variable is allocated on the heap, \var{HeapPtr} is incremented by the
  3369. size of the allocated memory block. This has the effect of stacking
  3370. dynamic variables on top of each other.
  3371. Each time a block is allocated, its size is normalized to have
  3372. a granularity of 16 bytes.
  3373. When \var{Dispose} or \var{FreeMem} is called to dispose of a
  3374. memory block which is not on the top of the heap, the heap becomes
  3375. fragmented. The deallocation routines also add the freed blocks to
  3376. the \var{freelist} which is actually a linked list of free blocks.
  3377. Furthermore, if the deallocated block was less then 8K in size, the
  3378. free list cache is also updated.
  3379. The free list cache is actually a cache of free heap blocks which
  3380. have specific lengths (the adjusted block size divided by 16 gives the
  3381. index into the free list cache table). It is faster to access then
  3382. searching through the entire \var{freelist}.
  3383. The format of an entry in the \var{freelist} is as follows:
  3384. \begin{verbatim}
  3385. PFreeRecord = ^TFreeRecord;
  3386. TFreeRecord = record
  3387. Size : longint;
  3388. Next : PFreeRecord;
  3389. Prev : PFreeRecord;
  3390. end;
  3391. \end{verbatim}
  3392. The \var{Next} field points to the next free block, while
  3393. the \var{Prev} field points to the previous free block.
  3394. The algorithm for allocating memory is as follows:
  3395. \begin{enumerate}
  3396. \item The size of the block to allocate is adjusted to a 16 byte granularity.
  3397. \item The cached free list is searched to find a free block of the specified
  3398. size or bigger size, if so it is allocated and the routine exits.
  3399. \item The \var{freelist} is searched to find a free block of the specified size
  3400. or of bigger size, if so it is allocated and the routine exits.
  3401. \item If not found in the \var{freelist} the heap is grown to allocate the
  3402. specified memory, and the routine exits.
  3403. \item If the heap cannot be grown internally anymore and if \var{heaperror} is set
  3404. accordingly, it calls the heap error handler. If there is no heap error handler
  3405. installed, the runtime library generates a runtime error 203.
  3406. \end{enumerate}
  3407. % Error handler routinr
  3408. \subsection{The HeapError variable}
  3409. The heap error permits developpers to install a heap error hook which
  3410. is called each time an allocation cannot be completed by the default
  3411. heap manager. \var{HeapError} is a pointer that points to a function
  3412. with the following prototype:
  3413. \begin{verbatim}
  3414. function HeapFunc(size : longint): integer;
  3415. \end{verbatim}
  3416. The \var{size} parameter indicates the size of the block which could
  3417. not be allocated. Depending on the success, the error handler routine
  3418. should return a value which indicates what the default heap manager
  3419. should do thereafter (cf. \seet{Heaperrorresult}).
  3420. \begin{FPCltable}{|c|l|}{Heap error result}{Heaperrorresult}
  3421. \hline
  3422. Value returned & Memory manager action \\ \hline
  3423. 0 & Generates a runtime error 203 \\
  3424. 1 & \var{GetMem},\var{ReallocMem} and \var{New} returns \var{nil} \\
  3425. 2 & Try allocating the memory block once again \\
  3426. \hline
  3427. \end{FPCltable}
  3428. % The heap grows
  3429. \subsection{The heap grows}
  3430. By default, \var{HeapError} points to the \var{GrowHeap} function,
  3431. which tries to increase the heap.
  3432. The \var{GrowHeap} function issues a system call to try to increase the size of the
  3433. memory available to your program. It first tries to increase memory in a 256Kb
  3434. chunk if the size to allocate is less than 256Kb, or 1024K otherwise.
  3435. If this fails, it tries to increase the heap by the amount you requested
  3436. from the heap.
  3437. If the call to \var{GrowHeap} was successful, then the needed memory will be
  3438. allocated.
  3439. If the call to \var{GrowHeap} fails, the value returned depends on the
  3440. value of the \var{ReturnNilIfGrowHeapFails} global variable. This is summarized
  3441. in \seet{growheapnil}.
  3442. \begin{FPCltable}{|c|l|}{ReturnNilIfGrowHeapFails value}{growheapnil}
  3443. \hline
  3444. ReturnNilGrowHeapFails & Default memory \\
  3445. value & manager action \\
  3446. \hline
  3447. FALSE & (The default) Runtime error 203 generated \\
  3448. TRUE & \var{GetMem}, \var{ReallocMem} and \var{New} returns \var{nil} \\
  3449. \hline
  3450. \end{FPCltable}
  3451. \var{ReturnNilIfGrowHeapFails} can be set to change the behavior of
  3452. the default memory manager error handler.
  3453. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3454. % Debugging the heap
  3455. \subsection{Debugging the heap}
  3456. \fpc provides a unit that allows you to trace allocation and deallocation
  3457. of heap memory: \file{heaptrc}.
  3458. If you specify the \var{-gh} switch on the command-line, or if you include
  3459. \var{heaptrc} as the first unit in your uses clause, the memory manager
  3460. will trace what is allocated and deallocated, and on exit of your program,
  3461. a summary will be sent to standard output.
  3462. More information on using the \var{heaptrc} mechanism can be found in the
  3463. \userref and \unitsref.
  3464. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3465. % Writing your own memory manager.
  3466. \subsection{Writing your own memory manager}
  3467. \fpc allows you to write and use your own memory manager. The standard
  3468. functions \var{GetMem}, \var{FreeMem}, \var{ReallocMem} and \var{Maxavail}
  3469. use a special record in the \file{system} unit to do the actual memory management.
  3470. The \file{system} unit initializes this record with the \file{system} unit's own memory
  3471. manager, but you can read and set this record using the
  3472. \var{GetMemoryManager} and \var{SetMemoryManager} calls:
  3473. \begin{verbatim}
  3474. procedure GetMemoryManager(var MemMgr: TMemoryManager);
  3475. procedure SetMemoryManager(const MemMgr: TMemoryManager);
  3476. \end{verbatim}
  3477. the \var{TMemoryManager} record is defined as follows:
  3478. \begin{verbatim}
  3479. TMemoryManager = record
  3480. Getmem : Function(Size:Longint):Pointer;
  3481. Freemem : Function(var p:pointer):Longint;
  3482. FreememSize : Function(var p:pointer;Size:Longint):Longint;
  3483. AllocMem : Function(Size:longint):Pointer;
  3484. ReAllocMem : Function(var p:pointer;Size:longint):Pointer;
  3485. MemSize : function(p:pointer):Longint;
  3486. MemAvail : Function:Longint;
  3487. MaxAvail : Function:Longint;
  3488. HeapSize : Function:Longint;
  3489. end;
  3490. \end{verbatim}
  3491. As you can see, the elements of this record are procedural variables.
  3492. The \file{system} unit does nothing but call these various variables when you
  3493. allocate or deallocate memory.
  3494. Each of these functions corresponds to the corresponding call in the \file{system}
  3495. unit. We'll describe each one of them:
  3496. \begin{description}
  3497. \item[Getmem] This function allocates a new block on the heap. The block
  3498. should be \var{Size} bytes long. The return value is a pointer to the newly
  3499. allocated block.
  3500. \item[Freemem] should release a previously allocated block. The pointer
  3501. \var{P} points to a previously allocated block. The Memory manager should
  3502. implement a mechanism to determine what the size of the memory block is
  3503. \footnote{By storing it's size at a negative offset for instance.} The
  3504. return value is optional, and can be used to return the size of the freed
  3505. memory.
  3506. \item[FreememSize] This function should release the memory pointed to by
  3507. \var{P}. The argument \var{Size} is the expected size of the memory block
  3508. pointed to by P. This should be disregarded, but can be used to check the
  3509. behaviour of the program.
  3510. \item[AllocMem] Is the same as getmem, only the allocated memory should
  3511. be filled with zeroes before the call returns.
  3512. \item[ReAllocMem] Should allocate a memory block \var{Size} bytes large,
  3513. and should fill it with the contents of the memory block pointed to by
  3514. \var{P}, truncating this to the new size of needed. After that, the memory
  3515. pointed to by P may be deallocated. The return value is a pointer to the
  3516. new memory block. Note that \var{P} may be \var{Nil}, in which case the
  3517. behaviour is equivalent to \var{GetMem}.
  3518. \item[MemSize] should return the total amount of memory available for
  3519. allocation. This function may return zero if the memory manager does not
  3520. allow to determine this information.
  3521. \item[MaxAvail] should return the size of the largest block of memory that
  3522. is still available for allocation. This function may return zero if the
  3523. memory manager does not allow to determine this information.
  3524. \item[HeapSize] should return the total size of the heap. This may be zero
  3525. is the memory manager does not allow to determine this information.
  3526. \end{description}
  3527. To implement your own memory manager, it is sufficient to construct such a
  3528. record and to issue a call to \var{SetMemoryManager}.
  3529. To avoid conflicts with the system memory manager, setting the memory
  3530. manager should happen as soon as possible in the initialization of your
  3531. program, i.e. before any call to \var{getmem} is processed.
  3532. This means in practice that the unit implementing the memory manager should
  3533. be the first in the \var{uses} clause of your program or library, since it
  3534. will then be initialized before all other units (except of the \file{system} unit)
  3535. This also means that it is not possible to use the \file{heaptrc} unit in
  3536. combination with a custom memory manager, since the \file{heaptrc} unit uses
  3537. the system memory manager to do all it's allocation. Putting the
  3538. \file{heaptrc} unit after the unit implementing the memory manager would
  3539. overwrite the memory manager record installed by the custom memory manager,
  3540. and vice versa.
  3541. The following unit shows a straightforward implementation of a custom
  3542. memory manager using the memory manager of the \var{C} library. It is
  3543. distributed as a package with \fpc.
  3544. \begin{verbatim}
  3545. unit cmem;
  3546. {$mode objfpc}
  3547. interface
  3548. Function Malloc (Size : Longint) : Pointer;cdecl;
  3549. external 'c' name 'malloc';
  3550. Procedure Free (P : pointer); cdecl; external 'c' name 'free';
  3551. Procedure FreeMem (P : Pointer); cdecl; external 'c' name 'free';
  3552. function ReAlloc (P : Pointer; Size : longint) : pointer; cdecl;
  3553. external 'c' name 'realloc';
  3554. Function CAlloc (unitSize,UnitCount : Longint) : pointer;cdecl;
  3555. external 'c' name 'calloc';
  3556. implementation
  3557. Function CGetMem (Size : Longint) : Pointer;
  3558. begin
  3559. result:=Malloc(Size);
  3560. end;
  3561. Function CFreeMem (Var P : pointer) : Longint;
  3562. begin
  3563. Free(P);
  3564. Result:=0;
  3565. end;
  3566. Function CFreeMemSize(var p:pointer;Size:Longint):Longint;
  3567. begin
  3568. Result:=CFreeMem(P);
  3569. end;
  3570. Function CAllocMem(Size : Longint) : Pointer;
  3571. begin
  3572. Result:=calloc(Size,1);
  3573. end;
  3574. Function CReAllocMem (var p:pointer;Size:longint):Pointer;
  3575. begin
  3576. Result:=realloc(p,size);
  3577. end;
  3578. Function CMemSize (p:pointer): Longint;
  3579. begin
  3580. Result:=0;
  3581. end;
  3582. Function CMemAvail : Longint;
  3583. begin
  3584. Result:=0;
  3585. end;
  3586. Function CMaxAvail: Longint;
  3587. begin
  3588. Result:=0;
  3589. end;
  3590. Function CHeapSize : Longint;
  3591. begin
  3592. Result:=0;
  3593. end;
  3594. Const
  3595. CMemoryManager : TMemoryManager =
  3596. (
  3597. GetMem : CGetmem;
  3598. FreeMem : CFreeMem;
  3599. FreememSize : CFreememSize;
  3600. AllocMem : CAllocMem;
  3601. ReallocMem : CReAllocMem;
  3602. MemSize : CMemSize;
  3603. MemAvail : CMemAvail;
  3604. MaxAvail : MaxAvail;
  3605. HeapSize : CHeapSize;
  3606. );
  3607. Var
  3608. OldMemoryManager : TMemoryManager;
  3609. Initialization
  3610. GetMemoryManager (OldMemoryManager);
  3611. SetMemoryManager (CmemoryManager);
  3612. Finalization
  3613. SetMemoryManager (OldMemoryManager);
  3614. end.
  3615. \end{verbatim}
  3616. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3617. % Accessing DOS memory under the GO32 extender
  3618. \section{Using \dos memory under the Go32 extender}
  3619. \label{se:AccessingDosMemory}
  3620. Because \fpc for \dos is a 32 bit compiler, and uses a \dos extender, accessing DOS
  3621. memory isn't trivial. What follows is an attempt to an explanation of how to
  3622. access and use \dos or real mode memory\footnote{Thanks for the explanation to
  3623. Thomas Schatzl (E-mail: \var{tom\_at\[email protected]})}.
  3624. In {\em Proteced Mode}, memory is accessed through {\em Selectors} and
  3625. {\em Offsets}. You can think of Selectors as the protected mode
  3626. equivalents of segments.
  3627. In \fpc, a pointer is an offset into the \var{DS} selector, which points to
  3628. the Data of your program.
  3629. To access the (real mode) \dos memory, somehow you need a selector that
  3630. points to the \dos memory.
  3631. The \file{go32} unit provides you with such a selector: The
  3632. \var{DosMemSelector} variable, as it is conveniently called.
  3633. You can also allocate memory in \dos's memory space, using the
  3634. \var{global\_dos\_alloc} function of the \file{go32} unit.
  3635. This function will allocate memory in a place where \dos sees it.
  3636. As an example, here is a function that returns memory in real mode \dos and
  3637. returns a selector:offset pair for it.
  3638. \begin{verbatim}
  3639. procedure dosalloc(var selector : word;
  3640. var segment : word;
  3641. size : longint);
  3642. var result : longint;
  3643. begin
  3644. result := global_dos_alloc(size);
  3645. selector := word(result);
  3646. segment := word(result shr 16);
  3647. end;
  3648. \end{verbatim}
  3649. (You need to free this memory using the \var{global\_dos\_free} function.)
  3650. You can access any place in memory using a selector. You can get a selector
  3651. using the \var{allocate\_ldt\_descriptor} function, and then let this selector
  3652. point to the physical memory you want using the
  3653. \var{set\_segment\_base\_address} function, and set its length using
  3654. \var{set\_segment\_limit} function.
  3655. You can manipulate the memory pointed to by the selector using the functions
  3656. of the GO32 unit. For instance with the \var{seg\_fillchar} function.
  3657. After using the selector, you must free it again using the
  3658. \var{free\_ldt\_selector} function.
  3659. More information on all this can be found in the \unitsref, the chapter on
  3660. the \file{go32} unit.
  3661. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3662. % Resource strings
  3663. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3664. \chapter{Resource strings}
  3665. \label{resourcestrings}
  3666. \section{Introduction}
  3667. Resource strings primarily exist to make internationalization of
  3668. applications easier, by introducing a language construct that provides
  3669. a uniform way of handling constant strings.
  3670. Most applications communicate with the user through some messages on the
  3671. graphical screen or console. Storing these messages in special constants
  3672. allows to store them in a uniform way in separate files, which can be used
  3673. for translation. A programmers interface exists to manipulate the actual
  3674. values of the constant strings at runtime, and a utility tool comes with the
  3675. Free Pascal compiler to convert the resource string files to whatever format
  3676. is wanted by the programmer. Both these things are discussed in the
  3677. following sections.
  3678. \section{The resource string file}
  3679. When a unit is compiled that contains a \var{resourcestring} section,
  3680. the compiler does 2 things:
  3681. \begin{enumerate}
  3682. \item It generates a table that contains the value of the strings as it
  3683. is declared in the sources.
  3684. \item It generates a {\em resource string file} that contains the names
  3685. of all strings, together with their declared values.
  3686. \end{enumerate}
  3687. This approach has 2 advantages: first of all, the value of the string is
  3688. always present in the program. If the programmer doesn't care to translate
  3689. the strings, the default values are always present in the binary. This also
  3690. avoids having to provide a file containing the strings. Secondly, having all
  3691. strings together in a compiler generated file ensures that all strings are
  3692. together (you can have multiple resourcestring sections in 1 unit or program)
  3693. and having this file in a fixed format, allows the programmer to choose his
  3694. way of internationalization.
  3695. For each unit that is compiled and that contains a resourcestring section,
  3696. the compiler generates a file that has the name of the unit, and an
  3697. extension \file{.rst}. The format of this file is as follows:
  3698. \begin{enumerate}
  3699. \item An empty line.
  3700. \item A line starting with a hash sign (\var{\#}) and the hash value of the
  3701. string, preceded by the text \var{hash value =}.
  3702. \item A third line, containing the name of the resource string in the format
  3703. \var{unitname.constantname}, all lowercase, followed by an equal sign, and
  3704. the string value, in a format equal to the pascal representation of this
  3705. string. The line may be continued on the next line, in that case it reads as
  3706. a pascal string expression with a plus sign in it.
  3707. \item Another empty line.
  3708. \end{enumerate}
  3709. If the unit contains no \var{resourcestring} section, no file is generated.
  3710. For example, the following unit:
  3711. \begin{verbatim}
  3712. unit rsdemo;
  3713. {$mode delphi}
  3714. {$H+}
  3715. interface
  3716. resourcestring
  3717. First = 'First';
  3718. Second = 'A Second very long string that should cover more than 1 line';
  3719. implementation
  3720. end.
  3721. \end{verbatim}
  3722. Will result in the following resource string file:
  3723. \begin{verbatim}
  3724. # hash value = 5048740
  3725. rsdemo.first='First'
  3726. # hash value = 171989989
  3727. rsdemo.second='A Second very long string that should cover more than 1 li'+
  3728. 'ne'
  3729. \end{verbatim}
  3730. The hash value is calculated with the function \var{Hash}. It is present in
  3731. the \file{objpas} unit. The value is the same value that the GNU gettext
  3732. mechanism uses. It is in no way unique, and can only be used to speed up
  3733. searches.
  3734. The \file{rstconv} utility that comes with the \fpc compiler allows to
  3735. manipulate these resource string files. At the moment, it can only be used
  3736. to make a \file{.po} file that can be fed to the GNU \file{msgfmt} program.
  3737. If someone wishes to have another format (Win32 resource files spring to
  3738. mind), one can enhance the \file{rstconv} program so it can generate
  3739. other types of files as well. GNU gettext was chosen because it is available
  3740. on all platforms, and is already widely used in the \var{Unix} and free
  3741. software community. Since the \fpc team doesn't want to restrict the use
  3742. of resource strings, the \file{.rst} format was chosen to provide a neutral
  3743. method, not restricted to any tool.
  3744. If you use resource strings in your units, and you want people to be able to
  3745. translate the strings, you must provide the resource string file. Currently,
  3746. there is no way to extract them from the unit file, though this is in
  3747. principle possible. It is not required to do this, the program can be
  3748. compiled without it, but then the translation of the strings isn't possible.
  3749. \section{Updating the string tables}
  3750. Having compiled a program with resourcestrings is not enough to
  3751. internationalize your program. At run-time, the program must initialize
  3752. the string tables with the correct values for the anguage that the user
  3753. selected. By default no such initialization is performed. All strings
  3754. are initialized with their declared values.
  3755. The \file{objpas} unit provides the mechanism to correctly initialize
  3756. the string tables. There is no need to include this unit in a \var{uses}
  3757. clause, since it is automatically loaded when a program or unit is
  3758. compiled in \var{Delphi} or \var{objfpc} mode. Since this is required
  3759. to use resource strings, the unit is always loaded when needed.
  3760. The resource strings are stored in tables, one per unit, and one for the
  3761. program, if it contains a \var{resourcestring} section as well. Each
  3762. resourcestring is stored with it's name, hash value, default value, and
  3763. the current value, all as \var{AnsiStrings}.
  3764. The objpas unit offers methods to retrieve the number of resourcestring
  3765. tables, the number of strings per table, and the above information for each
  3766. string. It also offers a method to set the current value of the strings.
  3767. Here are the declarations of all the functions:
  3768. \begin{verbatim}
  3769. Function ResourceStringTableCount : Longint;
  3770. Function ResourceStringCount(TableIndex : longint) : longint;
  3771. Function GetResourceStringName(TableIndex,
  3772. StringIndex : Longint) : Ansistring;
  3773. Function GetResourceStringHash(TableIndex,
  3774. StringIndex : Longint) : Longint;
  3775. Function GetResourceStringDefaultValue(TableIndex,
  3776. StringIndex : Longint) : AnsiString;
  3777. Function GetResourceStringCurrentValue(TableIndex,
  3778. StringIndex : Longint) : AnsiString;
  3779. Function SetResourceStringValue(TableIndex,
  3780. StringIndex : longint;
  3781. Value : Ansistring) : Boolean;
  3782. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  3783. \end{verbatim}
  3784. Two other function exist, for convenience only:
  3785. \begin{verbatim}
  3786. Function Hash(S : AnsiString) : longint;
  3787. Procedure ResetResourceTables;
  3788. \end{verbatim}
  3789. Here is a short explanation of what each function does. A more detailed
  3790. explanation of the functions can be found in the \refref.
  3791. \begin{description}
  3792. \item[ResourceStringTableCount] returns the number of resource string tables
  3793. in the program.
  3794. \item[ResourceStringCount] returns the number of resource string entries in
  3795. a given table (tables are denoted by a zero-based index).
  3796. \item[GetResourceStringName] returns the name of a resource string in a
  3797. resource table. This is the name of the unit, a dot (.) and the name of
  3798. the string constant, all in lowercase. The strings are denoted by index,
  3799. also zero-based.
  3800. \item[GetResourceStringHash] returns the hash value of a resource string, as
  3801. calculated by the compiler with the \var{Hash} function.
  3802. \item[GetResourceStringDefaultValue] returns the default value of a resource
  3803. string, i.e. the value that appears in the resource string declaration, and
  3804. that is stored in the binary.
  3805. \item[GetResourceStringCurrentValue] returns the current value of a resource
  3806. string, i.e. the value set by the initialization (the default value), or the
  3807. value set by some previous internationalization routine.
  3808. \item[SetResourceStringValue] sets the current value of a resource string.
  3809. This function must be called to initialize all strings.
  3810. \item[SetResourceStrings] giving this function a callback will cause the
  3811. calback to be called for all resource strings, one by one, and set the value
  3812. of the string to the return value of the callback.
  3813. \end{description}
  3814. Two other functions exist, for convenience only:
  3815. \begin{description}
  3816. \item [Hash] can be used to calculate the hash value of a string. The hash
  3817. value stored in the tables is the result of this function, applied on the
  3818. default value. That value is calculated at compile time by the compiler.
  3819. \item[ResetResourceTables] will reset all the resource strings to their
  3820. default values. It is called by the initialization code of the objpas unit.
  3821. \end{description}
  3822. Given some \var{Translate} function, the following code would initialize
  3823. all resource strings:
  3824. \begin{verbatim}
  3825. Var I,J : Longint;
  3826. S : AnsiString;
  3827. begin
  3828. For I:=0 to ResourceStringTableCount-1 do
  3829. For J:=0 to ResourceStringCount(i)-1 do
  3830. begin
  3831. S:=Translate(GetResourceStringDefaultValue(I,J));
  3832. SetResourceStringValue(I,J,S);
  3833. end;
  3834. end;
  3835. \end{verbatim}
  3836. Other methods are of course possible, and the \var{Translate} function
  3837. can be implemented in a variety of ways.
  3838. \section{GNU gettext}
  3839. The unit \file{gettext} provides a way to internationalize an application
  3840. with the GNU \file{gettext} utilities. This unit is supplied with the
  3841. Free Component Library (FCL). it can be used as follows:
  3842. for a given application, the following steps must be followed:
  3843. \begin{enumerate}
  3844. \item Collect all resource string files and concatenate them together.
  3845. \item Invoke the \file{rstconv} program with the file resulting out of step
  3846. 1, resulting in a single \file{.po} file containing all resource strings of
  3847. the program.
  3848. \item Translate the \file{.po} file of step 2 in all required languages.
  3849. \item Run the \file{msgfmt} formatting program on all the \file{.po} files,
  3850. resulting in a set of \file{.mo} files, which can be distributed with your
  3851. application.
  3852. \item Call the \file{gettext} unit's \var{TranslateReosurceStrings} method,
  3853. giving it a template for the location of the \file{.mo} files, e.g. as in
  3854. \begin{verbatim}
  3855. TranslateResourcestrings('intl/restest.%s.mo');
  3856. \end{verbatim}
  3857. the \var{\%s} specifier will be replaced by the contents of the \var{LANG}
  3858. environment variable. This call should happen at program startup.
  3859. \end{enumerate}
  3860. An example program exists in the FCL sources, in the \file{fcl/tests}
  3861. directory.
  3862. \section{Caveat}
  3863. In principle it is possible to translate all resource strings at any time in
  3864. a running program. However, this change is not communicated to other
  3865. strings; its change is noticed only when a constant string is being used.
  3866. Consider the following example:
  3867. \begin{verbatim}
  3868. Const
  3869. help = 'With a little help of a programmer.';
  3870. Var
  3871. A : AnsiString;
  3872. begin
  3873. { lots of code }
  3874. A:=Help;
  3875. { Again some code}
  3876. TranslateStrings;
  3877. { More code }
  3878. \end{verbatim}
  3879. After the call to \var{TranslateStrings}, the value of \var{A} will remain
  3880. unchanged. This means that the assignment \var{A:=Help} must be executed
  3881. again in order for the change to become visible. This is important,
  3882. especially for GUI programs which have e.g. a menu. In order for the
  3883. change in resource strings to become visible, the new values must be
  3884. reloaded by program code into the menus \dots
  3885. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3886. % Thread programming
  3887. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3888. \chapter{Thread programming}
  3889. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3890. % Introduction
  3891. \section{Introduction}
  3892. \fpc supports thread programming: There is a language construct
  3893. available for thread-local storage (\var{ThreadVar}), and cross-platform
  3894. low-level thread routines are available for those operating systems that
  3895. support threads.
  3896. The compiler must be told to enable threading in a program. This is done
  3897. using the \var{\{\$THREADING\}} directive:
  3898. \begin{verbatim}
  3899. Program MyThreads;
  3900. {$THREADING ON}
  3901. Uses Xyz;
  3902. \end{verbatim}
  3903. The threading directive must appear in the program source code, before the
  3904. uses clause: One of the effects of the directive is that the \file{systhrds}
  3905. unit is inserted in the \var{uses} clause of the program. This unit contains
  3906. all threading routines.
  3907. Although it is not forbidden to do so, it is not recommended to use system-specific
  3908. threading routines: The language support for multithreaded programs will not be
  3909. enabled, meaning that threadvars will not work, the heap manager will be confused
  3910. which may lead to severe program errors.
  3911. The above code only enables language support for threading. The actual threading
  3912. is implemented using a thread manager. On OSes which have built-in support for
  3913. threading (such as \windows), the system thread manager will be used and threading
  3914. is functional. For other OSes (for example, \linux) the threading code resides in
  3915. the C library (it uses pthreads) and must be enabled specifically: this means that
  3916. a thread manager must be used which uses Libc's threading routines to implement
  3917. the necessary threading routines. The system thread manager will raise an exception
  3918. if threads are started.
  3919. For \linux (and other Unixes), the C thread manager can be enabled by inserting the
  3920. \var{cthreads} unit in the program's unit clause. Without this, threading programs
  3921. will give an error when started.
  3922. At a later time, a system thread manager may be implemented which implements threads
  3923. without Libc support.
  3924. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3925. % Programming threads
  3926. \section{Programming threads}
  3927. To start a new thread, the \var{BeginThread} function should be used. It has one
  3928. mandatory argument: the function which will be executed in the new thread. The result
  3929. of the function is the exit result of the thread. The thread function can be passed a
  3930. pointer, which can be used to acces initialization data: The programmer must make sure
  3931. that the data is accessible from the thread and does not go out of scope before the
  3932. thread has accessed it.
  3933. \begin{verbatim}
  3934. Type
  3935. TThreadFunc = function(parameter : pointer) : longint;
  3936. function BeginThread(ThreadFunction: tthreadfunc) : DWord;
  3937. function BeginThread(ThreadFunction: tthreadfunc;
  3938. p: pointer): DWord;
  3939. function BeginThread(ThreadFunction: tthreadfunc;
  3940. p: pointer;
  3941. var ThreadId : THandle) : DWord;
  3942. \end{verbatim}
  3943. If present, the pointer \var{p} will be passed to the thread function
  3944. when it is started (otherwise, \var{Nil} is passed). If \var{ThreadID}
  3945. is present, the ID of the thread will be stored in it.
  3946. The newly started thread will run until the \var{ThreadFunction} exits,
  3947. or until it explicitly calls the \var{EndThread} function:
  3948. \begin{verbatim}
  3949. procedure EndThread(ExitCode : DWord);
  3950. procedure EndThread;
  3951. \end{verbatim}
  3952. The exitcode can be examined by the code which started the thread.
  3953. The following is a small example of how to program a thread:
  3954. \begin{verbatim}
  3955. {$mode objfpc}
  3956. {$threading on}
  3957. uses
  3958. sysutils {$ifdef unix},cthreads{$endif} ;
  3959. const
  3960. threadcount = 100;
  3961. stringlen = 10000;
  3962. var
  3963. finished : longint;
  3964. threadvar
  3965. thri : longint;
  3966. function f(p : pointer) : longint;
  3967. var
  3968. s : ansistring;
  3969. begin
  3970. Writeln('thread ',longint(p),' started');
  3971. thri:=0;
  3972. while (thri<stringlen) do
  3973. begin
  3974. s:=s+'1';
  3975. inc(thri);
  3976. end;
  3977. Writeln('thread ',longint(p),' finished');
  3978. InterLockedIncrement(finished);
  3979. f:=0;
  3980. end;
  3981. var
  3982. i : longint;
  3983. begin
  3984. finished:=0;
  3985. for i:=1 to threadcount do
  3986. BeginThread(@f,pointer(i));
  3987. while finished<threadcount do ;
  3988. Writeln(finished);
  3989. end.
  3990. \end{verbatim}
  3991. The \var{InterLockedIncrement} is a thread-safe version of the standard \var{Inc} function.
  3992. To provide system-independent support for thread programming, some utility functions are
  3993. implemented to manipulate threads. To use these functions the thread ID must have been
  3994. retrieved when the thread was started, because most functions require the ID to identify
  3995. the thread on which they should act:
  3996. \begin{verbatim}
  3997. function SuspendThread(threadHandle: dword): dword;
  3998. function ResumeThread(threadHandle: dword): dword;
  3999. function KillThread(threadHandle: dword): dword;
  4000. function WaitForThreadTerminate(threadHandle: dword;
  4001. TimeoutMs : longint): dword;
  4002. function ThreadSetPriority(threadHandle: dword;
  4003. Prio: longint): boolean;
  4004. function ThreadGetPriority(threadHandle: dword): Integer;
  4005. function GetCurrentThreadId: dword;
  4006. procedure ThreadSwitch;
  4007. \end{verbatim}
  4008. The meaning of these functions should be clear:
  4009. \begin{description}
  4010. \item[SuspendThread] Suspends the execution of the thread.
  4011. \item[ResumeThread] Resumes execution of a suspended thread.
  4012. \item[KillThread] Kills the thread: the thread is removed from memory.
  4013. \item[WaitForThreadTerminate] Waits for the thread to terminate.
  4014. The function returns when the thread has finished executing, or
  4015. when the timeout expired.
  4016. \item[ThreadSetPriority] Sets the execution priority of the thread.
  4017. This call is not always allowed.
  4018. \item[ThreadGetPriority] Returns the current execution priority of the thread.
  4019. \item[GetCurrentThreadId] Returns the ID of the current thread.
  4020. \item[ThreadSwitch] Allows other threads to execute at this point,
  4021. can cause a thread switch, but this is not guaranteed, it depends
  4022. on the OS and the number of processors.
  4023. \end{description}
  4024. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4025. % Critical sections
  4026. \section{Critical sections}
  4027. When programming threads, it is sometimes necessary to avoid concurrent
  4028. access to certain resources, or to avoid having a certain routine
  4029. executed by two threads. This can be done using a Critical Section.
  4030. The FPC heap manager uses critical sections when multithreading is
  4031. enabled.
  4032. The \var{TRTLCriticalSection} type is an Opaque type; it depends on the
  4033. OS on which the code is executed. It should be initialized before it
  4034. is first used, and should be disposed of when it is no longer necessary.
  4035. To protect a piece of code, a call to \var{EnterCriticalSection} should
  4036. be made: When this call returns, it is guaranteed that the current thread
  4037. is the only thread executing the subsequent code. The call may have
  4038. suspended the current thread for an indefinite time to ensure this.
  4039. When the protected code is finished, \var{LeaveCriticalSection} must
  4040. be called: this will enable other threads to start executing the protected
  4041. code. To minimize waiting time for the threads, it is important to keep
  4042. the protected block as small as possible.
  4043. The definition of these calls is as follows:
  4044. \begin{verbatim}
  4045. procedure InitCriticalSection(var cs: TRTLCriticalSection);
  4046. procedure DoneCriticalSection(var cs: TRTLCriticalSection);
  4047. procedure EnterCriticalSection(var cs: TRTLCriticalSection);
  4048. procedure LeaveCriticalSection(var cs: TRTLCriticalSection);
  4049. \end{verbatim}
  4050. The meaning of these calls is again almost obvious:
  4051. \begin{description}
  4052. \item[InitCriticalSection] Initializes a critical section.
  4053. This call must be made before either \var{EnterCrititicalSection}
  4054. or \var{LeaveCriticalSection} is used.
  4055. \item[DoneCriticalSection] Frees the resources associated with a
  4056. critical section. After this call neither \var{EnterCrititicalSection}
  4057. nor \var{LeaveCriticalSection} may be used.
  4058. \item[EnterCriticalSection] When this call returns, the calling
  4059. thread is the only thread running the code between the \var{EnterCriticalSection}
  4060. call and the following \var{LeaveCriticalsection} call.
  4061. \item[LeaveCriticalSection] Signals that the protected code can be executed
  4062. by other threads.
  4063. \end{description}
  4064. Note that the \var{LeaveCriticalsection} call {\em must} be executed.
  4065. Failing to do so will prevent all other threads from executing the code
  4066. in the critical section. It is therefore good practice to enclose the
  4067. critical section in a \var{Try..finally} block. Typically, the code
  4068. will look as follows:
  4069. \begin{verbatim}
  4070. Var
  4071. MyCS : TRTLCriticalSection;
  4072. Procedure CriticalProc;
  4073. begin
  4074. EnterCriticalSection(MyCS);
  4075. Try
  4076. // Protected Code
  4077. Finally
  4078. LeaveCriticalSection(MyCS);
  4079. end;
  4080. end;
  4081. Procedure ThreadProcedure;
  4082. begin
  4083. // Code executed in threads...
  4084. CriticalProc;
  4085. // More Code executed in threads...
  4086. end;
  4087. begin
  4088. InitCriticalSection(MyCS);
  4089. // Code to start threads.
  4090. DoneCriticalSection(MyCS);
  4091. end.
  4092. \end{verbatim}
  4093. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4094. % The Thread Manager
  4095. \section{The Thread Manager}
  4096. Just like the heap is implemented using a heap manager, and widestring
  4097. management is left to a widestring manager, the threads have been
  4098. implemented using a thread manager. This means that there is a record which
  4099. has fields of procedural type for all possible functions used in the thread
  4100. routines. The thread routines use these fields to do the actual work.
  4101. The thread routines install a system thread manager specific for each
  4102. system. On Windows, the normal Windows routines are used to implement the
  4103. functions in the thread manager. On Linux and other unices, the system
  4104. thread manager does nothing: it will generate an error when thread routines
  4105. are used. The rationale is that the routines for thread management are
  4106. located in the C library. Implementing the system thread manager would make
  4107. the RTL dependent on the C library, which is not desirable. To avoid
  4108. dependency on the C library, the Thread Manager is implemented in a separate
  4109. unit (\file{cthreads}). The initialization code of this unit sets the thread
  4110. manager to a thread manager record which uses the C (pthreads) routines.
  4111. The thread manager record can be retrieved and set just as the record for
  4112. the heap manager. The record looks (currently) as follows:
  4113. \begin{verbatim}
  4114. TThreadManager = Record
  4115. InitManager : Function : Boolean;
  4116. DoneManager : Function : Boolean;
  4117. BeginThread : TBeginThreadHandler;
  4118. EndThread : TEndThreadHandler;
  4119. SuspendThread : TThreadHandler;
  4120. ResumeThread : TThreadHandler;
  4121. KillThread : TThreadHandler;
  4122. ThreadSwitch : TThreadSwitchHandler;
  4123. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  4124. ThreadSetPriority : TThreadSetPriorityHandler;
  4125. ThreadGetPriority : TThreadGetPriorityHandler;
  4126. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  4127. InitCriticalSection : TCriticalSectionHandler;
  4128. DoneCriticalSection : TCriticalSectionHandler;
  4129. EnterCriticalSection : TCriticalSectionHandler;
  4130. LeaveCriticalSection : TCriticalSectionHandler;
  4131. InitThreadVar : TInitThreadVarHandler;
  4132. RelocateThreadVar : TRelocateThreadVarHandler;
  4133. AllocateThreadVars : TAllocateThreadVarsHandler;
  4134. ReleaseThreadVars : TReleaseThreadVarsHandler;
  4135. end;
  4136. \end{verbatim}
  4137. The meaning of most of these functions should be obvious from the descriptions in
  4138. previous sections.
  4139. The \var{InitManager} and \var{DoneManager} are called when the
  4140. threadmanager is set (\var{InitManager}), or when it is unset
  4141. (\var{DoneManager}). They can be used to initialize the thread manager or to
  4142. clean up when it is done. If either of them returns \var{False}, the
  4143. operation fails.
  4144. There are some special entries in the record, linked to thread variable
  4145. management:
  4146. \begin{description}
  4147. \item[InitThreadVar] is called when a thread variable must be initialized.
  4148. It is of type
  4149. \begin{verbatim}
  4150. TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
  4151. \end{verbatim}
  4152. The \var{offset} parameter indicates the offset in the thread variable
  4153. block: All thread variables are located in a single block, one after the
  4154. other. The size parameter indicates the size of the thread variable. This
  4155. function will be called once for all thread variables in the program.
  4156. \item[RelocateThreadVar] is called each time when a thread is started, and
  4157. once for the main thread. It is of type:
  4158. \begin{verbatim}
  4159. TRelocateThreadVarHandler = Function(offset : dword) : pointer;
  4160. \end{verbatim}
  4161. It should return the new location for the thread-local variable.
  4162. \item[AllocateThreadVars] is called when room must be allocated for all
  4163. threadvars for a new thread. It's a simple procedure, without parameters. The total size of the threadvars is
  4164. stored by the compiler in the \var{threadvarblocksize} global variable.
  4165. The heap manager may {\em not} be used in this procedure: the heap manager
  4166. itself uses threadvars, which have not yet been allocated.
  4167. \item[ReleaseThreadVars] This procedure (without parameters) is called when
  4168. a thread terminates, and all memory allocated must be released again.
  4169. \end{description}
  4170. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4171. % Optimizations done in the compiler
  4172. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4173. \chapter{Optimizations}
  4174. \section{Non processor specific}
  4175. The following sections describe the general optimizations
  4176. done by the compiler, they are not processor specific. Some
  4177. of these require some compiler switch override while others are done
  4178. automatically (those which require a switch will be noted as such).
  4179. \subsection{Constant folding}
  4180. In \fpc, if the operand(s) of an operator are constants, they
  4181. will be evaluated at compile time.
  4182. Example
  4183. \begin{verbatim}
  4184. x:=1+2+3+6+5;
  4185. \end{verbatim}
  4186. will generate the same code as
  4187. \begin{verbatim}
  4188. x:=17;
  4189. \end{verbatim}
  4190. Furthermore, if an array index is a constant, the offset will
  4191. be evaluated at compile time. This means that accessing MyData[5]
  4192. is as efficient as accessing a normal variable.
  4193. Finally, calling \var{Chr}, \var{Hi}, \var{Lo}, \var{Ord}, \var{Pred},
  4194. or \var{Succ} functions with constant parameters generates no
  4195. run-time library calls, instead, the values are evaluated at
  4196. compile time.
  4197. \subsection{Constant merging}
  4198. Using the same constant string, floating point value or constant set
  4199. two or more times generates only one copy of that constant.
  4200. \subsection{Short cut evaluation}
  4201. Evaluation of boolean expression stops as soon as the result is
  4202. known, which makes code execute faster then if all boolean operands
  4203. were evaluated.
  4204. \subsection{Constant set inlining}
  4205. Using the \var{in} operator is always more efficient then using the
  4206. equivalent \verb|<>|, \verb|=|, \verb|<=|, \verb|>=|, \verb|<| and \verb|>|
  4207. operators. This is because range comparisons can be done more easily with
  4208. \var{in} then with normal comparison operators.
  4209. \subsection{Small sets}
  4210. Sets which contain less then 33 elements can be directly encoded
  4211. using a 32-bit value, therefore no run-time library calls to
  4212. evaluate operands on these sets are required; they are directly encoded
  4213. by the code generator.
  4214. \subsection{Range checking}
  4215. Assignments of constants to variables are range checked at compile
  4216. time, which removes the need of the generation of runtime range checking
  4217. code.
  4218. \subsection{And instead of modulo}
  4219. When the second operand of a \var{mod} on an unsigned value is a constant
  4220. power of 2, an \var{and} instruction is used instead of an integer division.
  4221. This generates more efficient code.
  4222. \subsection{Shifts instead of multiply or divide}
  4223. When one of the operands in a multiplication is a power of
  4224. two, they are encoded using arithmetic shift instructions,
  4225. which generates more efficient code.
  4226. Similarly, if the divisor in a \var{div} operation is a power
  4227. of two, it is encoded using arithmetic shift instructions.
  4228. The same is true when accessing array indexes which are
  4229. powers of two, the address is calculated using arithmetic
  4230. shifts instead of the multiply instruction.
  4231. \subsection{Automatic alignment}
  4232. By default all variables larger then a byte are guaranteed to be aligned
  4233. at least on a word boundary.
  4234. Alignment on the stack and in the data section is processor dependant.
  4235. \subsection{Smart linking}
  4236. This feature removes all unreferenced code in the final executable
  4237. file, making the executable file much smaller.
  4238. Smart linking is switched on with the \var{-Cx} command-line switch, or
  4239. using the \var{\{\$SMARTLINK ON\}} global directive.
  4240. \subsection{Inline routines}
  4241. The following runtime library routines are coded directly into the
  4242. final executable: \var{Lo}, \var{Hi}, \var{High}, \var{Sizeof},
  4243. \var{TypeOf}, \var{Length}, \var{Pred}, \var{Succ}, \var{Inc},
  4244. \var{Dec} and \var{Assigned}.
  4245. \subsection{Stack frame omission}
  4246. Under specific conditions, the stack frame (entry and exit code for
  4247. the routine, see section \sees{CallingConventions}) will be omitted, and the
  4248. variable will directly be accessed via the stack pointer.
  4249. Conditions for omission of the stack frame:
  4250. \begin{itemize}
  4251. \item The function has no parameters nor local variables.
  4252. \item Routine is declared with the \var{assembler} modifier.
  4253. \item Routine is not a class.
  4254. \end{itemize}
  4255. \subsection{Register variables}
  4256. When using the \var{-Or} switch, local variables or parameters
  4257. which are used very often will be moved to registers for faster
  4258. access.
  4259. \section{Processor specific}
  4260. This lists the low-level optimizations performed, on a processor
  4261. per processor basis.
  4262. \subsection{Intel 80x86 specific}
  4263. Here follows a listing of the optimizing techniques used in the compiler:
  4264. \begin{enumerate}
  4265. \item When optimizing for a specific Processor (\var{-Op1, -Op2, -Op3},
  4266. the following is done:
  4267. \begin{itemize}
  4268. \item In \var{case} statements, a check is done whether a jump table
  4269. or a sequence of conditional jumps should be used for optimal performance.
  4270. \item Determines a number of strategies when doing peephole optimization, e.g.:
  4271. \var{movzbl (\%ebp), \%eax} will be changed into
  4272. \var{xorl \%eax,\%eax; movb (\%ebp),\%al } for Pentium and PentiumMMX.
  4273. \end{itemize}
  4274. \item When optimizing for speed (\var{-OG}, the default) or size (\var{-Og}), a choice is
  4275. made between using shorter instructions (for size) such as \var{enter \$4},
  4276. or longer instructions \var{subl \$4,\%esp} for speed. When smaller size is
  4277. requested, data is aligned to minimal boundaries. When speed is
  4278. requested, data is aligned on most efficient boundaries as much as possible.
  4279. \item Fast optimizations (\var{-O1}): activate the peephole optimizer
  4280. \item Slower optimizations (\var{-O2}): also activate the common subexpression
  4281. elimination (formerly called the "reloading optimizer")
  4282. \item Uncertain optimizations (\var{-Ou}): With this switch, the common subexpression
  4283. elimination algorithm can be forced into making uncertain optimizations.
  4284. Although you can enable uncertain optimizations in most cases, for people who
  4285. do not understand the following technical explanation, it might be the safest to
  4286. leave them off.
  4287. % Jonas's own words..
  4288. \begin{remark}If uncertain optimizations are enabled, the CSE algortihm assumes
  4289. that
  4290. \begin{itemize}
  4291. \item If something is written to a local/global register or a
  4292. procedure/function parameter, this value doesn't overwrite the value to
  4293. which a pointer points.
  4294. \item If something is written to memory pointed to by a pointer variable,
  4295. this value doesn't overwrite the value of a local/global variable or a
  4296. procedure/function parameter.
  4297. \end{itemize}
  4298. % end of quote
  4299. \end{remark}
  4300. The practical upshot of this is that you cannot use the uncertain
  4301. optimizations if you both write and read local or global variables directly and
  4302. through pointers (this includes \var{Var} parameters, as those are pointers too).
  4303. The following example will produce bad code when you switch on
  4304. uncertain optimizations:
  4305. \begin{verbatim}
  4306. Var temp: Longint;
  4307. Procedure Foo(Var Bar: Longint);
  4308. Begin
  4309. If (Bar = temp)
  4310. Then
  4311. Begin
  4312. Inc(Bar);
  4313. If (Bar <> temp) then Writeln('bug!')
  4314. End
  4315. End;
  4316. Begin
  4317. Foo(Temp);
  4318. End.
  4319. \end{verbatim}
  4320. The reason it produces bad code is because you access the global variable
  4321. \var{Temp} both through its name \var{Temp} and through a pointer, in this
  4322. case using the \var{Bar} variable parameter, which is nothing but a pointer
  4323. to \var{Temp} in the above code.
  4324. On the other hand, you can use the uncertain optimizations if
  4325. you access global/local variables or parameters through pointers,
  4326. and {\em only} access them through this pointer\footnote{
  4327. You can use multiple pointers to point to the same variable as well, that
  4328. doesn't matter.}.
  4329. For example:
  4330. \begin{verbatim}
  4331. Type TMyRec = Record
  4332. a, b: Longint;
  4333. End;
  4334. PMyRec = ^TMyRec;
  4335. TMyRecArray = Array [1..100000] of TMyRec;
  4336. PMyRecArray = ^TMyRecArray;
  4337. Var MyRecArrayPtr: PMyRecArray;
  4338. MyRecPtr: PMyRec;
  4339. Counter: Longint;
  4340. Begin
  4341. New(MyRecArrayPtr);
  4342. For Counter := 1 to 100000 Do
  4343. Begin
  4344. MyRecPtr := @MyRecArrayPtr^[Counter];
  4345. MyRecPtr^.a := Counter;
  4346. MyRecPtr^.b := Counter div 2;
  4347. End;
  4348. End.
  4349. \end{verbatim}
  4350. Will produce correct code, because the global variable \var{MyRecArrayPtr}
  4351. is not accessed directly, but only through a pointer (\var{MyRecPtr} in this
  4352. case).
  4353. In conclusion, one could say that you can use uncertain optimizations {\em
  4354. only} when you know what you're doing.
  4355. \end{enumerate}
  4356. \subsection{Motorola 680x0 specific}
  4357. Using the \var{-O2} (the default) switch does several optimizations
  4358. in the code produced, the most notable being:
  4359. \begin{itemize}
  4360. \item Sign extension from byte to long will use \var{EXTB}
  4361. \item Returning of functions will use \var{RTD}
  4362. \item Range checking will generate no run-time calls
  4363. \item Multiplication will use the long \var{MULS} instruction, no
  4364. runtime library call will be generated
  4365. \item Division will use the long \var{DIVS} instruction, no
  4366. runtime library call will be generated
  4367. \end{itemize}
  4368. \section{Optimization switches}
  4369. This is where the various optimizing switches and their actions are
  4370. described, grouped per switch.
  4371. \begin{description}
  4372. \item [-On:\ ] with n = 1..3: these switches activate the optimizer.
  4373. A higher level automatically includes all lower levels.
  4374. \begin{itemize}
  4375. \item Level 1 (\var{-O1}) activates the peephole optimizer
  4376. (common instruction sequences are replaced by faster equivalents).
  4377. \item Level 2 (\var{-O2}) enables the assembler data flow analyzer,
  4378. which allows the common subexpression elimination procedure to
  4379. remove unnecessary reloads of registers with values they already contain.
  4380. \item Level 3 (\var{-O3}) enables uncertain optimizations. For more info, see -Ou.
  4381. \end{itemize}
  4382. \item[-OG:\ ]
  4383. This causes the code generator (and optimizer, IF activated), to favor
  4384. faster, but code-wise larger, instruction sequences (such as
  4385. "\verb|subl $4,%esp|") instead of slower, smaller instructions
  4386. ("\verb|enter $4|"). This is the default setting.
  4387. \item[-Og:\ ] This one is exactly the reverse of -OG, and as such these
  4388. switches are mutually exclusive: enabling one will disable the other.
  4389. \item[-Or:\ ] This setting causes the code generator to
  4390. check which variables are used most, so it can keep those in a register.
  4391. \item[-Opn:\ ] with n = 1..3: Setting the target processor does NOT
  4392. activate the optimizer. It merely influences the code generator and,
  4393. if activated, the optimizer:
  4394. \begin{itemize}
  4395. \item During the code generation process, this setting is used to
  4396. decide whether a jump table or a sequence of successive jumps provides
  4397. the best performance in a case statement.
  4398. \item The peephole optimizer takes a number of decisions based on this
  4399. setting, for example it translates certain complex instructions, such
  4400. as
  4401. \begin{verbatim}
  4402. movzbl (mem), %eax|
  4403. \end{verbatim}
  4404. to a combination of simpler instructions
  4405. \begin{verbatim}
  4406. xorl %eax, %eax
  4407. movb (mem), %al
  4408. \end{verbatim}
  4409. for the Pentium.
  4410. \end{itemize}
  4411. \item[-Ou:\ ] This enables uncertain optimizations. You cannot use these
  4412. always, however. The previous section explains when they can be used, and
  4413. when they cannot be used.
  4414. \end{description}
  4415. \section{Tips to get faster code}
  4416. Here, some general tips for getting better code are presented. They
  4417. mainly concern coding style.
  4418. \begin{itemize}
  4419. \item Find a better algorithm. No matter how much you and the compiler
  4420. tweak the code, a quicksort will (almost) always outperform a bubble
  4421. sort, for example.
  4422. \item Use variables of the native size of the processor you're writing
  4423. for. This is currently 32-bit or 64-bit for \fpc, so you are best to
  4424. use longword and longint variables.
  4425. \item Turn on the optimizer.
  4426. \item Write your if/then/else statements so that the code in the "then"-part
  4427. gets executed most of the time (improves the rate of successful jump prediction).
  4428. \item Do not use ansistrings, widestrings and exception support, as these
  4429. require a lot of code overhead.
  4430. \item Profile your code (see the -pg switch) to find out where the
  4431. bottlenecks are. If you want, you can rewrite those parts in assembler.
  4432. You can take the code generated by the compiler as a starting point. When
  4433. given the \var{-a} command-line switch, the compiler will not erase the
  4434. assembler file at the end of the assembly process, so you can study the
  4435. assembler file.
  4436. \end{itemize}
  4437. \section{Tips to get smaller code}
  4438. Here are some tips given to get the smallest code possible.
  4439. \begin{itemize}
  4440. \item Find a better algorithm.
  4441. \item Use the \var{-Og} compiler switch.
  4442. \item Regroup global static variables in the same module which have the same
  4443. size together to minimize the number of alignment directives (which increases
  4444. the \var{.bss} and \var{.data} sections unecessarily). Internally this is
  4445. due to the fact that all static data is written to in the assembler file,
  4446. in the order they are declared in the pascal source code.
  4447. \item Do not use the \var{cdecl} modifier, as this generates
  4448. about 1 additional instruction after each subroutine call.
  4449. \item Use the smartlinking options for all your units
  4450. (including the \var{system} unit).
  4451. \item Do not use ansistrings, widestrings and exception support, as these
  4452. require a lot of code overhead.
  4453. \item Turn off range checking and stack-checking.
  4454. \item Turn off runtime type information generation
  4455. \end{itemize}
  4456. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4457. % programming shared libraries
  4458. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4459. \chapter{Programming shared libraries}
  4460. \label{ch:libraries}
  4461. \section{Introduction}
  4462. \fpc supports the creation of shared libraries on several operating
  4463. systems. The following table (\seet{DLLSupport}) indicates which
  4464. operating systems support the creation of shared libraries.
  4465. \begin{FPCltable}{lll}{Shared library support}{DLLSupport} \hline
  4466. Operating systems & Library extension & Library prefix \\ \hline
  4467. linux & .so & lib \\
  4468. windows & .dll & <none> \\
  4469. BeOS & .so & lib \\
  4470. FreeBSD & .so & lib \\
  4471. NetBSD & .so & lib \\
  4472. \hline
  4473. \end{FPCltable}
  4474. The library prefix column indicates how the names of the libraries are
  4475. resolved and created. For example, under \linux, the library name will
  4476. alwaus have the \var{lib} prefix when it is created. So if you create a
  4477. library called \file{mylib}, under \linux, this will result in the
  4478. \file{libmylib.so}. Furthermore, when importing routines from shared
  4479. libraries, it is not necessary to give the library prefix or the
  4480. filename extension.
  4481. In the following sections we discuss how to create a library, and how
  4482. to use these libraries in programs.
  4483. \section{Creating a library}
  4484. Creation of libraries is supported in any mode of the \fpc compiler,
  4485. but it may be that the arguments or return values differ if the library is
  4486. compiled in 2 different modes. E.g. if your function expects an
  4487. \var{Integer} argument, then the library will expect different integer
  4488. sizes if you compile it in Delphi mode or in TP mode.
  4489. A library can be created just as a program, only it uses the \var{library}
  4490. keyword, and it has an \var{exports} section. The following listing
  4491. demonstrates a simple library:
  4492. \FPCexample{subs}
  4493. The function \var{SubStr} does not have to be declared in the library file
  4494. itself. It can also be declared in the interface section of a unit that
  4495. is used by the library.
  4496. Compilation of this source will result in the creation of a library called
  4497. \file{libsubs.so} on \unix systems, or \file{subs.dll} on \windows or \ostwo.
  4498. The compiler will take care of any additional linking that is required to create a
  4499. shared library.
  4500. The library exports one function: \var{SubStr}. The case is important. The
  4501. case as it appears in the \var{exports} clause is used to export the
  4502. function.
  4503. If you want your library to be called from programs compiled with
  4504. other compilers, it is important to specify the correct calling
  4505. convention for the exported functions. Since the generated programs
  4506. by other compilers do not know about the \fpc calling conventions,
  4507. your functions would be called incorrectly, resulting in a corrupted
  4508. stack.
  4509. On \windows, most libraries use the \var{stdcall} convention, so it may be
  4510. better to use that one if your library is to be used on \windows systems. On
  4511. most \unix systems, the C calling convention is used, therefore the
  4512. \var{cdecl} modifier should be used in that case.
  4513. \section{Using a library in a pascal program}
  4514. In order to use a function that resides in a library, it is sufficient to
  4515. declare the function as it exists in the library as an \var{external}
  4516. function, with correct arguments and return type. The calling convention
  4517. used by the function should be declared correctly as well. The compiler
  4518. will then link the library as specified in the \var{external} statement
  4519. to your program\footnote{If you omit the library name in the \var{external}
  4520. modifier, then you can still tell the compiler to link to that library using
  4521. the \var{\{\$Linklib\}} directive.}.
  4522. For example, to use the library as defined above from a pascal program, you can use
  4523. the following pascal program:
  4524. \FPCexample{psubs}
  4525. As is shown in the example, you must declare the function as \var{external}.
  4526. Here also, it is necessary to specify the correct calling convention (it
  4527. should always match the convention as used by the function in the library),
  4528. and to use the correct casing for your declaration. Also notice, that
  4529. the library importing did not specify the filename extension, nor
  4530. was the \var{lib} prefix added.
  4531. This program can be compiled without any additional command-switches,
  4532. and should run just like that, provided the library is placed where
  4533. the system can find it. For example, on \linux, this is \file{/usr/lib} or any
  4534. directory listed in the \file{/etc/ld.so.conf} file. On \windows, this
  4535. can be the program directory, the \windows system directory, or any directoy
  4536. mentioned in the \var{PATH}.
  4537. Using the library in this way links the library to your program at compile
  4538. time. This means that
  4539. \begin{enumerate}
  4540. \item The library must be present on the system where the program is
  4541. compiled.
  4542. \item The library must be present on the system where the program is
  4543. executed.
  4544. \item Both libraries must be exactly the same.
  4545. \end{enumerate}
  4546. Or it may simply be that you don't know the name of the function to
  4547. be called, you just know the arguments it expects.
  4548. It is therefore also possible to load the library at run-time, store
  4549. the function address in a procedural variable, and use this procedural
  4550. variable to access the function in the library.
  4551. The following example demonstrates this technique:
  4552. \FPCexample{plsubs}
  4553. As in the case of compile-time linking, the crucial thing in this
  4554. listing is the declaration of the \var{TSubStrFunc} type.
  4555. It should match the declaration of the function you're trying to use.
  4556. Failure to specify a correct definition will result in a faulty stack or,
  4557. worse still, may cause your program to crash with an access violation.
  4558. \section{Using a pascal library from a C program}
  4559. \begin{remark}The examples in this section assume a \linux system; similar commands
  4560. as the ones below exist for other operating systems, though.
  4561. \end{remark}
  4562. You can also call a \fpc generated library from a C program:
  4563. \Cexample{ctest}
  4564. To compile this example, the following command can be used:
  4565. \begin{verbatim}
  4566. gcc -o ctest ctest.c -lsubs
  4567. \end{verbatim}
  4568. provided the code is in \file{ctest.c}.
  4569. The library can also be loaded dynamically from C, as shown in the following
  4570. example:
  4571. \Cexample{ctest2}
  4572. This can be compiled using the following command:
  4573. \begin{verbatim}
  4574. gcc -o ctest2 ctest2.c -ldl
  4575. \end{verbatim}
  4576. The \var{-ldl} tells gcc that the program needs the \file{libdl.so} library
  4577. to load dynamical libraries.
  4578. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4579. % Some windows issues
  4580. \section{Some Windows issues}
  4581. \label{shlibwinissues}
  4582. By default, \fpc (actually, the linker used by \fpc)
  4583. creates libraries that are not relocatable. This means that they must be
  4584. loaded at a fixed address in memory: this address is called the
  4585. ImageBase address. If two \fpc generated libraries are loaded by a
  4586. program, there will be a conflict, because the first librarie already
  4587. occupies the memory location where the second library should be loaded.
  4588. There are 2 switches in Free Pascal which control the generation of
  4589. shared libraries under \windows:
  4590. \begin{description}
  4591. \item[-WR] Generate a relocatable library. This library can be moved to
  4592. another location in memory if the ImageBase address it wants is already
  4593. in use.
  4594. \item[-WB] Specify the ImageBase address for the generated library.
  4595. The standard ImageBase used by \fpc is \var{0x10000000}. This switch
  4596. allows to change that by specifying another address, for instance
  4597. \var{-WB11000000}.
  4598. \end{description}
  4599. The first option is preferred, as a program may load many libraries
  4600. present on the system, and they could already be using the ImageBase
  4601. address. The second option is faster, as no relocation needs to be
  4602. done if the ImageBase address is not yet in use.
  4603. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4604. % using resources
  4605. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4606. \chapter{Using Windows resources}
  4607. \label{ch:windres}
  4608. \section{The resource directive \var{\$R}}
  4609. Under \windows, you can include resources in your executable or library
  4610. using the \var{\{\$R filename\}} directive. These resources can then
  4611. be accessed through the standard \windows API calls.
  4612. When the compiler encounters a resource directive, it just creates an
  4613. entry in the unit \file{.ppu} file; it doesn't link the resource. Only
  4614. when it creates a library or executable, it looks for all the resource
  4615. files for which it encountered a directive, and tries to link them in.
  4616. The default extension for resource files is \file{.res}. When the
  4617. filename has as the first character an asterix (\var{*}), the
  4618. compiler will replace the asterix with the name of the current unit,
  4619. library or program.
  4620. \begin{remark}This means that the asterix may only be used after a \var{unit},
  4621. \var{library} or \var{program} clause.
  4622. \end{remark}
  4623. \section{Creating resources}
  4624. The \fpc compiler itself doesn't create any resource files; it just
  4625. compiles them into the executable. To create resource files, you
  4626. can use some GUI tools as the Borland resource workshop; but it is
  4627. also possible to use a \windows resource compiler like \gnu
  4628. \file{windres}. \file{windres} comes with the \gnu binutils, but the
  4629. \fpc distribution also contains a version which you can use.
  4630. The usage of windres is straightforward; it reads an input file
  4631. describing the resources to create and outputs a resource file.
  4632. A typical invocation of \file{windres} would be
  4633. \begin{verbatim}
  4634. windres -i mystrings.rc -o mystrings.res
  4635. \end{verbatim}
  4636. this will read the \file{mystrings.rc} file and output a
  4637. \file{mystrings.res} resource file.
  4638. A complete overview of the windres tools is outside the scope of this
  4639. document, but here are some things you can use it for:
  4640. \begin{description}
  4641. \item[stringtables] that contain lists of strings.
  4642. \item[bitmaps] which are read from an external file.
  4643. \item[icons] which are also read from an external file.
  4644. \item[Version information] which can be viewed with the \windows
  4645. explorer.
  4646. \item[Menus] Can be designed as resources and used in your GUI
  4647. applications.
  4648. \item[Arbitrary data] Can be included as resources and read with the
  4649. windows API calls.
  4650. \end{description}
  4651. Some of these will be described below.
  4652. \section{Using string tables.}
  4653. String tables can be used to store and retrieve large collections of
  4654. strings in your application.
  4655. A string table looks as follows:
  4656. \begin{verbatim}
  4657. STRINGTABLE { 1, "hello World !"
  4658. 2, "hello world again !"
  4659. 3, "last hello world !" }
  4660. \end{verbatim}
  4661. You can compile this (we assume the file is called \file{tests.rc}) as
  4662. follows:
  4663. \begin{verbatim}
  4664. windres -i tests.rc -o tests.res
  4665. \end{verbatim}
  4666. And this is the way to retrieve the strings from your program:
  4667. \begin{verbatim}
  4668. program tests;
  4669. {$mode objfpc}
  4670. Uses Windows;
  4671. {$R *.res}
  4672. Function LoadResourceString (Index : longint): Shortstring;
  4673. begin
  4674. SetLength(Result,LoadString(FindResource(0,Nil,RT_STRING),Index,@Result[1],SizeOf(Result)))
  4675. end;
  4676. Var
  4677. I: longint;
  4678. begin
  4679. For i:=1 to 3 do
  4680. Writeln (Loadresourcestring(I));
  4681. end.
  4682. \end{verbatim}
  4683. The call to \var{FindResource} searches for the stringtable in the
  4684. compiled-in resources. The \var{LoadString} function then reads the
  4685. string with index \var{i} out of the table, and puts it in a buffer,
  4686. which can then be used. Both calls are in the \file{windows} unit.
  4687. \section{Inserting version information}
  4688. The win32 API allows to store version information in your binaries.
  4689. This information can be made visible with the \windows Explorer, by
  4690. right-clicking on the executable or library, and selecting the
  4691. 'Properties' menu. In the tab 'Version' the version information will
  4692. be displayed.
  4693. Here is how to insert version information in your binary:
  4694. \begin{verbatim}
  4695. 1 VERSIONINFO
  4696. FILEVERSION 4, 0, 3, 17
  4697. PRODUCTVERSION 3, 0, 0, 0
  4698. FILEFLAGSMASK 0
  4699. FILEOS 0x40000
  4700. FILETYPE 1
  4701. {
  4702. BLOCK "StringFileInfo"
  4703. {
  4704. BLOCK "040904E4"
  4705. {
  4706. VALUE "CompanyName", "Free Pascal"
  4707. VALUE "FileDescription", "Free Pascal version information extractor"
  4708. VALUE "FileVersion", "1.0"
  4709. VALUE "InternalName", "Showver"
  4710. VALUE "LegalCopyright", "GNU Public License"
  4711. VALUE "OriginalFilename", "showver.pp"
  4712. VALUE "ProductName", "Free Pascal"
  4713. VALUE "ProductVersion", "1.0"
  4714. }
  4715. }
  4716. }
  4717. \end{verbatim}
  4718. As you can see, you can insert various kinds of information in the version info
  4719. block. The keyword \var{VERSIONINFO} marks the beginning of the version
  4720. information resource block. The keywords \var{FILEVERSION},
  4721. \var{PRODUCTVERSION} give the actual file version, while the block
  4722. \var{StringFileInfo} gives other information that is displayed in the
  4723. explorer.
  4724. The Free Component Library comes with a unit (\file{fileinfo}) that allows
  4725. to extract and view version information in a straightforward and easy manner;
  4726. the demo program that comes with it (\file{showver}) shows version information
  4727. for an arbitrary executable or DLL.
  4728. \section{Inserting an application icon}
  4729. When \windows shows an executable in the Explorer, it looks for an icon
  4730. in the executable to show in front of the filename, the application
  4731. icon.
  4732. Inserting an application icon is very easy and can be done as follows
  4733. \begin{verbatim}
  4734. AppIcon ICON "filename.ico"
  4735. \end{verbatim}
  4736. This will read the file \file{filename.ico} and insert it in the
  4737. resource file.
  4738. \section{Using a pascal preprocessor}
  4739. Sometimes you want to use symbolic names in your resource file, and
  4740. use the same names in your program to access the resources. To accomplish
  4741. this, there exists a preprocessor for \file{windres} that understands pascal
  4742. syntax: \file{fprcp}. This preprocessor is shipped with the \fpc
  4743. distribution.
  4744. The idea is that the preprocessor reads a pascal unit that has some
  4745. symbolic constants defined in it, and replaces symbolic names in the
  4746. resource file by the values of the constants in the unit:
  4747. As an example: consider the follwoing unit:
  4748. \begin{verbatim}
  4749. unit myunit;
  4750. interface
  4751. Const
  4752. First = 1;
  4753. Second = 2:
  4754. Third = 3;
  4755. Implementation
  4756. end.
  4757. \end{verbatim}
  4758. And the following resource file:
  4759. \begin{verbatim}
  4760. #include "myunit.pp"
  4761. STRINGTABLE { First, "hello World !"
  4762. Second, "hello world again !"
  4763. Third, "last hello world !" }
  4764. \end{verbatim}
  4765. if you invoke windres with the \var{--preprocessor} option:
  4766. \begin{verbatim}
  4767. windres --preprocessor fprcp -i myunit.rc -o myunit.res
  4768. \end{verbatim}
  4769. Then the preprocessor will replace the symbolic names 'first', 'second'
  4770. and 'third' with their actual values.
  4771. In your program, you can then refer to the strings by their symbolic
  4772. names (the constants) instead of using a numeric index.
  4773. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4774. % Appendices
  4775. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4776. \appendix
  4777. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4778. % Appendix A
  4779. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4780. \chapter{Anatomy of a unit file}
  4781. \label{ch:AppA}
  4782. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4783. % Basics
  4784. \section{Basics}
  4785. As described in \seec{GenCode}, unit description files (hereafter
  4786. called PPU files for short), are used to determine if the unit
  4787. code must be recompiled or not. In other words, the PPU files act
  4788. as mini-makefiles, which is used to check dependencies of the
  4789. different code modules, as well as verify if the modules are up
  4790. to date or not. Furthermore, it contains all public symbols defined
  4791. for a module.
  4792. The general format of the \var{ppu} file format is shown
  4793. in \seefig{ppu}.
  4794. \FPCpic{The PPU file format}{}{ppu}
  4795. To read or write the ppufile, the ppu unit \file{ppu.pas} can be used,
  4796. which has an object called tppufile which holds all routines that deal
  4797. with ppufile handling. While describing the layout of a ppufile, the
  4798. methods which can be used for it are presented as well.
  4799. A unit file consists of basically five or six parts:
  4800. \begin{enumerate}
  4801. \item A unit header.
  4802. \item A general information part (wrongly named interface section in the code)
  4803. \item A definition part. Contains all type and procedure definitions.
  4804. \item A symbol part. Contains all symbol names and references to their
  4805. definitions.
  4806. \item A browser part. Contains all references from this unit to other
  4807. units and inside this unit. Only available when the \var{uf\_has\_browser} flag is
  4808. set in the unit flags
  4809. \item A file implementation part (currently unused).
  4810. \end{enumerate}
  4811. \section{reading ppufiles}
  4812. We will first create an object ppufile which will be used below. We are
  4813. opening unit \file{test.ppu} as an example.
  4814. \begin{verbatim}
  4815. var
  4816. ppufile : pppufile;
  4817. begin
  4818. { Initialize object }
  4819. ppufile:=new(pppufile,init('test.ppu');
  4820. { open the unit and read the header, returns false when it fails }
  4821. if not ppufile.openfile then
  4822. error('error opening unit test.ppu');
  4823. { here we can read the unit }
  4824. { close unit }
  4825. ppufile.closefile;
  4826. { release object }
  4827. dispose(ppufile,done);
  4828. end;
  4829. \end{verbatim}
  4830. Note: When a function fails (for example not enough bytes left in an
  4831. entry) it sets the \var{ppufile.error} variable.
  4832. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4833. % The Header
  4834. \section{The Header}
  4835. The header consists of a record (\var{tppuheader}) containing
  4836. several pieces of information for recompilation. This is shown
  4837. in \seet{PPUHeader}. The header is always stored in little-endian
  4838. format.
  4839. \begin{FPCltable}{|l|c|l|}{PPU Header}{PPUHeader}
  4840. \hline
  4841. offset & size (bytes) & description \\
  4842. \hline
  4843. 00h & 3 & Magic : 'PPU' in ASCII \\
  4844. 03h & 3 & PPU File format version (e.g : '021' in ASCII) \\
  4845. 06h & 2 & Compiler version used to compile this module (major,minor)\\
  4846. 08h & 2 & Code module target processor \\
  4847. 0Ah & 2 & Code module target operating system \\
  4848. 0Ch & 4 & Flags for PPU file \\
  4849. 10h & 4 & Size of PPU file (without header) \\
  4850. 14h & 4 & CRC-32 of the entire PPU file \\
  4851. 18h & 4 & CRC-32 of partial data of PPU file (public data mostly) \\
  4852. 1Ch & 8 & Reserved \\
  4853. \hline
  4854. \end{FPCltable}
  4855. The header is already read by the \var{ppufile.openfile} command.
  4856. You can access all fields using \var{ppufile.header} which holds
  4857. the current header record.
  4858. \begin{FPCltable}{|ll|}{PPU CPU Field values}{PPUHeaderCPU}
  4859. \hline
  4860. value & description \\
  4861. \hline
  4862. 0 & unknown \\
  4863. 1 & Intel 80x86 or compatible \\
  4864. 2 & Motorola 680x0 or compatible \\
  4865. 3 & Alpha AXP or compatible \\
  4866. 4 & PowerPC or compatible \\
  4867. \hline
  4868. \end{FPCltable}
  4869. Some of the possible flags in the header, are described in \seet{PPUHeaderFlags}.
  4870. Not all the flags are described, for more information, read the source code of
  4871. \file{ppu.pas}.
  4872. \begin{FPCltable}{|ll|}{PPU Header Flag values}{PPUHeaderFlags}
  4873. \hline
  4874. Symbolic bit flag name & Description\\
  4875. \hline
  4876. uf\_init & Module has an initialization (either Delphi or TP style) section. \\
  4877. uf\_finalize & Module has a finalization section. \\
  4878. uf\_big\_endian & All the data stored in the chunks is in big-endian format.\\
  4879. uf\_has\_browser & Unit contains symbol browser information. \\
  4880. uf\_smart\_linked & The code module has been smartlinked.\\
  4881. uf\_static\_linked & The code is statically linked. \\
  4882. uf\_has\_resources & Unit has resource section. \\
  4883. \hline
  4884. \end{FPCltable}
  4885. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
  4886. % The sections
  4887. \section{The sections}
  4888. Apart from the header section, all the data in the PPU file is separated
  4889. into data blocks, which permit easily adding additional data blocks,
  4890. without compromising backward compatibility. This is similar to both
  4891. Electronic Arts IFF chunk format and Microsoft's RIFF chunk format.
  4892. Each 'chunk' (\var{tppuentry}) has the following format, and can be
  4893. nested:
  4894. \begin{FPCltable}{|l|c|l|}{chunk data format}{ChunkFormats}
  4895. \hline
  4896. offset & size (bytes) & description \\
  4897. \hline
  4898. 00h & 1 & Block type (nested (2) or main (1)) \\
  4899. 01h & 1 & Block identifier \\
  4900. 02h & 4 & Size of this data block \\
  4901. 06h+ & <variable> & Data for this block \\
  4902. \hline
  4903. \end{FPCltable}
  4904. Each main section chunk must end with an end chunk. Nested chunks
  4905. are used for record, class or object fields.
  4906. To read an entry you can simply call \var{ppufile.readentry:byte},
  4907. it returns the
  4908. \var{tppuentry.nr} field, which holds the type of the entry.
  4909. A common way how this works is (example is for the symbols):
  4910. \begin{verbatim}
  4911. repeat
  4912. b:=ppufile.readentry;
  4913. case b of
  4914. ib<etc> : begin
  4915. end;
  4916. ibendsyms : break;
  4917. end;
  4918. until false;
  4919. \end{verbatim}
  4920. The possible entry types are found in \file{ppu.pas}, but a short
  4921. description of the most common ones are shown in \seet{PPUEntryTypes}.
  4922. \begin{FPCltable}{|lll|}{Possible PPU Entry types}{PPUEntryTypes}
  4923. \hline
  4924. Symbolic name & Location & Description\\
  4925. \hline
  4926. ibmodulename & General & Name of this unit.\\
  4927. ibsourcefiles & General & Name of source files.\\
  4928. ibusedmacros & General & Name and state of macros used.\\
  4929. ibloadunit & General & Modules used by this units. \\
  4930. inlinkunitofiles & General & Object files associated with this unit. \\
  4931. iblinkunitstaticlibs & General & Static libraries associated with this unit. \\
  4932. iblinkunitsharedlibs & General & Shared libraries associated with this unit. \\
  4933. ibendinterface & General & End of General information section. \\
  4934. ibstartdefs & Interface & Start of definitions. \\
  4935. ibenddefs & Interface & End of definitions. \\
  4936. ibstartsyms & Interface & Start of symbol data. \\
  4937. ibendsyms & Interface & End of symbol data. \\
  4938. ibendimplementation & Implementation & End of implementation data. \\
  4939. ibendbrowser & Browser & End of browser section. \\
  4940. ibend & General & End of Unit file. \\
  4941. \hline
  4942. \end{FPCltable}
  4943. Then you can parse each entry type yourself. \var{ppufile.readentry} will take
  4944. care of skipping unread bytes in the entry and reads the next entry
  4945. correctly! A special function is \var{skipuntilentry(untilb:byte):boolean;}
  4946. which will read the ppufile until it finds entry \var{untilb} in the main
  4947. entries.
  4948. Parsing an entry can be done with \var{ppufile.getxxx} functions. The
  4949. available functions are:
  4950. \begin{verbatim}
  4951. procedure ppufile.getdata(var b;len:longint);
  4952. function getbyte:byte;
  4953. function getword:word;
  4954. function getlongint:longint;
  4955. function getreal:ppureal;
  4956. function getstring:string;
  4957. \end{verbatim}
  4958. To check if you're at the end of an entry you can use the following
  4959. function:
  4960. \begin{verbatim}
  4961. function EndOfEntry:boolean;
  4962. \end{verbatim}
  4963. {\em notes:}
  4964. \begin{enumerate}
  4965. \item \var{ppureal} is the best real that exists for the cpu where the
  4966. unit is created for. Currently it is \var{extended} for i386 and
  4967. \var{single} for m68k.
  4968. \item the \var{ibobjectdef} and \var{ibrecorddef} have stored a definition
  4969. and symbol section for themselves. So you'll need a recursive call. See
  4970. \file{ppudump.pp} for a correct implementation.
  4971. \end{enumerate}
  4972. A complete list of entries and what their fields contain can be found
  4973. in \file{ppudump.pp}.
  4974. \section{Creating ppufiles}
  4975. Creating a new ppufile works almost the same as reading one.
  4976. First you need to init the object and call create:
  4977. \begin{verbatim}
  4978. ppufile:=new(pppufile,init('output.ppu'));
  4979. ppufile.createfile;
  4980. \end{verbatim}
  4981. After that you can simply write all needed entries. You'll have to take
  4982. care that you write at least the basic entries for the sections:
  4983. \begin{verbatim}
  4984. ibendinterface
  4985. ibenddefs
  4986. ibendsyms
  4987. ibendbrowser (only when you've set uf_has_browser!)
  4988. ibendimplementation
  4989. ibend
  4990. \end{verbatim}
  4991. Writing an entry is a little different than reading it. You need to first
  4992. put everything in the entry with ppufile.putxxx:
  4993. \begin{verbatim}
  4994. procedure putdata(var b;len:longint);
  4995. procedure putbyte(b:byte);
  4996. procedure putword(w:word);
  4997. procedure putlongint(l:longint);
  4998. procedure putreal(d:ppureal);
  4999. procedure putstring(s:string);
  5000. \end{verbatim}
  5001. After putting all the things in the entry you need to call
  5002. \var{ppufile.writeentry(ibnr:byte)} where \var{ibnr} is the entry number
  5003. you're writing.
  5004. At the end of the file you need to call \var{ppufile.writeheader} to write the
  5005. new header to the file. This takes automatically care of the new size of the
  5006. ppufile. When that is also done you can call \var{ppufile.closefile} and dispose the
  5007. object.
  5008. Extra functions/variables available for writing are:
  5009. \begin{verbatim}
  5010. ppufile.NewHeader;
  5011. ppufile.NewEntry;
  5012. \end{verbatim}
  5013. This will give you a clean header or entry. Normally this is called
  5014. automatically in \var{ppufile.writeentry}, so there should be no need to
  5015. call these methods.
  5016. \begin{verbatim}
  5017. ppufile.flush;
  5018. \end{verbatim}
  5019. to flush the current buffers to the disk
  5020. \begin{verbatim}
  5021. ppufile.do_crc:boolean;
  5022. \end{verbatim}
  5023. set to false if you don't want that the crc is updated, this is necessary
  5024. if you write for example the browser data.
  5025. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5026. % Appendix B
  5027. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5028. \chapter{Compiler and RTL source tree structure}
  5029. \label{ch:AppB}
  5030. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5031. % The compiler source tree
  5032. \section{The compiler source tree}
  5033. All compiler source files are in several directories, normally the
  5034. non-processor specific parts are in \file{source/compiler}. Subdirectories
  5035. are present for each of the supported processors and target operating systems.
  5036. For more informations about the structure of the compiler have a look at the
  5037. Compiler Manual which contains also some informations about
  5038. compiler internals.
  5039. The \file{compiler} directory also contains a subdirectory \var{utils},
  5040. which contains mainly the utilities for creation and maintainance of the
  5041. message files.
  5042. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5043. % The RTL source tree
  5044. \section{The RTL source tree}
  5045. The RTL source tree is divided in many subdirectories, but is very
  5046. structured and easy to understand. It mainly consists of three parts:
  5047. \begin{enumerate}
  5048. \item A OS-dependent directory. This contains the files that are different for
  5049. each operating system. When compiling the RTL, you should do it here. The
  5050. following directories exist:
  5051. \begin{itemize}
  5052. \item \file{atari} for the \atari.
  5053. \item \file{amiga} for the \amiga.
  5054. \item \file{beos} for \beos. It has one subdirectory for each of the
  5055. supported processors.
  5056. \item \file{darwin} for the unix-compatibility layer on \macos.
  5057. \item \file{freebsd} for the \freebsd platform.
  5058. \item \file{go32v1} For \dos, using the GO32v1 extender. Not maintained any
  5059. more.
  5060. \item \file{go32v2} For \dos, using the GO32v2 extender.
  5061. \item \file{linux} for \linux platforms. It has one subdirectory for each of the supported
  5062. processors.
  5063. \item \file{macos} for the \macos platform.
  5064. \item \file{morphos} for the MorphOS platform.
  5065. \item \file{netbsd} for \netbsd platforms. It has one subdirectory for each of the supported
  5066. processors.
  5067. \item \file{netware} for the Novell netware platform.
  5068. \item \file{openbsd} for the OpenBSD platform.
  5069. \item \file{palmos} for the \palmos Dragonball processor based platform.
  5070. \item \file{os2} for \ostwo.
  5071. \item \file{sunos} for the \solaris platform. It has one subdirectory for each of the supported
  5072. processors.
  5073. \item \file{qnx} for the \qnx.
  5074. \item \file{win32} for Win32 platforms.
  5075. \item \file{posix} for posix interfaces (used for easier porting).
  5076. \item \file{unix} for unix common interfaces (used for easier porting).
  5077. \end{itemize}
  5078. \item A processor dependent directory. This contains files that are system
  5079. independent, but processor dependent. It contains mostly optimized routines
  5080. for a specific processor. The following directories exist:
  5081. \begin{itemize}
  5082. \item \file{i386} for the Intel 80x86 series of processors.
  5083. \item \file{m68k} for the Motorola 680x0 series of processors.
  5084. \item \file{powerpc} for the PowerPC processor.
  5085. \item \file{sparc} for the SUN SPARC processor.
  5086. \item \file{x86\_64} for Intel compatible 64-bit processors such as the AMD64.
  5087. \end{itemize}
  5088. \item An OS-independent and Processor independent directory: \file{inc}. This
  5089. contains complete units, and include files containing interface parts of
  5090. units as well as generic versions of processor specific routines.
  5091. \end{enumerate}
  5092. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5093. % Appendix C
  5094. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5095. \chapter{Compiler limits}
  5096. \label{ch:AppC}
  5097. There are certain compiler limits inherent to the compiler:
  5098. \begin{enumerate}
  5099. \item Procedure or Function definitions can be nested to a level of 32. This
  5100. can be changed by changing the \var{maxnesting} constant.
  5101. \item Maximally 1024 units can be used in a program when using the compiler.
  5102. You can change this by redefining the \var{maxunits} constant in the compiler
  5103. source file.
  5104. \item The maximum nesting level of pre-processor macros is 16. This can be
  5105. changed by changing the value of \var{max\_macro\_nesting}.
  5106. \item Arrays are limited to 2 GBytes in size in the default processor mode.
  5107. \end{enumerate}
  5108. For processor specific compiler limitations refer to the Processor
  5109. Limitations section in this guide (\ref{se:ProcessorLimits}).
  5110. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5111. % Appendix D
  5112. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5113. \chapter{Compiler modes}
  5114. \label{ch:AppD}
  5115. Here we list the exact effect of the different compiler modes. They can be
  5116. set with the \var{\$Mode} switch, or by command line switches.
  5117. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5118. % FPC mode
  5119. \section{FPC mode}
  5120. This mode is selected by the \var{{\$MODE FPC}} switch. On the command-line,
  5121. this means that you use none of the other compatibility mode switches.
  5122. It is the default mode of the compiler (\var{-Mfpc}). This means essentially:
  5123. \begin{enumerate}
  5124. \item You must use the address operator to assign procedural variables.
  5125. \item A forward declaration must be repeated exactly the same by the
  5126. implementation of a function/procedure. In particular, you can not omit the
  5127. parameters when implementing the function or procedure.
  5128. \item Overloading of functions is allowed.
  5129. \item Nested comments are allowed.
  5130. \item The Objpas unit is NOT loaded.
  5131. \item You can use the cvar type.
  5132. \item PChars are converted to strings automatically.
  5133. \end{enumerate}
  5134. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5135. % TP mode
  5136. \section{TP mode}
  5137. This mode is selected by the \var{{\$MODE TP}} switch. It tries to emulate,
  5138. as closely as possible, the behavior of Turbo Pascal 7. On the command-line,
  5139. this mode is selected by the \var{-Mtp} switch.
  5140. \begin{enumerate}
  5141. \item Enumeration sizes default to a storage size of 1 byte if there
  5142. are less than 257 elements.
  5143. \item You cannot use the address operator to assign procedural variables.
  5144. \item A forward declaration must not be repeated exactly the same by the
  5145. implementation of a function/procedure. In particular, you can omit the
  5146. parameters when implementing the function or procedure.
  5147. \item Overloading of functions is not allowed.
  5148. \item The Objpas unit is NOT loaded.
  5149. \item Nested comments are not allowed.
  5150. \item You can not use the cvar type.
  5151. \end{enumerate}
  5152. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5153. % Delphi mode.
  5154. \section{Delphi mode}
  5155. This mode is selected by the \var{{\$MODE DELPHI}} switch. It tries to emulate,
  5156. as closely as possible, the behavior of Delphi 4. On the command-line,
  5157. this mode is selected by the \var{-Mdelpih} switch.
  5158. \begin{enumerate}
  5159. \item You can not use the address operator to assign procedural variables.
  5160. \item A forward declaration must not be repeated exactly the same by the
  5161. implementation of a function/procedure. In particular, you not omit the
  5162. parameters when implementing the function or procedure.
  5163. \item Overloading of functions is not allowed.
  5164. \item Nested comments are not allowed.
  5165. \item The Objpas unit is loaded right after the \file{system} unit. One of the
  5166. consequences of this is that the type \var{Integer} is redefined as
  5167. \var{Longint}.
  5168. \item Parameters in class methods can have the same names as class
  5169. properties (although it is bad programming practice).
  5170. \end{enumerate}
  5171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5172. % GPC Mode
  5173. \section{GPC mode}
  5174. This mode is selected by the \var{{\$MODE GPC}} switch. On the command-line,
  5175. this mode is selected by the \var{-Mgpc} switch.
  5176. \begin{enumerate}
  5177. \item You must use the address operator to assign procedural variables.
  5178. \item A forward declaration must not be repeated exactly the same by the
  5179. implementation of a function/procedure. In particular, you can omit the
  5180. parameters when implementing the function or procedure.
  5181. \item Overloading of functions is not allowed.
  5182. \item The Objpas unit is NOT loaded.
  5183. \item Nested comments are not allowed.
  5184. \item You can not use the cvar type.
  5185. \end{enumerate}
  5186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5187. % Objfpc mode
  5188. \section{OBJFPC mode}
  5189. This mode is selected by the \var{{\$MODE OBJFPC}} switch. On the command-line,
  5190. this mode is selected by the \var{-Mobjfpc} switch.
  5191. \begin{enumerate}
  5192. \item You must use the address operator to assign procedural variables.
  5193. \item A forward declaration must be repeated exactly the same by the
  5194. implementation of a function/procedure. In particular, you can not omit the
  5195. parameters when implementing the function or procedure.
  5196. \item Overloading of functions is allowed.
  5197. \item Nested comments are allowed.
  5198. \item The Objpas unit is loaded right after the \file{system} unit. One of the
  5199. consequences of this is that the type \var{Integer} is redefined as
  5200. \var{Longint}.
  5201. \item You can use the cvar type.
  5202. \item PChars are converted to strings automatically.
  5203. \item Parameters in class methods cannot have the same names as class
  5204. properties.
  5205. \end{enumerate}
  5206. \section{MAC mode}
  5207. This mode is selected by the \var{{\$MODE MAC}} switch. On the
  5208. command-line, this mode is selected by the \var{-MMAC} switch. It mainly
  5209. switches on some extra features:
  5210. \begin{enumerate}
  5211. \item Support for the \var{\$SETC} directive.
  5212. \item Support for the \var{\$IFC}, \var{\$ELSEC} and \var{\$ENDC}
  5213. directives.
  5214. \item Support for the \var{UNDEFINED} construct in macros.
  5215. \item Support for \var{TRUE} and \var{FALSE} as values in macro expressions.
  5216. \item Macros may be assigned hexadecimal numbers, like \var{\$2345}.
  5217. \item The \var {Implementation} keyword can be omitted if the implementation
  5218. section is empty.
  5219. \item The \var{cdecl} modifier keyword can be abbreviated to \var{C}.
  5220. \item \var{UNIV} modifer for types in parameter lists is accepted, but is
  5221. otherwise ignored.
  5222. \item \var{...} (ellipsis) is allowed in procedure declarations, is
  5223. functionally equal to the \var{varargs} keyword.
  5224. \item Procedures declared in the interface section which do not have a counterpart
  5225. in the implementation section are considered external (implicit externals).
  5226. In other words, for external procedures, the \var{external} keyword may be
  5227. omitted.
  5228. \end{enumerate}
  5229. (Note: Macros are called 'Compiler Variables' in \macos dialects.)
  5230. Currently, the following \macos pascal extensions are not yet supported in
  5231. \var{MAC} mode:
  5232. \begin{itemize}
  5233. \item A nested procedure cannot be an actual parameter to a procedure.
  5234. \item No anonymous procedure types in formal parameters.
  5235. \item External procedures declared in the interface must have the directive \var{External}.
  5236. \item \var{Continue} instead of \var{Cycle}.
  5237. \item \var{Break} instead of \var{Leave}
  5238. \item \var{Exit} should not have the name of the procedure to exit as parameter.
  5239. Instead, for a function the value to return can be supplied as parameter.
  5240. \item No propagating \var{uses}.
  5241. \item Compiler directives defined in interface sections are not exported.
  5242. \end{itemize}
  5243. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5244. % Appendix E
  5245. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5246. \chapter{Using \file{fpcmake}}
  5247. \label{ch:makefile}
  5248. \newcommand{\mvar}[1]{\var{\$(#1)}}
  5249. \newcommand{\fpcmake}{\file{fpcmake}}
  5250. \section{Introduction}
  5251. \fpc comes with a special makefile tool, \fpcmake{}, which can be
  5252. used to construct a \file{Makefile} for use with \gnu \file{make}.
  5253. All sources from the \fpc team are compiled with this system.
  5254. \fpcmake{} uses a file \file{Makefile.fpc} and constructs a file
  5255. \file{Makefile} from it, based on the settings in \file{Makefile.fpc}.
  5256. The following sections explain what settings can be set in \file{Makefile.fpc},
  5257. what variables are set by \var{fpcmake}, what variables it expects to be set,
  5258. and what targets it defines. After that, some settings in the resulting
  5259. \file{Makefile} are explained.
  5260. \section{Functionality}
  5261. \label{se:fpcmakefunctionality}
  5262. \file{fpcmake} generates a makefile, suitable for GNU \file{make},
  5263. which can be used to
  5264. \begin{enumerate}
  5265. \item Compile units and programs, fit for testing or for final distribution.
  5266. \item Compile example units and programs separately.
  5267. \item Install compiled units and programs in standard locations.
  5268. \item Make archives for distribution of the generated programs and units.
  5269. \item Clean up after compilation and tests.
  5270. \end{enumerate}
  5271. \file{fpcmake} knows how the \fpc compiler operates, which command line
  5272. options it uses, how it searches for files and so on; It uses this knowledge
  5273. to construct sensible command-lines.
  5274. Specifically, it constructs the following targets in the final makefile:
  5275. \begin{description}
  5276. \item[all] Makes all units and programs.
  5277. \item[debug] Makes all units and programs with debug info included.
  5278. \item[smart] Makes all units and programs in smartlinked version.
  5279. \item[examples] Makes all example units and programs.
  5280. \item[shared] Makes all units and programs in shared library version
  5281. (currently disabled)
  5282. \item[install] Installs all units and programs.
  5283. \item[sourceinstall] Installs the sources to the \fpc source tree.
  5284. \item[exampleinstall] Installs any example programs and units.
  5285. \item[distinstall] Installs all units and programs, as well as example units
  5286. and programs.
  5287. \item[zipinstall] Makes an archive of the programs and units which can be
  5288. used to install them on another location, i.e. it makes an archive that can
  5289. be used to distribute the units and programs.
  5290. \item[zipsourceinstall] Makes an archive of the program and unit sources
  5291. which can be used to distribute the sources.
  5292. \item[zipexampleinstall] Makes an archive of the example programs and units
  5293. which can be used to install them on another location, i.e. it makes an
  5294. archive that can be used to distribute the example units and programs.
  5295. \item[zipdistinstall] Makes an archive of both the normal as well as the
  5296. example programs and units. This archive can be used to install them on
  5297. another location, i.e. it makes an archive that can be used to
  5298. distribute.
  5299. \item[clean] Cleans all files that are produced by a compilation.
  5300. \item[distclean] Cleans all files that are produced by a compilation, as
  5301. well as any archives, examples or files left by examples.
  5302. \item[cleanall] Same as clean.
  5303. \item[info] Produces some information on the screen about used programs,
  5304. file and directory locations, where things will go when installing and so
  5305. on.
  5306. \end{description}
  5307. Each of these targets can be highly configured, or even totally overridden
  5308. by the configuration file \file{Makefile.fpc}
  5309. \section{Usage}
  5310. \file {fpcmake} reads a \file{Makefile.fpc} and converts it to a
  5311. \file{Makefile} suitable for reading by \gnu \file{make}
  5312. to compile your projects. It is similar in functionality to GNU
  5313. \file{configure} or \file{Imake} for making X projects.
  5314. \fpcmake{} accepts filenames of makefile description files
  5315. as its command-line arguments. For each of these files it will
  5316. create a \file{Makefile} in the same directory where the file is
  5317. located, overwriting any existing file with that name.
  5318. If no options are given, it just attempts to read the file
  5319. \file{Makefile.fpc} in the current directory and tries to
  5320. construct a \file{Makefile} from it if the \var{-m} option is
  5321. given. Any previously existing \file{Makefile} will be erased.
  5322. if the \var{-p} option is given, instead of a \file{Makefile}, a
  5323. \var{Package.fpc} is generated. A \file{Package.fpc} file describes the
  5324. package and it's dependencies on other packages.
  5325. Additionally, the following command-line options are recognized:
  5326. \begin{description}
  5327. \item[-p] A \file{Package.fpc} file is generated.
  5328. \item[-w] A \file{Makefile} is generated.
  5329. \item[-T targets] Support only specified target systems. \var{Targets} is a
  5330. comma-separated list of targets. Only rule for the specified targets
  5331. will be written.
  5332. \item[-v] Be more verbose.
  5333. \item[-q] be quiet.
  5334. \item[-h] Writes a small help message to the screen.
  5335. \end{description}
  5336. % Makefile.fpc format.
  5337. \section{Format of the configuration file}
  5338. This section describes the rules that can be present in the file
  5339. that is fed to \fpcmake{}.
  5340. The file \file{Makefile.fpc} is a plain ASCII file that contains
  5341. a number of pre-defined sections as in a \windows \file{.ini}-file,
  5342. or a Samba configuration file.
  5343. They look more or less as follows:
  5344. \begin{verbatim}
  5345. [package]
  5346. name=mysql
  5347. version=1.0.5
  5348. [target]
  5349. units=mysql_com mysql_version mysql
  5350. examples=testdb
  5351. [require]
  5352. libc=y
  5353. [install]
  5354. fpcpackage=y
  5355. [default]
  5356. fpcdir=../..
  5357. \end{verbatim}
  5358. The following sections are recognized (in alphabetical order):
  5359. \subsection{clean}
  5360. Specifies rules for cleaning the directory of units and programs.
  5361. The following entries are recognized:
  5362. \begin{description}
  5363. \item[units] names of all units that should be removed when cleaning.
  5364. Don't specify extensions, the makefile will append these by itself.
  5365. \item[files] names of files that should be removed. Specify full filenames.
  5366. \end{description}
  5367. \subsection{compiler}
  5368. In this section values for various compiler options can be specified,
  5369. such as the location of several directories and search paths.
  5370. The following general keywords are recognised:
  5371. \begin{description}
  5372. \item[options] The value of this key will be passed on to the compiler as
  5373. options.
  5374. \item[version] If a specific or minimum compiler version is needed to
  5375. compile the units or programs, then this version should be specified here.
  5376. \end{description}
  5377. The following keys can be used to control the location of the various
  5378. directories used by the compiler:
  5379. \begin{description}
  5380. \item[unitdir]
  5381. A colon-separated list of directories that must be added to the unit
  5382. search path of the compiler.
  5383. \item[librarydir]
  5384. A colon-separated list of directories that must be added to the library
  5385. search path of the compiler.
  5386. \item[objectdir]
  5387. A colon-separated list of directories that must be added to the object file
  5388. search path of the compiler.
  5389. \item[targetdir]
  5390. Specifies the directory where the compiled programs should go.
  5391. \item[sourcedir]
  5392. A space separated list of directories where sources can reside.
  5393. This will be used for the \var{vpath} setting of \gnu \file{make}.
  5394. \item[unittargetdir]
  5395. Specifies the directory where the compiled units should go.
  5396. \item[includedir]
  5397. A colon-separated list of directories that must be added to the include file
  5398. search path of the compiler.
  5399. \item[sourcedir]
  5400. \end{description}
  5401. \subsection{Default}
  5402. The \var{default} section contains some default settings. The following keywords
  5403. are recognized:
  5404. \begin{description}
  5405. \item[cpu]
  5406. Specifies the default target processor for which the \file{Makefile}
  5407. should compile the units and programs. By default this is determined from
  5408. the default compiler processor.
  5409. \item[dir]
  5410. Specifies any subdirectories that make should also descend in and
  5411. make the specified target there as well.
  5412. \item[fpcdir]
  5413. Specifies the directory where all the \fpc source trees reside. Below this
  5414. directory the \file{Makefile} expects to find the \file{rtl}, \file{fcl} and
  5415. \file{packages} directory trees.
  5416. \item[rule] Specifies the default rule to execute. \fpcmake{}
  5417. will make sure that this rule is executed if make is executed without
  5418. arguments, i.e., without an explicit target.
  5419. \item[target]
  5420. Specifies the default operating system target for which the \file{Makefile}
  5421. should compile the units and programs. By default this is determined from
  5422. the default compiler target.
  5423. \end{description}
  5424. \subsection{Dist}
  5425. The \var{Dist} section controls the generation of a distribution package.
  5426. A distribution package is a set of archive files (zip files or tar files
  5427. on unix systems) that can be used to distribute the package.
  5428. The following keys can be placed in this section:
  5429. \begin{description}
  5430. \item[destdir] Specifies the directory where the generated zip files should
  5431. be placed.
  5432. \item[zipname] Name of the archive file to be created. If no zipname is
  5433. specified, this defaults to the package name.
  5434. \item[ziptarget] This is the target that should be executed before the
  5435. archive file is made. This defaults to \var{install}.
  5436. \end{description}
  5437. \subsection{Install}
  5438. Contains instructions for installation of the compiler units and programs. The
  5439. following keywords are recognized:
  5440. \begin{description}
  5441. \item[basedir]
  5442. The directory that is used as the base directory for the installation of
  5443. units. Default this is \var{prefix} appended with \var{/lib/fpc/FPC\_VERSION}
  5444. for \linux or simply the \var{prefix} on other platforms.
  5445. \item[datadir] Directory where data files will be installed, i.e. the files
  5446. specified with the \var{Files} keyword.
  5447. \item[fpcpackage] A boolean key. If this key is specified and equals \var{y},
  5448. the files will be installed as a fpc package under the \fpc units directory,
  5449. i.e. under a separate directory. The directory will be named with the name
  5450. specified in the \var{package} section.
  5451. \item[files] extra data files to be installed in the directory specified
  5452. with the \var{datadir} key.
  5453. \item[prefix] is the directory below which all installs are done.
  5454. This corresponds to the \var{--prefix} argument to \gnu \file{configure}.
  5455. It is used for the installation of programs and units. By default, this is
  5456. \file{/usr} on \linux, and \file{/pp} on all other platforms.
  5457. \item[units] extra units that should be installed, and which are not part of
  5458. the unit targets. The units in the units target will be installed
  5459. automatically.
  5460. \end{description}
  5461. Units will be installed in the subdirectory \file{units/\$(OS\_TARGET)}
  5462. of the \var{dirbase} entry.
  5463. \subsection{Package}
  5464. If a package (i.e. a collection of units that work together) is being
  5465. compiled, then this section is used to keep package information.
  5466. The following information can be stored:
  5467. \begin{description}
  5468. \item[name] The name of the package. When installing it under the package
  5469. directory, this name will be used to create a directory (unless it is
  5470. overridden by one of the installation options)
  5471. \item[version] The version of this package.
  5472. \item[main] If the package is part of another package, this key can be
  5473. specified to indicate which package it is part of.
  5474. \end{description}
  5475. \subsection{Prerules}
  5476. Anything that is in this section will be inserted as-is in the makefile
  5477. \textit{before} the makefile target rules that are generated by fpcmake.
  5478. This means that any variables that are normally defined by fpcmake rules
  5479. should not be used in this section.
  5480. \subsection{Requires}
  5481. This section is used to indicate dependency on external packages (i.e units)
  5482. or tools. The following keywords can be used:
  5483. \begin{description}
  5484. \item[fpcmake] Minimal version of fpcmake that this \file{makefile.fpc} needs.
  5485. \item[packages] Other packages that should be compiled before this package
  5486. can be compiled. Note that this will also add all packages these packages
  5487. depend on to the dependencies of this package. By default, the \fpc Run-Time
  5488. Library is added to this list.
  5489. \item[libc] a boolean value that indicates whether this package needs the C
  5490. library.
  5491. \item[nortl] a boolean that prevents the addition of the \fpc Run-Time
  5492. Library to the required packages.
  5493. \item[unitdir] These directories will be added to the units search path of
  5494. the compiler.
  5495. \item[packagedir] List of package directories. The packages in these
  5496. directories will be made as well before making the current package.
  5497. \item[tools] A list of executables of extra tools that are required. The
  5498. full path to these tools will be defined in the makefile as a variable with
  5499. the same name as the tool name, only in uppercase. For example, the
  5500. following definition:
  5501. \begin{verbatim}
  5502. tools=upx
  5503. \end{verbatim}
  5504. will lead to the definition of a variable with the name \var{UPX} which will
  5505. contain the full path to the \file{upx} executable.
  5506. \end{description}
  5507. \subsection{Rules}
  5508. In this section dependency rules for the units and any other needed
  5509. targets can be inserted. It will be included at the end of the generated
  5510. makefile. Targets or 'default rules' that are defined by \fpcmake{}
  5511. can be inserted here; if they are not present, then \fpcmake{} will generate
  5512. a rule that will call the generic \var{fpc\_} version. For a list of standard
  5513. targets that will be defined by \fpcmake{}, see \sees{fpcmakefunctionality}.
  5514. For example, it is possible to define a target \var{all:}. If it is not
  5515. defined, then \fpcmake{} will generate one which simply calls
  5516. \var{fpc\_all}:
  5517. \begin{verbatim}
  5518. all: fpc_all
  5519. \end{verbatim}
  5520. The \var{fpc\_all} rule will make all targets as defined in the \var{Target}
  5521. section.
  5522. \subsection{Target}
  5523. This is the most important section of the \file{makefile.fpc} file. Here the
  5524. files are defined which should be compiled when the 'all' target is
  5525. executed.
  5526. The following keywords can be used there:
  5527. \begin{description}
  5528. \item[dirs]
  5529. A space separated list of directories where make should also be run.
  5530. \item[exampledirs]
  5531. A space separated list of directories with example programs. The examples
  5532. target will descend in this list of directories as well.
  5533. \item[examples]
  5534. A space separated list of example programs that need to be compiled when
  5535. the user asks to compile the examples. Do not specify an extension,
  5536. the extension will be appended.
  5537. \item[loaders]
  5538. A space separated list of names of assembler files that must be assembled.
  5539. Don't specify the extension, the extension will be appended.
  5540. \item[programs]
  5541. A space separated list of program names that need to be compiled. Do not
  5542. specify an extension, the extension will be appended.
  5543. \item[rsts] a list of \file{rst} files that needs to be converted to \file{.po}
  5544. files for use with \gnu \file{gettext} and internationalization routines.
  5545. \item[units]
  5546. A space separated list of unit names that need to be compiled. Do not
  5547. specify an extension, just the name of the unit as it would appear un a
  5548. \var{uses} clause is sufficient.
  5549. \end{description}
  5550. \section{Programs needed to use the generated makefile}
  5551. At least the following programs are needed by the generated \file{Makefile}
  5552. to function correctly:
  5553. \begin{description}
  5554. \item[cp] a copy program.
  5555. \item[date] a program that prints the date.
  5556. \item[install] a program to install files.
  5557. \item[make] the \file{make} program, obviously.
  5558. \item[pwd] a program that prints the current working directory.
  5559. \item[rm] a program to delete files.
  5560. \item[zip] the zip archiver program. (on dos/windows/OS2 systems only)
  5561. \item[tar] the tar archiver program (on Unix systems only).
  5562. \end{description}
  5563. These are standard programs on \linux systems, with the possible exception of
  5564. \file{make}. For \dos or \windowsnt, they can be found in the
  5565. file \file{makeutil.zip} on the \fpc FTP site.
  5566. The following programs are optionally needed if you use some special targets.
  5567. Which ones you need are controlled by the settings in the \var{tools} section.
  5568. \begin{description}
  5569. \item[cmp] a \dos and \windowsnt file comparer.
  5570. \item[diff] a file comparer.
  5571. \item[ppdep] the ppdep depency lister. Distributed with \fpc.
  5572. \item[ppufiles] the ppufiles unit file dependency lister. Distributed with \fpc.
  5573. \item[ppumove] the \fpc unit mover.
  5574. \item[sed] the \file{sed} program.
  5575. \item[upx] the UPX executable packer.
  5576. \end{description}
  5577. All of these can also be found on the \fpc FTP site for \dos and \windowsnt.
  5578. \file{ppdep,ppufiles} and \file{ppumove} are distributed with the \fpc
  5579. compiler.
  5580. %
  5581. \section{Variables that affect the generated makefile}
  5582. The makefile generated by \fpcmake{} contains a lot of variables.
  5583. Some of them are set in the makefile itself, others can be set and are taken
  5584. into account when set.
  5585. These variables can be split in two groups:
  5586. \begin{itemize}
  5587. \item Directory variables.
  5588. \item Compiler command-line variables.
  5589. \end{itemize}
  5590. Each group will be discussed separately.
  5591. \subsection{Directory variables}
  5592. The first set of variables controls the directories that are
  5593. recognised in the makefile. They should not be set in the
  5594. \file{Makefile.fpc} file, but can be specified on the commandline.
  5595. \begin{description}
  5596. \item[INCDIR] this is a list of directories, separated by spaces, that will
  5597. be added as include directories to the compiler command-line. Each
  5598. directory in the list is prepended with \var{-I} and added to the
  5599. compiler options.
  5600. \item[UNITDIR] this is a list of directories, separated by spaces, that will
  5601. be added as unit search directories to the compiler command-line. Each
  5602. directory in the list is prepended with \var{-Fu} and added to the
  5603. compiler options.
  5604. \item[LIBDIR] is a list of library paths, separated by spaces. Each
  5605. directory in the list is prepended with \var{-Fl} and added to the
  5606. compiler options.
  5607. \item[OBJDIR] is a list of object file directories, separated by spaces, that is
  5608. added to the object files path, i.e. Each directory in the list is prepended with
  5609. \var{-Fo}.
  5610. \end{description}
  5611. \subsection{Compiler command-line variables }
  5612. The following variable can be set on the \file{make} command-line,
  5613. they will be recognised and integrated in the compiler command-line options.:
  5614. \begin{description}
  5615. \item[CREATESMART] If this variable is defined, it tells the compiler to
  5616. create smartlinked units. Adds \var{-CX} to the command-line options.
  5617. \item[DEBUG] If defined, this will cause the compiler to include debug information in
  5618. the generated units and programs. It adds \var{-gl} to the compiler
  5619. command-line, and will define the \var{DEBUG} define.
  5620. \item[LINKSMART] Defining this variable tells the compiler to use smartlinking.
  5621. It adds \var{-XX} to the compiler command-line options.
  5622. \item[OPT] Any options that you want to pass to the compiler. The contents
  5623. of \var{OPT} is simply added to the compiler command-line.
  5624. \item[OPTDEF] Are optional defines, added to the command-line of the
  5625. compiler. They get \var{-d} prepended to them.
  5626. \item[OPTIMIZE] if this variable is defined, this will add \var{-OG2p3} to
  5627. the command-line options.
  5628. \item[RELEASE] if this variable is defined, this will add the \var{-Xs -OG2p3 -n}
  5629. options to the command-line options, and will define the \var{RELEASE} define.
  5630. \item[STRIP] if this variable is defined, this will add the \var{-Xs} option
  5631. to the command-line options.
  5632. \item[VERBOSE] if this variable is defined, then \var{-vnwi} will be added
  5633. to the command-line options.
  5634. \end{description}
  5635. \section{Variables set by \fpcmake{}}
  5636. The makefile generated by \fpcmake{} contains a lot of makefile variables.
  5637. \fpcmake{} will write all of the keys in the \file{makefile.fpc} as
  5638. makefile variables in the form \var{SECTION\_KEYNAME}. This means that the
  5639. following section:
  5640. \begin{verbatim}
  5641. [package]
  5642. name=mysql
  5643. version=1.0.5
  5644. \end{verbatim}
  5645. will result in the following variable definitions:
  5646. \begin{verbatim}
  5647. override PACKAGE_NAME=mysql
  5648. override PACKAGE_VERSION=1.0.5
  5649. \end{verbatim}
  5650. Most targets and rules are constructed using these variables. They will be
  5651. listed below, together with other variables that are defined by \fpcmake{}.
  5652. The following sets of variables are defined:
  5653. \begin{itemize}
  5654. \item Directory variables.
  5655. \item Program names.
  5656. \item File extensions.
  5657. \item Target files.
  5658. \end{itemize}
  5659. Each of these sets is discussed in the subsequent:
  5660. \subsection{Directory variables}
  5661. The following compiler directories are defined by the makefile:
  5662. \begin{description}
  5663. \item[BASEDIR] is set to the current directory if the \file{pwd} command is
  5664. available. If not, it is set to '.'.
  5665. \item[COMPILER\_INCDIR] is a space-separated list of library paths. Each
  5666. directory in the list is prepended with \var{-Fl} and added to the
  5667. compiler options. Set by the \var{incdir} keyword in the \var{Compiler} section.
  5668. \item[COMPILER\_LIBDIR] is a space-separated list of library paths. Each
  5669. directory in the list is prepended with \var{-Fl} and added to the
  5670. compiler options. Set by the \var{libdir} keyword in the \var{Compiler} section.
  5671. \item[COMPILER\_OBJDIR] is a list of object file directories, separated by
  5672. spaces. Each directory in the list is prepended with \var{-Fo} and
  5673. added to the compiler options.
  5674. Set by the \var{objdir} keyword in the \var{Compiler} section.
  5675. \item[COMPILER\_TARGETDIR] This directory is added as the output directory of
  5676. the compiler, where all units and executables are written, i.e. it gets
  5677. \var{-FE} prepended. It is set by the \var{targtdir} keyword in the
  5678. \var{Compiler} section.
  5679. \item[COMPILER\_TARGETUNITDIR] If set, this directory is added as the output directory of
  5680. the compiler, where all units and executables are written, i.e. it gets
  5681. \var{-FU} prepended.It is set by the \var{targtdir} keyword in the
  5682. \var{Dirs} section.
  5683. \item[COMPILER\_UNITDIR] is a list of unit directories, separated by spaces.
  5684. Each directory in the list is prepended with \var{-Fu} and is added to the
  5685. compiler options.
  5686. Set by the \var{unitdir} keyword in the \var{Compiler} section.
  5687. \item[GCCLIBDIR] (\linux only) is set to the directory where \file{libgcc.a}
  5688. is. If \var{needgcclib} is set to \var{True} in the \var{Libs} section, then
  5689. this directory is added to the compiler commandline with \var{-Fl}.
  5690. \item[OTHERLIBDIR] is a space-separated list of library paths. Each
  5691. directory in the list is prepended with \var{-Fl} and added to the
  5692. compiler options. If it is not defined on linux, then the contents of
  5693. the \file{/etc/ld.so.conf} file is added.
  5694. \end{description}
  5695. The following directories are used for installs:
  5696. \begin{description}
  5697. \item[INSTALL\_BASEDIR] is the base for all directories where units are
  5698. installed. By default, On \linux, this is set to
  5699. \mvar{INSTALL\_PREFIX}\var{/lib/fpc/}\mvar{RELEASEVER}.\\ On other systems,
  5700. it is set to \mvar{PREFIXINSTALLDIR}. You can also set it with the
  5701. \var{basedir} variable in the \var{Install} section.
  5702. \item[INSTALL\_BINDIR] is set to \mvar{INSTALL\_BASEDIR}/\var{bin} on \linux,
  5703. and\\ \mvar{INSTALL\_BASEDIR}/\var{bin}/\mvar{OS\_TARGET} on other systems.
  5704. This is the place where binaries are installed.
  5705. \item[INSTALL\_DATADIR] The directory where data files are installed. Set by
  5706. the \var{Data} key in the \var{Install} section.
  5707. \item[INSTALL\_LIBDIR] is set to \mvar{INSTALL\_PREFIX}/lib on \linux,\\
  5708. and \mvar{INSTALL\_UNITDIR} on other systems.
  5709. \item[INSTALL\_PREFIX] is set to \file{/usr/local} on \linux, \file{/pp} on \dos
  5710. or \windowsnt. Set by the \var{prefix} keyword in the \var{Install}
  5711. section.
  5712. \item[INSTALL\_UNITDIR] is where units will be installed. This is set to\\
  5713. \mvar{INSTALL\_BASEDIR}/units/\mvar{OS\_TARGET}. If the units are compiled
  5714. as a package, \mvar{PACKAGE\_NAME} is added to the directory.
  5715. \end{description}
  5716. \subsection{Target variables}
  5717. The second set of variables controls the targets that are constructed
  5718. by the makefile. They are created by \fpcmake{}, so you can use
  5719. them in your rules, but you shouldn't assign values to them yourself.
  5720. \begin{description}
  5721. \item[TARGET\_DIRS] This is the list of directories that make will descend
  5722. into when compiling. Set by the \var{Dirs} key in the \var{Target} section?
  5723. \item[TARGET\_EXAMPLES] The list of examples programs that must be compiled.
  5724. Set by the \var{examples} key in the \var{Target} section.
  5725. \item[TARGET\_EXAMPLEDIRS] the list of directories that make will descend
  5726. into when compiling examples. Set by the \var{exampledirs} key in the
  5727. \var{Target} section.
  5728. \item[TARGET\_LOADERS] is a list of space-separated names that identify
  5729. loaders to be compiled. This is mainly used in the compiler's RTL sources.
  5730. It is set by the \var{loaders} keyword in the \var{Targets} section.
  5731. \item[TARGET\_PROGRAMS] This is a list of executable names that will be compiled.
  5732. the makefile appends \mvar{EXEEXT} to these names. It is set by the
  5733. \var{programs} keyword in the \var{Target} section.
  5734. \item[TARGET\_UNITS] This is a list of unit names that will be compiled. The
  5735. makefile appends \mvar{PPUEXT} to each of these names to form the unit file
  5736. name. The sourcename is formed by adding \mvar{PASEXT}.
  5737. It is set by the \var{units} keyword in the \var{Target} section.
  5738. \item[ZIPNAME] is the name of the archive that will be created by the
  5739. makefile.
  5740. It is set by the \var{zipname} keyword in the \var{Zip} section.
  5741. \item[ZIPTARGET] is the target that is built before the archive is made.
  5742. this target is built first. If successful, the zip archive will be made.
  5743. It is set by the \var{ziptarget} keyword in the \var{Zip} section.
  5744. \end{description}
  5745. \subsection{Compiler command-line variables}
  5746. The following variables control the compiler command-line:
  5747. \begin{description}
  5748. \item[CPU\_SOURCE] the target CPU type is added as a define to the compiler
  5749. command line. This is determined by the Makefile itself.
  5750. \item[CPU\_TARGET] the target CPU type is added as a define to the compiler
  5751. command line. This is determined by the Makefile itself.
  5752. \item[OS\_SOURCE] What platform the makefile is used on. Detected
  5753. automatically.
  5754. \item[OS\_TARGET] What platform will be compiled for. Added to the
  5755. compiler command-line with a \var{-T} prepended.
  5756. \end{description}
  5757. \subsection{Program names}
  5758. The following variables are program names, used in makefile targets.
  5759. \begin{description}
  5760. \item[AS] The assembler. Default set to \file{as}.
  5761. \item[COPY] a file copy program. Default set to \file{cp -fp}.
  5762. \item[COPYTREE] a directory tree copy program. Default set to \file{cp -frp}.
  5763. \item[CMP] a program to compare files. Default set to \var{cmp}.
  5764. \item[DEL] a file removal program. Default set to \file{rm -f}.
  5765. \item[DELTREE] a directory removal program. Default set to \file{rm -rf}.
  5766. \item[DATE] a program to display the date.
  5767. \item[DIFF] a program to produce diff files.
  5768. \item[ECHO] an echo program.
  5769. \item[FPC] the Free Pascal compiler executable. Default set to
  5770. \var{ppc386.exe}
  5771. \item[INSTALL] a program to install files. Default set to \file{install -m
  5772. 644} on \linux.
  5773. \item[INSTALLEXE] a program to install executable files. Default set to \file{install -m
  5774. 755} on \linux.
  5775. \item[LD] The linker. Default set to \file{ld}.
  5776. \item[LDCONFIG] (\linux only) the program used to update the loader cache.
  5777. \item[MKDIR] a program to create directories if they don't exist yet. Default
  5778. set to \file{install -m 755 -d}
  5779. \item[MOVE] a file move program. Default set to \file{mv -f}
  5780. \item[PP] the Free Pascal compiler executable. Default set to
  5781. \var{ppc386.exe}
  5782. \item[PPAS] the name of the shell script created by the compiler if the
  5783. \var{-s} option is specified. This command will be executed after
  5784. compilation, if the \var{-s} option was detected among the options.
  5785. \item[PPUMOVE] the program to move units into one big unit library.
  5786. \item[PWD] the pwd program.
  5787. \item[SED] a stream-line editor program. Default set to \file{sed}.
  5788. \item[UPX] an executable packer to compress your executables into
  5789. self-extracting compressed executables.
  5790. \item[ZIPPROG] a zip program to compress files. zip targets are made with
  5791. this program
  5792. \end{description}
  5793. \subsection{File extensions}
  5794. The following variables denote extensions of files. These variables include
  5795. the \var{.} (dot) of the extension. They are appended to object names.
  5796. \begin{description}
  5797. \item[ASMEXT] is the extension of assembler files produced by the compiler.
  5798. \item[LOADEREXT] is the extension of the assembler files that make up the
  5799. executable startup code.
  5800. \item[OEXT] is the extension of the object files that the compiler creates.
  5801. \item[PACKAGESUFFIX] is a suffix that is appended to package names in zip
  5802. targets. This serves so packages can be made for different OSes.
  5803. \item[PPLEXT] is the extension of shared library unit files.
  5804. \item[PPUEXT] is the extension of default units.
  5805. \item[RSTEXT] is the extension of the \file{.rst} resource string files.
  5806. \item[SHAREDLIBEXT] is the extension of shared libraries.
  5807. \item[SMARTEXT] is the extension of smartlinked unit assembler files.
  5808. \item[STATICLIBEXT] is the extension of static libraries.
  5809. \end{description}
  5810. \subsection{Target files}
  5811. The following variables are defined to make targets and rules easier:
  5812. \begin{description}
  5813. \item[COMPILER] is the complete compiler commandline, with all options
  5814. added, after all \file{Makefile} variables have been examined.
  5815. \item[DATESTR] contains the date.
  5816. \item[UNITPPUFILES] a list of unit files that will be made. This is just
  5817. the list of unit objects, with the correct unit extension appended.
  5818. \end{description}
  5819. \section{Rules and targets created by \fpcmake{}}
  5820. The \var{makefile.fpc} defines a series of targets, which can be called by
  5821. your own targets. They have names that resemble default names (such as
  5822. 'all', 'clean'), only they have \var{fpc\_} prepended.
  5823. \subsection{Pattern rules}
  5824. The makefile makes the following pattern rules:
  5825. \begin{description}
  5826. \item[units] how to make a pascal unit form a pascal source file.
  5827. \item[executables] how to make an executable from a pascal source file.
  5828. \item[object file] how to make an object file from an assembler file.
  5829. \end{description}
  5830. \subsection{Build rules}
  5831. The following build targets are defined:
  5832. \begin{description}
  5833. \item[fpc\_all] target that builds all units and executables as well as
  5834. loaders. If \var{DEFAULTUNITS} is defined, executables are excluded from the
  5835. targets.
  5836. \item[fpc\_debug] the same as \var{fpc\_all}, only with debug information
  5837. included.
  5838. \item[fpc\_exes] target to make all executables in \var{EXEOBJECTS}.
  5839. \item[fpc\_loaders] target to make all files in \var{LOADEROBJECTS}.
  5840. \item[fpc\_packages] target to make all packages that are needed to make
  5841. the files.
  5842. \item[fpc\_shared] target that makes all units as dynamic libraries.
  5843. \item[fpc\_smart] target that makes all units as smartlinked units.
  5844. \item[fpc\_units] target to make all units in \var{UNITOBJECTS}.
  5845. \end{description}
  5846. \subsection{Cleaning rules}
  5847. The following cleaning targets are defined:
  5848. \begin{description}
  5849. \item[fpc\_clean] cleans all files that result when \var{fpc\_all} was made.
  5850. \item[fpc\_distclean] is the same as both previous target commands, but also
  5851. deletes all object, unit and assembler files that are present.
  5852. \end{description}
  5853. \subsection{archiving rules}
  5854. The following archiving targets are defined:
  5855. \begin{description}
  5856. \item[fpc\_zipdistinstall] Target to make a distribution install of the
  5857. package.
  5858. \item[fpc\_zipinstall] Target to make an install zip of the compiled units
  5859. of the package.
  5860. \item[fpc\_zipexampleinstall] Target to make a zip of the example files.
  5861. \item[fpc\_zipsourceinstall] Target to make a zip of the source files.
  5862. \end{description}
  5863. The zip is made uzing the \var{ZIPEXE} program. Under \linux, a
  5864. \file{.tar.gz} file is created.
  5865. \subsection{Installation rules}
  5866. \begin{description}
  5867. \item[fpc\_distinstall] target which calls the \var{install} and
  5868. \var{exampleinstall} targets.
  5869. \item[fpc\_install] target to install the units.
  5870. \item[fpc\_sourceinstall] target to install the sources (in case a
  5871. distribution is made)
  5872. \item[fpc\_exampleinstall] target to install the examples. (in case
  5873. a distribution is made)
  5874. \end{description}
  5875. \subsection{Informative rules}
  5876. There is only one target which produces information about the used
  5877. variables, rules and targets: \var{fpc\_info}.
  5878. The following information about the makefile is presented:
  5879. \begin{itemize}
  5880. \item general configuration information: the location of
  5881. the makefile, the compiler version, target OS, CPU.
  5882. \item the directories, used by the compiler.
  5883. \item all directories where files will be installed.
  5884. \item all objects that will be made.
  5885. \item all defined tools.
  5886. \end{itemize}
  5887. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5888. % Appendix F
  5889. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5890. \chapter{Compiling the compiler}
  5891. \label{ch:AppF}
  5892. \section{Introduction}
  5893. The \fpc team releases at intervals a completely prepared package, with
  5894. compiler and units all ready to use, the so-called releases. After a
  5895. release, work on the compiler continues, bugs are fixed and features are
  5896. added. The \fpc team doesn't make a new release whenever they change
  5897. something in the compiler, instead the sources are available for anyone to
  5898. use and compile. Compiled versions of RTL and compiler are also made daily,
  5899. and put on the web.
  5900. There are, nevertheless, circumstances when the compiler must be recompiled
  5901. manually. When changes are made to compiler code, or when the compiler is
  5902. downloaded through CVS.
  5903. There are essentially 2 ways of recompiling the compiler: by hand, or using
  5904. the makefiles. Each of these methods will be discussed.
  5905. \section{Before starting}
  5906. To compile the compiler easily, it is best to keep the following directory
  5907. structure (a base directory of \file{/pp/src} is supposed, but that may be
  5908. different):
  5909. \begin{verbatim}
  5910. /pp/src/Makefile
  5911. /makefile.fpc
  5912. /rtl/linux
  5913. /inc
  5914. /i386
  5915. /...
  5916. /compiler
  5917. \end{verbatim}
  5918. When the makefiles should be used, the above directory tree must be used.
  5919. The compiler and rtl source are zipped in such a way that when both are
  5920. unzipped in the same directory (\file{/pp/src} in the above) the above
  5921. directory tree results.
  5922. There are 2 ways to start compiling the compiler and RTL. Both ways must be
  5923. used, depending on the situation. Usually, the RTL must be compiled first,
  5924. before compiling the compiler, after which the compiler is compiled using
  5925. the current compiler. In some special cases the compiler must be compiled
  5926. first, with a previously compiled RTL.
  5927. How to decide which should be compiled first? In general, the answer is that
  5928. the RTL should be compiled first. There are 2 exceptions to this rule:
  5929. \begin{enumerate}
  5930. \item The first case is when some of the internal routines in the RTL
  5931. have changed, or if new internal routines appeared. Since the OLD compiler
  5932. doesn't know about these changed internal routines, it will emit function
  5933. calls that are based on the old compiled RTL, and hence are not correct.
  5934. Either the result will not link, or the binary will give errors.
  5935. \item The second case is when something is added to the RTL that the
  5936. compiler needs to know about (a new default assembler mechanism, for
  5937. example).
  5938. \end{enumerate}
  5939. How to know if one of these things has occurred? There is no way to know,
  5940. except by mailing the \fpc team. When the compiler cannot be recompiled
  5941. when first compiling the RTL, then try the other way.
  5942. \section{Compiling using \file{make}}
  5943. When compiling with \var{make} it is necessary to have the above directory
  5944. structure. Compiling the compiler is achieved with the target \var{cycle}.
  5945. Under normal circumstances, recompiling the compiler is limited to the
  5946. following instructions (assuming you start in directory \file{/pp/src}):
  5947. \begin{verbatim}
  5948. cd compiler
  5949. make cycle
  5950. \end{verbatim}
  5951. This will work only if the \file{makefile} is installed correctly and
  5952. if the needed tools are present in the \var{PATH}. Which tools must be
  5953. installed can be found in appendix \ref{ch:makefile}.
  5954. The above instructions will do the following:
  5955. \begin{enumerate}
  5956. \item Using the current compiler, the RTL is compiled in the correct
  5957. directory, which is determined by the OS. e.g. under \linux,
  5958. the RTL is compiled in directory \file{rtl/linux}.
  5959. \item The compiler is compiled using the newly compiled RTL. If successful,
  5960. the newly compiled compiler executable is copied to a temporary executable.
  5961. \item Using the temporary executable from the previous step, the RTL is
  5962. re-compiled.
  5963. \item Using the temporary executable and the newly compiled RTL from the
  5964. last step, the compiler is compiled again.
  5965. \end{enumerate}
  5966. The last two steps are repeated 3 times, until three passes have been made or
  5967. until the generated compiler binary is equal to the binary it was compiled
  5968. with. This process ensures that the compiler binary is correct.
  5969. Compiling for another target:
  5970. When compiling the compiler for another target, it is necessary to specify
  5971. the \var{OS\_TARGET} makefile variable. It can be set to the following
  5972. values: \var{win32}, \var{go32v2}, \var{os2} and \var{linux}.
  5973. As an example, cross-compilation for the go32v2 target from the win32 target
  5974. is chosen:
  5975. \begin{verbatim}
  5976. cd compiler
  5977. make cycle OS_TARGET=go32v2
  5978. \end{verbatim}
  5979. This will compile the go32v2 RTL, and compile a \var{go32v2} compiler.
  5980. When compiling a new compiler and the compiler should be compiled using an
  5981. existing compiled RTL, the \var{all} target must be used, and another RTL
  5982. directory than the default (which is the \file{../rtl/\$(OS\_TARGET)}
  5983. directory) must be indicated. For instance, assuming that the compiled RTL
  5984. units are in \var{/pp/rtl}, typing
  5985. \begin{verbatim}
  5986. cd compiler
  5987. make clean
  5988. make all UNITDIR=/pp/rtl
  5989. \end{verbatim}
  5990. should use the RTL from the \var{/pp/rtl} dirrectory.
  5991. This will then compile the compiler using the RTL units in \file{/pp/rtl}.
  5992. After this has been done, the 'make cycle' can be used, starting with this
  5993. compiler:
  5994. \begin{verbatim}
  5995. make cycle PP=./ppc386
  5996. \end{verbatim}
  5997. This will do the \var{make cycle} from above, but will start with the compiler
  5998. that was generated by the \var{make all} instruction.
  5999. In all cases, many options can be passed to \var{make} to influence the
  6000. compile process. In general, the makefiles add any needed compiler options
  6001. to the command-line, so that the RTL and compiler can be compiled.
  6002. Additional options (e.g. optimization options) can be specified by passing them in
  6003. \var{OPT}.
  6004. \section{Compiling by hand}
  6005. Compiling by hand is difficult and tedious, but can be done. The
  6006. compilation of RTL and compiler will be treated separately.
  6007. \subsection{Compiling the RTL}
  6008. To recompile the RTL, so a new compiler can be built, at least the following
  6009. units must be built, in the order specified:
  6010. \begin{enumerate}
  6011. \item[loaders] the program stubs, that are the startup code for each pascal
  6012. program. These files have the \file{.as} extension, because they are written
  6013. in assembler. They must be assembled with the \gnu \file{as} assembler. These stubs
  6014. are in the OS-dependent directory, except for \linux, where they are in a
  6015. processor dependent subdirectory of the \linux directory (\file{i386} or
  6016. \file{m68k}).
  6017. \item[system] the \file{system} unit. This unit is named differently on different
  6018. systems:
  6019. \begin{itemize}
  6020. \item Only on GO32v2, it's called \file{system}.
  6021. \item For \linux it's called \file{syslinux}.
  6022. \item For \windowsnt it's called \file{syswin32}.
  6023. \item For \ostwo it's called \file{sysos2}
  6024. \end{itemize}
  6025. This unit resides in the OS-dependent subdirectories of the RTL.
  6026. \item[strings] The strings unit. This unit resides in the \file{inc}
  6027. subdirectory of the RTL.
  6028. \item[dos] The \file{dos} unit. It resides in the OS-dependent subdirectory
  6029. of the RTL. Possibly other units will be compiled as a consequence of trying
  6030. to compile this unit (e.g. on \linux, the \file{linux} unit will be
  6031. compiled, on go32, the \file{go32} unit will be compiled).
  6032. \item[objects] the objects unit. It resides in the \file{inc} subdirectory
  6033. of the RTL.
  6034. \end{enumerate}
  6035. To compile these units on a i386, the following statements will do:
  6036. \begin{verbatim}
  6037. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 -Us -Sg syslinux.pp
  6038. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/strings.pp
  6039. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 dos.pp
  6040. ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/objects.pp
  6041. \end{verbatim}
  6042. These are the minimum command-line options, needed to compile the RTL.
  6043. For another processor, the \var{i386} should be changed into the appropriate
  6044. processor. For another operating system (target) the
  6045. \file{syslinux} should be changed in the appropriate system unit file,
  6046. and the target OS setting (\var{-T}) must be set accordingly.
  6047. Depending on the target OS there are other units that can be
  6048. compiled, but which are not strictly needed to recompile the compiler.
  6049. The following units are available for all plaforms:
  6050. \begin{description}
  6051. \item[objpas] Needed for Delphi mode. Needs \var{-S2} as an option. Resides
  6052. in the \file{objpas} subdirectory.
  6053. \item[sysutils] many utility functions, like in Delphi. Resides in the
  6054. \file{objpas} directory, and needs \var{-S2} to compile.
  6055. \item[typinfo] functions to access RTTI information, like Delphi. Resides in
  6056. the \file{objpas} directory.
  6057. \item[math] math functions like in Delphi. Resides in the \file{objpas}
  6058. directory.
  6059. \item[mmx] extensions for MMX class Intel processors. Resides in
  6060. in the \file{i386} directory.
  6061. \item[getopts] a GNU compatible getopts unit. resides in the \file{inc}
  6062. directory.
  6063. \item[heaptrc] to debug the heap. resides in the \file{inc} directory.
  6064. \end{description}
  6065. \subsection{Compiling the compiler}
  6066. Compiling the compiler can be done with one statement. It's always best to
  6067. remove all units from the compiler directory first, so something like
  6068. \begin{verbatim}
  6069. rm *.ppu *.o
  6070. \end{verbatim}
  6071. on \linux, and on \dos
  6072. \begin{verbatim}
  6073. del *.ppu
  6074. del *.o
  6075. \end{verbatim}
  6076. After this, the compiler can be compiled with the following command-line:
  6077. \begin{verbatim}
  6078. ppc386 -Tlinux -Fu../rtl/linux -di386 -dGDB pp.pas
  6079. \end{verbatim}
  6080. So, the minimum options are:
  6081. \begin{enumerate}
  6082. \item The target OS. Can be skipped when compiling for the same target as
  6083. the compiler which is being used.
  6084. \item A path to an RTL. Can be skipped if a correct fpc.cfg configuration
  6085. is on the system. If the compiler should be compiled with the RTL that was
  6086. compiled first, this should be \file{../rtl/OS} (replace the OS with the
  6087. appropriate operating system subdirectory of the RTL).
  6088. \item A define with the processor for which the compiler is compiled for. Required.
  6089. \item \var{-dGDB}. Required.
  6090. \item \var{-Sg} is needed, some parts of the compiler use \var{goto}
  6091. statements (to be specific: the scanner).
  6092. \end{enumerate}
  6093. So the absolute minimal command line is
  6094. \begin{verbatim}
  6095. ppc386 -di386 -dGDB -Sg pp.pas
  6096. \end{verbatim}
  6097. Some other command-line options can be used, but the above are the
  6098. minimum. A list of recognised options can be found in \seet{FPCdefines}.
  6099. \begin{FPCltable}{ll}{Possible defines when compiling FPC}{FPCdefines}
  6100. Define & does what \\ \hline
  6101. TP & Needed to compile the compiler with Turbo or Borland Pascal. \\
  6102. GDB & Support of the GNU Debugger (required switch). \\
  6103. I386 & Generate a compiler for the Intel i386+ processor family. \\
  6104. M68K & Generate a compiler for the M680x0 processor family. \\
  6105. EXTDEBUG & Some extra debug code is executed. \\
  6106. MEMDEBUG & Some memory usage information is displayed. \\
  6107. SUPPORT\_MMX & only i386: enables the compiler switch \var{MMX} which \\
  6108. &allows the compiler to generate MMX instructions.\\
  6109. EXTERN\_MSG & Don't compile the msgfiles in the compiler, always use \\
  6110. & external messagefiles (default for TP).\\
  6111. LOGSECONDPASS & Write compiler node information in assembler output. \\
  6112. NOOPT & Do not include the optimizer in the compiler. \\
  6113. \hline
  6114. \end{FPCltable}
  6115. This list may be subject to change, the source file \file{pp.pas} always
  6116. contains an up-to-date list.
  6117. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6118. % Appendix G : Compiler defines
  6119. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6120. \chapter{Compiler defines during compilation}
  6121. \label{ch:AppG}
  6122. This appendix describes the possible defines when compiling programs
  6123. using \fpc. A brief explanation of the define, and when it is used
  6124. is also given.
  6125. \begin{FPCltable}{ll}{Possible defines when compiling using FPC}{FPCOutdefines}
  6126. Define & description \\ \hline
  6127. FPC\_LINK\_DYNAMIC & Defined when the output will be linked dynamically.\\
  6128. & This is defined when using the -XD compiler switch.\\
  6129. FPC\_LINK\_STATIC & Defined when the output will be linked statically.\\
  6130. & This is the default mode. \\
  6131. FPC\_LINK\_SMART & Defined when the output will be smartlinked.\\
  6132. & This is defined when using the -XX compiler switch.\\
  6133. FPC\_PROFILE & Defined when profiling code is added to program.\\
  6134. & This is defined when using the -pg compiler switch.\\
  6135. FPK & Always defined for \fpc. \\
  6136. FPC & Always defined for \fpc. \\
  6137. VER1 & Always defined for \fpc version 1.x.x. \\
  6138. VER1\_0 & Always defined for \fpc version 1.0.x. \\
  6139. ENDIAN\_LITTLE & Defined when the \fpc target is a little-endian processor \\
  6140. & (80x86, Alpha, ARM). \\
  6141. ENDIAN\_BIG & Defined when the \fpc target is a big-endian processor \\
  6142. & (680x0, PowerPC, SPARC, MIPS). \\
  6143. FPC\_DELPHI & \fpc is in Delphi mode, either using compiler switch -Sd or \\
  6144. & using the \var{\$MODE DELPHI} directive. \\
  6145. FPC\_OBJFPC & \fpc is in Delphi mode, either using compiler switch -S2 or \\
  6146. & using the \var{\$MODE OBJFPC} directive. \\
  6147. FPC\_TP & \fpc is in Turbo Pascal mode, either using compiler switch -So or \\
  6148. & using the \var{\$MODE TP} directive. \\
  6149. FPC\_GPC & \fpc is in GNU Pascal mode, either using compiler switch -Sp or \\
  6150. & using the \var{\$MODE GPC} directive. \\
  6151. \end{FPCltable}
  6152. \begin{remark}
  6153. The \var{ENDIAN\_LITTLE} and \var{ENDIAN\_BIG} defines were added
  6154. starting from \fpc version 1.0.5.
  6155. \end{remark}
  6156. \begin{FPCltable}{ll}{Possible CPU defines when compiling using FPC}{FPCCPUdefines}
  6157. Define & When defined? \\ \hline
  6158. CPU86 & \fpc target is an Intel 80x86 or compatible.\\
  6159. CPU87 & \fpc target is an Intel 80x86 or compatible.\\
  6160. CPUI386 & \fpc target is an Intel 80386 or later.\\
  6161. CPU68k & \fpc target is a Motorola 680x0 or compatible.\\
  6162. CPUM68020 & \fpc target is a Motorola 68020 or later.\\
  6163. CPU68 & \fpc target is a Motorola 680x0 or compatible.\\
  6164. CPUSPARC & \fpc target is a SPARC v7 or compatible.\\
  6165. CPUALPHA & \fpc target is an Alpha AXP or compatible.\\
  6166. CPUPOWERPC & \fpc target is a 32-bit PowerPC or compatible.\\
  6167. \end{FPCltable}
  6168. \begin{FPCltable}{ll}{Possible defines when compiling using target OS}{FPCOSdefines}
  6169. Target operating system & Defines \\ \hline
  6170. linux & LINUX, UNIX\\
  6171. freebsd & FREEBSD, BSD, UNIX\\
  6172. netbsd & NETBSD, BSD, UNIX\\
  6173. sunos & SUNOS, SOLARIS, UNIX\\
  6174. go32v2 & GO32V2, DPMI\\
  6175. os2 & OS2\\
  6176. Windows 32-bit & WIN32\\
  6177. Classic Amiga & AMIGA\\
  6178. Atari TOS & ATARI\\
  6179. Classic Macintosh & MAC\\
  6180. PalmOS & PALMOS\\
  6181. BeOS & BEOS, UNIX\\
  6182. QNX RTP & QNX, UNIX
  6183. \end{FPCltable}
  6184. \begin{remark}
  6185. The \unix define was added starting from \fpc version 1.0.5. The BSD
  6186. operating systems no longer define LINUX starting with version 1.0.7.
  6187. \end{remark}
  6188. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6189. % Appendix H : Operating system specific behavior
  6190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6191. <<<<<<< prog.tex
  6192. \chapter{Stack configuration}
  6193. \label{ch:AppH}
  6194. This gives some important information on stack settings under
  6195. the different operating systems. It might be important when
  6196. porting applications to other operating systems.
  6197. \section{DOS}
  6198. Under the DOS targets, the default stack is set to 256 kB.
  6199. This can be modified with the GO32V2 target using a special
  6200. DJGPP utility \file{stubedit}. It is to note that the stack
  6201. size may be enlarged with the compiler switch (\var{-Cs}).
  6202. If the size specified with \var{-Cs} is \emph{greater}
  6203. than the default stack size, it will be used instead,
  6204. otherwise the default stack size is used.
  6205. \section{Linux}
  6206. Under \linux, stack size is only limited by the available memory of
  6207. the system.
  6208. \section{Netbsd}
  6209. Under \netbsd, stack size is only limited by the available memory of
  6210. the system.
  6211. \section{Freebsd}
  6212. Under \freebsd, stack size is only limited by the available memory of
  6213. the system.
  6214. \section{BeOS}
  6215. Under \beos, stack size is fixed at 256Kb. It currently
  6216. cannot be changed, it is recommended to turn on stack
  6217. checking when compiling for this target platform.
  6218. \section{Windows}
  6219. Under \windows, stack size is only limited by the available memory of
  6220. the system.
  6221. \section{OS/2}
  6222. Under \ostwo, stack size is specified at a default
  6223. value of 8 Mbytes. This currently cannot be changed
  6224. directly.
  6225. \section{Amiga}
  6226. Under AmigaOS, stack size is determined by the user, which sets this
  6227. value using the stack program. Typical sizes range from 4 kB to 40 kB.
  6228. The stack size currently cannot be changed, it is recommended to turn
  6229. on stack checking when compiling for this target platform.
  6230. \section{Atari}
  6231. Under Atari TOS, stack size is currently limited to 8 kB.
  6232. The stack size currently cannot be changed, it is recommended to turn
  6233. on stack checking when compiling for this target platform.
  6234. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6235. % Appendix I : Operating system specific behavior
  6236. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6237. \chapter{Operating system specific behavior}
  6238. \label{ch:AppH}
  6239. This appendix describes some special behaviors which vary
  6240. from operating system to operating system. This is described
  6241. in \seet{OSBehave}. The GCC saved registers indicates what
  6242. registers are saved when certain declaration modifiers are used.
  6243. \begin{FPCltable}{lcr}{Operating system specific behavior}{OSBehave}
  6244. Operating systems & Min. param. stack align & GCC saved registers\\
  6245. \hline
  6246. Amiga & 2 & D2..D7,A2..A5\\
  6247. Atari & 2 & D2..D7,A2..A5\\
  6248. BeOS-x86 & 4 & ESI, EDI, EBX\\
  6249. DOS & 2 & ESI, EDI, EBX\\
  6250. FreeBSD & 4 & ESI, EDI, EBX\\
  6251. linux-m68k & & D2..D7,A2..A5\\
  6252. linux-x86 & 4 & ESI, EDI, EBX\\
  6253. MacOS-68k & & D2..D7,A2..A5\\
  6254. NetBSD-x86 & & ESI, EDI, EBX\\
  6255. NetBSD-m68k & & D2..D7,A2..A5\\
  6256. OS/2 & 4 & ESI, EDI, EBX\\
  6257. PalmOS & 2 & D2..D7,A2..A5\\
  6258. QNX-x86 & & ESI, EDI, EBX\\
  6259. Solaris-x86 & 4 & ESI, EDI, EBX\\
  6260. Win32 & 4 & ESI, EDI, EBX\\
  6261. \hline
  6262. \end{FPCltable}
  6263. \end{document}