prog.tex 203 KB

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