prog.tex 264 KB

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