prog.tex 223 KB

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