prog.tex 222 KB

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