prog.tex 220 KB

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